]> sjero.net Git - linphone/blob - coreapi/linphonecall.c
API and makefiles for ZRTP support.
[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 #include "mediastreamer2/mseventqueue.h"
36
37 #ifdef VIDEO_ENABLED
38 static MSWebCam *get_nowebcam_device(){
39         return ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),"StaticImage: Static picture");
40 }
41 #endif
42
43 static const char* get_zrtp_identifier(LinphoneCore *lc){
44         const char *confZid=lp_config_get_string(lc->config,"rtp","zid",NULL);
45         if (confZid != NULL) {
46                 return confZid;
47         } else {
48                 int32_t *zid=calloc(3,32);
49                 int i=0;
50                 for(;i<3;i++) {
51                         zid[i]=rand();
52                 }
53                 lp_config_set_string(lc->config,"rtp","zid",(char*)zid);
54                 return lp_config_get_string(lc->config,"rtp","zid",NULL);
55         }
56 }
57
58 const char* linphone_call_get_authentication_token(LinphoneCall *call){
59         return call->audiostream->auth_token;
60 }
61
62 bool_t linphone_call_get_authentication_token_verified(LinphoneCall *call){
63         return call->audiostream->auth_token_verified;
64 }
65 bool_t linphone_call_are_all_streams_encrypted(LinphoneCall *call) {
66         // Check ZRTP encryption in audiostream
67         if (!call->audiostream->encrypted) {
68                 return FALSE;
69         }
70
71 #ifdef VIDEO_ENABLED
72         // If video enabled, check ZRTP encryption in videostream
73         const LinphoneCallParams *params=linphone_call_get_current_params(call);
74         if (params->has_video && !call->videostream->encrypted) {
75                 return FALSE;
76         }
77 #endif
78
79         return TRUE;
80 }
81
82 static void propagate_encryption_changed(LinphoneCall *call){
83         if (call->core->vtable.call_encryption_changed == NULL) return;
84
85         if (!linphone_call_are_all_streams_encrypted(call)) {
86                 call->core->vtable.call_encryption_changed(call->core, call, FALSE, NULL);
87         } else {
88                 call->core->vtable.call_encryption_changed(call->core, call, TRUE, call->audiostream->auth_token);
89         }
90 }
91
92 #ifdef VIDEO_ENABLED
93 static void linphone_call_videostream_encryption_changed(void *data, bool_t encrypted){
94         ms_message("Video stream is %s", encrypted ? "encrypted" : "not encrypted");
95
96         LinphoneCall *call = (LinphoneCall *)data;
97         call->videostream->encrypted=encrypted;
98         propagate_encryption_changed(call);
99 }
100 #endif
101
102 static void linphone_call_audiostream_encryption_changed(void *data, bool_t encrypted) {
103         ms_message("Audio stream is %s ", encrypted ? "encrypted" : "not encrypted");
104
105         LinphoneCall *call = (LinphoneCall *)data;
106         call->audiostream->encrypted=encrypted;
107         propagate_encryption_changed(call);
108
109
110 #ifdef VIDEO_ENABLED
111         // Enable video encryption
112         const LinphoneCallParams *params=linphone_call_get_current_params(call);
113         if (params->has_video) {
114                 ms_message("Trying to enable encryption on video stream");
115                 OrtpZrtpParams params;
116                 params.zid=get_zrtp_identifier(call->core);
117                 params.zid_file=NULL; //unused
118                 OrtpZrtpUiCb cbs={0};
119                 cbs.data=call;
120                 cbs.encryption_changed=linphone_call_videostream_encryption_changed;
121                 params.ui_cbs=&cbs;
122                 video_stream_enable_zrtp(call->videostream,call->audiostream,&params);
123         }
124 #endif
125 }
126
127
128 static void linphone_call_audiostream_auth_token_ready(void *data, const char* auth_token, bool_t verified) {
129         LinphoneCall *call=(LinphoneCall *)data;
130         if (call->audiostream->auth_token != NULL)
131                 ms_free(call->audiostream->auth_token);
132
133         call->audiostream->auth_token=ms_strdup(auth_token);
134         call->audiostream->auth_token_verified=verified;
135
136         ms_message("Authentication token is %s (%s)", auth_token, verified?"verified":"unverified");
137 }
138
139
140 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandwidth_limit){
141         MSList *l=NULL;
142         const MSList *it;
143         for(it=codecs;it!=NULL;it=it->next){
144                 PayloadType *pt=(PayloadType*)it->data;
145                 if (pt->flags & PAYLOAD_TYPE_ENABLED){
146                         if (bandwidth_limit>0 && !linphone_core_is_payload_type_usable_for_bandwidth(lc,pt,bandwidth_limit)){
147                                 ms_message("Codec %s/%i eliminated because of audio bandwidth constraint.",pt->mime_type,pt->clock_rate);
148                                 continue;
149                         }
150                         if (linphone_core_check_payload_type_usability(lc,pt)){
151                                 l=ms_list_append(l,payload_type_clone(pt));
152                         }
153                 }
154         }
155         return l;
156 }
157
158 static SalMediaDescription *_create_local_media_description(LinphoneCore *lc, LinphoneCall *call, unsigned int session_id, unsigned int session_ver){
159         MSList *l;
160         PayloadType *pt;
161         const char *me=linphone_core_get_identity(lc);
162         LinphoneAddress *addr=linphone_address_new(me);
163         const char *username=linphone_address_get_username (addr);
164         SalMediaDescription *md=sal_media_description_new();
165         
166         md->session_id=session_id;
167         md->session_ver=session_ver;
168         md->nstreams=1;
169         strncpy(md->addr,call->localip,sizeof(md->addr));
170         strncpy(md->username,username,sizeof(md->username));
171         md->bandwidth=linphone_core_get_download_bandwidth(lc);
172         
173         /*set audio capabilities */
174         strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
175         md->streams[0].port=call->audio_port;
176         md->streams[0].proto=SalProtoRtpAvp;
177         md->streams[0].type=SalAudio;
178         md->streams[0].ptime=lc->net_conf.down_ptime;
179         l=make_codec_list(lc,lc->codecs_conf.audio_codecs,call->params.audio_bw);
180         pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
181         l=ms_list_append(l,pt);
182         md->streams[0].payloads=l;
183         
184
185         if (call->params.has_video){
186                 md->nstreams++;
187                 md->streams[1].port=call->video_port;
188                 md->streams[1].proto=SalProtoRtpAvp;
189                 md->streams[1].type=SalVideo;
190                 l=make_codec_list(lc,lc->codecs_conf.video_codecs,0);
191                 md->streams[1].payloads=l;
192         }
193         linphone_address_destroy(addr);
194         return md;
195 }
196
197 void update_local_media_description(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription **md){
198         if (*md == NULL) {
199                 *md = _create_local_media_description(lc,call,0,0);
200         } else {
201                 unsigned int id = (*md)->session_id;
202                 unsigned int ver = (*md)->session_ver+1;
203                 sal_media_description_unref(*md);
204                 *md = _create_local_media_description(lc,call,id,ver);
205         }
206 }
207
208 SalMediaDescription *create_local_media_description(LinphoneCore *lc, LinphoneCall *call){
209         unsigned int id=rand() & 0xfff;
210         return _create_local_media_description(lc,call,id,id);
211 }
212
213 static int find_port_offset(LinphoneCore *lc){
214         int offset;
215         MSList *elem;
216         int audio_port;
217         bool_t already_used=FALSE;
218         for(offset=0;offset<100;offset+=2){
219                 audio_port=linphone_core_get_audio_port (lc)+offset;
220                 already_used=FALSE;
221                 for(elem=lc->calls;elem!=NULL;elem=elem->next){
222                         LinphoneCall *call=(LinphoneCall*)elem->data;
223                         if (call->audio_port==audio_port) {
224                                 already_used=TRUE;
225                                 break;
226                         }
227                 }
228                 if (!already_used) break;
229         }
230         if (offset==100){
231                 ms_error("Could not find any free port !");
232                 return -1;
233         }
234         return offset;
235 }
236
237 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
238         int port_offset;
239         call->refcnt=1;
240         call->state=LinphoneCallIdle;
241         call->start_time=time(NULL);
242         call->media_start_time=0;
243         call->log=linphone_call_log_new(call, from, to);
244         call->owns_call_log=TRUE;
245         linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
246         port_offset=find_port_offset (call->core);
247         if (port_offset==-1) return;
248         call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
249         call->video_port=linphone_core_get_video_port(call->core)+port_offset;
250         
251 }
252
253 static void discover_mtu(LinphoneCore *lc, const char *remote){
254         int mtu;
255         if (lc->net_conf.mtu==0 ){
256                 /*attempt to discover mtu*/
257                 mtu=ms_discover_mtu(remote);
258                 if (mtu>0){
259                         ms_set_mtu(mtu);
260                         ms_message("Discovered mtu is %i, RTP payload max size is %i",
261                                 mtu, ms_get_payload_max_size());
262                 }
263         }
264 }
265
266 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, const LinphoneCallParams *params)
267 {
268         LinphoneCall *call=ms_new0(LinphoneCall,1);
269         call->dir=LinphoneCallOutgoing;
270         call->op=sal_op_new(lc->sal);
271         sal_op_set_user_pointer(call->op,call);
272         call->core=lc;
273         linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
274         linphone_call_init_common(call,from,to);
275         call->params=*params;
276         call->localdesc=create_local_media_description (lc,call);
277         call->camera_active=params->has_video;
278         if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
279                 linphone_core_run_stun_tests(call->core,call);
280         discover_mtu(lc,linphone_address_get_domain (to));
281         if (params->referer){
282                 sal_call_set_referer (call->op,params->referer->op);
283         }
284         return call;
285 }
286
287 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
288         LinphoneCall *call=ms_new0(LinphoneCall,1);
289         char *from_str;
290
291         call->dir=LinphoneCallIncoming;
292         sal_op_set_user_pointer(op,call);
293         call->op=op;
294         call->core=lc;
295
296         if (lc->sip_conf.ping_with_options){
297                 /*the following sends an option request back to the caller so that
298                  we get a chance to discover our nat'd address before answering.*/
299                 call->ping_op=sal_op_new(lc->sal);
300                 from_str=linphone_address_as_string(from);
301                 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
302                 sal_op_set_user_pointer(call->ping_op,call);
303                 sal_ping(call->ping_op,linphone_core_find_best_identity(lc,from,NULL),from_str);
304                 ms_free(from_str);
305         }
306         
307         linphone_address_clean(from);
308         linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
309         linphone_call_init_common(call, from, to);
310         call->params.has_video=linphone_core_video_enabled(lc);
311         call->localdesc=create_local_media_description (lc,call);
312         call->camera_active=call->params.has_video;
313         if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
314                 linphone_core_run_stun_tests(call->core,call);
315         discover_mtu(lc,linphone_address_get_domain(from));
316         return call;
317 }
318
319 /* this function is called internally to get rid of a call.
320  It performs the following tasks:
321  - remove the call from the internal list of calls
322  - update the call logs accordingly
323 */
324
325 static void linphone_call_set_terminated(LinphoneCall *call){
326         LinphoneCore *lc=call->core;
327         
328         linphone_core_update_allocated_audio_bandwidth(lc);
329
330         call->owns_call_log=FALSE;
331         linphone_call_log_completed(call);
332         
333         
334         if (call == lc->current_call){
335                 ms_message("Resetting the current call");
336                 lc->current_call=NULL;
337         }
338
339         if (linphone_core_del_call(lc,call) != 0){
340                 ms_error("Could not remove the call from the list !!!");
341         }
342         
343         if (ms_list_size(lc->calls)==0)
344                 linphone_core_notify_all_friends(lc,lc->presence_mode);
345         
346 }
347
348 const char *linphone_call_state_to_string(LinphoneCallState cs){
349         switch (cs){
350                 case LinphoneCallIdle:
351                         return "LinphoneCallIdle";
352                 case LinphoneCallIncomingReceived:
353                         return "LinphoneCallIncomingReceived";
354                 case LinphoneCallOutgoingInit:
355                         return "LinphoneCallOutgoingInit";
356                 case LinphoneCallOutgoingProgress:
357                         return "LinphoneCallOutgoingProgress";
358                 case LinphoneCallOutgoingRinging:
359                         return "LinphoneCallOutgoingRinging";
360                 case LinphoneCallOutgoingEarlyMedia:
361                         return "LinphoneCallOutgoingEarlyMedia";
362                 case LinphoneCallConnected:
363                         return "LinphoneCallConnected";
364                 case LinphoneCallStreamsRunning:
365                         return "LinphoneCallStreamsRunning";
366                 case LinphoneCallPausing:
367                         return "LinphoneCallPausing";
368                 case LinphoneCallPaused:
369                         return "LinphoneCallPaused";
370                 case LinphoneCallResuming:
371                         return "LinphoneCallResuming";
372                 case LinphoneCallRefered:
373                         return "LinphoneCallRefered";
374                 case LinphoneCallError:
375                         return "LinphoneCallError";
376                 case LinphoneCallEnd:
377                         return "LinphoneCallEnd";
378                 case LinphoneCallPausedByRemote:
379                         return "LinphoneCallPausedByRemote";
380                 case LinphoneCallUpdatedByRemote:
381                         return "LinphoneCallUpdatedByRemote";
382                 case LinphoneCallIncomingEarlyMedia:
383                         return "LinphoneCallIncomingEarlyMedia";
384                 case LinphoneCallUpdated:
385                         return "LinphoneCallUpdated";
386                 case LinphoneCallReleased:
387                         return "LinphoneCallReleased";
388         }
389         return "undefined state";
390 }
391
392 void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const char *message){
393         LinphoneCore *lc=call->core;
394
395         if (call->state!=cstate){
396                 if (call->state==LinphoneCallEnd || call->state==LinphoneCallError){
397                         if (cstate!=LinphoneCallReleased){
398                                 ms_warning("Spurious call state change from %s to %s, ignored.",linphone_call_state_to_string(call->state),
399                            linphone_call_state_to_string(cstate));
400                                 return;
401                         }
402                 }
403                 ms_message("Call %p: moving from state %s to %s",call,linphone_call_state_to_string(call->state),
404                            linphone_call_state_to_string(cstate));
405                 if (cstate!=LinphoneCallRefered){
406                         /*LinphoneCallRefered is rather an event, not a state.
407                          Indeed it does not change the state of the call (still paused or running)*/
408                         call->state=cstate;
409                 }
410                 if (cstate==LinphoneCallEnd || cstate==LinphoneCallError){
411              if (call->reason==LinphoneReasonDeclined){
412                  call->log->status=LinphoneCallDeclined;
413              }
414             linphone_call_set_terminated (call);
415                 }
416         if (cstate == LinphoneCallConnected) {
417             call->log->status=LinphoneCallSuccess;
418         }
419                 
420                 if (lc->vtable.call_state_changed)
421                         lc->vtable.call_state_changed(lc,call,cstate,message);
422                 if (cstate==LinphoneCallReleased){
423                         if (call->op!=NULL) {
424                                 /* so that we cannot have anymore upcalls for SAL
425                                  concerning this call*/
426                                 sal_op_release(call->op);
427                                 call->op=NULL;
428                         }
429                         linphone_call_unref(call);
430                 }
431         }
432 }
433
434 static void linphone_call_destroy(LinphoneCall *obj)
435 {
436         if (obj->op!=NULL) {
437                 sal_op_release(obj->op);
438                 obj->op=NULL;
439         }
440         if (obj->resultdesc!=NULL) {
441                 sal_media_description_unref(obj->resultdesc);
442                 obj->resultdesc=NULL;
443         }
444         if (obj->localdesc!=NULL) {
445                 sal_media_description_unref(obj->localdesc);
446                 obj->localdesc=NULL;
447         }
448         if (obj->ping_op) {
449                 sal_op_release(obj->ping_op);
450         }
451         if (obj->refer_to){
452                 ms_free(obj->refer_to);
453         }
454         if (obj->owns_call_log)
455                 linphone_call_log_destroy(obj->log);
456         ms_free(obj);
457 }
458
459 /**
460  * @addtogroup call_control
461  * @{
462 **/
463
464 /**
465  * Increments the call 's reference count.
466  * An application that wishes to retain a pointer to call object
467  * must use this function to unsure the pointer remains
468  * valid. Once the application no more needs this pointer,
469  * it must call linphone_call_unref().
470 **/
471 void linphone_call_ref(LinphoneCall *obj){
472         obj->refcnt++;
473 }
474
475 /**
476  * Decrements the call object reference count.
477  * See linphone_call_ref().
478 **/
479 void linphone_call_unref(LinphoneCall *obj){
480         obj->refcnt--;
481         if (obj->refcnt==0){
482                 linphone_call_destroy(obj);
483         }
484 }
485
486 /**
487  * Returns current parameters associated to the call.
488 **/
489 const LinphoneCallParams * linphone_call_get_current_params(const LinphoneCall *call){
490         return &call->current_params;
491 }
492
493 /**
494  * Returns the remote address associated to this call
495  *
496 **/
497 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
498         return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
499 }
500
501 /**
502  * Returns the remote address associated to this call as a string.
503  *
504  * The result string must be freed by user using ms_free().
505 **/
506 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
507         return linphone_address_as_string(linphone_call_get_remote_address(call));
508 }
509
510 /**
511  * Retrieves the call's current state.
512 **/
513 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
514         return call->state;
515 }
516
517 /**
518  * Returns the reason for a call termination (either error or normal termination)
519 **/
520 LinphoneReason linphone_call_get_reason(const LinphoneCall *call){
521         return call->reason;
522 }
523
524 /**
525  * Get the user_pointer in the LinphoneCall
526  *
527  * @ingroup call_control
528  *
529  * return user_pointer an opaque user pointer that can be retrieved at any time
530 **/
531 void *linphone_call_get_user_pointer(LinphoneCall *call)
532 {
533         return call->user_pointer;
534 }
535
536 /**
537  * Set the user_pointer in the LinphoneCall
538  *
539  * @ingroup call_control
540  *
541  * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
542 **/
543 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
544 {
545         call->user_pointer = user_pointer;
546 }
547
548 /**
549  * Returns the call log associated to this call.
550 **/
551 LinphoneCallLog *linphone_call_get_call_log(const LinphoneCall *call){
552         return call->log;
553 }
554
555 /**
556  * Returns the refer-to uri (if the call was transfered).
557 **/
558 const char *linphone_call_get_refer_to(const LinphoneCall *call){
559         return call->refer_to;
560 }
561
562 /**
563  * Returns direction of the call (incoming or outgoing).
564 **/
565 LinphoneCallDir linphone_call_get_dir(const LinphoneCall *call){
566         return call->log->dir;
567 }
568
569 /**
570  * Returns the far end's user agent description string, if available.
571 **/
572 const char *linphone_call_get_remote_user_agent(LinphoneCall *call){
573         if (call->op){
574                 return sal_op_get_remote_ua (call->op);
575         }
576         return NULL;
577 }
578
579 /**
580  * Returns true if this calls has received a transfer that has not been
581  * executed yet.
582  * Pending transfers are executed when this call is being paused or closed,
583  * locally or by remote endpoint.
584  * If the call is already paused while receiving the transfer request, the 
585  * transfer immediately occurs.
586 **/
587 bool_t linphone_call_has_transfer_pending(const LinphoneCall *call){
588         return call->refer_pending;
589 }
590
591 /**
592  * Returns call's duration in seconds.
593 **/
594 int linphone_call_get_duration(const LinphoneCall *call){
595         if (call->media_start_time==0) return 0;
596         return time(NULL)-call->media_start_time;
597 }
598
599 /**
600  * Returns the call object this call is replacing, if any.
601  * Call replacement can occur during call transfers.
602  * By default, the core automatically terminates the replaced call and accept the new one.
603  * This function allows the application to know whether a new incoming call is a one that replaces another one.
604 **/
605 LinphoneCall *linphone_call_get_replaced_call(LinphoneCall *call){
606         SalOp *op=sal_call_get_replaces(call->op);
607         if (op){
608                 return (LinphoneCall*)sal_op_get_user_pointer(op);
609         }
610         return NULL;
611 }
612
613 /**
614  * Indicate whether camera input should be sent to remote end.
615 **/
616 void linphone_call_enable_camera (LinphoneCall *call, bool_t enable){
617 #ifdef VIDEO_ENABLED
618         if (call->videostream!=NULL && call->videostream->ticker!=NULL){
619                 LinphoneCore *lc=call->core;
620                 MSWebCam *nowebcam=get_nowebcam_device();
621                 if (call->camera_active!=enable && lc->video_conf.device!=nowebcam){
622                         video_stream_change_camera(call->videostream,
623                                      enable ? lc->video_conf.device : nowebcam);
624                 }
625         }
626         call->camera_active=enable;
627 #endif
628 }
629
630 /**
631  * Take a photo of currently received video and write it into a jpeg file.
632 **/
633 int linphone_call_take_video_snapshot(LinphoneCall *call, const char *file){
634 #ifdef VIDEO_ENABLED
635         if (call->videostream!=NULL && call->videostream->jpegwriter!=NULL){
636                 return ms_filter_call_method(call->videostream->jpegwriter,MS_JPEG_WRITER_TAKE_SNAPSHOT,(void*)file);
637         }
638         ms_warning("Cannot take snapshot: no currently running video stream on this call.");
639         return -1;
640 #endif
641         return -1;
642 }
643
644 /**
645  * Returns TRUE if camera pictures are sent to the remote party.
646 **/
647 bool_t linphone_call_camera_enabled (const LinphoneCall *call){
648         return call->camera_active;
649 }
650
651 /**
652  * Enable video stream.
653 **/
654 void linphone_call_params_enable_video(LinphoneCallParams *cp, bool_t enabled){
655         cp->has_video=enabled;
656 }
657
658 /**
659  * Returns whether video is enabled.
660 **/
661 bool_t linphone_call_params_video_enabled(const LinphoneCallParams *cp){
662         return cp->has_video;
663 }
664
665 /**
666  * Enable sending of real early media (during outgoing calls).
667 **/
668 void linphone_call_params_enable_early_media_sending(LinphoneCallParams *cp, bool_t enabled){
669         cp->real_early_media=enabled;
670 }
671
672 bool_t linphone_call_params_early_media_sending_enabled(const LinphoneCallParams *cp){
673         return cp->real_early_media;
674 }
675
676 /**
677  * Refine bandwidth settings for this call by setting a bandwidth limit for audio streams.
678  * As a consequence, codecs whose bitrates are not compatible with this limit won't be used.
679 **/
680 void linphone_call_params_set_audio_bandwidth_limit(LinphoneCallParams *cp, int bandwidth){
681         cp->audio_bw=bandwidth;
682 }
683
684 #ifdef VIDEO_ENABLED
685 /**
686  * Request remote side to send us a Video Fast Update.
687 **/
688 void linphone_call_send_vfu_request(LinphoneCall *call)
689 {
690         if (LinphoneCallStreamsRunning == linphone_call_get_state(call))
691                 sal_call_send_vfu_request(call->op);
692 }
693 #endif
694
695 /**
696  *
697 **/
698 LinphoneCallParams * linphone_call_params_copy(const LinphoneCallParams *cp){
699         LinphoneCallParams *ncp=ms_new0(LinphoneCallParams,1);
700         memcpy(ncp,cp,sizeof(LinphoneCallParams));
701         return ncp;
702 }
703
704 /**
705  *
706 **/
707 void linphone_call_params_destroy(LinphoneCallParams *p){
708         ms_free(p);
709 }
710
711 /**
712  * @}
713 **/
714
715
716 #ifdef TEST_EXT_RENDERER
717 static void rendercb(void *data, const MSPicture *local, const MSPicture *remote){
718         ms_message("rendercb, local buffer=%p, remote buffer=%p",
719                    local ? local->planes[0] : NULL, remote? remote->planes[0] : NULL);
720 }
721 #endif
722
723 #ifdef VIDEO_ENABLED
724 static void video_stream_event_cb(void *user_pointer, const MSFilter *f, const unsigned int event_id, const void *args){
725         ms_warning("In linphonecall.c: video_stream_event_cb");
726         switch (event_id) {
727                 case MS_VIDEO_DECODER_DECODING_ERRORS:
728                         ms_warning("Case is MS_VIDEO_DECODER_DECODING_ERRORS");
729                         linphone_call_send_vfu_request((LinphoneCall*) user_pointer);
730                         break;
731                 default:
732                         ms_warning("Unhandled event %i", event_id);
733                         break;
734         }
735 }
736 #endif
737
738 void linphone_call_init_media_streams(LinphoneCall *call){
739         LinphoneCore *lc=call->core;
740         SalMediaDescription *md=call->localdesc;
741         AudioStream *audiostream;
742         
743         call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
744         if (linphone_core_echo_limiter_enabled(lc)){
745                 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
746                 if (strcasecmp(type,"mic")==0)
747                         audio_stream_enable_echo_limiter(audiostream,ELControlMic);
748                 else if (strcasecmp(type,"full")==0)
749                         audio_stream_enable_echo_limiter(audiostream,ELControlFull);
750         }
751         audio_stream_enable_gain_control(audiostream,TRUE);
752         if (linphone_core_echo_cancellation_enabled(lc)){
753                 int len,delay,framesize;
754                 const char *statestr=lp_config_get_string(lc->config,"sound","ec_state",NULL);
755                 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
756                 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
757                 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
758                 audio_stream_set_echo_canceller_params(audiostream,len,delay,framesize);
759                 if (statestr && audiostream->ec){
760                         ms_filter_call_method(audiostream->ec,MS_ECHO_CANCELLER_SET_STATE_STRING,(void*)statestr);
761                 }
762         }
763         audio_stream_enable_automatic_gain_control(audiostream,linphone_core_agc_enabled(lc));
764         {
765                 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
766                 audio_stream_enable_noise_gate(audiostream,enabled);
767         }
768         
769         if (lc->a_rtp)
770                 rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
771
772 #ifdef VIDEO_ENABLED
773
774         if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
775                 call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
776         if( lc->video_conf.displaytype != NULL)
777                 video_stream_set_display_filter_name(call->videostream,lc->video_conf.displaytype);
778         video_stream_set_event_callback(call->videostream,video_stream_event_cb, call);
779         if (lc->v_rtp)
780                 rtp_session_set_transports(call->videostream->session,lc->v_rtp,lc->v_rtcp);
781 #ifdef TEST_EXT_RENDERER
782                 video_stream_set_render_callback(call->videostream,rendercb,NULL);
783 #endif
784         }
785 #else
786         call->videostream=NULL;
787 #endif
788 }
789
790
791 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
792
793 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
794         LinphoneCore* lc = (LinphoneCore*)user_data;
795         if (dtmf<0 || dtmf>15){
796                 ms_warning("Bad dtmf value %i",dtmf);
797                 return;
798         }
799         if (lc->vtable.dtmf_received != NULL)
800                 lc->vtable.dtmf_received(lc, linphone_core_get_current_call(lc), dtmf_tab[dtmf]);
801 }
802
803 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
804         if (st->equalizer){
805                 MSFilter *f=st->equalizer;
806                 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
807                 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
808                 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
809                 if (enabled){
810                         if (gains){
811                                 do{
812                                         int bytes;
813                                         MSEqualizerGain g;
814                                         if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
815                                                 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
816                                                 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
817                                                 gains+=bytes;
818                                         }else break;
819                                 }while(1);
820                         }
821                 }
822         }
823 }
824
825
826 static void post_configure_audio_streams(LinphoneCall*call){
827         AudioStream *st=call->audiostream;
828         LinphoneCore *lc=call->core;
829         float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
830         float thres = 0;
831         float recv_gain;
832         float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
833         float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
834         int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
835         
836         if (!call->audio_muted)
837                 audio_stream_set_mic_gain(st,mic_gain);
838         else 
839                 audio_stream_set_mic_gain(st,0);
840
841         recv_gain = lc->sound_conf.soft_play_lev;
842         if (recv_gain != 0) {
843                 linphone_core_set_playback_gain_db (lc,recv_gain);
844         }
845         if (st->volsend){
846                 ms_filter_call_method(st->volsend,MS_VOLUME_REMOVE_DC,&dc_removal);
847                 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
848                 thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
849                 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
850                 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
851                 float transmit_thres=lp_config_get_float(lc->config,"sound","el_transmit_thres",-1);
852                 MSFilter *f=NULL;
853                 f=st->volsend;
854                 if (speed==-1) speed=0.03;
855                 if (force==-1) force=25;
856                 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
857                 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
858                 if (thres!=-1)
859                         ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
860                 if (sustain!=-1)
861                         ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
862                 if (transmit_thres!=-1)
863                                 ms_filter_call_method(f,MS_VOLUME_SET_EA_TRANSMIT_THRESHOLD,&transmit_thres);
864
865                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
866                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
867         }
868         if (st->volrecv){
869                 /* parameters for a limited noise-gate effect, using echo limiter threshold */
870                 float floorgain = 1/mic_gain;
871                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&thres);
872                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
873         }
874         parametrize_equalizer(lc,st);
875         if (lc->vtable.dtmf_received!=NULL){
876                 /* replace by our default action*/
877                 audio_stream_play_received_dtmfs(call->audiostream,FALSE);
878                 rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
879         }
880 }
881
882 static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
883         int bw;
884         const MSList *elem;
885         RtpProfile *prof=rtp_profile_new("Call profile");
886         bool_t first=TRUE;
887         int remote_bw=0;
888         LinphoneCore *lc=call->core;
889         int up_ptime=0;
890         *used_pt=-1;
891         
892         for(elem=desc->payloads;elem!=NULL;elem=elem->next){
893                 PayloadType *pt=(PayloadType*)elem->data;
894                 int number;
895                 
896                 if ((pt->flags & PAYLOAD_TYPE_FLAG_CAN_SEND) && first) {
897                         if (desc->type==SalAudio){
898                                 linphone_core_update_allocated_audio_bandwidth_in_call(call,pt);
899                                 up_ptime=linphone_core_get_upload_ptime(lc);
900                         }
901                         *used_pt=payload_type_get_number(pt);
902                         first=FALSE;
903                 }
904                 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
905                 else if (md->bandwidth>0) {
906                         /*case where b=AS is given globally, not per stream*/
907                         remote_bw=md->bandwidth;
908                         if (desc->type==SalVideo){
909                                 remote_bw=get_video_bandwidth(remote_bw,call->audio_bw);
910                         }
911                 }
912                 
913                 if (desc->type==SalAudio){                      
914                                 bw=get_min_bandwidth(call->audio_bw,remote_bw);
915                 }else bw=get_min_bandwidth(get_video_bandwidth(linphone_core_get_upload_bandwidth (lc),call->audio_bw),remote_bw);
916                 if (bw>0) pt->normal_bitrate=bw*1000;
917                 else if (desc->type==SalAudio){
918                         pt->normal_bitrate=-1;
919                 }
920                 if (desc->ptime>0){
921                         up_ptime=desc->ptime;
922                 }
923                 if (up_ptime>0){
924                         char tmp[40];
925                         snprintf(tmp,sizeof(tmp),"ptime=%i",up_ptime);
926                         payload_type_append_send_fmtp(pt,tmp);
927                 }
928                 number=payload_type_get_number(pt);
929                 if (rtp_profile_get_payload(prof,number)!=NULL){
930                         ms_warning("A payload type with number %i already exists in profile !",number);
931                 }else
932                         rtp_profile_set_payload(prof,number,pt);
933         }
934         return prof;
935 }
936
937
938 static void setup_ring_player(LinphoneCore *lc, LinphoneCall *call){
939         int pause_time=3000;
940         audio_stream_play(call->audiostream,lc->sound_conf.ringback_tone);
941         ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
942 }
943
944
945 void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_muted, bool_t send_ringbacktone){
946         LinphoneCore *lc=call->core;
947         LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
948         const char *tool="linphone-" LINPHONE_VERSION;
949         char *cname;
950         int used_pt=-1;
951 #ifdef VIDEO_ENABLED
952         const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
953                                                         SalProtoRtpAvp,SalVideo);
954 #endif
955         bool_t use_arc=linphone_core_adaptive_rate_control_enabled(lc);
956         
957         if(call->audiostream == NULL)
958         {
959                 ms_fatal("start_media_stream() called without prior init !");
960                 return;
961         }
962         call->current_params = call->params;
963         /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
964         int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
965
966         if (call->media_start_time==0) call->media_start_time=time(NULL);
967
968         cname=linphone_address_as_string_uri_only(me);
969         {
970                 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
971                                                         SalProtoRtpAvp,SalAudio);
972                 if (stream && stream->dir!=SalStreamInactive && stream->port!=0){
973                         MSSndCard *playcard=lc->sound_conf.lsd_card ? 
974                                 lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
975                         MSSndCard *captcard=lc->sound_conf.capt_sndcard;
976                         const char *playfile=lc->play_file;
977                         const char *recfile=lc->rec_file;
978                         call->audio_profile=make_profile(call,call->resultdesc,stream,&used_pt);
979                         bool_t use_ec,use_arc_audio=use_arc;
980
981                         if (used_pt!=-1){
982                                 if (playcard==NULL) {
983                                         ms_warning("No card defined for playback !");
984                                 }
985                                 if (captcard==NULL) {
986                                         ms_warning("No card defined for capture !");
987                                 }
988                                 /*Replace soundcard filters by inactive file players or recorders
989                                  when placed in recvonly or sendonly mode*/
990                                 if (stream->port==0 || stream->dir==SalStreamRecvOnly){
991                                         captcard=NULL;
992                                         playfile=NULL;
993                                 }else if (stream->dir==SalStreamSendOnly){
994                                         playcard=NULL;
995                                         captcard=NULL;
996                                         recfile=NULL;
997                                         /*And we will eventually play "playfile" if set by the user*/
998                                         /*playfile=NULL;*/
999                                 }
1000                                 if (send_ringbacktone){
1001                                         captcard=NULL;
1002                                         playfile=NULL;/* it is setup later*/
1003                                 }
1004                                 /*if playfile are supplied don't use soundcards*/
1005                                 if (lc->use_files) {
1006                                         captcard=NULL;
1007                                         playcard=NULL;
1008                                 }
1009                                 use_ec=captcard==NULL ? FALSE : linphone_core_echo_cancellation_enabled(lc);
1010 #if defined(VIDEO_ENABLED)
1011                                 if (vstream && vstream->dir!=SalStreamInactive && vstream->payloads!=NULL){
1012                                         /*when video is used, do not make adaptive rate control on audio, it is stupid.*/
1013                                         use_arc_audio=FALSE;
1014         #if defined(ANDROID)
1015                                         /*On android we have to disable the echo canceller to preserve CPU for video codecs */
1016                                         use_ec=FALSE;
1017         #endif
1018                                 }
1019 #endif
1020                                 audio_stream_enable_adaptive_bitrate_control(call->audiostream,use_arc_audio);
1021                                 audio_stream_start_full(
1022                                         call->audiostream,
1023                                         call->audio_profile,
1024                                         stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
1025                                         stream->port,
1026                                         stream->port+1,
1027                                         used_pt,
1028                                         jitt_comp,
1029                                         playfile,
1030                                         recfile,
1031                                         playcard,
1032                                         captcard,
1033                                         use_ec
1034                                         );
1035                                 post_configure_audio_streams(call);
1036                                 if (all_inputs_muted && !send_ringbacktone){
1037                                         audio_stream_set_mic_gain(call->audiostream,0);
1038                                 }
1039                                 if (stream->dir==SalStreamSendOnly && playfile!=NULL){
1040                                         int pause_time=500;
1041                                         ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
1042                                 }
1043                                 if (send_ringbacktone){
1044                                         setup_ring_player(lc,call);
1045                                 }
1046                                 audio_stream_set_rtcp_information(call->audiostream, cname, tool);
1047                         }else ms_warning("No audio stream accepted ?");
1048                 }
1049         }
1050 #ifdef VIDEO_ENABLED
1051         {
1052                 
1053                 used_pt=-1;
1054                 /* shutdown preview */
1055                 if (lc->previewstream!=NULL) {
1056                         video_preview_stop(lc->previewstream);
1057                         lc->previewstream=NULL;
1058                 }
1059                 call->current_params.has_video=FALSE;
1060                 if (vstream && vstream->dir!=SalStreamInactive && vstream->port!=0) {
1061                         const char *addr=vstream->addr[0]!='\0' ? vstream->addr : call->resultdesc->addr;
1062                         call->video_profile=make_profile(call,call->resultdesc,vstream,&used_pt);
1063                         if (used_pt!=-1){
1064                                 VideoStreamDir dir=VideoStreamSendRecv;
1065                                 MSWebCam *cam=lc->video_conf.device;
1066                                 bool_t is_inactive=FALSE;
1067
1068                                 call->current_params.has_video=TRUE;
1069                                 
1070                                 video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
1071                                 video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
1072                                 if (lc->video_window_id!=0)
1073                                         video_stream_set_native_window_id(call->videostream,lc->video_window_id);
1074                                 if (lc->preview_window_id!=0)
1075                                         video_stream_set_native_preview_window_id (call->videostream,lc->preview_window_id);
1076                                 video_stream_use_preview_video_window (call->videostream,lc->use_preview_window);
1077                                 
1078                                 if (vstream->dir==SalStreamSendOnly && lc->video_conf.capture ){
1079                                         cam=get_nowebcam_device();
1080                                         dir=VideoStreamSendOnly;
1081                                 }else if (vstream->dir==SalStreamRecvOnly && lc->video_conf.display ){
1082                                         dir=VideoStreamRecvOnly;
1083                                 }else if (vstream->dir==SalStreamSendRecv){
1084                                         if (lc->video_conf.display && lc->video_conf.capture)
1085                                                 dir=VideoStreamSendRecv;
1086                                         else if (lc->video_conf.display)
1087                                                 dir=VideoStreamRecvOnly;
1088                                         else
1089                                                 dir=VideoStreamSendOnly;
1090                                 }else{
1091                                         ms_warning("video stream is inactive.");
1092                                         /*either inactive or incompatible with local capabilities*/
1093                                         is_inactive=TRUE;
1094                                 }
1095                                 if (call->camera_active==FALSE || all_inputs_muted){
1096                                         cam=get_nowebcam_device();
1097                                 }
1098                                 if (!is_inactive){
1099                                         video_stream_set_direction (call->videostream, dir);
1100                                         video_stream_start(call->videostream,
1101                                                 call->video_profile, addr, vstream->port,
1102                                                 vstream->port+1,
1103                                                 used_pt, jitt_comp, cam);
1104                                         video_stream_set_rtcp_information(call->videostream, cname,tool);
1105                                 }
1106                         }else ms_warning("No video stream accepted.");
1107                 }else{
1108                         ms_warning("No valid video stream defined.");
1109                 }
1110         }
1111 #endif
1112         call->all_muted=all_inputs_muted;
1113         call->playing_ringbacktone=send_ringbacktone;
1114         call->up_bw=linphone_core_get_upload_bandwidth(lc);
1115         
1116         if (ortp_zrtp_available()) {
1117                 OrtpZrtpParams params;
1118                 params.zid=get_zrtp_identifier(lc);
1119                 params.zid_file=lc->zrtp_secrets_cache;
1120                 OrtpZrtpUiCb cbs={0};
1121                 cbs.data=call;
1122                 cbs.encryption_changed=linphone_call_audiostream_encryption_changed;
1123                 cbs.sas_ready=linphone_call_audiostream_auth_token_ready;
1124                 params.ui_cbs=&cbs;
1125                 audio_stream_enable_zrtp(call->audiostream,&params);
1126         }
1127
1128         goto end;
1129         end:
1130                 ms_free(cname);
1131                 linphone_address_destroy(me);
1132 }
1133
1134 static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
1135         audio_stream_get_local_rtp_stats (st,&log->local_stats);
1136         log->quality=audio_stream_get_average_quality_rating(st);
1137 }
1138
1139 void linphone_call_stop_media_streams(LinphoneCall *call){
1140         if (call->audiostream!=NULL) {
1141                 if (call->audiostream->ec){
1142                         const char *state_str=NULL;
1143                         ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_STATE_STRING,&state_str);
1144                         if (state_str){
1145                                 ms_message("Writing echo canceller state, %i bytes",strlen(state_str));
1146                                 lp_config_set_string(call->core->config,"sound","ec_state",state_str);
1147                         }
1148                 }
1149                 linphone_call_log_fill_stats (call->log,call->audiostream);
1150                 audio_stream_stop(call->audiostream);
1151                 call->audiostream=NULL;
1152         }
1153 #ifdef VIDEO_ENABLED
1154         if (call->videostream!=NULL){
1155                 video_stream_stop(call->videostream);
1156                 call->videostream=NULL;
1157         }
1158         ms_event_queue_skip(call->core->msevq);
1159         
1160 #endif
1161         if (call->audio_profile){
1162                 rtp_profile_clear_all(call->audio_profile);
1163                 rtp_profile_destroy(call->audio_profile);
1164                 call->audio_profile=NULL;
1165         }
1166         if (call->video_profile){
1167                 rtp_profile_clear_all(call->video_profile);
1168                 rtp_profile_destroy(call->video_profile);
1169                 call->video_profile=NULL;
1170         }
1171 }
1172
1173
1174
1175 void linphone_call_enable_echo_cancellation(LinphoneCall *call, bool_t enable) {
1176         if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1177                 bool_t bypass_mode = !enable;
1178                 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_SET_BYPASS_MODE,&bypass_mode);
1179         }
1180 }
1181 bool_t linphone_call_echo_cancellation_enabled(LinphoneCall *call) {
1182         if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1183                 bool_t val;
1184                 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_BYPASS_MODE,&val);
1185                 return !val;
1186         } else {
1187                 return linphone_core_echo_cancellation_enabled(call->core);
1188         }
1189 }
1190
1191 void linphone_call_enable_echo_limiter(LinphoneCall *call, bool_t val){
1192         if (call!=NULL && call->audiostream!=NULL ) {
1193                 if (val) {
1194                 const char *type=lp_config_get_string(call->core->config,"sound","el_type","mic");
1195                 if (strcasecmp(type,"mic")==0)
1196                         audio_stream_enable_echo_limiter(call->audiostream,ELControlMic);
1197                 else if (strcasecmp(type,"full")==0)
1198                         audio_stream_enable_echo_limiter(call->audiostream,ELControlFull);
1199                 } else {
1200                         audio_stream_enable_echo_limiter(call->audiostream,ELInactive);
1201                 }
1202         }
1203 }
1204
1205 bool_t linphone_call_echo_limiter_enabled(const LinphoneCall *call){
1206         if (call!=NULL && call->audiostream!=NULL ){
1207                 return call->audiostream->el_type !=ELInactive ;
1208         } else {
1209                 return linphone_core_echo_limiter_enabled(call->core);
1210         }
1211 }
1212
1213 /**
1214  * @addtogroup call_misc
1215  * @{
1216 **/ 
1217
1218 /**
1219  * Returns the measured sound volume played locally (received from remote)
1220  * It is expressed in dbm0. 
1221 **/
1222 float linphone_call_get_play_volume(LinphoneCall *call){
1223         AudioStream *st=call->audiostream;
1224         if (st && st->volsend){
1225                 float vol=0;
1226                 ms_filter_call_method(st->volsend,MS_VOLUME_GET,&vol);
1227                 return vol;
1228                 
1229         }
1230         return LINPHONE_VOLUME_DB_LOWEST;
1231 }
1232
1233 /**
1234  * Returns the measured sound volume recorded locally (sent to remote)
1235  * It is expressed in dbm0. 
1236 **/
1237 float linphone_call_get_record_volume(LinphoneCall *call){
1238         AudioStream *st=call->audiostream;
1239         if (st && st->volrecv){
1240                 float vol=0;
1241                 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&vol);
1242                 return vol;
1243                 
1244         }
1245         return LINPHONE_VOLUME_DB_LOWEST;
1246 }
1247
1248 /**
1249  * Obtain real-time quality rating of the call
1250  *
1251  * Based on local RTP statistics and RTCP feedback, a quality rating is computed and updated
1252  * during all the duration of the call. This function returns its value at the time of the function call.
1253  * It is expected that the rating is updated at least every 5 seconds or so.
1254  * The rating is a floating point number comprised between 0 and 5.
1255  *
1256  * 4-5 = good quality <br>
1257  * 3-4 = average quality <br>
1258  * 2-3 = poor quality <br>
1259  * 1-2 = very poor quality <br>
1260  * 0-1 = can't be worse, mostly unusable <br>
1261  *
1262  * @returns The function returns -1 if no quality measurement is available, for example if no 
1263  * active audio stream exist. Otherwise it returns the quality rating.
1264 **/
1265 float linphone_call_get_current_quality(LinphoneCall *call){
1266         if (call->audiostream){
1267                 return audio_stream_get_quality_rating(call->audiostream);
1268         }
1269         return -1;
1270 }
1271
1272 /**
1273  * Returns call quality averaged over all the duration of the call.
1274  *
1275  * See linphone_call_get_current_quality() for more details about quality measurement.
1276 **/
1277 float linphone_call_get_average_quality(LinphoneCall *call){
1278         if (call->audiostream){
1279                 return audio_stream_get_average_quality_rating(call->audiostream);
1280         }
1281         return -1;
1282 }
1283
1284 /**
1285  * @}
1286 **/
1287
1288 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1289         ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1290         (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1291         (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1292         (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1293         (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1294 }
1295
1296 static void linphone_core_disconnected(LinphoneCore *lc, LinphoneCall *call){
1297         char temp[256];
1298         char *from=NULL;
1299         if(call)
1300                 from = linphone_call_get_remote_address_as_string(call);
1301         if (from)
1302         {
1303                 snprintf(temp,sizeof(temp),"Remote end %s seems to have disconnected, the call is going to be closed.",from);
1304                 free(from);
1305         }               
1306         else
1307         {
1308                 snprintf(temp,sizeof(temp),"Remote end seems to have disconnected, the call is going to be closed.");
1309         }
1310         if (lc->vtable.display_warning!=NULL)
1311                 lc->vtable.display_warning(lc,temp);
1312         linphone_core_terminate_call(lc,call);
1313 }
1314
1315 void linphone_call_background_tasks(LinphoneCall *call, bool_t one_second_elapsed){
1316         int disconnect_timeout = linphone_core_get_nortp_timeout(call->core);
1317         bool_t disconnected=FALSE;
1318         
1319         if (call->state==LinphoneCallStreamsRunning && one_second_elapsed){
1320                 RtpSession *as=NULL,*vs=NULL;
1321                 float audio_load=0, video_load=0;
1322                 if (call->audiostream!=NULL){
1323                         as=call->audiostream->session;
1324                         if (call->audiostream->ticker)
1325                                 audio_load=ms_ticker_get_average_load(call->audiostream->ticker);
1326                 }
1327                 if (call->videostream!=NULL){
1328                         if (call->videostream->ticker)
1329                                 video_load=ms_ticker_get_average_load(call->videostream->ticker);
1330                         vs=call->videostream->session;
1331                 }
1332                 display_bandwidth(as,vs);
1333                 ms_message("Thread processing load: audio=%f\tvideo=%f",audio_load,video_load);
1334         }
1335 #ifdef VIDEO_ENABLED
1336         if (call->videostream!=NULL)
1337                 video_stream_iterate(call->videostream);
1338 #endif
1339         if (call->audiostream!=NULL)
1340                 audio_stream_iterate(call->audiostream);
1341         if (one_second_elapsed && call->audiostream!=NULL && disconnect_timeout>0 )
1342                 disconnected=!audio_stream_alive(call->audiostream,disconnect_timeout);
1343         if (disconnected)
1344                 linphone_core_disconnected(call->core,call);
1345 }
1346
1347 void linphone_call_log_completed(LinphoneCall *call){
1348         LinphoneCore *lc=call->core;
1349         
1350         call->log->duration=time(NULL)-call->start_time;
1351         
1352         if (call->log->status==LinphoneCallMissed){
1353                 char *info;
1354                 lc->missed_calls++;
1355                 info=ortp_strdup_printf(ngettext("You have missed %i call.",
1356                                          "You have missed %i calls.", lc->missed_calls),
1357                                 lc->missed_calls);
1358         if (lc->vtable.display_status!=NULL)
1359             lc->vtable.display_status(lc,info);
1360                 ms_free(info);
1361         }
1362         lc->call_logs=ms_list_prepend(lc->call_logs,(void *)call->log);
1363         if (ms_list_size(lc->call_logs)>lc->max_call_logs){
1364                 MSList *elem,*prevelem=NULL;
1365                 /*find the last element*/
1366                 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
1367                         prevelem=elem;
1368                 }
1369                 elem=prevelem;
1370                 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
1371                 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
1372         }
1373         if (lc->vtable.call_log_updated!=NULL){
1374                 lc->vtable.call_log_updated(lc,call->log);
1375         }
1376         call_logs_write_to_config_file(lc);
1377 }
1378
1379