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