~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjmedia/src/pjmedia/sound_port.c

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: sound_port.c 4079 2012-04-24 10:26:07Z ming $ */
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/sound_port.h>
21
 
#include <pjmedia/alaw_ulaw.h>
22
 
#include <pjmedia/delaybuf.h>
23
 
#include <pjmedia/echo.h>
24
 
#include <pjmedia/errno.h>
25
 
#include <pj/assert.h>
26
 
#include <pj/log.h>
27
 
#include <pj/rand.h>
28
 
#include <pj/string.h>      /* pj_memset() */
29
 
 
30
 
#define AEC_TAIL            128     /* default AEC length in ms */
31
 
#define AEC_SUSPEND_LIMIT   5       /* seconds of no activity   */
32
 
 
33
 
#define THIS_FILE           "sound_port.c"
34
 
 
35
 
//#define TEST_OVERFLOW_UNDERFLOW
36
 
 
37
 
struct pjmedia_snd_port
38
 
{
39
 
    int                  rec_id;
40
 
    int                  play_id;
41
 
    pj_uint32_t          aud_caps;
42
 
    pjmedia_aud_param    aud_param;
43
 
    pjmedia_aud_stream  *aud_stream;
44
 
    pjmedia_dir          dir;
45
 
    pjmedia_port        *port;
46
 
 
47
 
    unsigned             clock_rate;
48
 
    unsigned             channel_count;
49
 
    unsigned             samples_per_frame;
50
 
    unsigned             bits_per_sample;
51
 
    unsigned             options;
52
 
    unsigned             prm_ec_options;
53
 
 
54
 
    /* software ec */
55
 
    pjmedia_echo_state  *ec_state;
56
 
    unsigned             ec_options;
57
 
    unsigned             ec_tail_len;
58
 
    pj_bool_t            ec_suspended;
59
 
    unsigned             ec_suspend_count;
60
 
    unsigned             ec_suspend_limit;
61
 
};
62
 
 
63
 
/*
64
 
 * The callback called by sound player when it needs more samples to be
65
 
 * played.
66
 
 */
67
 
static pj_status_t play_cb(void *user_data, pjmedia_frame *frame)
68
 
{
69
 
    pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
70
 
    pjmedia_port *port;
71
 
    const unsigned required_size = frame->size;
72
 
    pj_status_t status;
73
 
 
74
 
    port = snd_port->port;
75
 
    if (port == NULL)
76
 
        goto no_frame;
77
 
 
78
 
    status = pjmedia_port_get_frame(port, frame);
79
 
    if (status != PJ_SUCCESS)
80
 
        goto no_frame;
81
 
 
82
 
    if (frame->type != PJMEDIA_FRAME_TYPE_AUDIO)
83
 
        goto no_frame;
84
 
 
85
 
    /* Must supply the required samples */
86
 
    pj_assert(frame->size == required_size);
87
 
 
88
 
    if (snd_port->ec_state) {
89
 
        if (snd_port->ec_suspended) {
90
 
            snd_port->ec_suspended = PJ_FALSE;
91
 
            //pjmedia_echo_state_reset(snd_port->ec_state);
92
 
            PJ_LOG(4,(THIS_FILE, "EC activated"));
93
 
        }
94
 
        snd_port->ec_suspend_count = 0;
95
 
        pjmedia_echo_playback(snd_port->ec_state, (pj_int16_t*)frame->buf);
96
 
    }
97
 
 
98
 
 
99
 
    return PJ_SUCCESS;
100
 
 
101
 
no_frame:
102
 
    frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
103
 
    frame->size = required_size;
104
 
    pj_bzero(frame->buf, frame->size);
105
 
 
106
 
    if (snd_port->ec_state && !snd_port->ec_suspended) {
107
 
        ++snd_port->ec_suspend_count;
108
 
        if (snd_port->ec_suspend_count > snd_port->ec_suspend_limit) {
109
 
            snd_port->ec_suspended = PJ_TRUE;
110
 
            PJ_LOG(4,(THIS_FILE, "EC suspended because of inactivity"));
111
 
        }
112
 
        if (snd_port->ec_state) {
113
 
            /* To maintain correct delay in EC */
114
 
            pjmedia_echo_playback(snd_port->ec_state, (pj_int16_t*)frame->buf);
115
 
        }
116
 
    }
117
 
 
118
 
    return PJ_SUCCESS;
119
 
}
120
 
 
121
 
 
122
 
