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