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