format_mp3.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Anthony Minessale <anthmct@yahoo.com>
  5. *
  6. * Derived from other asterisk sound formats by
  7. * Mark Spencer <markster@linux-support.net>
  8. *
  9. * Thanks to mpglib from http://www.mpg123.org/
  10. * and Chris Stenton [jacs@gnome.co.uk]
  11. * for coding the ability to play stereo and non-8khz files
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. * This program is free software, distributed under the terms of
  19. * the GNU General Public License Version 2. See the LICENSE file
  20. * at the top of the source tree.
  21. */
  22. /*!
  23. * \file
  24. * \brief MP3 Format Handler
  25. * \ingroup formats
  26. */
  27. /*** MODULEINFO
  28. <defaultenabled>no</defaultenabled>
  29. <support_level>extended</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "mp3/mpg123.h"
  34. #include "mp3/mpglib.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/mod_format.h"
  37. #include "asterisk/logger.h"
  38. #define MP3_BUFLEN 320
  39. #define MP3_SCACHE 16384
  40. #define MP3_DCACHE 8192
  41. struct mp3_private {
  42. char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
  43. char empty; /* Empty character */
  44. int lasttimeout;
  45. int maxlen;
  46. struct timeval last;
  47. struct mpstr mp;
  48. char sbuf[MP3_SCACHE];
  49. char dbuf[MP3_DCACHE];
  50. int buflen;
  51. int sbuflen;
  52. int dbuflen;
  53. int dbufoffset;
  54. int sbufoffset;
  55. int lastseek;
  56. int offset;
  57. long seek;
  58. };
  59. static const char name[] = "mp3";
  60. #define BLOCKSIZE 160
  61. #define OUTSCALE 4096
  62. #define GAIN -4 /* 2^GAIN is the multiple to increase the volume by */
  63. #if __BYTE_ORDER == __LITTLE_ENDIAN
  64. #define htoll(b) (b)
  65. #define htols(b) (b)
  66. #define ltohl(b) (b)
  67. #define ltohs(b) (b)
  68. #else
  69. #if __BYTE_ORDER == __BIG_ENDIAN
  70. #define htoll(b) \
  71. (((((b) ) & 0xFF) << 24) | \
  72. ((((b) >> 8) & 0xFF) << 16) | \
  73. ((((b) >> 16) & 0xFF) << 8) | \
  74. ((((b) >> 24) & 0xFF) ))
  75. #define htols(b) \
  76. (((((b) ) & 0xFF) << 8) | \
  77. ((((b) >> 8) & 0xFF) ))
  78. #define ltohl(b) htoll(b)
  79. #define ltohs(b) htols(b)
  80. #else
  81. #error "Endianess not defined"
  82. #endif
  83. #endif
  84. static int mp3_open(struct ast_filestream *s)
  85. {
  86. struct mp3_private *p = s->_private;
  87. InitMP3(&p->mp, OUTSCALE);
  88. return 0;
  89. }
  90. static void mp3_close(struct ast_filestream *s)
  91. {
  92. struct mp3_private *p = s->_private;
  93. ExitMP3(&p->mp);
  94. return;
  95. }
  96. static int mp3_squeue(struct ast_filestream *s)
  97. {
  98. struct mp3_private *p = s->_private;
  99. int res=0;
  100. p->lastseek = ftell(s->f);
  101. p->sbuflen = fread(p->sbuf, 1, MP3_SCACHE, s->f);
  102. if(p->sbuflen < 0) {
  103. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", p->sbuflen, strerror(errno));
  104. return -1;
  105. }
  106. res = decodeMP3(&p->mp,p->sbuf,p->sbuflen,p->dbuf,MP3_DCACHE,&p->dbuflen);
  107. if(res != MP3_OK)
  108. return -1;
  109. p->sbuflen -= p->dbuflen;
  110. p->dbufoffset = 0;
  111. return 0;
  112. }
  113. static int mp3_dqueue(struct ast_filestream *s)
  114. {
  115. struct mp3_private *p = s->_private;
  116. int res=0;
  117. if((res = decodeMP3(&p->mp,NULL,0,p->dbuf,MP3_DCACHE,&p->dbuflen)) == MP3_OK) {
  118. p->sbuflen -= p->dbuflen;
  119. p->dbufoffset = 0;
  120. }
  121. return res;
  122. }
  123. static int mp3_queue(struct ast_filestream *s)
  124. {
  125. struct mp3_private *p = s->_private;
  126. int res = 0, bytes = 0;
  127. if(p->seek) {
  128. ExitMP3(&p->mp);
  129. InitMP3(&p->mp, OUTSCALE);
  130. fseek(s->f, 0, SEEK_SET);
  131. p->sbuflen = p->dbuflen = p->offset = 0;
  132. while(p->offset < p->seek) {
  133. if(mp3_squeue(s))
  134. return -1;
  135. while(p->offset < p->seek && ((res = mp3_dqueue(s))) == MP3_OK) {
  136. for(bytes = 0 ; bytes < p->dbuflen ; bytes++) {
  137. p->dbufoffset++;
  138. p->offset++;
  139. if(p->offset >= p->seek)
  140. break;
  141. }
  142. }
  143. if(res == MP3_ERR)
  144. return -1;
  145. }
  146. p->seek = 0;
  147. return 0;
  148. }
  149. if(p->dbuflen == 0) {
  150. if(p->sbuflen) {
  151. res = mp3_dqueue(s);
  152. if(res == MP3_ERR)
  153. return -1;
  154. }
  155. if(! p->sbuflen || res != MP3_OK) {
  156. if(mp3_squeue(s))
  157. return -1;
  158. }
  159. }
  160. return 0;
  161. }
  162. static struct ast_frame *mp3_read(struct ast_filestream *s, int *whennext)
  163. {
  164. struct mp3_private *p = s->_private;
  165. int delay =0;
  166. int save=0;
  167. /* Send a frame from the file to the appropriate channel */
  168. if(mp3_queue(s))
  169. return NULL;
  170. if(p->dbuflen) {
  171. for(p->buflen=0; p->buflen < MP3_BUFLEN && p->buflen < p->dbuflen; p->buflen++) {
  172. s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[p->buflen+p->dbufoffset];
  173. p->sbufoffset++;
  174. }
  175. p->dbufoffset += p->buflen;
  176. p->dbuflen -= p->buflen;
  177. if(p->buflen < MP3_BUFLEN) {
  178. if(mp3_queue(s))
  179. return NULL;
  180. for(save = p->buflen; p->buflen < MP3_BUFLEN; p->buflen++) {
  181. s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[(p->buflen-save)+p->dbufoffset];
  182. p->sbufoffset++;
  183. }
  184. p->dbufoffset += (MP3_BUFLEN - save);
  185. p->dbuflen -= (MP3_BUFLEN - save);
  186. }
  187. }
  188. p->offset += p->buflen;
  189. delay = p->buflen/2;
  190. s->fr.frametype = AST_FRAME_VOICE;
  191. s->fr.subclass.codec = AST_FORMAT_SLINEAR;
  192. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, p->buflen);
  193. s->fr.mallocd = 0;
  194. s->fr.samples = delay;
  195. *whennext = delay;
  196. return &s->fr;
  197. }
  198. static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
  199. {
  200. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  201. return -1;
  202. }
  203. static int mp3_seek(struct ast_filestream *s, off_t sample_offset, int whence)
  204. {
  205. struct mp3_private *p = s->_private;
  206. off_t min,max,cur;
  207. long offset=0,samples;
  208. samples = sample_offset * 2;
  209. min = 0;
  210. fseek(s->f, 0, SEEK_END);
  211. max = ftell(s->f) * 100;
  212. cur = p->offset;
  213. if (whence == SEEK_SET)
  214. offset = samples + min;
  215. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  216. offset = samples + cur;
  217. else if (whence == SEEK_END)
  218. offset = max - samples;
  219. if (whence != SEEK_FORCECUR) {
  220. offset = (offset > max)?max:offset;
  221. }
  222. p->seek = offset;
  223. return fseek(s->f, offset, SEEK_SET);
  224. }
  225. static int mp3_rewrite(struct ast_filestream *s, const char *comment)
  226. {
  227. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  228. return -1;
  229. }
  230. static int mp3_trunc(struct ast_filestream *s)
  231. {
  232. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  233. return -1;
  234. }
  235. static off_t mp3_tell(struct ast_filestream *s)
  236. {
  237. struct mp3_private *p = s->_private;
  238. return p->offset/2;
  239. }
  240. static char *mp3_getcomment(struct ast_filestream *s)
  241. {
  242. return NULL;
  243. }
  244. static const struct ast_format mp3_f = {
  245. .name = "mp3",
  246. .exts = "mp3",
  247. .format = AST_FORMAT_SLINEAR,
  248. .open = mp3_open,
  249. .write = mp3_write,
  250. .rewrite = mp3_rewrite,
  251. .seek = mp3_seek,
  252. .trunc = mp3_trunc,
  253. .tell = mp3_tell,
  254. .read = mp3_read,
  255. .close = mp3_close,
  256. .getcomment = mp3_getcomment,
  257. .buf_size = MP3_BUFLEN + AST_FRIENDLY_OFFSET,
  258. .desc_size = sizeof(struct mp3_private),
  259. };
  260. static int load_module(void)
  261. {
  262. InitMP3Constants();
  263. return ast_format_register(&mp3_f);
  264. }
  265. static int unload_module(void)
  266. {
  267. return ast_format_unregister(name);
  268. }
  269. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MP3 format [Any rate but 8000hz mono is optimal]");