/*
123
 
 * The callback called by sound recorder when it has finished capturing a
124
 
 * frame.
125
 
 */
126
 
static pj_status_t rec_cb(void *user_data, pjmedia_frame *frame)
127
 
{
128
 
    pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
129
 
    pjmedia_port *port;
130
 
 
131
 
    port = snd_port->port;
132
 
    if (port == NULL)
133
 
        return PJ_SUCCESS;
134
 
 
135
 
    /* Cancel echo */
136
 
    if (snd_port->ec_state && !snd_port->ec_suspended) {
137
 
        pjmedia_echo_capture(snd_port->ec_state, (pj_int16_t*) frame->buf, 0);
138
 
    }
139
 
 
140
 
    pjmedia_port_put_frame(port, frame);
141
 
 
142
 
    return PJ_SUCCESS;
143
 
}
144
 
 
145
 
/*
146
 
 * The callback called by sound player when it needs more samples to be
147
 
 * played. This version is for non-PCM data.
148
 
 */
149
 
static pj_status_t play_cb_ext(void *user_data, pjmedia_frame *frame)
150
 
{
151
 
    pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
152
 
    pjmedia_port *port = snd_port->port;
153
 
 
154
 
    if (port == NULL) {
155
 
        frame->type = PJMEDIA_FRAME_TYPE_NONE;
156
 
        return PJ_SUCCESS;
157
 
    }
158
 
 
159
 
    pjmedia_port_get_frame(port, frame);
160
 
 
161
 
    return PJ_SUCCESS;
162
 
}
163
 
 
164
 
 
165
 
/*
166
 
 * The callback called by sound recorder when it has finished capturing a
167
 
 * frame. This version is for non-PCM data.
168
 
 */
169
 
static pj_status_t rec_cb_ext(void *user_data, pjmedia_frame *frame)
170
 
{
171
 
    pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
172
 
    pjmedia_port *port;
173
 
 
174
 
    port = snd_port->port;
175
 
    if (port == NULL)
176
 
        return PJ_SUCCESS;
177
 
 
178
 
    pjmedia_port_put_frame(port, frame);
179
 
 
180
 
    return PJ_SUCCESS;
181
 
}
182
 
 
183
 
/* Initialize with default values (zero) */
184
 
PJ_DEF(void) pjmedia_snd_port_param_default(pjmedia_snd_port_param *prm)
185
 
{
186
 
    pj_bzero(prm, sizeof(*prm));
187
 
}
188
 
 
189
 
/*
190
 
 * Start the sound stream.
191
 
 * This may be called even when the sound stream has already been started.
192
 
 */
193
 
static pj_status_t start_sound_device( pj_pool_t *pool,
194
 
                                       pjmedia_snd_port *snd_port )
195
 
