2 * Asterisk -- A telephony toolkit for Linux.
4 * Microsoft WAV File Format using libaudiofile
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/channel.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/sched.h>
18 #include <asterisk/module.h>
19 #include <arpa/inet.h>
26 #include <audiofile.h>
29 /* Read 320 samples at a time, max */
30 #define WAV_MAX_SIZE 320
32 /* Fudge in milliseconds */
35 struct ast_filestream {
36 /* First entry MUST be reserved for the channel type */
37 void *reserved[AST_RESERVED_POINTERS];
38 /* This is what a filestream means to us */
39 int fd; /* Descriptor */
44 struct ast_channel *owner;
45 struct ast_filestream *next;
46 struct ast_frame fr; /* Frame information */
47 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
48 short samples[WAV_MAX_SIZE];
52 static struct ast_filestream *glist = NULL;
53 static pthread_mutex_t wav_lock = PTHREAD_MUTEX_INITIALIZER;
54 static int glistcnt = 0;
56 static char *name = "wav";
57 static char *desc = "Microsoft WAV format (PCM/16, 8000Hz mono)";
58 static char *exts = "wav";
60 static struct ast_filestream *wav_open(int fd)
62 /* We don't have any header to read or anything really, but
63 if we did, it would go here. We also might want to check
64 and be sure it's a valid file. */
65 struct ast_filestream *tmp;
69 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
70 tmp->afs = afNewFileSetup();
72 ast_log(LOG_WARNING, "Unable to create file setup\n");
76 afInitFileFormat(tmp->afs, AF_FILE_WAVE);
77 tmp->af = afOpenFD(fd, "r", tmp->afs);
79 afFreeFileSetup(tmp->afs);
80 ast_log(LOG_WARNING, "Unable to open file descriptor\n");
85 afGetFileFormat(tmp->af, &version);
86 if (version != AF_FILE_WAVE) {
87 ast_log(LOG_WARNING, "This is not a wave file (%d)\n", version);
91 /* Read the format and make sure it's exactly what we seek. */
92 if (afGetChannels(tmp->af, AF_DEFAULT_TRACK) != 1) {
93 ast_log(LOG_WARNING, "Invalid number of channels %d. Should be mono (1)\n", afGetChannels(tmp->af, AF_DEFAULT_TRACK));
96 afGetSampleFormat(tmp->af, AF_DEFAULT_TRACK, &fmt, &width);
97 if (fmt != AF_SAMPFMT_TWOSCOMP) {
98 ast_log(LOG_WARNING, "Input file is not signed\n");
101 rate = afGetRate(tmp->af, AF_DEFAULT_TRACK);
102 if ((rate < 7900) || (rate > 8100)) {
103 ast_log(LOG_WARNING, "Rate %f is not close enough to 8000 Hz\n", rate);
107 ast_log(LOG_WARNING, "Input file is not 16-bit\n");
111 afCloseFile(tmp->af);
112 afFreeFileSetup(tmp->afs);
116 if (pthread_mutex_lock(&wav_lock)) {
117 afCloseFile(tmp->af);
118 afFreeFileSetup(tmp->afs);
119 ast_log(LOG_WARNING, "Unable to lock wav list\n");
127 tmp->fr.data = tmp->samples;
128 tmp->fr.frametype = AST_FRAME_VOICE;
129 tmp->fr.subclass = AST_FORMAT_SLINEAR;
130 /* datalen will vary for each frame */
133 tmp->lasttimeout = -1;
135 pthread_mutex_unlock(&wav_lock);
136 ast_update_use_count();
141 static struct ast_filestream *wav_rewrite(int fd, char *comment)
143 /* We don't have any header to read or anything really, but
144 if we did, it would go here. We also might want to check
145 and be sure it's a valid file. */
146 struct ast_filestream *tmp;
147 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
148 tmp->afs = afNewFileSetup();
150 ast_log(LOG_WARNING, "Unable to create file setup\n");
155 afInitFileFormat(tmp->afs, AF_FILE_WAVE);
157 afInitChannels(tmp->afs, AF_DEFAULT_TRACK, 1);
158 /* Signed linear, 16-bit */
159 afInitSampleFormat(tmp->afs, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
161 afInitRate(tmp->afs, AF_DEFAULT_TRACK, (double)8000.0);
162 tmp->af = afOpenFD(fd, "w", tmp->afs);
164 afFreeFileSetup(tmp->afs);
165 ast_log(LOG_WARNING, "Unable to open file descriptor\n");
169 if (pthread_mutex_lock(&wav_lock)) {
170 ast_log(LOG_WARNING, "Unable to lock wav list\n");
178 tmp->lasttimeout = -1;
180 pthread_mutex_unlock(&wav_lock);
181 ast_update_use_count();
183 ast_log(LOG_WARNING, "Out of memory\n");
187 static struct ast_frame *wav_read(struct ast_filestream *s)
192 static void wav_close(struct ast_filestream *s)
194 struct ast_filestream *tmp, *tmpl = NULL;
195 if (pthread_mutex_lock(&wav_lock)) {
196 ast_log(LOG_WARNING, "Unable to lock wav list\n");
203 tmpl->next = tmp->next;
213 s->owner->stream = NULL;
214 if (s->owner->streamid > -1)
215 ast_sched_del(s->owner->sched, s->owner->streamid);
216 s->owner->streamid = -1;
218 pthread_mutex_unlock(&wav_lock);
219 ast_update_use_count();
221 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
222 afCloseFile(tmp->af);
223 afFreeFileSetup(tmp->afs);
228 static int ast_read_callback(void *data)
230 u_int32_t delay = -1;
233 struct ast_filestream *s = data;
234 /* Send a frame from the file to the appropriate channel */
236 if ((res = afReadFrames(s->af, AF_DEFAULT_TRACK, s->samples, sizeof(s->samples)/2)) < 1) {
238 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
239 s->owner->streamid = -1;
242 /* Per 8 samples, one milisecond */
244 s->fr.frametype = AST_FRAME_VOICE;
245 s->fr.subclass = AST_FORMAT_SLINEAR;
246 s->fr.offset = AST_FRIENDLY_OFFSET;
247 s->fr.datalen = res * 2;
248 s->fr.data = s->samples;
250 s->fr.timelen = delay;
251 /* Unless there is no delay, we're going to exit out as soon as we
252 have processed the current frame. */
253 /* If there is a delay, lets schedule the next event */
254 if (delay != s->lasttimeout) {
255 /* We'll install the next timeout now. */
256 s->owner->streamid = ast_sched_add(s->owner->sched,
258 ast_read_callback, s);
260 s->lasttimeout = delay;
262 /* Just come back again at the same time */
265 /* Lastly, process the frame */
266 if (ast_write(s->owner, &s->fr)) {
267 ast_log(LOG_WARNING, "Failed to write frame\n");
268 s->owner->streamid = -1;
275 static int wav_apply(struct ast_channel *c, struct ast_filestream *s)
277 /* Select our owner for this stream, and get the ball rolling. */
279 ast_read_callback(s);
283 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
286 if (f->frametype != AST_FRAME_VOICE) {
287 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
290 if (f->subclass != AST_FORMAT_SLINEAR) {
291 ast_log(LOG_WARNING, "Asked to write non-signed linear frame (%d)!\n", f->subclass);
294 if ((res = afWriteFrames(fs->af, AF_DEFAULT_TRACK, f->data, f->datalen/2)) != f->datalen/2) {
295 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
301 static char *wav_getcomment(struct ast_filestream *s)
308 return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
322 struct ast_filestream *tmp, *tmpl;
323 if (pthread_mutex_lock(&wav_lock)) {
324 ast_log(LOG_WARNING, "Unable to lock wav list\n");
330 ast_softhangup(tmp->owner);
335 pthread_mutex_unlock(&wav_lock);
336 return ast_format_unregister(name);
342 if (pthread_mutex_lock(&wav_lock)) {
343 ast_log(LOG_WARNING, "Unable to lock wav list\n");
347 pthread_mutex_unlock(&wav_lock);