echo-tester.c 3.32 KB
Newer Older
Erwan Croze's avatar
Erwan Croze committed
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
/*
linphone
Copyright (C) 2016 Belledonne Communications SARL
Author: Erwan CROZE (erwan.croze@belledonne-communications.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "private.h"

#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/mssndcard.h"
#include "mediastreamer2/msticker.h"
#include <ortp/ortp.h>

EchoTester* ec_tester_new(MSFactory *factory, MSSndCard *capture_card, MSSndCard *playback_card, unsigned int rate) {
    EchoTester *ect = ms_new0(EchoTester,1);
    ect->factory = factory;
    ect->capture_card = capture_card;
    ect->playback_card = playback_card;
    ect->rate = rate;

    return ect;
}

static void ect_init_filters(EchoTester *ect) {
    unsigned int rate;
    int channels = 1;
    int ect_channels = 1;
42 43
    MSTickerParams params;
	memset(&params, 0, sizeof(params));
Erwan Croze's avatar
Erwan Croze committed
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
	params.name="Echo tester";
	params.prio=MS_TICKER_PRIO_HIGH;
	ect->ticker=ms_ticker_new_with_params(&params);

    ect->in = ms_snd_card_create_reader(ect->capture_card);
    ect->out = ms_snd_card_create_writer(ect->playback_card);

    ms_filter_call_method(ect->in,MS_FILTER_SET_SAMPLE_RATE,&ect->rate);
    ms_filter_call_method(ect->in,MS_FILTER_GET_SAMPLE_RATE,&rate);
    ms_filter_call_method(ect->in,MS_FILTER_SET_NCHANNELS,&ect_channels);
    ms_filter_call_method(ect->in,MS_FILTER_GET_NCHANNELS,&channels);

    ms_filter_call_method(ect->out,MS_FILTER_SET_SAMPLE_RATE,&ect->rate);
    ms_filter_call_method(ect->out,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&rate);
    ms_filter_call_method(ect->out,MS_FILTER_SET_NCHANNELS,&ect_channels);
    ms_filter_call_method(ect->out,MS_FILTER_SET_OUTPUT_NCHANNELS,&channels);

    ms_filter_link(ect->in,0,ect->out,0);

    ms_ticker_attach(ect->ticker,ect->in);
    ms_ticker_attach(ect->ticker,ect->out);
}

static void ect_uninit_filters(EchoTester *ect) {
    ms_ticker_detach(ect->ticker,ect->in);
    ms_ticker_detach(ect->ticker,ect->out);

    ms_filter_unlink(ect->in,0,ect->out,0);

    ms_filter_destroy(ect->in);
    ms_filter_destroy(ect->out);

    ms_ticker_destroy(ect->ticker);
}

void ec_tester_destroy(EchoTester *ect) {
    ms_free(ect);
}

83
LinphoneStatus linphone_core_start_echo_tester(LinphoneCore *lc, unsigned int rate) {
Erwan Croze's avatar
Erwan Croze committed
84 85 86 87 88 89 90 91 92 93 94
    if (lc->ect != NULL) {
        ms_error("Echo tester is still on going !");
        return -1;
    }
    lc->ect = ec_tester_new(lc->factory, lc->sound_conf.capt_sndcard
        ,lc->sound_conf.play_sndcard, rate);
    ect_init_filters(lc->ect);

    return 1;
}

95
LinphoneStatus linphone_core_stop_echo_tester(LinphoneCore *lc) {
Erwan Croze's avatar
Erwan Croze committed
96 97 98 99 100 101 102 103 104
    if (lc->ect == NULL) {
        ms_error("Echo tester is not running !");
        return -1;
    }
    ect_uninit_filters(lc->ect);

    ec_tester_destroy(lc->ect);
    lc->ect = NULL;
    return 1;
105
}