1
/* $Id: echo_port.c 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pjmedia/echo_port.h>
21
#include <pjmedia/echo.h>
22
#include <pjmedia/errno.h>
23
#include <pj/assert.h>
28
#define THIS_FILE "ec_port.c"
29
#define SIGNATURE PJMEDIA_SIG_PORT_ECHO
35
pjmedia_port *dn_port;
36
pjmedia_echo_state *ec;
40
static pj_status_t ec_put_frame(pjmedia_port *this_port,
41
pjmedia_frame *frame);
42
static pj_status_t ec_get_frame(pjmedia_port *this_port,
43
pjmedia_frame *frame);
44
static pj_status_t ec_on_destroy(pjmedia_port *this_port);
47
PJ_DEF(pj_status_t) pjmedia_echo_port_create(pj_pool_t *pool,
48
pjmedia_port *dn_port,
52
pjmedia_port **p_port )
54
const pj_str_t AEC = { "EC", 2 };
55
pjmedia_audio_format_detail *afd;
59
PJ_ASSERT_RETURN(pool && dn_port && p_port, PJ_EINVAL);
61
afd = pjmedia_format_get_audio_format_detail(&dn_port->info.fmt, PJ_TRUE);
63
PJ_ASSERT_RETURN(afd->bits_per_sample==16 && tail_ms,
66
/* Create the port and the AEC itself */
67
ec = PJ_POOL_ZALLOC_T(pool, struct ec);
69
pjmedia_port_info_init(&ec->base.info, &AEC, SIGNATURE,
73
PJMEDIA_AFD_SPF(afd));
75
status = pjmedia_echo_create2(pool, afd->clock_rate,
78
tail_ms, latency_ms, options, &ec->ec);
79
if (status != PJ_SUCCESS)
83
ec->dn_port = dn_port;
84
ec->base.get_frame = &ec_get_frame;
85
ec->base.put_frame = &ec_put_frame;
86
ec->base.on_destroy = &ec_on_destroy;
95
static pj_status_t ec_put_frame( pjmedia_port *this_port,
98
struct ec *ec = (struct ec*)this_port;
100
PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
102
if (frame->type == PJMEDIA_FRAME_TYPE_NONE ) {
103
return pjmedia_port_put_frame(ec->dn_port, frame);
106
PJ_ASSERT_RETURN(frame->size == PJMEDIA_PIA_AVG_FSZ(&this_port->info),
109
pjmedia_echo_capture(ec->ec, (pj_int16_t*)frame->buf, 0);
111
return pjmedia_port_put_frame(ec->dn_port, frame);
115
static pj_status_t ec_get_frame( pjmedia_port *this_port,
116
pjmedia_frame *frame)
118
struct ec *ec = (struct ec*)this_port;
121
PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
123
status = pjmedia_port_get_frame(ec->dn_port, frame);
124
if (status!=PJ_SUCCESS || frame->type!=PJMEDIA_FRAME_TYPE_AUDIO) {
125
pjmedia_zero_samples((pj_int16_t*)frame->buf,
126
PJMEDIA_PIA_SPF(&this_port->info));
129
pjmedia_echo_playback(ec->ec, (pj_int16_t*)frame->buf);
135
static pj_status_t ec_on_destroy(pjmedia_port *this_port)
137
struct ec *ec = (struct ec*)this_port;
139
PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
141
pjmedia_echo_destroy(ec->ec);