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"
30 #include "mediastreamer2/mediastream.h"
31 #include "mediastreamer2/msvolume.h"
32 #include "mediastreamer2/msequalizer.h"
33 #include "mediastreamer2/msfileplayer.h"
34 #include "mediastreamer2/msjpegwriter.h"
35 #include "mediastreamer2/mseventqueue.h"
38 static MSWebCam *get_nowebcam_device(){
39 return ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),"StaticImage: Static picture");
43 static const char* get_zrtp_identifier(LinphoneCore *lc){
44 const char *confZid=lp_config_get_string(lc->config,"rtp","zid",NULL);
45 if (confZid != NULL) {
48 int32_t *zid=calloc(3,32);
53 lp_config_set_string(lc->config,"rtp","zid",(char*)zid);
54 return lp_config_get_string(lc->config,"rtp","zid",NULL);
58 const char* linphone_call_get_authentication_token(LinphoneCall *call){
59 return call->audiostream->auth_token;
62 bool_t linphone_call_get_authentication_token_verified(LinphoneCall *call){
63 return call->audiostream->auth_token_verified;
65 bool_t linphone_call_are_all_streams_encrypted(LinphoneCall *call) {
66 // Check ZRTP encryption in audiostream
67 if (!call->audiostream->encrypted) {
72 // If video enabled, check ZRTP encryption in videostream
73 const LinphoneCallParams *params=linphone_call_get_current_params(call);
74 if (params->has_video && !call->videostream->encrypted) {
82 void propagate_encryption_changed(LinphoneCall *call){
83 if (call->core->vtable.call_encryption_changed == NULL) return;
85 if (!linphone_call_are_all_streams_encrypted(call)) {
86 call->core->vtable.call_encryption_changed(call->core, call, FALSE, NULL);
88 call->core->vtable.call_encryption_changed(call->core, call, TRUE, call->audiostream->auth_token);
93 static void linphone_call_videostream_encryption_changed(void *data, bool_t encrypted){
94 ms_message("Video stream is %s", encrypted ? "encrypted" : "not encrypted");
96 LinphoneCall *call = (LinphoneCall *)data;
97 call->videostream->encrypted=encrypted;
98 propagate_encryption_changed(call);
102 static void linphone_call_audiostream_encryption_changed(void *data, bool_t encrypted) {
103 ms_message("Audio stream is %s ", encrypted ? "encrypted" : "not encrypted");
105 LinphoneCall *call = (LinphoneCall *)data;
106 call->audiostream->encrypted=encrypted;
107 propagate_encryption_changed(call);
111 // Enable video encryption
112 const LinphoneCallParams *params=linphone_call_get_current_params(call);
113 if (params->has_video) {
114 ms_message("Trying to enable encryption on video stream");
115 OrtpZrtpParams params;
116 params.zid=get_zrtp_identifier(call->core);
117 params.zid_file=NULL; //unused
118 video_stream_enable_zrtp(call->videostream,call->audiostream,¶ms);
124 static void linphone_call_audiostream_auth_token_ready(void *data, const char* auth_token, bool_t verified) {
125 LinphoneCall *call=(LinphoneCall *)data;
126 if (call->audiostream->auth_token != NULL)
127 ms_free(call->audiostream->auth_token);
129 call->audiostream->auth_token=ms_strdup(auth_token);
130 call->audiostream->auth_token_verified=verified;
132 ms_message("Authentication token is %s (%s)", auth_token, verified?"verified":"unverified");
136 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandwidth_limit){
139 for(it=codecs;it!=NULL;it=it->next){
140 PayloadType *pt=(PayloadType*)it->data;
141 if (pt->flags & PAYLOAD_TYPE_ENABLED){
142 if (bandwidth_limit>0 && !linphone_core_is_payload_type_usable_for_bandwidth(lc,pt,bandwidth_limit)){
143 ms_message("Codec %s/%i eliminated because of audio bandwidth constraint.",pt->mime_type,pt->clock_rate);
146 if (linphone_core_check_payload_type_usability(lc,pt)){
147 l=ms_list_append(l,payload_type_clone(pt));
154 static SalMediaDescription *_create_local_media_description(LinphoneCore *lc, LinphoneCall *call, unsigned int session_id, unsigned int session_ver){
157 const char *me=linphone_core_get_identity(lc);
158 LinphoneAddress *addr=linphone_address_new(me);
159 const char *username=linphone_address_get_username (addr);
160 SalMediaDescription *md=sal_media_description_new();
162 md->session_id=session_id;
163 md->session_ver=session_ver;
165 strncpy(md->addr,call->localip,sizeof(md->addr));
166 strncpy(md->username,username,sizeof(md->username));
167 md->bandwidth=linphone_core_get_download_bandwidth(lc);
169 /*set audio capabilities */
170 strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
171 md->streams[0].port=call->audio_port;
172 md->streams[0].proto=SalProtoRtpAvp;
173 md->streams[0].type=SalAudio;
174 md->streams[0].ptime=lc->net_conf.down_ptime;
175 l=make_codec_list(lc,lc->codecs_conf.audio_codecs,call->params.audio_bw);
176 pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
177 l=ms_list_append(l,pt);
178 md->streams[0].payloads=l;
181 if (call->params.has_video){
183 md->streams[1].port=call->video_port;
184 md->streams[1].proto=SalProtoRtpAvp;
185 md->streams[1].type=SalVideo;
186 l=make_codec_list(lc,lc->codecs_conf.video_codecs,0);
187 md->streams[1].payloads=l;
189 linphone_address_destroy(addr);
193 void update_local_media_description(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription **md){
195 *md = _create_local_media_description(lc,call,0,0);
197 unsigned int id = (*md)->session_id;
198 unsigned int ver = (*md)->session_ver+1;
199 sal_media_description_unref(*md);
200 *md = _create_local_media_description(lc,call,id,ver);
204 SalMediaDescription *create_local_media_description(LinphoneCore *lc, LinphoneCall *call){
205 unsigned int id=rand() & 0xfff;
206 return _create_local_media_description(lc,call,id,id);
209 static int find_port_offset(LinphoneCore *lc){
213 bool_t already_used=FALSE;
214 for(offset=0;offset<100;offset+=2){
215 audio_port=linphone_core_get_audio_port (lc)+offset;
217 for(elem=lc->calls;elem!=NULL;elem=elem->next){
218 LinphoneCall *call=(LinphoneCall*)elem->data;
219 if (call->audio_port==audio_port) {
224 if (!already_used) break;
227 ms_error("Could not find any free port !");
233 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
236 call->state=LinphoneCallIdle;
237 call->start_time=time(NULL);
238 call->media_start_time=0;
239 call->log=linphone_call_log_new(call, from, to);
240 call->owns_call_log=TRUE;
241 linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
242 port_offset=find_port_offset (call->core);
243 if (port_offset==-1) return;
244 call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
245 call->video_port=linphone_core_get_video_port(call->core)+port_offset;
249 static void discover_mtu(LinphoneCore *lc, const char *remote){
251 if (lc->net_conf.mtu==0 ){
252 /*attempt to discover mtu*/
253 mtu=ms_discover_mtu(remote);
256 ms_message("Discovered mtu is %i, RTP payload max size is %i",
257 mtu, ms_get_payload_max_size());
262 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, const LinphoneCallParams *params)
264 LinphoneCall *call=ms_new0(LinphoneCall,1);
265 call->dir=LinphoneCallOutgoing;
266 call->op=sal_op_new(lc->sal);
267 sal_op_set_user_pointer(call->op,call);
269 linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
270 linphone_call_init_common(call,from,to);
271 call->params=*params;
272 call->localdesc=create_local_media_description (lc,call);
273 call->camera_active=params->has_video;
274 if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
275 linphone_core_run_stun_tests(call->core,call);
276 discover_mtu(lc,linphone_address_get_domain (to));
277 if (params->referer){
278 sal_call_set_referer (call->op,params->referer->op);
283 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
284 LinphoneCall *call=ms_new0(LinphoneCall,1);
287 call->dir=LinphoneCallIncoming;
288 sal_op_set_user_pointer(op,call);
292 if (lc->sip_conf.ping_with_options){
293 /*the following sends an option request back to the caller so that
294 we get a chance to discover our nat'd address before answering.*/
295 call->ping_op=sal_op_new(lc->sal);
296 from_str=linphone_address_as_string(from);
297 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
298 sal_op_set_user_pointer(call->ping_op,call);
299 sal_ping(call->ping_op,linphone_core_find_best_identity(lc,from,NULL),from_str);
303 linphone_address_clean(from);
304 linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
305 linphone_call_init_common(call, from, to);
306 call->params.has_video=linphone_core_video_enabled(lc);
307 call->localdesc=create_local_media_description (lc,call);
308 call->camera_active=call->params.has_video;
309 if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
310 linphone_core_run_stun_tests(call->core,call);
311 discover_mtu(lc,linphone_address_get_domain(from));
315 /* this function is called internally to get rid of a call.
316 It performs the following tasks:
317 - remove the call from the internal list of calls
318 - update the call logs accordingly
321 static void linphone_call_set_terminated(LinphoneCall *call){
322 LinphoneCore *lc=call->core;
324 linphone_core_update_allocated_audio_bandwidth(lc);
326 call->owns_call_log=FALSE;
327 linphone_call_log_completed(call);
330 if (call == lc->current_call){
331 ms_message("Resetting the current call");
332 lc->current_call=NULL;
335 if (linphone_core_del_call(lc,call) != 0){
336 ms_error("Could not remove the call from the list !!!");
339 if (ms_list_size(lc->calls)==0)
340 linphone_core_notify_all_friends(lc,lc->presence_mode);
344 const char *linphone_call_state_to_string(LinphoneCallState cs){
346 case LinphoneCallIdle:
347 return "LinphoneCallIdle";
348 case LinphoneCallIncomingReceived:
349 return "LinphoneCallIncomingReceived";
350 case LinphoneCallOutgoingInit:
351 return "LinphoneCallOutgoingInit";
352 case LinphoneCallOutgoingProgress:
353 return "LinphoneCallOutgoingProgress";
354 case LinphoneCallOutgoingRinging:
355 return "LinphoneCallOutgoingRinging";
356 case LinphoneCallOutgoingEarlyMedia:
357 return "LinphoneCallOutgoingEarlyMedia";
358 case LinphoneCallConnected:
359 return "LinphoneCallConnected";
360 case LinphoneCallStreamsRunning:
361 return "LinphoneCallStreamsRunning";
362 case LinphoneCallPausing:
363 return "LinphoneCallPausing";
364 case LinphoneCallPaused:
365 return "LinphoneCallPaused";
366 case LinphoneCallResuming:
367 return "LinphoneCallResuming";
368 case LinphoneCallRefered:
369 return "LinphoneCallRefered";
370 case LinphoneCallError:
371 return "LinphoneCallError";
372 case LinphoneCallEnd:
373 return "LinphoneCallEnd";
374 case LinphoneCallPausedByRemote:
375 return "LinphoneCallPausedByRemote";
376 case LinphoneCallUpdatedByRemote:
377 return "LinphoneCallUpdatedByRemote";
378 case LinphoneCallIncomingEarlyMedia:
379 return "LinphoneCallIncomingEarlyMedia";
380 case LinphoneCallUpdated:
381 return "LinphoneCallUpdated";
382 case LinphoneCallReleased:
383 return "LinphoneCallReleased";
385 return "undefined state";
388 void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const char *message){
389 LinphoneCore *lc=call->core;
391 if (call->state!=cstate){
392 if (call->state==LinphoneCallEnd || call->state==LinphoneCallError){
393 if (cstate!=LinphoneCallReleased){
394 ms_warning("Spurious call state change from %s to %s, ignored.",linphone_call_state_to_string(call->state),
395 linphone_call_state_to_string(cstate));
399 ms_message("Call %p: moving from state %s to %s",call,linphone_call_state_to_string(call->state),
400 linphone_call_state_to_string(cstate));
401 if (cstate!=LinphoneCallRefered){
402 /*LinphoneCallRefered is rather an event, not a state.
403 Indeed it does not change the state of the call (still paused or running)*/
406 if (cstate==LinphoneCallEnd || cstate==LinphoneCallError){
407 if (call->reason==LinphoneReasonDeclined){
408 call->log->status=LinphoneCallDeclined;
410 linphone_call_set_terminated (call);
412 if (cstate == LinphoneCallConnected) {
413 call->log->status=LinphoneCallSuccess;
416 if (lc->vtable.call_state_changed)
417 lc->vtable.call_state_changed(lc,call,cstate,message);
418 if (cstate==LinphoneCallReleased){
419 if (call->op!=NULL) {
420 /* so that we cannot have anymore upcalls for SAL
421 concerning this call*/
422 sal_op_release(call->op);
425 linphone_call_unref(call);
430 static void linphone_call_destroy(LinphoneCall *obj)
433 sal_op_release(obj->op);
436 if (obj->resultdesc!=NULL) {
437 sal_media_description_unref(obj->resultdesc);
438 obj->resultdesc=NULL;
440 if (obj->localdesc!=NULL) {
441 sal_media_description_unref(obj->localdesc);
445 sal_op_release(obj->ping_op);
448 ms_free(obj->refer_to);
450 if (obj->owns_call_log)
451 linphone_call_log_destroy(obj->log);
456 * @addtogroup call_control
461 * Increments the call 's reference count.
462 * An application that wishes to retain a pointer to call object
463 * must use this function to unsure the pointer remains
464 * valid. Once the application no more needs this pointer,
465 * it must call linphone_call_unref().
467 void linphone_call_ref(LinphoneCall *obj){
472 * Decrements the call object reference count.
473 * See linphone_call_ref().
475 void linphone_call_unref(LinphoneCall *obj){
478 linphone_call_destroy(obj);
483 * Returns current parameters associated to the call.
485 const LinphoneCallParams * linphone_call_get_current_params(const LinphoneCall *call){
486 return &call->current_params;
490 * Returns the remote address associated to this call
493 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
494 return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
498 * Returns the remote address associated to this call as a string.
500 * The result string must be freed by user using ms_free().
502 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
503 return linphone_address_as_string(linphone_call_get_remote_address(call));
507 * Retrieves the call's current state.
509 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
514 * Returns the reason for a call termination (either error or normal termination)
516 LinphoneReason linphone_call_get_reason(const LinphoneCall *call){
521 * Get the user_pointer in the LinphoneCall
523 * @ingroup call_control
525 * return user_pointer an opaque user pointer that can be retrieved at any time
527 void *linphone_call_get_user_pointer(LinphoneCall *call)
529 return call->user_pointer;
533 * Set the user_pointer in the LinphoneCall
535 * @ingroup call_control
537 * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
539 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
541 call->user_pointer = user_pointer;
545 * Returns the call log associated to this call.
547 LinphoneCallLog *linphone_call_get_call_log(const LinphoneCall *call){
552 * Returns the refer-to uri (if the call was transfered).
554 const char *linphone_call_get_refer_to(const LinphoneCall *call){
555 return call->refer_to;
559 * Returns direction of the call (incoming or outgoing).
561 LinphoneCallDir linphone_call_get_dir(const LinphoneCall *call){
562 return call->log->dir;
566 * Returns the far end's user agent description string, if available.
568 const char *linphone_call_get_remote_user_agent(LinphoneCall *call){
570 return sal_op_get_remote_ua (call->op);
576 * Returns true if this calls has received a transfer that has not been
578 * Pending transfers are executed when this call is being paused or closed,
579 * locally or by remote endpoint.
580 * If the call is already paused while receiving the transfer request, the
581 * transfer immediately occurs.
583 bool_t linphone_call_has_transfer_pending(const LinphoneCall *call){
584 return call->refer_pending;
588 * Returns call's duration in seconds.
590 int linphone_call_get_duration(const LinphoneCall *call){
591 if (call->media_start_time==0) return 0;
592 return time(NULL)-call->media_start_time;
596 * Returns the call object this call is replacing, if any.
597 * Call replacement can occur during call transfers.
598 * By default, the core automatically terminates the replaced call and accept the new one.
599 * This function allows the application to know whether a new incoming call is a one that replaces another one.
601 LinphoneCall *linphone_call_get_replaced_call(LinphoneCall *call){
602 SalOp *op=sal_call_get_replaces(call->op);
604 return (LinphoneCall*)sal_op_get_user_pointer(op);
610 * Indicate whether camera input should be sent to remote end.
612 void linphone_call_enable_camera (LinphoneCall *call, bool_t enable){
614 if (call->videostream!=NULL && call->videostream->ticker!=NULL){
615 LinphoneCore *lc=call->core;
616 MSWebCam *nowebcam=get_nowebcam_device();
617 if (call->camera_active!=enable && lc->video_conf.device!=nowebcam){
618 video_stream_change_camera(call->videostream,
619 enable ? lc->video_conf.device : nowebcam);
622 call->camera_active=enable;
627 * Take a photo of currently received video and write it into a jpeg file.
629 int linphone_call_take_video_snapshot(LinphoneCall *call, const char *file){
631 if (call->videostream!=NULL && call->videostream->jpegwriter!=NULL){
632 return ms_filter_call_method(call->videostream->jpegwriter,MS_JPEG_WRITER_TAKE_SNAPSHOT,(void*)file);
634 ms_warning("Cannot take snapshot: no currently running video stream on this call.");
641 * Returns TRUE if camera pictures are sent to the remote party.
643 bool_t linphone_call_camera_enabled (const LinphoneCall *call){
644 return call->camera_active;
648 * Enable video stream.
650 void linphone_call_params_enable_video(LinphoneCallParams *cp, bool_t enabled){
651 cp->has_video=enabled;
655 * Returns whether video is enabled.
657 bool_t linphone_call_params_video_enabled(const LinphoneCallParams *cp){
658 return cp->has_video;
662 * Enable sending of real early media (during outgoing calls).
664 void linphone_call_params_enable_early_media_sending(LinphoneCallParams *cp, bool_t enabled){
665 cp->real_early_media=enabled;
668 bool_t linphone_call_params_early_media_sending_enabled(const LinphoneCallParams *cp){
669 return cp->real_early_media;
673 * Refine bandwidth settings for this call by setting a bandwidth limit for audio streams.
674 * As a consequence, codecs whose bitrates are not compatible with this limit won't be used.
676 void linphone_call_params_set_audio_bandwidth_limit(LinphoneCallParams *cp, int bandwidth){
677 cp->audio_bw=bandwidth;
682 * Request remote side to send us a Video Fast Update.
684 void linphone_call_send_vfu_request(LinphoneCall *call)
686 if (LinphoneCallStreamsRunning == linphone_call_get_state(call))
687 sal_call_send_vfu_request(call->op);
694 LinphoneCallParams * linphone_call_params_copy(const LinphoneCallParams *cp){
695 LinphoneCallParams *ncp=ms_new0(LinphoneCallParams,1);
696 memcpy(ncp,cp,sizeof(LinphoneCallParams));
703 void linphone_call_params_destroy(LinphoneCallParams *p){
712 #ifdef TEST_EXT_RENDERER
713 static void rendercb(void *data, const MSPicture *local, const MSPicture *remote){
714 ms_message("rendercb, local buffer=%p, remote buffer=%p",
715 local ? local->planes[0] : NULL, remote? remote->planes[0] : NULL);
720 static void video_stream_event_cb(void *user_pointer, const MSFilter *f, const unsigned int event_id, const void *args){
721 ms_warning("In linphonecall.c: video_stream_event_cb");
723 case MS_VIDEO_DECODER_DECODING_ERRORS:
724 ms_warning("Case is MS_VIDEO_DECODER_DECODING_ERRORS");
725 linphone_call_send_vfu_request((LinphoneCall*) user_pointer);
728 ms_warning("Unhandled event %i", event_id);
734 void linphone_call_init_media_streams(LinphoneCall *call){
735 LinphoneCore *lc=call->core;
736 SalMediaDescription *md=call->localdesc;
737 AudioStream *audiostream;
739 call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
740 if (linphone_core_echo_limiter_enabled(lc)){
741 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
742 if (strcasecmp(type,"mic")==0)
743 audio_stream_enable_echo_limiter(audiostream,ELControlMic);
744 else if (strcasecmp(type,"full")==0)
745 audio_stream_enable_echo_limiter(audiostream,ELControlFull);
747 audio_stream_enable_gain_control(audiostream,TRUE);
748 if (linphone_core_echo_cancellation_enabled(lc)){
749 int len,delay,framesize;
750 const char *statestr=lp_config_get_string(lc->config,"sound","ec_state",NULL);
751 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
752 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
753 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
754 audio_stream_set_echo_canceller_params(audiostream,len,delay,framesize);
755 if (statestr && audiostream->ec){
756 ms_filter_call_method(audiostream->ec,MS_ECHO_CANCELLER_SET_STATE_STRING,(void*)statestr);
759 audio_stream_enable_automatic_gain_control(audiostream,linphone_core_agc_enabled(lc));
761 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
762 audio_stream_enable_noise_gate(audiostream,enabled);
766 rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
770 if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
771 call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
772 if( lc->video_conf.displaytype != NULL)
773 video_stream_set_display_filter_name(call->videostream,lc->video_conf.displaytype);
774 video_stream_set_event_callback(call->videostream,video_stream_event_cb, call);
776 rtp_session_set_transports(call->videostream->session,lc->v_rtp,lc->v_rtcp);
777 #ifdef TEST_EXT_RENDERER
778 video_stream_set_render_callback(call->videostream,rendercb,NULL);
782 call->videostream=NULL;
787 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
789 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
790 LinphoneCore* lc = (LinphoneCore*)user_data;
791 if (dtmf<0 || dtmf>15){
792 ms_warning("Bad dtmf value %i",dtmf);
795 if (lc->vtable.dtmf_received != NULL)
796 lc->vtable.dtmf_received(lc, linphone_core_get_current_call(lc), dtmf_tab[dtmf]);
799 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
801 MSFilter *f=st->equalizer;
802 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
803 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
804 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
810 if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
811 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
812 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
822 static void post_configure_audio_streams(LinphoneCall*call){
823 AudioStream *st=call->audiostream;
824 LinphoneCore *lc=call->core;
825 float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
828 float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
829 float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
830 int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
832 if (!call->audio_muted)
833 audio_stream_set_mic_gain(st,mic_gain);
835 audio_stream_set_mic_gain(st,0);
837 recv_gain = lc->sound_conf.soft_play_lev;
838 if (recv_gain != 0) {
839 linphone_core_set_playback_gain_db (lc,recv_gain);
842 ms_filter_call_method(st->volsend,MS_VOLUME_REMOVE_DC,&dc_removal);
843 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
844 thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
845 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
846 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
847 float transmit_thres=lp_config_get_float(lc->config,"sound","el_transmit_thres",-1);
850 if (speed==-1) speed=0.03;
851 if (force==-1) force=25;
852 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
853 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
855 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
857 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
858 if (transmit_thres!=-1)
859 ms_filter_call_method(f,MS_VOLUME_SET_EA_TRANSMIT_THRESHOLD,&transmit_thres);
861 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
862 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
865 /* parameters for a limited noise-gate effect, using echo limiter threshold */
866 float floorgain = 1/mic_gain;
867 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&thres);
868 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
870 parametrize_equalizer(lc,st);
871 if (lc->vtable.dtmf_received!=NULL){
872 /* replace by our default action*/
873 audio_stream_play_received_dtmfs(call->audiostream,FALSE);
874 rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
878 static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
881 RtpProfile *prof=rtp_profile_new("Call profile");
884 LinphoneCore *lc=call->core;
888 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
889 PayloadType *pt=(PayloadType*)elem->data;
892 if ((pt->flags & PAYLOAD_TYPE_FLAG_CAN_SEND) && first) {
893 if (desc->type==SalAudio){
894 linphone_core_update_allocated_audio_bandwidth_in_call(call,pt);
895 up_ptime=linphone_core_get_upload_ptime(lc);
897 *used_pt=payload_type_get_number(pt);
900 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
901 else if (md->bandwidth>0) {
902 /*case where b=AS is given globally, not per stream*/
903 remote_bw=md->bandwidth;
904 if (desc->type==SalVideo){
905 remote_bw=get_video_bandwidth(remote_bw,call->audio_bw);
909 if (desc->type==SalAudio){
910 bw=get_min_bandwidth(call->audio_bw,remote_bw);
911 }else bw=get_min_bandwidth(get_video_bandwidth(linphone_core_get_upload_bandwidth (lc),call->audio_bw),remote_bw);
912 if (bw>0) pt->normal_bitrate=bw*1000;
913 else if (desc->type==SalAudio){
914 pt->normal_bitrate=-1;
917 up_ptime=desc->ptime;
921 snprintf(tmp,sizeof(tmp),"ptime=%i",up_ptime);
922 payload_type_append_send_fmtp(pt,tmp);
924 number=payload_type_get_number(pt);
925 if (rtp_profile_get_payload(prof,number)!=NULL){
926 ms_warning("A payload type with number %i already exists in profile !",number);
928 rtp_profile_set_payload(prof,number,pt);
934 static void setup_ring_player(LinphoneCore *lc, LinphoneCall *call){
936 audio_stream_play(call->audiostream,lc->sound_conf.ringback_tone);
937 ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
941 void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_muted, bool_t send_ringbacktone){
942 LinphoneCore *lc=call->core;
943 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
944 const char *tool="linphone-" LINPHONE_VERSION;
948 const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
949 SalProtoRtpAvp,SalVideo);
951 bool_t use_arc=linphone_core_adaptive_rate_control_enabled(lc);
953 if(call->audiostream == NULL)
955 ms_fatal("start_media_stream() called without prior init !");
958 call->current_params = call->params;
959 /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
960 int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
962 if (call->media_start_time==0) call->media_start_time=time(NULL);
964 cname=linphone_address_as_string_uri_only(me);
966 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
967 SalProtoRtpAvp,SalAudio);
968 if (stream && stream->dir!=SalStreamInactive && stream->port!=0){
969 MSSndCard *playcard=lc->sound_conf.lsd_card ?
970 lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
971 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
972 const char *playfile=lc->play_file;
973 const char *recfile=lc->rec_file;
974 call->audio_profile=make_profile(call,call->resultdesc,stream,&used_pt);
975 bool_t use_ec,use_arc_audio=use_arc;
978 if (playcard==NULL) {
979 ms_warning("No card defined for playback !");
981 if (captcard==NULL) {
982 ms_warning("No card defined for capture !");
984 /*Replace soundcard filters by inactive file players or recorders
985 when placed in recvonly or sendonly mode*/
986 if (stream->port==0 || stream->dir==SalStreamRecvOnly){
989 }else if (stream->dir==SalStreamSendOnly){
993 /*And we will eventually play "playfile" if set by the user*/
996 if (send_ringbacktone){
998 playfile=NULL;/* it is setup later*/
1000 /*if playfile are supplied don't use soundcards*/
1001 if (lc->use_files) {
1005 use_ec=captcard==NULL ? FALSE : linphone_core_echo_cancellation_enabled(lc);
1006 #if defined(VIDEO_ENABLED)
1007 if (vstream && vstream->dir!=SalStreamInactive && vstream->payloads!=NULL){
1008 /*when video is used, do not make adaptive rate control on audio, it is stupid.*/
1009 use_arc_audio=FALSE;
1010 #if defined(ANDROID)
1011 /*On android we have to disable the echo canceller to preserve CPU for video codecs */
1016 audio_stream_enable_adaptive_bitrate_control(call->audiostream,use_arc_audio);
1017 audio_stream_start_full(
1019 call->audio_profile,
1020 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
1031 post_configure_audio_streams(call);
1032 if (all_inputs_muted && !send_ringbacktone){
1033 audio_stream_set_mic_gain(call->audiostream,0);
1035 if (stream->dir==SalStreamSendOnly && playfile!=NULL){
1037 ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
1039 if (send_ringbacktone){
1040 setup_ring_player(lc,call);
1042 audio_stream_set_rtcp_information(call->audiostream, cname, tool);
1043 }else ms_warning("No audio stream accepted ?");
1046 #ifdef VIDEO_ENABLED
1050 /* shutdown preview */
1051 if (lc->previewstream!=NULL) {
1052 video_preview_stop(lc->previewstream);
1053 lc->previewstream=NULL;
1055 call->current_params.has_video=FALSE;
1056 if (vstream && vstream->dir!=SalStreamInactive && vstream->port!=0) {
1057 const char *addr=vstream->addr[0]!='\0' ? vstream->addr : call->resultdesc->addr;
1058 call->video_profile=make_profile(call,call->resultdesc,vstream,&used_pt);
1060 VideoStreamDir dir=VideoStreamSendRecv;
1061 MSWebCam *cam=lc->video_conf.device;
1062 bool_t is_inactive=FALSE;
1064 call->current_params.has_video=TRUE;
1066 video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
1067 video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
1068 if (lc->video_window_id!=0)
1069 video_stream_set_native_window_id(call->videostream,lc->video_window_id);
1070 if (lc->preview_window_id!=0)
1071 video_stream_set_native_preview_window_id (call->videostream,lc->preview_window_id);
1072 video_stream_use_preview_video_window (call->videostream,lc->use_preview_window);
1074 if (vstream->dir==SalStreamSendOnly && lc->video_conf.capture ){
1075 cam=get_nowebcam_device();
1076 dir=VideoStreamSendOnly;
1077 }else if (vstream->dir==SalStreamRecvOnly && lc->video_conf.display ){
1078 dir=VideoStreamRecvOnly;
1079 }else if (vstream->dir==SalStreamSendRecv){
1080 if (lc->video_conf.display && lc->video_conf.capture)
1081 dir=VideoStreamSendRecv;
1082 else if (lc->video_conf.display)
1083 dir=VideoStreamRecvOnly;
1085 dir=VideoStreamSendOnly;
1087 ms_warning("video stream is inactive.");
1088 /*either inactive or incompatible with local capabilities*/
1091 if (call->camera_active==FALSE || all_inputs_muted){
1092 cam=get_nowebcam_device();
1095 video_stream_set_direction (call->videostream, dir);
1096 video_stream_start(call->videostream,
1097 call->video_profile, addr, vstream->port,
1099 used_pt, jitt_comp, cam);
1100 video_stream_set_rtcp_information(call->videostream, cname,tool);
1102 }else ms_warning("No video stream accepted.");
1104 ms_warning("No valid video stream defined.");
1108 call->all_muted=all_inputs_muted;
1109 call->playing_ringbacktone=send_ringbacktone;
1110 call->up_bw=linphone_core_get_upload_bandwidth(lc);
1112 if (ortp_zrtp_available()) {
1113 OrtpZrtpParams params;
1114 params.zid=get_zrtp_identifier(lc);
1115 params.zid_file=lc->zrtp_secrets_cache;
1116 audio_stream_enable_zrtp(call->audiostream,¶ms);
1122 linphone_address_destroy(me);
1125 static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
1126 audio_stream_get_local_rtp_stats (st,&log->local_stats);
1127 log->quality=audio_stream_get_average_quality_rating(st);
1130 void linphone_call_stop_media_streams(LinphoneCall *call){
1131 if (call->audiostream!=NULL) {
1132 if (call->audiostream->ec){
1133 const char *state_str=NULL;
1134 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_STATE_STRING,&state_str);
1136 ms_message("Writing echo canceller state, %i bytes",strlen(state_str));
1137 lp_config_set_string(call->core->config,"sound","ec_state",state_str);
1140 linphone_call_log_fill_stats (call->log,call->audiostream);
1141 audio_stream_stop(call->audiostream);
1142 call->audiostream=NULL;
1144 #ifdef VIDEO_ENABLED
1145 if (call->videostream!=NULL){
1146 video_stream_stop(call->videostream);
1147 call->videostream=NULL;
1149 ms_event_queue_skip(call->core->msevq);
1152 if (call->audio_profile){
1153 rtp_profile_clear_all(call->audio_profile);
1154 rtp_profile_destroy(call->audio_profile);
1155 call->audio_profile=NULL;
1157 if (call->video_profile){
1158 rtp_profile_clear_all(call->video_profile);
1159 rtp_profile_destroy(call->video_profile);
1160 call->video_profile=NULL;
1166 void linphone_call_enable_echo_cancellation(LinphoneCall *call, bool_t enable) {
1167 if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1168 bool_t bypass_mode = !enable;
1169 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_SET_BYPASS_MODE,&bypass_mode);
1172 bool_t linphone_call_echo_cancellation_enabled(LinphoneCall *call) {
1173 if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1175 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_BYPASS_MODE,&val);
1178 return linphone_core_echo_cancellation_enabled(call->core);
1182 void linphone_call_enable_echo_limiter(LinphoneCall *call, bool_t val){
1183 if (call!=NULL && call->audiostream!=NULL ) {
1185 const char *type=lp_config_get_string(call->core->config,"sound","el_type","mic");
1186 if (strcasecmp(type,"mic")==0)
1187 audio_stream_enable_echo_limiter(call->audiostream,ELControlMic);
1188 else if (strcasecmp(type,"full")==0)
1189 audio_stream_enable_echo_limiter(call->audiostream,ELControlFull);
1191 audio_stream_enable_echo_limiter(call->audiostream,ELInactive);
1196 bool_t linphone_call_echo_limiter_enabled(const LinphoneCall *call){
1197 if (call!=NULL && call->audiostream!=NULL ){
1198 return call->audiostream->el_type !=ELInactive ;
1200 return linphone_core_echo_limiter_enabled(call->core);
1205 * @addtogroup call_misc
1210 * Returns the measured sound volume played locally (received from remote)
1211 * It is expressed in dbm0.
1213 float linphone_call_get_play_volume(LinphoneCall *call){
1214 AudioStream *st=call->audiostream;
1215 if (st && st->volsend){
1217 ms_filter_call_method(st->volsend,MS_VOLUME_GET,&vol);
1221 return LINPHONE_VOLUME_DB_LOWEST;
1225 * Returns the measured sound volume recorded locally (sent to remote)
1226 * It is expressed in dbm0.
1228 float linphone_call_get_record_volume(LinphoneCall *call){
1229 AudioStream *st=call->audiostream;
1230 if (st && st->volrecv){
1232 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&vol);
1236 return LINPHONE_VOLUME_DB_LOWEST;
1240 * Obtain real-time quality rating of the call
1242 * Based on local RTP statistics and RTCP feedback, a quality rating is computed and updated
1243 * during all the duration of the call. This function returns its value at the time of the function call.
1244 * It is expected that the rating is updated at least every 5 seconds or so.
1245 * The rating is a floating point number comprised between 0 and 5.
1247 * 4-5 = good quality <br>
1248 * 3-4 = average quality <br>
1249 * 2-3 = poor quality <br>
1250 * 1-2 = very poor quality <br>
1251 * 0-1 = can't be worse, mostly unusable <br>
1253 * @returns The function returns -1 if no quality measurement is available, for example if no
1254 * active audio stream exist. Otherwise it returns the quality rating.
1256 float linphone_call_get_current_quality(LinphoneCall *call){
1257 if (call->audiostream){
1258 return audio_stream_get_quality_rating(call->audiostream);
1264 * Returns call quality averaged over all the duration of the call.
1266 * See linphone_call_get_current_quality() for more details about quality measurement.
1268 float linphone_call_get_average_quality(LinphoneCall *call){
1269 if (call->audiostream){
1270 return audio_stream_get_average_quality_rating(call->audiostream);
1279 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1280 ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1281 (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1282 (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1283 (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1284 (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1287 static void linphone_core_disconnected(LinphoneCore *lc, LinphoneCall *call){
1291 from = linphone_call_get_remote_address_as_string(call);
1294 snprintf(temp,sizeof(temp),"Remote end %s seems to have disconnected, the call is going to be closed.",from);
1299 snprintf(temp,sizeof(temp),"Remote end seems to have disconnected, the call is going to be closed.");
1301 if (lc->vtable.display_warning!=NULL)
1302 lc->vtable.display_warning(lc,temp);
1303 linphone_core_terminate_call(lc,call);
1306 void linphone_call_background_tasks(LinphoneCall *call, bool_t one_second_elapsed){
1307 int disconnect_timeout = linphone_core_get_nortp_timeout(call->core);
1308 bool_t disconnected=FALSE;
1310 if (call->state==LinphoneCallStreamsRunning && one_second_elapsed){
1311 RtpSession *as=NULL,*vs=NULL;
1312 float audio_load=0, video_load=0;
1313 if (call->audiostream!=NULL){
1314 as=call->audiostream->session;
1315 if (call->audiostream->ticker)
1316 audio_load=ms_ticker_get_average_load(call->audiostream->ticker);
1318 if (call->videostream!=NULL){
1319 if (call->videostream->ticker)
1320 video_load=ms_ticker_get_average_load(call->videostream->ticker);
1321 vs=call->videostream->session;
1323 display_bandwidth(as,vs);
1324 ms_message("Thread processing load: audio=%f\tvideo=%f",audio_load,video_load);
1326 #ifdef VIDEO_ENABLED
1327 if (call->videostream!=NULL) {
1328 if (call->videostream->evq){
1329 OrtpEvent *ev=ortp_ev_queue_get(call->videostream->evq);
1331 OrtpEventType evt=ortp_event_get_type(ev);
1332 if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
1333 OrtpEventData *evd=ortp_event_get_data(ev);
1334 linphone_call_videostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
1336 ortp_event_destroy(ev);
1339 video_stream_iterate(call->videostream);
1342 if (call->audiostream!=NULL) {
1343 if (call->audiostream->evq){
1344 OrtpEvent *ev=ortp_ev_queue_get(call->audiostream->evq);
1346 OrtpEventType evt=ortp_event_get_type(ev);
1347 if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
1348 OrtpEventData *evd=ortp_event_get_data(ev);
1349 linphone_call_audiostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
1350 } else if (evt == ORTP_EVENT_ZRTP_SAS_READY) {
1351 OrtpEventData *evd=ortp_event_get_data(ev);
1352 linphone_call_audiostream_auth_token_ready(call, evd->info.zrtp_sas.sas, evd->info.zrtp_sas.verified);
1354 ortp_event_destroy(ev);
1357 audio_stream_iterate(call->audiostream);
1359 if (one_second_elapsed && call->audiostream!=NULL && disconnect_timeout>0 )
1360 disconnected=!audio_stream_alive(call->audiostream,disconnect_timeout);
1362 linphone_core_disconnected(call->core,call);
1365 void linphone_call_log_completed(LinphoneCall *call){
1366 LinphoneCore *lc=call->core;
1368 call->log->duration=time(NULL)-call->start_time;
1370 if (call->log->status==LinphoneCallMissed){
1373 info=ortp_strdup_printf(ngettext("You have missed %i call.",
1374 "You have missed %i calls.", lc->missed_calls),
1376 if (lc->vtable.display_status!=NULL)
1377 lc->vtable.display_status(lc,info);
1380 lc->call_logs=ms_list_prepend(lc->call_logs,(void *)call->log);
1381 if (ms_list_size(lc->call_logs)>lc->max_call_logs){
1382 MSList *elem,*prevelem=NULL;
1383 /*find the last element*/
1384 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
1388 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
1389 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
1391 if (lc->vtable.call_log_updated!=NULL){
1392 lc->vtable.call_log_updated(lc,call->log);
1394 call_logs_write_to_config_file(lc);