format_wav_gsm.c 14 KB

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