format_wav_gsm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Save GSM in the proprietary Microsoft format.
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/channel.h>
  15. #include <asterisk/file.h>
  16. #include <asterisk/logger.h>
  17. #include <asterisk/sched.h>
  18. #include <asterisk/module.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <stdlib.h>
  22. #include <sys/time.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include "asterisk/endian.h"
  28. #include "msgsm.h"
  29. /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
  30. /* Portions of the conversion code are by guido@sienanet.it */
  31. /* begin binary data: */
  32. char msgsm_silence[] = /* 65 */
  33. {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
  34. ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
  35. ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
  36. ,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48
  37. ,0x92,0x24,0x49,0x92,0x00};
  38. /* end binary data. size = 65 bytes */
  39. struct ast_filestream {
  40. void *reserved[AST_RESERVED_POINTERS];
  41. /* Believe it or not, we must decode/recode to account for the
  42. weird MS format */
  43. /* This is what a filestream means to us */
  44. int fd; /* Descriptor */
  45. struct ast_frame fr; /* Frame information */
  46. char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
  47. char empty; /* Empty character */
  48. unsigned char gsm[66]; /* Two Real GSM Frames */
  49. int foffset;
  50. int secondhalf; /* Are we on the second half */
  51. struct timeval last;
  52. };
  53. AST_MUTEX_DEFINE_STATIC(wav_lock);
  54. static int glistcnt = 0;
  55. static char *name = "wav49";
  56. static char *desc = "Microsoft WAV format (Proprietary GSM)";
  57. static char *exts = "WAV|wav49";
  58. #if __BYTE_ORDER == __LITTLE_ENDIAN
  59. #define htoll(b) (b)
  60. #define htols(b) (b)
  61. #define ltohl(b) (b)
  62. #define ltohs(b) (b)
  63. #else
  64. #if __BYTE_ORDER == __BIG_ENDIAN
  65. #define htoll(b) \
  66. (((((b) ) & 0xFF) << 24) | \
  67. ((((b) >> 8) & 0xFF) << 16) | \
  68. ((((b) >> 16) & 0xFF) << 8) | \
  69. ((((b) >> 24) & 0xFF) ))
  70. #define htols(b) \
  71. (((((b) ) & 0xFF) << 8) | \
  72. ((((b) >> 8) & 0xFF) ))
  73. #define ltohl(b) htoll(b)
  74. #define ltohs(b) htols(b)
  75. #else
  76. #error "Endianess not defined"
  77. #endif
  78. #endif
  79. static int check_header(int fd)
  80. {
  81. int type, size, formtype;
  82. int fmt, hsize, fact;
  83. short format, chans;
  84. int freq;
  85. int data;
  86. if (read(fd, &type, 4) != 4) {
  87. ast_log(LOG_WARNING, "Read failed (type)\n");
  88. return -1;
  89. }
  90. if (read(fd, &size, 4) != 4) {
  91. ast_log(LOG_WARNING, "Read failed (size)\n");
  92. return -1;
  93. }
  94. size = ltohl(size);
  95. if (read(fd, &formtype, 4) != 4) {
  96. ast_log(LOG_WARNING, "Read failed (formtype)\n");
  97. return -1;
  98. }
  99. if (memcmp(&type, "RIFF", 4)) {
  100. ast_log(LOG_WARNING, "Does not begin with RIFF\n");
  101. return -1;
  102. }
  103. if (memcmp(&formtype, "WAVE", 4)) {
  104. ast_log(LOG_WARNING, "Does not contain WAVE\n");
  105. return -1;
  106. }
  107. if (read(fd, &fmt, 4) != 4) {
  108. ast_log(LOG_WARNING, "Read failed (fmt)\n");
  109. return -1;
  110. }
  111. if (memcmp(&fmt, "fmt ", 4)) {
  112. ast_log(LOG_WARNING, "Does not say fmt\n");
  113. return -1;
  114. }
  115. if (read(fd, &hsize, 4) != 4) {
  116. ast_log(LOG_WARNING, "Read failed (formtype)\n");
  117. return -1;
  118. }
  119. if (ltohl(hsize) != 20) {
  120. ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
  121. return -1;
  122. }
  123. if (read(fd, &format, 2) != 2) {
  124. ast_log(LOG_WARNING, "Read failed (format)\n");
  125. return -1;
  126. }
  127. if (ltohs(format) != 49) {
  128. ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
  129. return -1;
  130. }
  131. if (read(fd, &chans, 2) != 2) {
  132. ast_log(LOG_WARNING, "Read failed (format)\n");
  133. return -1;
  134. }
  135. if (ltohs(chans) != 1) {
  136. ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
  137. return -1;
  138. }
  139. if (read(fd, &freq, 4) != 4) {
  140. ast_log(LOG_WARNING, "Read failed (freq)\n");
  141. return -1;
  142. }
  143. if (ltohl(freq) != 8000) {
  144. ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
  145. return -1;
  146. }
  147. /* Ignore the byte frequency */
  148. if (read(fd, &freq, 4) != 4) {
  149. ast_log(LOG_WARNING, "Read failed (X_1)\n");
  150. return -1;
  151. }
  152. /* Ignore the two weird fields */
  153. if (read(fd, &freq, 4) != 4) {
  154. ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
  155. return -1;
  156. }
  157. /* Ignore the byte frequency */
  158. if (read(fd, &freq, 4) != 4) {
  159. ast_log(LOG_WARNING, "Read failed (Y_1)\n");
  160. return -1;
  161. }
  162. /* Check for the word fact */
  163. if (read(fd, &fact, 4) != 4) {
  164. ast_log(LOG_WARNING, "Read failed (fact)\n");
  165. return -1;
  166. }
  167. if (memcmp(&fact, "fact", 4)) {
  168. ast_log(LOG_WARNING, "Does not say fact\n");
  169. return -1;
  170. }
  171. /* Ignore the "fact value" */
  172. if (read(fd, &fact, 4) != 4) {
  173. ast_log(LOG_WARNING, "Read failed (fact header)\n");
  174. return -1;
  175. }
  176. if (read(fd, &fact, 4) != 4) {
  177. ast_log(LOG_WARNING, "Read failed (fact value)\n");
  178. return -1;
  179. }
  180. /* Check for the word data */
  181. if (read(fd, &data, 4) != 4) {
  182. ast_log(LOG_WARNING, "Read failed (data)\n");
  183. return -1;
  184. }
  185. if (memcmp(&data, "data", 4)) {
  186. ast_log(LOG_WARNING, "Does not say data\n");
  187. return -1;
  188. }
  189. /* Ignore the data length */
  190. if (read(fd, &data, 4) != 4) {
  191. ast_log(LOG_WARNING, "Read failed (data)\n");
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. static int update_header(int fd)
  197. {
  198. off_t cur,end,bytes;
  199. int datalen,filelen;
  200. cur = lseek(fd, 0, SEEK_CUR);
  201. end = lseek(fd, 0, SEEK_END);
  202. /* in a gsm WAV, data starts 60 bytes in */
  203. bytes = end - 60;
  204. datalen = htoll((bytes + 1) & ~0x1);
  205. filelen = htoll(52 + ((bytes + 1) & ~0x1));
  206. if (cur < 0) {
  207. ast_log(LOG_WARNING, "Unable to find our position\n");
  208. return -1;
  209. }
  210. if (lseek(fd, 4, SEEK_SET) != 4) {
  211. ast_log(LOG_WARNING, "Unable to set our position\n");
  212. return -1;
  213. }
  214. if (write(fd, &filelen, 4) != 4) {
  215. ast_log(LOG_WARNING, "Unable to set write file size\n");
  216. return -1;
  217. }
  218. if (lseek(fd, 56, SEEK_SET) != 56) {
  219. ast_log(LOG_WARNING, "Unable to set our position\n");
  220. return -1;
  221. }
  222. if (write(fd, &datalen, 4) != 4) {
  223. ast_log(LOG_WARNING, "Unable to set write datalen\n");
  224. return -1;
  225. }
  226. if (lseek(fd, cur, SEEK_SET) != cur) {
  227. ast_log(LOG_WARNING, "Unable to return to position\n");
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. static int write_header(int fd)
  233. {
  234. unsigned int hz=htoll(8000);
  235. unsigned int bhz = htoll(1625);
  236. unsigned int hs = htoll(20);
  237. unsigned short fmt = htols(49);
  238. unsigned short chans = htols(1);
  239. unsigned int fhs = htoll(4);
  240. unsigned int x_1 = htoll(65);
  241. unsigned short x_2 = htols(2);
  242. unsigned short x_3 = htols(320);
  243. unsigned int y_1 = htoll(20160);
  244. unsigned int size = htoll(0);
  245. /* Write a GSM header, ignoring sizes which will be filled in later */
  246. if (write(fd, "RIFF", 4) != 4) {
  247. ast_log(LOG_WARNING, "Unable to write header\n");
  248. return -1;
  249. }
  250. if (write(fd, &size, 4) != 4) {
  251. ast_log(LOG_WARNING, "Unable to write header\n");
  252. return -1;
  253. }
  254. if (write(fd, "WAVEfmt ", 8) != 8) {
  255. ast_log(LOG_WARNING, "Unable to write header\n");
  256. return -1;
  257. }
  258. if (write(fd, &hs, 4) != 4) {
  259. ast_log(LOG_WARNING, "Unable to write header\n");
  260. return -1;
  261. }
  262. if (write(fd, &fmt, 2) != 2) {
  263. ast_log(LOG_WARNING, "Unable to write header\n");
  264. return -1;
  265. }
  266. if (write(fd, &chans, 2) != 2) {
  267. ast_log(LOG_WARNING, "Unable to write header\n");
  268. return -1;
  269. }
  270. if (write(fd, &hz, 4) != 4) {
  271. ast_log(LOG_WARNING, "Unable to write header\n");
  272. return -1;
  273. }
  274. if (write(fd, &bhz, 4) != 4) {
  275. ast_log(LOG_WARNING, "Unable to write header\n");
  276. return -1;
  277. }
  278. if (write(fd, &x_1, 4) != 4) {
  279. ast_log(LOG_WARNING, "Unable to write header\n");
  280. return -1;
  281. }
  282. if (write(fd, &x_2, 2) != 2) {
  283. ast_log(LOG_WARNING, "Unable to write header\n");
  284. return -1;
  285. }
  286. if (write(fd, &x_3, 2) != 2) {
  287. ast_log(LOG_WARNING, "Unable to write header\n");
  288. return -1;
  289. }
  290. if (write(fd, "fact", 4) != 4) {
  291. ast_log(LOG_WARNING, "Unable to write header\n");
  292. return -1;
  293. }
  294. if (write(fd, &fhs, 4) != 4) {
  295. ast_log(LOG_WARNING, "Unable to write header\n");
  296. return -1;
  297. }
  298. if (write(fd, &y_1, 4) != 4) {
  299. ast_log(LOG_WARNING, "Unable to write header\n");
  300. return -1;
  301. }
  302. if (write(fd, "data", 4) != 4) {
  303. ast_log(LOG_WARNING, "Unable to write header\n");
  304. return -1;
  305. }
  306. if (write(fd, &size, 4) != 4) {
  307. ast_log(LOG_WARNING, "Unable to write header\n");
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. static struct ast_filestream *wav_open(int fd)
  313. {
  314. /* We don't have any header to read or anything really, but
  315. if we did, it would go here. We also might want to check
  316. and be sure it's a valid file. */
  317. struct ast_filestream *tmp;
  318. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  319. memset(tmp, 0, sizeof(struct ast_filestream));
  320. if (check_header(fd)) {
  321. free(tmp);
  322. return NULL;
  323. }
  324. if (ast_mutex_lock(&wav_lock)) {
  325. ast_log(LOG_WARNING, "Unable to lock wav list\n");
  326. free(tmp);
  327. return NULL;
  328. }
  329. tmp->fd = fd;
  330. tmp->fr.data = tmp->gsm;
  331. tmp->fr.frametype = AST_FRAME_VOICE;
  332. tmp->fr.subclass = AST_FORMAT_GSM;
  333. /* datalen will vary for each frame */
  334. tmp->fr.src = name;
  335. tmp->fr.mallocd = 0;
  336. tmp->secondhalf = 0;
  337. glistcnt++;
  338. ast_mutex_unlock(&wav_lock);
  339. ast_update_use_count();
  340. }
  341. return tmp;
  342. }
  343. static struct ast_filestream *wav_rewrite(int fd, char *comment)
  344. {
  345. /* We don't have any header to read or anything really, but
  346. if we did, it would go here. We also might want to check
  347. and be sure it's a valid file. */
  348. struct ast_filestream *tmp;
  349. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  350. memset(tmp, 0, sizeof(struct ast_filestream));
  351. if (write_header(fd)) {
  352. free(tmp);
  353. return NULL;
  354. }
  355. if (ast_mutex_lock(&wav_lock)) {
  356. ast_log(LOG_WARNING, "Unable to lock wav list\n");
  357. free(tmp);
  358. return NULL;
  359. }
  360. tmp->fd = fd;
  361. glistcnt++;
  362. ast_mutex_unlock(&wav_lock);
  363. ast_update_use_count();
  364. } else
  365. ast_log(LOG_WARNING, "Out of memory\n");
  366. return tmp;
  367. }
  368. static void wav_close(struct ast_filestream *s)
  369. {
  370. char zero = 0;
  371. if (ast_mutex_lock(&wav_lock)) {
  372. ast_log(LOG_WARNING, "Unable to lock wav list\n");
  373. return;
  374. }
  375. glistcnt--;
  376. ast_mutex_unlock(&wav_lock);
  377. ast_update_use_count();
  378. /* Pad to even length */
  379. if (lseek(s->fd, 0, SEEK_END) & 0x1)
  380. write(s->fd, &zero, 1);
  381. close(s->fd);
  382. free(s);
  383. s = NULL;
  384. }
  385. static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
  386. {
  387. int res;
  388. char msdata[66];
  389. /* Send a frame from the file to the appropriate channel */
  390. s->fr.frametype = AST_FRAME_VOICE;
  391. s->fr.subclass = AST_FORMAT_GSM;
  392. s->fr.offset = AST_FRIENDLY_OFFSET;
  393. s->fr.samples = 160;
  394. s->fr.datalen = 33;
  395. s->fr.mallocd = 0;
  396. if (s->secondhalf) {
  397. /* Just return a frame based on the second GSM frame */
  398. s->fr.data = s->gsm + 33;
  399. } else {
  400. if ((res = read(s->fd, msdata, 65)) != 65) {
  401. if (res && (res != 1))
  402. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  403. return NULL;
  404. }
  405. /* Convert from MS format to two real GSM frames */
  406. conv65(msdata, s->gsm);
  407. s->fr.data = s->gsm;
  408. }
  409. s->secondhalf = !s->secondhalf;
  410. *whennext = 160;
  411. return &s->fr;
  412. }
  413. static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
  414. {
  415. int res;
  416. char msdata[66];
  417. int len =0;
  418. int alreadyms=0;
  419. if (f->frametype != AST_FRAME_VOICE) {
  420. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  421. return -1;
  422. }
  423. if (f->subclass != AST_FORMAT_GSM) {
  424. ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
  425. return -1;
  426. }
  427. if (!(f->datalen % 65))
  428. alreadyms = 1;
  429. while(len < f->datalen) {
  430. if (alreadyms) {
  431. fs->secondhalf = 0;
  432. if ((res = write(fs->fd, f->data + len, 65)) != 65) {
  433. ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
  434. return -1;
  435. }
  436. update_header(fs->fd);
  437. len += 65;
  438. } else {
  439. if (fs->secondhalf) {
  440. memcpy(fs->gsm + 33, f->data + len, 33);
  441. conv66(fs->gsm, msdata);
  442. if ((res = write(fs->fd, msdata, 65)) != 65) {
  443. ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
  444. return -1;
  445. }
  446. update_header(fs->fd);
  447. } else {
  448. /* Copy the data and do nothing */
  449. memcpy(fs->gsm, f->data + len, 33);
  450. }
  451. fs->secondhalf = !fs->secondhalf;
  452. len += 33;
  453. }
  454. }
  455. return 0;
  456. }
  457. static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
  458. {
  459. off_t offset=0,distance,cur,min,max;
  460. min = 60;
  461. cur = lseek(fs->fd, 0, SEEK_CUR);
  462. max = lseek(fs->fd, 0, SEEK_END);
  463. /* I'm getting sloppy here, I'm only going to go to even splits of the 2
  464. * frames, if you want tighter cuts use format_gsm, format_pcm, or format_wav */
  465. distance = (sample_offset/320) * 65;
  466. if(whence == SEEK_SET)
  467. offset = distance + min;
  468. else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
  469. offset = distance + cur;
  470. else if(whence == SEEK_END)
  471. offset = max - distance;
  472. // always protect against seeking past end of header
  473. offset = (offset < min)?min:offset;
  474. if (whence != SEEK_FORCECUR) {
  475. offset = (offset > max)?max:offset;
  476. } else if (offset > max) {
  477. int i;
  478. lseek(fs->fd, 0, SEEK_END);
  479. for (i=0; i< (offset - max) / 65; i++) {
  480. write(fs->fd, msgsm_silence, 65);
  481. }
  482. }
  483. fs->secondhalf = 0;
  484. return lseek(fs->fd, offset, SEEK_SET);
  485. }
  486. static int wav_trunc(struct ast_filestream *fs)
  487. {
  488. if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)))
  489. return -1;
  490. return update_header(fs->fd);
  491. }
  492. static long wav_tell(struct ast_filestream *fs)
  493. {
  494. off_t offset;
  495. offset = lseek(fs->fd, 0, SEEK_CUR);
  496. /* since this will most likely be used later in play or record, lets stick
  497. * to that level of resolution, just even frames boundaries */
  498. return (offset - 52)/65*320;
  499. }
  500. static char *wav_getcomment(struct ast_filestream *s)
  501. {
  502. return NULL;
  503. }
  504. int load_module()
  505. {
  506. return ast_format_register(name, exts, AST_FORMAT_GSM,
  507. wav_open,
  508. wav_rewrite,
  509. wav_write,
  510. wav_seek,
  511. wav_trunc,
  512. wav_tell,
  513. wav_read,
  514. wav_close,
  515. wav_getcomment);
  516. }
  517. int unload_module()
  518. {
  519. return ast_format_unregister(name);
  520. }
  521. int usecount()
  522. {
  523. int res;
  524. if (ast_mutex_lock(&wav_lock)) {
  525. ast_log(LOG_WARNING, "Unable to lock wav list\n");
  526. return -1;
  527. }
  528. res = glistcnt;
  529. ast_mutex_unlock(&wav_lock);
  530. return res;
  531. }
  532. char *description()
  533. {
  534. return desc;
  535. }
  536. char *key()
  537. {
  538. return ASTERISK_GPL_KEY;
  539. }