Version 0.3.0 from FTP
[asterisk/asterisk.git] / formats / format_wav.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Work with WAV in the proprietary Microsoft format.
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/lock.h>
15 #include <asterisk/channel.h>
16 #include <asterisk/file.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/sched.h>
19 #include <asterisk/module.h>
20 #include <arpa/inet.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <endian.h>
29
30 /* Some Ideas for this code came from makewave.c by Jeffery Chilton */
31
32 /* Portions of the conversion code are by guido@sienanet.it */
33
34 struct ast_filestream {
35         void *reserved[AST_RESERVED_POINTERS];
36         /* This is what a filestream means to us */
37         int fd; /* Descriptor */
38         int bytes;
39         int needsgain;
40         struct ast_channel *owner;
41         struct ast_frame fr;                            /* Frame information */
42         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
43         char empty;                                                     /* Empty character */
44         short buf[160]; 
45         int foffset;
46         int lasttimeout;
47         struct timeval last;
48         int adj;
49         struct ast_filestream *next;
50 };
51
52
53 static struct ast_filestream *glist = NULL;
54 static pthread_mutex_t wav_lock = AST_MUTEX_INITIALIZER;
55 static int glistcnt = 0;
56
57 static char *name = "wav";
58 static char *desc = "Microsoft WAV format (8000hz Signed Linear)";
59 static char *exts = "wav";
60
61 #define BLOCKSIZE 160
62
63 #define GAIN 2          /* 2^GAIN is the multiple to increase the volume by */
64
65 #if __BYTE_ORDER == __LITTLE_ENDIAN
66 #define htoll(b) (b)
67 #define htols(b) (b)
68 #define ltohl(b) (b)
69 #define ltohs(b) (b)
70 #else
71 #if __BYTE_ORDER == __BIG_ENDIAN
72 #define htoll(b)  \
73           (((((b)      ) & 0xFF) << 24) | \
74                ((((b) >>  8) & 0xFF) << 16) | \
75                    ((((b) >> 16) & 0xFF) <<  8) | \
76                    ((((b) >> 24) & 0xFF)      ))
77 #define htols(b) \
78           (((((b)      ) & 0xFF) << 8) | \
79                    ((((b) >> 8) & 0xFF)      ))
80 #define ltohl(b) htoll(b)
81 #define ltohs(b) htols(b)
82 #else
83 #error "Endianess not defined"
84 #endif
85 #endif
86
87
88 static int check_header(int fd)
89 {
90         int type, size, formtype;
91         int fmt, hsize;
92         short format, chans, bysam, bisam;
93         int bysec;
94         int freq;
95         int data;
96         if (read(fd, &type, 4) != 4) {
97                 ast_log(LOG_WARNING, "Read failed (type)\n");
98                 return -1;
99         }
100         if (read(fd, &size, 4) != 4) {
101                 ast_log(LOG_WARNING, "Read failed (size)\n");
102                 return -1;
103         }
104         size = ltohl(size);
105         if (read(fd, &formtype, 4) != 4) {
106                 ast_log(LOG_WARNING, "Read failed (formtype)\n");
107                 return -1;
108         }
109         if (memcmp(&type, "RIFF", 4)) {
110                 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
111                 return -1;
112         }
113         if (memcmp(&formtype, "WAVE", 4)) {
114                 ast_log(LOG_WARNING, "Does not contain WAVE\n");
115                 return -1;
116         }
117         if (read(fd, &fmt, 4) != 4) {
118                 ast_log(LOG_WARNING, "Read failed (fmt)\n");
119                 return -1;
120         }
121         if (memcmp(&fmt, "fmt ", 4)) {
122                 ast_log(LOG_WARNING, "Does not say fmt\n");
123                 return -1;
124         }
125         if (read(fd, &hsize, 4) != 4) {
126                 ast_log(LOG_WARNING, "Read failed (formtype)\n");
127                 return -1;
128         }
129         if (ltohl(hsize) != 16) {
130                 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
131                 return -1;
132         }
133         if (read(fd, &format, 2) != 2) {
134                 ast_log(LOG_WARNING, "Read failed (format)\n");
135                 return -1;
136         }
137         if (ltohs(format) != 1) {
138                 ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
139                 return -1;
140         }
141         if (read(fd, &chans, 2) != 2) {
142                 ast_log(LOG_WARNING, "Read failed (format)\n");
143                 return -1;
144         }
145         if (ltohs(chans) != 1) {
146                 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
147                 return -1;
148         }
149         if (read(fd, &freq, 4) != 4) {
150                 ast_log(LOG_WARNING, "Read failed (freq)\n");
151                 return -1;
152         }
153         if (ltohl(freq) != 8000) {
154                 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
155                 return -1;
156         }
157         /* Ignore the byte frequency */
158         if (read(fd, &bysec, 4) != 4) {
159                 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
160                 return -1;
161         }
162         /* Check bytes per sample */
163         if (read(fd, &bysam, 2) != 2) {
164                 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
165                 return -1;
166         }
167         if (ltohs(bysam) != 2) {
168                 ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
169                 return -1;
170         }
171         if (read(fd, &bisam, 2) != 2) {
172                 ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
173                 return -1;
174         }
175         /* Begin data chunk */
176         if (read(fd, &data, 4) != 4) {
177                 ast_log(LOG_WARNING, "Read failed (data)\n");
178                 return -1;
179         }
180         if (memcmp(&data, "data", 4)) {
181                 ast_log(LOG_WARNING, "Does not say data\n");
182                 return -1;
183         }
184         /* Ignore the data length */
185         if (read(fd, &data, 4) != 4) {
186                 ast_log(LOG_WARNING, "Read failed (data)\n");
187                 return -1;
188         }
189         return 0;
190 }
191
192 static int update_header(int fd)
193 {
194         off_t cur,end;
195         int datalen,filelen,bytes;
196         
197         
198         cur = lseek(fd, 0, SEEK_CUR);
199         end = lseek(fd, 0, SEEK_END);
200         /* data starts 44 bytes in */
201         bytes = end - 44;
202         datalen = htoll(bytes);
203         /* chunk size is bytes of data plus 36 bytes of header */
204         filelen = htoll(36 + bytes);
205         
206         if (cur < 0) {
207                 ast_log(LOG_WARNING, "Unable to find our position\n");
208                 return -1;
209         }
210         if (lseek(fd, 4, SEEK_SET) != 4) {
211                 ast_log(LOG_WARNING, "Unable to set our position\n");
212                 return -1;
213         }
214         if (write(fd, &filelen, 4) != 4) {
215                 ast_log(LOG_WARNING, "Unable to set write file size\n");
216                 return -1;
217         }
218         if (lseek(fd, 40, SEEK_SET) != 40) {
219                 ast_log(LOG_WARNING, "Unable to set our position\n");
220                 return -1;
221         }
222         if (write(fd, &datalen, 4) != 4) {
223                 ast_log(LOG_WARNING, "Unable to set write datalen\n");
224                 return -1;
225         }
226         if (lseek(fd, cur, SEEK_SET) != cur) {
227                 ast_log(LOG_WARNING, "Unable to return to position\n");
228                 return -1;
229         }
230         return 0;
231 }
232
233 static int write_header(int fd)
234 {
235         unsigned int hz=htoll(8000);
236         unsigned int bhz = htoll(16000);
237         unsigned int hs = htoll(16);
238         unsigned short fmt = htols(1);
239         unsigned short chans = htols(1);
240         unsigned short bysam = htols(2);
241         unsigned short bisam = htols(16);
242         unsigned int size = htoll(0);
243         /* Write a wav header, ignoring sizes which will be filled in later */
244         lseek(fd,0,SEEK_SET);
245         if (write(fd, "RIFF", 4) != 4) {
246                 ast_log(LOG_WARNING, "Unable to write header\n");
247                 return -1;
248         }
249         if (write(fd, &size, 4) != 4) {
250                 ast_log(LOG_WARNING, "Unable to write header\n");
251                 return -1;
252         }
253         if (write(fd, "WAVEfmt ", 8) != 8) {
254                 ast_log(LOG_WARNING, "Unable to write header\n");
255                 return -1;
256         }
257         if (write(fd, &hs, 4) != 4) {
258                 ast_log(LOG_WARNING, "Unable to write header\n");
259                 return -1;
260         }
261         if (write(fd, &fmt, 2) != 2) {
262                 ast_log(LOG_WARNING, "Unable to write header\n");
263                 return -1;
264         }
265         if (write(fd, &chans, 2) != 2) {
266                 ast_log(LOG_WARNING, "Unable to write header\n");
267                 return -1;
268         }
269         if (write(fd, &hz, 4) != 4) {
270                 ast_log(LOG_WARNING, "Unable to write header\n");
271                 return -1;
272         }
273         if (write(fd, &bhz, 4) != 4) {
274                 ast_log(LOG_WARNING, "Unable to write header\n");
275                 return -1;
276         }
277         if (write(fd, &bysam, 2) != 2) {
278                 ast_log(LOG_WARNING, "Unable to write header\n");
279                 return -1;
280         }
281         if (write(fd, &bisam, 2) != 2) {
282                 ast_log(LOG_WARNING, "Unable to write header\n");
283                 return -1;
284         }
285         if (write(fd, "data", 4) != 4) {
286                 ast_log(LOG_WARNING, "Unable to write header\n");
287                 return -1;
288         }
289         if (write(fd, &size, 4) != 4) {
290                 ast_log(LOG_WARNING, "Unable to write header\n");
291                 return -1;
292         }
293         return 0;
294 }
295
296 static struct ast_filestream *wav_open(int fd)
297 {
298         /* We don't have any header to read or anything really, but
299            if we did, it would go here.  We also might want to check
300            and be sure it's a valid file.  */
301         struct ast_filestream *tmp;
302         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
303                 memset(tmp, 0, sizeof(struct ast_filestream));
304                 if (check_header(fd)) {
305                         free(tmp);
306                         return NULL;
307                 }
308                 if (ast_pthread_mutex_lock(&wav_lock)) {
309                         ast_log(LOG_WARNING, "Unable to lock wav list\n");
310                         free(tmp);
311                         return NULL;
312                 }
313                 tmp->next = glist;
314                 glist = tmp;
315                 tmp->fd = fd;
316                 tmp->owner = NULL;
317                 tmp->needsgain = 1;
318                 tmp->fr.data = tmp->buf;
319                 tmp->fr.frametype = AST_FRAME_VOICE;
320                 tmp->fr.subclass = AST_FORMAT_SLINEAR;
321                 /* datalen will vary for each frame */
322                 tmp->fr.src = name;
323                 tmp->fr.mallocd = 0;
324                 tmp->lasttimeout = -1;
325                 tmp->bytes = 0;
326                 glistcnt++;
327                 ast_pthread_mutex_unlock(&wav_lock);
328                 ast_update_use_count();
329         }
330         return tmp;
331 }
332
333 static struct ast_filestream *wav_rewrite(int fd, char *comment)
334 {
335         /* We don't have any header to read or anything really, but
336            if we did, it would go here.  We also might want to check
337            and be sure it's a valid file.  */
338         struct ast_filestream *tmp;
339         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
340                 memset(tmp, 0, sizeof(struct ast_filestream));
341                 if (write_header(fd)) {
342                         free(tmp);
343                         return NULL;
344                 }
345                 if (ast_pthread_mutex_lock(&wav_lock)) {
346                         ast_log(LOG_WARNING, "Unable to lock wav list\n");
347                         free(tmp);
348                         return NULL;
349                 }
350                 tmp->next = glist;
351                 glist = tmp;
352                 tmp->fd = fd;
353                 tmp->owner = NULL;
354                 tmp->lasttimeout = -1;
355                 glistcnt++;
356                 ast_pthread_mutex_unlock(&wav_lock);
357                 ast_update_use_count();
358         } else
359                 ast_log(LOG_WARNING, "Out of memory\n");
360         return tmp;
361 }
362
363 static struct ast_frame *wav_read(struct ast_filestream *s)
364 {
365         return NULL;
366 }
367
368 static void wav_close(struct ast_filestream *s)
369 {
370         struct ast_filestream *tmp, *tmpl = NULL;
371         char zero = 0;
372         if (ast_pthread_mutex_lock(&wav_lock)) {
373                 ast_log(LOG_WARNING, "Unable to lock wav list\n");
374                 return;
375         }
376         tmp = glist;
377         while(tmp) {
378                 if (tmp == s) {
379                         if (tmpl)
380                                 tmpl->next = tmp->next;
381                         else
382                                 glist = tmp->next;
383                         break;
384                 }
385                 tmpl = tmp;
386                 tmp = tmp->next;
387         }
388         glistcnt--;
389         if (s->owner) {
390                 s->owner->stream = NULL;
391                 if (s->owner->streamid > -1)
392                         ast_sched_del(s->owner->sched, s->owner->streamid);
393                 s->owner->streamid = -1;
394         }
395         ast_pthread_mutex_unlock(&wav_lock);
396         ast_update_use_count();
397         if (!tmp) 
398                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
399         /* Pad to even length */
400         if (s->bytes & 0x1)
401                 write(s->fd, &zero, 1);
402         close(s->fd);
403         free(s);
404         s = NULL;
405 #if 0
406         printf("bytes = %d\n", s->bytes);
407 #endif
408 }
409
410 static int ast_read_callback(void *data)
411 {
412         int retval = 0;
413         int res;
414         int delay;
415         int x;
416         struct ast_filestream *s = data;
417         short tmp[sizeof(s->buf) / 2];
418         /* Send a frame from the file to the appropriate channel */
419         
420         if ( (res = read(s->fd, tmp, sizeof(tmp))) <= 0 ) {
421                 if (res) {
422                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
423                 }
424                 s->owner->streamid = -1;
425                 return 0;
426         }
427
428 #if __BYTE_ORDER == __BIG_ENDIAN
429         for( x = 0; x < sizeof(tmp)/2; x++) tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
430 #endif
431
432         if (s->needsgain) {
433                 for (x=0;x<sizeof(tmp)/2;x++)
434                         if (tmp[x] & ((1 << GAIN) - 1)) {
435                                 /* If it has data down low, then it's not something we've artificially increased gain
436                                    on, so we don't need to gain adjust it */
437                                 s->needsgain = 0;
438                         }
439         }
440         if (s->needsgain) {
441                 for (x=0;x<sizeof(tmp)/2;x++) {
442                         s->buf[x] = tmp[x] >> GAIN;
443                 }
444         } else {
445                 memcpy(s->buf, tmp, sizeof(s->buf));
446         }
447                         
448         delay = res / 2;
449         s->fr.frametype = AST_FRAME_VOICE;
450         s->fr.subclass = AST_FORMAT_SLINEAR;
451         s->fr.offset = AST_FRIENDLY_OFFSET;
452         s->fr.datalen = res;
453         s->fr.data = s->buf;
454         s->fr.mallocd = 0;
455         s->fr.samples = delay;
456         delay /= 8;
457         /* Lastly, process the frame */ 
458         if (delay != s->lasttimeout) {
459                 s->owner->streamid = ast_sched_add(s->owner->sched, delay, ast_read_callback, s);
460                 s->lasttimeout = delay;
461         } else {
462                 retval = -1;
463         }
464         
465         
466         if (ast_write(s->owner, &s->fr)) {
467                 ast_log(LOG_WARNING, "Failed to write frame\n");
468                 s->owner->streamid = -1;
469                 return 0;
470         }
471         
472         return retval;
473 }
474
475 static int wav_apply(struct ast_channel *c, struct ast_filestream *s)
476 {
477         /* Select our owner for this stream, and get the ball rolling. */
478         s->owner = c;
479         return 0;
480 }
481
482 static int wav_play(struct ast_filestream *s)
483 {
484         ast_read_callback(s);
485         return 0;
486 }
487
488 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
489 {
490         int res = 0;
491         int x;
492         short tmp[8000], *tmpi;
493         float tmpf;
494         if (f->frametype != AST_FRAME_VOICE) {
495                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
496                 return -1;
497         }
498         if (f->subclass != AST_FORMAT_SLINEAR) {
499                 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
500                 return -1;
501         }
502         if (f->datalen > sizeof(tmp)) {
503                 ast_log(LOG_WARNING, "Data length is too long\n");
504                 return -1;
505         }
506         if (!f->datalen)
507                 return -1;
508
509 #if 0
510         printf("Data Length: %d\n", f->datalen);
511 #endif  
512
513         if (fs->buf) {
514                 tmpi = f->data;
515                 /* Volume adjust here to accomodate */
516                 for (x=0;x<f->datalen/2;x++) {
517                         tmpf = ((float)tmpi[x]) * ((float)(1 << GAIN));
518                         if (tmpf > 32767.0)
519                                 tmpf = 32767.0;
520                         if (tmpf < -32768.0)
521                                 tmpf = -32768.0;
522                         tmp[x] = tmpf;
523                         tmp[x] &= ~((1 << GAIN) - 1);
524
525 #if __BYTE_ORDER == __BIG_ENDIAN
526                         tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
527 #endif
528
529                 }
530                 if ((write (fs->fd, tmp, f->datalen) != f->datalen) ) {
531                         ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
532                         return -1;
533                 }
534         } else {
535                 ast_log(LOG_WARNING, "Cannot write data to file.\n");
536                 return -1;
537         }
538         
539         fs->bytes += f->datalen;
540         update_header(fs->fd);
541                 
542         return 0;
543
544 }
545
546 static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
547 {
548         off_t min,max,cur;
549         long offset,samples;
550         
551         samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
552         min = 44; /* wav header is 44 bytes */
553         cur = lseek(fs->fd, 0, SEEK_CUR);
554         max = lseek(fs->fd, 0, SEEK_END);
555         if(whence == SEEK_SET)
556                 offset = samples + min;
557         if(whence == SEEK_CUR)
558                 offset = samples + cur;
559         if(whence == SEEK_END)
560                 offset = max - samples; 
561         offset = (offset > max)?max:offset;
562         offset = (offset < min)?min:offset;
563         return lseek(fs->fd,offset,SEEK_SET);
564 }
565
566 static int wav_trunc(struct ast_filestream *fs)
567 {
568         if(ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)))
569                 return -1;
570         return update_header(fs->fd);
571 }
572
573 static long wav_tell(struct ast_filestream *fs)
574 {
575         off_t offset;
576         offset = lseek(fs->fd, 0, SEEK_CUR);
577         /* subtract header size to get samples, then divide by 2 for 16 bit samples */
578         return (offset - 44)/2;
579 }
580
581 static char *wav_getcomment(struct ast_filestream *s)
582 {
583         return NULL;
584 }
585
586 int load_module()
587 {
588         return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
589                                                                 wav_open,
590                                                                 wav_rewrite,
591                                                                 wav_apply,
592                                                                 wav_play,
593                                                                 wav_write,
594                                                                 wav_seek,
595                                                                 wav_trunc,
596                                                                 wav_tell,
597                                                                 wav_read,
598                                                                 wav_close,
599                                                                 wav_getcomment);
600                                                                 
601                                                                 
602 }
603
604 int unload_module()
605 {
606         struct ast_filestream *tmp, *tmpl;
607         if (ast_pthread_mutex_lock(&wav_lock)) {
608                 ast_log(LOG_WARNING, "Unable to lock wav list\n");
609                 return -1;
610         }
611         tmp = glist;
612         while(tmp) {
613                 if (tmp->owner)
614                         ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
615                 tmpl = tmp;
616                 tmp = tmp->next;
617                 free(tmpl);
618         }
619         ast_pthread_mutex_unlock(&wav_lock);
620         return ast_format_unregister(name);
621 }       
622
623 int usecount()
624 {
625         int res;
626         if (ast_pthread_mutex_lock(&wav_lock)) {
627                 ast_log(LOG_WARNING, "Unable to lock wav list\n");
628                 return -1;
629         }
630         res = glistcnt;
631         ast_pthread_mutex_unlock(&wav_lock);
632         return res;
633 }
634
635 char *description()
636 {
637         return desc;
638 }
639
640
641 char *key()
642 {
643         return ASTERISK_GPL_KEY;
644 }