summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/echo_webrtc.c
blob: c3501f6c3fcfa022f0186d47ed7135859bd16800 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* $Id$ */
/* 
 * Copyright (C) 2011-2015 Teluu Inc. (http://www.teluu.com)
 *
 * 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/echo.h>
#include <pjmedia/errno.h>
#include <pj/assert.h>
#include <pj/log.h>
#include <pj/pool.h>

#if defined(PJMEDIA_HAS_WEBRTC_AEC) && PJMEDIA_HAS_WEBRTC_AEC != 0

#include <webrtc/modules/audio_processing/aec/include/echo_cancellation.h>
#include <webrtc/modules/audio_processing/aecm/include/echo_control_mobile.h>
#include <webrtc/modules/audio_processing/aec/aec_core.h>

#include "echo_internal.h"

#define THIS_FILE		"echo_webrtc.c"

#if PJMEDIA_WEBRTC_AEC_USE_MOBILE == 1
    #include <webrtc/modules/audio_processing/ns/include/noise_suppression_x.h>

    #define NsHandle NsxHandle
    #define WebRtcNs_Create WebRtcNsx_Create
    #define WebRtcNs_Init WebRtcNsx_Init
    #define WebRtcNs_Free WebRtcNsx_Free

    #define WebRtcAec_Create WebRtcAecm_Create
    #define WebRtcAec_Init(handle, clock, sclock) WebRtcAecm_Init(handle, clock)
    #define WebRtcAec_Free WebRtcAecm_Free
    #define WebRtcAec_get_error_code WebRtcAecm_get_error_code
    #define WebRtcAec_enable_delay_agnostic(core, enable)
    #define WebRtcAec_set_config WebRtcAecm_set_config
    #define WebRtcAec_BufferFarend WebRtcAecm_BufferFarend
    #define AecConfig AecmConfig
    #define SHOW_DELAY_METRICS	0
    typedef short sample;
#else
    #include <webrtc/modules/audio_processing/ns/include/noise_suppression.h>

    typedef float sample;

    /* If SHOW_DELAY_METRICS is set to non-zero, delay metrics stats will
     * be printed every SHOW_DELAY_METRICS-th call to webrtc_aec_cancel_echo().
     * For example, if ptime is 20ms, set this to 250 to print the metrics
     * every 250*20/1000=5 seconds.
     */
    #define SHOW_DELAY_METRICS	0
    
#endif

#define BUF_LEN			160

typedef struct webrtc_ec
{
    void       *AEC_inst;
    NsHandle   *NS_inst;
    unsigned    options;
    unsigned	samples_per_frame;
    unsigned	tail;
    unsigned    clock_rate;
    unsigned	channel_count;
    unsigned    subframe_len;
    sample      tmp_buf[BUF_LEN];
    sample      tmp_buf2[BUF_LEN];
#if SHOW_DELAY_METRICS
    unsigned	counter;
#endif
} webrtc_ec;


static void print_webrtc_aec_error(const char *tag, void *AEC_inst)
{
    unsigned status = WebRtcAec_get_error_code(AEC_inst);
    PJ_LOG(3, (THIS_FILE, "WebRTC AEC error (%s) %d ", tag, status));
}

static void set_config(void *AEC_inst, unsigned options)
{
    unsigned aggr_opt = options & PJMEDIA_ECHO_AGGRESSIVENESS_MASK;
    int status;
    AecConfig aec_config;

#if PJMEDIA_WEBRTC_AEC_USE_MOBILE
    aec_config.echoMode = 3;
    if (aggr_opt == PJMEDIA_ECHO_AGGRESSIVENESS_CONSERVATIVE)
        aec_config.echoMode = 0;
    else if (aggr_opt == PJMEDIA_ECHO_AGGRESSIVENESS_AGGRESSIVE)
   	aec_config.echoMode = 4;
    aec_config.cngMode = AecmTrue;
#else
    
    aec_config.nlpMode = kAecNlpModerate;
    if (aggr_opt == PJMEDIA_ECHO_AGGRESSIVENESS_CONSERVATIVE)
	aec_config.nlpMode = kAecNlpConservative;
    else if (aggr_opt == PJMEDIA_ECHO_AGGRESSIVENESS_AGGRESSIVE)
        aec_config.nlpMode = kAecNlpAggressive;
    else
        aec_config.nlpMode = kAecNlpModerate;
    
    aec_config.skewMode = kAecFalse;
#if SHOW_DELAY_METRICS
    aec_config.metricsMode = kAecTrue;
    aec_config.delay_logging = kAecTrue;
#else
    aec_config.metricsMode = kAecFalse;
    aec_config.delay_logging = kAecFalse;
#endif

#endif

    status = WebRtcAec_set_config(AEC_inst, aec_config);
    if (status != 0) {
        print_webrtc_aec_error("Init config", AEC_inst);
    }
}

