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