diff options
author | Benny Prijono <bennylp@teluu.com> | 2006-08-02 18:22:22 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2006-08-02 18:22:22 +0000 |
commit | 2c5b8f5dca277dd3fee1ab1298bb0b2308372eae (patch) | |
tree | 395e0e06ffdb66993cb79cd31f5b38ec561e0c67 | |
parent | 6d4d05a0b62d4d7be91f807943009f2ec57c2421 (diff) |
Added experimental AEC media port (aec_port.[hc]) based on Speex AEC in pjmedia and pjsua-lib.
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@642 74dad513-b988-da41-8d7b-12977e46ad98
-rw-r--r-- | pjmedia/build/pjmedia.dsp | 12 | ||||
-rw-r--r-- | pjmedia/include/pjmedia.h | 1 | ||||
-rw-r--r-- | pjmedia/include/pjmedia/aec_port.h | 65 | ||||
-rw-r--r-- | pjmedia/src/pjmedia/aec_port.c | 162 | ||||
-rw-r--r-- | pjsip/include/pjsua-lib/pjsua.h | 7 | ||||
-rw-r--r-- | pjsip/include/pjsua-lib/pjsua_internal.h | 1 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_media.c | 19 |
7 files changed, 263 insertions, 4 deletions
diff --git a/pjmedia/build/pjmedia.dsp b/pjmedia/build/pjmedia.dsp index e024f655..62a544d3 100644 --- a/pjmedia/build/pjmedia.dsp +++ b/pjmedia/build/pjmedia.dsp @@ -41,7 +41,7 @@ RSC=rc.exe # PROP Intermediate_Dir ".\output\pjmedia-i386-win32-vc6-release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
-# ADD CPP /nologo /MD /W4 /GX /O2 /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../src/pjmedia/portaudio" /D "NDEBUG" /D "PA_NO_ASIO" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /c
+# ADD CPP /nologo /MD /W4 /GX /O2 /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../src/pjmedia/portaudio" /I "../src/pjmedia-codec" /D "NDEBUG" /D "PA_NO_ASIO" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /c
# SUBTRACT CPP /YX
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
@@ -65,7 +65,7 @@ LIB32=link.exe -lib # PROP Intermediate_Dir ".\output\pjmedia-i386-win32-vc6-debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
-# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../src/pjmedia/portaudio" /D "_DEBUG" /D "PA_NO_ASIO" /D "PA_NO_WMME" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /GZ /c
+# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../src/pjmedia/portaudio" /I "../src/pjmedia-codec" /D "_DEBUG" /D "PA_NO_ASIO" /D "PA_NO_WMME" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /GZ /c
# SUBTRACT CPP /YX
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
@@ -87,6 +87,10 @@ LIB32=link.exe -lib # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
+SOURCE=..\src\pjmedia\aec_port.c
+# End Source File
+# Begin Source File
+
SOURCE=..\src\pjmedia\alaw_ulaw.c
# End Source File
# Begin Source File
@@ -227,6 +231,10 @@ SOURCE=..\src\pjmedia\wave.c # PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
+SOURCE=..\include\pjmedia\aec_port.h
+# End Source File
+# Begin Source File
+
SOURCE=..\include\pjmedia\clock.h
# End Source File
# Begin Source File
diff --git a/pjmedia/include/pjmedia.h b/pjmedia/include/pjmedia.h index a6c0e49b..fc79c463 100644 --- a/pjmedia/include/pjmedia.h +++ b/pjmedia/include/pjmedia.h @@ -25,6 +25,7 @@ */ #include <pjmedia/types.h> +#include <pjmedia/aec_port.h> #include <pjmedia/clock.h> #include <pjmedia/codec.h> #include <pjmedia/conference.h> diff --git a/pjmedia/include/pjmedia/aec_port.h b/pjmedia/include/pjmedia/aec_port.h new file mode 100644 index 00000000..f18b1378 --- /dev/null +++ b/pjmedia/include/pjmedia/aec_port.h @@ -0,0 +1,65 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org> + * + * 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_AEC_PORT_H__ +#define __PJMEDIA_AEC_PORT_H__ + +/** + * @file aec_port.h + * @brief AEC (Accoustic Echo Cancellation) media port. + */ +#include <pjmedia/port.h> + + + +/** + * @defgroup PJMEDIA_AEC_PORT AEC Port + * @ingroup PJMEDIA_PORT + * @brief AEC (Accoustic Echo Cancellation) media port. + * @{ + */ + + +PJ_BEGIN_DECL + + +/** + * Create AEC port. + * + * @param pool Pool to allocate memory. + * @param dn_port Downstream port. + * @param tail_length Tail length in samples. + * @param p_port Pointer to receive the port instance. + * + * @return PJ_SUCCESS on success. + */ +PJ_DECL(pj_status_t) pjmedia_aec_port_create( pj_pool_t *pool, + pjmedia_port *dn_port, + unsigned tail_length, + pjmedia_port **p_port ); + + + +PJ_END_DECL + +/** + * @} + */ + + +#endif /* __PJMEDIA_AEC_PORT_H__ */ diff --git a/pjmedia/src/pjmedia/aec_port.c b/pjmedia/src/pjmedia/aec_port.c new file mode 100644 index 00000000..d00d9298 --- /dev/null +++ b/pjmedia/src/pjmedia/aec_port.c @@ -0,0 +1,162 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org> + * + * 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 <pjmedia/aec_port.h> +#include "../pjmedia-codec/speex/speex_echo.h" +#include <pjmedia/errno.h> +#include <pj/assert.h> +#include <pj/log.h> +#include <pj/pool.h> + + +#define THIS_FILE "aec_port.c" +#define SIGNATURE PJMEDIA_PORT_SIGNATURE('A', 'E', 'C', ' ') + + +struct aec_port +{ + pjmedia_port base; + pjmedia_port *dn_port; + SpeexEchoState *state; + pj_int16_t *tmp_frame; + pj_bool_t has_frame; + pj_int16_t *last_frame; +}; + + +static pj_status_t aec_put_frame(pjmedia_port *this_port, + const pjmedia_frame *frame); +static pj_status_t aec_get_frame(pjmedia_port *this_port, + pjmedia_frame *frame); +static pj_status_t aec_on_destroy(pjmedia_port *this_port); + + +PJ_DEF(pj_status_t) pjmedia_aec_port_create( pj_pool_t *pool, + pjmedia_port *dn_port, + unsigned tail_length, + pjmedia_port **p_port ) +{ + const pj_str_t AEC = { "AEC", 3 }; + struct aec_port *aec_port; + int sampling_rate; + + PJ_ASSERT_RETURN(pool && dn_port && p_port, PJ_EINVAL); + PJ_ASSERT_RETURN(dn_port->info.bits_per_sample==16 && tail_length, + PJ_EINVAL); + + /* Create and initialize the port */ + aec_port = pj_pool_zalloc(pool, sizeof(struct aec_port)); + + pjmedia_port_info_init(&aec_port->base.info, &AEC, SIGNATURE, + dn_port->info.clock_rate, + dn_port->info.channel_count, + dn_port->info.bits_per_sample, + dn_port->info.samples_per_frame); + + aec_port->state = speex_echo_state_init(dn_port->info.samples_per_frame, + tail_length); + + /* Set sampling rate */ + sampling_rate = 0; + speex_echo_ctl(aec_port->state, SPEEX_ECHO_GET_SAMPLING_RATE, + &sampling_rate); + sampling_rate = dn_port->info.clock_rate; + speex_echo_ctl(aec_port->state, SPEEX_ECHO_SET_SAMPLING_RATE, + &sampling_rate); + + /* More init */ + aec_port->dn_port = dn_port; + aec_port->base.get_frame = &aec_get_frame; + aec_port->base.put_frame = &aec_put_frame; + aec_port->base.on_destroy = &aec_on_destroy; + + aec_port->last_frame = pj_pool_zalloc(pool, sizeof(pj_int16_t) * + dn_port->info.samples_per_frame); + aec_port->tmp_frame = pj_pool_zalloc(pool, sizeof(pj_int16_t) * + dn_port->info.samples_per_frame); + + /* Done */ + *p_port = &aec_port->base; + + PJ_LOG(4,(THIS_FILE, "AEC created for port %.*s", + (int)dn_port->info.name.slen, + dn_port->info.name.ptr)); + return PJ_SUCCESS; +} + + +static pj_status_t aec_put_frame(pjmedia_port *this_port, + const pjmedia_frame *frame) +{ + struct aec_port *aec_port = (struct aec_port*)this_port; + + PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL); + + if (frame->type == PJMEDIA_FRAME_TYPE_NONE || !aec_port->has_frame) { + return pjmedia_port_put_frame(aec_port->dn_port, frame); + } + + PJ_ASSERT_RETURN(frame->size == this_port->info.samples_per_frame * 2, + PJ_EINVAL); + + speex_echo_cancel(aec_port->state, + (const spx_int16_t*)frame->buf, + (const spx_int16_t*)aec_port->last_frame, + (spx_int16_t*)aec_port->tmp_frame, + NULL); + + pjmedia_copy_samples(frame->buf, aec_port->tmp_frame, + this_port->info.samples_per_frame); + + return pjmedia_port_put_frame(aec_port->dn_port, frame); +} + + +static pj_status_t aec_get_frame( pjmedia_port *this_port, + pjmedia_frame *frame) +{ + struct aec_port *aec_port = (struct aec_port*)this_port; + pj_status_t status; + + PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL); + + status = pjmedia_port_get_frame(aec_port->dn_port, frame); + if (status==PJ_SUCCESS && frame->type==PJMEDIA_FRAME_TYPE_AUDIO) { + aec_port->has_frame = PJ_TRUE; + pjmedia_copy_samples(aec_port->tmp_frame, frame->buf, + this_port->info.samples_per_frame); + } else { + aec_port->has_frame = PJ_FALSE; + } + + return status; +} + + +static pj_status_t aec_on_destroy(pjmedia_port *this_port) +{ + struct aec_port *aec_port = (struct aec_port*)this_port; + + PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL); + + speex_echo_state_destroy(aec_port->state); + + return PJ_SUCCESS; +} + + diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h index fe1b1a70..5ca718ce 100644 --- a/pjsip/include/pjsua-lib/pjsua.h +++ b/pjsip/include/pjsua-lib/pjsua.h @@ -2128,6 +2128,12 @@ struct pjsua_media_config */ unsigned rx_drop_pct; + /** + * Echo canceller tail length, in miliseconds. + * + * Default: 256 + */ + unsigned ec_tail_len; }; @@ -2146,6 +2152,7 @@ PJ_INLINE(void) pjsua_media_config_default(pjsua_media_config *cfg) cfg->thread_cnt = 1; cfg->quality = 6; cfg->ilbc_mode = 20; + cfg->ec_tail_len = 256; } diff --git a/pjsip/include/pjsua-lib/pjsua_internal.h b/pjsip/include/pjsua-lib/pjsua_internal.h index c862219f..95c5c40e 100644 --- a/pjsip/include/pjsua-lib/pjsua_internal.h +++ b/pjsip/include/pjsua-lib/pjsua_internal.h @@ -204,6 +204,7 @@ struct pjsua_data pjmedia_conf *mconf; /**< Conference bridge. */ int cap_dev; /**< Capture device ID. */ int play_dev; /**< Playback device ID. */ + pjmedia_port *aec_port; /**< AEC port to conf bridge. */ pj_bool_t no_snd; /**< No sound (app will manage it) */ pjmedia_snd_port *snd_port; /**< Sound port. */ pjmedia_master_port *null_snd; /**< Master port for null sound. */ diff --git a/pjsip/src/pjsua-lib/pjsua_media.c b/pjsip/src/pjsua-lib/pjsua_media.c index d92d0cb9..284cdb58 100644 --- a/pjsip/src/pjsua-lib/pjsua_media.c +++ b/pjsip/src/pjsua-lib/pjsua_media.c @@ -968,8 +968,23 @@ PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf); pj_assert(conf_port != NULL); - /* Connect to the conference port */ - status = pjmedia_snd_port_connect(pjsua_var.snd_port, conf_port); + /* Create AEC if it's not created */ + if (pjsua_var.aec_port == NULL) { + status = pjmedia_aec_port_create(pjsua_var.pool, conf_port, + conf_port->info.clock_rate * + pjsua_var.media_cfg.ec_tail_len / + 1000, + &pjsua_var.aec_port); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Unable to create AEC port", status); + pjmedia_snd_port_destroy(pjsua_var.snd_port); + pjsua_var.snd_port = NULL; + return status; + } + } + + /* Connect to the AEC port */ + status = pjmedia_snd_port_connect(pjsua_var.snd_port, pjsua_var.aec_port); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Unable to connect conference port to " "sound device", status); |