]> sjero.net Git - linphone/blob - coreapi/linphonecall.c
8139bbdb2961a96d34dccc1e178441a422b92e19
[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 #include "mediastreamer2/msfileplayer.h"
34 #include "mediastreamer2/msjpegwriter.h"
35
36 #ifdef VIDEO_ENABLED
37 static MSWebCam *get_nowebcam_device(){
38         return ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),"StaticImage: Static picture");
39 }
40 #endif
41
42
43 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, bool_t only_one_codec){
44         MSList *l=NULL;
45         const MSList *it;
46         for(it=codecs;it!=NULL;it=it->next){
47                 PayloadType *pt=(PayloadType*)it->data;
48                 if ((pt->flags & PAYLOAD_TYPE_ENABLED) && linphone_core_check_payload_type_usability(lc,pt)){
49                         l=ms_list_append(l,payload_type_clone(pt));
50                         if (only_one_codec) break;
51                 }
52         }
53         return l;
54 }
55
56 SalMediaDescription *create_local_media_description(LinphoneCore *lc, 
57                 LinphoneCall *call, bool_t with_video, bool_t only_one_codec){
58         MSList *l;
59         PayloadType *pt;
60         const char *me=linphone_core_get_identity(lc);
61         LinphoneAddress *addr=linphone_address_new(me);
62         const char *username=linphone_address_get_username (addr);
63         SalMediaDescription *md=sal_media_description_new();
64
65         md->nstreams=1;
66         strncpy(md->addr,call->localip,sizeof(md->addr));
67         strncpy(md->username,username,sizeof(md->username));
68         md->bandwidth=linphone_core_get_download_bandwidth(lc);
69         /*set audio capabilities */
70         strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
71         md->streams[0].port=call->audio_port;
72         md->streams[0].proto=SalProtoRtpAvp;
73         md->streams[0].type=SalAudio;
74         md->streams[0].ptime=lc->net_conf.down_ptime;
75         l=make_codec_list(lc,lc->codecs_conf.audio_codecs,only_one_codec);
76         pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
77         l=ms_list_append(l,pt);
78         md->streams[0].payloads=l;
79         
80         if (lc->dw_audio_bw>0)
81                 md->streams[0].bandwidth=lc->dw_audio_bw;
82
83         if (with_video){
84                 md->nstreams++;
85                 md->streams[1].port=call->video_port;
86                 md->streams[1].proto=SalProtoRtpAvp;
87                 md->streams[1].type=SalVideo;
88                 l=make_codec_list(lc,lc->codecs_conf.video_codecs,only_one_codec);
89                 md->streams[1].payloads=l;
90                 if (lc->dw_video_bw)
91                         md->streams[1].bandwidth=lc->dw_video_bw;
92         }
93         linphone_address_destroy(addr);
94         return md;
95 }
96
97 static int find_port_offset(LinphoneCore *lc){
98         int offset;
99         MSList *elem;
100         int audio_port;
101         bool_t already_used=FALSE;
102         for(offset=0;offset<100;offset+=2){
103                 audio_port=linphone_core_get_audio_port (lc)+offset;
104                 already_used=FALSE;
105                 for(elem=lc->calls;elem!=NULL;elem=elem->next){
106                         LinphoneCall *call=(LinphoneCall*)elem->data;
107                         if (call->audio_port==audio_port) {
108                                 already_used=TRUE;
109                                 break;
110                         }
111                 }
112                 if (!already_used) break;
113         }
114         if (offset==100){
115                 ms_error("Could not find any free port !");
116                 return -1;
117         }
118         return offset;
119 }
120
121 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
122         int port_offset;
123         call->refcnt=1;
124         call->state=LinphoneCallIdle;
125         call->start_time=time(NULL);
126         call->media_start_time=0;
127         call->log=linphone_call_log_new(call, from, to);
128         linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
129         port_offset=find_port_offset (call->core);
130         if (port_offset==-1) return;
131         call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
132         call->video_port=linphone_core_get_video_port(call->core)+port_offset;
133         
134 }
135
136 static void discover_mtu(LinphoneCore *lc, const char *remote){
137         int mtu;
138         if (lc->net_conf.mtu==0 ){
139                 /*attempt to discover mtu*/
140                 mtu=ms_discover_mtu(remote);
141                 if (mtu>0){
142                         ms_set_mtu(mtu);
143                         ms_message("Discovered mtu is %i, RTP payload max size is %i",
144                                 mtu, ms_get_payload_max_size());
145                 }
146         }
147 }
148
149 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, const LinphoneCallParams *params)
150 {
151         LinphoneCall *call=ms_new0(LinphoneCall,1);
152         call->dir=LinphoneCallOutgoing;
153         call->op=sal_op_new(lc->sal);
154         sal_op_set_user_pointer(call->op,call);
155         call->core=lc;
156         linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
157         linphone_call_init_common(call,from,to);
158         call->params=*params;
159         call->localdesc=create_local_media_description (lc,call,params->has_video,FALSE);
160         call->camera_active=params->has_video;
161         if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
162                 linphone_core_run_stun_tests(call->core,call);
163         discover_mtu(lc,linphone_address_get_domain (to));
164         return call;
165 }
166
167 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
168         LinphoneCall *call=ms_new0(LinphoneCall,1);
169         char *to_str;
170         char *from_str;
171
172         call->dir=LinphoneCallIncoming;
173         sal_op_set_user_pointer(op,call);
174         call->op=op;
175         call->core=lc;
176
177         if (lc->sip_conf.ping_with_options){
178                 /*the following sends an option request back to the caller so that
179                  we get a chance to discover our nat'd address before answering.*/
180                 call->ping_op=sal_op_new(lc->sal);
181                 to_str=linphone_address_as_string(to);
182                 from_str=linphone_address_as_string(from);
183                 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
184                 sal_op_set_user_pointer(call->ping_op,call);
185                 sal_ping(call->ping_op,to_str,from_str);
186                 ms_free(to_str);
187                 ms_free(from_str);
188         }
189         
190         linphone_address_clean(from);
191         linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
192         linphone_call_init_common(call, from, to);
193         call->params.has_video=linphone_core_video_enabled(lc);
194         call->localdesc=create_local_media_description (lc,call,
195                                   call->params.has_video,lc->sip_conf.only_one_codec);
196         call->camera_active=call->params.has_video;
197         if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
198                 linphone_core_run_stun_tests(call->core,call);
199         discover_mtu(lc,linphone_address_get_domain(from));
200         return call;
201 }
202
203 /* this function is called internally to get rid of a call.
204  It performs the following tasks:
205  - remove the call from the internal list of calls
206  - unref the LinphoneCall object
207  - update the call logs accordingly
208 */
209
210 static void linphone_call_set_terminated(LinphoneCall *call){
211         LinphoneCallStatus status=LinphoneCallAborted;
212         LinphoneCore *lc=call->core;
213         
214         linphone_core_update_allocated_audio_bandwidth(lc);
215         if (call->state==LinphoneCallEnd){
216                 status=LinphoneCallSuccess;
217                 
218         }
219         linphone_call_log_completed(call->log,call, status);
220         
221         if (call == lc->current_call){
222                 ms_message("Resetting the current call");
223                 lc->current_call=NULL;
224                 linphone_core_start_pending_refered_calls(lc);
225         }
226
227         if (linphone_core_del_call(lc,call) != 0){
228                 ms_error("Could not remove the call from the list !!!");
229         }
230         
231         if (ms_list_size(lc->calls)==0)
232                 linphone_core_notify_all_friends(lc,lc->presence_mode);
233         
234         if (call->op!=NULL) {
235                 /* so that we cannot have anymore upcalls for SAL
236                  concerning this call*/
237                 sal_op_release(call->op);
238                 call->op=NULL;
239         }
240         linphone_call_unref(call);
241 }
242
243 const char *linphone_call_state_to_string(LinphoneCallState cs){
244         switch (cs){
245                 case LinphoneCallIdle:
246                         return "LinphoneCallIdle";
247                 case LinphoneCallIncomingReceived:
248                         return "LinphoneCallIncomingReceived";
249                 case LinphoneCallOutgoingInit:
250                         return "LinphoneCallOutgoingInit";
251                 case LinphoneCallOutgoingProgress:
252                         return "LinphoneCallOutgoingProgress";
253                 case LinphoneCallOutgoingRinging:
254                         return "LinphoneCallOutgoingRinging";
255                 case LinphoneCallOutgoingEarlyMedia:
256                         return "LinphoneCallOutgoingEarlyMedia";
257                 case LinphoneCallConnected:
258                         return "LinphoneCallConnected";
259                 case LinphoneCallStreamsRunning:
260                         return "LinphoneCallStreamsRunning";
261                 case LinphoneCallPausing:
262                         return "LinphoneCallPausing";
263                 case LinphoneCallPaused:
264                         return "LinphoneCallPaused";
265                 case LinphoneCallResuming:
266                         return "LinphoneCallResuming";
267                 case LinphoneCallRefered:
268                         return "LinphoneCallRefered";
269                 case LinphoneCallError:
270                         return "LinphoneCallRefered";
271                 case LinphoneCallEnd:
272                         return "LinphoneCallEnd";
273                 case LinphoneCallPausedByRemote:
274                         return "LinphoneCallPausedByRemote";
275                 case LinphoneCallUpdatedByRemote:
276                         return "LinphoneCallUpdatedByRemote";
277         }
278         return "undefined state";
279 }
280
281 void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const char *message){
282         LinphoneCore *lc=call->core;
283         bool_t finalize_call=FALSE;
284         if (call->state!=cstate){
285                 ms_message("Call %p: moving from state %s to %s",call,linphone_call_state_to_string(call->state),
286                            linphone_call_state_to_string(cstate));
287                 if (cstate!=LinphoneCallRefered){
288                         /*LinphoneCallRefered is rather an event, not a state.
289                          Indeed it does not change the state of the call (still paused or running)*/
290                         call->state=cstate;
291                 }
292                 if (cstate==LinphoneCallEnd || cstate==LinphoneCallError){
293                         finalize_call=TRUE;
294                         linphone_call_ref(call);
295                         linphone_call_set_terminated (call);
296                 }
297                 if (lc->vtable.call_state_changed)
298                         lc->vtable.call_state_changed(lc,call,cstate,message);
299                 if (finalize_call)
300                         linphone_call_unref(call);
301         }
302 }
303
304 static void linphone_call_destroy(LinphoneCall *obj)
305 {
306         if (obj->op!=NULL) {
307                 sal_op_release(obj->op);
308                 obj->op=NULL;
309         }
310         if (obj->resultdesc!=NULL) {
311                 sal_media_description_unref(obj->resultdesc);
312                 obj->resultdesc=NULL;
313         }
314         if (obj->localdesc!=NULL) {
315                 sal_media_description_unref(obj->localdesc);
316                 obj->localdesc=NULL;
317         }
318         if (obj->ping_op) {
319                 sal_op_release(obj->ping_op);
320         }
321         if (obj->refer_to){
322                 ms_free(obj->refer_to);
323         }
324         ms_free(obj);
325 }
326
327 /**
328  * @addtogroup call_control
329  * @{
330 **/
331
332 /**
333  * Increments the call 's reference count.
334  * An application that wishes to retain a pointer to call object
335  * must use this function to unsure the pointer remains
336  * valid. Once the application no more needs this pointer,
337  * it must call linphone_call_unref().
338 **/
339 void linphone_call_ref(LinphoneCall *obj){
340         obj->refcnt++;
341 }
342
343 /**
344  * Decrements the call object reference count.
345  * See linphone_call_ref().
346 **/
347 void linphone_call_unref(LinphoneCall *obj){
348         obj->refcnt--;
349         if (obj->refcnt==0){
350                 linphone_call_destroy(obj);
351         }
352 }
353
354 /**
355  * Returns current parameters associated to the call.
356 **/
357 const LinphoneCallParams * linphone_call_get_current_params(const LinphoneCall *call){
358         return &call->params;
359 }
360
361 /**
362  * Returns the remote address associated to this call
363  *
364 **/
365 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
366         return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
367 }
368
369 /**
370  * Returns the remote address associated to this call as a string.
371  *
372  * The result string must be freed by user using ms_free().
373 **/
374 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
375         return linphone_address_as_string(linphone_call_get_remote_address(call));
376 }
377
378 /**
379  * Retrieves the call's current state.
380 **/
381 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
382         return call->state;
383 }
384
385 /**
386  * Get the user_pointer in the LinphoneCall
387  *
388  * @ingroup call_control
389  *
390  * return user_pointer an opaque user pointer that can be retrieved at any time
391 **/
392 void *linphone_call_get_user_pointer(LinphoneCall *call)
393 {
394         return call->user_pointer;
395 }
396
397 /**
398  * Set the user_pointer in the LinphoneCall
399  *
400  * @ingroup call_control
401  *
402  * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
403 **/
404 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
405 {
406         call->user_pointer = user_pointer;
407 }
408
409 /**
410  * Returns the call log associated to this call.
411 **/
412 LinphoneCallLog *linphone_call_get_call_log(const LinphoneCall *call){
413         return call->log;
414 }
415
416 /**
417  * Returns the refer-to uri (if the call was transfered).
418 **/
419 const char *linphone_call_get_refer_to(const LinphoneCall *call){
420         return call->refer_to;
421 }
422
423 /**
424  * Returns direction of the call (incoming or outgoing).
425 **/
426 LinphoneCallDir linphone_call_get_dir(const LinphoneCall *call){
427         return call->log->dir;
428 }
429
430 /**
431  * Returns the far end's user agent description string, if available.
432 **/
433 const char *linphone_call_get_remote_user_agent(LinphoneCall *call){
434         if (call->op){
435                 return sal_op_get_remote_ua (call->op);
436         }
437         return NULL;
438 }
439
440 /**
441  * Returns true if this calls has received a transfer that has not been
442  * executed yet.
443  * Pending transfers are executed when this call is being paused or closed,
444  * locally or by remote endpoint.
445  * If the call is already paused while receiving the transfer request, the 
446  * transfer immediately occurs.
447 **/
448 bool_t linphone_call_has_transfer_pending(const LinphoneCall *call){
449         return call->refer_pending;
450 }
451
452 /**
453  * Returns call's duration in seconds.
454 **/
455 int linphone_call_get_duration(const LinphoneCall *call){
456         if (call->media_start_time==0) return 0;
457         return time(NULL)-call->media_start_time;
458 }
459
460 /**
461  * Indicate whether camera input should be sent to remote end.
462 **/
463 void linphone_call_enable_camera (LinphoneCall *call, bool_t enable){
464 #ifdef VIDEO_ENABLED
465         if (call->videostream!=NULL && call->videostream->ticker!=NULL){
466                 LinphoneCore *lc=call->core;
467                 MSWebCam *nowebcam=get_nowebcam_device();
468                 if (call->camera_active!=enable && lc->video_conf.device!=nowebcam){
469                         video_stream_change_camera(call->videostream,
470                                      enable ? lc->video_conf.device : nowebcam);
471                 }
472         }
473         call->camera_active=enable;
474 #endif
475 }
476
477 /**
478  * Take a photo of currently received video and write it into a jpeg file.
479 **/
480 int linphone_call_take_video_snapshot(LinphoneCall *call, const char *file){
481 #ifdef VIDEO_ENABLED
482         if (call->videostream!=NULL && call->videostream->jpegwriter!=NULL){
483                 return ms_filter_call_method(call->videostream->jpegwriter,MS_JPEG_WRITER_TAKE_SNAPSHOT,(void*)file);
484         }
485         ms_warning("Cannot take snapshot: no currently running video stream on this call.");
486         return -1;
487 #endif
488         return -1;
489 }
490
491 /**
492  *
493 **/
494 bool_t linphone_call_camera_enabled (const LinphoneCall *call){
495         return call->camera_active;
496 }
497
498 /**
499  * 
500 **/
501 void linphone_call_params_enable_video(LinphoneCallParams *cp, bool_t enabled){
502         cp->has_video=enabled;
503 }
504
505 /**
506  *
507 **/
508 bool_t linphone_call_params_video_enabled(const LinphoneCallParams *cp){
509         return cp->has_video;
510 }
511
512 /**
513  *
514 **/
515 LinphoneCallParams * linphone_call_params_copy(const LinphoneCallParams *cp){
516         LinphoneCallParams *ncp=ms_new0(LinphoneCallParams,1);
517         memcpy(ncp,cp,sizeof(LinphoneCallParams));
518         return ncp;
519 }
520
521 /**
522  *
523 **/
524 void linphone_call_params_destroy(LinphoneCallParams *p){
525         ms_free(p);
526 }
527
528 /**
529  * @}
530 **/
531
532
533 #ifdef TEST_EXT_RENDERER
534 static void rendercb(void *data, const MSPicture *local, const MSPicture *remote){
535         ms_message("rendercb, local buffer=%p, remote buffer=%p",
536                    local ? local->planes[0] : NULL, remote? remote->planes[0] : NULL);
537 }
538 #endif
539
540 void linphone_call_init_media_streams(LinphoneCall *call){
541         LinphoneCore *lc=call->core;
542         SalMediaDescription *md=call->localdesc;
543         AudioStream *audiostream;
544         
545         call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
546         if (linphone_core_echo_limiter_enabled(lc)){
547                 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
548                 if (strcasecmp(type,"mic")==0)
549                         audio_stream_enable_echo_limiter(audiostream,ELControlMic);
550                 else if (strcasecmp(type,"full")==0)
551                         audio_stream_enable_echo_limiter(audiostream,ELControlFull);
552         }
553         audio_stream_enable_gain_control(audiostream,TRUE);
554         if (linphone_core_echo_cancellation_enabled(lc)){
555                 int len,delay,framesize;
556                 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
557                 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
558                 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
559                 audio_stream_set_echo_canceller_params(audiostream,len,delay,framesize);
560         }
561         audio_stream_enable_automatic_gain_control(audiostream,linphone_core_agc_enabled(lc));
562         {
563                 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
564                 audio_stream_enable_noise_gate(audiostream,enabled);
565         }
566         if (lc->a_rtp)
567                 rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
568
569 #ifdef VIDEO_ENABLED
570         if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
571                 call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
572         if( lc->video_conf.displaytype != NULL)
573                 video_stream_set_display_filter_name(call->videostream,lc->video_conf.displaytype);
574 #ifdef TEST_EXT_RENDERER
575                 video_stream_set_render_callback(call->videostream,rendercb,NULL);
576 #endif
577         }
578 #else
579         call->videostream=NULL;
580 #endif
581 }
582
583
584 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
585
586 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
587         LinphoneCore* lc = (LinphoneCore*)user_data;
588         if (dtmf<0 || dtmf>15){
589                 ms_warning("Bad dtmf value %i",dtmf);
590                 return;
591         }
592         if (lc->vtable.dtmf_received != NULL)
593                 lc->vtable.dtmf_received(lc, linphone_core_get_current_call(lc), dtmf_tab[dtmf]);
594 }
595
596 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
597         if (st->equalizer){
598                 MSFilter *f=st->equalizer;
599                 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
600                 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
601                 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
602                 if (enabled){
603                         if (gains){
604                                 do{
605                                         int bytes;
606                                         MSEqualizerGain g;
607                                         if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
608                                                 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
609                                                 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
610                                                 gains+=bytes;
611                                         }else break;
612                                 }while(1);
613                         }
614                 }
615         }
616 }
617
618
619 static void post_configure_audio_streams(LinphoneCall*call){
620         AudioStream *st=call->audiostream;
621         LinphoneCore *lc=call->core;
622         float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
623         float thres = 0;
624         float recv_gain;
625         float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
626         float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
627         int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
628         
629         if (mic_gain!=-1)
630                 audio_stream_set_mic_gain(st,mic_gain);
631         call->audio_muted=FALSE;
632
633         recv_gain = lc->sound_conf.soft_play_lev;
634         if (recv_gain != 0) {
635                 linphone_core_set_playback_gain_db (lc,recv_gain);
636         }
637         if (st->volsend){
638                 ms_filter_call_method(st->volsend,MS_VOLUME_REMOVE_DC,&dc_removal);
639         }
640         if (linphone_core_echo_limiter_enabled(lc)){
641                 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
642                 thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
643                 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
644                 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
645                 MSFilter *f=NULL;
646                 if (st->el_type!=ELInactive){
647                         f=st->volsend;
648                         if (speed==-1) speed=0.03;
649                         if (force==-1) force=25;
650                         ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
651                         ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
652                         if (thres!=-1)
653                                 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
654                         if (sustain!=-1)
655                                 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
656                 }
657         }
658                 
659         if (st->volsend){
660                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
661                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
662         }
663         if (st->volrecv){
664                 /* parameters for a limited noise-gate effect, using echo limiter threshold */
665                 float floorgain = 1/mic_gain;
666                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&thres);
667                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
668         }
669         parametrize_equalizer(lc,st);
670         if (lc->vtable.dtmf_received!=NULL){
671                 /* replace by our default action*/
672                 audio_stream_play_received_dtmfs(call->audiostream,FALSE);
673                 rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
674         }
675 }
676
677
678
679
680 static RtpProfile *make_profile(LinphoneCore *lc, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
681         int bw;
682         const MSList *elem;
683         RtpProfile *prof=rtp_profile_new("Call profile");
684         bool_t first=TRUE;
685         int remote_bw=0;
686         *used_pt=-1;
687         
688         for(elem=desc->payloads;elem!=NULL;elem=elem->next){
689                 PayloadType *pt=(PayloadType*)elem->data;
690                 int number;
691                 
692                 if (first) {
693                         if (desc->type==SalAudio){
694                                 linphone_core_update_allocated_audio_bandwidth_in_call(lc,pt);
695                         }
696                         *used_pt=payload_type_get_number(pt);
697                         first=FALSE;
698                 }
699                 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
700                 else if (md->bandwidth>0) {
701                         /*case where b=AS is given globally, not per stream*/
702                         remote_bw=md->bandwidth;
703                         if (desc->type==SalVideo){
704                                 remote_bw-=lc->audio_bw;
705                         }
706                 }
707                 
708                 if (desc->type==SalAudio){                      
709                                 bw=get_min_bandwidth(lc->up_audio_bw,remote_bw);
710                 }else bw=get_min_bandwidth(lc->up_video_bw,remote_bw);
711                 if (bw>0) pt->normal_bitrate=bw*1000;
712                 else if (desc->type==SalAudio){
713                         pt->normal_bitrate=-1;
714                 }
715                 if (desc->ptime>0){
716                         char tmp[40];
717                         snprintf(tmp,sizeof(tmp),"ptime=%i",desc->ptime);
718                         payload_type_append_send_fmtp(pt,tmp);
719                 }
720                 number=payload_type_get_number(pt);
721                 if (rtp_profile_get_payload(prof,number)!=NULL){
722                         ms_warning("A payload type with number %i already exists in profile !",number);
723                 }else
724                         rtp_profile_set_payload(prof,number,pt);
725         }
726         return prof;
727 }
728
729 static void setup_ring_player(LinphoneCore *lc, LinphoneCall *call){
730         int pause_time=3000;
731         audio_stream_play(call->audiostream,lc->sound_conf.ringback_tone);
732         ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
733 }
734
735 static void _linphone_call_start_media_streams(LinphoneCall *call, bool_t send_early_media){
736         LinphoneCore *lc=call->core;
737         LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
738         const char *tool="linphone-" LINPHONE_VERSION;
739         char *cname;
740         int used_pt=-1;
741         if(call->audiostream == NULL)
742         {
743                 ms_fatal("start_media_stream() called without prior init !");
744                 return;
745         }
746         /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
747         int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
748
749         if (call->media_start_time==0) call->media_start_time=time(NULL);
750
751         cname=linphone_address_as_string_uri_only(me);
752         {
753                 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
754                                                         SalProtoRtpAvp,SalAudio);
755                 if (stream && stream->dir!=SalStreamInactive){
756                         MSSndCard *playcard=lc->sound_conf.lsd_card ? 
757                                 lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
758                         MSSndCard *captcard=lc->sound_conf.capt_sndcard;
759                         const char *playfile=lc->play_file;
760                         const char *recfile=lc->rec_file;
761                         call->audio_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
762                         if (used_pt!=-1){
763                                 if (playcard==NULL) {
764                                         ms_warning("No card defined for playback !");
765                                 }
766                                 if (captcard==NULL) {
767                                         ms_warning("No card defined for capture !");
768                                 }
769                                 /*Replace soundcard filters by inactive file players or recorders
770                                  when placed in recvonly or sendonly mode*/
771                                 if (stream->port==0 || stream->dir==SalStreamRecvOnly){
772                                         captcard=NULL;
773                                         playfile=NULL;
774                                 }else if (stream->dir==SalStreamSendOnly || send_early_media){
775                                         playcard=NULL;
776                                         captcard=NULL;
777                                         recfile=NULL;
778                                         if (send_early_media)
779                                                 playfile=NULL;
780                                 }
781                                 /*if playfile are supplied don't use soundcards*/
782                                 if (lc->use_files) {
783                                         captcard=NULL;
784                                         playcard=NULL;
785                                 }
786                                 audio_stream_start_full(
787                                         call->audiostream,
788                                         call->audio_profile,
789                                         stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
790                                         stream->port,
791                                         stream->port+1,
792                                         used_pt,
793                                         jitt_comp,
794                                         playfile,
795                                         recfile,
796                                         playcard,
797                                         captcard,
798                                         send_early_media ? FALSE : linphone_core_echo_cancellation_enabled(lc));
799                                 post_configure_audio_streams(call);
800                                 if (send_early_media) setup_ring_player(lc,call);
801                                 audio_stream_set_rtcp_information(call->audiostream, cname, tool);
802                         }else ms_warning("No audio stream accepted ?");
803                 }
804         }
805 #ifdef VIDEO_ENABLED
806         if (!send_early_media){
807                 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
808                                                         SalProtoRtpAvp,SalVideo);
809                 used_pt=-1;
810                 /* shutdown preview */
811                 if (lc->previewstream!=NULL) {
812                         video_preview_stop(lc->previewstream);
813                         lc->previewstream=NULL;
814                 }
815                 if (stream && stream->dir!=SalStreamInactive) {
816                         const char *addr=stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr;
817                         call->video_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
818                         if (used_pt!=-1){
819                                 VideoStreamDir dir=VideoStreamSendRecv;
820                                 MSWebCam *cam=lc->video_conf.device;
821                                 bool_t is_inactive=FALSE;
822
823                                 call->params.has_video=TRUE;
824                                 
825                                 video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
826                                 video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
827                                 if (lc->video_window_id!=0)
828                                         video_stream_set_native_window_id(call->videostream,lc->video_window_id);
829                                 if (lc->preview_window_id!=0)
830                                         video_stream_set_native_preview_window_id (call->videostream,lc->preview_window_id);
831                                 video_stream_use_preview_video_window (call->videostream,lc->use_preview_window);
832                                 
833                                 if (stream->dir==SalStreamSendOnly && lc->video_conf.capture ){
834                                         cam=get_nowebcam_device();
835                                         dir=VideoStreamSendOnly;
836                                 }else if (stream->dir==SalStreamRecvOnly && lc->video_conf.display ){
837                                         dir=VideoStreamRecvOnly;
838                                 }else if (stream->dir==SalStreamSendRecv){
839                                         if (lc->video_conf.display && lc->video_conf.capture)
840                                                 dir=VideoStreamSendRecv;
841                                         else if (lc->video_conf.display)
842                                                 dir=VideoStreamRecvOnly;
843                                         else
844                                                 dir=VideoStreamSendOnly;
845                                 }else{
846                                         ms_warning("video stream is inactive.");
847                                         /*either inactive or incompatible with local capabilities*/
848                                         is_inactive=TRUE;
849                                 }
850                                 if (call->camera_active==FALSE){
851                                         cam=get_nowebcam_device();
852                                 }
853                                 if (!is_inactive){
854                                         video_stream_set_direction (call->videostream, dir);
855                                         video_stream_start(call->videostream,
856                                                 call->video_profile, addr, stream->port,
857                                                 stream->port+1,
858                                                 used_pt, jitt_comp, cam);
859                                         video_stream_set_rtcp_information(call->videostream, cname,tool);
860                                 }
861                         }else ms_warning("No video stream accepted.");
862                 }else{
863                         ms_warning("No valid video stream defined.");
864                 }
865         }
866 #endif
867         goto end;
868         end:
869                 ms_free(cname);
870                 linphone_address_destroy(me);
871 }
872
873
874 void linphone_call_start_media_streams(LinphoneCall *call){
875         _linphone_call_start_media_streams(call,FALSE);
876 }
877
878 void linphone_call_start_early_media(LinphoneCall *call){
879         _linphone_call_start_media_streams(call,TRUE);
880 }
881
882 static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
883         audio_stream_get_local_rtp_stats (st,&log->local_stats);
884 }
885
886 void linphone_call_stop_media_streams(LinphoneCall *call){
887         if (call->audiostream!=NULL) {
888                 linphone_call_log_fill_stats (call->log,call->audiostream);
889                 audio_stream_stop(call->audiostream);
890                 call->audiostream=NULL;
891         }
892 #ifdef VIDEO_ENABLED
893         if (call->videostream!=NULL){
894                 video_stream_stop(call->videostream);
895                 call->videostream=NULL;
896         }
897         
898 #endif
899         if (call->audio_profile){
900                 rtp_profile_clear_all(call->audio_profile);
901                 rtp_profile_destroy(call->audio_profile);
902                 call->audio_profile=NULL;
903         }
904         if (call->video_profile){
905                 rtp_profile_clear_all(call->video_profile);
906                 rtp_profile_destroy(call->video_profile);
907                 call->video_profile=NULL;
908         }
909 }
910