sound_port.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_PORT_H__
  20. #define __PJMEDIA_SOUND_PORT_H__
  21. /**
  22. * @file sound_port.h
  23. * @brief Media port connection abstraction to sound device.
  24. */
  25. #include <pjmedia-audiodev/audiodev.h>
  26. #include <pjmedia/clock.h>
  27. #include <pjmedia/port.h>
  28. #include <pjmedia/echo.h>
  29. PJ_BEGIN_DECL
  30. /**
  31. * @defgroup PJMED_SND_PORT Sound Device Port
  32. * @ingroup PJMEDIA_PORT_CLOCK
  33. * @brief Media Port Connection Abstraction to the Sound Device
  34. @{
  35. As explained in @ref PJMED_SND, the sound hardware abstraction provides
  36. some callbacks for its user:
  37. - it calls <b><tt>rec_cb</tt></b> callback when it has finished capturing
  38. one media frame, and
  39. - it calls <b><tt>play_cb</tt></b> when it needs media frame to be
  40. played to the sound playback hardware.
  41. The @ref PJMED_SND_PORT (the object being explained here) add a
  42. thin wrapper to the hardware abstraction:
  43. - it will call downstream port's <tt>put_frame()</tt>
  44. when <b><tt>rec_cb()</tt></b> is called (i.e. when the sound hardware
  45. has finished capturing frame), and
  46. - it will call downstream port's <tt>get_frame()</tt> when
  47. <b><tt>play_cb()</tt></b> is called (i.e. every time the
  48. sound hardware needs more frames to be played to the playback hardware).
  49. This simple abstraction enables media to flow automatically (and
  50. in timely manner) from the downstream media port to the sound device.
  51. In other words, the sound device port supplies media clock to
  52. the ports. The media clock concept is explained in @ref PJMEDIA_PORT_CLOCK
  53. section.
  54. Application registers downstream port to the sound device port by
  55. calling #pjmedia_snd_port_connect();
  56. */
  57. /**
  58. * Sound port options.
  59. */
  60. enum pjmedia_snd_port_option
  61. {
  62. /**
  63. * Don't start the audio device when creating a sound port.
  64. */
  65. PJMEDIA_SND_PORT_NO_AUTO_START = 1
  66. };
  67. /**
  68. * This structure specifies the parameters to create the sound port.
  69. * Use pjmedia_snd_port_param_default() to initialize this structure with
  70. * default values (mostly zeroes)
  71. */
  72. typedef struct pjmedia_snd_port_param
  73. {
  74. /**
  75. * Base structure.
  76. */
  77. pjmedia_aud_param base;
  78. /**
  79. * Sound port creation options.
  80. */
  81. unsigned options;
  82. /**
  83. * Echo cancellation options/flags.
  84. */
  85. unsigned ec_options;
  86. /**
  87. * Arbitrary user data for playback and record preview callbacks below.
  88. */
  89. void *user_data;
  90. /**
  91. * Optional callback for audio frame preview right before queued to
  92. * the speaker.
  93. * Notes:
  94. * - application MUST NOT block or perform long operation in the callback
  95. * as the callback may be executed in sound device thread
  96. * - when using software echo cancellation, application MUST NOT modify
  97. * the audio data from within the callback, otherwise the echo canceller
  98. * will not work properly.
  99. * - the return value of the callback will be ignored
  100. */
  101. pjmedia_aud_play_cb on_play_frame;
  102. /**
  103. * Optional callback for audio frame preview recorded from the microphone
  104. * before being processed by any media component such as software echo
  105. * canceller.
  106. * Notes:
  107. * - application MUST NOT block or perform long operation in the callback
  108. * as the callback may be executed in sound device thread
  109. * - when using software echo cancellation, application MUST NOT modify
  110. * the audio data from within the callback, otherwise the echo canceller
  111. * will not work properly.
  112. * - the return value of the callback will be ignored
  113. */
  114. pjmedia_aud_rec_cb on_rec_frame;
  115. } pjmedia_snd_port_param;
  116. /**
  117. * Initialize pjmedia_snd_port_param with default values.
  118. *
  119. * @param prm The parameter.
  120. */
  121. PJ_DECL(void) pjmedia_snd_port_param_default(pjmedia_snd_port_param *prm);
  122. /**
  123. * This opaque type describes sound device port connection.
  124. * Sound device port is not a media port, but it is used to connect media
  125. * port to the sound device.
  126. */
  127. typedef struct pjmedia_snd_port pjmedia_snd_port;
  128. /**
  129. * Create bidirectional sound port for both capturing and playback of
  130. * audio samples.
  131. *
  132. * @param pool Pool to allocate sound port structure.
  133. * @param rec_id Device index for recorder/capture stream, or
  134. * -1 to use the first capable device.
  135. * @param play_id Device index for playback stream, or -1 to use
  136. * the first capable device.
  137. * @param clock_rate Sound device's clock rate to set.
  138. * @param channel_count Set number of channels, 1 for mono, or 2 for
  139. * stereo. The channel count determines the format
  140. * of the frame.
  141. * @param samples_per_frame Number of samples per frame.
  142. * @param bits_per_sample Set the number of bits per sample. The normal
  143. * value for this parameter is 16 bits per sample.
  144. * @param options Options flag.
  145. * @param p_port Pointer to receive the sound device port instance.
  146. *
  147. * @return PJ_SUCCESS on success, or the appropriate error
  148. * code.
  149. */
  150. PJ_DECL(pj_status_t) pjmedia_snd_port_create( pj_pool_t *pool,
  151. int rec_id,
  152. int play_id,
  153. unsigned clock_rate,
  154. unsigned channel_count,
  155. unsigned samples_per_frame,
  156. unsigned bits_per_sample,
  157. unsigned options,
  158. pjmedia_snd_port **p_port);
  159. /**
  160. * Create unidirectional sound device port for capturing audio streams from
  161. * the sound device with the specified parameters.
  162. *
  163. * @param pool Pool to allocate sound port structure.
  164. * @param index Device index, or -1 to let the library choose the
  165. * first available device.
  166. * @param clock_rate Sound device's clock rate to set.
  167. * @param channel_count Set number of channels, 1 for mono, or 2 for
  168. * stereo. The channel count determines the format
  169. * of the frame.
  170. * @param samples_per_frame Number of samples per frame.
  171. * @param bits_per_sample Set the number of bits per sample. The normal
  172. * value for this parameter is 16 bits per sample.
  173. * @param options Options flag.
  174. * @param p_port Pointer to receive the sound device port instance.
  175. *
  176. * @return PJ_SUCCESS on success, or the appropriate error
  177. * code.
  178. */
  179. PJ_DECL(pj_status_t) pjmedia_snd_port_create_rec(pj_pool_t *pool,
  180. int index,
  181. unsigned clock_rate,
  182. unsigned channel_count,
  183. unsigned samples_per_frame,
  184. unsigned bits_per_sample,
  185. unsigned options,
  186. pjmedia_snd_port **p_port);
  187. /**
  188. * Create unidirectional sound device port for playing audio streams with the
  189. * specified parameters.
  190. *
  191. * @param pool Pool to allocate sound port structure.
  192. * @param index Device index, or -1 to let the library choose the
  193. * first available device.
  194. * @param clock_rate Sound device's clock rate to set.
  195. * @param channel_count Set number of channels, 1 for mono, or 2 for
  196. * stereo. The channel count determines the format
  197. * of the frame.
  198. * @param samples_per_frame Number of samples per frame.
  199. * @param bits_per_sample Set the number of bits per sample. The normal
  200. * value for this parameter is 16 bits per sample.
  201. * @param options Options flag.
  202. * @param p_port Pointer to receive the sound device port instance.
  203. *
  204. * @return PJ_SUCCESS on success, or the appropriate error
  205. * code.
  206. */
  207. PJ_DECL(pj_status_t) pjmedia_snd_port_create_player(pj_pool_t *pool,
  208. int index,
  209. unsigned clock_rate,
  210. unsigned channel_count,
  211. unsigned samples_per_frame,
  212. unsigned bits_per_sample,
  213. unsigned options,
  214. pjmedia_snd_port **p_port);
  215. /**
  216. * Create sound device port according to the specified parameters.
  217. *
  218. * @param pool Pool to allocate sound port structure.
  219. * @param prm Sound port parameter.
  220. * @param p_port Pointer to receive the sound device port instance.
  221. *
  222. * @return PJ_SUCCESS on success, or the appropriate error
  223. * code.
  224. */
  225. PJ_DECL(pj_status_t) pjmedia_snd_port_create2(pj_pool_t *pool,
  226. const pjmedia_snd_port_param *prm,
  227. pjmedia_snd_port **p_port);
  228. /**
  229. * Destroy sound device port.
  230. *
  231. * @param snd_port The sound device port.
  232. *
  233. * @return PJ_SUCCESS on success, or the appropriate error
  234. * code.
  235. */
  236. PJ_DECL(pj_status_t) pjmedia_snd_port_destroy(pjmedia_snd_port *snd_port);
  237. /**
  238. * Retrieve the sound stream associated by this sound device port.
  239. *
  240. * @param snd_port The sound device port.
  241. *
  242. * @return The sound stream instance.
  243. */
  244. PJ_DECL(pjmedia_aud_stream*) pjmedia_snd_port_get_snd_stream(
  245. pjmedia_snd_port *snd_port);
  246. /**
  247. * Change the echo cancellation settings. The echo cancellation settings
  248. * should have been specified when this sound port was created, by setting
  249. * the appropriate fields in the pjmedia_aud_param, because not all sound
  250. * device implementation supports changing the EC setting once the device
  251. * has been opened.
  252. *
  253. * The behavior of this function depends on whether device or software AEC
  254. * is being used. If the device supports AEC, this function will forward
  255. * the change request to the device and it will be up to the device whether
  256. * to support the request. If software AEC is being used (the software EC
  257. * will be used if the device does not support AEC), this function will
  258. * change the software EC settings.
  259. *
  260. * @param snd_port The sound device port.
  261. * @param pool Pool to re-create the echo canceller if necessary.
  262. * @param tail_ms Maximum echo tail length to be supported, in
  263. * miliseconds. If zero is specified, the EC would
  264. * be disabled.
  265. * @param options The options to be passed to #pjmedia_echo_create().
  266. * This is only used if software EC is being used.
  267. *
  268. * @return PJ_SUCCESS on success.
  269. */
  270. PJ_DECL(pj_status_t) pjmedia_snd_port_set_ec( pjmedia_snd_port *snd_port,
  271. pj_pool_t *pool,
  272. unsigned tail_ms,
  273. unsigned options);
  274. /**
  275. * Get current echo canceller tail length, in miliseconds. The tail length
  276. * will be zero if EC is not enabled.
  277. *
  278. * @param snd_port The sound device port.
  279. * @param p_length Pointer to receive the tail length.
  280. *
  281. * @return PJ_SUCCESS on success.
  282. */
  283. PJ_DECL(pj_status_t) pjmedia_snd_port_get_ec_tail(pjmedia_snd_port *snd_port,
  284. unsigned *p_length);
  285. /**
  286. * Get echo canceller statistics.
  287. *
  288. * @param snd_port The sound device port.
  289. * @param p_stat Pointer to receive the stat.
  290. *
  291. * @return PJ_SUCCESS on success, or the appropriate error
  292. * code.
  293. */
  294. PJ_DECL(pj_status_t) pjmedia_snd_port_get_ec_stat(pjmedia_snd_port *snd_port,
  295. pjmedia_echo_stat *p_stat);
  296. /**
  297. * Get a clock source from the sound port.
  298. *
  299. * @param snd_port The sound port.
  300. * @param dir Sound port's direction.
  301. *
  302. * @return The clock source.
  303. */
  304. PJ_DECL(pjmedia_clock_src *)
  305. pjmedia_snd_port_get_clock_src( pjmedia_snd_port *snd_port,
  306. pjmedia_dir dir );
  307. /**
  308. * Connect a port to the sound device port. If the sound device port has a
  309. * sound recorder device, then this will start periodic function call to
  310. * the port's put_frame() function. If the sound device has a sound player
  311. * device, then this will start periodic function call to the port's
  312. * get_frame() function.
  313. *
  314. * For this version of PJMEDIA, the media port MUST have the same audio
  315. * settings as the sound device port, or otherwise the connection will
  316. * fail. This means the port MUST have the same clock_rate, channel count,
  317. * samples per frame, and bits per sample as the sound device port.
  318. *
  319. * @param snd_port The sound device port.
  320. * @param port The media port to be connected.
  321. *
  322. * @return PJ_SUCCESS on success, or the appropriate error
  323. * code.
  324. */
  325. PJ_DECL(pj_status_t) pjmedia_snd_port_connect(pjmedia_snd_port *snd_port,
  326. pjmedia_port *port);
  327. /**
  328. * Retrieve the port instance currently attached to the sound port, if any.
  329. *
  330. * @param snd_port The sound device port.
  331. *
  332. * @return The port instance currently attached to the
  333. * sound device port, or NULL if there is no port
  334. * currently attached to the sound device port.
  335. */
  336. PJ_DECL(pjmedia_port*) pjmedia_snd_port_get_port(pjmedia_snd_port *snd_port);
  337. /**
  338. * Disconnect currently attached port from the sound device port.
  339. *
  340. * @param snd_port The sound device port.
  341. *
  342. * @return PJ_SUCCESS on success.
  343. */
  344. PJ_DECL(pj_status_t) pjmedia_snd_port_disconnect(pjmedia_snd_port *snd_port);
  345. /**
  346. * @}
  347. */
  348. PJ_END_DECL
  349. #endif /* __PJMEDIA_SOUND_PORT_H__ */