{
196
 
    pjmedia_aud_rec_cb snd_rec_cb;
197
 
    pjmedia_aud_play_cb snd_play_cb;
198
 
    pjmedia_aud_param param_copy;
199
 
    pj_status_t status;
200
 
 
201
 
    /* Check if sound has been started. */
202
 
    if (snd_port->aud_stream != NULL)
203
 
        return PJ_SUCCESS;
204
 
 
205
 
    PJ_ASSERT_RETURN(snd_port->dir == PJMEDIA_DIR_CAPTURE ||
206
 
                     snd_port->dir == PJMEDIA_DIR_PLAYBACK ||
207
 
                     snd_port->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK,
208
 
                     PJ_EBUG);
209
 
 
210
 
    /* Get device caps */
211
 
    if (snd_port->aud_param.dir & PJMEDIA_DIR_CAPTURE) {
212
 
        pjmedia_aud_dev_info dev_info;
213
 
 
214
 
        status = pjmedia_aud_dev_get_info(snd_port->aud_param.rec_id, 
215
 
                                          &dev_info);
216
 
        if (status != PJ_SUCCESS)
217
 
            return status;
218
 
 
219
 
        snd_port->aud_caps = dev_info.caps;
220
 
    } else {
221
 
        snd_port->aud_caps = 0;
222
 
    }
223
 
 
224
 
    /* Process EC settings */
225
 
    pj_memcpy(&param_copy, &snd_port->aud_param, sizeof(param_copy));
226
 
    if (param_copy.flags & PJMEDIA_AUD_DEV_CAP_EC) {
227
 
        /* EC is wanted */
228
 
        if ((snd_port->prm_ec_options & PJMEDIA_ECHO_USE_SW_ECHO) == 0 &&
229
 
            snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC)
230
 
        {
231
 
            /* Device supports EC */
232
 
            /* Nothing to do */
233
 
        } else {
234
 
            /* Application wants to use software EC or device
235
 
             * doesn't support EC, remove EC settings from
236
 
             * device parameters
237
 
             */
238
 
            param_copy.flags &= ~(PJMEDIA_AUD_DEV_CAP_EC |
239
 
                                  PJMEDIA_AUD_DEV_CAP_EC_TAIL);
240
 
        }
241
 
    }
242
 
 
243
 
    /* Use different callback if format is not PCM */
244
 
    if (snd_port->aud_param.ext_fmt.id == PJMEDIA_FORMAT_L16) {
245
 
        snd_rec_cb = &rec_cb;
246
 
        snd_play_cb = &play_cb;
247
 
    } else {
248
 
        snd_rec_cb = &rec_cb_ext;
249
 
        snd_play_cb = &play_cb_ext;
250
 
    }
251
 
 
252
 
    /* Open the device */
253
 
    status = pjmedia_aud_stream_create(&param_copy,
254
 
                                       snd_rec_cb,
255
 
                                       snd_play_cb,
256
 
                                       snd_port,
257
 
                                       &snd_port->aud_stream);
258
 
 
259
 
    if (status != PJ_SUCCESS)
260
 
        return status;
261
 
 
262
 
    /* Inactivity limit before EC is suspended. */
263
 
    snd_port->ec_suspend_limit = AEC_SUSPEND_LIMIT *
264
 
                                 (snd_port->clock_rate / 
265
 
                                  snd_port->samples_per_frame);
266
 
 
267
 
    /* Create software EC if parameter specifies EC and
268
 
     * (app specifically requests software EC or device
269
 
     * doesn't support EC). Only do this if the format is PCM!
270
 
     */
271
 
    if ((snd_port->aud_param.flags & PJMEDIA_AUD_DEV_CAP_EC) &&
272
 
        ((snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC)==0 ||
273
 
         (snd_port->prm_ec_options & PJMEDIA_ECHO_USE_SW_ECHO) != 0) &&
274
 
        param_copy.ext_fmt.id == PJMEDIA_FORMAT_PCM)
275
 
    {
276
 
        if ((snd_port->aud_param.flags & PJMEDIA_AUD_DEV_CAP_EC_TAIL)==0) {
277
 
            snd_port->aud_param.flags |= PJMEDIA_AUD_DEV_CAP_EC_TAIL;
278
 
            snd_port->aud_param.ec_tail_ms = AEC_TAIL;
279
 
            PJ_LOG(4,(THIS_FILE, "AEC tail is set to default %u ms",
280
 
                                 snd_port->aud_param.ec_tail_ms));
281
 
        }
282
 
            
283
 
        status = pjmedia_snd_port_set_ec(snd_port, pool, 
284
 
                                         snd_port->aud_param.ec_tail_ms,
285
 
                                         snd_port->prm_ec_options);
286
 
        if (status != PJ_SUCCESS) {
287
 
            pjmedia_aud_stream_destroy(snd_port->aud_stream);
288
 
            snd_port->aud_stream = NULL;
289
 
            return status;
290
 
        }
291
 
    }
292
 
 
293
 
    /* Start sound stream. */
294
 
    if (!(snd_port->options & PJMEDIA_SND_PORT_NO_AUTO_START)) {
295
 
        status = pjmedia_aud_stream_start(snd_port->aud_stream);
296
 
    }
297
 
    if (status != PJ_SUCCESS) {
298
 
        pjmedia_aud_stream_destroy(snd_port->aud_stream);
299
 
        snd_port->aud_stream = NULL;
300
 
        return status;
301
 
    }
302
 
 
303
 
    return PJ_SUCCESS;
304
 
}
305
 
 
306
 
 
307
 
/*
308
 
 * Stop the sound device.
309
 
 * This may be called even when there's no sound device in the port.
310
 
 */
311
 
static pj_status_t stop_sound_device( pjmedia_snd_port *snd_port )
312
 
