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