]> sjero.net Git - linphone/blob - coreapi/ec-calibrator.c
Merge branch 'master' of git.savannah.nongnu.org:/srv/git/linphone
[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
27
28
29
30 static void ecc_init_filters(EcCalibrator *ecc){
31         ecc->ticker=ms_ticker_new();
32
33         ecc->sndread=ms_snd_card_create_reader(ecc->play_card);
34         ecc->det=ms_filter_new(MS_TONE_DETECTOR_ID);
35         ecc->rec=ms_filter_new(MS_FILE_REC_ID);
36
37         ms_filter_link(ecc->sndread,0,ecc->det,0);
38         ms_filter_link(ecc->det,0,ecc->rec,0);
39
40         ecc->play=ms_filter_new(MS_FILE_PLAYER_ID);
41         ecc->gen=ms_filter_new(MS_DTMF_GEN_ID);
42         ecc->sndwrite=ms_snd_card_create_writer(ecc->capt_card);
43
44         ms_filter_link(ecc->play,0,ecc->gen,0);
45         ms_filter_link(ecc->gen,0,ecc->sndwrite,0);
46
47         ms_ticker_attach(ecc->ticker,ecc->play);
48         ms_ticker_attach(ecc->ticker,ecc->sndread);
49 }
50
51 static void ecc_deinit_filters(EcCalibrator *ecc){
52         ms_ticker_detach(ecc->ticker,ecc->play);
53         ms_ticker_detach(ecc->ticker,ecc->sndread);
54
55         ms_filter_unlink(ecc->play,0,ecc->gen,0);
56         ms_filter_unlink(ecc->gen,0,ecc->sndwrite,0);
57
58         ms_filter_unlink(ecc->sndread,0,ecc->det,0);
59         ms_filter_unlink(ecc->det,0,ecc->rec,0);
60
61         ms_filter_destroy(ecc->sndread);
62         ms_filter_destroy(ecc->det);
63         ms_filter_destroy(ecc->rec);
64         ms_filter_destroy(ecc->play);
65         ms_filter_destroy(ecc->gen);
66         ms_filter_destroy(ecc->sndwrite);
67
68         ms_ticker_destroy(ecc->ticker);
69 }
70
71 static void on_tone_sent(void *data, MSFilter *f, unsigned int event_id, void *arg){
72         MSDtmfGenEvent *ev=(MSDtmfGenEvent*)arg;
73         EcCalibrator *ecc=(EcCalibrator*)data;
74         ecc->sent_count++;
75         ecc->acc-=ev->tone_start_time;
76         ms_message("Sent tone at %u",(unsigned int)ev->tone_start_time);
77 }
78
79 static void on_tone_received(void *data, MSFilter *f, unsigned int event_id, void *arg){
80         MSToneDetectorEvent *ev=(MSToneDetectorEvent*)arg;
81         EcCalibrator *ecc=(EcCalibrator*)data;
82         ecc->recv_count++;
83         ecc->acc+=ev->tone_start_time;
84         ms_message("Received tone at %u",(unsigned int)ev->tone_start_time);
85 }
86
87 static void ecc_play_tones(EcCalibrator *ecc){
88         MSDtmfGenCustomTone tone;
89         MSToneDetectorDef expected_tone;
90
91         
92         ms_filter_set_notify_callback(ecc->det,on_tone_received,ecc);
93
94         expected_tone.frequency=2000;
95         expected_tone.min_duration=40;
96         expected_tone.min_amplitude=0.02;
97
98         ms_filter_call_method (ecc->det,MS_TONE_DETECTOR_ADD_SCAN,&expected_tone);
99         
100         tone.frequency=1000;
101         tone.duration=1000;
102         tone.amplitude=1.0;
103
104         /*play an initial tone to startup the audio playback/capture*/
105         ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
106         sleep(2);
107
108         ms_filter_set_notify_callback(ecc->gen,on_tone_sent,ecc);
109         tone.frequency=2000;
110         tone.duration=100;
111
112         ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
113         sleep(1);
114         ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
115         sleep(1);
116         ms_filter_call_method(ecc->gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
117         sleep(1);
118
119         if (ecc->sent_count==3 && ecc->recv_count==3){
120                 int delay=ecc->acc/3;
121                 if (delay<0){
122                         ms_error("Quite surprising calibration result, delay=%i",delay);
123                         ecc->status=LinphoneEcCalibratorFailed;
124                 }else{ms_message("Echo calibration estimated delay to be %i ms",delay);
125                         ecc->delay=delay;
126                         ecc->status=LinphoneEcCalibratorDone;
127                 }
128         }else{
129                 ms_error("Echo calibration failed, tones received = %i",ecc->recv_count);
130                 ecc->status=LinphoneEcCalibratorFailed;
131         }
132         
133 }
134
135 static void  * ecc_thread(void *p){
136         EcCalibrator *ecc=(EcCalibrator*)p;
137         
138         ecc_init_filters(ecc);
139         ecc_play_tones(ecc);
140         ecc_deinit_filters(ecc);
141         return NULL;
142 }
143
144 EcCalibrator * ec_calibrator_new(MSSndCard *play_card, MSSndCard *capt_card, LinphoneEcCalibrationCallback cb, void *cb_data ){
145         EcCalibrator *ecc=ms_new0(EcCalibrator,1);
146
147         ecc->cb=cb;
148         ecc->cb_data=cb_data;
149         ecc->capt_card=capt_card;
150         ecc->play_card=play_card;
151         ms_thread_create(&ecc->thread,NULL,ecc_thread,ecc);
152         return ecc;
153 }
154
155 LinphoneEcCalibratorStatus ec_calibrator_get_status(EcCalibrator *ecc){
156         return ecc->status;
157 }
158
159 void ec_calibrator_destroy(EcCalibrator *ecc){
160         ms_thread_join(ecc->thread,NULL);
161         ms_free(ecc);
162 }
163
164 int linphone_core_start_echo_calibration(LinphoneCore *lc, LinphoneEcCalibrationCallback cb, void *cb_data){
165         if (lc->ecc!=NULL){
166                 ms_error("Echo calibration is still on going !");
167                 return -1;
168         }
169         lc->ecc=ec_calibrator_new(lc->sound_conf.play_sndcard,lc->sound_conf.capt_sndcard,cb,cb_data);
170         return 0;
171 }