Version 0.1.3 from FTP
[asterisk/asterisk.git] / formats / format_wav.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Microsoft WAV File Format using libaudiofile 
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
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>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <pthread.h>
26 #include <audiofile.h>
27
28
29 /* Read 320 samples at a time, max */ 
30 #define WAV_MAX_SIZE 320
31
32 /* Fudge in milliseconds */
33 #define WAV_FUDGE 2
34
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 */
40         /* Audio File */
41         AFfilesetup afs;
42         AFfilehandle af;
43         int lasttimeout;
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];
49 };
50
51
52 static struct ast_filestream *glist = NULL;
53 static pthread_mutex_t wav_lock = PTHREAD_MUTEX_INITIALIZER;
54 static int glistcnt = 0;
55
56 static char *name = "wav";
57 static char *desc = "Microsoft WAV format (PCM/16, 8000Hz mono)";
58 static char *exts = "wav";
59
60 static struct ast_filestream *wav_open(int fd)
61 {
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;
66         int notok = 0;
67         int fmt, width;
68         double rate;
69         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
70                 tmp->afs = afNewFileSetup();
71                 if (!tmp->afs) {
72                         ast_log(LOG_WARNING, "Unable to create file setup\n");
73                         free(tmp);
74                         return NULL;
75                 }
76                 afInitFileFormat(tmp->afs, AF_FILE_WAVE);
77                 tmp->af = afOpenFD(fd, "r", tmp->afs);
78                 if (!tmp->af) {
79                         afFreeFileSetup(tmp->afs);
80                         ast_log(LOG_WARNING, "Unable to open file descriptor\n");
81                         free(tmp);
82                         return NULL;
83                 }
84 #if 0
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);
88                         notok++;
89                 }
90 #endif
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));
94                         notok++;
95                 }
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");
99                         notok++;
100                 }
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);
104                         notok++;
105                 }
106                 if (width != 16) {
107                         ast_log(LOG_WARNING, "Input file is not 16-bit\n");
108                         notok++;
109                 }
110                 if (notok) {
111                         afCloseFile(tmp->af);
112                         afFreeFileSetup(tmp->afs);
113                         free(tmp);
114                         return NULL;
115                 }
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");
120                         free(tmp);
121                         return NULL;
122                 }
123                 tmp->next = glist;
124                 glist = tmp;
125                 tmp->fd = fd;
126                 tmp->owner = NULL;
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 */
131                 tmp->fr.src = name;
132                 tmp->fr.mallocd = 0;
133                 tmp->lasttimeout = -1;
134                 glistcnt++;
135                 pthread_mutex_unlock(&wav_lock);
136                 ast_update_use_count();
137         }
138         return tmp;
139 }
140
141 static struct ast_filestream *wav_rewrite(int fd, char *comment)
142 {
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();
149                 if (!tmp->afs) {
150                         ast_log(LOG_WARNING, "Unable to create file setup\n");
151                         free(tmp);
152                         return NULL;
153                 }
154                 /* WAV format */
155                 afInitFileFormat(tmp->afs, AF_FILE_WAVE);
156                 /* Mono */
157                 afInitChannels(tmp->afs, AF_DEFAULT_TRACK, 1);
158                 /* Signed linear, 16-bit */
159                 afInitSampleFormat(tmp->afs, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
160                 /* 8000 Hz */
161                 afInitRate(tmp->afs, AF_DEFAULT_TRACK, (double)8000.0);
162                 tmp->af = afOpenFD(fd, "w", tmp->afs);
163                 if (!tmp->af) {
164                         afFreeFileSetup(tmp->afs);
165                         ast_log(LOG_WARNING, "Unable to open file descriptor\n");
166                         free(tmp);
167                         return NULL;
168                 }
169                 if (pthread_mutex_lock(&wav_lock)) {
170                         ast_log(LOG_WARNING, "Unable to lock wav list\n");
171                         free(tmp);
172                         return NULL;
173                 }
174                 tmp->next = glist;
175                 glist = tmp;
176                 tmp->fd = fd;
177                 tmp->owner = NULL;
178                 tmp->lasttimeout = -1;
179                 glistcnt++;
180                 pthread_mutex_unlock(&wav_lock);
181                 ast_update_use_count();
182         } else
183                 ast_log(LOG_WARNING, "Out of memory\n");
184         return tmp;
185 }
186
187 static struct ast_frame *wav_read(struct ast_filestream *s)
188 {
189         return NULL;
190 }
191
192 static void wav_close(struct ast_filestream *s)
193 {
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");
197                 return;
198         }
199         tmp = glist;
200         while(tmp) {
201                 if (tmp == s) {
202                         if (tmpl)
203                                 tmpl->next = tmp->next;
204                         else
205                                 glist = tmp->next;
206                         break;
207                 }
208                 tmpl = tmp;
209                 tmp = tmp->next;
210         }
211         glistcnt--;
212         if (s->owner) {
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;
217         }
218         pthread_mutex_unlock(&wav_lock);
219         ast_update_use_count();
220         if (!tmp) 
221                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
222         afCloseFile(tmp->af);
223         afFreeFileSetup(tmp->afs);
224         close(s->fd);
225         free(s);
226 }
227
228 static int ast_read_callback(void *data)
229 {
230         u_int32_t delay = -1;
231         int retval = 0;
232         int res;
233         struct ast_filestream *s = data;
234         /* Send a frame from the file to the appropriate channel */
235
236         if ((res = afReadFrames(s->af, AF_DEFAULT_TRACK, s->samples, sizeof(s->samples)/2)) < 1) {
237                 if (res)
238                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
239                 s->owner->streamid = -1;
240                 return 0;
241         }
242         /* Per 8 samples, one milisecond */
243         delay = res / 8;
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;
249         s->fr.mallocd = 0;
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, 
257                                                                                           delay, 
258                                                                                           ast_read_callback, s);
259                 
260                 s->lasttimeout = delay;
261         } else {
262                 /* Just come back again at the same time */
263                 retval = -1;
264         }
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;
269                 return 0;
270         }
271         
272         return retval;
273 }
274
275 static int wav_apply(struct ast_channel *c, struct ast_filestream *s)
276 {
277         /* Select our owner for this stream, and get the ball rolling. */
278         s->owner = c;
279         ast_read_callback(s);
280         return 0;
281 }
282
283 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
284 {
285         int res;
286         if (f->frametype != AST_FRAME_VOICE) {
287                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
288                 return -1;
289         }
290         if (f->subclass != AST_FORMAT_SLINEAR) {
291                 ast_log(LOG_WARNING, "Asked to write non-signed linear frame (%d)!\n", f->subclass);
292                 return -1;
293         }
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));
296                 return -1;
297         }       
298         return 0;
299 }
300
301 static char *wav_getcomment(struct ast_filestream *s)
302 {
303         return NULL;
304 }
305
306 int load_module()
307 {
308         return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
309                                                                 wav_open,
310                                                                 wav_rewrite,
311                                                                 wav_apply,
312                                                                 wav_write,
313                                                                 wav_read,
314                                                                 wav_close,
315                                                                 wav_getcomment);                                                                
316                                                                 
317                                                                 
318 }
319
320 int unload_module()
321 {
322         struct ast_filestream *tmp, *tmpl;
323         if (pthread_mutex_lock(&wav_lock)) {
324                 ast_log(LOG_WARNING, "Unable to lock wav list\n");
325                 return -1;
326         }
327         tmp = glist;
328         while(tmp) {
329                 if (tmp->owner)
330                         ast_softhangup(tmp->owner);
331                 tmpl = tmp;
332                 tmp = tmp->next;
333                 free(tmpl);
334         }
335         pthread_mutex_unlock(&wav_lock);
336         return ast_format_unregister(name);
337 }       
338
339 int usecount()
340 {
341         int res;
342         if (pthread_mutex_lock(&wav_lock)) {
343                 ast_log(LOG_WARNING, "Unable to lock wav list\n");
344                 return -1;
345         }
346         res = glistcnt;
347         pthread_mutex_unlock(&wav_lock);
348         return res;
349 }
350
351 char *description()
352 {
353         return desc;
354 }
355