3 Copyright (C) 2011 Belledonne Communications SARL
4 Author: Simon MORLAT (simon.morlat@linphone.org)
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
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.
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.
23 #include "mediastreamer2/mstonedetector.h"
24 #include "mediastreamer2/dtmfgen.h"
30 static void ecc_init_filters(EcCalibrator *ecc){
32 ecc->ticker=ms_ticker_new();
34 ecc->sndread=ms_snd_card_create_reader(ecc->play_card);
35 ms_filter_call_method(ecc->sndread,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
36 ecc->det=ms_filter_new(MS_TONE_DETECTOR_ID);
37 ms_filter_call_method(ecc->det,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
38 ecc->rec=ms_filter_new(MS_FILE_REC_ID);
40 ms_filter_link(ecc->sndread,0,ecc->det,0);
41 ms_filter_link(ecc->det,0,ecc->rec,0);
43 ecc->play=ms_filter_new(MS_FILE_PLAYER_ID);
44 ecc->gen=ms_filter_new(MS_DTMF_GEN_ID);
45 ms_filter_call_method(ecc->gen,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
46 ecc->resampler=ms_filter_new(MS_RESAMPLE_ID);
47 ecc->sndwrite=ms_snd_card_create_writer(ecc->capt_card);
49 ms_filter_link(ecc->play,0,ecc->gen,0);
50 ms_filter_link(ecc->gen,0,ecc->resampler,0);
51 ms_filter_link(ecc->resampler,0,ecc->sndwrite,0);
53 ms_filter_call_method(ecc->sndwrite,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
54 ms_filter_call_method(ecc->sndwrite,MS_FILTER_GET_SAMPLE_RATE,&rate);
55 ms_filter_call_method(ecc->resampler,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
56 ms_filter_call_method(ecc->resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&rate);
58 ms_ticker_attach(ecc->ticker,ecc->play);
59 ms_ticker_attach(ecc->ticker,ecc->sndread);
62 static void ecc_deinit_filters(EcCalibrator *ecc){
63 ms_ticker_detach(ecc->ticker,ecc->play);
64 ms_ticker_detach(ecc->ticker,ecc->sndread);
66 ms_filter_unlink(ecc->play,0,ecc->gen,0);
67 ms_filter_unlink(ecc->gen,0,ecc->resampler,0);
68 ms_filter_unlink(ecc->resampler,0,ecc->sndwrite,0);
70 ms_filter_unlink(ecc->sndread,0,ecc->det,0);
71 ms_filter_unlink(ecc->det,0,ecc->rec,0);
73 ms_filter_destroy(ecc->sndread);
74 ms_filter_destroy(ecc->det);
75 ms_filter_destroy(ecc->rec);
76 ms_filter_destroy(ecc->play);
77 ms_filter_destroy(ecc->gen);
78 ms_filter_destroy(ecc->resampler);
79 ms_filter_destroy(ecc->sndwrite);
81 ms_ticker_destroy(ecc->ticker);
84 static void on_tone_sent(void *data, MSFilter *f, unsigned int event_id, void *arg){
85 MSDtmfGenEvent *ev=(MSDtmfGenEvent*)arg;
86 EcCalibrator *ecc=(EcCalibrator*)data;
88 ecc->acc-=ev->tone_start_time;
89 ms_message("Sent tone at %u",(unsigned int)ev->tone_start_time);
92 static void on_tone_received(void *data, MSFilter *f, unsigned int event_id, void *arg){
93 MSToneDetectorEvent *ev=(MSToneDetectorEvent*)arg;
94 EcCalibrator *ecc=(EcCalibrator*)data;
96 ecc->acc+=ev->tone_start_time;
97 ms_message("Received tone at %u",(unsigned int)ev->tone_start_time);
100 static void ecc_play_tones(EcCalibrator *ecc){
101 MSDtmfGenCustomTone tone;
102 MSToneDetectorDef expected_tone;
105 ms_filter_set_notify_callback(ecc->det,on_tone_received,ecc);
107 expected_tone.frequency=2000;
108 expected_tone.min_duration=40;
109 expected_tone.min_amplitude=0.02;
111 ms_filter_call_method (ecc->det,MS_TONE_DETECTOR_ADD_SCAN,&expected_tone);
117 /*play an initial tone to startup the audio playback/capture*/
118 ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
121 ms_filter_set_notify_callback(ecc->gen,on_tone_sent,ecc);
125 ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
127 ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
129 ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
132 if (ecc->sent_count==3 && ecc->recv_count==3){
133 int delay=ecc->acc/3;
135 ms_error("Quite surprising calibration result, delay=%i",delay);
136 ecc->status=LinphoneEcCalibratorFailed;
137 }else{ms_message("Echo calibration estimated delay to be %i ms",delay);
139 ecc->status=LinphoneEcCalibratorDone;
142 ms_error("Echo calibration failed, tones received = %i",ecc->recv_count);
143 ecc->status=LinphoneEcCalibratorFailed;
148 static void * ecc_thread(void *p){
149 EcCalibrator *ecc=(EcCalibrator*)p;
151 ecc_init_filters(ecc);
153 ecc_deinit_filters(ecc);
154 ms_thread_exit(NULL);
158 EcCalibrator * ec_calibrator_new(MSSndCard *play_card, MSSndCard *capt_card, unsigned int rate, LinphoneEcCalibrationCallback cb, void *cb_data ){
159 EcCalibrator *ecc=ms_new0(EcCalibrator,1);
163 ecc->cb_data=cb_data;
164 ecc->capt_card=capt_card;
165 ecc->play_card=play_card;
166 ms_thread_create(&ecc->thread,NULL,ecc_thread,ecc);
170 LinphoneEcCalibratorStatus ec_calibrator_get_status(EcCalibrator *ecc){
174 void ec_calibrator_destroy(EcCalibrator *ecc){
175 ms_thread_join(ecc->thread,NULL);
179 int linphone_core_start_echo_calibration(LinphoneCore *lc, LinphoneEcCalibrationCallback cb, void *cb_data){
181 ms_error("Echo calibration is still on going !");
184 unsigned int rate = lp_config_get_int(lc->config,"sound","echo_cancellation_rate",8000);
185 lc->ecc=ec_calibrator_new(lc->sound_conf.play_sndcard,lc->sound_conf.capt_sndcard,rate,cb,cb_data);