2 * Asterisk -- A telephony toolkit for Linux.
4 * Save GSM in the proprietary Microsoft format.
6 * Copyright (C) 1999 - 2005, 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
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27 #include "asterisk/lock.h"
28 #include "asterisk/channel.h"
29 #include "asterisk/file.h"
30 #include "asterisk/logger.h"
31 #include "asterisk/sched.h"
32 #include "asterisk/module.h"
33 #include "asterisk/endian.h"
37 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
39 /* Portions of the conversion code are by guido@sienanet.it */
41 /* begin binary data: */
42 char msgsm_silence[] = /* 65 */
43 {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
44 ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
45 ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
46 ,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48
47 ,0x92,0x24,0x49,0x92,0x00};
48 /* end binary data. size = 65 bytes */
50 struct ast_filestream {
51 void *reserved[AST_RESERVED_POINTERS];
52 /* Believe it or not, we must decode/recode to account for the
54 /* This is what a filestream means to us */
55 int fd; /* Descriptor */
56 struct ast_frame fr; /* Frame information */
57 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
58 char empty; /* Empty character */
59 unsigned char gsm[66]; /* Two Real GSM Frames */
61 int secondhalf; /* Are we on the second half */
66 AST_MUTEX_DEFINE_STATIC(wav_lock);
67 static int glistcnt = 0;
69 static char *name = "wav49";
70 static char *desc = "Microsoft WAV format (Proprietary GSM)";
71 static char *exts = "WAV|wav49";
73 #if __BYTE_ORDER == __LITTLE_ENDIAN
79 #if __BYTE_ORDER == __BIG_ENDIAN
81 (((((b) ) & 0xFF) << 24) | \
82 ((((b) >> 8) & 0xFF) << 16) | \
83 ((((b) >> 16) & 0xFF) << 8) | \
84 ((((b) >> 24) & 0xFF) ))
86 (((((b) ) & 0xFF) << 8) | \
87 ((((b) >> 8) & 0xFF) ))
88 #define ltohl(b) htoll(b)
89 #define ltohs(b) htols(b)
91 #error "Endianess not defined"
96 static int check_header(int fd)
98 int type, size, formtype;
103 if (read(fd, &type, 4) != 4) {
104 ast_log(LOG_WARNING, "Read failed (type)\n");
107 if (read(fd, &size, 4) != 4) {
108 ast_log(LOG_WARNING, "Read failed (size)\n");
112 if (read(fd, &formtype, 4) != 4) {
113 ast_log(LOG_WARNING, "Read failed (formtype)\n");
116 if (memcmp(&type, "RIFF", 4)) {
117 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
120 if (memcmp(&formtype, "WAVE", 4)) {
121 ast_log(LOG_WARNING, "Does not contain WAVE\n");
124 if (read(fd, &fmt, 4) != 4) {
125 ast_log(LOG_WARNING, "Read failed (fmt)\n");
128 if (memcmp(&fmt, "fmt ", 4)) {
129 ast_log(LOG_WARNING, "Does not say fmt\n");
132 if (read(fd, &hsize, 4) != 4) {
133 ast_log(LOG_WARNING, "Read failed (formtype)\n");
136 if (ltohl(hsize) != 20) {
137 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
140 if (read(fd, &format, 2) != 2) {
141 ast_log(LOG_WARNING, "Read failed (format)\n");
144 if (ltohs(format) != 49) {
145 ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
148 if (read(fd, &chans, 2) != 2) {
149 ast_log(LOG_WARNING, "Read failed (format)\n");
152 if (ltohs(chans) != 1) {
153 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
156 if (read(fd, &freq, 4) != 4) {
157 ast_log(LOG_WARNING, "Read failed (freq)\n");
160 if (ltohl(freq) != 8000) {
161 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
164 /* Ignore the byte frequency */
165 if (read(fd, &freq, 4) != 4) {
166 ast_log(LOG_WARNING, "Read failed (X_1)\n");
169 /* Ignore the two weird fields */
170 if (read(fd, &freq, 4) != 4) {
171 ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
174 /* Ignore the byte frequency */
175 if (read(fd, &freq, 4) != 4) {
176 ast_log(LOG_WARNING, "Read failed (Y_1)\n");
179 /* Check for the word fact */
180 if (read(fd, &fact, 4) != 4) {
181 ast_log(LOG_WARNING, "Read failed (fact)\n");
184 if (memcmp(&fact, "fact", 4)) {
185 ast_log(LOG_WARNING, "Does not say fact\n");
188 /* Ignore the "fact value" */
189 if (read(fd, &fact, 4) != 4) {
190 ast_log(LOG_WARNING, "Read failed (fact header)\n");
193 if (read(fd, &fact, 4) != 4) {
194 ast_log(LOG_WARNING, "Read failed (fact value)\n");
197 /* Check for the word data */
198 if (read(fd, &data, 4) != 4) {
199 ast_log(LOG_WARNING, "Read failed (data)\n");
202 if (memcmp(&data, "data", 4)) {
203 ast_log(LOG_WARNING, "Does not say data\n");
206 /* Ignore the data length */
207 if (read(fd, &data, 4) != 4) {
208 ast_log(LOG_WARNING, "Read failed (data)\n");
214 static int update_header(int fd)
219 cur = lseek(fd, 0, SEEK_CUR);
220 end = lseek(fd, 0, SEEK_END);
221 /* in a gsm WAV, data starts 60 bytes in */
223 datalen = htoll((bytes + 1) & ~0x1);
224 filelen = htoll(52 + ((bytes + 1) & ~0x1));
226 ast_log(LOG_WARNING, "Unable to find our position\n");
229 if (lseek(fd, 4, SEEK_SET) != 4) {
230 ast_log(LOG_WARNING, "Unable to set our position\n");
233 if (write(fd, &filelen, 4) != 4) {
234 ast_log(LOG_WARNING, "Unable to set write file size\n");
237 if (lseek(fd, 56, SEEK_SET) != 56) {
238 ast_log(LOG_WARNING, "Unable to set our position\n");
241 if (write(fd, &datalen, 4) != 4) {
242 ast_log(LOG_WARNING, "Unable to set write datalen\n");
245 if (lseek(fd, cur, SEEK_SET) != cur) {
246 ast_log(LOG_WARNING, "Unable to return to position\n");
252 static int write_header(int fd)
254 unsigned int hz=htoll(8000);
255 unsigned int bhz = htoll(1625);
256 unsigned int hs = htoll(20);
257 unsigned short fmt = htols(49);
258 unsigned short chans = htols(1);
259 unsigned int fhs = htoll(4);
260 unsigned int x_1 = htoll(65);
261 unsigned short x_2 = htols(2);
262 unsigned short x_3 = htols(320);
263 unsigned int y_1 = htoll(20160);
264 unsigned int size = htoll(0);
265 /* Write a GSM header, ignoring sizes which will be filled in later */
266 if (write(fd, "RIFF", 4) != 4) {
267 ast_log(LOG_WARNING, "Unable to write header\n");
270 if (write(fd, &size, 4) != 4) {
271 ast_log(LOG_WARNING, "Unable to write header\n");
274 if (write(fd, "WAVEfmt ", 8) != 8) {
275 ast_log(LOG_WARNING, "Unable to write header\n");
278 if (write(fd, &hs, 4) != 4) {
279 ast_log(LOG_WARNING, "Unable to write header\n");
282 if (write(fd, &fmt, 2) != 2) {
283 ast_log(LOG_WARNING, "Unable to write header\n");
286 if (write(fd, &chans, 2) != 2) {
287 ast_log(LOG_WARNING, "Unable to write header\n");
290 if (write(fd, &hz, 4) != 4) {
291 ast_log(LOG_WARNING, "Unable to write header\n");
294 if (write(fd, &bhz, 4) != 4) {
295 ast_log(LOG_WARNING, "Unable to write header\n");
298 if (write(fd, &x_1, 4) != 4) {
299 ast_log(LOG_WARNING, "Unable to write header\n");
302 if (write(fd, &x_2, 2) != 2) {
303 ast_log(LOG_WARNING, "Unable to write header\n");
306 if (write(fd, &x_3, 2) != 2) {
307 ast_log(LOG_WARNING, "Unable to write header\n");
310 if (write(fd, "fact", 4) != 4) {
311 ast_log(LOG_WARNING, "Unable to write header\n");
314 if (write(fd, &fhs, 4) != 4) {
315 ast_log(LOG_WARNING, "Unable to write header\n");
318 if (write(fd, &y_1, 4) != 4) {
319 ast_log(LOG_WARNING, "Unable to write header\n");
322 if (write(fd, "data", 4) != 4) {
323 ast_log(LOG_WARNING, "Unable to write header\n");
326 if (write(fd, &size, 4) != 4) {
327 ast_log(LOG_WARNING, "Unable to write header\n");
333 static struct ast_filestream *wav_open(int fd)
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 (check_header(fd)) {
345 if (ast_mutex_lock(&wav_lock)) {
346 ast_log(LOG_WARNING, "Unable to lock wav list\n");
351 tmp->fr.data = tmp->gsm;
352 tmp->fr.frametype = AST_FRAME_VOICE;
353 tmp->fr.subclass = AST_FORMAT_GSM;
354 /* datalen will vary for each frame */
359 ast_mutex_unlock(&wav_lock);
360 ast_update_use_count();
365 static struct ast_filestream *wav_rewrite(int fd, const char *comment)
367 /* We don't have any header to read or anything really, but
368 if we did, it would go here. We also might want to check
369 and be sure it's a valid file. */
370 struct ast_filestream *tmp;
371 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
372 memset(tmp, 0, sizeof(struct ast_filestream));
373 if (write_header(fd)) {
377 if (ast_mutex_lock(&wav_lock)) {
378 ast_log(LOG_WARNING, "Unable to lock wav list\n");
384 ast_mutex_unlock(&wav_lock);
385 ast_update_use_count();
387 ast_log(LOG_WARNING, "Out of memory\n");
391 static void wav_close(struct ast_filestream *s)
394 if (ast_mutex_lock(&wav_lock)) {
395 ast_log(LOG_WARNING, "Unable to lock wav list\n");
399 ast_mutex_unlock(&wav_lock);
400 ast_update_use_count();
401 /* Pad to even length */
402 if (lseek(s->fd, 0, SEEK_END) & 0x1)
403 write(s->fd, &zero, 1);
409 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
413 /* Send a frame from the file to the appropriate channel */
415 s->fr.frametype = AST_FRAME_VOICE;
416 s->fr.subclass = AST_FORMAT_GSM;
417 s->fr.offset = AST_FRIENDLY_OFFSET;
422 /* Just return a frame based on the second GSM frame */
423 s->fr.data = s->gsm + 33;
425 if ((res = read(s->fd, msdata, 65)) != 65) {
426 if (res && (res != 1))
427 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
430 /* Convert from MS format to two real GSM frames */
431 conv65(msdata, s->gsm);
434 s->secondhalf = !s->secondhalf;
439 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
445 if (f->frametype != AST_FRAME_VOICE) {
446 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
449 if (f->subclass != AST_FORMAT_GSM) {
450 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
453 if (!(f->datalen % 65))
455 while(len < f->datalen) {
458 if ((res = write(fs->fd, f->data + len, 65)) != 65) {
459 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
462 update_header(fs->fd);
465 if (fs->secondhalf) {
466 memcpy(fs->gsm + 33, f->data + len, 33);
467 conv66(fs->gsm, msdata);
468 if ((res = write(fs->fd, msdata, 65)) != 65) {
469 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
472 update_header(fs->fd);
474 /* Copy the data and do nothing */
475 memcpy(fs->gsm, f->data + len, 33);
477 fs->secondhalf = !fs->secondhalf;
484 static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
486 off_t offset=0,distance,cur,min,max;
488 cur = lseek(fs->fd, 0, SEEK_CUR);
489 max = lseek(fs->fd, 0, SEEK_END);
490 /* I'm getting sloppy here, I'm only going to go to even splits of the 2
491 * frames, if you want tighter cuts use format_gsm, format_pcm, or format_wav */
492 distance = (sample_offset/320) * 65;
493 if(whence == SEEK_SET)
494 offset = distance + min;
495 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
496 offset = distance + cur;
497 else if(whence == SEEK_END)
498 offset = max - distance;
499 /* always protect against seeking past end of header */
500 offset = (offset < min)?min:offset;
501 if (whence != SEEK_FORCECUR) {
502 offset = (offset > max)?max:offset;
503 } else if (offset > max) {
505 lseek(fs->fd, 0, SEEK_END);
506 for (i=0; i< (offset - max) / 65; i++) {
507 write(fs->fd, msgsm_silence, 65);
511 return lseek(fs->fd, offset, SEEK_SET);
514 static int wav_trunc(struct ast_filestream *fs)
516 if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)))
518 return update_header(fs->fd);
521 static long wav_tell(struct ast_filestream *fs)
524 offset = lseek(fs->fd, 0, SEEK_CUR);
525 /* since this will most likely be used later in play or record, lets stick
526 * to that level of resolution, just even frames boundaries */
527 return (offset - 52)/65*320;
530 static char *wav_getcomment(struct ast_filestream *s)
537 return ast_format_register(name, exts, AST_FORMAT_GSM,
553 return ast_format_unregister(name);
559 if (ast_mutex_lock(&wav_lock)) {
560 ast_log(LOG_WARNING, "Unable to lock wav list\n");
564 ast_mutex_unlock(&wav_lock);
576 return ASTERISK_GPL_KEY;