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;
90 static bool_t linphone_call_are_all_streams_encrypted(LinphoneCall *call) {
91 // Check ZRTP encryption in audiostream
92 if (!call->audiostream_encrypted) {
97 // If video enabled, check ZRTP encryption in videostream
98 const LinphoneCallParams *params=linphone_call_get_current_params(call);
99 if (params->has_video && !call->videostream_encrypted) {
107 void propagate_encryption_changed(LinphoneCall *call){
108 LinphoneCore *lc=call->core;
109 if (!linphone_call_are_all_streams_encrypted(call)) {
110 ms_message("Some streams are not encrypted");
111 call->current_params.media_encryption=LinphoneMediaEncryptionNone;
112 if (lc->vtable.call_encryption_changed)
113 lc->vtable.call_encryption_changed(call->core, call, FALSE, call->auth_token);
115 ms_message("All streams are encrypted");
116 call->current_params.media_encryption=LinphoneMediaEncryptionZRTP;
117 if (lc->vtable.call_encryption_changed)
118 lc->vtable.call_encryption_changed(call->core, call, TRUE, call->auth_token);
123 static void linphone_call_videostream_encryption_changed(void *data, bool_t encrypted){
124 ms_message("Video stream is %s", encrypted ? "encrypted" : "not encrypted");
126 LinphoneCall *call = (LinphoneCall *)data;
127 call->videostream_encrypted=encrypted;
128 propagate_encryption_changed(call);
132 static void linphone_call_audiostream_encryption_changed(void *data, bool_t encrypted) {
133 char status[255]={0};
134 ms_message("Audio stream is %s ", encrypted ? "encrypted" : "not encrypted");
136 LinphoneCall *call = (LinphoneCall *)data;
137 call->audiostream_encrypted=encrypted;
139 if (encrypted && call->core->vtable.display_status != NULL) {
140 snprintf(status,sizeof(status)-1,_("Authentication token is %s"),call->auth_token);
141 call->core->vtable.display_status(call->core, status);
144 propagate_encryption_changed(call);
148 // Enable video encryption
149 const LinphoneCallParams *params=linphone_call_get_current_params(call);
150 if (params->has_video) {
151 ms_message("Trying to enable encryption on video stream");
152 OrtpZrtpParams params;
153 params.zid=get_hexa_zrtp_identifier(call->core);
154 params.zid_file=NULL; //unused
155 video_stream_enable_zrtp(call->videostream,call->audiostream,¶ms);
161 static void linphone_call_audiostream_auth_token_ready(void *data, const char* auth_token, bool_t verified) {
162 LinphoneCall *call=(LinphoneCall *)data;
163 if (call->auth_token != NULL)
164 ms_free(call->auth_token);
166 call->auth_token=ms_strdup(auth_token);
167 call->auth_token_verified=verified;
169 ms_message("Authentication token is %s (%s)", auth_token, verified?"verified":"unverified");
172 void linphone_call_set_authentication_token_verified(LinphoneCall *call, bool_t verified){
173 if (call->audiostream==NULL){
174 ms_error("linphone_call_set_authentication_token_verified(): No audio stream");
176 if (call->audiostream->ortpZrtpContext==NULL){
177 ms_error("linphone_call_set_authentication_token_verified(): No zrtp context.");
179 if (!call->auth_token_verified && verified){
180 ortp_zrtp_sas_verified(call->audiostream->ortpZrtpContext);
181 }else if (call->auth_token_verified && !verified){
182 ortp_zrtp_sas_reset_verified(call->audiostream->ortpZrtpContext);
184 call->auth_token_verified=verified;
185 propagate_encryption_changed(call);
188 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandwidth_limit){
191 for(it=codecs;it!=NULL;it=it->next){
192 PayloadType *pt=(PayloadType*)it->data;
193 if (pt->flags & PAYLOAD_TYPE_ENABLED){
194 if (bandwidth_limit>0 && !linphone_core_is_payload_type_usable_for_bandwidth(lc,pt,bandwidth_limit)){
195 ms_message("Codec %s/%i eliminated because of audio bandwidth constraint.",pt->mime_type,pt->clock_rate);
198 if (linphone_core_check_payload_type_usability(lc,pt)){
199 l=ms_list_append(l,payload_type_clone(pt));
206 static SalMediaDescription *_create_local_media_description(LinphoneCore *lc, LinphoneCall *call, unsigned int session_id, unsigned int session_ver){
210 const char *me=linphone_core_get_identity(lc);
211 LinphoneAddress *addr=linphone_address_new(me);
212 const char *username=linphone_address_get_username (addr);
213 SalMediaDescription *md=sal_media_description_new();
215 md->session_id=session_id;
216 md->session_ver=session_ver;
218 strncpy(md->addr,call->localip,sizeof(md->addr));
219 strncpy(md->username,username,sizeof(md->username));
220 md->bandwidth=linphone_core_get_download_bandwidth(lc);
222 /*set audio capabilities */
223 strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
224 md->streams[0].port=call->audio_port;
225 md->streams[0].proto=(call->params.media_encryption == LinphoneMediaEncryptionSRTP) ?
226 SalProtoRtpSavp : SalProtoRtpAvp;
227 md->streams[0].type=SalAudio;
228 md->streams[0].ptime=lc->net_conf.down_ptime;
229 l=make_codec_list(lc,lc->codecs_conf.audio_codecs,call->params.audio_bw);
230 pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
231 l=ms_list_append(l,pt);
232 md->streams[0].payloads=l;
235 if (call->params.has_video){
237 md->streams[1].port=call->video_port;
238 md->streams[1].proto=md->streams[0].proto;
239 md->streams[1].type=SalVideo;
240 l=make_codec_list(lc,lc->codecs_conf.video_codecs,0);
241 md->streams[1].payloads=l;
244 for(i=0; i<md->nstreams; i++) {
245 if (md->streams[i].proto == SalProtoRtpSavp) {
246 md->streams[i].crypto[0].tag = 1;
247 md->streams[i].crypto[0].algo = AES_128_SHA1_80;
248 if (!generate_b64_crypto_key(30, md->streams[i].crypto[0].master_key))
249 md->streams[i].crypto[0].algo = 0;
250 md->streams[i].crypto[1].tag = 2;
251 md->streams[i].crypto[1].algo = AES_128_SHA1_32;
252 if (!generate_b64_crypto_key(30, md->streams[i].crypto[1].master_key))
253 md->streams[i].crypto[1].algo = 0;
254 md->streams[i].crypto[2].algo = 0;
258 linphone_address_destroy(addr);
262 void update_local_media_description(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription **md){
264 *md = _create_local_media_description(lc,call,0,0);
266 unsigned int id = (*md)->session_id;
267 unsigned int ver = (*md)->session_ver+1;
268 sal_media_description_unref(*md);
269 *md = _create_local_media_description(lc,call,id,ver);
273 SalMediaDescription *create_local_media_description(LinphoneCore *lc, LinphoneCall *call){
274 unsigned int id=rand() & 0xfff;
275 return _create_local_media_description(lc,call,id,id);
278 static int find_port_offset(LinphoneCore *lc){
282 bool_t already_used=FALSE;
283 for(offset=0;offset<100;offset+=2){
284 audio_port=linphone_core_get_audio_port (lc)+offset;
286 for(elem=lc->calls;elem!=NULL;elem=elem->next){
287 LinphoneCall *call=(LinphoneCall*)elem->data;
288 if (call->audio_port==audio_port) {
293 if (!already_used) break;
296 ms_error("Could not find any free port !");
302 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
305 call->state=LinphoneCallIdle;
306 call->start_time=time(NULL);
307 call->media_start_time=0;
308 call->log=linphone_call_log_new(call, from, to);
309 call->owns_call_log=TRUE;
310 linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
311 port_offset=find_port_offset (call->core);
312 if (port_offset==-1) return;
313 call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
314 call->video_port=linphone_core_get_video_port(call->core)+port_offset;
318 static void discover_mtu(LinphoneCore *lc, const char *remote){
320 if (lc->net_conf.mtu==0 ){
321 /*attempt to discover mtu*/
322 mtu=ms_discover_mtu(remote);
325 ms_message("Discovered mtu is %i, RTP payload max size is %i",
326 mtu, ms_get_payload_max_size());
331 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, const LinphoneCallParams *params)
333 LinphoneCall *call=ms_new0(LinphoneCall,1);
334 call->dir=LinphoneCallOutgoing;
335 call->op=sal_op_new(lc->sal);
336 sal_op_set_user_pointer(call->op,call);
338 linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
339 linphone_call_init_common(call,from,to);
340 call->params=*params;
341 call->localdesc=create_local_media_description (lc,call);
342 call->camera_active=params->has_video;
343 if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
344 linphone_core_run_stun_tests(call->core,call);
345 discover_mtu(lc,linphone_address_get_domain (to));
346 if (params->referer){
347 sal_call_set_referer(call->op,params->referer->op);
352 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
353 LinphoneCall *call=ms_new0(LinphoneCall,1);
356 call->dir=LinphoneCallIncoming;
357 sal_op_set_user_pointer(op,call);
361 if (lc->sip_conf.ping_with_options){
362 /*the following sends an option request back to the caller so that
363 we get a chance to discover our nat'd address before answering.*/
364 call->ping_op=sal_op_new(lc->sal);
365 from_str=linphone_address_as_string(from);
366 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
367 sal_op_set_user_pointer(call->ping_op,call);
368 sal_ping(call->ping_op,linphone_core_find_best_identity(lc,from,NULL),from_str);
372 linphone_address_clean(from);
373 linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
374 linphone_call_init_common(call, from, to);
375 linphone_core_init_default_params(lc, &call->params);
376 call->localdesc=create_local_media_description (lc,call);
377 call->camera_active=call->params.has_video;
378 if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
379 linphone_core_run_stun_tests(call->core,call);
380 discover_mtu(lc,linphone_address_get_domain(from));
384 /* this function is called internally to get rid of a call.
385 It performs the following tasks:
386 - remove the call from the internal list of calls
387 - update the call logs accordingly
390 static void linphone_call_set_terminated(LinphoneCall *call){
391 LinphoneCore *lc=call->core;
393 linphone_core_update_allocated_audio_bandwidth(lc);
395 call->owns_call_log=FALSE;
396 linphone_call_log_completed(call);
399 if (call == lc->current_call){
400 ms_message("Resetting the current call");
401 lc->current_call=NULL;
404 if (linphone_core_del_call(lc,call) != 0){
405 ms_error("Could not remove the call from the list !!!");
408 if (ms_list_size(lc->calls)==0)
409 linphone_core_notify_all_friends(lc,lc->presence_mode);
411 linphone_core_conference_check_uninit(&lc->conf_ctx);
412 if (call->ringing_beep){
413 linphone_core_stop_dtmf(lc);
414 call->ringing_beep=FALSE;
418 const char *linphone_call_state_to_string(LinphoneCallState cs){
420 case LinphoneCallIdle:
421 return "LinphoneCallIdle";
422 case LinphoneCallIncomingReceived:
423 return "LinphoneCallIncomingReceived";
424 case LinphoneCallOutgoingInit:
425 return "LinphoneCallOutgoingInit";
426 case LinphoneCallOutgoingProgress:
427 return "LinphoneCallOutgoingProgress";
428 case LinphoneCallOutgoingRinging:
429 return "LinphoneCallOutgoingRinging";
430 case LinphoneCallOutgoingEarlyMedia:
431 return "LinphoneCallOutgoingEarlyMedia";
432 case LinphoneCallConnected:
433 return "LinphoneCallConnected";
434 case LinphoneCallStreamsRunning:
435 return "LinphoneCallStreamsRunning";
436 case LinphoneCallPausing:
437 return "LinphoneCallPausing";
438 case LinphoneCallPaused:
439 return "LinphoneCallPaused";
440 case LinphoneCallResuming:
441 return "LinphoneCallResuming";
442 case LinphoneCallRefered:
443 return "LinphoneCallRefered";
444 case LinphoneCallError:
445 return "LinphoneCallError";
446 case LinphoneCallEnd:
447 return "LinphoneCallEnd";
448 case LinphoneCallPausedByRemote:
449 return "LinphoneCallPausedByRemote";
450 case LinphoneCallUpdatedByRemote:
451 return "LinphoneCallUpdatedByRemote";
452 case LinphoneCallIncomingEarlyMedia:
453 return "LinphoneCallIncomingEarlyMedia";
454 case LinphoneCallUpdated:
455 return "LinphoneCallUpdated";
456 case LinphoneCallReleased:
457 return "LinphoneCallReleased";
459 return "undefined state";
462 void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const char *message){
463 LinphoneCore *lc=call->core;
465 if (call->state!=cstate){
466 if (call->state==LinphoneCallEnd || call->state==LinphoneCallError){
467 if (cstate!=LinphoneCallReleased){
468 ms_warning("Spurious call state change from %s to %s, ignored.",linphone_call_state_to_string(call->state),
469 linphone_call_state_to_string(cstate));
473 ms_message("Call %p: moving from state %s to %s",call,linphone_call_state_to_string(call->state),
474 linphone_call_state_to_string(cstate));
475 if (cstate!=LinphoneCallRefered){
476 /*LinphoneCallRefered is rather an event, not a state.
477 Indeed it does not change the state of the call (still paused or running)*/
480 if (cstate==LinphoneCallEnd || cstate==LinphoneCallError){
481 if (call->reason==LinphoneReasonDeclined){
482 call->log->status=LinphoneCallDeclined;
484 linphone_call_set_terminated (call);
486 if (cstate == LinphoneCallConnected) {
487 call->log->status=LinphoneCallSuccess;
488 call->media_start_time=time(NULL);
491 if (lc->vtable.call_state_changed)
492 lc->vtable.call_state_changed(lc,call,cstate,message);
493 if (cstate==LinphoneCallReleased){
494 if (call->op!=NULL) {
495 /* so that we cannot have anymore upcalls for SAL
496 concerning this call*/
497 sal_op_release(call->op);
500 linphone_call_unref(call);
505 static void linphone_call_destroy(LinphoneCall *obj)
508 sal_op_release(obj->op);
511 if (obj->resultdesc!=NULL) {
512 sal_media_description_unref(obj->resultdesc);
513 obj->resultdesc=NULL;
515 if (obj->localdesc!=NULL) {
516 sal_media_description_unref(obj->localdesc);
520 sal_op_release(obj->ping_op);
523 ms_free(obj->refer_to);
525 if (obj->owns_call_log)
526 linphone_call_log_destroy(obj->log);
527 if (obj->auth_token) {
528 ms_free(obj->auth_token);
535 * @addtogroup call_control
540 * Increments the call 's reference count.
541 * An application that wishes to retain a pointer to call object
542 * must use this function to unsure the pointer remains
543 * valid. Once the application no more needs this pointer,
544 * it must call linphone_call_unref().
546 LinphoneCall * linphone_call_ref(LinphoneCall *obj){
552 * Decrements the call object reference count.
553 * See linphone_call_ref().
555 void linphone_call_unref(LinphoneCall *obj){
558 linphone_call_destroy(obj);
563 * Returns current parameters associated to the call.
565 const LinphoneCallParams * linphone_call_get_current_params(const LinphoneCall *call){
566 return &call->current_params;
570 * Returns the remote address associated to this call
573 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
574 return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
578 * Returns the remote address associated to this call as a string.
580 * The result string must be freed by user using ms_free().
582 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
583 return linphone_address_as_string(linphone_call_get_remote_address(call));
587 * Retrieves the call's current state.
589 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
594 * Returns the reason for a call termination (either error or normal termination)
596 LinphoneReason linphone_call_get_reason(const LinphoneCall *call){
601 * Get the user_pointer in the LinphoneCall
603 * @ingroup call_control
605 * return user_pointer an opaque user pointer that can be retrieved at any time
607 void *linphone_call_get_user_pointer(LinphoneCall *call)
609 return call->user_pointer;
613 * Set the user_pointer in the LinphoneCall
615 * @ingroup call_control
617 * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
619 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
621 call->user_pointer = user_pointer;
625 * Returns the call log associated to this call.
627 LinphoneCallLog *linphone_call_get_call_log(const LinphoneCall *call){
632 * Returns the refer-to uri (if the call was transfered).
634 const char *linphone_call_get_refer_to(const LinphoneCall *call){
635 return call->refer_to;
639 * Returns direction of the call (incoming or outgoing).
641 LinphoneCallDir linphone_call_get_dir(const LinphoneCall *call){
642 return call->log->dir;
646 * Returns the far end's user agent description string, if available.
648 const char *linphone_call_get_remote_user_agent(LinphoneCall *call){
650 return sal_op_get_remote_ua (call->op);
656 * Returns true if this calls has received a transfer that has not been
658 * Pending transfers are executed when this call is being paused or closed,
659 * locally or by remote endpoint.
660 * If the call is already paused while receiving the transfer request, the
661 * transfer immediately occurs.
663 bool_t linphone_call_has_transfer_pending(const LinphoneCall *call){
664 return call->refer_pending;
668 * Returns call's duration in seconds.
670 int linphone_call_get_duration(const LinphoneCall *call){
671 if (call->media_start_time==0) return 0;
672 return time(NULL)-call->media_start_time;
676 * Returns the call object this call is replacing, if any.
677 * Call replacement can occur during call transfers.
678 * By default, the core automatically terminates the replaced call and accept the new one.
679 * This function allows the application to know whether a new incoming call is a one that replaces another one.
681 LinphoneCall *linphone_call_get_replaced_call(LinphoneCall *call){
682 SalOp *op=sal_call_get_replaces(call->op);
684 return (LinphoneCall*)sal_op_get_user_pointer(op);
690 * Indicate whether camera input should be sent to remote end.
692 void linphone_call_enable_camera (LinphoneCall *call, bool_t enable){
694 if (call->videostream!=NULL && call->videostream->ticker!=NULL){
695 LinphoneCore *lc=call->core;
696 MSWebCam *nowebcam=get_nowebcam_device();
697 if (call->camera_active!=enable && lc->video_conf.device!=nowebcam){
698 video_stream_change_camera(call->videostream,
699 enable ? lc->video_conf.device : nowebcam);
702 call->camera_active=enable;
707 * Take a photo of currently received video and write it into a jpeg file.
709 int linphone_call_take_video_snapshot(LinphoneCall *call, const char *file){
711 if (call->videostream!=NULL && call->videostream->jpegwriter!=NULL){
712 return ms_filter_call_method(call->videostream->jpegwriter,MS_JPEG_WRITER_TAKE_SNAPSHOT,(void*)file);
714 ms_warning("Cannot take snapshot: no currently running video stream on this call.");
721 * Returns TRUE if camera pictures are sent to the remote party.
723 bool_t linphone_call_camera_enabled (const LinphoneCall *call){
724 return call->camera_active;
728 * Enable video stream.
730 void linphone_call_params_enable_video(LinphoneCallParams *cp, bool_t enabled){
731 cp->has_video=enabled;
735 * Returns whether video is enabled.
737 bool_t linphone_call_params_video_enabled(const LinphoneCallParams *cp){
738 return cp->has_video;
741 enum LinphoneMediaEncryption linphone_call_params_get_media_encryption(const LinphoneCallParams *cp) {
742 return cp->media_encryption;
745 void linphone_call_params_set_media_encryption(LinphoneCallParams *cp, enum LinphoneMediaEncryption e) {
746 cp->media_encryption = e;
751 * Enable sending of real early media (during outgoing calls).
753 void linphone_call_params_enable_early_media_sending(LinphoneCallParams *cp, bool_t enabled){
754 cp->real_early_media=enabled;
757 bool_t linphone_call_params_early_media_sending_enabled(const LinphoneCallParams *cp){
758 return cp->real_early_media;
762 * Returns true if the call is part of the locally managed conference.
764 bool_t linphone_call_params_local_conference_mode(const LinphoneCallParams *cp){
765 return cp->in_conference;
769 * Refine bandwidth settings for this call by setting a bandwidth limit for audio streams.
770 * As a consequence, codecs whose bitrates are not compatible with this limit won't be used.
772 void linphone_call_params_set_audio_bandwidth_limit(LinphoneCallParams *cp, int bandwidth){
773 cp->audio_bw=bandwidth;
778 * Request remote side to send us a Video Fast Update.
780 void linphone_call_send_vfu_request(LinphoneCall *call)
782 if (LinphoneCallStreamsRunning == linphone_call_get_state(call))
783 sal_call_send_vfu_request(call->op);
790 LinphoneCallParams * linphone_call_params_copy(const LinphoneCallParams *cp){
791 LinphoneCallParams *ncp=ms_new0(LinphoneCallParams,1);
792 memcpy(ncp,cp,sizeof(LinphoneCallParams));
799 void linphone_call_params_destroy(LinphoneCallParams *p){
808 #ifdef TEST_EXT_RENDERER
809 static void rendercb(void *data, const MSPicture *local, const MSPicture *remote){
810 ms_message("rendercb, local buffer=%p, remote buffer=%p",
811 local ? local->planes[0] : NULL, remote? remote->planes[0] : NULL);
816 static void video_stream_event_cb(void *user_pointer, const MSFilter *f, const unsigned int event_id, const void *args){
817 ms_warning("In linphonecall.c: video_stream_event_cb");
819 case MS_VIDEO_DECODER_DECODING_ERRORS:
820 ms_warning("Case is MS_VIDEO_DECODER_DECODING_ERRORS");
821 linphone_call_send_vfu_request((LinphoneCall*) user_pointer);
824 ms_warning("Unhandled event %i", event_id);
830 void linphone_call_init_media_streams(LinphoneCall *call){
831 LinphoneCore *lc=call->core;
832 SalMediaDescription *md=call->localdesc;
833 AudioStream *audiostream;
835 call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
836 if (linphone_core_echo_limiter_enabled(lc)){
837 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
838 if (strcasecmp(type,"mic")==0)
839 audio_stream_enable_echo_limiter(audiostream,ELControlMic);
840 else if (strcasecmp(type,"full")==0)
841 audio_stream_enable_echo_limiter(audiostream,ELControlFull);
843 audio_stream_enable_gain_control(audiostream,TRUE);
844 if (linphone_core_echo_cancellation_enabled(lc)){
845 int len,delay,framesize;
846 const char *statestr=lp_config_get_string(lc->config,"sound","ec_state",NULL);
847 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
848 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
849 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
850 audio_stream_set_echo_canceller_params(audiostream,len,delay,framesize);
851 if (statestr && audiostream->ec){
852 ms_filter_call_method(audiostream->ec,MS_ECHO_CANCELLER_SET_STATE_STRING,(void*)statestr);
855 audio_stream_enable_automatic_gain_control(audiostream,linphone_core_agc_enabled(lc));
857 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
858 audio_stream_enable_noise_gate(audiostream,enabled);
862 rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
864 call->audiostream_app_evq = ortp_ev_queue_new();
865 rtp_session_register_event_queue(audiostream->session,call->audiostream_app_evq);
869 if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
870 call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
871 if( lc->video_conf.displaytype != NULL)
872 video_stream_set_display_filter_name(call->videostream,lc->video_conf.displaytype);
873 video_stream_set_event_callback(call->videostream,video_stream_event_cb, call);
875 rtp_session_set_transports(call->videostream->session,lc->v_rtp,lc->v_rtcp);
876 call->videostream_app_evq = ortp_ev_queue_new();
877 rtp_session_register_event_queue(call->videostream->session,call->videostream_app_evq);
878 #ifdef TEST_EXT_RENDERER
879 video_stream_set_render_callback(call->videostream,rendercb,NULL);
883 call->videostream=NULL;
888 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
890 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
891 LinphoneCore* lc = (LinphoneCore*)user_data;
892 if (dtmf<0 || dtmf>15){
893 ms_warning("Bad dtmf value %i",dtmf);
896 if (lc->vtable.dtmf_received != NULL)
897 lc->vtable.dtmf_received(lc, linphone_core_get_current_call(lc), dtmf_tab[dtmf]);
900 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
902 MSFilter *f=st->equalizer;
903 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
904 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
905 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
911 if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
912 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
913 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
922 void _post_configure_audio_stream(AudioStream *st, LinphoneCore *lc, bool_t muted){
923 float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
926 float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
927 float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
928 int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
931 audio_stream_set_mic_gain(st,mic_gain);
933 audio_stream_set_mic_gain(st,0);
935 recv_gain = lc->sound_conf.soft_play_lev;
936 if (recv_gain != 0) {
937 linphone_core_set_playback_gain_db (lc,recv_gain);
941 ms_filter_call_method(st->volsend,MS_VOLUME_REMOVE_DC,&dc_removal);
942 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
943 thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
944 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
945 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
946 float transmit_thres=lp_config_get_float(lc->config,"sound","el_transmit_thres",-1);
949 if (speed==-1) speed=0.03;
950 if (force==-1) force=25;
951 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
952 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
954 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
956 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
957 if (transmit_thres!=-1)
958 ms_filter_call_method(f,MS_VOLUME_SET_EA_TRANSMIT_THRESHOLD,&transmit_thres);
960 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
961 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
964 /* parameters for a limited noise-gate effect, using echo limiter threshold */
965 float floorgain = 1/mic_gain;
966 int spk_agc=lp_config_get_int(lc->config,"sound","speaker_agc_enabled",0);
967 ms_filter_call_method(st->volrecv, MS_VOLUME_ENABLE_AGC, &spk_agc);
968 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
969 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
971 parametrize_equalizer(lc,st);
974 static void post_configure_audio_streams(LinphoneCall*call){
975 AudioStream *st=call->audiostream;
976 LinphoneCore *lc=call->core;
977 _post_configure_audio_stream(st,lc,call->audio_muted);
978 if (lc->vtable.dtmf_received!=NULL){
979 /* replace by our default action*/
980 audio_stream_play_received_dtmfs(call->audiostream,FALSE);
981 rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
985 static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
988 RtpProfile *prof=rtp_profile_new("Call profile");
991 LinphoneCore *lc=call->core;
995 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
996 PayloadType *pt=(PayloadType*)elem->data;
999 if ((pt->flags & PAYLOAD_TYPE_FLAG_CAN_SEND) && first) {
1000 if (desc->type==SalAudio){
1001 linphone_core_update_allocated_audio_bandwidth_in_call(call,pt);
1002 up_ptime=linphone_core_get_upload_ptime(lc);
1004 *used_pt=payload_type_get_number(pt);
1007 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
1008 else if (md->bandwidth>0) {
1009 /*case where b=AS is given globally, not per stream*/
1010 remote_bw=md->bandwidth;
1011 if (desc->type==SalVideo){
1012 remote_bw=get_video_bandwidth(remote_bw,call->audio_bw);
1016 if (desc->type==SalAudio){
1017 bw=get_min_bandwidth(call->audio_bw,remote_bw);
1018 }else bw=get_min_bandwidth(get_video_bandwidth(linphone_core_get_upload_bandwidth (lc),call->audio_bw),remote_bw);
1019 if (bw>0) pt->normal_bitrate=bw*1000;
1020 else if (desc->type==SalAudio){
1021 pt->normal_bitrate=-1;
1024 up_ptime=desc->ptime;
1028 snprintf(tmp,sizeof(tmp),"ptime=%i",up_ptime);
1029 payload_type_append_send_fmtp(pt,tmp);
1031 number=payload_type_get_number(pt);
1032 if (rtp_profile_get_payload(prof,number)!=NULL){
1033 ms_warning("A payload type with number %i already exists in profile !",number);
1035 rtp_profile_set_payload(prof,number,pt);
1041 static void setup_ring_player(LinphoneCore *lc, LinphoneCall *call){
1042 int pause_time=3000;
1043 audio_stream_play(call->audiostream,lc->sound_conf.ringback_tone);
1044 ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
1047 #define LINPHONE_RTCP_SDES_TOOL "Linphone-" LINPHONE_VERSION
1049 static bool_t linphone_call_sound_resources_available(LinphoneCall *call){
1050 LinphoneCore *lc=call->core;
1051 LinphoneCall *current=linphone_core_get_current_call(lc);
1052 return !linphone_core_is_in_conference(lc) &&
1053 (current==NULL || current==call);
1056 static void linphone_call_start_audio_stream(LinphoneCall *call, const char *cname, bool_t muted, bool_t send_ringbacktone, bool_t use_arc){
1057 LinphoneCore *lc=call->core;
1058 int jitt_comp=lc->rtp_conf.audio_jitt_comp;
1060 /* look for savp stream first */
1061 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
1062 SalProtoRtpSavp,SalAudio);
1063 /* no savp audio stream, use avp */
1065 stream=sal_media_description_find_stream(call->resultdesc,
1066 SalProtoRtpAvp,SalAudio);
1068 if (stream && stream->dir!=SalStreamInactive && stream->port!=0){
1069 MSSndCard *playcard=lc->sound_conf.lsd_card ?
1070 lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
1071 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
1072 const char *playfile=lc->play_file;
1073 const char *recfile=lc->rec_file;
1074 call->audio_profile=make_profile(call,call->resultdesc,stream,&used_pt);
1078 if (playcard==NULL) {
1079 ms_warning("No card defined for playback !");
1081 if (captcard==NULL) {
1082 ms_warning("No card defined for capture !");
1084 /*Replace soundcard filters by inactive file players or recorders
1085 when placed in recvonly or sendonly mode*/
1086 if (stream->port==0 || stream->dir==SalStreamRecvOnly){
1089 }else if (stream->dir==SalStreamSendOnly){
1093 /*And we will eventually play "playfile" if set by the user*/
1096 if (send_ringbacktone){
1098 playfile=NULL;/* it is setup later*/
1100 /*if playfile are supplied don't use soundcards*/
1101 if (lc->use_files) {
1105 if (call->params.in_conference){
1106 /* first create the graph without soundcard resources*/
1107 captcard=playcard=NULL;
1109 if (!linphone_call_sound_resources_available(call)){
1110 ms_message("Sound resources are used by another call, not using soundcard.");
1111 captcard=playcard=NULL;
1113 use_ec=captcard==NULL ? FALSE : linphone_core_echo_cancellation_enabled(lc);
1115 audio_stream_enable_adaptive_bitrate_control(call->audiostream,use_arc);
1116 audio_stream_start_full(
1118 call->audio_profile,
1119 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
1121 linphone_core_rtcp_enabled(lc) ? (stream->port+1) : 0,
1130 post_configure_audio_streams(call);
1131 if (muted && !send_ringbacktone){
1132 audio_stream_set_mic_gain(call->audiostream,0);
1134 if (stream->dir==SalStreamSendOnly && playfile!=NULL){
1136 ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
1138 if (send_ringbacktone){
1139 setup_ring_player(lc,call);
1141 audio_stream_set_rtcp_information(call->audiostream, cname, LINPHONE_RTCP_SDES_TOOL);
1142 if (call->params.in_conference){
1143 /*transform the graph to connect it to the conference filter */
1144 linphone_call_add_to_conf(call);
1147 if (stream->proto == SalProtoRtpSavp) {
1148 const SalStreamDescription *local_st_desc=sal_media_description_find_stream(call->localdesc,
1149 SalProtoRtpSavp,SalAudio);
1151 audio_stream_enable_strp(
1153 stream->crypto[0].algo,
1154 local_st_desc->crypto[0].master_key,
1155 stream->crypto[0].master_key);
1157 }else ms_warning("No audio stream accepted ?");
1161 static void linphone_call_start_video_stream(LinphoneCall *call, const char *cname,bool_t all_inputs_muted){
1162 #ifdef VIDEO_ENABLED
1163 LinphoneCore *lc=call->core;
1165 /* look for savp stream first */
1166 const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
1167 SalProtoRtpSavp,SalVideo);
1168 /* no savp audio stream, use avp */
1170 vstream=sal_media_description_find_stream(call->resultdesc,
1171 SalProtoRtpAvp,SalVideo);
1173 /* shutdown preview */
1174 if (lc->previewstream!=NULL) {
1175 video_preview_stop(lc->previewstream);
1176 lc->previewstream=NULL;
1178 call->current_params.has_video=FALSE;
1179 if (vstream!=NULL && vstream->dir!=SalStreamInactive && vstream->port!=0) {
1180 const char *addr=vstream->addr[0]!='\0' ? vstream->addr : call->resultdesc->addr;
1181 call->video_profile=make_profile(call,call->resultdesc,vstream,&used_pt);
1183 VideoStreamDir dir=VideoStreamSendRecv;
1184 MSWebCam *cam=lc->video_conf.device;
1185 bool_t is_inactive=FALSE;
1187 call->current_params.has_video=TRUE;
1189 video_stream_enable_adaptive_bitrate_control(call->videostream,
1190 linphone_core_adaptive_rate_control_enabled(lc));
1191 video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
1192 video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
1193 if (lc->video_window_id!=0)
1194 video_stream_set_native_window_id(call->videostream,lc->video_window_id);
1195 if (lc->preview_window_id!=0)
1196 video_stream_set_native_preview_window_id (call->videostream,lc->preview_window_id);
1197 video_stream_use_preview_video_window (call->videostream,lc->use_preview_window);
1199 if (vstream->dir==SalStreamSendOnly && lc->video_conf.capture ){
1200 cam=get_nowebcam_device();
1201 dir=VideoStreamSendOnly;
1202 }else if (vstream->dir==SalStreamRecvOnly && lc->video_conf.display ){
1203 dir=VideoStreamRecvOnly;
1204 }else if (vstream->dir==SalStreamSendRecv){
1205 if (lc->video_conf.display && lc->video_conf.capture)
1206 dir=VideoStreamSendRecv;
1207 else if (lc->video_conf.display)
1208 dir=VideoStreamRecvOnly;
1210 dir=VideoStreamSendOnly;
1212 ms_warning("video stream is inactive.");
1213 /*either inactive or incompatible with local capabilities*/
1216 if (call->camera_active==FALSE || all_inputs_muted){
1217 cam=get_nowebcam_device();
1220 video_stream_set_direction (call->videostream, dir);
1221 ms_message("%s lc rotation:%d\n", __FUNCTION__, lc->device_rotation);
1222 video_stream_set_device_rotation(call->videostream, lc->device_rotation);
1223 video_stream_start(call->videostream,
1224 call->video_profile, addr, vstream->port,
1225 linphone_core_rtcp_enabled(lc) ? (vstream->port+1) : 0,
1226 used_pt, lc->rtp_conf.audio_jitt_comp, cam);
1227 video_stream_set_rtcp_information(call->videostream, cname,LINPHONE_RTCP_SDES_TOOL);
1230 if (vstream->proto == SalProtoRtpSavp) {
1231 const SalStreamDescription *local_st_desc=sal_media_description_find_stream(call->localdesc,
1232 SalProtoRtpSavp,SalVideo);
1234 video_stream_enable_strp(
1236 vstream->crypto[0].algo,
1237 local_st_desc->crypto[0].master_key,
1238 vstream->crypto[0].master_key
1241 }else ms_warning("No video stream accepted.");
1243 ms_warning("No valid video stream defined.");
1248 void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_muted, bool_t send_ringbacktone){
1249 LinphoneCore *lc=call->core;
1250 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
1253 #ifdef VIDEO_ENABLED
1254 const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
1255 SalProtoRtpAvp,SalVideo);
1258 if(call->audiostream == NULL)
1260 ms_fatal("start_media_stream() called without prior init !");
1263 call->current_params = call->params;
1264 cname=linphone_address_as_string_uri_only(me);
1266 #if defined(VIDEO_ENABLED)
1267 if (vstream!=NULL && vstream->dir!=SalStreamInactive && vstream->payloads!=NULL){
1268 /*when video is used, do not make adaptive rate control on audio, it is stupid.*/
1272 linphone_call_start_audio_stream(call,cname,all_inputs_muted,send_ringbacktone,use_arc);
1273 if (call->videostream!=NULL) {
1274 linphone_call_start_video_stream(call,cname,all_inputs_muted);
1277 call->all_muted=all_inputs_muted;
1278 call->playing_ringbacktone=send_ringbacktone;
1279 call->up_bw=linphone_core_get_upload_bandwidth(lc);
1281 if (call->params.media_encryption==LinphoneMediaEncryptionZRTP) {
1282 OrtpZrtpParams params;
1283 /*will be set later when zrtp is activated*/
1284 call->current_params.media_encryption=LinphoneMediaEncryptionNone;
1286 params.zid=get_hexa_zrtp_identifier(lc);
1287 params.zid_file=lc->zrtp_secrets_cache;
1288 audio_stream_enable_zrtp(call->audiostream,¶ms);
1294 linphone_address_destroy(me);
1297 static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
1298 audio_stream_get_local_rtp_stats (st,&log->local_stats);
1299 log->quality=audio_stream_get_average_quality_rating(st);
1302 void linphone_call_stop_media_streams(LinphoneCall *call){
1303 if (call->audiostream!=NULL) {
1304 rtp_session_unregister_event_queue(call->audiostream->session,call->audiostream_app_evq);
1305 ortp_ev_queue_flush(call->audiostream_app_evq);
1306 ortp_ev_queue_destroy(call->audiostream_app_evq);
1308 if (call->audiostream->ec){
1309 const char *state_str=NULL;
1310 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_STATE_STRING,&state_str);
1312 ms_message("Writing echo canceller state, %i bytes",(int)strlen(state_str));
1313 lp_config_set_string(call->core->config,"sound","ec_state",state_str);
1316 linphone_call_log_fill_stats (call->log,call->audiostream);
1317 if (call->endpoint){
1318 linphone_call_remove_from_conf(call);
1320 audio_stream_stop(call->audiostream);
1321 call->audiostream=NULL;
1325 #ifdef VIDEO_ENABLED
1326 if (call->videostream!=NULL){
1327 rtp_session_unregister_event_queue(call->videostream->session,call->videostream_app_evq);
1328 ortp_ev_queue_flush(call->videostream_app_evq);
1329 ortp_ev_queue_destroy(call->videostream_app_evq);
1330 video_stream_stop(call->videostream);
1331 call->videostream=NULL;
1334 ms_event_queue_skip(call->core->msevq);
1336 if (call->audio_profile){
1337 rtp_profile_clear_all(call->audio_profile);
1338 rtp_profile_destroy(call->audio_profile);
1339 call->audio_profile=NULL;
1341 if (call->video_profile){
1342 rtp_profile_clear_all(call->video_profile);
1343 rtp_profile_destroy(call->video_profile);
1344 call->video_profile=NULL;
1350 void linphone_call_enable_echo_cancellation(LinphoneCall *call, bool_t enable) {
1351 if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1352 bool_t bypass_mode = !enable;
1353 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_SET_BYPASS_MODE,&bypass_mode);
1356 bool_t linphone_call_echo_cancellation_enabled(LinphoneCall *call) {
1357 if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1359 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_BYPASS_MODE,&val);
1362 return linphone_core_echo_cancellation_enabled(call->core);
1366 void linphone_call_enable_echo_limiter(LinphoneCall *call, bool_t val){
1367 if (call!=NULL && call->audiostream!=NULL ) {
1369 const char *type=lp_config_get_string(call->core->config,"sound","el_type","mic");
1370 if (strcasecmp(type,"mic")==0)
1371 audio_stream_enable_echo_limiter(call->audiostream,ELControlMic);
1372 else if (strcasecmp(type,"full")==0)
1373 audio_stream_enable_echo_limiter(call->audiostream,ELControlFull);
1375 audio_stream_enable_echo_limiter(call->audiostream,ELInactive);
1380 bool_t linphone_call_echo_limiter_enabled(const LinphoneCall *call){
1381 if (call!=NULL && call->audiostream!=NULL ){
1382 return call->audiostream->el_type !=ELInactive ;
1384 return linphone_core_echo_limiter_enabled(call->core);
1389 * @addtogroup call_misc
1394 * Returns the measured sound volume played locally (received from remote)
1395 * It is expressed in dbm0.
1397 float linphone_call_get_play_volume(LinphoneCall *call){
1398 AudioStream *st=call->audiostream;
1399 if (st && st->volrecv){
1401 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&vol);
1405 return LINPHONE_VOLUME_DB_LOWEST;
1409 * Returns the measured sound volume recorded locally (sent to remote)
1410 * It is expressed in dbm0.
1412 float linphone_call_get_record_volume(LinphoneCall *call){
1413 AudioStream *st=call->audiostream;
1414 if (st && st->volsend && !call->audio_muted && call->state==LinphoneCallStreamsRunning){
1416 ms_filter_call_method(st->volsend,MS_VOLUME_GET,&vol);
1420 return LINPHONE_VOLUME_DB_LOWEST;
1424 * Obtain real-time quality rating of the call
1426 * Based on local RTP statistics and RTCP feedback, a quality rating is computed and updated
1427 * during all the duration of the call. This function returns its value at the time of the function call.
1428 * It is expected that the rating is updated at least every 5 seconds or so.
1429 * The rating is a floating point number comprised between 0 and 5.
1431 * 4-5 = good quality <br>
1432 * 3-4 = average quality <br>
1433 * 2-3 = poor quality <br>
1434 * 1-2 = very poor quality <br>
1435 * 0-1 = can't be worse, mostly unusable <br>
1437 * @returns The function returns -1 if no quality measurement is available, for example if no
1438 * active audio stream exist. Otherwise it returns the quality rating.
1440 float linphone_call_get_current_quality(LinphoneCall *call){
1441 if (call->audiostream){
1442 return audio_stream_get_quality_rating(call->audiostream);
1448 * Returns call quality averaged over all the duration of the call.
1450 * See linphone_call_get_current_quality() for more details about quality measurement.
1452 float linphone_call_get_average_quality(LinphoneCall *call){
1453 if (call->audiostream){
1454 return audio_stream_get_average_quality_rating(call->audiostream);
1463 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1464 ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1465 (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1466 (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1467 (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1468 (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1471 static void linphone_core_disconnected(LinphoneCore *lc, LinphoneCall *call){
1475 from = linphone_call_get_remote_address_as_string(call);
1478 snprintf(temp,sizeof(temp),"Remote end %s seems to have disconnected, the call is going to be closed.",from);
1483 snprintf(temp,sizeof(temp),"Remote end seems to have disconnected, the call is going to be closed.");
1485 if (lc->vtable.display_warning!=NULL)
1486 lc->vtable.display_warning(lc,temp);
1487 linphone_core_terminate_call(lc,call);
1490 void linphone_call_background_tasks(LinphoneCall *call, bool_t one_second_elapsed){
1491 int disconnect_timeout = linphone_core_get_nortp_timeout(call->core);
1492 bool_t disconnected=FALSE;
1494 if (call->state==LinphoneCallStreamsRunning && one_second_elapsed){
1495 RtpSession *as=NULL,*vs=NULL;
1496 float audio_load=0, video_load=0;
1497 if (call->audiostream!=NULL){
1498 as=call->audiostream->session;
1499 if (call->audiostream->ticker)
1500 audio_load=ms_ticker_get_average_load(call->audiostream->ticker);
1502 if (call->videostream!=NULL){
1503 if (call->videostream->ticker)
1504 video_load=ms_ticker_get_average_load(call->videostream->ticker);
1505 vs=call->videostream->session;
1507 display_bandwidth(as,vs);
1508 ms_message("Thread processing load: audio=%f\tvideo=%f",audio_load,video_load);
1510 #ifdef VIDEO_ENABLED
1511 if (call->videostream!=NULL) {
1512 // Beware that the application queue should not depend on treatments fron the
1513 // mediastreamer queue.
1514 video_stream_iterate(call->videostream);
1516 if (call->videostream_app_evq){
1518 while (NULL != (ev=ortp_ev_queue_get(call->videostream_app_evq))){
1519 OrtpEventType evt=ortp_event_get_type(ev);
1520 if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
1521 OrtpEventData *evd=ortp_event_get_data(ev);
1522 linphone_call_videostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
1524 ortp_event_destroy(ev);
1529 if (call->audiostream!=NULL) {
1530 // Beware that the application queue should not depend on treatments fron the
1531 // mediastreamer queue.
1532 audio_stream_iterate(call->audiostream);
1534 if (call->audiostream->evq){
1536 while (NULL != (ev=ortp_ev_queue_get(call->audiostream_app_evq))){
1537 OrtpEventType evt=ortp_event_get_type(ev);
1538 if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
1539 OrtpEventData *evd=ortp_event_get_data(ev);
1540 linphone_call_audiostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
1541 } else if (evt == ORTP_EVENT_ZRTP_SAS_READY) {
1542 OrtpEventData *evd=ortp_event_get_data(ev);
1543 linphone_call_audiostream_auth_token_ready(call, evd->info.zrtp_sas.sas, evd->info.zrtp_sas.verified);
1545 ortp_event_destroy(ev);
1549 if (call->state==LinphoneCallStreamsRunning && one_second_elapsed && call->audiostream!=NULL && disconnect_timeout>0 )
1550 disconnected=!audio_stream_alive(call->audiostream,disconnect_timeout);
1552 linphone_core_disconnected(call->core,call);
1555 void linphone_call_log_completed(LinphoneCall *call){
1556 LinphoneCore *lc=call->core;
1558 call->log->duration=time(NULL)-call->start_time;
1560 if (call->log->status==LinphoneCallMissed){
1563 info=ortp_strdup_printf(ngettext("You have missed %i call.",
1564 "You have missed %i calls.", lc->missed_calls),
1566 if (lc->vtable.display_status!=NULL)
1567 lc->vtable.display_status(lc,info);
1570 lc->call_logs=ms_list_prepend(lc->call_logs,(void *)call->log);
1571 if (ms_list_size(lc->call_logs)>lc->max_call_logs){
1572 MSList *elem,*prevelem=NULL;
1573 /*find the last element*/
1574 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
1578 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
1579 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
1581 if (lc->vtable.call_log_updated!=NULL){
1582 lc->vtable.call_log_updated(lc,call->log);
1584 call_logs_write_to_config_file(lc);