{
313
 
    /* Check if we have sound stream device. */
314
 
    if (snd_port->aud_stream) {
315
 
        pjmedia_aud_stream_stop(snd_port->aud_stream);
316
 
        pjmedia_aud_stream_destroy(snd_port->aud_stream);
317
 
        snd_port->aud_stream = NULL;
318
 
    }
319
 
 
320
 
    /* Destroy AEC */
321
 
    if (snd_port->ec_state) {
322
 
        pjmedia_echo_destroy(snd_port->ec_state);
323
 
        snd_port->ec_state = NULL;
324
 
    }
325
 
 
326
 
    return PJ_SUCCESS;
327
 
}
328
 
 
329
 
 
330
 
/*
331
 
 * Create bidirectional port.
332
 
 */
333
 
PJ_DEF(pj_status_t) pjmedia_snd_port_create( pj_pool_t *pool,
334
 
                                             int rec_id,
335
 
                                             int play_id,
336
 
                                             unsigned clock_rate,
337
 
                                             unsigned channel_count,
338
 
                                             unsigned samples_per_frame,
339
 
                                             unsigned bits_per_sample,
340
 
                                             unsigned options,
341
 
                                             pjmedia_snd_port **p_port)
342
 
{
343
 
    pjmedia_snd_port_param param;
344
 
    pj_status_t status;
345
 
 
346
 
    pjmedia_snd_port_param_default(&param);
347
 
 
348
 
    status = pjmedia_aud_dev_default_param(rec_id, &param.base);
349
 
    if (status != PJ_SUCCESS)
350
 
        return status;
351
 
 
352
 
    param.base.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
353
 
    param.base.rec_id = rec_id;
354
 
    param.base.play_id = play_id;
355
 
    param.base.clock_rate = clock_rate;
356
 
    param.base.channel_count = channel_count;
357
 
    param.base.samples_per_frame = samples_per_frame;
358
 
    param.base.bits_per_sample = bits_per_sample;
359
 
    param.options = options;
360
 
    param.ec_options = 0;
361
 
 
362
 
    return pjmedia_snd_port_create2(pool, &param, p_port);
363
 
}
364
 
 
365
 
/*
366
 
 * Create sound recorder AEC.
367
 
 */
368
 
PJ_DEF(pj_status_t) pjmedia_snd_port_create_rec( pj_pool_t *pool,
369
 
                                                 int dev_id,
370
 
                                                 unsigned clock_rate,
371
 
                                                 unsigned channel_count,
372
 
                                                 unsigned samples_per_frame,
373
 
                                                 unsigned bits_per_sample,
374
 
                                                 unsigned options,
375
 
                                                 pjmedia_snd_port **p_port)
376
 
{
377
 
    pjmedia_snd_port_param param;
378
 
    pj_status_t status;
379
 
 
380
 
    pjmedia_snd_port_param_default(&param);
381
 
 
382
 
    status = pjmedia_aud_dev_default_param(dev_id, &param.base);
383
 
    if (status != PJ_SUCCESS)
384
 
        return status;
385
 
 
386
 
    param.base.dir = PJMEDIA_DIR_CAPTURE;
387
 
    param.base.rec_id = dev_id;
388
 
    param.base.clock_rate = clock_rate;
389
 
    param.base.channel_count = channel_count;
390
 
    param.base.samples_per_frame = samples_per_frame;
391
 
    param.base.bits_per_sample = bits_per_sample;
392
 
    param.options = options;
393
 
    param.ec_options = 0;
394
 
 
395
 
    return pjmedia_snd_port_create2(pool, &param, p_port);
396
 
}
397
 
 
398
 
 
399
 
/*
400
 
 * Create sound player port.
401
 
 */
402
 
PJ_DEF(pj_status_t) pjmedia_snd_port_create_player( pj_pool_t *pool,
403
 
                                                    int dev_id,
404
 
                                                    unsigned clock_rate,
405
 
                                                    unsigned channel_count,
406
 
                                                    unsigned samples_per_frame,
407
 
                                                    unsigned bits_per_sample,
408
 
                                                    unsigned options,
409
 
                                                    pjmedia_snd_port **p_port)
410
 
