blob: 4c098cc4012136ae42b94a0143bbdeff07114d46 [file] [log] [blame]
Alexandre Lision67916dd2014-01-24 13:33:04 -05001/* $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#include <pjmedia/echo_port.h>
21#include <pjmedia/echo.h>
22#include <pjmedia/errno.h>
23#include <pj/assert.h>
24#include <pj/log.h>
25#include <pj/pool.h>
26
27
28#define THIS_FILE "ec_port.c"
29#define SIGNATURE PJMEDIA_SIG_PORT_ECHO
30#define BUF_COUNT 32
31
32struct ec
33{
34 pjmedia_port base;
35 pjmedia_port *dn_port;
36 pjmedia_echo_state *ec;
37};
38
39
40static pj_status_t ec_put_frame(pjmedia_port *this_port,
41 pjmedia_frame *frame);
42static pj_status_t ec_get_frame(pjmedia_port *this_port,
43 pjmedia_frame *frame);
44static pj_status_t ec_on_destroy(pjmedia_port *this_port);
45
46
47PJ_DEF(pj_status_t) pjmedia_echo_port_create(pj_pool_t *pool,
48 pjmedia_port *dn_port,
49 unsigned tail_ms,
50 unsigned latency_ms,
51 unsigned options,
52 pjmedia_port **p_port )
53{
54 const pj_str_t AEC = { "EC", 2 };
55 pjmedia_audio_format_detail *afd;
56 struct ec *ec;
57 pj_status_t status;
58
59 PJ_ASSERT_RETURN(pool && dn_port && p_port, PJ_EINVAL);
60
61 afd = pjmedia_format_get_audio_format_detail(&dn_port->info.fmt, PJ_TRUE);
62
63 PJ_ASSERT_RETURN(afd->bits_per_sample==16 && tail_ms,
64 PJ_EINVAL);
65
66 /* Create the port and the AEC itself */
67 ec = PJ_POOL_ZALLOC_T(pool, struct ec);
68
69 pjmedia_port_info_init(&ec->base.info, &AEC, SIGNATURE,
70 afd->clock_rate,
71 afd->channel_count,
72 afd->bits_per_sample,
73 PJMEDIA_AFD_SPF(afd));
74
75 status = pjmedia_echo_create2(pool, afd->clock_rate,
76 afd->channel_count,
77 PJMEDIA_AFD_SPF(afd),
78 tail_ms, latency_ms, options, &ec->ec);
79 if (status != PJ_SUCCESS)
80 return status;
81
82 /* More init */
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;
87
88 /* Done */
89 *p_port = &ec->base;
90
91 return PJ_SUCCESS;
92}
93
94
95static pj_status_t ec_put_frame( pjmedia_port *this_port,
96 pjmedia_frame *frame)
97{
98 struct ec *ec = (struct ec*)this_port;
99
100 PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
101
102 if (frame->type == PJMEDIA_FRAME_TYPE_NONE ) {
103 return pjmedia_port_put_frame(ec->dn_port, frame);
104 }
105
106 PJ_ASSERT_RETURN(frame->size == PJMEDIA_PIA_AVG_FSZ(&this_port->info),
107 PJ_EINVAL);
108
109 pjmedia_echo_capture(ec->ec, (pj_int16_t*)frame->buf, 0);
110
111 return pjmedia_port_put_frame(ec->dn_port, frame);
112}
113
114
115static pj_status_t ec_get_frame( pjmedia_port *this_port,
116 pjmedia_frame *frame)
117{
118 struct ec *ec = (struct ec*)this_port;
119 pj_status_t status;
120
121 PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
122
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));
127 }
128
129 pjmedia_echo_playback(ec->ec, (pj_int16_t*)frame->buf);
130
131 return status;
132}
133
134
135static pj_status_t ec_on_destroy(pjmedia_port *this_port)
136{
137 struct ec *ec = (struct ec*)this_port;
138
139 PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
140
141 pjmedia_echo_destroy(ec->ec);
142
143 return PJ_SUCCESS;
144}
145
146