wav_port.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_WAV_PORT_H__
  20. #define __PJMEDIA_WAV_PORT_H__
  21. /**
  22. * @file wav_port.h
  23. * @brief WAV file player and writer.
  24. */
  25. #include <pjmedia/port.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJMEDIA_FILE_PLAY WAV File Player
  29. * @ingroup PJMEDIA_PORT
  30. * @brief Audio playback from WAV file
  31. * @{
  32. */
  33. /**
  34. * WAV file player options.
  35. */
  36. enum pjmedia_file_player_option
  37. {
  38. /**
  39. * Tell the file player to return NULL frame when the whole
  40. * file has been played.
  41. */
  42. PJMEDIA_FILE_NO_LOOP = 1
  43. };
  44. /**
  45. * Additional information about the WAV player.
  46. */
  47. typedef struct pjmedia_wav_player_info
  48. {
  49. /**
  50. * Format ID of the payload.
  51. */
  52. pjmedia_format_id fmt_id;
  53. /**
  54. * The number of bits per sample of the file payload. For example,
  55. * the value is 16 for PCM WAV and 8 for Alaw/Ulas WAV files.
  56. */
  57. unsigned payload_bits_per_sample;
  58. /**
  59. * The WAV payload size in bytes.
  60. */
  61. pj_uint32_t size_bytes;
  62. /**
  63. * The WAV payload size in samples.
  64. */
  65. pj_uint32_t size_samples;
  66. } pjmedia_wav_player_info;
  67. /**
  68. * Create a media port to play streams from a WAV file. WAV player port
  69. * supports for reading WAV file with uncompressed 16 bit PCM format or
  70. * compressed G.711 A-law/U-law format.
  71. *
  72. * @param pool Pool to create memory buffers for this port.
  73. * @param filename File name to open.
  74. * @param ptime The duration (in miliseconds) of each frame read
  75. * from this port. If the value is zero, the default
  76. * duration (20ms) will be used.
  77. * @param flags Port creation flags.
  78. * @param buff_size Buffer size to be allocated. If the value is zero or
  79. * negative, the port will use default buffer size (which
  80. * is about 4KB).
  81. * @param p_port Pointer to receive the file port instance.
  82. *
  83. * @return PJ_SUCCESS on success.
  84. */
  85. PJ_DECL(pj_status_t) pjmedia_wav_player_port_create( pj_pool_t *pool,
  86. const char *filename,
  87. unsigned ptime,
  88. unsigned flags,
  89. pj_ssize_t buff_size,
  90. pjmedia_port **p_port );
  91. /**
  92. * Get additional info about the file player.
  93. *
  94. * @param port The file port.
  95. * @param i The info.
  96. *
  97. * @return PJ_SUCCESS on success or the appropriate error code.
  98. */
  99. PJ_DECL(pj_status_t) pjmedia_wav_player_get_info(pjmedia_port *port,
  100. pjmedia_wav_player_info *i);
  101. /**
  102. * Get the data length, in bytes.
  103. *
  104. * @param port The file player port.
  105. *
  106. * @return The length of the data, in bytes. On error, the
  107. * error code is given as negative value.
  108. */
  109. PJ_DECL(pj_ssize_t) pjmedia_wav_player_get_len(pjmedia_port *port);
  110. /**
  111. * Set the file play position of WAV player.
  112. *
  113. * @param port The file player port.
  114. * @param offset Playback position in bytes, relative to the start of
  115. * the payload.
  116. *
  117. * @return PJ_SUCCESS on success.
  118. */
  119. PJ_DECL(pj_status_t) pjmedia_wav_player_port_set_pos( pjmedia_port *port,
  120. pj_uint32_t offset );
  121. /**
  122. * Get the file play position of WAV player, in bytes.
  123. *
  124. * @param port The file player port.
  125. *
  126. * @return The current play position, in bytes. On error, the
  127. * error code is given as negative value.
  128. */
  129. PJ_DECL(pj_ssize_t) pjmedia_wav_player_port_get_pos( pjmedia_port *port );
  130. #if !DEPRECATED_FOR_TICKET_2251
  131. /**
  132. * Register a callback to be called when the file reading has reached the
  133. * end of file. If the file is set to play repeatedly, then the callback
  134. * will be called multiple times. Note that only one callback can be
  135. * registered for each file port.
  136. *
  137. * @param port The file player port.
  138. * @param user_data User data to be specified in the callback
  139. * @param cb Callback to be called. If the callback returns non-
  140. * PJ_SUCCESS, the playback will stop. Note that if
  141. * application destroys the file port in the callback,
  142. * it must return non-PJ_SUCCESS here.
  143. *
  144. * @return PJ_SUCCESS on success.
  145. */
  146. PJ_DECL(pj_status_t)
  147. pjmedia_wav_player_set_eof_cb( pjmedia_port *port,
  148. void *user_data,
  149. pj_status_t (*cb)(pjmedia_port *port,
  150. void *usr_data));
  151. #endif
  152. /**
  153. * Register a callback to be called when the file reading has reached the
  154. * end of file. If the file is set to play repeatedly, then the callback
  155. * will be called multiple times. Note that only one callback can be
  156. * registered for each file port.
  157. *
  158. * @param port The file player port.
  159. * @param user_data User data to be specified in the callback
  160. * @param cb Callback to be called. Note that if
  161. * application wishes to stop the playback, it
  162. * can disconnect the port in the callback, and
  163. * only after all connections have been removed
  164. * could the application safely destroy the port.
  165. *
  166. * @return PJ_SUCCESS on success.
  167. */
  168. PJ_DECL(pj_status_t)
  169. pjmedia_wav_player_set_eof_cb2(pjmedia_port *port,
  170. void *user_data,
  171. void (*cb)(pjmedia_port *port,
  172. void *usr_data));
  173. /**
  174. * @}
  175. */
  176. /**
  177. * @defgroup PJMEDIA_FILE_REC File Writer (Recorder)
  178. * @ingroup PJMEDIA_PORT
  179. * @brief Audio capture/recording to WAV file
  180. * @{
  181. */
  182. /**
  183. * WAV file writer options.
  184. */
  185. enum pjmedia_file_writer_option
  186. {
  187. /**
  188. * Tell the file writer to save the audio in PCM format.
  189. */
  190. PJMEDIA_FILE_WRITE_PCM = 0,
  191. /**
  192. * Tell the file writer to save the audio in G711 Alaw format.
  193. */
  194. PJMEDIA_FILE_WRITE_ALAW = 1,
  195. /**
  196. * Tell the file writer to save the audio in G711 Alaw format.
  197. */
  198. PJMEDIA_FILE_WRITE_ULAW = 2,
  199. };
  200. /**
  201. * Create a media port to record streams to a WAV file. Note that the port
  202. * must be closed properly (with #pjmedia_port_destroy()) so that the WAV
  203. * header can be filled with correct values (such as the file length).
  204. * WAV writer port supports for writing audio in uncompressed 16 bit PCM format
  205. * or compressed G.711 U-law/A-law format, this needs to be specified in
  206. * \a flags param.
  207. *
  208. * @param pool Pool to create memory buffers for this port.
  209. * @param filename File name.
  210. * @param clock_rate The sampling rate.
  211. * @param channel_count Number of channels.
  212. * @param samples_per_frame Number of samples per frame.
  213. * @param bits_per_sample Number of bits per sample (eg 16).
  214. * @param flags Port creation flags, see
  215. * #pjmedia_file_writer_option.
  216. * @param buff_size Buffer size to be allocated. If the value is
  217. * zero or negative, the port will use default buffer
  218. * size (which is about 4KB).
  219. * @param p_port Pointer to receive the file port instance.
  220. *
  221. * @return PJ_SUCCESS on success.
  222. */
  223. PJ_DECL(pj_status_t) pjmedia_wav_writer_port_create(pj_pool_t *pool,
  224. const char *filename,
  225. unsigned clock_rate,
  226. unsigned channel_count,
  227. unsigned samples_per_frame,
  228. unsigned bits_per_sample,
  229. unsigned flags,
  230. pj_ssize_t buff_size,
  231. pjmedia_port **p_port );
  232. /**
  233. * Get current writing position. Note that this does not necessarily match
  234. * the size written to the file, since the WAV writer employs some internal
  235. * buffering. Also the value reported here only indicates the payload size
  236. * (it does not include the size of the WAV header),
  237. *
  238. * @param port The file writer port.
  239. *
  240. * @return Positive value to indicate the position (in bytes),
  241. * or negative value containing the error code.
  242. */
  243. PJ_DECL(pj_ssize_t) pjmedia_wav_writer_port_get_pos( pjmedia_port *port );
  244. #if !DEPRECATED_FOR_TICKET_2251
  245. /**
  246. * Register the callback to be called when the file writing has reached
  247. * certain size. Application can use this callback, for example, to limit
  248. * the size of the output file.
  249. *
  250. * @param port The file writer port.
  251. * @param pos The file position on which the callback will be called.
  252. * @param user_data User data to be specified in the callback, and will be
  253. * given on the callback.
  254. * @param cb Callback to be called. If the callback returns non-
  255. * PJ_SUCCESS, the writing will stop. Note that if
  256. * application destroys the port in the callback, it must
  257. * return non-PJ_SUCCESS here.
  258. *
  259. * @return PJ_SUCCESS on success.
  260. */
  261. PJ_DECL(pj_status_t)
  262. pjmedia_wav_writer_port_set_cb( pjmedia_port *port,
  263. pj_size_t pos,
  264. void *user_data,
  265. pj_status_t (*cb)(pjmedia_port *port,
  266. void *usr_data));
  267. #endif
  268. /**
  269. * Register the callback to be called when the file writing has reached
  270. * certain size. Application can use this callback, for example, to limit
  271. * the size of the output file.
  272. *
  273. * @param port The file writer port.
  274. * @param pos The file position on which the callback will be called.
  275. * @param user_data User data to be specified in the callback, and will be
  276. * given on the callback.
  277. * @param cb Callback to be called. Note that if
  278. * application wishes to stop the writing, it
  279. * can disconnect the port in the callback, and
  280. * only after all connections have been removed
  281. * could the application safely destroy the port.
  282. *
  283. * @return PJ_SUCCESS on success.
  284. */
  285. PJ_DECL(pj_status_t)
  286. pjmedia_wav_writer_port_set_cb2(pjmedia_port *port,
  287. pj_size_t pos,
  288. void *user_data,
  289. void (*cb)(pjmedia_port *port,
  290. void *usr_data));
  291. /**
  292. * @}
  293. */
  294. PJ_END_DECL
  295. #endif /* __PJMEDIA_WAV_PORT_H__ */