{
411
 
    pjmedia_snd_port_param param;
412
 
    pj_status_t status;
413
 
 
414
 
    pjmedia_snd_port_param_default(&param);
415
 
 
416
 
    status = pjmedia_aud_dev_default_param(dev_id, &param.base);
417
 
    if (status != PJ_SUCCESS)
418
 
        return status;
419
 
 
420
 
    param.base.dir = PJMEDIA_DIR_PLAYBACK;
421
 
    param.base.play_id = dev_id;
422
 
    param.base.clock_rate = clock_rate;
423
 
    param.base.channel_count = channel_count;
424
 
    param.base.samples_per_frame = samples_per_frame;
425
 
    param.base.bits_per_sample = bits_per_sample;
426
 
    param.options = options;
427
 
    param.ec_options = 0;
428
 
 
429
 
    return pjmedia_snd_port_create2(pool, &param, p_port);
430
 
}
431
 
 
432
 
 
433
 
/*
434
 
 * Create sound port.
435
 
 */
436
 
PJ_DEF(pj_status_t) pjmedia_snd_port_create2(pj_pool_t *pool,
437
 
                                             const pjmedia_snd_port_param *prm,
438
 
                                             pjmedia_snd_port **p_port)
439
 
{
440
 
    pjmedia_snd_port *snd_port;
441
 
    pj_status_t status;
442
 
 
443
 
    PJ_ASSERT_RETURN(pool && prm && p_port, PJ_EINVAL);
444
 
 
445
 
    snd_port = PJ_POOL_ZALLOC_T(pool, pjmedia_snd_port);
446
 
    PJ_ASSERT_RETURN(snd_port, PJ_ENOMEM);
447
 
 
448
 
    snd_port->dir = prm->base.dir;
449
 
    snd_port->rec_id = prm->base.rec_id;
450
 
    snd_port->play_id = prm->base.play_id;
451
 
    snd_port->clock_rate = prm->base.clock_rate;
452
 
    snd_port->channel_count = prm->base.channel_count;
453
 
    snd_port->samples_per_frame = prm->base.samples_per_frame;
454
 
    snd_port->bits_per_sample = prm->base.bits_per_sample;
455
 
    pj_memcpy(&snd_port->aud_param, &prm->base, sizeof(snd_port->aud_param));
456
 
    snd_port->options = prm->options;
457
 
    snd_port->prm_ec_options = prm->ec_options;
458
 
    
459
 
    /* Start sound device immediately.
460
 
     * If there's no port connected, the sound callback will return
461
 
     * empty signal.
462
 
     */
463
 
    status = start_sound_device( pool, snd_port );
464
 
    if (status != PJ_SUCCESS) {
465
 
        pjmedia_snd_port_destroy(snd_port);
466
 
        return status;
467
 
    }
468
 
 
469
 
    *p_port = snd_port;
470
 
    return PJ_SUCCESS;
471
 
}
472
 
 
473
 
 
474
 
/*
475
 
 * Destroy port (also destroys the sound device).
476
 
 */
477
 
PJ_DEF(pj_status_t) pjmedia_snd_port_destroy(pjmedia_snd_port *snd_port)
478
 
{
479
 
    PJ_ASSERT_RETURN(snd_port, PJ_EINVAL);
480
 
 
481
 
    return stop_sound_device(snd_port);
482
 
}
483
 
 
484
 
 
485
 
/*
486
 
 * Retrieve the sound stream associated by this sound device port.
487
 
 */
488
 
PJ_DEF(pjmedia_aud_stream*) pjmedia_snd_port_get_snd_stream(
489
 
                                                pjmedia_snd_port *snd_port)
490
 
{
491
 
    PJ_ASSERT_RETURN(snd_port, NULL);
492
 
    return snd_port->aud_stream;
493
 
}
494
 
 
495
 
 
496
 
/*
497
 
 * Change EC settings.
498
 
 */
499
 
PJ_DEF(pj_status_t) pjmedia_snd_port_set_ec( pjmedia_snd_port *snd_port,
500
 
                                             pj_pool_t *pool,
501
 
                                             unsigned tail_ms,
502
 
                                             unsigned options)
503
 
