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"
36 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, bool_t only_one_codec){
39 for(it=codecs;it!=NULL;it=it->next){
40 PayloadType *pt=(PayloadType*)it->data;
41 if ((pt->flags & PAYLOAD_TYPE_ENABLED) && linphone_core_check_payload_type_usability(lc,pt)){
42 l=ms_list_append(l,payload_type_clone(pt));
43 if (only_one_codec) break;
49 static SalMediaDescription *create_local_media_description(LinphoneCore *lc,
50 LinphoneCall *call, const char *username, bool_t only_one_codec){
53 SalMediaDescription *md=sal_media_description_new();
55 strncpy(md->addr,call->localip,sizeof(md->addr));
56 strncpy(md->username,username,sizeof(md->username));
57 md->bandwidth=linphone_core_get_download_bandwidth(lc);
58 /*set audio capabilities */
59 strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
60 md->streams[0].port=call->audio_port;
61 md->streams[0].proto=SalProtoRtpAvp;
62 md->streams[0].type=SalAudio;
63 md->streams[0].ptime=lc->net_conf.down_ptime;
64 l=make_codec_list(lc,lc->codecs_conf.audio_codecs,only_one_codec);
65 pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
66 l=ms_list_append(l,pt);
67 md->streams[0].payloads=l;
69 if (lc->dw_audio_bw>0)
70 md->streams[0].bandwidth=lc->dw_audio_bw;
72 if (linphone_core_video_enabled (lc)){
74 md->streams[1].port=call->video_port;
75 md->streams[1].proto=SalProtoRtpAvp;
76 md->streams[1].type=SalVideo;
77 l=make_codec_list(lc,lc->codecs_conf.video_codecs,only_one_codec);
78 md->streams[1].payloads=l;
80 md->streams[1].bandwidth=lc->dw_video_bw;
85 static int find_port_offset(LinphoneCore *lc){
89 bool_t already_used=FALSE;
90 for(offset=0;offset<100;offset+=2){
91 audio_port=linphone_core_get_audio_port (lc)+offset;
93 for(elem=lc->calls;elem!=NULL;elem=elem->next){
94 LinphoneCall *call=(LinphoneCall*)elem->data;
95 if (call->audio_port==audio_port) {
100 if (!already_used) break;
103 ms_error("Could not find any free port !");
109 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
112 call->state=LinphoneCallIdle;
113 call->start_time=time(NULL);
114 call->media_start_time=0;
115 call->log=linphone_call_log_new(call, from, to);
116 linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
117 port_offset=find_port_offset (call->core);
118 if (port_offset==-1) return;
119 call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
120 call->video_port=linphone_core_get_video_port(call->core)+port_offset;
124 static void discover_mtu(LinphoneCore *lc, const char *remote){
126 if (lc->net_conf.mtu==0 ){
127 /*attempt to discover mtu*/
128 mtu=ms_discover_mtu(remote);
131 ms_message("Discovered mtu is %i, RTP payload max size is %i",
132 mtu, ms_get_payload_max_size());
137 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to)
139 LinphoneCall *call=ms_new0(LinphoneCall,1);
140 call->dir=LinphoneCallOutgoing;
141 call->op=sal_op_new(lc->sal);
142 sal_op_set_user_pointer(call->op,call);
144 linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
145 linphone_call_init_common(call,from,to);
146 call->localdesc=create_local_media_description (lc,call,
147 linphone_address_get_username(from),FALSE);
148 if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
149 linphone_core_run_stun_tests(call->core,call);
150 discover_mtu(lc,linphone_address_get_domain (to));
154 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
155 LinphoneCall *call=ms_new0(LinphoneCall,1);
156 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
160 call->dir=LinphoneCallIncoming;
161 sal_op_set_user_pointer(op,call);
165 if (lc->sip_conf.ping_with_options){
166 /*the following sends an option request back to the caller so that
167 we get a chance to discover our nat'd address before answering.*/
168 call->ping_op=sal_op_new(lc->sal);
169 to_str=linphone_address_as_string(to);
170 from_str=linphone_address_as_string(from);
171 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
172 sal_op_set_user_pointer(call->ping_op,call);
173 sal_ping(call->ping_op,to_str,from_str);
178 linphone_address_clean(from);
179 linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
180 linphone_call_init_common(call, from, to);
181 call->localdesc=create_local_media_description (lc,call,
182 linphone_address_get_username(me),lc->sip_conf.only_one_codec);
183 if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
184 linphone_core_run_stun_tests(call->core,call);
185 discover_mtu(lc,linphone_address_get_domain(from));
186 linphone_address_destroy(me);
190 /* this function is called internally to get rid of a call.
191 It performs the following tasks:
192 - remove the call from the internal list of calls
193 - unref the LinphoneCall object
194 - update the call logs accordingly
197 static void linphone_call_set_terminated(LinphoneCall *call){
198 LinphoneCallStatus status=LinphoneCallAborted;
199 LinphoneCore *lc=call->core;
201 linphone_core_update_allocated_audio_bandwidth(lc);
202 if (call->state==LinphoneCallEnd){
203 status=LinphoneCallSuccess;
206 linphone_call_log_completed(call->log,call, status);
208 if (call == lc->current_call){
209 ms_message("Resetting the current call");
210 lc->current_call=NULL;
211 linphone_core_start_pending_refered_calls(lc);
214 if (linphone_core_del_call(lc,call) != 0){
215 ms_error("Could not remove the call from the list !!!");
218 if (ms_list_size(lc->calls)==0)
219 linphone_core_notify_all_friends(lc,lc->presence_mode);
221 if (call->op!=NULL) {
222 /* so that we cannot have anymore upcalls for SAL
223 concerning this call*/
224 sal_op_release(call->op);
227 linphone_call_unref(call);
230 void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const char *message){
231 LinphoneCore *lc=call->core;
232 bool_t finalize_call=FALSE;
233 if (call->state!=cstate){
234 if (cstate!=LinphoneCallRefered){
235 /*LinphoneCallRefered is rather an event, not a state.
236 Indeed it does not change the state of the call (still paused or running)*/
239 if (cstate==LinphoneCallEnd || cstate==LinphoneCallError){
241 linphone_call_ref(call);
242 linphone_call_set_terminated (call);
244 if (lc->vtable.call_state_changed)
245 lc->vtable.call_state_changed(lc,call,cstate,message);
247 linphone_call_unref(call);
251 static void linphone_call_destroy(LinphoneCall *obj)
254 sal_op_release(obj->op);
257 if (obj->resultdesc!=NULL) {
258 sal_media_description_unref(obj->resultdesc);
259 obj->resultdesc=NULL;
261 if (obj->localdesc!=NULL) {
262 sal_media_description_unref(obj->localdesc);
266 sal_op_release(obj->ping_op);
269 ms_free(obj->refer_to);
275 * @addtogroup call_control
280 * Increments the call 's reference count.
281 * An application that wishes to retain a pointer to call object
282 * must use this function to unsure the pointer remains
283 * valid. Once the application no more needs this pointer,
284 * it must call linphone_call_unref().
286 void linphone_call_ref(LinphoneCall *obj){
291 * Decrements the call object reference count.
292 * See linphone_call_ref().
294 void linphone_call_unref(LinphoneCall *obj){
297 linphone_call_destroy(obj);
301 * Returns the remote address associated to this call
304 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
305 return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
309 * Returns the remote address associated to this call as a string.
311 * The result string must be freed by user using ms_free().
313 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
314 return linphone_address_as_string(linphone_call_get_remote_address(call));
318 * Retrieves the call's current state.
320 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
325 * Get the user_pointer in the LinphoneCall
327 * @ingroup call_control
329 * return user_pointer an opaque user pointer that can be retrieved at any time
331 void *linphone_call_get_user_pointer(LinphoneCall *call)
333 return call->user_pointer;
337 * Set the user_pointer in the LinphoneCall
339 * @ingroup call_control
341 * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
343 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
345 call->user_pointer = user_pointer;
349 * Returns the call log associated to this call.
351 LinphoneCallLog *linphone_call_get_call_log(const LinphoneCall *call){
356 * Returns the refer-to uri (if the call was transfered).
358 const char *linphone_call_get_refer_to(const LinphoneCall *call){
359 return call->refer_to;
362 LinphoneCallDir linphone_call_get_dir(const LinphoneCall *call){
363 return call->log->dir;
367 * Returns true if this calls has received a transfer that has not been
369 * Pending transfers are executed when this call is being paused or closed,
370 * locally or by remote endpoint.
371 * If the call is already paused while receiving the transfer request, the
372 * transfer immediately occurs.
374 bool_t linphone_call_has_transfer_pending(const LinphoneCall *call){
375 return call->refer_pending;
379 * Returns call's duration in seconds.
381 int linphone_call_get_duration(const LinphoneCall *call){
382 if (call->media_start_time==0) return 0;
383 return time(NULL)-call->media_start_time;
391 #ifdef TEST_EXT_RENDERER
392 static void rendercb(void *data, const MSPicture *local, const MSPicture *remote){
393 ms_message("rendercb, local buffer=%p, remote buffer=%p",
394 local ? local->planes[0] : NULL, remote? remote->planes[0] : NULL);
398 void linphone_call_init_media_streams(LinphoneCall *call){
399 LinphoneCore *lc=call->core;
400 SalMediaDescription *md=call->localdesc;
401 AudioStream *audiostream;
403 call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
404 if (linphone_core_echo_limiter_enabled(lc)){
405 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
406 if (strcasecmp(type,"mic")==0)
407 audio_stream_enable_echo_limiter(audiostream,ELControlMic);
408 else if (strcasecmp(type,"full")==0)
409 audio_stream_enable_echo_limiter(audiostream,ELControlFull);
411 audio_stream_enable_gain_control(audiostream,TRUE);
412 if (linphone_core_echo_cancellation_enabled(lc)){
413 int len,delay,framesize;
414 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
415 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
416 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
417 audio_stream_set_echo_canceller_params(audiostream,len,delay,framesize);
419 audio_stream_enable_automatic_gain_control(audiostream,linphone_core_agc_enabled(lc));
421 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
422 audio_stream_enable_noise_gate(audiostream,enabled);
425 rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
428 if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
429 call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
430 #ifdef TEST_EXT_RENDERER
431 video_stream_set_render_callback(call->videostream,rendercb,NULL);
435 call->videostream=NULL;
440 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
442 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
443 LinphoneCore* lc = (LinphoneCore*)user_data;
444 if (dtmf<0 || dtmf>15){
445 ms_warning("Bad dtmf value %i",dtmf);
448 if (lc->vtable.dtmf_received != NULL)
449 lc->vtable.dtmf_received(lc, linphone_core_get_current_call(lc), dtmf_tab[dtmf]);
452 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
454 MSFilter *f=st->equalizer;
455 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
456 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
457 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
463 if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
464 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
465 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
475 static void post_configure_audio_streams(LinphoneCall*call){
476 AudioStream *st=call->audiostream;
477 LinphoneCore *lc=call->core;
478 float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
481 float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
482 float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
483 int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
486 audio_stream_set_mic_gain(st,mic_gain);
487 call->audio_muted=FALSE;
489 recv_gain = lc->sound_conf.soft_play_lev;
490 if (recv_gain != 0) {
491 linphone_core_set_playback_gain_db (lc,recv_gain);
494 ms_filter_call_method(st->volsend,MS_VOLUME_REMOVE_DC,&dc_removal);
496 if (linphone_core_echo_limiter_enabled(lc)){
497 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
498 thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
499 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
500 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
502 if (st->el_type!=ELInactive){
504 if (speed==-1) speed=0.03;
505 if (force==-1) force=25;
506 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
507 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
509 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
511 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
516 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
517 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
520 /* parameters for a limited noise-gate effect, using echo limiter threshold */
521 float floorgain = 1/mic_gain;
522 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&thres);
523 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
525 parametrize_equalizer(lc,st);
526 if (lc->vtable.dtmf_received!=NULL){
527 /* replace by our default action*/
528 audio_stream_play_received_dtmfs(call->audiostream,FALSE);
529 rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
536 static RtpProfile *make_profile(LinphoneCore *lc, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
539 RtpProfile *prof=rtp_profile_new("Call profile");
544 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
545 PayloadType *pt=(PayloadType*)elem->data;
549 if (desc->type==SalAudio){
550 linphone_core_update_allocated_audio_bandwidth_in_call(lc,pt);
552 *used_pt=payload_type_get_number(pt);
555 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
556 else if (md->bandwidth>0) {
557 /*case where b=AS is given globally, not per stream*/
558 remote_bw=md->bandwidth;
559 if (desc->type==SalVideo){
560 remote_bw-=lc->audio_bw;
564 if (desc->type==SalAudio){
565 bw=get_min_bandwidth(lc->up_audio_bw,remote_bw);
566 }else bw=get_min_bandwidth(lc->up_video_bw,remote_bw);
567 if (bw>0) pt->normal_bitrate=bw*1000;
568 else if (desc->type==SalAudio){
569 pt->normal_bitrate=-1;
573 snprintf(tmp,sizeof(tmp),"ptime=%i",desc->ptime);
574 payload_type_append_send_fmtp(pt,tmp);
576 number=payload_type_get_number(pt);
577 if (rtp_profile_get_payload(prof,number)!=NULL){
578 ms_warning("A payload type with number %i already exists in profile !",number);
579 payload_type_destroy(pt);
581 rtp_profile_set_payload(prof,number,pt);
586 void linphone_call_start_media_streams(LinphoneCall *call){
587 LinphoneCore *lc=call->core;
588 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
589 const char *tool="linphone-" LINPHONE_VERSION;
592 if(call->audiostream == NULL)
594 ms_fatal("start_media_stream() called without prior init !");
597 /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
598 int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
600 if (call->media_start_time==0) call->media_start_time=time(NULL);
602 cname=linphone_address_as_string_uri_only(me);
604 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
605 SalProtoRtpAvp,SalAudio);
607 MSSndCard *playcard=lc->sound_conf.lsd_card ?
608 lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
609 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
610 const char *playfile=lc->play_file;
611 const char *recfile=lc->rec_file;
612 call->audio_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
614 if (playcard==NULL) {
615 ms_warning("No card defined for playback !");
617 if (captcard==NULL) {
618 ms_warning("No card defined for capture !");
620 ms_message("streamdir is %i",stream->dir);
621 /*Replace soundcard filters by inactive file players or recorders
622 when placed in recvonly or sendonly mode*/
623 if (stream->port==0 || stream->dir==SalStreamRecvOnly){
626 }else if (stream->dir==SalStreamSendOnly){
631 /*if playfile are supplied don't use soundcards*/
636 audio_stream_start_full(
639 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
648 linphone_core_echo_cancellation_enabled(lc));
649 post_configure_audio_streams(call);
650 audio_stream_set_rtcp_information(call->audiostream, cname, tool);
651 }else ms_warning("No audio stream accepted ?");
656 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
657 SalProtoRtpAvp,SalVideo);
659 /* shutdown preview */
660 if (lc->previewstream!=NULL) {
661 video_preview_stop(lc->previewstream);
662 lc->previewstream=NULL;
665 const char *addr=stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr;
666 call->video_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
668 VideoStreamDir dir=VideoStreamSendRecv;
669 MSWebCam *cam=lc->video_conf.device;
670 bool_t is_inactive=FALSE;
672 video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
673 video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
675 if (stream->dir==SalStreamSendOnly && lc->video_conf.capture ){
676 cam=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),"Static picture");
677 dir=VideoStreamSendOnly;
678 }else if (stream->dir==SalStreamRecvOnly && lc->video_conf.display ){
679 dir=VideoStreamRecvOnly;
680 }else if (stream->dir==SalStreamSendRecv){
681 if (lc->video_conf.display && lc->video_conf.capture)
682 dir=VideoStreamSendRecv;
683 else if (lc->video_conf.display)
684 dir=VideoStreamRecvOnly;
686 dir=VideoStreamSendOnly;
688 ms_warning("video stream is inactive.");
689 /*either inactive or incompatible with local capabilities*/
693 video_stream_set_direction (call->videostream, dir);
694 video_stream_start(call->videostream,
695 call->video_profile, addr, stream->port,
697 used_pt, jitt_comp, cam);
698 video_stream_set_rtcp_information(call->videostream, cname,tool);
700 }else ms_warning("No video stream accepted.");
702 ms_warning("No valid video stream defined.");
709 linphone_address_destroy(me);
714 static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
715 audio_stream_get_local_rtp_stats (st,&log->local_stats);
718 void linphone_call_stop_media_streams(LinphoneCall *call){
719 if (call->audiostream!=NULL) {
720 linphone_call_log_fill_stats (call->log,call->audiostream);
721 audio_stream_stop(call->audiostream);
722 call->audiostream=NULL;
725 if (call->videostream!=NULL){
726 video_stream_stop(call->videostream);
727 call->videostream=NULL;
731 if (call->audio_profile){
732 rtp_profile_clear_all(call->audio_profile);
733 rtp_profile_destroy(call->audio_profile);
734 call->audio_profile=NULL;
736 if (call->video_profile){
737 rtp_profile_clear_all(call->video_profile);
738 rtp_profile_destroy(call->video_profile);
739 call->video_profile=NULL;