format_mp3.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include "mp3/mpg123.h"
  33. #include "mp3/mpglib.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/mod_format.h"
  36. #include "asterisk/logger.h"
  37. #include "asterisk/format_cache.h"
  38. #define MP3_BUFLEN 320
  39. #define MP3_SCACHE 16384
  40. #define MP3_DCACHE 8192
  41. struct mp3_private {
  42. /*! state for the mp3 decoder */
  43. struct mpstr mp;
  44. /*! buffer to hold mp3 data after read from disk */
  45. char sbuf[MP3_SCACHE];
  46. /*! buffer for slinear audio after being decoded out of sbuf */
  47. char dbuf[MP3_DCACHE];
  48. /*! how much data has been written to the output buffer in the ast_filestream */
  49. int buflen;
  50. /*! how much data has been written to sbuf */
  51. int sbuflen;
  52. /*! how much data is left to be read out of dbuf, starting at dbufoffset */
  53. int dbuflen;
  54. /*! current offset for reading data out of dbuf */
  55. int dbufoffset;
  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. res = ftell(s->f);
  101. p->sbuflen = fread(p->sbuf, 1, MP3_SCACHE, s->f);
  102. if (p->sbuflen < MP3_SCACHE) {
  103. if (ferror(s->f)) {
  104. ast_log(LOG_WARNING, "Error while reading MP3 file: %s\n", strerror(errno));
  105. return -1;
  106. }
  107. }
  108. res = decodeMP3(&p->mp,p->sbuf,p->sbuflen,p->dbuf,MP3_DCACHE,&p->dbuflen);
  109. if(res != MP3_OK)
  110. return -1;
  111. p->sbuflen -= p->dbuflen;
  112. p->dbufoffset = 0;
  113. return 0;
  114. }
  115. static int mp3_dqueue(struct ast_filestream *s)
  116. {
  117. struct mp3_private *p = s->_private;
  118. int res=0;
  119. if((res = decodeMP3(&p->mp,NULL,0,p->dbuf,MP3_DCACHE,&p->dbuflen)) == MP3_OK) {
  120. p->sbuflen -= p->dbuflen;
  121. p->dbufoffset = 0;
  122. }
  123. return res;
  124. }
  125. static int mp3_queue(struct ast_filestream *s)
  126. {
  127. struct mp3_private *p = s->_private;
  128. int res = 0, bytes = 0;
  129. if(p->seek) {
  130. ExitMP3(&p->mp);
  131. InitMP3(&p->mp, OUTSCALE);
  132. fseek(s->f, 0, SEEK_SET);
  133. p->sbuflen = p->dbuflen = p->offset = 0;
  134. while(p->offset < p->seek) {
  135. if(mp3_squeue(s))
  136. return -1;
  137. while(p->offset < p->seek && ((res = mp3_dqueue(s))) == MP3_OK) {
  138. for(bytes = 0 ; bytes < p->dbuflen ; bytes++) {
  139. p->dbufoffset++;
  140. p->offset++;
  141. if(p->offset >= p->seek)
  142. break;
  143. }
  144. }
  145. if(res == MP3_ERR)
  146. return -1;
  147. }
  148. p->seek = 0;
  149. return 0;
  150. }
  151. if(p->dbuflen == 0) {
  152. if(p->sbuflen) {
  153. res = mp3_dqueue(s);
  154. if(res == MP3_ERR)
  155. return -1;
  156. }
  157. if(! p->sbuflen || res != MP3_OK) {
  158. if(mp3_squeue(s))
  159. return -1;
  160. }
  161. }
  162. return 0;
  163. }
  164. static struct ast_frame *mp3_read(struct ast_filestream *s, int *whennext)
  165. {
  166. struct mp3_private *p = s->_private;
  167. int delay =0;
  168. int save=0;
  169. /* Pre-populate the buffer that holds audio to be returned (dbuf) */
  170. if (mp3_queue(s)) {
  171. return NULL;
  172. }
  173. if (p->dbuflen) {
  174. /* Read out what's waiting in dbuf */
  175. for (p->buflen = 0; p->buflen < MP3_BUFLEN && p->buflen < p->dbuflen; p->buflen++) {
  176. s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[p->buflen + p->dbufoffset];
  177. }
  178. p->dbufoffset += p->buflen;
  179. p->dbuflen -= p->buflen;
  180. }
  181. if (p->buflen < MP3_BUFLEN) {
  182. /* dbuf didn't have enough, so reset dbuf, fill it back up and continue */
  183. p->dbuflen = p->dbufoffset = 0;
  184. if (mp3_queue(s)) {
  185. return NULL;
  186. }
  187. /* Make sure dbuf has enough to complete this read attempt */
  188. if (p->dbuflen >= (MP3_BUFLEN - p->buflen)) {
  189. for (save = p->buflen; p->buflen < MP3_BUFLEN; p->buflen++) {
  190. s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[(p->buflen - save) + p->dbufoffset];
  191. }
  192. p->dbufoffset += (MP3_BUFLEN - save);
  193. p->dbuflen -= (MP3_BUFLEN - save);
  194. }
  195. }
  196. p->offset += p->buflen;
  197. delay = p->buflen / 2;
  198. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, p->buflen);
  199. s->fr.samples = delay;
  200. *whennext = delay;
  201. return &s->fr;
  202. }
  203. static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
  204. {
  205. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  206. return -1;
  207. }
  208. static int mp3_seek(struct ast_filestream *s, off_t sample_offset, int whence)
  209. {
  210. struct mp3_private *p = s->_private;
  211. off_t min,max,cur;
  212. long offset=0,samples;
  213. samples = sample_offset * 2;
  214. min = 0;
  215. fseek(s->f, 0, SEEK_END);
  216. max = ftell(s->f) * 100;
  217. cur = p->offset;
  218. if (whence == SEEK_SET)
  219. offset = samples + min;
  220. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  221. offset = samples + cur;
  222. else if (whence == SEEK_END)
  223. offset = max - samples;
  224. if (whence != SEEK_FORCECUR) {
  225. offset = (offset > max)?max:offset;
  226. }
  227. p->seek = offset;
  228. return fseek(s->f, offset, SEEK_SET);
  229. }
  230. static int mp3_rewrite(struct ast_filestream *s, const char *comment)
  231. {
  232. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  233. return -1;
  234. }
  235. static int mp3_trunc(struct ast_filestream *s)
  236. {
  237. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  238. return -1;
  239. }
  240. static off_t mp3_tell(struct ast_filestream *s)
  241. {
  242. struct mp3_private *p = s->_private;
  243. return p->offset/2;
  244. }
  245. static char *mp3_getcomment(struct ast_filestream *s)
  246. {
  247. return NULL;
  248. }
  249. static struct ast_format_def mp3_f = {
  250. .name = "mp3",
  251. .exts = "mp3",
  252. .open = mp3_open,
  253. .write = mp3_write,
  254. .rewrite = mp3_rewrite,
  255. .seek = mp3_seek,
  256. .trunc = mp3_trunc,
  257. .tell = mp3_tell,
  258. .read = mp3_read,
  259. .close = mp3_close,
  260. .getcomment = mp3_getcomment,
  261. .buf_size = MP3_BUFLEN + AST_FRIENDLY_OFFSET,
  262. .desc_size = sizeof(struct mp3_private),
  263. };
  264. static int load_module(void)
  265. {
  266. mp3_f.format = ast_format_slin;
  267. InitMP3Constants();
  268. return ast_format_def_register(&mp3_f);
  269. }
  270. static int unload_module(void)
  271. {
  272. return ast_format_def_unregister(name);
  273. }
  274. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "MP3 format [Any rate but 8000hz mono is optimal]");