/*
 * Create the AEC.
 */
PJ_DEF(pj_status_t) webrtc_aec_create(pj_pool_t *pool,
                                      unsigned clock_rate,
                                      unsigned channel_count,
                                      unsigned samples_per_frame,
                                      unsigned tail_ms,
                                      unsigned options,
                                      void **p_echo )
{
    webrtc_ec *echo;
    int status;
    
    *p_echo = NULL;
    
    echo = PJ_POOL_ZALLOC_T(pool, webrtc_ec);
    PJ_ASSERT_RETURN(echo != NULL, PJ_ENOMEM);
    
    /* Currently we only support mono. */
    if (channel_count != 1)
    	return PJ_ENOTSUP;

    echo->channel_count = channel_count;
    echo->samples_per_frame = samples_per_frame;
    echo->tail = tail_ms;
    echo->clock_rate = clock_rate;
    /* SWB is processed as 160 frame size */
    if (clock_rate > 8000)
        echo->subframe_len = 160;
    else
    	echo->subframe_len = 80;
    echo->options = options;
    
    /* Create WebRTC AEC */
    echo->AEC_inst = WebRtcAec_Create();
    if (!echo->AEC_inst) {
    	return PJ_ENOMEM;
    }
    
    /* Init WebRTC AEC */
    status = WebRtcAec_Init(echo->AEC_inst, clock_rate, clock_rate);
    if (status != 0) {
        print_webrtc_aec_error("Init", echo->AEC_inst);
        WebRtcAec_Free(echo->AEC_inst);
    	return PJ_ENOTSUP;
    }

    /* WebRtc is very dependent on delay calculation, which will be passed
     * to WebRtcAec_Process() below. A poor estimate, even by as little as
     * 40ms, may affect the echo cancellation results greatly.
     * Hence, we need to enable delay-agnostic echo cancellation. This
     * low-level feature relies on internally estimated delays between
     * the process and reverse streams, thus not relying on reported
     * system delays.
     * Still, with the delay agnostic feature, it may take some time (5-10s
     * or more) for the Aec module to learn the optimal delay, thus
     * a good initial estimate is necessary for good EC quality in
     * the beginning of a call.
     */
    WebRtcAec_enable_delay_agnostic(WebRtcAec_aec_core(echo->AEC_inst), 1);
    
    set_config(echo->AEC_inst, options);

    if (options & PJMEDIA_ECHO_USE_NOISE_SUPPRESSOR) {
        echo->NS_inst = WebRtcNs_Create();
        if (echo->NS_inst) {
            status = WebRtcNs_Init(echo->NS_inst, clock_rate);
            if (status != 0) {
                WebRtcNs_Free(echo->NS_inst);
                echo->NS_inst = NULL;
            }
        }
        if (!echo->NS_inst) {
            PJ_LOG(3, (THIS_FILE, "Unable to create WebRTC noise suppressor"));
        }
    }

#if PJMEDIA_WEBRTC_AEC_USE_MOBILE
    PJ_LOG(3, (THIS_FILE, "WebRTC AEC mobile successfully created with "
			  "options %d", options));
#else
    PJ_LOG(3, (THIS_FILE, "WebRTC AEC successfully created with "
			  "options %d", options));
#endif

    /* Done */
    *p_echo = echo;
    return PJ_SUCCESS;
    
}


/*
 * Destroy AEC
 */
PJ_DEF(pj_status_t) webrtc_aec_destroy(void *state )
{
    webrtc_ec *echo = (webrtc_ec*) state;
    PJ_ASSERT_RETURN(echo, PJ_EINVAL);
    
    if (echo->AEC_inst) {
    	WebRtcAec_Free(echo->AEC_inst);
    	echo->AEC_inst = NULL;
    }
    if (echo->NS_inst) {
        WebRtcNs_Free(echo->NS_inst);
        echo->NS_inst = NULL;
    }
    
    return PJ_SUCCESS;
}


/*
 * Reset AEC
 */
