]> sjero.net Git - linphone/blob - coreapi/ec-calibrator.c
Set UTC time in received chat messages.
[linphone] / coreapi / ec-calibrator.c
1 /*
2 linphone
3 Copyright (C) 2011 Belledonne Communications SARL
4 Author: Simon MORLAT (simon.morlat@linphone.org)
5
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.
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
21 #include "private.h"
22
23 #include "mediastreamer2/mstonedetector.h"
24 #include "mediastreamer2/dtmfgen.h"
25
26 #include "lpconfig.h"
27
28
29
30 static void ecc_init_filters(EcCalibrator *ecc){
31         unsigned int rate;
32         MSTickerParams params={0};
33         params.name="Echo calibrator";
34         params.prio=MS_TICKER_PRIO_HIGH;
35         ecc->ticker=ms_ticker_new_with_params(&params);
36
37         ecc->sndread=ms_snd_card_create_reader(ecc->play_card);
38         ms_filter_call_method(ecc->sndread,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
39         ms_filter_call_method(ecc->sndread,MS_FILTER_GET_SAMPLE_RATE,&rate);
40         ecc->read_resampler=ms_filter_new(MS_RESAMPLE_ID);
41         ms_filter_call_method(ecc->read_resampler,MS_FILTER_SET_SAMPLE_RATE,&rate);
42         ms_filter_call_method(ecc->read_resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&ecc->rate);
43         
44         
45         ecc->det=ms_filter_new(MS_TONE_DETECTOR_ID);
46         ms_filter_call_method(ecc->det,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
47         ecc->rec=ms_filter_new(MS_FILE_REC_ID);
48
49         ms_filter_link(ecc->sndread,0,ecc->read_resampler,0);
50         ms_filter_link(ecc->read_resampler,0,ecc->det,0);
51         ms_filter_link(ecc->det,0,ecc->rec,0);
52
53         ecc->play=ms_filter_new(MS_FILE_PLAYER_ID);
54         ecc->gen=ms_filter_new(MS_DTMF_GEN_ID);
55         ms_filter_call_method(ecc->gen,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
56         ecc->write_resampler=ms_filter_new(MS_RESAMPLE_ID);
57         ecc->sndwrite=ms_snd_card_create_writer(ecc->capt_card);
58         
59         ms_filter_call_method(ecc->sndwrite,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
60         ms_filter_call_method(ecc->sndwrite,MS_FILTER_GET_SAMPLE_RATE,&rate);
61         ms_filter_call_method(ecc->write_resampler,MS_FILTER_SET_SAMPLE_RATE,&ecc->rate);
62         ms_filter_call_method(ecc->write_resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&rate);
63
64         ms_filter_link(ecc->play,0,ecc->gen,0);
65         ms_filter_link(ecc->gen,0,ecc->write_resampler,0);
66         ms_filter_link(ecc->write_resampler,0,ecc->sndwrite,0);
67
68         ms_ticker_attach(ecc->ticker,ecc->sndread);
69         ms_ticker_attach(ecc->ticker,ecc->play);
70         
71 }
72
73 static void ecc_deinit_filters(EcCalibrator *ecc){
74         ms_ticker_detach(ecc->ticker,ecc->sndread);
75         ms_ticker_detach(ecc->ticker,ecc->play);
76
77         ms_filter_unlink(ecc->play,0,ecc->gen,0);
78         ms_filter_unlink(ecc->gen,0,ecc->write_resampler,0);
79         ms_filter_unlink(ecc->write_resampler,0,ecc->sndwrite,0);
80
81         ms_filter_unlink(ecc->sndread,0,ecc->read_resampler,0);
82         ms_filter_unlink(ecc->read_resampler,0,ecc->det,0);
83         ms_filter_unlink(ecc->det,0,ecc->rec,0);
84
85         ms_filter_destroy(ecc->sndread);
86         ms_filter_destroy(ecc->det);
87         ms_filter_destroy(ecc->rec);
88         ms_filter_destroy(ecc->play);
89         ms_filter_destroy(ecc->gen);
90         ms_filter_destroy(ecc->read_resampler);
91         ms_filter_destroy(ecc->write_resampler);
92         ms_filter_destroy(ecc->sndwrite);
93
94         ms_ticker_destroy(ecc->ticker);
95 }
96
97 static void on_tone_sent(void *data, MSFilter *f, unsigned int event_id, void *arg){
98         MSDtmfGenEvent *ev=(MSDtmfGenEvent*)arg;
99         EcCalibrator *ecc=(EcCalibrator*)data;
100         ecc->acc-=ev->tone_start_time;
101         ms_message("Sent tone at %u",(unsigned int)ev->tone_start_time);
102 }
103
104 static bool_t is_valid_tone(EcCalibrator *ecc, MSToneDetectorEvent *ev){
105         bool_t *toneflag=NULL;
106         if (strcmp(ev->tone_name,"freq1")==0){
107                 toneflag=&ecc->freq1;
108         }else if (strcmp(ev->tone_name,"freq2")==0){
109                 toneflag=&ecc->freq2;
110         }else if (strcmp(ev->tone_name,"freq3")==0){
111                 toneflag=&ecc->freq3;
112         }else{
113                 ms_error("Calibrator bug.");
114                 return FALSE;
115         }
116         if (*toneflag){
117                 ms_message("Duplicated tone event, ignored.");
118                 return FALSE;
119         }
120         *toneflag=TRUE;
121         return TRUE;
122 }
123
124 static void on_tone_received(void *data, MSFilter *f, unsigned int event_id, void *arg){
125         MSToneDetectorEvent *ev=(MSToneDetectorEvent*)arg;
126         EcCalibrator *ecc=(EcCalibrator*)data;
127         if (is_valid_tone(ecc,ev)){
128                 ecc->acc+=ev->tone_start_time;
129                 ms_message("Received tone at %u",(unsigned int)ev->tone_start_time);
130         }
131 }
132
133 static void ecc_play_tones(EcCalibrator *ecc){
134         MSDtmfGenCustomTone tone;
135         MSToneDetectorDef expected_tone;
136         
137         memset(&tone,0,sizeof(tone));
138         memset(&expected_tone,0,sizeof(expected_tone));
139
140         ms_filter_set_notify_callback(ecc->det,on_tone_received,ecc);
141
142         /* configure the tones to be scanned */
143         
144         strncpy(expected_tone.tone_name,"freq1",sizeof(expected_tone.tone_name));
145         expected_tone.frequency=2000;
146         expected_tone.min_duration=40;
147         expected_tone.min_amplitude=0.1;
148
149         ms_filter_call_method (ecc->det,MS_TONE_DETECTOR_ADD_SCAN,&expected_tone);
150         
151         strncpy(expected_tone.tone_name,"freq2",sizeof(expected_tone.tone_name));
152         expected_tone.frequency=2300;
153         expected_tone.min_duration=40;
154         expected_tone.min_amplitude=0.1;
155
156         ms_filter_call_method (ecc->det,MS_TONE_DETECTOR_ADD_SCAN,&expected_tone);
157         
158         strncpy(expected_tone.tone_name,"freq3",sizeof(expected_tone.tone_name));
159         expected_tone.frequency=2500;
160         expected_tone.min_duration=40;
161         expected_tone.min_amplitude=0.1;
162
163         ms_filter_call_method (ecc->det,MS_TONE_DETECTOR_ADD_SCAN,&expected_tone);
164         
165         /*play an initial tone to startup the audio playback/capture*/
166         
167         tone.frequencies[0]=140;
168         tone.duration=1000;
169         tone.amplitude=0.5;
170
171         ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
172         ms_sleep(2);
173
174         ms_filter_set_notify_callback(ecc->gen,on_tone_sent,ecc);
175         
176         /* play the three tones*/
177         
178         tone.frequencies[0]=2000;
179         tone.duration=100;
180         ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
181         ms_usleep(300000);
182         
183         tone.frequencies[0]=2300;
184         tone.duration=100;
185         ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
186         ms_usleep(300000);
187         
188         tone.frequencies[0]=2500;
189         tone.duration=100;
190         ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
191         ms_sleep(1);
192         
193         if (ecc->freq1 && ecc->freq2 && ecc->freq3) {
194                 int delay=ecc->acc/3;
195                 if (delay<0){
196                         ms_error("Quite surprising calibration result, delay=%i",delay);
197                         ecc->status=LinphoneEcCalibratorFailed;
198                 }else{
199                         ms_message("Echo calibration estimated delay to be %i ms",delay);
200                         ecc->delay=delay;
201                         ecc->status=LinphoneEcCalibratorDone;
202                 }
203         } else if ((ecc->freq1 || ecc->freq2 || ecc->freq3)==FALSE) {
204                         ms_message("Echo calibration succeeded, no echo has been detected");
205                         ecc->status = LinphoneEcCalibratorDoneNoEcho;
206         } else {
207                         ecc->status = LinphoneEcCalibratorFailed;
208         }
209
210         if (ecc->status == LinphoneEcCalibratorFailed) {
211                 ms_error("Echo calibration failed.");
212         }
213 }
214
215 static void  * ecc_thread(void *p){
216         EcCalibrator *ecc=(EcCalibrator*)p;
217         
218         ecc_init_filters(ecc);
219         ecc_play_tones(ecc);
220         ecc_deinit_filters(ecc);
221         ms_thread_exit(NULL);
222         return NULL;
223 }
224
225 EcCalibrator * ec_calibrator_new(MSSndCard *play_card, MSSndCard *capt_card, unsigned int rate, LinphoneEcCalibrationCallback cb, void *cb_data ){
226         EcCalibrator *ecc=ms_new0(EcCalibrator,1);
227
228         ecc->rate=rate;
229         ecc->cb=cb;
230         ecc->cb_data=cb_data;
231         ecc->capt_card=capt_card;
232         ecc->play_card=play_card;
233         ms_thread_create(&ecc->thread,NULL,ecc_thread,ecc);
234         return ecc;
235 }
236
237 LinphoneEcCalibratorStatus ec_calibrator_get_status(EcCalibrator *ecc){
238         return ecc->status;
239 }
240
241 void ec_calibrator_destroy(EcCalibrator *ecc){
242         ms_thread_join(ecc->thread,NULL);
243         ms_free(ecc);
244 }
245
246 int linphone_core_start_echo_calibration(LinphoneCore *lc, LinphoneEcCalibrationCallback cb, void *cb_data){
247         if (lc->ecc!=NULL){
248                 ms_error("Echo calibration is still on going !");
249                 return -1;
250         }
251         unsigned int rate = lp_config_get_int(lc->config,"sound","echo_cancellation_rate",8000);
252         lc->ecc=ec_calibrator_new(lc->sound_conf.play_sndcard,lc->sound_conf.capt_sndcard,rate,cb,cb_data);
253         return 0;
254 }
255