{
504
 
    pjmedia_aud_param prm;
505
 
    pj_status_t status;
506
 
 
507
 
    /* Sound must be opened in full-duplex mode */
508
 
    PJ_ASSERT_RETURN(snd_port && 
509
 
                     snd_port->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK,
510
 
                     PJ_EINVALIDOP);
511
 
 
512
 
    /* Determine whether we use device or software EC */
513
 
    if ((snd_port->prm_ec_options & PJMEDIA_ECHO_USE_SW_ECHO) == 0 &&
514
 
        snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC)
515
 
    {
516
 
        /* We use device EC */
517
 
        pj_bool_t ec_enabled;
518
 
 
519
 
        /* Query EC status */
520
 
        status = pjmedia_aud_stream_get_cap(snd_port->aud_stream,
521
 
                                            PJMEDIA_AUD_DEV_CAP_EC,
522
 
                                            &ec_enabled);
523
 
        if (status != PJ_SUCCESS)
524
 
            return status;
525
 
 
526
 
        if (tail_ms != 0) {
527
 
            /* Change EC setting */
528
 
 
529
 
            if (!ec_enabled) {
530
 
                /* Enable EC first */
531
 
                pj_bool_t value = PJ_TRUE;
532
 
                status = pjmedia_aud_stream_set_cap(snd_port->aud_stream, 
533
 
                                                    PJMEDIA_AUD_DEV_CAP_EC,
534
 
                                                    &value);
535
 
                if (status != PJ_SUCCESS)
536
 
                    return status;
537
 
            }
538
 
 
539
 
            if ((snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC_TAIL)==0) {
540
 
                /* Device does not support setting EC tail */
541
 
                return PJMEDIA_EAUD_INVCAP;
542
 
            }
543
 
 
544
 
            return pjmedia_aud_stream_set_cap(snd_port->aud_stream,
545
 
                                              PJMEDIA_AUD_DEV_CAP_EC_TAIL,
546
 
                                              &tail_ms);
547
 
 
548
 
        } else if (ec_enabled) {
549
 
            /* Disable EC */
550
 
            pj_bool_t value = PJ_FALSE;
551
 
            return pjmedia_aud_stream_set_cap(snd_port->aud_stream, 
552
 
                                              PJMEDIA_AUD_DEV_CAP_EC,
553
 
                                              &value);
554
 
        } else {
555
 
            /* Request to disable EC but EC has been disabled */
556
 
            /* Do nothing */
557
 
            return PJ_SUCCESS;
558
 
        }
559
 
 
560
 
    } else {
561
 
        /* We use software EC */
562
 
 
563
 
        /* Check if there is change in parameters */
564
 
        if (tail_ms==snd_port->ec_tail_len && options==snd_port->ec_options) {
565
 
            PJ_LOG(5,(THIS_FILE, "pjmedia_snd_port_set_ec() ignored, no "
566
 
                                 "change in settings"));
567
 
            return PJ_SUCCESS;
568
 
        }
569
 
 
570
 
        status = pjmedia_aud_stream_get_param(snd_port->aud_stream, &prm);
571
 
        if (status != PJ_SUCCESS)
572
 
            return status;
573
 
 
574
 
        /* Audio stream must be in PCM format */
575
 
        PJ_ASSERT_RETURN(prm.ext_fmt.id == PJMEDIA_FORMAT_PCM,
576
 
                         PJ_EINVALIDOP);
577
 
 
578
 
        /* Destroy AEC */
579
 
        if (snd_port->ec_state) {
580
 
            pjmedia_echo_destroy(snd_port->ec_state);
581
 
            snd_port->ec_state = NULL;
582
 
        }
583
 
 
584
 
        if (tail_ms != 0) {
585
 
            unsigned delay_ms;
586
 
 
587
 
            //No need to add input latency in the latency calculation,
588
 
            //since actual input latency should be zero.
589
 
            //delay_ms = (si.rec_latency + si.play_latency) * 1000 /
590
 
            //     snd_port->clock_rate;
591
 
            /* Set EC latency to 3/4 of output latency to reduce the
592
 
             * possibility of missing/late reference frame.
593
 
             */
594
 
            delay_ms = prm.output_latency_ms * 3/4;
595
 
            status = pjmedia_echo_create2(pool, snd_port->clock_rate, 
596
 
                                          snd_port->channel_count,
597
 
                                          snd_port->samples_per_frame, 
598
 
                                          tail_ms, delay_ms,
599
 
                                          options, &snd_port->ec_state);
600
 
            if (status != PJ_SUCCESS)
601
 
                snd_port->ec_state = NULL;
602
 
            else
603
 
                snd_port->ec_suspended = PJ_FALSE;
604
 
        } else {
605
 
            PJ_LOG(4,(THIS_FILE, "Echo canceller is now disabled in the "
606
 
                                 "sound port"));
607
 
            status = PJ_SUCCESS;
608
 
        }
609
 
 
610
 
        snd_port->ec_options = options;
611
 
        snd_port->ec_tail_len = tail_ms;
612
 
    }
613
 
 
614
 
    return status;
615
 
}
616
 
 
617
 
 
618
 
