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