2 * Asterisk -- A telephony toolkit for Linux.
4 * Save GSM in the proprietary Microsoft format.
6 * Copyright (C) 1999-2004, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
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 <netinet/in.h>
21 #include <arpa/inet.h>
32 #include "solaris-compat/compat.h"
34 #include <machine/endian.h>
39 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
41 /* Portions of the conversion code are by guido@sienanet.it */
43 /* begin binary data: */
44 char msgsm_silence[] = /* 65 */
45 {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
46 ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
47 ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
48 ,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48
49 ,0x92,0x24,0x49,0x92,0x00};
50 /* end binary data. size = 65 bytes */
52 struct ast_filestream {
53 void *reserved[AST_RESERVED_POINTERS];
54 /* Believe it or not, we must decode/recode to account for the
56 /* This is what a filestream means to us */
57 int fd; /* Descriptor */
59 struct ast_frame fr; /* Frame information */
60 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
61 char empty; /* Empty character */
62 unsigned char gsm[66]; /* Two Real GSM Frames */
64 int secondhalf; /* Are we on the second half */
69 AST_MUTEX_DEFINE_STATIC(wav_lock);
70 static int glistcnt = 0;
72 static char *name = "wav49";
73 static char *desc = "Microsoft WAV format (Proprietary GSM)";
74 static char *exts = "WAV|wav49";
76 #if __BYTE_ORDER == __LITTLE_ENDIAN
82 #if __BYTE_ORDER == __BIG_ENDIAN
84 (((((b) ) & 0xFF) << 24) | \
85 ((((b) >> 8) & 0xFF) << 16) | \
86 ((((b) >> 16) & 0xFF) << 8) | \
87 ((((b) >> 24) & 0xFF) ))
89 (((((b) ) & 0xFF) << 8) | \
90 ((((b) >> 8) & 0xFF) ))
91 #define ltohl(b) htoll(b)
92 #define ltohs(b) htols(b)
94 #error "Endianess not defined"
99 static int check_header(int fd)
101 int type, size, formtype;
102 int fmt, hsize, fact;
106 if (read(fd, &type, 4) != 4) {
107 ast_log(LOG_WARNING, "Read failed (type)\n");
110 if (read(fd, &size, 4) != 4) {
111 ast_log(LOG_WARNING, "Read failed (size)\n");
115 if (read(fd, &formtype, 4) != 4) {
116 ast_log(LOG_WARNING, "Read failed (formtype)\n");
119 if (memcmp(&type, "RIFF", 4)) {
120 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
123 if (memcmp(&formtype, "WAVE", 4)) {
124 ast_log(LOG_WARNING, "Does not contain WAVE\n");
127 if (read(fd, &fmt, 4) != 4) {
128 ast_log(LOG_WARNING, "Read failed (fmt)\n");
131 if (memcmp(&fmt, "fmt ", 4)) {
132 ast_log(LOG_WARNING, "Does not say fmt\n");
135 if (read(fd, &hsize, 4) != 4) {
136 ast_log(LOG_WARNING, "Read failed (formtype)\n");
139 if (ltohl(hsize) != 20) {
140 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
143 if (read(fd, &format, 2) != 2) {
144 ast_log(LOG_WARNING, "Read failed (format)\n");
147 if (ltohs(format) != 49) {
148 ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
151 if (read(fd, &chans, 2) != 2) {
152 ast_log(LOG_WARNING, "Read failed (format)\n");
155 if (ltohs(chans) != 1) {
156 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
159 if (read(fd, &freq, 4) != 4) {
160 ast_log(LOG_WARNING, "Read failed (freq)\n");
163 if (ltohl(freq) != 8000) {
164 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
167 /* Ignore the byte frequency */
168 if (read(fd, &freq, 4) != 4) {
169 ast_log(LOG_WARNING, "Read failed (X_1)\n");
172 /* Ignore the two weird fields */
173 if (read(fd, &freq, 4) != 4) {
174 ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
177 /* Ignore the byte frequency */
178 if (read(fd, &freq, 4) != 4) {
179 ast_log(LOG_WARNING, "Read failed (Y_1)\n");
182 /* Check for the word fact */
183 if (read(fd, &fact, 4) != 4) {
184 ast_log(LOG_WARNING, "Read failed (fact)\n");
187 if (memcmp(&fact, "fact", 4)) {
188 ast_log(LOG_WARNING, "Does not say fact\n");
191 /* Ignore the "fact value" */
192 if (read(fd, &fact, 4) != 4) {
193 ast_log(LOG_WARNING, "Read failed (fact header)\n");
196 if (read(fd, &fact, 4) != 4) {
197 ast_log(LOG_WARNING, "Read failed (fact value)\n");
200 /* Check for the word data */
201 if (read(fd, &data, 4) != 4) {
202 ast_log(LOG_WARNING, "Read failed (data)\n");
205 if (memcmp(&data, "data", 4)) {
206 ast_log(LOG_WARNING, "Does not say data\n");
209 /* Ignore the data length */
210 if (read(fd, &data, 4) != 4) {
211 ast_log(LOG_WARNING, "Read failed (data)\n");
217 static int update_header(int fd)
222 cur = lseek(fd, 0, SEEK_CUR);
223 end = lseek(fd, 0, SEEK_END);
224 /* in a gsm WAV, data starts 60 bytes in */
226 datalen = htoll((bytes + 1) & ~0x1);
227 filelen = htoll(52 + ((bytes + 1) & ~0x1));
229 ast_log(LOG_WARNING, "Unable to find our position\n");
232 if (lseek(fd, 4, SEEK_SET) != 4) {
233 ast_log(LOG_WARNING, "Unable to set our position\n");
236 if (write(fd, &filelen, 4) != 4) {
237 ast_log(LOG_WARNING, "Unable to set write file size\n");
240 if (lseek(fd, 56, SEEK_SET) != 56) {
241 ast_log(LOG_WARNING, "Unable to set our position\n");
244 if (write(fd, &datalen, 4) != 4) {
245 ast_log(LOG_WARNING, "Unable to set write datalen\n");
248 if (lseek(fd, cur, SEEK_SET) != cur) {
249 ast_log(LOG_WARNING, "Unable to return to position\n");
255 static int write_header(int fd)
257 unsigned int hz=htoll(8000);
258 unsigned int bhz = htoll(1625);
259 unsigned int hs = htoll(20);
260 unsigned short fmt = htols(49);
261 unsigned short chans = htols(1);
262 unsigned int fhs = htoll(4);
263 unsigned int x_1 = htoll(65);
264 unsigned short x_2 = htols(2);
265 unsigned short x_3 = htols(320);
266 unsigned int y_1 = htoll(20160);
267 unsigned int size = htoll(0);
268 /* Write a GSM header, ignoring sizes which will be filled in later */
269 if (write(fd, "RIFF", 4) != 4) {
270 ast_log(LOG_WARNING, "Unable to write header\n");
273 if (write(fd, &size, 4) != 4) {
274 ast_log(LOG_WARNING, "Unable to write header\n");
277 if (write(fd, "WAVEfmt ", 8) != 8) {
278 ast_log(LOG_WARNING, "Unable to write header\n");
281 if (write(fd, &hs, 4) != 4) {
282 ast_log(LOG_WARNING, "Unable to write header\n");
285 if (write(fd, &fmt, 2) != 2) {
286 ast_log(LOG_WARNING, "Unable to write header\n");
289 if (write(fd, &chans, 2) != 2) {
290 ast_log(LOG_WARNING, "Unable to write header\n");
293 if (write(fd, &hz, 4) != 4) {
294 ast_log(LOG_WARNING, "Unable to write header\n");
297 if (write(fd, &bhz, 4) != 4) {
298 ast_log(LOG_WARNING, "Unable to write header\n");
301 if (write(fd, &x_1, 4) != 4) {
302 ast_log(LOG_WARNING, "Unable to write header\n");
305 if (write(fd, &x_2, 2) != 2) {
306 ast_log(LOG_WARNING, "Unable to write header\n");
309 if (write(fd, &x_3, 2) != 2) {
310 ast_log(LOG_WARNING, "Unable to write header\n");
313 if (write(fd, "fact", 4) != 4) {
314 ast_log(LOG_WARNING, "Unable to write header\n");
317 if (write(fd, &fhs, 4) != 4) {
318 ast_log(LOG_WARNING, "Unable to write header\n");
321 if (write(fd, &y_1, 4) != 4) {
322 ast_log(LOG_WARNING, "Unable to write header\n");
325 if (write(fd, "data", 4) != 4) {
326 ast_log(LOG_WARNING, "Unable to write header\n");
329 if (write(fd, &size, 4) != 4) {
330 ast_log(LOG_WARNING, "Unable to write header\n");
336 static struct ast_filestream *wav_open(int fd)
338 /* We don't have any header to read or anything really, but
339 if we did, it would go here. We also might want to check
340 and be sure it's a valid file. */
341 struct ast_filestream *tmp;
342 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
343 memset(tmp, 0, sizeof(struct ast_filestream));
344 if (check_header(fd)) {
348 if (ast_mutex_lock(&wav_lock)) {
349 ast_log(LOG_WARNING, "Unable to lock wav list\n");
354 tmp->fr.data = tmp->gsm;
355 tmp->fr.frametype = AST_FRAME_VOICE;
356 tmp->fr.subclass = AST_FORMAT_GSM;
357 /* datalen will vary for each frame */
362 ast_mutex_unlock(&wav_lock);
363 ast_update_use_count();
368 static struct ast_filestream *wav_rewrite(int fd, const char *comment)
370 /* We don't have any header to read or anything really, but
371 if we did, it would go here. We also might want to check
372 and be sure it's a valid file. */
373 struct ast_filestream *tmp;
374 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
375 memset(tmp, 0, sizeof(struct ast_filestream));
376 if (write_header(fd)) {
380 if (ast_mutex_lock(&wav_lock)) {
381 ast_log(LOG_WARNING, "Unable to lock wav list\n");
387 ast_mutex_unlock(&wav_lock);
388 ast_update_use_count();
390 ast_log(LOG_WARNING, "Out of memory\n");
394 static void wav_close(struct ast_filestream *s)
397 if (ast_mutex_lock(&wav_lock)) {
398 ast_log(LOG_WARNING, "Unable to lock wav list\n");
402 ast_mutex_unlock(&wav_lock);
403 ast_update_use_count();
404 /* Pad to even length */
406 write(s->fd, &zero, 1);
412 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
416 /* Send a frame from the file to the appropriate channel */
418 s->fr.frametype = AST_FRAME_VOICE;
419 s->fr.subclass = AST_FORMAT_GSM;
420 s->fr.offset = AST_FRIENDLY_OFFSET;
425 /* Just return a frame based on the second GSM frame */
426 s->fr.data = s->gsm + 33;
428 if ((res = read(s->fd, msdata, 65)) != 65) {
429 if (res && (res != 1))
430 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
433 /* Convert from MS format to two real GSM frames */
434 conv65(msdata, s->gsm);
437 s->secondhalf = !s->secondhalf;
442 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
448 if (f->frametype != AST_FRAME_VOICE) {
449 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
452 if (f->subclass != AST_FORMAT_GSM) {
453 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
456 if (!(f->datalen % 65))
458 while(len < f->datalen) {
461 if ((res = write(fs->fd, f->data + len, 65)) != 65) {
462 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
466 update_header(fs->fd);
469 if (fs->secondhalf) {
470 memcpy(fs->gsm + 33, f->data + len, 33);
471 conv66(fs->gsm, msdata);
472 if ((res = write(fs->fd, msdata, 65)) != 65) {
473 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
477 update_header(fs->fd);
479 /* Copy the data and do nothing */
480 memcpy(fs->gsm, f->data + len, 33);
482 fs->secondhalf = !fs->secondhalf;
489 static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
491 off_t offset=0,distance,cur,min,max;
493 cur = lseek(fs->fd, 0, SEEK_CUR);
494 max = lseek(fs->fd, 0, SEEK_END);
495 /* I'm getting sloppy here, I'm only going to go to even splits of the 2
496 * frames, if you want tighter cuts use format_gsm, format_pcm, or format_wav */
497 distance = (sample_offset/320) * 65;
498 if(whence == SEEK_SET)
499 offset = distance + min;
500 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
501 offset = distance + cur;
502 else if(whence == SEEK_END)
503 offset = max - distance;
504 /* always protect against seeking past end of header */
505 offset = (offset < min)?min:offset;
506 if (whence != SEEK_FORCECUR) {
507 offset = (offset > max)?max:offset;
508 } else if (offset > max) {
510 lseek(fs->fd, 0, SEEK_END);
511 for (i=0; i< (offset - max) / 65; i++) {
512 write(fs->fd, msgsm_silence, 65);
516 return lseek(fs->fd, offset, SEEK_SET);
519 static int wav_trunc(struct ast_filestream *fs)
521 if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)))
523 return update_header(fs->fd);
526 static long wav_tell(struct ast_filestream *fs)
529 offset = lseek(fs->fd, 0, SEEK_CUR);
530 /* since this will most likely be used later in play or record, lets stick
531 * to that level of resolution, just even frames boundaries */
532 return (offset - 52)/65*320;
535 static char *wav_getcomment(struct ast_filestream *s)
542 return ast_format_register(name, exts, AST_FORMAT_GSM,
558 return ast_format_unregister(name);
564 if (ast_mutex_lock(&wav_lock)) {
565 ast_log(LOG_WARNING, "Unable to lock wav list\n");
569 ast_mutex_unlock(&wav_lock);
581 return ASTERISK_GPL_KEY;