sound.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_SOUND_H__
  20. #define __PJMEDIA_SOUND_H__
  21. /**
  22. * @file sound.h
  23. * @brief Legacy sound device API
  24. */
  25. #include <pjmedia-audiodev/audiodev.h>
  26. #include <pjmedia/types.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup PJMED_SND Portable Sound Hardware Abstraction
  30. * @ingroup PJMED_SND_PORT
  31. * @brief PJMEDIA abstraction for sound device hardware
  32. * @{
  33. *
  34. * <strong>Warning: this sound device API has been deprecated
  35. * and replaced by PJMEDIA Audio Device API. Please see
  36. * http://trac.pjsip.org/repos/wiki/Audio_Dev_API for more
  37. * information.</strong>
  38. *
  39. * This section describes lower level abstraction for sound device
  40. * hardware. Application normally uses the higher layer @ref
  41. * PJMED_SND_PORT abstraction since it works seamlessly with
  42. * @ref PJMEDIA_PORT.
  43. *
  44. * The sound hardware abstraction basically runs <b>asychronously</b>,
  45. * and application must register callbacks to be called to receive/
  46. * supply audio frames from/to the sound hardware.
  47. *
  48. * A full duplex sound stream (created with #pjmedia_snd_open())
  49. * requires application to supply two callbacks:
  50. * - <b><tt>rec_cb</tt></b> callback to be called when it has finished
  51. * capturing one media frame, and
  52. * - <b><tt>play_cb</tt></b> callback to be called when it needs media
  53. * frame to be played to the sound playback hardware.
  54. *
  55. * Half duplex sound stream (created with #pjmedia_snd_open_rec() or
  56. * #pjmedia_snd_open_player()) will only need one of the callback to
  57. * be specified.
  58. *
  59. * After sound stream is created, application need to call
  60. * #pjmedia_snd_stream_start() to start capturing/playing back media
  61. * frames from/to the sound device.
  62. */
  63. /** Opaque declaration for pjmedia_snd_stream. */
  64. typedef struct pjmedia_snd_stream pjmedia_snd_stream;
  65. /**
  66. * Device information structure returned by #pjmedia_snd_get_dev_info.
  67. */
  68. typedef struct pjmedia_snd_dev_info
  69. {
  70. char name[PJMEDIA_AUD_DEV_INFO_NAME_LEN];
  71. /**< Device name. */
  72. unsigned input_count; /**< Max number of input channels. */
  73. unsigned output_count; /**< Max number of output channels. */
  74. unsigned default_samples_per_sec;/**< Default sampling rate. */
  75. } pjmedia_snd_dev_info;
  76. /**
  77. * Stream information, can be retrieved from a live stream by calling
  78. * #pjmedia_snd_stream_get_info().
  79. */
  80. typedef struct pjmedia_snd_stream_info
  81. {
  82. pjmedia_dir dir; /**< Stream direction. */
  83. int play_id; /**< Playback dev id, or -1 for rec only*/
  84. int rec_id; /**< Capture dev id, or -1 for play only*/
  85. unsigned clock_rate; /**< Actual clock rate. */
  86. unsigned channel_count; /**< Number of channels. */
  87. unsigned samples_per_frame; /**< Samples per frame. */
  88. unsigned bits_per_sample; /**< Bits per sample. */
  89. unsigned rec_latency; /**< Record latency, in samples. */
  90. unsigned play_latency; /**< Playback latency, in samples. */
  91. } pjmedia_snd_stream_info;
  92. /**
  93. * This callback is called by player stream when it needs additional data
  94. * to be played by the device. Application must fill in the whole of output
  95. * buffer with sound samples.
  96. *
  97. * @param user_data User data associated with the stream.
  98. * @param timestamp Timestamp, in samples.
  99. * @param output Buffer to be filled out by application.
  100. * @param size The size requested in bytes, which will be equal to
  101. * the size of one whole packet.
  102. *
  103. * @return Non-zero to stop the stream.
  104. */
  105. typedef pj_status_t (*pjmedia_snd_play_cb)(/* in */ void *user_data,
  106. /* in */ pj_uint32_t timestamp,
  107. /* out */ void *output,
  108. /* out */ unsigned size);
  109. /**
  110. * This callback is called by recorder stream when it has captured the whole
  111. * packet worth of audio samples.
  112. *
  113. * @param user_data User data associated with the stream.
  114. * @param timestamp Timestamp, in samples.
  115. * @param input Buffer containing the captured audio samples.
  116. * @param size The size of the data in the buffer, in bytes.
  117. *
  118. * @return Non-zero to stop the stream.
  119. */
  120. typedef pj_status_t (*pjmedia_snd_rec_cb)(/* in */ void *user_data,
  121. /* in */ pj_uint32_t timestamp,
  122. /* in */ void *input,
  123. /* in*/ unsigned size);
  124. /**
  125. * Init the sound library.
  126. *
  127. * @param factory The sound factory.
  128. *
  129. * @return Zero on success.
  130. */
  131. PJ_INLINE(pj_status_t) pjmedia_snd_init(pj_pool_factory *factory)
  132. {
  133. /* This function is inlined to avoid pjmedia's dependency on
  134. * pjmedia-audiodev.
  135. */
  136. return pjmedia_aud_subsys_init(factory);
  137. }
  138. /**
  139. * Get the number of devices detected by the library.
  140. *
  141. * @return Number of devices.
  142. */
  143. PJ_INLINE(int) pjmedia_snd_get_dev_count(void)
  144. {
  145. /* This function is inlined to avoid pjmedia's dependency on
  146. * pjmedia-audiodev.
  147. */
  148. return (int)pjmedia_aud_dev_count();
  149. }
  150. /**
  151. * Get device info.
  152. *
  153. * @param index The index of the device, which should be in the range
  154. * from zero to #pjmedia_snd_get_dev_count - 1.
  155. */
  156. PJ_DECL(const pjmedia_snd_dev_info*) pjmedia_snd_get_dev_info(unsigned index);
  157. /**
  158. * Set sound device latency, this function must be called before sound device
  159. * opened, or otherwise default latency setting will be used, @see
  160. * PJMEDIA_SND_DEFAULT_REC_LATENCY & PJMEDIA_SND_DEFAULT_PLAY_LATENCY.
  161. *
  162. * Choosing latency value is not straightforward, it should accomodate both
  163. * minimum latency and stability. Lower latency tends to cause sound device
  164. * less reliable (producing audio dropouts) on CPU load disturbance. Moreover,
  165. * the best latency setting may vary based on many aspects, e.g: sound card,
  166. * CPU, OS, kernel, etc.
  167. *
  168. * @param input_latency The latency of input device, in ms, set to 0
  169. * for default PJMEDIA_SND_DEFAULT_REC_LATENCY.
  170. * @param output_latency The latency of output device, in ms, set to 0
  171. * for default PJMEDIA_SND_DEFAULT_PLAY_LATENCY.
  172. *
  173. * @return PJ_SUCCESS on success.
  174. */
  175. PJ_DECL(pj_status_t) pjmedia_snd_set_latency(unsigned input_latency,
  176. unsigned output_latency);
  177. /**
  178. * Create sound stream for both capturing audio and audio playback, from the
  179. * same device. This is the recommended way to create simultaneous recorder
  180. * and player streams (instead of creating separate capture and playback
  181. * streams), because it works on backends that does not allow
  182. * a device to be opened more than once.
  183. *
  184. * @param rec_id Device index for recorder/capture stream, or
  185. * -1 to use the first capable device.
  186. * @param play_id Device index for playback stream, or -1 to use
  187. * the first capable device.
  188. * @param clock_rate Sound device's clock rate to set.
  189. * @param channel_count Set number of channels, 1 for mono, or 2 for
  190. * stereo. The channel count determines the format
  191. * of the frame.
  192. * @param samples_per_frame Number of samples per frame.
  193. * @param bits_per_sample Set the number of bits per sample. The normal
  194. * value for this parameter is 16 bits per sample.
  195. * @param rec_cb Callback to handle captured audio samples.
  196. * @param play_cb Callback to be called when the sound player needs
  197. * more audio samples to play.
  198. * @param user_data User data to be associated with the stream.
  199. * @param p_snd_strm Pointer to receive the stream instance.
  200. *
  201. * @return PJ_SUCCESS on success.
  202. */
  203. PJ_DECL(pj_status_t) pjmedia_snd_open(int rec_id,
  204. int play_id,
  205. unsigned clock_rate,
  206. unsigned channel_count,
  207. unsigned samples_per_frame,
  208. unsigned bits_per_sample,
  209. pjmedia_snd_rec_cb rec_cb,
  210. pjmedia_snd_play_cb play_cb,
  211. void *user_data,
  212. pjmedia_snd_stream **p_snd_strm);
  213. /**
  214. * Create a unidirectional audio stream for capturing audio samples from
  215. * the sound device.
  216. *
  217. * @param index Device index, or -1 to let the library choose the
  218. * first available device.
  219. * @param clock_rate Sound device's clock rate to set.
  220. * @param channel_count Set number of channels, 1 for mono, or 2 for
  221. * stereo. The channel count determines the format
  222. * of the frame.
  223. * @param samples_per_frame Number of samples per frame.
  224. * @param bits_per_sample Set the number of bits per sample. The normal
  225. * value for this parameter is 16 bits per sample.
  226. * @param rec_cb Callback to handle captured audio samples.
  227. * @param user_data User data to be associated with the stream.
  228. * @param p_snd_strm Pointer to receive the stream instance.
  229. *
  230. * @return PJ_SUCCESS on success.
  231. */
  232. PJ_DECL(pj_status_t) pjmedia_snd_open_rec( int index,
  233. unsigned clock_rate,
  234. unsigned channel_count,
  235. unsigned samples_per_frame,
  236. unsigned bits_per_sample,
  237. pjmedia_snd_rec_cb rec_cb,
  238. void *user_data,
  239. pjmedia_snd_stream **p_snd_strm);
  240. /**
  241. * Create a unidirectional audio stream for playing audio samples to the
  242. * sound device.
  243. *
  244. * @param index Device index, or -1 to let the library choose the
  245. * first available device.
  246. * @param clock_rate Sound device's clock rate to set.
  247. * @param channel_count Set number of channels, 1 for mono, or 2 for
  248. * stereo. The channel count determines the format
  249. * of the frame.
  250. * @param samples_per_frame Number of samples per frame.
  251. * @param bits_per_sample Set the number of bits per sample. The normal
  252. * value for this parameter is 16 bits per sample.
  253. * @param play_cb Callback to be called when the sound player needs
  254. * more audio samples to play.
  255. * @param user_data User data to be associated with the stream.
  256. * @param p_snd_strm Pointer to receive the stream instance.
  257. *
  258. * @return PJ_SUCCESS on success.
  259. */
  260. PJ_DECL(pj_status_t) pjmedia_snd_open_player( int index,
  261. unsigned clock_rate,
  262. unsigned channel_count,
  263. unsigned samples_per_frame,
  264. unsigned bits_per_sample,
  265. pjmedia_snd_play_cb play_cb,
  266. void *user_data,
  267. pjmedia_snd_stream **p_snd_strm );
  268. /**
  269. * Get information about live stream.
  270. *
  271. * @param strm The stream to be queried.
  272. * @param pi Pointer to stream information to be filled up with
  273. * information about the stream.
  274. *
  275. * @return PJ_SUCCESS on success or the appropriate error code.
  276. */
  277. PJ_DECL(pj_status_t) pjmedia_snd_stream_get_info(pjmedia_snd_stream *strm,
  278. pjmedia_snd_stream_info *pi);
  279. /**
  280. * Start the stream.
  281. *
  282. * @param stream The recorder or player stream.
  283. *
  284. * @return Zero on success.
  285. */
  286. PJ_DECL(pj_status_t) pjmedia_snd_stream_start(pjmedia_snd_stream *stream);
  287. /**
  288. * Stop the stream.
  289. *
  290. * @param stream The recorder or player stream.
  291. *
  292. * @return Zero on success.
  293. */
  294. PJ_DECL(pj_status_t) pjmedia_snd_stream_stop(pjmedia_snd_stream *stream);
  295. /**
  296. * Destroy the stream.
  297. *
  298. * @param stream The recorder of player stream.
  299. *
  300. * @return Zero on success.
  301. */
  302. PJ_DECL(pj_status_t) pjmedia_snd_stream_close(pjmedia_snd_stream *stream);
  303. /**
  304. * Deinitialize sound library.
  305. *
  306. * @return Zero on success.
  307. */
  308. PJ_INLINE(pj_status_t) pjmedia_snd_deinit(void)
  309. {
  310. /* This function is inlined to avoid pjmedia's dependency on
  311. * pjmedia-audiodev.
  312. */
  313. return pjmedia_aud_subsys_shutdown();
  314. }
  315. /**
  316. * @}
  317. */
  318. PJ_END_DECL
  319. #endif /* __PJMEDIA_SOUND_H__ */