PJ_DEF(void) webrtc_aec_reset(void *state )
{
    webrtc_ec *echo = (webrtc_ec*) state;
    int status;
    
    pj_assert(echo != NULL);
    
    /* Re-initialize the EC */
    status = WebRtcAec_Init(echo->AEC_inst, echo->clock_rate, echo->clock_rate);
    if (status != 0) {
        print_webrtc_aec_error("reset", echo->AEC_inst);
        return;
    }
    
    set_config(echo->AEC_inst, echo->options);
    
    PJ_LOG(4, (THIS_FILE, "WebRTC AEC reset succeeded"));
}


/*
 * Perform echo cancellation.
 */
PJ_DEF(pj_status_t) webrtc_aec_cancel_echo( void *state,
					    pj_int16_t *rec_frm,
					    const pj_int16_t *play_frm,
					    unsigned options,
					    void *reserved )
{
    webrtc_ec *echo = (webrtc_ec*) state;
    int status;
    unsigned i, j, frm_idx = 0;
    const sample * buf_ptr;
    sample * out_buf_ptr;

    /* Sanity checks */
    PJ_ASSERT_RETURN(echo && rec_frm && play_frm, PJ_EINVAL);
    
    for(i = echo->samples_per_frame / echo->subframe_len; i > 0; i--) {
#if PJMEDIA_WEBRTC_AEC_USE_MOBILE
	buf_ptr = &play_frm[frm_idx];
#else
        for (j = 0; j < echo->subframe_len; j++) {
            echo->tmp_buf[j] = rec_frm[frm_idx+j];
            echo->tmp_buf2[j] = play_frm[frm_idx+j];
        }
        buf_ptr = echo->tmp_buf2;
#endif
        
        /* Feed farend buffer */
        status = WebRtcAec_BufferFarend(echo->AEC_inst, buf_ptr,
                                        echo->subframe_len);
        if (status != 0) {
            print_webrtc_aec_error("Buffer farend", echo->AEC_inst);
            return PJ_EUNKNOWN;
        }        
        
	buf_ptr = echo->tmp_buf;
	out_buf_ptr = echo->tmp_buf2;
        if (echo->NS_inst) {
#if PJMEDIA_WEBRTC_AEC_USE_MOBILE
	    buf_ptr = &rec_frm[frm_idx];
            WebRtcNsx_Process(echo->NS_inst, &buf_ptr, echo->channel_count,
            		      &out_buf_ptr);
            buf_ptr = out_buf_ptr;
            out_buf_ptr = echo->tmp_buf;
#else
            WebRtcNs_Analyze(echo->NS_inst, buf_ptr);
#endif
        }
        
        /* Process echo cancellation */
#if PJMEDIA_WEBRTC_AEC_USE_MOBILE
        status = WebRtcAecm_Process(echo->AEC_inst, &rec_frm[frm_idx],
        			    (echo->NS_inst? buf_ptr: NULL),
        			    out_buf_ptr, echo->subframe_len,
        			    echo->tail);
#else
        status = WebRtcAec_Process(echo->AEC_inst, &buf_ptr,
                                   echo->channel_count, &out_buf_ptr,
                                   echo->subframe_len, echo->tail, 0);
#endif
        if (status != 0) {
            print_webrtc_aec_error("Process echo", echo->AEC_inst);
            return PJ_EUNKNOWN;
        }

#if !PJMEDIA_WEBRTC_AEC_USE_MOBILE
    	if (echo->NS_inst) {
            /* Noise suppression */
	    buf_ptr = echo->tmp_buf2;
	    out_buf_ptr = echo->tmp_buf;
            WebRtcNs_Process(echo->NS_inst, &buf_ptr,
                             echo->channel_count, &out_buf_ptr);
    	}
#endif
    
       	for (j = 0; j < echo->subframe_len; j++) {
 	    rec_frm[frm_idx++] = (pj_int16_t)out_buf_ptr[j];
    	}
    }

#if SHOW_DELAY_METRICS
    if (++echo->counter >= SHOW_DELAY_METRICS) {
    	int median, std;
    	float frac_delay;

        if (WebRtcAec_GetDelayMetrics(echo->AEC_inst, &median, &std,
        			      &frac_delay) == 0)
        {
            PJ_LOG(3, (THIS_FILE, "WebRTC delay metrics: median=%d, std=%d, "
            			  "fraction of poor delays=%f",
            			  median, std, frac_delay));
        }
        echo->counter = 0;
    }
#endif

    return PJ_SUCCESS;
}

#endif