format_mp3.c 7.3 KB

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