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