format_ogg_vorbis.c 12 KB

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