]> sjero.net Git - linphone/blob - coreapi/linphonecall.c
fbb4f02121eacfcec0caa133492245cb605d0847
[linphone] / coreapi / linphonecall.c
1
2 /*
3 linphone
4 Copyright (C) 2010  Belledonne Communications SARL 
5  (simon.morlat@linphone.org)
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 #ifdef WIN32
22 #include <time.h>
23 #endif
24 #include "linphonecore.h"
25 #include "sipsetup.h"
26 #include "lpconfig.h"
27 #include "private.h"
28
29
30 #include "mediastreamer2/mediastream.h"
31 #include "mediastreamer2/msvolume.h"
32 #include "mediastreamer2/msequalizer.h"
33
34
35
36 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, bool_t only_one_codec){
37         MSList *l=NULL;
38         const MSList *it;
39         for(it=codecs;it!=NULL;it=it->next){
40                 PayloadType *pt=(PayloadType*)it->data;
41                 if ((pt->flags & PAYLOAD_TYPE_ENABLED) && linphone_core_check_payload_type_usability(lc,pt)){
42                         l=ms_list_append(l,payload_type_clone(pt));
43                         if (only_one_codec) break;
44                 }
45         }
46         return l;
47 }
48
49 static SalMediaDescription *create_local_media_description(LinphoneCore *lc, 
50                 LinphoneCall *call, const char *username, bool_t only_one_codec){
51         MSList *l;
52         PayloadType *pt;
53         SalMediaDescription *md=sal_media_description_new();
54         md->nstreams=1;
55         strncpy(md->addr,call->localip,sizeof(md->addr));
56         strncpy(md->username,username,sizeof(md->username));
57         md->bandwidth=linphone_core_get_download_bandwidth(lc);
58         /*set audio capabilities */
59         strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
60         md->streams[0].port=call->audio_port;
61         md->streams[0].proto=SalProtoRtpAvp;
62         md->streams[0].type=SalAudio;
63         md->streams[0].ptime=lc->net_conf.down_ptime;
64         l=make_codec_list(lc,lc->codecs_conf.audio_codecs,only_one_codec);
65         pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
66         l=ms_list_append(l,pt);
67         md->streams[0].payloads=l;
68         
69         if (lc->dw_audio_bw>0)
70                 md->streams[0].bandwidth=lc->dw_audio_bw;
71
72         if (linphone_core_video_enabled (lc)){
73                 md->nstreams++;
74                 md->streams[1].port=call->video_port;
75                 md->streams[1].proto=SalProtoRtpAvp;
76                 md->streams[1].type=SalVideo;
77                 l=make_codec_list(lc,lc->codecs_conf.video_codecs,only_one_codec);
78                 md->streams[1].payloads=l;
79                 if (lc->dw_video_bw)
80                         md->streams[1].bandwidth=lc->dw_video_bw;
81         }
82         return md;
83 }
84
85 static int find_port_offset(LinphoneCore *lc){
86         int offset;
87         MSList *elem;
88         int audio_port;
89         bool_t already_used=FALSE;
90         for(offset=0;offset<100;++offset){
91                 audio_port=linphone_core_get_audio_port (lc)+offset;
92                 already_used=FALSE;
93                 for(elem=lc->calls;elem!=NULL;elem=elem->next){
94                         LinphoneCall *call=(LinphoneCall*)elem->data;
95                         if (call->audio_port==audio_port) {
96                                 already_used=TRUE;
97                                 break;
98                         }
99                 }
100                 if (!already_used) break;
101         }
102         if (offset==100){
103                 ms_error("Could not find any free port !");
104                 return -1;
105         }
106         return offset;
107 }
108
109 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
110         int port_offset;
111         call->refcnt=1;
112         call->state=LinphoneCallIdle;
113         call->start_time=time(NULL);
114         call->media_start_time=0;
115         call->log=linphone_call_log_new(call, from, to);
116         linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
117         port_offset=find_port_offset (call->core);
118         if (port_offset==-1) return;
119         call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
120         call->video_port=linphone_core_get_video_port(call->core)+port_offset;
121         
122 }
123
124 static void discover_mtu(LinphoneCore *lc, const char *remote){
125         int mtu;
126         if (lc->net_conf.mtu==0 ){
127                 /*attempt to discover mtu*/
128                 mtu=ms_discover_mtu(remote);
129                 if (mtu>0){
130                         ms_set_mtu(mtu);
131                         ms_message("Discovered mtu is %i, RTP payload max size is %i",
132                                 mtu, ms_get_payload_max_size());
133                 }
134         }
135 }
136
137 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to)
138 {
139         LinphoneCall *call=ms_new0(LinphoneCall,1);
140         call->dir=LinphoneCallOutgoing;
141         call->op=sal_op_new(lc->sal);
142         sal_op_set_user_pointer(call->op,call);
143         call->core=lc;
144         linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
145         linphone_call_init_common(call,from,to);
146         call->localdesc=create_local_media_description (lc,call,
147                 linphone_address_get_username(from),FALSE);
148         if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
149                 linphone_core_run_stun_tests(call->core,call);
150         discover_mtu(lc,linphone_address_get_domain (to));
151         return call;
152 }
153
154 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
155         LinphoneCall *call=ms_new0(LinphoneCall,1);
156         LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
157         char *to_str;
158         char *from_str;
159
160         call->dir=LinphoneCallIncoming;
161         sal_op_set_user_pointer(op,call);
162         call->op=op;
163         call->core=lc;
164
165         if (lc->sip_conf.ping_with_options){
166                 /*the following sends an option request back to the caller so that
167                  we get a chance to discover our nat'd address before answering.*/
168                 call->ping_op=sal_op_new(lc->sal);
169                 to_str=linphone_address_as_string(to);
170                 from_str=linphone_address_as_string(from);
171                 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
172                 sal_op_set_user_pointer(call->ping_op,call);
173                 sal_ping(call->ping_op,to_str,from_str);
174                 ms_free(to_str);
175                 ms_free(from_str);
176         }
177         
178         linphone_address_clean(from);
179         linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
180         linphone_call_init_common(call, from, to);
181         call->localdesc=create_local_media_description (lc,call,
182             linphone_address_get_username(me),lc->sip_conf.only_one_codec);
183         if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
184                 linphone_core_run_stun_tests(call->core,call);
185         discover_mtu(lc,linphone_address_get_domain(from));
186         linphone_address_destroy(me);
187         return call;
188 }
189
190 /* this function is called internally to get rid of a call.
191  It performs the following tasks:
192  - remove the call from the internal list of calls
193  - unref the LinphoneCall object
194  - update the call logs accordingly
195 */
196
197 static void linphone_call_set_terminated(LinphoneCall *call){
198         LinphoneCallStatus status=LinphoneCallAborted;
199         LinphoneCore *lc=call->core;
200         
201         linphone_core_update_allocated_audio_bandwidth(lc);
202         if (call->state==LinphoneCallEnd){
203                 status=LinphoneCallSuccess;
204                 
205         }
206         linphone_call_log_completed(call->log,call, status);
207         if (linphone_core_del_call(lc,call) != 0){
208                 ms_error("Could not remove the call from the list !!!");
209         }
210         if (call == lc->current_call){
211                 ms_message("Resetting the current call");
212                 lc->current_call=NULL;
213         }
214         if (ms_list_size(lc->calls)==0)
215                 linphone_core_notify_all_friends(lc,lc->presence_mode);
216         
217         if (call->op!=NULL) {
218                 /* so that we cannot have anymore upcalls for SAL
219                  concerning this call*/
220                 sal_op_release(call->op);
221                 call->op=NULL;
222         }
223         linphone_call_unref(call);
224 }
225
226 void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const char *message){
227         LinphoneCore *lc=call->core;
228         if (call->state!=cstate){
229                 call->state=cstate;
230                 if (lc->vtable.call_state_changed)
231                         lc->vtable.call_state_changed(lc,call,cstate,message);
232         }
233         if (call->state==LinphoneCallEnd || call->state==LinphoneCallError)
234                 linphone_call_set_terminated (call);
235 }
236
237 static void linphone_call_destroy(LinphoneCall *obj)
238 {
239         if (obj->op!=NULL) {
240                 sal_op_release(obj->op);
241                 obj->op=NULL;
242         }
243         if (obj->resultdesc!=NULL) {
244                 sal_media_description_unref(obj->resultdesc);
245                 obj->resultdesc=NULL;
246         }
247         if (obj->localdesc!=NULL) {
248                 sal_media_description_unref(obj->localdesc);
249                 obj->localdesc=NULL;
250         }
251         if (obj->ping_op) {
252                 sal_op_release(obj->ping_op);
253         }
254         ms_free(obj);
255 }
256
257 /**
258  * @addtogroup calls
259  * @{
260 **/
261
262 /**
263  * Increments the call 's reference count.
264  * An application that wishes to retain a pointer to call object
265  * must use this function to unsure the pointer remains
266  * valid. Once the application no more needs this pointer,
267  * it must call linphone_call_unref().
268 **/
269 void linphone_call_ref(LinphoneCall *obj){
270         obj->refcnt++;
271 }
272
273 /**
274  * Decrements the call object reference count.
275  * See linphone_call_ref().
276 **/
277 void linphone_call_unref(LinphoneCall *obj){
278         obj->refcnt--;
279         if (obj->refcnt==0)
280                 linphone_call_destroy(obj);
281 }
282
283 /**
284  * Returns the remote address associated to this call
285  *
286 **/
287 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
288         return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
289 }
290
291 /**
292  * Returns the remote address associated to this call as a string.
293  *
294  * The result string must be freed by user using ms_free().
295 **/
296 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
297         return linphone_address_as_string(linphone_call_get_remote_address(call));
298 }
299
300 /**
301  * Retrieves the call's current state.
302 **/
303 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
304         return call->state;
305 }
306
307 /**
308  * Get the user_pointer in the LinphoneCall
309  *
310  * @ingroup call_control
311  *
312  * return user_pointer an opaque user pointer that can be retrieved at any time
313 **/
314 void *linphone_call_get_user_pointer(LinphoneCall *call)
315 {
316         return call->user_pointer;
317 }
318
319 /**
320  * Set the user_pointer in the LinphoneCall
321  *
322  * @ingroup call_control
323  *
324  * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
325 **/
326 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
327 {
328         call->user_pointer = user_pointer;
329 }
330
331 /**
332  * Returns the call log associated to this call.
333 **/
334 LinphoneCallLog *linphone_call_get_call_log(const LinphoneCall *call){
335         return call->log;
336 }
337
338 /**
339  * Returns the refer-to uri (if the call received was transfered).
340 **/
341 const char *linphone_call_get_refer_to(const LinphoneCall *call){
342         return call->refer_to;
343 }
344
345 LinphoneCallDir linphone_call_get_dir(const LinphoneCall *call){
346         return call->log->dir;
347 }
348
349 /**
350  * @}
351 **/
352
353
354 #ifdef TEST_EXT_RENDERER
355 static void rendercb(void *data, const MSPicture *local, const MSPicture *remote){
356         ms_message("rendercb, local buffer=%p, remote buffer=%p",
357                    local ? local->planes[0] : NULL, remote? remote->planes[0] : NULL);
358 }
359 #endif
360
361 void linphone_call_init_media_streams(LinphoneCall *call){
362         LinphoneCore *lc=call->core;
363         SalMediaDescription *md=call->localdesc;
364         AudioStream *audiostream;
365         
366         call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
367         if (linphone_core_echo_limiter_enabled(lc)){
368                 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
369                 if (strcasecmp(type,"mic")==0)
370                         audio_stream_enable_echo_limiter(audiostream,ELControlMic);
371                 else if (strcasecmp(type,"full")==0)
372                         audio_stream_enable_echo_limiter(audiostream,ELControlFull);
373         }
374         audio_stream_enable_gain_control(audiostream,TRUE);
375         if (linphone_core_echo_cancellation_enabled(lc)){
376                 int len,delay,framesize;
377                 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
378                 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
379                 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
380                 audio_stream_set_echo_canceller_params(audiostream,len,delay,framesize);
381         }
382         audio_stream_enable_automatic_gain_control(audiostream,linphone_core_agc_enabled(lc));
383         {
384                 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
385                 audio_stream_enable_noise_gate(audiostream,enabled);
386         }
387         if (lc->a_rtp)
388                 rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
389
390 #ifdef VIDEO_ENABLED
391         if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
392                 call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
393 #ifdef TEST_EXT_RENDERER
394                 video_stream_set_render_callback(call->videostream,rendercb,NULL);
395 #endif
396         }
397 #else
398         call->videostream=NULL;
399 #endif
400 }
401
402
403 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
404
405 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
406         LinphoneCore* lc = (LinphoneCore*)user_data;
407         if (dtmf<0 || dtmf>15){
408                 ms_warning("Bad dtmf value %i",dtmf);
409                 return;
410         }
411         if (lc->vtable.dtmf_received != NULL)
412                 lc->vtable.dtmf_received(lc, linphone_core_get_current_call(lc), dtmf_tab[dtmf]);
413 }
414
415 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
416         if (st->equalizer){
417                 MSFilter *f=st->equalizer;
418                 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
419                 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
420                 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
421                 if (enabled){
422                         if (gains){
423                                 do{
424                                         int bytes;
425                                         MSEqualizerGain g;
426                                         if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
427                                                 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
428                                                 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
429                                                 gains+=bytes;
430                                         }else break;
431                                 }while(1);
432                         }
433                 }
434         }
435 }
436
437
438 static void post_configure_audio_streams(LinphoneCall*call){
439         AudioStream *st=call->audiostream;
440         LinphoneCore *lc=call->core;
441         float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
442         float thres = 0;
443         float recv_gain;
444         float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
445         float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
446         int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
447         
448         if (mic_gain!=-1)
449                 audio_stream_set_mic_gain(st,mic_gain);
450         call->audio_muted=FALSE;
451
452         recv_gain = lc->sound_conf.soft_play_lev;
453         if (recv_gain != 0) {
454                 linphone_core_set_playback_gain_db (lc,recv_gain);
455         }
456         if (st->volsend){
457                 ms_filter_call_method(st->volsend,MS_VOLUME_REMOVE_DC,&dc_removal);
458         }
459         if (linphone_core_echo_limiter_enabled(lc)){
460                 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
461                 thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
462                 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
463                 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
464                 MSFilter *f=NULL;
465                 if (st->el_type!=ELInactive){
466                         f=st->volsend;
467                         if (speed==-1) speed=0.03;
468                         if (force==-1) force=25;
469                         ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
470                         ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
471                         if (thres!=-1)
472                                 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
473                         if (sustain!=-1)
474                                 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
475                 }
476         }
477                 
478         if (st->volsend){
479                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
480                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
481         }
482         if (st->volrecv){
483                 /* parameters for a limited noise-gate effect, using echo limiter threshold */
484                 float floorgain = 1/mic_gain;
485                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&thres);
486                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
487         }
488         parametrize_equalizer(lc,st);
489         if (lc->vtable.dtmf_received!=NULL){
490                 /* replace by our default action*/
491                 audio_stream_play_received_dtmfs(call->audiostream,FALSE);
492                 rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
493         }
494 }
495
496
497
498
499 static RtpProfile *make_profile(LinphoneCore *lc, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
500         int bw;
501         const MSList *elem;
502         RtpProfile *prof=rtp_profile_new("Call profile");
503         bool_t first=TRUE;
504         int remote_bw=0;
505         *used_pt=-1;
506         
507         for(elem=desc->payloads;elem!=NULL;elem=elem->next){
508                 PayloadType *pt=(PayloadType*)elem->data;
509         
510                 if (first) {
511                         if (desc->type==SalAudio){
512                                 linphone_core_update_allocated_audio_bandwidth_in_call(lc,pt);
513                         }
514                         *used_pt=payload_type_get_number(pt);
515                         first=FALSE;
516                 }
517                 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
518                 else if (md->bandwidth>0) {
519                         /*case where b=AS is given globally, not per stream*/
520                         remote_bw=md->bandwidth;
521                         if (desc->type==SalVideo){
522                                 remote_bw-=lc->audio_bw;
523                         }
524                 }
525                 
526                 if (desc->type==SalAudio){                      
527                                 bw=get_min_bandwidth(lc->up_audio_bw,remote_bw);
528                 }else bw=get_min_bandwidth(lc->up_video_bw,remote_bw);
529                 if (bw>0) pt->normal_bitrate=bw*1000;
530                 else if (desc->type==SalAudio){
531                         pt->normal_bitrate=-1;
532                 }
533                 if (desc->ptime>0){
534                         char tmp[40];
535                         snprintf(tmp,sizeof(tmp),"ptime=%i",desc->ptime);
536                         payload_type_append_send_fmtp(pt,tmp);
537                 }
538                 rtp_profile_set_payload(prof,payload_type_get_number(pt),pt);
539         }
540         return prof;
541 }
542
543 void linphone_call_start_media_streams(LinphoneCall *call){
544         LinphoneCore *lc=call->core;
545         LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
546         const char *tool="linphone-" LINPHONE_VERSION;
547         char *cname;
548         int used_pt=-1;
549         if(call->audiostream == NULL)
550         {
551                 ms_fatal("start_media_stream() called without prior init !");
552                 return;
553         }
554         /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
555         int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
556
557         if (call->media_start_time==0) call->media_start_time=time(NULL);
558
559         cname=linphone_address_as_string_uri_only(me);
560         {
561                 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
562                                                         SalProtoRtpAvp,SalAudio);
563                 if (stream){
564                         MSSndCard *playcard=lc->sound_conf.lsd_card ? 
565                                 lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
566                         MSSndCard *captcard=lc->sound_conf.capt_sndcard;
567                         const char *playfile=lc->play_file;
568                         const char *recfile=lc->rec_file;
569                         call->audio_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
570                         if (used_pt!=-1){
571                                 if (playcard==NULL) {
572                                         ms_warning("No card defined for playback !");
573                                 }
574                                 if (captcard==NULL) {
575                                         ms_warning("No card defined for capture !");
576                                 }
577                                 ms_message("streamdir is %i",stream->dir);
578                                 /*Replace soundcard filters by inactive file players or recorders
579                                  when placed in recvonly or sendonly mode*/
580                                 if (stream->port==0 || stream->dir==SalStreamRecvOnly){
581                                         captcard=NULL;
582                                         playfile=NULL;
583                                 }else if (stream->dir==SalStreamSendOnly){
584                                         playcard=NULL;
585                                         captcard=NULL;
586                                         recfile=NULL;
587                                 }
588                                 /*if playfile are supplied don't use soundcards*/
589                                 if (playfile) captcard=NULL;
590                                 if (recfile) playcard=NULL;
591                                 audio_stream_start_full(
592                                         call->audiostream,
593                                         call->audio_profile,
594                                         stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
595                                         stream->port,
596                                         stream->port+1,
597                                         used_pt,
598                                         jitt_comp,
599                                         playfile,
600                                         recfile,
601                                         playcard,
602                                         captcard,
603                                         linphone_core_echo_cancellation_enabled(lc));
604                                 post_configure_audio_streams(call);
605                                 audio_stream_set_rtcp_information(call->audiostream, cname, tool);
606                         }else ms_warning("No audio stream accepted ?");
607                 }
608         }
609 #ifdef VIDEO_ENABLED
610         {
611                 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
612                                                         SalProtoRtpAvp,SalVideo);
613                 used_pt=-1;
614                 /* shutdown preview */
615                 if (lc->previewstream!=NULL) {
616                         video_preview_stop(lc->previewstream);
617                         lc->previewstream=NULL;
618                 }
619                 if (stream) {
620                         const char *addr=stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr;
621                         call->video_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
622                         if (used_pt!=-1){
623                                 VideoStreamDir dir=VideoStreamSendRecv;
624                                 MSWebCam *cam=lc->video_conf.device;
625                                 bool_t is_inactive=FALSE;
626                                 
627                                 video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
628                                 video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
629                                                 
630                                 if (stream->dir==SalStreamSendOnly && lc->video_conf.capture ){
631                                         cam=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),"Static picture");
632                                         dir=VideoStreamSendOnly;
633                                 }else if (stream->dir==SalStreamRecvOnly && lc->video_conf.display ){
634                                         dir=VideoStreamRecvOnly;
635                                 }else if (stream->dir==SalStreamSendRecv){
636                                         if (lc->video_conf.display && lc->video_conf.capture)
637                                                 dir=VideoStreamSendRecv;
638                                         else if (lc->video_conf.display)
639                                                 dir=VideoStreamRecvOnly;
640                                         else
641                                                 dir=VideoStreamSendOnly;
642                                 }else{
643                                         ms_warning("video stream is inactive.");
644                                         /*either inactive or incompatible with local capabilities*/
645                                         is_inactive=TRUE;
646                                 }
647                                 if (!is_inactive){
648                                         video_stream_set_direction (call->videostream, dir);
649                                         video_stream_start(call->videostream,
650                                                 call->video_profile, addr, stream->port,
651                                                 stream->port+1,
652                                                 used_pt, jitt_comp, cam);
653                                         video_stream_set_rtcp_information(call->videostream, cname,tool);
654                                 }
655                         }else ms_warning("No video stream accepted.");
656                 }else{
657                         ms_warning("No valid video stream defined.");
658                 }
659         }
660 #endif
661         goto end;
662         end:
663                 ms_free(cname);
664                 linphone_address_destroy(me);
665 }
666
667
668
669 static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
670         audio_stream_get_local_rtp_stats (st,&log->local_stats);
671 }
672
673 void linphone_call_stop_media_streams(LinphoneCall *call){
674         if (call->audiostream!=NULL) {
675                 linphone_call_log_fill_stats (call->log,call->audiostream);
676                 audio_stream_stop(call->audiostream);
677                 call->audiostream=NULL;
678         }
679 #ifdef VIDEO_ENABLED
680         if (call->videostream!=NULL){
681                 video_stream_stop(call->videostream);
682                 call->videostream=NULL;
683         }
684         
685 #endif
686         if (call->audio_profile){
687                 rtp_profile_clear_all(call->audio_profile);
688                 rtp_profile_destroy(call->audio_profile);
689                 call->audio_profile=NULL;
690         }
691         if (call->video_profile){
692                 rtp_profile_clear_all(call->video_profile);
693                 rtp_profile_destroy(call->video_profile);
694                 call->video_profile=NULL;
695         }
696 }
697
698