summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/aec_port.c
blob: d00d9298c6a0f06df665adf1790baca81f657691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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;
}