diff --git a/deps/pjsip/pjmedia/include/pjmedia/sound_port.h b/deps/pjsip/pjmedia/include/pjmedia/sound_port.h index a58c9334..a1d243b2 100644 --- a/deps/pjsip/pjmedia/include/pjmedia/sound_port.h +++ b/deps/pjsip/pjmedia/include/pjmedia/sound_port.h @@ -1,352 +1,362 @@ /* $Id: sound_port.h 4082 2012-04-24 13:09:14Z bennylp $ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef __PJMEDIA_SOUND_PORT_H__ #define __PJMEDIA_SOUND_PORT_H__ /** * @file sound_port.h * @brief Media port connection abstraction to sound device. */ #include #include #include PJ_BEGIN_DECL /** * @defgroup PJMED_SND_PORT Sound Device Port * @ingroup PJMEDIA_PORT_CLOCK * @brief Media Port Connection Abstraction to the Sound Device @{ As explained in @ref PJMED_SND, the sound hardware abstraction provides some callbacks for its user: - it calls rec_cb callback when it has finished capturing one media frame, and - it calls play_cb when it needs media frame to be played to the sound playback hardware. The @ref PJMED_SND_PORT (the object being explained here) add a thin wrapper to the hardware abstraction: - it will call downstream port's put_frame() when rec_cb() is called (i.e. when the sound hardware has finished capturing frame), and - it will call downstream port's get_frame() when play_cb() is called (i.e. every time the sound hardware needs more frames to be played to the playback hardware). This simple abstraction enables media to flow automatically (and in timely manner) from the downstream media port to the sound device. In other words, the sound device port supplies media clock to the ports. The media clock concept is explained in @ref PJMEDIA_PORT_CLOCK section. Application registers downstream port to the sound device port by calling #pjmedia_snd_port_connect(); */ /** * Sound port options. */ enum pjmedia_snd_port_option { /** * Don't start the audio device when creating a sound port. */ PJMEDIA_SND_PORT_NO_AUTO_START = 1 }; /** * This structure specifies the parameters to create the sound port. * Use pjmedia_snd_port_param_default() to initialize this structure with * default values (mostly zeroes) */ typedef struct pjmedia_snd_port_param { /** * Base structure. */ pjmedia_aud_param base; /** * Sound port creation options. */ unsigned options; /** * Echo cancellation options/flags. */ unsigned ec_options; } pjmedia_snd_port_param; /** * Initialize pjmedia_snd_port_param with default values. * * @param prm The parameter. */ PJ_DECL(void) pjmedia_snd_port_param_default(pjmedia_snd_port_param *prm); /** * This opaque type describes sound device port connection. * Sound device port is not a media port, but it is used to connect media * port to the sound device. */ typedef struct pjmedia_snd_port pjmedia_snd_port; /** * Create bidirectional sound port for both capturing and playback of * audio samples. * * @param pool Pool to allocate sound port structure. * @param rec_id Device index for recorder/capture stream, or * -1 to use the first capable device. * @param play_id Device index for playback stream, or -1 to use * the first capable device. * @param clock_rate Sound device's clock rate to set. * @param channel_count Set number of channels, 1 for mono, or 2 for * stereo. The channel count determines the format * of the frame. * @param samples_per_frame Number of samples per frame. * @param bits_per_sample Set the number of bits per sample. The normal * value for this parameter is 16 bits per sample. * @param options Options flag. * @param p_port Pointer to receive the sound device port instance. * * @return PJ_SUCCESS on success, or the appropriate error * code. */ PJ_DECL(pj_status_t) pjmedia_snd_port_create( pj_pool_t *pool, int rec_id, int play_id, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_snd_port **p_port); /** * Create unidirectional sound device port for capturing audio streams from * the sound device with the specified parameters. * * @param pool Pool to allocate sound port structure. * @param index Device index, or -1 to let the library choose the * first available device. * @param clock_rate Sound device's clock rate to set. * @param channel_count Set number of channels, 1 for mono, or 2 for * stereo. The channel count determines the format * of the frame. * @param samples_per_frame Number of samples per frame. * @param bits_per_sample Set the number of bits per sample. The normal * value for this parameter is 16 bits per sample. * @param options Options flag. * @param p_port Pointer to receive the sound device port instance. * * @return PJ_SUCCESS on success, or the appropriate error * code. */ PJ_DECL(pj_status_t) pjmedia_snd_port_create_rec(pj_pool_t *pool, int index, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_snd_port **p_port); /** * Create unidirectional sound device port for playing audio streams with the * specified parameters. * * @param pool Pool to allocate sound port structure. * @param index Device index, or -1 to let the library choose the * first available device. * @param clock_rate Sound device's clock rate to set. * @param channel_count Set number of channels, 1 for mono, or 2 for * stereo. The channel count determines the format * of the frame. * @param samples_per_frame Number of samples per frame. * @param bits_per_sample Set the number of bits per sample. The normal * value for this parameter is 16 bits per sample. * @param options Options flag. * @param p_port Pointer to receive the sound device port instance. * * @return PJ_SUCCESS on success, or the appropriate error * code. */ PJ_DECL(pj_status_t) pjmedia_snd_port_create_player(pj_pool_t *pool, int index, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_snd_port **p_port); /** * Create sound device port according to the specified parameters. * * @param pool Pool to allocate sound port structure. * @param prm Sound port parameter. * @param p_port Pointer to receive the sound device port instance. * * @return PJ_SUCCESS on success, or the appropriate error * code. */ PJ_DECL(pj_status_t) pjmedia_snd_port_create2(pj_pool_t *pool, const pjmedia_snd_port_param *prm, pjmedia_snd_port **p_port); /** * Destroy sound device port. * * @param snd_port The sound device port. * * @return PJ_SUCCESS on success, or the appropriate error * code. */ PJ_DECL(pj_status_t) pjmedia_snd_port_destroy(pjmedia_snd_port *snd_port); /** * Retrieve the sound stream associated by this sound device port. * * @param snd_port The sound device port. * * @return The sound stream instance. */ PJ_DECL(pjmedia_aud_stream*) pjmedia_snd_port_get_snd_stream( pjmedia_snd_port *snd_port); /** * Change the echo cancellation settings. The echo cancellation settings * should have been specified when this sound port was created, by setting * the appropriate fields in the pjmedia_aud_param, because not all sound * device implementation supports changing the EC setting once the device * has been opened. * * The behavior of this function depends on whether device or software AEC * is being used. If the device supports AEC, this function will forward * the change request to the device and it will be up to the device whether * to support the request. If software AEC is being used (the software EC * will be used if the device does not support AEC), this function will * change the software EC settings. * * @param snd_port The sound device port. * @param pool Pool to re-create the echo canceller if necessary. * @param tail_ms Maximum echo tail length to be supported, in * miliseconds. If zero is specified, the EC would * be disabled. * @param options The options to be passed to #pjmedia_echo_create(). * This is only used if software EC is being used. * * @return PJ_SUCCESS on success. */ PJ_DECL(pj_status_t) pjmedia_snd_port_set_ec( pjmedia_snd_port *snd_port, pj_pool_t *pool, unsigned tail_ms, unsigned options); /** * Get current echo canceller tail length, in miliseconds. The tail length * will be zero if EC is not enabled. * * @param snd_port The sound device port. * @param p_length Pointer to receive the tail length. * * @return PJ_SUCCESS on success. */ PJ_DECL(pj_status_t) pjmedia_snd_port_get_ec_tail(pjmedia_snd_port *snd_port, unsigned *p_length); /** * Get a clock source from the sound port. * * @param snd_port The sound port. * @param dir Sound port's direction. * * @return The clock source. */ PJ_DECL(pjmedia_clock_src *) pjmedia_snd_port_get_clock_src( pjmedia_snd_port *snd_port, pjmedia_dir dir ); +/** + * Reset the EC state in the sound port. + * + * @param snd_port The sound device port. + * + * @return PJ_SUCCESS on success. + */ +PJ_DECL(pj_status_t) pjmedia_snd_port_reset_ec_state(pjmedia_snd_port *snd_port); + + /** * Connect a port to the sound device port. If the sound device port has a * sound recorder device, then this will start periodic function call to * the port's put_frame() function. If the sound device has a sound player * device, then this will start periodic function call to the port's * get_frame() function. * * For this version of PJMEDIA, the media port MUST have the same audio * settings as the sound device port, or otherwise the connection will * fail. This means the port MUST have the same clock_rate, channel count, * samples per frame, and bits per sample as the sound device port. * * @param snd_port The sound device port. * @param port The media port to be connected. * * @return PJ_SUCCESS on success, or the appropriate error * code. */ PJ_DECL(pj_status_t) pjmedia_snd_port_connect(pjmedia_snd_port *snd_port, pjmedia_port *port); /** * Retrieve the port instance currently attached to the sound port, if any. * * @param snd_port The sound device port. * * @return The port instance currently attached to the * sound device port, or NULL if there is no port * currently attached to the sound device port. */ PJ_DECL(pjmedia_port*) pjmedia_snd_port_get_port(pjmedia_snd_port *snd_port); /** * Disconnect currently attached port from the sound device port. * * @param snd_port The sound device port. * * @return PJ_SUCCESS on success. */ PJ_DECL(pj_status_t) pjmedia_snd_port_disconnect(pjmedia_snd_port *snd_port); /** * @} */ PJ_END_DECL #endif /* __PJMEDIA_SOUND_PORT_H__ */ diff --git a/deps/pjsip/pjmedia/src/pjmedia/sound_port.c b/deps/pjsip/pjmedia/src/pjmedia/sound_port.c index 94e3c859..dada3155 100644 --- a/deps/pjsip/pjmedia/src/pjmedia/sound_port.c +++ b/deps/pjsip/pjmedia/src/pjmedia/sound_port.c @@ -1,742 +1,754 @@ /* $Id: sound_port.c 4487 2013-04-23 05:37:41Z bennylp $ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include /* pj_memset() */ #define AEC_TAIL 128 /* default AEC length in ms */ #define AEC_SUSPEND_LIMIT 5 /* seconds of no activity */ #define THIS_FILE "sound_port.c" //#define TEST_OVERFLOW_UNDERFLOW struct pjmedia_snd_port { int rec_id; int play_id; pj_uint32_t aud_caps; pjmedia_aud_param aud_param; pjmedia_aud_stream *aud_stream; pjmedia_dir dir; pjmedia_port *port; pjmedia_clock_src cap_clocksrc, play_clocksrc; unsigned clock_rate; unsigned channel_count; unsigned samples_per_frame; unsigned bits_per_sample; unsigned options; unsigned prm_ec_options; /* software ec */ pjmedia_echo_state *ec_state; unsigned ec_options; unsigned ec_tail_len; pj_bool_t ec_suspended; unsigned ec_suspend_count; unsigned ec_suspend_limit; }; /* * The callback called by sound player when it needs more samples to be * played. */ static pj_status_t play_cb(void *user_data, pjmedia_frame *frame) { pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data; pjmedia_port *port; const unsigned required_size = frame->size; pj_status_t status; pjmedia_clock_src_update(&snd_port->play_clocksrc, &frame->timestamp); port = snd_port->port; if (port == NULL) goto no_frame; status = pjmedia_port_get_frame(port, frame); if (status != PJ_SUCCESS) goto no_frame; if (frame->type != PJMEDIA_FRAME_TYPE_AUDIO) goto no_frame; /* Must supply the required samples */ pj_assert(frame->size == required_size); if (snd_port->ec_state) { if (snd_port->ec_suspended) { snd_port->ec_suspended = PJ_FALSE; - //pjmedia_echo_state_reset(snd_port->ec_state); + pjmedia_echo_reset(snd_port->ec_state); PJ_LOG(4,(THIS_FILE, "EC activated")); } snd_port->ec_suspend_count = 0; pjmedia_echo_playback(snd_port->ec_state, (pj_int16_t*)frame->buf); } return PJ_SUCCESS; no_frame: frame->type = PJMEDIA_FRAME_TYPE_AUDIO; frame->size = required_size; pj_bzero(frame->buf, frame->size); if (snd_port->ec_state && !snd_port->ec_suspended) { ++snd_port->ec_suspend_count; if (snd_port->ec_suspend_count > snd_port->ec_suspend_limit) { snd_port->ec_suspended = PJ_TRUE; PJ_LOG(4,(THIS_FILE, "EC suspended because of inactivity")); } if (snd_port->ec_state) { /* To maintain correct delay in EC */ pjmedia_echo_playback(snd_port->ec_state, (pj_int16_t*)frame->buf); } } return PJ_SUCCESS; } /* * The callback called by sound recorder when it has finished capturing a * frame. */ static pj_status_t rec_cb(void *user_data, pjmedia_frame *frame) { pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data; pjmedia_port *port; pjmedia_clock_src_update(&snd_port->cap_clocksrc, &frame->timestamp); port = snd_port->port; if (port == NULL) return PJ_SUCCESS; /* Cancel echo */ if (snd_port->ec_state && !snd_port->ec_suspended) { pjmedia_echo_capture(snd_port->ec_state, (pj_int16_t*) frame->buf, 0); } pjmedia_port_put_frame(port, frame); return PJ_SUCCESS; } /* * The callback called by sound player when it needs more samples to be * played. This version is for non-PCM data. */ static pj_status_t play_cb_ext(void *user_data, pjmedia_frame *frame) { pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data; pjmedia_port *port = snd_port->port; if (port == NULL) { frame->type = PJMEDIA_FRAME_TYPE_NONE; return PJ_SUCCESS; } pjmedia_port_get_frame(port, frame); return PJ_SUCCESS; } /* * The callback called by sound recorder when it has finished capturing a * frame. This version is for non-PCM data. */ static pj_status_t rec_cb_ext(void *user_data, pjmedia_frame *frame) { pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data; pjmedia_port *port; port = snd_port->port; if (port == NULL) return PJ_SUCCESS; pjmedia_port_put_frame(port, frame); return PJ_SUCCESS; } /* Initialize with default values (zero) */ PJ_DEF(void) pjmedia_snd_port_param_default(pjmedia_snd_port_param *prm) { pj_bzero(prm, sizeof(*prm)); } /* * Start the sound stream. * This may be called even when the sound stream has already been started. */ static pj_status_t start_sound_device( pj_pool_t *pool, pjmedia_snd_port *snd_port ) { pjmedia_aud_rec_cb snd_rec_cb; pjmedia_aud_play_cb snd_play_cb; pjmedia_aud_param param_copy; pj_status_t status; /* Check if sound has been started. */ if (snd_port->aud_stream != NULL) return PJ_SUCCESS; PJ_ASSERT_RETURN(snd_port->dir == PJMEDIA_DIR_CAPTURE || snd_port->dir == PJMEDIA_DIR_PLAYBACK || snd_port->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK, PJ_EBUG); /* Get device caps */ if (snd_port->aud_param.dir & PJMEDIA_DIR_CAPTURE) { pjmedia_aud_dev_info dev_info; status = pjmedia_aud_dev_get_info(snd_port->aud_param.rec_id, &dev_info); if (status != PJ_SUCCESS) return status; snd_port->aud_caps = dev_info.caps; } else { snd_port->aud_caps = 0; } /* Process EC settings */ pj_memcpy(¶m_copy, &snd_port->aud_param, sizeof(param_copy)); if (param_copy.flags & PJMEDIA_AUD_DEV_CAP_EC) { /* EC is wanted */ if ((snd_port->prm_ec_options & PJMEDIA_ECHO_USE_SW_ECHO) == 0 && (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC)) { /* Device supports EC */ /* Nothing to do */ } else { /* Application wants to use software EC or device * doesn't support EC, remove EC settings from * device parameters */ param_copy.flags &= ~(PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL); } } /* Use different callback if format is not PCM */ if (snd_port->aud_param.ext_fmt.id == PJMEDIA_FORMAT_L16) { snd_rec_cb = &rec_cb; snd_play_cb = &play_cb; } else { snd_rec_cb = &rec_cb_ext; snd_play_cb = &play_cb_ext; } /* Open the device */ status = pjmedia_aud_stream_create(¶m_copy, snd_rec_cb, snd_play_cb, snd_port, &snd_port->aud_stream); if (status != PJ_SUCCESS) return status; /* Inactivity limit before EC is suspended. */ snd_port->ec_suspend_limit = AEC_SUSPEND_LIMIT * (snd_port->clock_rate / snd_port->samples_per_frame); /* Create software EC if parameter specifies EC and * (app specifically requests software EC or device * doesn't support EC). Only do this if the format is PCM! */ if ((snd_port->aud_param.flags & PJMEDIA_AUD_DEV_CAP_EC) && ((snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC)==0 || (snd_port->prm_ec_options & PJMEDIA_ECHO_USE_SW_ECHO) != 0) && param_copy.ext_fmt.id == PJMEDIA_FORMAT_PCM) { if ((snd_port->aud_param.flags & PJMEDIA_AUD_DEV_CAP_EC_TAIL)==0) { snd_port->aud_param.flags |= PJMEDIA_AUD_DEV_CAP_EC_TAIL; snd_port->aud_param.ec_tail_ms = AEC_TAIL; PJ_LOG(4,(THIS_FILE, "AEC tail is set to default %u ms", snd_port->aud_param.ec_tail_ms)); } status = pjmedia_snd_port_set_ec(snd_port, pool, snd_port->aud_param.ec_tail_ms, snd_port->prm_ec_options); if (status != PJ_SUCCESS) { pjmedia_aud_stream_destroy(snd_port->aud_stream); snd_port->aud_stream = NULL; return status; } } /* Start sound stream. */ if (!(snd_port->options & PJMEDIA_SND_PORT_NO_AUTO_START)) { status = pjmedia_aud_stream_start(snd_port->aud_stream); } if (status != PJ_SUCCESS) { pjmedia_aud_stream_destroy(snd_port->aud_stream); snd_port->aud_stream = NULL; return status; } return PJ_SUCCESS; } /* * Stop the sound device. * This may be called even when there's no sound device in the port. */ static pj_status_t stop_sound_device( pjmedia_snd_port *snd_port ) { /* Check if we have sound stream device. */ if (snd_port->aud_stream) { pjmedia_aud_stream_stop(snd_port->aud_stream); pjmedia_aud_stream_destroy(snd_port->aud_stream); snd_port->aud_stream = NULL; } /* Destroy AEC */ if (snd_port->ec_state) { pjmedia_echo_destroy(snd_port->ec_state); snd_port->ec_state = NULL; } return PJ_SUCCESS; } /* * Create bidirectional port. */ PJ_DEF(pj_status_t) pjmedia_snd_port_create( pj_pool_t *pool, int rec_id, int play_id, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_snd_port **p_port) { pjmedia_snd_port_param param; pj_status_t status; pjmedia_snd_port_param_default(¶m); status = pjmedia_aud_dev_default_param(rec_id, ¶m.base); if (status != PJ_SUCCESS) return status; param.base.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; param.base.rec_id = rec_id; param.base.play_id = play_id; param.base.clock_rate = clock_rate; param.base.channel_count = channel_count; param.base.samples_per_frame = samples_per_frame; param.base.bits_per_sample = bits_per_sample; param.options = options; param.ec_options = 0; return pjmedia_snd_port_create2(pool, ¶m, p_port); } /* * Create sound recorder AEC. */ PJ_DEF(pj_status_t) pjmedia_snd_port_create_rec( pj_pool_t *pool, int dev_id, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_snd_port **p_port) { pjmedia_snd_port_param param; pj_status_t status; pjmedia_snd_port_param_default(¶m); status = pjmedia_aud_dev_default_param(dev_id, ¶m.base); if (status != PJ_SUCCESS) return status; param.base.dir = PJMEDIA_DIR_CAPTURE; param.base.rec_id = dev_id; param.base.clock_rate = clock_rate; param.base.channel_count = channel_count; param.base.samples_per_frame = samples_per_frame; param.base.bits_per_sample = bits_per_sample; param.options = options; param.ec_options = 0; return pjmedia_snd_port_create2(pool, ¶m, p_port); } /* * Create sound player port. */ PJ_DEF(pj_status_t) pjmedia_snd_port_create_player( pj_pool_t *pool, int dev_id, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_snd_port **p_port) { pjmedia_snd_port_param param; pj_status_t status; pjmedia_snd_port_param_default(¶m); status = pjmedia_aud_dev_default_param(dev_id, ¶m.base); if (status != PJ_SUCCESS) return status; param.base.dir = PJMEDIA_DIR_PLAYBACK; param.base.play_id = dev_id; param.base.clock_rate = clock_rate; param.base.channel_count = channel_count; param.base.samples_per_frame = samples_per_frame; param.base.bits_per_sample = bits_per_sample; param.options = options; param.ec_options = 0; return pjmedia_snd_port_create2(pool, ¶m, p_port); } /* * Create sound port. */ PJ_DEF(pj_status_t) pjmedia_snd_port_create2(pj_pool_t *pool, const pjmedia_snd_port_param *prm, pjmedia_snd_port **p_port) { pjmedia_snd_port *snd_port; pj_status_t status; unsigned ptime_usec; PJ_ASSERT_RETURN(pool && prm && p_port, PJ_EINVAL); snd_port = PJ_POOL_ZALLOC_T(pool, pjmedia_snd_port); PJ_ASSERT_RETURN(snd_port, PJ_ENOMEM); snd_port->dir = prm->base.dir; snd_port->rec_id = prm->base.rec_id; snd_port->play_id = prm->base.play_id; snd_port->clock_rate = prm->base.clock_rate; snd_port->channel_count = prm->base.channel_count; snd_port->samples_per_frame = prm->base.samples_per_frame; snd_port->bits_per_sample = prm->base.bits_per_sample; pj_memcpy(&snd_port->aud_param, &prm->base, sizeof(snd_port->aud_param)); snd_port->options = prm->options; snd_port->prm_ec_options = prm->ec_options; ptime_usec = prm->base.samples_per_frame * 1000 / prm->base.channel_count / prm->base.clock_rate * 1000; pjmedia_clock_src_init(&snd_port->cap_clocksrc, PJMEDIA_TYPE_AUDIO, snd_port->clock_rate, ptime_usec); pjmedia_clock_src_init(&snd_port->play_clocksrc, PJMEDIA_TYPE_AUDIO, snd_port->clock_rate, ptime_usec); /* Start sound device immediately. * If there's no port connected, the sound callback will return * empty signal. */ status = start_sound_device( pool, snd_port ); if (status != PJ_SUCCESS) { pjmedia_snd_port_destroy(snd_port); return status; } *p_port = snd_port; return PJ_SUCCESS; } /* * Destroy port (also destroys the sound device). */ PJ_DEF(pj_status_t) pjmedia_snd_port_destroy(pjmedia_snd_port *snd_port) { PJ_ASSERT_RETURN(snd_port, PJ_EINVAL); return stop_sound_device(snd_port); } /* * Retrieve the sound stream associated by this sound device port. */ PJ_DEF(pjmedia_aud_stream*) pjmedia_snd_port_get_snd_stream( pjmedia_snd_port *snd_port) { PJ_ASSERT_RETURN(snd_port, NULL); return snd_port->aud_stream; } +/* Reset EC state */ +PJ_DEF(pj_status_t) pjmedia_snd_port_reset_ec_state( pjmedia_snd_port *snd_port ) +{ + PJ_ASSERT_RETURN(snd_port, PJ_EINVAL); + if (snd_port->ec_state) { + pjmedia_echo_reset(snd_port->ec_state); + PJ_LOG(4,(THIS_FILE, "EC reset")); + } + return PJ_SUCCESS; +} + + /* * Change EC settings. */ PJ_DEF(pj_status_t) pjmedia_snd_port_set_ec( pjmedia_snd_port *snd_port, pj_pool_t *pool, unsigned tail_ms, unsigned options) { pjmedia_aud_param prm; pj_status_t status; /* Sound must be opened in full-duplex mode */ PJ_ASSERT_RETURN(snd_port && snd_port->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK, PJ_EINVALIDOP); /* Determine whether we use device or software EC */ if ((snd_port->prm_ec_options & PJMEDIA_ECHO_USE_SW_ECHO) == 0 && (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC)) { /* We use device EC */ pj_bool_t ec_enabled; /* Query EC status */ status = pjmedia_aud_stream_get_cap(snd_port->aud_stream, PJMEDIA_AUD_DEV_CAP_EC, &ec_enabled); if (status != PJ_SUCCESS) return status; if (tail_ms != 0) { /* Change EC setting */ if (!ec_enabled) { /* Enable EC first */ pj_bool_t value = PJ_TRUE; status = pjmedia_aud_stream_set_cap(snd_port->aud_stream, PJMEDIA_AUD_DEV_CAP_EC, &value); if (status != PJ_SUCCESS) return status; } if ((snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC_TAIL)==0) { /* Device does not support setting EC tail */ return PJMEDIA_EAUD_INVCAP; } return pjmedia_aud_stream_set_cap(snd_port->aud_stream, PJMEDIA_AUD_DEV_CAP_EC_TAIL, &tail_ms); } else if (ec_enabled) { /* Disable EC */ pj_bool_t value = PJ_FALSE; return pjmedia_aud_stream_set_cap(snd_port->aud_stream, PJMEDIA_AUD_DEV_CAP_EC, &value); } else { /* Request to disable EC but EC has been disabled */ /* Do nothing */ return PJ_SUCCESS; } } else { /* We use software EC */ /* Check if there is change in parameters */ if (tail_ms==snd_port->ec_tail_len && options==snd_port->ec_options) { PJ_LOG(5,(THIS_FILE, "pjmedia_snd_port_set_ec() ignored, no " "change in settings")); return PJ_SUCCESS; } status = pjmedia_aud_stream_get_param(snd_port->aud_stream, &prm); if (status != PJ_SUCCESS) return status; /* Audio stream must be in PCM format */ PJ_ASSERT_RETURN(prm.ext_fmt.id == PJMEDIA_FORMAT_PCM, PJ_EINVALIDOP); /* Destroy AEC */ if (snd_port->ec_state) { pjmedia_echo_destroy(snd_port->ec_state); snd_port->ec_state = NULL; } if (tail_ms != 0) { unsigned delay_ms; //No need to add input latency in the latency calculation, //since actual input latency should be zero. //delay_ms = (si.rec_latency + si.play_latency) * 1000 / // snd_port->clock_rate; /* Set EC latency to 3/4 of output latency to reduce the * possibility of missing/late reference frame. */ delay_ms = prm.output_latency_ms * 3/4; status = pjmedia_echo_create2(pool, snd_port->clock_rate, snd_port->channel_count, snd_port->samples_per_frame, tail_ms, delay_ms, options, &snd_port->ec_state); if (status != PJ_SUCCESS) snd_port->ec_state = NULL; else snd_port->ec_suspended = PJ_FALSE; } else { PJ_LOG(4,(THIS_FILE, "Echo canceller is now disabled in the " "sound port")); status = PJ_SUCCESS; } snd_port->ec_options = options; snd_port->ec_tail_len = tail_ms; } return status; } /* Get AEC tail length */ PJ_DEF(pj_status_t) pjmedia_snd_port_get_ec_tail( pjmedia_snd_port *snd_port, unsigned *p_length) { PJ_ASSERT_RETURN(snd_port && p_length, PJ_EINVAL); /* Determine whether we use device or software EC */ if (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC) { /* We use device EC */ pj_bool_t ec_enabled; pj_status_t status; /* Query EC status */ status = pjmedia_aud_stream_get_cap(snd_port->aud_stream, PJMEDIA_AUD_DEV_CAP_EC, &ec_enabled); if (status != PJ_SUCCESS) return status; if (!ec_enabled) { *p_length = 0; } else if (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC_TAIL) { /* Get device EC tail */ status = pjmedia_aud_stream_get_cap(snd_port->aud_stream, PJMEDIA_AUD_DEV_CAP_EC_TAIL, p_length); if (status != PJ_SUCCESS) return status; } else { /* Just use default */ *p_length = AEC_TAIL; } } else { /* We use software EC */ *p_length = snd_port->ec_state ? snd_port->ec_tail_len : 0; } return PJ_SUCCESS; } /* * Get clock source. */ PJ_DEF(pjmedia_clock_src *) pjmedia_snd_port_get_clock_src( pjmedia_snd_port *snd_port, pjmedia_dir dir ) { return (dir == PJMEDIA_DIR_CAPTURE? &snd_port->cap_clocksrc: &snd_port->play_clocksrc); } /* * Connect a port. */ PJ_DEF(pj_status_t) pjmedia_snd_port_connect( pjmedia_snd_port *snd_port, pjmedia_port *port) { pjmedia_audio_format_detail *afd; PJ_ASSERT_RETURN(snd_port && port, PJ_EINVAL); afd = pjmedia_format_get_audio_format_detail(&port->info.fmt, PJ_TRUE); /* Check that port has the same configuration as the sound device * port. */ if (afd->clock_rate != snd_port->clock_rate) return PJMEDIA_ENCCLOCKRATE; if (PJMEDIA_AFD_SPF(afd) != snd_port->samples_per_frame) return PJMEDIA_ENCSAMPLESPFRAME; if (afd->channel_count != snd_port->channel_count) return PJMEDIA_ENCCHANNEL; if (afd->bits_per_sample != snd_port->bits_per_sample) return PJMEDIA_ENCBITS; /* Port is okay. */ snd_port->port = port; return PJ_SUCCESS; } /* * Get the connected port. */ PJ_DEF(pjmedia_port*) pjmedia_snd_port_get_port(pjmedia_snd_port *snd_port) { PJ_ASSERT_RETURN(snd_port, NULL); return snd_port->port; } /* * Disconnect port. */ PJ_DEF(pj_status_t) pjmedia_snd_port_disconnect(pjmedia_snd_port *snd_port) { PJ_ASSERT_RETURN(snd_port, PJ_EINVAL); snd_port->port = NULL; return PJ_SUCCESS; }