format_ogg_vorbis.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005, Jeff Ollie
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief OGG/Vorbis streams.
  19. * \arg File name extension: ogg
  20. * \ingroup formats
  21. */
  22. /* the order of these dependencies is important... it also specifies
  23. the link order of the libraries during linking
  24. */
  25. /*** MODULEINFO
  26. <depend>vorbis</depend>
  27. <depend>ogg</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <vorbis/codec.h>
  33. #include <vorbis/vorbisenc.h>
  34. #include <vorbis/vorbisfile.h>
  35. #ifdef _WIN32
  36. #include <io.h>
  37. #endif
  38. #include "asterisk/mod_format.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/format_cache.h"
  41. /*
  42. * this is the number of samples we deal with. Samples are converted
  43. * to SLINEAR so each one uses 2 bytes in the buffer.
  44. */
  45. #define SAMPLES_MAX 512
  46. #define BUF_SIZE (2*SAMPLES_MAX)
  47. #define BLOCK_SIZE 4096 /* used internally in the vorbis routines */
  48. struct ogg_vorbis_desc { /* format specific parameters */
  49. /* OggVorbis_File structure for libvorbisfile interface */
  50. OggVorbis_File ov_f;
  51. /* structures for handling the Ogg container */
  52. ogg_stream_state os;
  53. ogg_page og;
  54. ogg_packet op;
  55. /* structures for handling Vorbis audio data */
  56. vorbis_info vi;
  57. vorbis_comment vc;
  58. vorbis_dsp_state vd;
  59. vorbis_block vb;
  60. /*! \brief Indicates whether this filestream is set up for reading or writing. */
  61. int writing;
  62. /*! \brief Stores the current pcm position to support tell() on writing mode. */
  63. off_t writing_pcm_pos;
  64. /*! \brief Indicates whether an End of Stream condition has been detected. */
  65. int eos;
  66. };
  67. #if !defined(HAVE_VORBIS_OPEN_CALLBACKS)
  68. /*
  69. * Declared for backward compatibility with vorbisfile v1.1.2.
  70. * Code taken from vorbisfile.h v1.2.0.
  71. */
  72. static int _ov_header_fseek_wrap(FILE *f, ogg_int64_t off, int whence)
  73. {
  74. if (f == NULL) {
  75. return -1;
  76. }
  77. return fseek(f, off, whence);
  78. }
  79. static ov_callbacks OV_CALLBACKS_NOCLOSE = {
  80. (size_t (*)(void *, size_t, size_t, void *)) fread,
  81. (int (*)(void *, ogg_int64_t, int)) _ov_header_fseek_wrap,
  82. (int (*)(void *)) NULL,
  83. (long (*)(void *)) ftell
  84. };
  85. #endif /* !defined(HAVE_VORBIS_OPEN_CALLBACKS) */
  86. /*!
  87. * \brief Create a new OGG/Vorbis filestream and set it up for reading.
  88. * \param s File that points to on disk storage of the OGG/Vorbis data.
  89. * \return The new filestream.
  90. */
  91. static int ogg_vorbis_open(struct ast_filestream *s)
  92. {
  93. int result;
  94. struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) s->_private;
  95. /* initialize private description block */
  96. memset(desc, 0, sizeof(struct ogg_vorbis_desc));
  97. desc->writing = 0;
  98. /* actually open file */
  99. result = ov_open_callbacks(s->f, &desc->ov_f, NULL, 0, OV_CALLBACKS_NOCLOSE);
  100. if (result != 0) {
  101. ast_log(LOG_ERROR, "Error opening Ogg/Vorbis file stream.\n");
  102. return -1;
  103. }
  104. /* check stream(s) type */
  105. if (desc->ov_f.vi->channels != 1) {
  106. ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
  107. ov_clear(&desc->ov_f);
  108. return -1;
  109. }
  110. if (desc->ov_f.vi->rate != DEFAULT_SAMPLE_RATE) {
  111. ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
  112. ov_clear(&desc->ov_f);
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. /*!
  118. * \brief Create a new OGG/Vorbis filestream and set it up for writing.
  119. * \param s File pointer that points to on-disk storage.
  120. * \param comment Comment that should be embedded in the OGG/Vorbis file.
  121. * \return A new filestream.
  122. */
  123. static int ogg_vorbis_rewrite(struct ast_filestream *s,
  124. const char *comment)
  125. {
  126. ogg_packet header;
  127. ogg_packet header_comm;
  128. ogg_packet header_code;
  129. struct ogg_vorbis_desc *tmp = (struct ogg_vorbis_desc *) s->_private;
  130. tmp->writing = 1;
  131. tmp->writing_pcm_pos = 0;
  132. vorbis_info_init(&tmp->vi);
  133. if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
  134. ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
  135. return -1;
  136. }
  137. vorbis_comment_init(&tmp->vc);
  138. vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
  139. if (comment)
  140. vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
  141. vorbis_analysis_init(&tmp->vd, &tmp->vi);
  142. vorbis_block_init(&tmp->vd, &tmp->vb);
  143. ogg_stream_init(&tmp->os, ast_random());
  144. vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
  145. &header_code);
  146. ogg_stream_packetin(&tmp->os, &header);
  147. ogg_stream_packetin(&tmp->os, &header_comm);
  148. ogg_stream_packetin(&tmp->os, &header_code);
  149. while (!tmp->eos) {
  150. if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
  151. break;
  152. if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
  153. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  154. }
  155. if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
  156. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  157. }
  158. if (ogg_page_eos(&tmp->og))
  159. tmp->eos = 1;
  160. }
  161. return 0;
  162. }
  163. /*!
  164. * \brief Write out any pending encoded data.
  165. * \param s An OGG/Vorbis filestream.
  166. * \param f The file to write to.
  167. */
  168. static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
  169. {
  170. while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
  171. vorbis_analysis(&s->vb, NULL);
  172. vorbis_bitrate_addblock(&s->vb);
  173. while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
  174. ogg_stream_packetin(&s->os, &s->op);
  175. while (!s->eos) {
  176. if (ogg_stream_pageout(&s->os, &s->og) == 0) {
  177. break;
  178. }
  179. if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
  180. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  181. }
  182. if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
  183. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  184. }
  185. if (ogg_page_eos(&s->og)) {
  186. s->eos = 1;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. /*!
  193. * \brief Write audio data from a frame to an OGG/Vorbis filestream.
  194. * \param fs An OGG/Vorbis filestream.
  195. * \param f A frame containing audio to be written to the filestream.
  196. * \return -1 if there was an error, 0 on success.
  197. */
  198. static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
  199. {
  200. int i;
  201. float **buffer;
  202. short *data;
  203. struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
  204. if (!s->writing) {
  205. ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
  206. return -1;
  207. }
  208. if (!f->datalen)
  209. return -1;
  210. data = (short *) f->data.ptr;
  211. buffer = vorbis_analysis_buffer(&s->vd, f->samples);
  212. for (i = 0; i < f->samples; i++)
  213. buffer[0][i] = (double)data[i] / 32768.0;
  214. vorbis_analysis_wrote(&s->vd, f->samples);
  215. write_stream(s, fs->f);
  216. s->writing_pcm_pos += f->samples;
  217. return 0;
  218. }
  219. /*!
  220. * \brief Close a OGG/Vorbis filestream.
  221. * \param fs A OGG/Vorbis filestream.
  222. */
  223. static void ogg_vorbis_close(struct ast_filestream *fs)
  224. {
  225. struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
  226. if (s->writing) {
  227. /* Tell the Vorbis encoder that the stream is finished
  228. * and write out the rest of the data */
  229. vorbis_analysis_wrote(&s->vd, 0);
  230. write_stream(s, fs->f);
  231. } else {
  232. /* clear OggVorbis_File handle */
  233. ov_clear(&s->ov_f);
  234. }
  235. }
  236. /*!
  237. * \brief Read a frame full of audio data from the filestream.
  238. * \param fs The filestream.
  239. * \param whennext Number of sample times to schedule the next call.
  240. * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
  241. */
  242. static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
  243. int *whennext)
  244. {
  245. struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
  246. int current_bitstream = -10;
  247. char *out_buf;
  248. long bytes_read;
  249. if (desc->writing) {
  250. ast_log(LOG_WARNING, "Reading is not supported on OGG/Vorbis on write files.\n");
  251. return NULL;
  252. }
  253. /* initialize frame */
  254. AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  255. out_buf = (char *) (fs->fr.data.ptr); /* SLIN data buffer */
  256. /* read samples from OV interface */
  257. bytes_read = ov_read(
  258. &desc->ov_f,
  259. out_buf, /* Buffer to write data */
  260. BUF_SIZE, /* Size of buffer */
  261. (__BYTE_ORDER == __BIG_ENDIAN), /* Endianes (0 for little) */
  262. 2, /* 1 = 8bit, 2 = 16bit */
  263. 1, /* 0 = unsigned, 1 = signed */
  264. &current_bitstream /* Returns the current bitstream section */
  265. );
  266. /* check returned data */
  267. if (bytes_read <= 0) {
  268. /* End of stream */
  269. return NULL;
  270. }
  271. /* Return decoded bytes */
  272. fs->fr.datalen = bytes_read;
  273. fs->fr.samples = bytes_read / 2;
  274. *whennext = fs->fr.samples;
  275. return &fs->fr;
  276. }
  277. /*!
  278. * \brief Trucate an OGG/Vorbis filestream.
  279. * \param fs The filestream to truncate.
  280. * \return 0 on success, -1 on failure.
  281. */
  282. static int ogg_vorbis_trunc(struct ast_filestream *fs)
  283. {
  284. ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
  285. return -1;
  286. }
  287. /*!
  288. * \brief Tell the current position in OGG/Vorbis filestream measured in pcms.
  289. * \param fs The filestream to take action on.
  290. * \return 0 or greater with the position measured in samples, or -1 for false.
  291. */
  292. static off_t ogg_vorbis_tell(struct ast_filestream *fs)
  293. {
  294. off_t pos;
  295. struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
  296. if (desc->writing) {
  297. return desc->writing_pcm_pos;
  298. }
  299. if ((pos = ov_pcm_tell(&desc->ov_f)) < 0) {
  300. return -1;
  301. }
  302. return pos;
  303. }
  304. /*!
  305. * \brief Seek to a specific position in an OGG/Vorbis filestream.
  306. * \param fs The filestream to take action on.
  307. * \param sample_offset New position for the filestream, measured in 8KHz samples.
  308. * \param whence Location to measure
  309. * \return 0 on success, -1 on failure.
  310. */
  311. static int ogg_vorbis_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  312. {
  313. int seek_result = -1;
  314. off_t relative_pcm_pos;
  315. struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
  316. if (desc->writing) {
  317. ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams in writing mode!\n");
  318. return -1;
  319. }
  320. /* ov_pcm_seek support seeking only from begining (SEEK_SET), the rest must be emulated */
  321. switch (whence) {
  322. case SEEK_SET:
  323. seek_result = ov_pcm_seek(&desc->ov_f, sample_offset);
  324. break;
  325. case SEEK_CUR:
  326. if ((relative_pcm_pos = ogg_vorbis_tell(fs)) < 0) {
  327. seek_result = -1;
  328. break;
  329. }
  330. seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos + sample_offset);
  331. break;
  332. case SEEK_END:
  333. if ((relative_pcm_pos = ov_pcm_total(&desc->ov_f, -1)) < 0) {
  334. seek_result = -1;
  335. break;
  336. }
  337. seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos - sample_offset);
  338. break;
  339. default:
  340. ast_log(LOG_WARNING, "Unknown *whence* to seek on OGG/Vorbis streams!\n");
  341. break;
  342. }
  343. /* normalize error value to -1,0 */
  344. return (seek_result == 0) ? 0 : -1;
  345. }
  346. static struct ast_format_def vorbis_f = {
  347. .name = "ogg_vorbis",
  348. .exts = "ogg",
  349. .open = ogg_vorbis_open,
  350. .rewrite = ogg_vorbis_rewrite,
  351. .write = ogg_vorbis_write,
  352. .seek = ogg_vorbis_seek,
  353. .trunc = ogg_vorbis_trunc,
  354. .tell = ogg_vorbis_tell,
  355. .read = ogg_vorbis_read,
  356. .close = ogg_vorbis_close,
  357. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  358. .desc_size = sizeof(struct ogg_vorbis_desc),
  359. };
  360. static int load_module(void)
  361. {
  362. vorbis_f.format = ast_format_slin;
  363. if (ast_format_def_register(&vorbis_f))
  364. return AST_MODULE_LOAD_FAILURE;
  365. return AST_MODULE_LOAD_SUCCESS;
  366. }
  367. static int unload_module(void)
  368. {
  369. return ast_format_def_unregister(vorbis_f.name);
  370. }
  371. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "OGG/Vorbis audio",
  372. .support_level = AST_MODULE_SUPPORT_CORE,
  373. .load = load_module,
  374. .unload = unload_module,
  375. .load_pri = AST_MODPRI_APP_DEPEND
  376. );