2 * Asterisk -- A telephony toolkit for Linux.
4 * Work with WAV in the proprietary Microsoft format.
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/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>
30 /* Some Ideas for this code came from makewave.c by Jeffery Chilton */
32 /* Portions of the conversion code are by guido@sienanet.it */
34 struct ast_filestream {
35 void *reserved[AST_RESERVED_POINTERS];
36 /* Believe it or not, we must decode/recode to account for the
38 /* This is what a filestream means to us */
39 int fd; /* Descriptor */
42 struct ast_channel *owner;
43 struct ast_frame fr; /* Frame information */
44 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
45 char empty; /* Empty character */
46 short buf[160]; /* Two Real GSM Frames */
51 struct ast_filestream *next;
55 static struct ast_filestream *glist = NULL;
56 static pthread_mutex_t wav_lock = AST_MUTEX_INITIALIZER;
57 static int glistcnt = 0;
59 static char *name = "wav";
60 static char *desc = "Microsoft WAV format (8000hz Signed Linear)";
61 static char *exts = "wav";
65 #define GAIN 2 /* 2^GAIN is the multiple to increase the volume by */
67 #if __BYTE_ORDER == __LITTLE_ENDIAN
73 #if __BYTE_ORDER == __BIG_ENDIAN
75 (((((b) ) & 0xFF) << 24) | \
76 ((((b) >> 8) & 0xFF) << 16) | \
77 ((((b) >> 16) & 0xFF) << 8) | \
78 ((((b) >> 24) & 0xFF) ))
80 (((((b) ) & 0xFF) << 8) | \
81 ((((b) >> 8) & 0xFF) ))
82 #define ltohl(b) htoll(b)
83 #define ltohs(b) htols(b)
85 #error "Endianess not defined"
90 static int check_header(int fd)
92 int type, size, formtype;
94 short format, chans, bysam, bisam;
98 if (read(fd, &type, 4) != 4) {
99 ast_log(LOG_WARNING, "Read failed (type)\n");
102 if (read(fd, &size, 4) != 4) {
103 ast_log(LOG_WARNING, "Read failed (size)\n");
107 if (read(fd, &formtype, 4) != 4) {
108 ast_log(LOG_WARNING, "Read failed (formtype)\n");
111 if (memcmp(&type, "RIFF", 4)) {
112 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
115 if (memcmp(&formtype, "WAVE", 4)) {
116 ast_log(LOG_WARNING, "Does not contain WAVE\n");
119 if (read(fd, &fmt, 4) != 4) {
120 ast_log(LOG_WARNING, "Read failed (fmt)\n");
123 if (memcmp(&fmt, "fmt ", 4)) {
124 ast_log(LOG_WARNING, "Does not say fmt\n");
127 if (read(fd, &hsize, 4) != 4) {
128 ast_log(LOG_WARNING, "Read failed (formtype)\n");
131 if (ltohl(hsize) != 16) {
132 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
135 if (read(fd, &format, 2) != 2) {
136 ast_log(LOG_WARNING, "Read failed (format)\n");
139 if (ltohs(format) != 1) {
140 ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
143 if (read(fd, &chans, 2) != 2) {
144 ast_log(LOG_WARNING, "Read failed (format)\n");
147 if (ltohs(chans) != 1) {
148 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
151 if (read(fd, &freq, 4) != 4) {
152 ast_log(LOG_WARNING, "Read failed (freq)\n");
155 if (ltohl(freq) != 8000) {
156 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
159 /* Ignore the byte frequency */
160 if (read(fd, &bysec, 4) != 4) {
161 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
164 /* Check bytes per sample */
165 if (read(fd, &bysam, 2) != 2) {
166 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
169 if (ltohs(bysam) != 2) {
170 ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
173 if (read(fd, &bisam, 2) != 2) {
174 ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
177 /* Begin data chunk */
178 if (read(fd, &data, 4) != 4) {
179 ast_log(LOG_WARNING, "Read failed (data)\n");
182 if (memcmp(&data, "data", 4)) {
183 ast_log(LOG_WARNING, "Does not say data\n");
186 /* Ignore the data length */
187 if (read(fd, &data, 4) != 4) {
188 ast_log(LOG_WARNING, "Read failed (data)\n");
194 static int update_header(int fd, int bytes)
197 int datalen = htoll(bytes);
198 /* int filelen = htoll(52 + ((bytes + 1) & ~0x1)); */
199 int filelen = htoll(36 + bytes);
201 cur = lseek(fd, 0, SEEK_CUR);
203 ast_log(LOG_WARNING, "Unable to find our position\n");
206 if (lseek(fd, 4, SEEK_SET) != 4) {
207 ast_log(LOG_WARNING, "Unable to set our position\n");
210 if (write(fd, &filelen, 4) != 4) {
211 ast_log(LOG_WARNING, "Unable to set write file size\n");
214 if (lseek(fd, 40, SEEK_SET) != 40) {
215 ast_log(LOG_WARNING, "Unable to set our position\n");
218 if (write(fd, &datalen, 4) != 4) {
219 ast_log(LOG_WARNING, "Unable to set write datalen\n");
222 if (lseek(fd, cur, SEEK_SET) != cur) {
223 ast_log(LOG_WARNING, "Unable to return to position\n");
229 static int write_header(int fd)
231 unsigned int hz=htoll(8000);
232 unsigned int bhz = htoll(16000);
233 unsigned int hs = htoll(16);
234 unsigned short fmt = htols(1);
235 unsigned short chans = htols(1);
236 unsigned short bysam = htols(2);
237 unsigned short bisam = htols(16);
238 unsigned int size = htoll(0);
239 /* Write a wav header, ignoring sizes which will be filled in later */
240 if (write(fd, "RIFF", 4) != 4) {
241 ast_log(LOG_WARNING, "Unable to write header\n");
244 if (write(fd, &size, 4) != 4) {
245 ast_log(LOG_WARNING, "Unable to write header\n");
248 if (write(fd, "WAVEfmt ", 8) != 8) {
249 ast_log(LOG_WARNING, "Unable to write header\n");
252 if (write(fd, &hs, 4) != 4) {
253 ast_log(LOG_WARNING, "Unable to write header\n");
256 if (write(fd, &fmt, 2) != 2) {
257 ast_log(LOG_WARNING, "Unable to write header\n");
260 if (write(fd, &chans, 2) != 2) {
261 ast_log(LOG_WARNING, "Unable to write header\n");
264 if (write(fd, &hz, 4) != 4) {
265 ast_log(LOG_WARNING, "Unable to write header\n");
268 if (write(fd, &bhz, 4) != 4) {
269 ast_log(LOG_WARNING, "Unable to write header\n");
272 if (write(fd, &bysam, 2) != 2) {
273 ast_log(LOG_WARNING, "Unable to write header\n");
276 if (write(fd, &bisam, 2) != 2) {
277 ast_log(LOG_WARNING, "Unable to write header\n");
280 if (write(fd, "data", 4) != 4) {
281 ast_log(LOG_WARNING, "Unable to write header\n");
284 if (write(fd, &size, 4) != 4) {
285 ast_log(LOG_WARNING, "Unable to write header\n");
291 static struct ast_filestream *wav_open(int fd)
293 /* We don't have any header to read or anything really, but
294 if we did, it would go here. We also might want to check
295 and be sure it's a valid file. */
296 struct ast_filestream *tmp;
297 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
298 memset(tmp, 0, sizeof(struct ast_filestream));
299 if (check_header(fd)) {
303 if (ast_pthread_mutex_lock(&wav_lock)) {
304 ast_log(LOG_WARNING, "Unable to lock wav list\n");
313 tmp->fr.data = tmp->buf;
314 tmp->fr.frametype = AST_FRAME_VOICE;
315 tmp->fr.subclass = AST_FORMAT_SLINEAR;
316 /* datalen will vary for each frame */
319 tmp->lasttimeout = -1;
322 ast_pthread_mutex_unlock(&wav_lock);
323 ast_update_use_count();
328 static struct ast_filestream *wav_rewrite(int fd, char *comment)
330 /* We don't have any header to read or anything really, but
331 if we did, it would go here. We also might want to check
332 and be sure it's a valid file. */
333 struct ast_filestream *tmp;
334 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
335 memset(tmp, 0, sizeof(struct ast_filestream));
336 if (write_header(fd)) {
340 if (ast_pthread_mutex_lock(&wav_lock)) {
341 ast_log(LOG_WARNING, "Unable to lock wav list\n");
349 tmp->lasttimeout = -1;
351 ast_pthread_mutex_unlock(&wav_lock);
352 ast_update_use_count();
354 ast_log(LOG_WARNING, "Out of memory\n");
358 static struct ast_frame *wav_read(struct ast_filestream *s)
363 static void wav_close(struct ast_filestream *s)
365 struct ast_filestream *tmp, *tmpl = NULL;
367 if (ast_pthread_mutex_lock(&wav_lock)) {
368 ast_log(LOG_WARNING, "Unable to lock wav list\n");
375 tmpl->next = tmp->next;
385 s->owner->stream = NULL;
386 if (s->owner->streamid > -1)
387 ast_sched_del(s->owner->sched, s->owner->streamid);
388 s->owner->streamid = -1;
390 ast_pthread_mutex_unlock(&wav_lock);
391 ast_update_use_count();
393 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
394 /* Pad to even length */
396 write(s->fd, &zero, 1);
400 printf("bytes = %d\n", s->bytes);
404 static int ast_read_callback(void *data)
410 struct ast_filestream *s = data;
411 short tmp[sizeof(s->buf) / 2];
412 /* Send a frame from the file to the appropriate channel */
414 if ( (res = read(s->fd, tmp, sizeof(tmp))) <= 0 ) {
416 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
418 s->owner->streamid = -1;
422 for (x=0;x<sizeof(tmp)/2;x++)
423 if (tmp[x] & ((1 << GAIN) - 1)) {
424 /* If it has data down low, then it's not something we've artificially increased gain
425 on, so we don't need to gain adjust it */
430 for (x=0;x<sizeof(tmp)/2;x++) {
431 s->buf[x] = tmp[x] >> GAIN;
434 memcpy(s->buf, tmp, sizeof(s->buf));
438 s->fr.frametype = AST_FRAME_VOICE;
439 s->fr.subclass = AST_FORMAT_SLINEAR;
440 s->fr.offset = AST_FRIENDLY_OFFSET;
444 s->fr.timelen = delay;
446 /* Lastly, process the frame */
447 if (delay != s->lasttimeout) {
448 s->owner->streamid = ast_sched_add(s->owner->sched, delay, ast_read_callback, s);
449 s->lasttimeout = delay;
455 if (ast_write(s->owner, &s->fr)) {
456 ast_log(LOG_WARNING, "Failed to write frame\n");
457 s->owner->streamid = -1;
464 static int wav_apply(struct ast_channel *c, struct ast_filestream *s)
466 /* Select our owner for this stream, and get the ball rolling. */
468 ast_read_callback(s);
472 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
476 short tmp[8000], *tmpi;
478 if (f->frametype != AST_FRAME_VOICE) {
479 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
482 if (f->subclass != AST_FORMAT_SLINEAR) {
483 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
486 if (f->datalen > sizeof(tmp)) {
487 ast_log(LOG_WARNING, "Data length is too long\n");
494 printf("Data Length: %d\n", f->datalen);
499 /* Volume adjust here to accomodate */
500 for (x=0;x<f->datalen/2;x++) {
501 tmpf = ((float)tmpi[x]) * ((float)(1 << GAIN));
507 tmp[x] &= ~((1 << GAIN) - 1);
509 if ((write (fs->fd, tmp, f->datalen) != f->datalen) ) {
510 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
514 ast_log(LOG_WARNING, "Cannot write data to file.\n");
518 fs->bytes += f->datalen;
519 update_header(fs->fd, fs->bytes);
525 static char *wav_getcomment(struct ast_filestream *s)
532 return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
546 struct ast_filestream *tmp, *tmpl;
547 if (ast_pthread_mutex_lock(&wav_lock)) {
548 ast_log(LOG_WARNING, "Unable to lock wav list\n");
554 ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
559 ast_pthread_mutex_unlock(&wav_lock);
560 return ast_format_unregister(name);
566 if (ast_pthread_mutex_lock(&wav_lock)) {
567 ast_log(LOG_WARNING, "Unable to lock wav list\n");
571 ast_pthread_mutex_unlock(&wav_lock);
583 return ASTERISK_GPL_KEY;