4 Copyright (C) 2010 Belledonne Communications SARL
5 (simon.morlat@linphone.org)
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.
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.
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.
24 #include "linphonecore.h"
28 #include <ortp/event.h>
32 #include "mediastreamer2/mediastream.h"
33 #include "mediastreamer2/msvolume.h"
34 #include "mediastreamer2/msequalizer.h"
35 #include "mediastreamer2/msfileplayer.h"
36 #include "mediastreamer2/msjpegwriter.h"
37 #include "mediastreamer2/mseventqueue.h"
40 static MSWebCam *get_nowebcam_device(){
41 return ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),"StaticImage: Static picture");
45 static bool_t generate_b64_crypto_key(int key_length, char* key_out) {
47 uint8_t* tmp = (uint8_t*) malloc(key_length);
48 if (ortp_crypto_get_random(tmp, key_length)!=0) {
49 ms_error("Failed to generate random key");
54 b64_size = b64_encode((const char*)tmp, key_length, NULL, 0);
56 ms_error("Failed to b64 encode key");
60 key_out[b64_size] = '\0';
61 b64_encode((const char*)tmp, key_length, key_out, 40);
66 LinphoneCore *linphone_call_get_core(const LinphoneCall *call){
70 static const char* get_hexa_zrtp_identifier(LinphoneCore *lc){
71 const char *confZid=lp_config_get_string(lc->config,"rtp","zid",NULL);
72 if (confZid != NULL) {
76 snprintf(zidstr,sizeof(zidstr),"%x-%x-%x",rand(),rand(),rand());
77 lp_config_set_string(lc->config,"rtp","zid",zidstr);
78 return lp_config_get_string(lc->config,"rtp","zid",NULL);
82 const char* linphone_call_get_authentication_token(LinphoneCall *call){
83 return call->auth_token;
86 bool_t linphone_call_get_authentication_token_verified(LinphoneCall *call){
87 return call->auth_token_verified;
89 bool_t linphone_call_are_all_streams_encrypted(LinphoneCall *call) {
90 // Check ZRTP encryption in audiostream
91 if (!call->audiostream_encrypted) {
96 // If video enabled, check ZRTP encryption in videostream
97 const LinphoneCallParams *params=linphone_call_get_current_params(call);
98 if (params->has_video && !call->videostream_encrypted) {
106 void propagate_encryption_changed(LinphoneCall *call){
107 if (call->core->vtable.call_encryption_changed == NULL) return;
109 if (!linphone_call_are_all_streams_encrypted(call)) {
110 ms_message("Some streams are not encrypted");
111 call->core->vtable.call_encryption_changed(call->core, call, FALSE, call->auth_token);
113 ms_message("All streams are encrypted");
114 call->core->vtable.call_encryption_changed(call->core, call, TRUE, call->auth_token);
119 static void linphone_call_videostream_encryption_changed(void *data, bool_t encrypted){
120 ms_message("Video stream is %s", encrypted ? "encrypted" : "not encrypted");
122 LinphoneCall *call = (LinphoneCall *)data;
123 call->videostream_encrypted=encrypted;
124 propagate_encryption_changed(call);
128 static void linphone_call_audiostream_encryption_changed(void *data, bool_t encrypted) {
129 char status[255]={0};
130 ms_message("Audio stream is %s ", encrypted ? "encrypted" : "not encrypted");
132 LinphoneCall *call = (LinphoneCall *)data;
133 call->audiostream_encrypted=encrypted;
135 if (encrypted && call->core->vtable.display_status != NULL) {
136 snprintf(status,sizeof(status)-1,_("Authentication token is %s"),call->auth_token);
137 call->core->vtable.display_status(call->core, status);
140 propagate_encryption_changed(call);
144 // Enable video encryption
145 const LinphoneCallParams *params=linphone_call_get_current_params(call);
146 if (params->has_video) {
147 ms_message("Trying to enable encryption on video stream");
148 OrtpZrtpParams params;
149 params.zid=get_hexa_zrtp_identifier(call->core);
150 params.zid_file=NULL; //unused
151 video_stream_enable_zrtp(call->videostream,call->audiostream,¶ms);
157 static void linphone_call_audiostream_auth_token_ready(void *data, const char* auth_token, bool_t verified) {
158 LinphoneCall *call=(LinphoneCall *)data;
159 if (call->auth_token != NULL)
160 ms_free(call->auth_token);
162 call->auth_token=ms_strdup(auth_token);
163 call->auth_token_verified=verified;
165 ms_message("Authentication token is %s (%s)", auth_token, verified?"verified":"unverified");
169 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandwidth_limit){
172 for(it=codecs;it!=NULL;it=it->next){
173 PayloadType *pt=(PayloadType*)it->data;
174 if (pt->flags & PAYLOAD_TYPE_ENABLED){
175 if (bandwidth_limit>0 && !linphone_core_is_payload_type_usable_for_bandwidth(lc,pt,bandwidth_limit)){
176 ms_message("Codec %s/%i eliminated because of audio bandwidth constraint.",pt->mime_type,pt->clock_rate);
179 if (linphone_core_check_payload_type_usability(lc,pt)){
180 l=ms_list_append(l,payload_type_clone(pt));
187 static SalMediaDescription *_create_local_media_description(LinphoneCore *lc, LinphoneCall *call, unsigned int session_id, unsigned int session_ver){
191 const char *me=linphone_core_get_identity(lc);
192 LinphoneAddress *addr=linphone_address_new(me);
193 const char *username=linphone_address_get_username (addr);
194 SalMediaDescription *md=sal_media_description_new();
196 md->session_id=session_id;
197 md->session_ver=session_ver;
199 strncpy(md->addr,call->localip,sizeof(md->addr));
200 strncpy(md->username,username,sizeof(md->username));
201 md->bandwidth=linphone_core_get_download_bandwidth(lc);
203 /*set audio capabilities */
204 strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
205 md->streams[0].port=call->audio_port;
206 md->streams[0].proto=(call->params.media_encryption == LinphoneMediaEncryptionSRTP) ?
207 SalProtoRtpSavp : SalProtoRtpAvp;
208 md->streams[0].type=SalAudio;
209 md->streams[0].ptime=lc->net_conf.down_ptime;
210 l=make_codec_list(lc,lc->codecs_conf.audio_codecs,call->params.audio_bw);
211 pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
212 l=ms_list_append(l,pt);
213 md->streams[0].payloads=l;
216 if (call->params.has_video){
218 md->streams[1].port=call->video_port;
219 md->streams[1].proto=md->streams[0].proto;
220 md->streams[1].type=SalVideo;
221 l=make_codec_list(lc,lc->codecs_conf.video_codecs,0);
222 md->streams[1].payloads=l;
225 for(i=0; i<md->nstreams; i++) {
226 if (md->streams[i].proto == SalProtoRtpSavp) {
227 md->streams[i].crypto[0].tag = 1;
228 md->streams[i].crypto[0].algo = AES_128_SHA1_80;
229 if (!generate_b64_crypto_key(30, md->streams[i].crypto[0].master_key))
230 md->streams[i].crypto[0].algo = 0;
231 md->streams[i].crypto[1].tag = 2;
232 md->streams[i].crypto[1].algo = AES_128_SHA1_32;
233 if (!generate_b64_crypto_key(30, md->streams[i].crypto[1].master_key))
234 md->streams[i].crypto[1].algo = 0;
235 md->streams[i].crypto[2].algo = 0;
239 linphone_address_destroy(addr);
243 void update_local_media_description(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription **md){
245 *md = _create_local_media_description(lc,call,0,0);
247 unsigned int id = (*md)->session_id;
248 unsigned int ver = (*md)->session_ver+1;
249 sal_media_description_unref(*md);
250 *md = _create_local_media_description(lc,call,id,ver);
254 SalMediaDescription *create_local_media_description(LinphoneCore *lc, LinphoneCall *call){
255 unsigned int id=rand() & 0xfff;
256 return _create_local_media_description(lc,call,id,id);
259 static int find_port_offset(LinphoneCore *lc){
263 bool_t already_used=FALSE;
264 for(offset=0;offset<100;offset+=2){
265 audio_port=linphone_core_get_audio_port (lc)+offset;
267 for(elem=lc->calls;elem!=NULL;elem=elem->next){
268 LinphoneCall *call=(LinphoneCall*)elem->data;
269 if (call->audio_port==audio_port) {
274 if (!already_used) break;
277 ms_error("Could not find any free port !");
283 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
286 call->state=LinphoneCallIdle;
287 call->start_time=time(NULL);
288 call->media_start_time=0;
289 call->log=linphone_call_log_new(call, from, to);
290 call->owns_call_log=TRUE;
291 linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
292 port_offset=find_port_offset (call->core);
293 if (port_offset==-1) return;
294 call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
295 call->video_port=linphone_core_get_video_port(call->core)+port_offset;
299 static void discover_mtu(LinphoneCore *lc, const char *remote){
301 if (lc->net_conf.mtu==0 ){
302 /*attempt to discover mtu*/
303 mtu=ms_discover_mtu(remote);
306 ms_message("Discovered mtu is %i, RTP payload max size is %i",
307 mtu, ms_get_payload_max_size());
312 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, const LinphoneCallParams *params)
314 LinphoneCall *call=ms_new0(LinphoneCall,1);
315 call->dir=LinphoneCallOutgoing;
316 call->op=sal_op_new(lc->sal);
317 sal_op_set_user_pointer(call->op,call);
319 linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
320 linphone_call_init_common(call,from,to);
321 call->params=*params;
322 call->localdesc=create_local_media_description (lc,call);
323 call->camera_active=params->has_video;
324 if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
325 linphone_core_run_stun_tests(call->core,call);
326 discover_mtu(lc,linphone_address_get_domain (to));
327 if (params->referer){
328 sal_call_set_referer (call->op,params->referer->op);
333 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
334 LinphoneCall *call=ms_new0(LinphoneCall,1);
337 call->dir=LinphoneCallIncoming;
338 sal_op_set_user_pointer(op,call);
342 if (lc->sip_conf.ping_with_options){
343 /*the following sends an option request back to the caller so that
344 we get a chance to discover our nat'd address before answering.*/
345 call->ping_op=sal_op_new(lc->sal);
346 from_str=linphone_address_as_string(from);
347 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
348 sal_op_set_user_pointer(call->ping_op,call);
349 sal_ping(call->ping_op,linphone_core_find_best_identity(lc,from,NULL),from_str);
353 linphone_address_clean(from);
354 linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
355 linphone_call_init_common(call, from, to);
356 linphone_core_init_default_params(lc, &call->params);
357 call->localdesc=create_local_media_description (lc,call);
358 call->camera_active=call->params.has_video;
359 if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
360 linphone_core_run_stun_tests(call->core,call);
361 discover_mtu(lc,linphone_address_get_domain(from));
365 /* this function is called internally to get rid of a call.
366 It performs the following tasks:
367 - remove the call from the internal list of calls
368 - update the call logs accordingly
371 static void linphone_call_set_terminated(LinphoneCall *call){
372 LinphoneCore *lc=call->core;
374 linphone_core_update_allocated_audio_bandwidth(lc);
376 call->owns_call_log=FALSE;
377 linphone_call_log_completed(call);
380 if (call == lc->current_call){
381 ms_message("Resetting the current call");
382 lc->current_call=NULL;
385 if (linphone_core_del_call(lc,call) != 0){
386 ms_error("Could not remove the call from the list !!!");
389 if (ms_list_size(lc->calls)==0)
390 linphone_core_notify_all_friends(lc,lc->presence_mode);
392 linphone_core_conference_check_uninit(&lc->conf_ctx);
393 if (call->ringing_beep){
394 linphone_core_stop_dtmf(lc);
395 call->ringing_beep=FALSE;
399 const char *linphone_call_state_to_string(LinphoneCallState cs){
401 case LinphoneCallIdle:
402 return "LinphoneCallIdle";
403 case LinphoneCallIncomingReceived:
404 return "LinphoneCallIncomingReceived";
405 case LinphoneCallOutgoingInit:
406 return "LinphoneCallOutgoingInit";
407 case LinphoneCallOutgoingProgress:
408 return "LinphoneCallOutgoingProgress";
409 case LinphoneCallOutgoingRinging:
410 return "LinphoneCallOutgoingRinging";
411 case LinphoneCallOutgoingEarlyMedia:
412 return "LinphoneCallOutgoingEarlyMedia";
413 case LinphoneCallConnected:
414 return "LinphoneCallConnected";
415 case LinphoneCallStreamsRunning:
416 return "LinphoneCallStreamsRunning";
417 case LinphoneCallPausing:
418 return "LinphoneCallPausing";
419 case LinphoneCallPaused:
420 return "LinphoneCallPaused";
421 case LinphoneCallResuming:
422 return "LinphoneCallResuming";
423 case LinphoneCallRefered:
424 return "LinphoneCallRefered";
425 case LinphoneCallError:
426 return "LinphoneCallError";
427 case LinphoneCallEnd:
428 return "LinphoneCallEnd";
429 case LinphoneCallPausedByRemote:
430 return "LinphoneCallPausedByRemote";
431 case LinphoneCallUpdatedByRemote:
432 return "LinphoneCallUpdatedByRemote";
433 case LinphoneCallIncomingEarlyMedia:
434 return "LinphoneCallIncomingEarlyMedia";
435 case LinphoneCallUpdated:
436 return "LinphoneCallUpdated";
437 case LinphoneCallReleased:
438 return "LinphoneCallReleased";
440 return "undefined state";
443 void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const char *message){
444 LinphoneCore *lc=call->core;
446 if (call->state!=cstate){
447 if (call->state==LinphoneCallEnd || call->state==LinphoneCallError){
448 if (cstate!=LinphoneCallReleased){
449 ms_warning("Spurious call state change from %s to %s, ignored.",linphone_call_state_to_string(call->state),
450 linphone_call_state_to_string(cstate));
454 ms_message("Call %p: moving from state %s to %s",call,linphone_call_state_to_string(call->state),
455 linphone_call_state_to_string(cstate));
456 if (cstate!=LinphoneCallRefered){
457 /*LinphoneCallRefered is rather an event, not a state.
458 Indeed it does not change the state of the call (still paused or running)*/
461 if (cstate==LinphoneCallEnd || cstate==LinphoneCallError){
462 if (call->reason==LinphoneReasonDeclined){
463 call->log->status=LinphoneCallDeclined;
465 linphone_call_set_terminated (call);
467 if (cstate == LinphoneCallConnected) {
468 call->log->status=LinphoneCallSuccess;
471 if (lc->vtable.call_state_changed)
472 lc->vtable.call_state_changed(lc,call,cstate,message);
473 if (cstate==LinphoneCallReleased){
474 if (call->op!=NULL) {
475 /* so that we cannot have anymore upcalls for SAL
476 concerning this call*/
477 sal_op_release(call->op);
480 linphone_call_unref(call);
485 static void linphone_call_destroy(LinphoneCall *obj)
488 sal_op_release(obj->op);
491 if (obj->resultdesc!=NULL) {
492 sal_media_description_unref(obj->resultdesc);
493 obj->resultdesc=NULL;
495 if (obj->localdesc!=NULL) {
496 sal_media_description_unref(obj->localdesc);
500 sal_op_release(obj->ping_op);
503 ms_free(obj->refer_to);
505 if (obj->owns_call_log)
506 linphone_call_log_destroy(obj->log);
507 if (obj->auth_token) {
508 ms_free(obj->auth_token);
515 * @addtogroup call_control
520 * Increments the call 's reference count.
521 * An application that wishes to retain a pointer to call object
522 * must use this function to unsure the pointer remains
523 * valid. Once the application no more needs this pointer,
524 * it must call linphone_call_unref().
526 LinphoneCall * linphone_call_ref(LinphoneCall *obj){
532 * Decrements the call object reference count.
533 * See linphone_call_ref().
535 void linphone_call_unref(LinphoneCall *obj){
538 linphone_call_destroy(obj);
543 * Returns current parameters associated to the call.
545 const LinphoneCallParams * linphone_call_get_current_params(const LinphoneCall *call){
546 return &call->current_params;
550 * Returns the remote address associated to this call
553 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
554 return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
558 * Returns the remote address associated to this call as a string.
560 * The result string must be freed by user using ms_free().
562 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
563 return linphone_address_as_string(linphone_call_get_remote_address(call));
567 * Retrieves the call's current state.
569 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
574 * Returns the reason for a call termination (either error or normal termination)
576 LinphoneReason linphone_call_get_reason(const LinphoneCall *call){
581 * Get the user_pointer in the LinphoneCall
583 * @ingroup call_control
585 * return user_pointer an opaque user pointer that can be retrieved at any time
587 void *linphone_call_get_user_pointer(LinphoneCall *call)
589 return call->user_pointer;
593 * Set the user_pointer in the LinphoneCall
595 * @ingroup call_control
597 * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
599 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
601 call->user_pointer = user_pointer;
605 * Returns the call log associated to this call.
607 LinphoneCallLog *linphone_call_get_call_log(const LinphoneCall *call){
612 * Returns the refer-to uri (if the call was transfered).
614 const char *linphone_call_get_refer_to(const LinphoneCall *call){
615 return call->refer_to;
619 * Returns direction of the call (incoming or outgoing).
621 LinphoneCallDir linphone_call_get_dir(const LinphoneCall *call){
622 return call->log->dir;
626 * Returns the far end's user agent description string, if available.
628 const char *linphone_call_get_remote_user_agent(LinphoneCall *call){
630 return sal_op_get_remote_ua (call->op);
636 * Returns true if this calls has received a transfer that has not been
638 * Pending transfers are executed when this call is being paused or closed,
639 * locally or by remote endpoint.
640 * If the call is already paused while receiving the transfer request, the
641 * transfer immediately occurs.
643 bool_t linphone_call_has_transfer_pending(const LinphoneCall *call){
644 return call->refer_pending;
648 * Returns call's duration in seconds.
650 int linphone_call_get_duration(const LinphoneCall *call){
651 if (call->media_start_time==0) return 0;
652 return time(NULL)-call->media_start_time;
656 * Returns the call object this call is replacing, if any.
657 * Call replacement can occur during call transfers.
658 * By default, the core automatically terminates the replaced call and accept the new one.
659 * This function allows the application to know whether a new incoming call is a one that replaces another one.
661 LinphoneCall *linphone_call_get_replaced_call(LinphoneCall *call){
662 SalOp *op=sal_call_get_replaces(call->op);
664 return (LinphoneCall*)sal_op_get_user_pointer(op);
670 * Indicate whether camera input should be sent to remote end.
672 void linphone_call_enable_camera (LinphoneCall *call, bool_t enable){
674 if (call->videostream!=NULL && call->videostream->ticker!=NULL){
675 LinphoneCore *lc=call->core;
676 MSWebCam *nowebcam=get_nowebcam_device();
677 if (call->camera_active!=enable && lc->video_conf.device!=nowebcam){
678 video_stream_change_camera(call->videostream,
679 enable ? lc->video_conf.device : nowebcam);
682 call->camera_active=enable;
687 * Take a photo of currently received video and write it into a jpeg file.
689 int linphone_call_take_video_snapshot(LinphoneCall *call, const char *file){
691 if (call->videostream!=NULL && call->videostream->jpegwriter!=NULL){
692 return ms_filter_call_method(call->videostream->jpegwriter,MS_JPEG_WRITER_TAKE_SNAPSHOT,(void*)file);
694 ms_warning("Cannot take snapshot: no currently running video stream on this call.");
701 * Returns TRUE if camera pictures are sent to the remote party.
703 bool_t linphone_call_camera_enabled (const LinphoneCall *call){
704 return call->camera_active;
708 * Enable video stream.
710 void linphone_call_params_enable_video(LinphoneCallParams *cp, bool_t enabled){
711 cp->has_video=enabled;
715 * Returns whether video is enabled.
717 bool_t linphone_call_params_video_enabled(const LinphoneCallParams *cp){
718 return cp->has_video;
721 enum LinphoneMediaEncryption linphone_call_params_get_media_encryption(LinphoneCallParams *cp) {
722 return cp->media_encryption;
725 void linphone_call_params_set_media_encryption(LinphoneCallParams *cp, enum LinphoneMediaEncryption e) {
726 cp->media_encryption = e;
731 * Enable sending of real early media (during outgoing calls).
733 void linphone_call_params_enable_early_media_sending(LinphoneCallParams *cp, bool_t enabled){
734 cp->real_early_media=enabled;
737 bool_t linphone_call_params_early_media_sending_enabled(const LinphoneCallParams *cp){
738 return cp->real_early_media;
742 * Returns true if the call is part of the locally managed conference.
744 bool_t linphone_call_params_local_conference_mode(const LinphoneCallParams *cp){
745 return cp->in_conference;
749 * Refine bandwidth settings for this call by setting a bandwidth limit for audio streams.
750 * As a consequence, codecs whose bitrates are not compatible with this limit won't be used.
752 void linphone_call_params_set_audio_bandwidth_limit(LinphoneCallParams *cp, int bandwidth){
753 cp->audio_bw=bandwidth;
758 * Request remote side to send us a Video Fast Update.
760 void linphone_call_send_vfu_request(LinphoneCall *call)
762 if (LinphoneCallStreamsRunning == linphone_call_get_state(call))
763 sal_call_send_vfu_request(call->op);
770 LinphoneCallParams * linphone_call_params_copy(const LinphoneCallParams *cp){
771 LinphoneCallParams *ncp=ms_new0(LinphoneCallParams,1);
772 memcpy(ncp,cp,sizeof(LinphoneCallParams));
779 void linphone_call_params_destroy(LinphoneCallParams *p){
788 #ifdef TEST_EXT_RENDERER
789 static void rendercb(void *data, const MSPicture *local, const MSPicture *remote){
790 ms_message("rendercb, local buffer=%p, remote buffer=%p",
791 local ? local->planes[0] : NULL, remote? remote->planes[0] : NULL);
796 static void video_stream_event_cb(void *user_pointer, const MSFilter *f, const unsigned int event_id, const void *args){
797 ms_warning("In linphonecall.c: video_stream_event_cb");
799 case MS_VIDEO_DECODER_DECODING_ERRORS:
800 ms_warning("Case is MS_VIDEO_DECODER_DECODING_ERRORS");
801 linphone_call_send_vfu_request((LinphoneCall*) user_pointer);
804 ms_warning("Unhandled event %i", event_id);
810 void linphone_call_init_media_streams(LinphoneCall *call){
811 LinphoneCore *lc=call->core;
812 SalMediaDescription *md=call->localdesc;
813 AudioStream *audiostream;
815 call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
816 if (linphone_core_echo_limiter_enabled(lc)){
817 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
818 if (strcasecmp(type,"mic")==0)
819 audio_stream_enable_echo_limiter(audiostream,ELControlMic);
820 else if (strcasecmp(type,"full")==0)
821 audio_stream_enable_echo_limiter(audiostream,ELControlFull);
823 audio_stream_enable_gain_control(audiostream,TRUE);
824 if (linphone_core_echo_cancellation_enabled(lc)){
825 int len,delay,framesize;
826 const char *statestr=lp_config_get_string(lc->config,"sound","ec_state",NULL);
827 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
828 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
829 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
830 audio_stream_set_echo_canceller_params(audiostream,len,delay,framesize);
831 if (statestr && audiostream->ec){
832 ms_filter_call_method(audiostream->ec,MS_ECHO_CANCELLER_SET_STATE_STRING,(void*)statestr);
835 audio_stream_enable_automatic_gain_control(audiostream,linphone_core_agc_enabled(lc));
837 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
838 audio_stream_enable_noise_gate(audiostream,enabled);
842 rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
844 call->audiostream_app_evq = ortp_ev_queue_new();
845 rtp_session_register_event_queue(audiostream->session,call->audiostream_app_evq);
849 if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
850 call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
851 if( lc->video_conf.displaytype != NULL)
852 video_stream_set_display_filter_name(call->videostream,lc->video_conf.displaytype);
853 video_stream_set_event_callback(call->videostream,video_stream_event_cb, call);
855 rtp_session_set_transports(call->videostream->session,lc->v_rtp,lc->v_rtcp);
856 call->videostream_app_evq = ortp_ev_queue_new();
857 rtp_session_register_event_queue(call->videostream->session,call->videostream_app_evq);
858 #ifdef TEST_EXT_RENDERER
859 video_stream_set_render_callback(call->videostream,rendercb,NULL);
863 call->videostream=NULL;
868 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
870 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
871 LinphoneCore* lc = (LinphoneCore*)user_data;
872 if (dtmf<0 || dtmf>15){
873 ms_warning("Bad dtmf value %i",dtmf);
876 if (lc->vtable.dtmf_received != NULL)
877 lc->vtable.dtmf_received(lc, linphone_core_get_current_call(lc), dtmf_tab[dtmf]);
880 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
882 MSFilter *f=st->equalizer;
883 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
884 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
885 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
891 if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
892 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
893 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
902 void _post_configure_audio_stream(AudioStream *st, LinphoneCore *lc, bool_t muted){
903 float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
906 float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
907 float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
908 int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
911 audio_stream_set_mic_gain(st,mic_gain);
913 audio_stream_set_mic_gain(st,0);
915 recv_gain = lc->sound_conf.soft_play_lev;
916 if (recv_gain != 0) {
917 linphone_core_set_playback_gain_db (lc,recv_gain);
921 ms_filter_call_method(st->volsend,MS_VOLUME_REMOVE_DC,&dc_removal);
922 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
923 thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
924 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
925 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
926 float transmit_thres=lp_config_get_float(lc->config,"sound","el_transmit_thres",-1);
929 if (speed==-1) speed=0.03;
930 if (force==-1) force=25;
931 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
932 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
934 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
936 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
937 if (transmit_thres!=-1)
938 ms_filter_call_method(f,MS_VOLUME_SET_EA_TRANSMIT_THRESHOLD,&transmit_thres);
940 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
941 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
944 /* parameters for a limited noise-gate effect, using echo limiter threshold */
945 float floorgain = 1/mic_gain;
946 int spk_agc=lp_config_get_int(lc->config,"sound","speaker_agc_enabled",0);
947 ms_filter_call_method(st->volrecv, MS_VOLUME_ENABLE_AGC, &spk_agc);
948 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
949 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
951 parametrize_equalizer(lc,st);
954 static void post_configure_audio_streams(LinphoneCall*call){
955 AudioStream *st=call->audiostream;
956 LinphoneCore *lc=call->core;
957 _post_configure_audio_stream(st,lc,call->audio_muted);
958 if (lc->vtable.dtmf_received!=NULL){
959 /* replace by our default action*/
960 audio_stream_play_received_dtmfs(call->audiostream,FALSE);
961 rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
965 static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
968 RtpProfile *prof=rtp_profile_new("Call profile");
971 LinphoneCore *lc=call->core;
975 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
976 PayloadType *pt=(PayloadType*)elem->data;
979 if ((pt->flags & PAYLOAD_TYPE_FLAG_CAN_SEND) && first) {
980 if (desc->type==SalAudio){
981 linphone_core_update_allocated_audio_bandwidth_in_call(call,pt);
982 up_ptime=linphone_core_get_upload_ptime(lc);
984 *used_pt=payload_type_get_number(pt);
987 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
988 else if (md->bandwidth>0) {
989 /*case where b=AS is given globally, not per stream*/
990 remote_bw=md->bandwidth;
991 if (desc->type==SalVideo){
992 remote_bw=get_video_bandwidth(remote_bw,call->audio_bw);
996 if (desc->type==SalAudio){
997 bw=get_min_bandwidth(call->audio_bw,remote_bw);
998 }else bw=get_min_bandwidth(get_video_bandwidth(linphone_core_get_upload_bandwidth (lc),call->audio_bw),remote_bw);
999 if (bw>0) pt->normal_bitrate=bw*1000;
1000 else if (desc->type==SalAudio){
1001 pt->normal_bitrate=-1;
1004 up_ptime=desc->ptime;
1008 snprintf(tmp,sizeof(tmp),"ptime=%i",up_ptime);
1009 payload_type_append_send_fmtp(pt,tmp);
1011 number=payload_type_get_number(pt);
1012 if (rtp_profile_get_payload(prof,number)!=NULL){
1013 ms_warning("A payload type with number %i already exists in profile !",number);
1015 rtp_profile_set_payload(prof,number,pt);
1021 static void setup_ring_player(LinphoneCore *lc, LinphoneCall *call){
1022 int pause_time=3000;
1023 audio_stream_play(call->audiostream,lc->sound_conf.ringback_tone);
1024 ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
1027 #define LINPHONE_RTCP_SDES_TOOL "Linphone-" LINPHONE_VERSION
1029 static bool_t linphone_call_sound_resources_available(LinphoneCall *call){
1030 LinphoneCore *lc=call->core;
1031 LinphoneCall *current=linphone_core_get_current_call(lc);
1032 return !linphone_core_is_in_conference(lc) &&
1033 (current==NULL || current==call);
1036 static void linphone_call_start_audio_stream(LinphoneCall *call, const char *cname, bool_t muted, bool_t send_ringbacktone, bool_t use_arc){
1037 LinphoneCore *lc=call->core;
1038 int jitt_comp=lc->rtp_conf.audio_jitt_comp;
1040 /* look for savp stream first */
1041 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
1042 SalProtoRtpSavp,SalAudio);
1043 /* no savp audio stream, use avp */
1045 stream=sal_media_description_find_stream(call->resultdesc,
1046 SalProtoRtpAvp,SalAudio);
1048 if (stream && stream->dir!=SalStreamInactive && stream->port!=0){
1049 MSSndCard *playcard=lc->sound_conf.lsd_card ?
1050 lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
1051 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
1052 const char *playfile=lc->play_file;
1053 const char *recfile=lc->rec_file;
1054 call->audio_profile=make_profile(call,call->resultdesc,stream,&used_pt);
1058 if (playcard==NULL) {
1059 ms_warning("No card defined for playback !");
1061 if (captcard==NULL) {
1062 ms_warning("No card defined for capture !");
1064 /*Replace soundcard filters by inactive file players or recorders
1065 when placed in recvonly or sendonly mode*/
1066 if (stream->port==0 || stream->dir==SalStreamRecvOnly){
1069 }else if (stream->dir==SalStreamSendOnly){
1073 /*And we will eventually play "playfile" if set by the user*/
1076 if (send_ringbacktone){
1078 playfile=NULL;/* it is setup later*/
1080 /*if playfile are supplied don't use soundcards*/
1081 if (lc->use_files) {
1085 if (call->params.in_conference){
1086 /* first create the graph without soundcard resources*/
1087 captcard=playcard=NULL;
1089 if (!linphone_call_sound_resources_available(call)){
1090 ms_message("Sound resources are used by another call, not using soundcard.");
1091 captcard=playcard=NULL;
1093 use_ec=captcard==NULL ? FALSE : linphone_core_echo_cancellation_enabled(lc);
1095 audio_stream_enable_adaptive_bitrate_control(call->audiostream,use_arc);
1096 audio_stream_start_full(
1098 call->audio_profile,
1099 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
1101 linphone_core_rtcp_enabled(lc) ? (stream->port+1) : 0,
1110 post_configure_audio_streams(call);
1111 if (muted && !send_ringbacktone){
1112 audio_stream_set_mic_gain(call->audiostream,0);
1114 if (stream->dir==SalStreamSendOnly && playfile!=NULL){
1116 ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
1118 if (send_ringbacktone){
1119 setup_ring_player(lc,call);
1121 audio_stream_set_rtcp_information(call->audiostream, cname, LINPHONE_RTCP_SDES_TOOL);
1122 if (call->params.in_conference){
1123 /*transform the graph to connect it to the conference filter */
1124 linphone_call_add_to_conf(call);
1127 if (stream->proto == SalProtoRtpSavp) {
1128 const SalStreamDescription *local_st_desc=sal_media_description_find_stream(call->localdesc,
1129 SalProtoRtpSavp,SalAudio);
1131 audio_stream_enable_strp(
1133 stream->crypto[0].algo,
1134 local_st_desc->crypto[0].master_key,
1135 stream->crypto[0].master_key);
1137 }else ms_warning("No audio stream accepted ?");
1141 static void linphone_call_start_video_stream(LinphoneCall *call, const char *cname,bool_t all_inputs_muted){
1142 #ifdef VIDEO_ENABLED
1143 LinphoneCore *lc=call->core;
1145 /* look for savp stream first */
1146 const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
1147 SalProtoRtpSavp,SalVideo);
1148 /* no savp audio stream, use avp */
1150 vstream=sal_media_description_find_stream(call->resultdesc,
1151 SalProtoRtpAvp,SalVideo);
1153 /* shutdown preview */
1154 if (lc->previewstream!=NULL) {
1155 video_preview_stop(lc->previewstream);
1156 lc->previewstream=NULL;
1158 call->current_params.has_video=FALSE;
1159 if (vstream!=NULL && vstream->dir!=SalStreamInactive && vstream->port!=0) {
1160 const char *addr=vstream->addr[0]!='\0' ? vstream->addr : call->resultdesc->addr;
1161 call->video_profile=make_profile(call,call->resultdesc,vstream,&used_pt);
1163 VideoStreamDir dir=VideoStreamSendRecv;
1164 MSWebCam *cam=lc->video_conf.device;
1165 bool_t is_inactive=FALSE;
1167 call->current_params.has_video=TRUE;
1169 video_stream_enable_adaptive_bitrate_control(call->videostream,
1170 linphone_core_adaptive_rate_control_enabled(lc));
1171 video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
1172 video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
1173 if (lc->video_window_id!=0)
1174 video_stream_set_native_window_id(call->videostream,lc->video_window_id);
1175 if (lc->preview_window_id!=0)
1176 video_stream_set_native_preview_window_id (call->videostream,lc->preview_window_id);
1177 video_stream_use_preview_video_window (call->videostream,lc->use_preview_window);
1179 if (vstream->dir==SalStreamSendOnly && lc->video_conf.capture ){
1180 cam=get_nowebcam_device();
1181 dir=VideoStreamSendOnly;
1182 }else if (vstream->dir==SalStreamRecvOnly && lc->video_conf.display ){
1183 dir=VideoStreamRecvOnly;
1184 }else if (vstream->dir==SalStreamSendRecv){
1185 if (lc->video_conf.display && lc->video_conf.capture)
1186 dir=VideoStreamSendRecv;
1187 else if (lc->video_conf.display)
1188 dir=VideoStreamRecvOnly;
1190 dir=VideoStreamSendOnly;
1192 ms_warning("video stream is inactive.");
1193 /*either inactive or incompatible with local capabilities*/
1196 if (call->camera_active==FALSE || all_inputs_muted){
1197 cam=get_nowebcam_device();
1200 video_stream_set_direction (call->videostream, dir);
1201 ms_message("%s lc rotation:%d\n", __FUNCTION__, lc->device_rotation);
1202 video_stream_set_device_rotation(call->videostream, lc->device_rotation);
1203 video_stream_start(call->videostream,
1204 call->video_profile, addr, vstream->port,
1205 linphone_core_rtcp_enabled(lc) ? (vstream->port+1) : 0,
1206 used_pt, lc->rtp_conf.audio_jitt_comp, cam);
1207 video_stream_set_rtcp_information(call->videostream, cname,LINPHONE_RTCP_SDES_TOOL);
1210 if (vstream->proto == SalProtoRtpSavp) {
1211 const SalStreamDescription *local_st_desc=sal_media_description_find_stream(call->localdesc,
1212 SalProtoRtpSavp,SalVideo);
1214 video_stream_enable_strp(
1216 vstream->crypto[0].algo,
1217 local_st_desc->crypto[0].master_key,
1218 vstream->crypto[0].master_key
1221 }else ms_warning("No video stream accepted.");
1223 ms_warning("No valid video stream defined.");
1228 void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_muted, bool_t send_ringbacktone){
1229 LinphoneCore *lc=call->core;
1230 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
1233 #ifdef VIDEO_ENABLED
1234 const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
1235 SalProtoRtpAvp,SalVideo);
1238 if(call->audiostream == NULL)
1240 ms_fatal("start_media_stream() called without prior init !");
1243 call->current_params = call->params;
1244 if (call->media_start_time==0) call->media_start_time=time(NULL);
1245 cname=linphone_address_as_string_uri_only(me);
1247 #if defined(VIDEO_ENABLED)
1248 if (vstream!=NULL && vstream->dir!=SalStreamInactive && vstream->payloads!=NULL){
1249 /*when video is used, do not make adaptive rate control on audio, it is stupid.*/
1253 linphone_call_start_audio_stream(call,cname,all_inputs_muted,send_ringbacktone,use_arc);
1254 if (call->videostream!=NULL) {
1255 linphone_call_start_video_stream(call,cname,all_inputs_muted);
1258 call->all_muted=all_inputs_muted;
1259 call->playing_ringbacktone=send_ringbacktone;
1260 call->up_bw=linphone_core_get_upload_bandwidth(lc);
1262 if (ortp_zrtp_available()) {
1263 OrtpZrtpParams params;
1264 params.zid=get_hexa_zrtp_identifier(lc);
1265 params.zid_file=lc->zrtp_secrets_cache;
1266 audio_stream_enable_zrtp(call->audiostream,¶ms);
1272 linphone_address_destroy(me);
1275 static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
1276 audio_stream_get_local_rtp_stats (st,&log->local_stats);
1277 log->quality=audio_stream_get_average_quality_rating(st);
1280 void linphone_call_stop_media_streams(LinphoneCall *call){
1281 if (call->audiostream!=NULL) {
1282 rtp_session_unregister_event_queue(call->audiostream->session,call->audiostream_app_evq);
1283 ortp_ev_queue_flush(call->audiostream_app_evq);
1284 ortp_ev_queue_destroy(call->audiostream_app_evq);
1286 if (call->audiostream->ec){
1287 const char *state_str=NULL;
1288 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_STATE_STRING,&state_str);
1290 ms_message("Writing echo canceller state, %i bytes",(int)strlen(state_str));
1291 lp_config_set_string(call->core->config,"sound","ec_state",state_str);
1294 linphone_call_log_fill_stats (call->log,call->audiostream);
1295 if (call->endpoint){
1296 linphone_call_remove_from_conf(call);
1298 audio_stream_stop(call->audiostream);
1299 call->audiostream=NULL;
1303 #ifdef VIDEO_ENABLED
1304 if (call->videostream!=NULL){
1305 rtp_session_unregister_event_queue(call->videostream->session,call->videostream_app_evq);
1306 ortp_ev_queue_flush(call->videostream_app_evq);
1307 ortp_ev_queue_destroy(call->videostream_app_evq);
1308 video_stream_stop(call->videostream);
1309 call->videostream=NULL;
1312 ms_event_queue_skip(call->core->msevq);
1314 if (call->audio_profile){
1315 rtp_profile_clear_all(call->audio_profile);
1316 rtp_profile_destroy(call->audio_profile);
1317 call->audio_profile=NULL;
1319 if (call->video_profile){
1320 rtp_profile_clear_all(call->video_profile);
1321 rtp_profile_destroy(call->video_profile);
1322 call->video_profile=NULL;
1328 void linphone_call_enable_echo_cancellation(LinphoneCall *call, bool_t enable) {
1329 if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1330 bool_t bypass_mode = !enable;
1331 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_SET_BYPASS_MODE,&bypass_mode);
1334 bool_t linphone_call_echo_cancellation_enabled(LinphoneCall *call) {
1335 if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1337 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_BYPASS_MODE,&val);
1340 return linphone_core_echo_cancellation_enabled(call->core);
1344 void linphone_call_enable_echo_limiter(LinphoneCall *call, bool_t val){
1345 if (call!=NULL && call->audiostream!=NULL ) {
1347 const char *type=lp_config_get_string(call->core->config,"sound","el_type","mic");
1348 if (strcasecmp(type,"mic")==0)
1349 audio_stream_enable_echo_limiter(call->audiostream,ELControlMic);
1350 else if (strcasecmp(type,"full")==0)
1351 audio_stream_enable_echo_limiter(call->audiostream,ELControlFull);
1353 audio_stream_enable_echo_limiter(call->audiostream,ELInactive);
1358 bool_t linphone_call_echo_limiter_enabled(const LinphoneCall *call){
1359 if (call!=NULL && call->audiostream!=NULL ){
1360 return call->audiostream->el_type !=ELInactive ;
1362 return linphone_core_echo_limiter_enabled(call->core);
1367 * @addtogroup call_misc
1372 * Returns the measured sound volume played locally (received from remote)
1373 * It is expressed in dbm0.
1375 float linphone_call_get_play_volume(LinphoneCall *call){
1376 AudioStream *st=call->audiostream;
1377 if (st && st->volrecv){
1379 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&vol);
1383 return LINPHONE_VOLUME_DB_LOWEST;
1387 * Returns the measured sound volume recorded locally (sent to remote)
1388 * It is expressed in dbm0.
1390 float linphone_call_get_record_volume(LinphoneCall *call){
1391 AudioStream *st=call->audiostream;
1392 if (st && st->volsend && !call->audio_muted && call->state==LinphoneCallStreamsRunning){
1394 ms_filter_call_method(st->volsend,MS_VOLUME_GET,&vol);
1398 return LINPHONE_VOLUME_DB_LOWEST;
1402 * Obtain real-time quality rating of the call
1404 * Based on local RTP statistics and RTCP feedback, a quality rating is computed and updated
1405 * during all the duration of the call. This function returns its value at the time of the function call.
1406 * It is expected that the rating is updated at least every 5 seconds or so.
1407 * The rating is a floating point number comprised between 0 and 5.
1409 * 4-5 = good quality <br>
1410 * 3-4 = average quality <br>
1411 * 2-3 = poor quality <br>
1412 * 1-2 = very poor quality <br>
1413 * 0-1 = can't be worse, mostly unusable <br>
1415 * @returns The function returns -1 if no quality measurement is available, for example if no
1416 * active audio stream exist. Otherwise it returns the quality rating.
1418 float linphone_call_get_current_quality(LinphoneCall *call){
1419 if (call->audiostream){
1420 return audio_stream_get_quality_rating(call->audiostream);
1426 * Returns call quality averaged over all the duration of the call.
1428 * See linphone_call_get_current_quality() for more details about quality measurement.
1430 float linphone_call_get_average_quality(LinphoneCall *call){
1431 if (call->audiostream){
1432 return audio_stream_get_average_quality_rating(call->audiostream);
1441 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1442 ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1443 (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1444 (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1445 (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1446 (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1449 static void linphone_core_disconnected(LinphoneCore *lc, LinphoneCall *call){
1453 from = linphone_call_get_remote_address_as_string(call);
1456 snprintf(temp,sizeof(temp),"Remote end %s seems to have disconnected, the call is going to be closed.",from);
1461 snprintf(temp,sizeof(temp),"Remote end seems to have disconnected, the call is going to be closed.");
1463 if (lc->vtable.display_warning!=NULL)
1464 lc->vtable.display_warning(lc,temp);
1465 linphone_core_terminate_call(lc,call);
1468 void linphone_call_background_tasks(LinphoneCall *call, bool_t one_second_elapsed){
1469 int disconnect_timeout = linphone_core_get_nortp_timeout(call->core);
1470 bool_t disconnected=FALSE;
1472 if (call->state==LinphoneCallStreamsRunning && one_second_elapsed){
1473 RtpSession *as=NULL,*vs=NULL;
1474 float audio_load=0, video_load=0;
1475 if (call->audiostream!=NULL){
1476 as=call->audiostream->session;
1477 if (call->audiostream->ticker)
1478 audio_load=ms_ticker_get_average_load(call->audiostream->ticker);
1480 if (call->videostream!=NULL){
1481 if (call->videostream->ticker)
1482 video_load=ms_ticker_get_average_load(call->videostream->ticker);
1483 vs=call->videostream->session;
1485 display_bandwidth(as,vs);
1486 ms_message("Thread processing load: audio=%f\tvideo=%f",audio_load,video_load);
1488 #ifdef VIDEO_ENABLED
1489 if (call->videostream!=NULL) {
1490 // Beware that the application queue should not depend on treatments fron the
1491 // mediastreamer queue.
1492 video_stream_iterate(call->videostream);
1494 if (call->videostream_app_evq){
1496 while (NULL != (ev=ortp_ev_queue_get(call->videostream_app_evq))){
1497 OrtpEventType evt=ortp_event_get_type(ev);
1498 if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
1499 OrtpEventData *evd=ortp_event_get_data(ev);
1500 linphone_call_videostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
1502 ortp_event_destroy(ev);
1507 if (call->audiostream!=NULL) {
1508 // Beware that the application queue should not depend on treatments fron the
1509 // mediastreamer queue.
1510 audio_stream_iterate(call->audiostream);
1512 if (call->audiostream->evq){
1514 while (NULL != (ev=ortp_ev_queue_get(call->audiostream_app_evq))){
1515 OrtpEventType evt=ortp_event_get_type(ev);
1516 if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
1517 OrtpEventData *evd=ortp_event_get_data(ev);
1518 linphone_call_audiostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
1519 } else if (evt == ORTP_EVENT_ZRTP_SAS_READY) {
1520 OrtpEventData *evd=ortp_event_get_data(ev);
1521 linphone_call_audiostream_auth_token_ready(call, evd->info.zrtp_sas.sas, evd->info.zrtp_sas.verified);
1523 ortp_event_destroy(ev);
1527 if (call->state==LinphoneCallStreamsRunning && one_second_elapsed && call->audiostream!=NULL && disconnect_timeout>0 )
1528 disconnected=!audio_stream_alive(call->audiostream,disconnect_timeout);
1530 linphone_core_disconnected(call->core,call);
1533 void linphone_call_log_completed(LinphoneCall *call){
1534 LinphoneCore *lc=call->core;
1536 call->log->duration=time(NULL)-call->start_time;
1538 if (call->log->status==LinphoneCallMissed){
1541 info=ortp_strdup_printf(ngettext("You have missed %i call.",
1542 "You have missed %i calls.", lc->missed_calls),
1544 if (lc->vtable.display_status!=NULL)
1545 lc->vtable.display_status(lc,info);
1548 lc->call_logs=ms_list_prepend(lc->call_logs,(void *)call->log);
1549 if (ms_list_size(lc->call_logs)>lc->max_call_logs){
1550 MSList *elem,*prevelem=NULL;
1551 /*find the last element*/
1552 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
1556 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
1557 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
1559 if (lc->vtable.call_log_updated!=NULL){
1560 lc->vtable.call_log_updated(lc,call->log);
1562 call_logs_write_to_config_file(lc);