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>
32 #include <machine/endian.h>
36 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
38 /* Portions of the conversion code are by guido@sienanet.it */
40 /* begin binary data: */
41 char msgsm_silence[] = /* 65 */
42 {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
43 ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
44 ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
45 ,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48
46 ,0x92,0x24,0x49,0x92,0x00};
47 /* end binary data. size = 65 bytes */
49 struct ast_filestream {
50 void *reserved[AST_RESERVED_POINTERS];
51 /* Believe it or not, we must decode/recode to account for the
53 /* This is what a filestream means to us */
54 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 static ast_mutex_t wav_lock = AST_MUTEX_INITIALIZER;
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);
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, 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 ast_mutex_unlock(&wav_lock);
395 ast_update_use_count();
396 /* Pad to even length */
398 write(s->fd, &zero, 1);
404 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
408 /* Send a frame from the file to the appropriate channel */
410 s->fr.frametype = AST_FRAME_VOICE;
411 s->fr.subclass = AST_FORMAT_GSM;
412 s->fr.offset = AST_FRIENDLY_OFFSET;
417 /* Just return a frame based on the second GSM frame */
418 s->fr.data = s->gsm + 33;
420 if ((res = read(s->fd, msdata, 65)) != 65) {
421 if (res && (res != 1))
422 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
425 /* Convert from MS format to two real GSM frames */
426 conv65(msdata, s->gsm);
429 s->secondhalf = !s->secondhalf;
434 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
440 if (f->frametype != AST_FRAME_VOICE) {
441 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
444 if (f->subclass != AST_FORMAT_GSM) {
445 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
448 if (!(f->datalen % 65))
450 while(len < f->datalen) {
453 if ((res = write(fs->fd, f->data + len, 65)) != 65) {
454 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
458 update_header(fs->fd);
461 if (fs->secondhalf) {
462 memcpy(fs->gsm + 33, f->data + len, 33);
463 conv66(fs->gsm, msdata);
464 if ((res = write(fs->fd, msdata, 65)) != 65) {
465 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
469 update_header(fs->fd);
471 /* Copy the data and do nothing */
472 memcpy(fs->gsm, f->data + len, 33);
474 fs->secondhalf = !fs->secondhalf;
481 static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
483 off_t offset=0,distance,cur,min,max;
485 cur = lseek(fs->fd, 0, SEEK_CUR);
486 max = lseek(fs->fd, 0, SEEK_END);
487 /* I'm getting sloppy here, I'm only going to go to even splits of the 2
488 * frames, if you want tighter cuts use format_gsm, format_pcm, or format_wav */
489 distance = (sample_offset/320) * 65;
490 if(whence == SEEK_SET)
491 offset = distance + min;
492 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
493 offset = distance + cur;
494 else if(whence == SEEK_END)
495 offset = max - distance;
496 if (whence != SEEK_FORCECUR) {
497 offset = (offset < min)?min:offset;
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;