2 * Asterisk -- A telephony toolkit for Linux.
4 * Save GSM 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 <netinet/in.h>
21 #include <arpa/inet.h>
31 #include <machine/endian.h>
35 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
37 /* Portions of the conversion code are by guido@sienanet.it */
39 /* begin binary data: */
40 char msgsm_silence[] = /* 65 */
41 {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
42 ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
43 ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
44 ,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48
45 ,0x92,0x24,0x49,0x92,0x00};
46 /* end binary data. size = 65 bytes */
48 struct ast_filestream {
49 void *reserved[AST_RESERVED_POINTERS];
50 /* Believe it or not, we must decode/recode to account for the
52 /* This is what a filestream means to us */
53 int fd; /* Descriptor */
55 struct ast_frame fr; /* Frame information */
56 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
57 char empty; /* Empty character */
58 unsigned char gsm[66]; /* Two Real GSM Frames */
60 int secondhalf; /* Are we on the second half */
65 AST_MUTEX_DEFINE_STATIC(wav_lock);
66 static int glistcnt = 0;
68 static char *name = "wav49";
69 static char *desc = "Microsoft WAV format (Proprietary GSM)";
70 static char *exts = "WAV|wav49";
72 #if __BYTE_ORDER == __LITTLE_ENDIAN
78 #if __BYTE_ORDER == __BIG_ENDIAN
80 (((((b) ) & 0xFF) << 24) | \
81 ((((b) >> 8) & 0xFF) << 16) | \
82 ((((b) >> 16) & 0xFF) << 8) | \
83 ((((b) >> 24) & 0xFF) ))
85 (((((b) ) & 0xFF) << 8) | \
86 ((((b) >> 8) & 0xFF) ))
87 #define ltohl(b) htoll(b)
88 #define ltohs(b) htols(b)
90 #error "Endianess not defined"
95 static int check_header(int fd)
97 int type, size, formtype;
102 if (read(fd, &type, 4) != 4) {
103 ast_log(LOG_WARNING, "Read failed (type)\n");
106 if (read(fd, &size, 4) != 4) {
107 ast_log(LOG_WARNING, "Read failed (size)\n");
111 if (read(fd, &formtype, 4) != 4) {
112 ast_log(LOG_WARNING, "Read failed (formtype)\n");
115 if (memcmp(&type, "RIFF", 4)) {
116 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
119 if (memcmp(&formtype, "WAVE", 4)) {
120 ast_log(LOG_WARNING, "Does not contain WAVE\n");
123 if (read(fd, &fmt, 4) != 4) {
124 ast_log(LOG_WARNING, "Read failed (fmt)\n");
127 if (memcmp(&fmt, "fmt ", 4)) {
128 ast_log(LOG_WARNING, "Does not say fmt\n");
131 if (read(fd, &hsize, 4) != 4) {
132 ast_log(LOG_WARNING, "Read failed (formtype)\n");
135 if (ltohl(hsize) != 20) {
136 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
139 if (read(fd, &format, 2) != 2) {
140 ast_log(LOG_WARNING, "Read failed (format)\n");
143 if (ltohs(format) != 49) {
144 ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
147 if (read(fd, &chans, 2) != 2) {
148 ast_log(LOG_WARNING, "Read failed (format)\n");
151 if (ltohs(chans) != 1) {
152 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
155 if (read(fd, &freq, 4) != 4) {
156 ast_log(LOG_WARNING, "Read failed (freq)\n");
159 if (ltohl(freq) != 8000) {
160 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
163 /* Ignore the byte frequency */
164 if (read(fd, &freq, 4) != 4) {
165 ast_log(LOG_WARNING, "Read failed (X_1)\n");
168 /* Ignore the two weird fields */
169 if (read(fd, &freq, 4) != 4) {
170 ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
173 /* Ignore the byte frequency */
174 if (read(fd, &freq, 4) != 4) {
175 ast_log(LOG_WARNING, "Read failed (Y_1)\n");
178 /* Check for the word fact */
179 if (read(fd, &fact, 4) != 4) {
180 ast_log(LOG_WARNING, "Read failed (fact)\n");
183 if (memcmp(&fact, "fact", 4)) {
184 ast_log(LOG_WARNING, "Does not say fact\n");
187 /* Ignore the "fact value" */
188 if (read(fd, &fact, 4) != 4) {
189 ast_log(LOG_WARNING, "Read failed (fact header)\n");
192 if (read(fd, &fact, 4) != 4) {
193 ast_log(LOG_WARNING, "Read failed (fact value)\n");
196 /* Check for the word data */
197 if (read(fd, &data, 4) != 4) {
198 ast_log(LOG_WARNING, "Read failed (data)\n");
201 if (memcmp(&data, "data", 4)) {
202 ast_log(LOG_WARNING, "Does not say data\n");
205 /* Ignore the data length */
206 if (read(fd, &data, 4) != 4) {
207 ast_log(LOG_WARNING, "Read failed (data)\n");
213 static int update_header(int fd)
218 cur = lseek(fd, 0, SEEK_CUR);
219 end = lseek(fd, 0, SEEK_END);
220 /* in a gsm WAV, data starts 60 bytes in */
222 datalen = htoll(bytes);
223 filelen = htoll(52 + ((bytes + 1) & ~0x1));
225 ast_log(LOG_WARNING, "Unable to find our position\n");
228 if (lseek(fd, 4, SEEK_SET) != 4) {
229 ast_log(LOG_WARNING, "Unable to set our position\n");
232 if (write(fd, &filelen, 4) != 4) {
233 ast_log(LOG_WARNING, "Unable to set write file size\n");
236 if (lseek(fd, 56, SEEK_SET) != 56) {
237 ast_log(LOG_WARNING, "Unable to set our position\n");
240 if (write(fd, &datalen, 4) != 4) {
241 ast_log(LOG_WARNING, "Unable to set write datalen\n");
244 if (lseek(fd, cur, SEEK_SET) != cur) {
245 ast_log(LOG_WARNING, "Unable to return to position\n");
251 static int write_header(int fd)
253 unsigned int hz=htoll(8000);
254 unsigned int bhz = htoll(1625);
255 unsigned int hs = htoll(20);
256 unsigned short fmt = htols(49);
257 unsigned short chans = htols(1);
258 unsigned int fhs = htoll(4);
259 unsigned int x_1 = htoll(65);
260 unsigned short x_2 = htols(2);
261 unsigned short x_3 = htols(320);
262 unsigned int y_1 = htoll(20160);
263 unsigned int size = htoll(0);
264 /* Write a GSM header, ignoring sizes which will be filled in later */
265 if (write(fd, "RIFF", 4) != 4) {
266 ast_log(LOG_WARNING, "Unable to write header\n");
269 if (write(fd, &size, 4) != 4) {
270 ast_log(LOG_WARNING, "Unable to write header\n");
273 if (write(fd, "WAVEfmt ", 8) != 8) {
274 ast_log(LOG_WARNING, "Unable to write header\n");
277 if (write(fd, &hs, 4) != 4) {
278 ast_log(LOG_WARNING, "Unable to write header\n");
281 if (write(fd, &fmt, 2) != 2) {
282 ast_log(LOG_WARNING, "Unable to write header\n");
285 if (write(fd, &chans, 2) != 2) {
286 ast_log(LOG_WARNING, "Unable to write header\n");
289 if (write(fd, &hz, 4) != 4) {
290 ast_log(LOG_WARNING, "Unable to write header\n");
293 if (write(fd, &bhz, 4) != 4) {
294 ast_log(LOG_WARNING, "Unable to write header\n");
297 if (write(fd, &x_1, 4) != 4) {
298 ast_log(LOG_WARNING, "Unable to write header\n");
301 if (write(fd, &x_2, 2) != 2) {
302 ast_log(LOG_WARNING, "Unable to write header\n");
305 if (write(fd, &x_3, 2) != 2) {
306 ast_log(LOG_WARNING, "Unable to write header\n");
309 if (write(fd, "fact", 4) != 4) {
310 ast_log(LOG_WARNING, "Unable to write header\n");
313 if (write(fd, &fhs, 4) != 4) {
314 ast_log(LOG_WARNING, "Unable to write header\n");
317 if (write(fd, &y_1, 4) != 4) {
318 ast_log(LOG_WARNING, "Unable to write header\n");
321 if (write(fd, "data", 4) != 4) {
322 ast_log(LOG_WARNING, "Unable to write header\n");
325 if (write(fd, &size, 4) != 4) {
326 ast_log(LOG_WARNING, "Unable to write header\n");
332 static struct ast_filestream *wav_open(int fd)
334 /* We don't have any header to read or anything really, but
335 if we did, it would go here. We also might want to check
336 and be sure it's a valid file. */
337 struct ast_filestream *tmp;
338 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
339 memset(tmp, 0, sizeof(struct ast_filestream));
340 if (check_header(fd)) {
344 if (ast_mutex_lock(&wav_lock)) {
345 ast_log(LOG_WARNING, "Unable to lock wav list\n");
350 tmp->fr.data = tmp->gsm;
351 tmp->fr.frametype = AST_FRAME_VOICE;
352 tmp->fr.subclass = AST_FORMAT_GSM;
353 /* datalen will vary for each frame */
358 ast_mutex_unlock(&wav_lock);
359 ast_update_use_count();
364 static struct ast_filestream *wav_rewrite(int fd, char *comment)
366 /* We don't have any header to read or anything really, but
367 if we did, it would go here. We also might want to check
368 and be sure it's a valid file. */
369 struct ast_filestream *tmp;
370 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
371 memset(tmp, 0, sizeof(struct ast_filestream));
372 if (write_header(fd)) {
376 if (ast_mutex_lock(&wav_lock)) {
377 ast_log(LOG_WARNING, "Unable to lock wav list\n");
383 ast_mutex_unlock(&wav_lock);
384 ast_update_use_count();
386 ast_log(LOG_WARNING, "Out of memory\n");
390 static void wav_close(struct ast_filestream *s)
393 ast_mutex_unlock(&wav_lock);
394 ast_update_use_count();
395 /* Pad to even length */
397 write(s->fd, &zero, 1);
403 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
407 /* Send a frame from the file to the appropriate channel */
409 s->fr.frametype = AST_FRAME_VOICE;
410 s->fr.subclass = AST_FORMAT_GSM;
411 s->fr.offset = AST_FRIENDLY_OFFSET;
416 /* Just return a frame based on the second GSM frame */
417 s->fr.data = s->gsm + 33;
419 if ((res = read(s->fd, msdata, 65)) != 65) {
420 if (res && (res != 1))
421 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
424 /* Convert from MS format to two real GSM frames */
425 conv65(msdata, s->gsm);
428 s->secondhalf = !s->secondhalf;
433 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
439 if (f->frametype != AST_FRAME_VOICE) {
440 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
443 if (f->subclass != AST_FORMAT_GSM) {
444 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
447 if (!(f->datalen % 65))
449 while(len < f->datalen) {
452 if ((res = write(fs->fd, f->data + len, 65)) != 65) {
453 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
457 update_header(fs->fd);
460 if (fs->secondhalf) {
461 memcpy(fs->gsm + 33, f->data + len, 33);
462 conv66(fs->gsm, msdata);
463 if ((res = write(fs->fd, msdata, 65)) != 65) {
464 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
468 update_header(fs->fd);
470 /* Copy the data and do nothing */
471 memcpy(fs->gsm, f->data + len, 33);
473 fs->secondhalf = !fs->secondhalf;
480 static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
482 off_t offset=0,distance,cur,min,max;
484 cur = lseek(fs->fd, 0, SEEK_CUR);
485 max = lseek(fs->fd, 0, SEEK_END);
486 /* I'm getting sloppy here, I'm only going to go to even splits of the 2
487 * frames, if you want tighter cuts use format_gsm, format_pcm, or format_wav */
488 distance = (sample_offset/320) * 65;
489 if(whence == SEEK_SET)
490 offset = distance + min;
491 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
492 offset = distance + cur;
493 else if(whence == SEEK_END)
494 offset = max - distance;
495 // always protect against seeking past end of header
496 offset = (offset < min)?min:offset;
497 if (whence != SEEK_FORCECUR) {
498 offset = (offset > max)?max:offset;
499 } else if (offset > max) {
501 lseek(fs->fd, 0, SEEK_END);
502 for (i=0; i< (offset - max) / 65; i++) {
503 write(fs->fd, msgsm_silence, 65);
507 return lseek(fs->fd, offset, SEEK_SET);
510 static int wav_trunc(struct ast_filestream *fs)
512 if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)))
514 return update_header(fs->fd);
517 static long wav_tell(struct ast_filestream *fs)
520 offset = lseek(fs->fd, 0, SEEK_CUR);
521 /* since this will most likely be used later in play or record, lets stick
522 * to that level of resolution, just even frames boundaries */
523 return (offset - 52)/65/320;
526 static char *wav_getcomment(struct ast_filestream *s)
533 return ast_format_register(name, exts, AST_FORMAT_GSM,
549 return ast_format_unregister(name);
555 if (ast_mutex_lock(&wav_lock)) {
556 ast_log(LOG_WARNING, "Unable to lock wav list\n");
560 ast_mutex_unlock(&wav_lock);
572 return ASTERISK_GPL_KEY;