/* Get AEC tail length */
619
 
PJ_DEF(pj_status_t) pjmedia_snd_port_get_ec_tail( pjmedia_snd_port *snd_port,
620
 
                                                  unsigned *p_length)
621
 
{
622
 
    PJ_ASSERT_RETURN(snd_port && p_length, PJ_EINVAL);
623
 
 
624
 
    /* Determine whether we use device or software EC */
625
 
    if (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC) {
626
 
        /* We use device EC */
627
 
        pj_bool_t ec_enabled;
628
 
        pj_status_t status;
629
 
 
630
 
        /* Query EC status */
631
 
        status = pjmedia_aud_stream_get_cap(snd_port->aud_stream,
632
 
                                            PJMEDIA_AUD_DEV_CAP_EC,
633
 
                                            &ec_enabled);
634
 
        if (status != PJ_SUCCESS)
635
 
            return status;
636
 
 
637
 
        if (!ec_enabled) {
638
 
            *p_length = 0;
639
 
        } else if (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC_TAIL) {
640
 
            /* Get device EC tail */
641
 
            status = pjmedia_aud_stream_get_cap(snd_port->aud_stream,
642
 
                                                PJMEDIA_AUD_DEV_CAP_EC_TAIL,
643
 
                                                p_length);
644
 
            if (status != PJ_SUCCESS)
645
 
                return status;
646
 
        } else {
647
 
            /* Just use default */
648
 
            *p_length = AEC_TAIL;
649
 
        }
650
 
 
651
 
    } else {
652
 
        /* We use software EC */
653
 
        *p_length =  snd_port->ec_state ? snd_port->ec_tail_len : 0;
654
 
    }
655
 
    return PJ_SUCCESS;
656
 
}
657
 
 
658
 
 
659
 
/*
660
 
 * Connect a port.
661
 
 */
662
 
PJ_DEF(pj_status_t) pjmedia_snd_port_connect( pjmedia_snd_port *snd_port,
663
 
                                              pjmedia_port *port)
664
 
{
665
 
    pjmedia_port_info *pinfo;
666
 
 
667
 
    PJ_ASSERT_RETURN(snd_port && port, PJ_EINVAL);
668
 
 
669
 
    /* Check that port has the same configuration as the sound device
670
 
     * port.
671
 
     */
672
 
    pinfo = &port->info;
673
 
    if (pinfo->clock_rate != snd_port->clock_rate)
674
 
        return PJMEDIA_ENCCLOCKRATE;
675
 
 
676
 
    if (pinfo->samples_per_frame != snd_port->samples_per_frame)
677
 
        return PJMEDIA_ENCSAMPLESPFRAME;
678
 
 
679
 
    if (pinfo->channel_count != snd_port->channel_count)
680
 
        return PJMEDIA_ENCCHANNEL;
681
 
 
682
 
    if (pinfo->bits_per_sample != snd_port->bits_per_sample)
683
 
        return PJMEDIA_ENCBITS;
684
 
 
685
 
    /* Port is okay. */
686
 
    snd_port->port = port;
687
 
    return PJ_SUCCESS;
688
 
}
689
 
 
690
 
 
691
 
/*
692
 
 * Get the connected port.
693
 
 */
694
 
PJ_DEF(pjmedia_port*) pjmedia_snd_port_get_port(pjmedia_snd_port *snd_port)
695
 
{
696
 
    PJ_ASSERT_RETURN(snd_port, NULL);
697
 
    return snd_port->port;
698
 
}
699
 
 
700
 
 
701
 
/*
702
 
 * Disconnect port.
703
 
 */
704
 
PJ_DEF(pj_status_t) pjmedia_snd_port_disconnect(pjmedia_snd_port *snd_port)
705
 
{
706
 
    PJ_ASSERT_RETURN(snd_port, PJ_EINVAL);
707
 
 
708
 
    snd_port->port = NULL;
709
 
 
710
 
    return PJ_SUCCESS;
711
 
}
712
 
 
713