2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Anthony Minessale
5 * Anthony Minessale (anthmct@yahoo.com)
7 * See http://www.asterisk.org for more information about
8 * the Asterisk project. Please do not directly contact
9 * any of the maintainers of this project for assistance;
10 * the project provides a web site, mailing lists and IRC
11 * channels for your use.
13 * This program is free software, distributed under the terms of
14 * the GNU General Public License Version 2. See the LICENSE file
15 * at the top of the source tree.
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/lock.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/sched.h"
42 #include "asterisk/module.h"
43 #include "asterisk/endian.h"
45 #define BUF_SIZE 320 /* 320 samples */
47 struct ast_filestream {
48 void *reserved[AST_RESERVED_POINTERS];
49 /* This is what a filestream means to us */
50 FILE *f; /* Descriptor */
51 struct ast_channel *owner;
52 struct ast_frame fr; /* Frame information */
53 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
54 char empty; /* Empty character */
55 unsigned char buf[BUF_SIZE]; /* Output Buffer */
60 AST_MUTEX_DEFINE_STATIC(slinear_lock);
61 static int glistcnt = 0;
63 static char *name = "sln";
64 static char *desc = "Raw Signed Linear Audio support (SLN)";
65 static char *exts = "sln|raw";
67 static struct ast_filestream *slinear_open(FILE *f)
69 /* We don't have any header to read or anything really, but
70 if we did, it would go here. We also might want to check
71 and be sure it's a valid file. */
72 struct ast_filestream *tmp;
73 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
74 memset(tmp, 0, sizeof(struct ast_filestream));
75 if (ast_mutex_lock(&slinear_lock)) {
76 ast_log(LOG_WARNING, "Unable to lock slinear list\n");
81 tmp->fr.data = tmp->buf;
82 tmp->fr.frametype = AST_FRAME_VOICE;
83 tmp->fr.subclass = AST_FORMAT_SLINEAR;
84 /* datalen will vary for each frame */
88 ast_mutex_unlock(&slinear_lock);
89 ast_update_use_count();
94 static struct ast_filestream *slinear_rewrite(FILE *f, const char *comment)
96 /* We don't have any header to read or anything really, but
97 if we did, it would go here. We also might want to check
98 and be sure it's a valid file. */
99 struct ast_filestream *tmp;
100 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
101 memset(tmp, 0, sizeof(struct ast_filestream));
102 if (ast_mutex_lock(&slinear_lock)) {
103 ast_log(LOG_WARNING, "Unable to lock slinear list\n");
109 ast_mutex_unlock(&slinear_lock);
110 ast_update_use_count();
112 ast_log(LOG_WARNING, "Out of memory\n");
116 static void slinear_close(struct ast_filestream *s)
118 if (ast_mutex_lock(&slinear_lock)) {
119 ast_log(LOG_WARNING, "Unable to lock slinear list\n");
123 ast_mutex_unlock(&slinear_lock);
124 ast_update_use_count();
130 static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext)
134 /* Send a frame from the file to the appropriate channel */
136 s->fr.frametype = AST_FRAME_VOICE;
137 s->fr.subclass = AST_FORMAT_SLINEAR;
138 s->fr.offset = AST_FRIENDLY_OFFSET;
141 if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
143 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
146 s->fr.samples = res/2;
148 delay = s->fr.samples;
153 static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
156 if (f->frametype != AST_FRAME_VOICE) {
157 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
160 if (f->subclass != AST_FORMAT_SLINEAR) {
161 ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass);
164 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
165 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
171 static int slinear_seek(struct ast_filestream *fs, long sample_offset, int whence)
173 off_t offset=0,min,cur,max;
178 fseek(fs->f, 0, SEEK_END);
180 if (whence == SEEK_SET)
181 offset = sample_offset;
182 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
183 offset = sample_offset + cur;
184 else if (whence == SEEK_END)
185 offset = max - sample_offset;
186 if (whence != SEEK_FORCECUR) {
187 offset = (offset > max)?max:offset;
189 /* always protect against seeking past begining. */
190 offset = (offset < min)?min:offset;
191 return fseek(fs->f, offset, SEEK_SET) / 2;
194 static int slinear_trunc(struct ast_filestream *fs)
196 return ftruncate(fileno(fs->f), ftell(fs->f));
199 static long slinear_tell(struct ast_filestream *fs)
202 offset = ftell(fs->f);
206 static char *slinear_getcomment(struct ast_filestream *s)
213 return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
229 return ast_format_unregister(name);
245 return ASTERISK_GPL_KEY;