3 Copyright (C) 2010 Simon MORLAT (simon.morlat@free.fr)
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "linphonecore.h"
25 #include "mediastreamer2/mediastream.h"
28 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details);
30 void linphone_core_update_streams(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription *new_md){
31 SalMediaDescription *oldmd=call->resultdesc;
33 if (lc->ringstream!=NULL){
34 ring_stop(lc->ringstream);
38 sal_media_description_ref(new_md);
39 call->media_pending=FALSE;
41 call->media_pending=TRUE;
43 call->resultdesc=new_md;
44 if (call->audiostream && call->audiostream->ticker){
45 /* we already started media: check if we really need to restart it*/
47 if (sal_media_description_equals(oldmd,new_md)){
48 sal_media_description_unref(oldmd);
50 ms_message("Early media finished, unmuting inputs...");
51 /*we were in early media, now we want to enable real media */
52 linphone_call_enable_camera (call,linphone_call_camera_enabled (call));
53 if (call->audiostream)
54 linphone_core_mute_mic (lc, linphone_core_is_mic_muted(lc));
56 ms_message("No need to restart streams, SDP is unchanged.");
59 ms_message("Media descriptions are different, need to restart the streams.");
62 linphone_call_stop_media_streams (call);
63 linphone_call_init_media_streams (call);
66 sal_media_description_unref(oldmd);
69 bool_t all_muted=FALSE;
70 bool_t send_ringbacktone=FALSE;
72 if (call->audiostream==NULL){
73 /*this happens after pausing the call locally. The streams is destroyed and then we wait the 200Ok to recreate it*/
74 linphone_call_init_media_streams (call);
76 if (call->state==LinphoneCallIncomingEarlyMedia ||
77 (call->state==LinphoneCallOutgoingEarlyMedia && !call->params.real_early_media)){
79 }else if (call->state==LinphoneCallIncomingEarlyMedia && linphone_core_get_remote_ringback_tone (lc)!=NULL){
80 send_ringbacktone=TRUE;
82 linphone_call_start_media_streams(call,all_muted,send_ringbacktone);
86 static bool_t is_duplicate_call(LinphoneCore *lc, const LinphoneAddress *from, const LinphoneAddress *to){
88 for(elem=lc->calls;elem!=NULL;elem=elem->next){
89 LinphoneCall *call=(LinphoneCall*)elem->data;
90 if (linphone_address_weak_equal(call->log->from,from) &&
91 linphone_address_weak_equal(call->log->to, to)){
98 static void call_received(SalOp *h){
99 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
102 const char *from,*to;
104 LinphoneAddress *from_parsed;
105 LinphoneAddress *from_addr, *to_addr;
106 SalMediaDescription *md;
107 bool_t propose_early_media=lp_config_get_int(lc->config,"sip","incoming_calls_early_media",FALSE);
108 const char *ringback_tone=linphone_core_get_remote_ringback_tone (lc);
110 /* first check if we can answer successfully to this invite */
111 if (lc->presence_mode==LinphoneStatusBusy ||
112 lc->presence_mode==LinphoneStatusOffline ||
113 lc->presence_mode==LinphoneStatusDoNotDisturb ||
114 lc->presence_mode==LinphoneStatusMoved){
115 if (lc->presence_mode==LinphoneStatusBusy )
116 sal_call_decline(h,SalReasonBusy,NULL);
117 else if (lc->presence_mode==LinphoneStatusOffline)
118 sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
119 else if (lc->presence_mode==LinphoneStatusDoNotDisturb)
120 sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
121 else if (lc->alt_contact!=NULL && lc->presence_mode==LinphoneStatusMoved)
122 sal_call_decline(h,SalReasonRedirect,lc->alt_contact);
126 if (!linphone_core_can_we_add_call(lc)){/*busy*/
127 sal_call_decline(h,SalReasonBusy,NULL);
131 from=sal_op_get_from(h);
133 from_addr=linphone_address_new(from);
134 to_addr=linphone_address_new(to);
136 if (is_duplicate_call(lc,from_addr,to_addr)){
137 ms_warning("Receiving duplicated call, refusing this one.");
138 sal_call_decline(h,SalReasonBusy,NULL);
139 linphone_address_destroy(from_addr);
140 linphone_address_destroy(to_addr);
144 call=linphone_call_new_incoming(lc,from_addr,to_addr,h);
145 sal_call_set_local_media_description(h,call->localdesc);
146 md=sal_call_get_final_media_description(h);
148 if (md && sal_media_description_empty(md)){
149 sal_call_decline(h,SalReasonMedia,NULL);
150 linphone_call_unref(call);
154 /* the call is acceptable so we can now add it to our list */
155 linphone_core_add_call(lc,call);
157 from_parsed=linphone_address_new(sal_op_get_from(h));
158 linphone_address_clean(from_parsed);
159 tmp=linphone_address_as_string(from_parsed);
160 linphone_address_destroy(from_parsed);
161 barmesg=ortp_strdup_printf("%s %s%s",tmp,_("is contacting you"),
162 (sal_call_autoanswer_asked(h)) ?_(" and asked autoanswer."):_("."));
163 if (lc->vtable.show) lc->vtable.show(lc);
164 if (lc->vtable.display_status)
165 lc->vtable.display_status(lc,barmesg);
167 /* play the ring if this is the only call*/
168 if (lc->sound_conf.ring_sndcard!=NULL && ms_list_size(lc->calls)==1){
169 lc->current_call=call;
170 if (lc->ringstream && lc->dmfs_playing_start_time!=0){
171 ring_stop(lc->ringstream);
173 lc->dmfs_playing_start_time=0;
175 if(lc->ringstream==NULL){
176 MSSndCard *ringcard=lc->sound_conf.lsd_card ?lc->sound_conf.lsd_card : lc->sound_conf.ring_sndcard;
177 ms_message("Starting local ring...");
178 lc->ringstream=ring_start(lc->sound_conf.local_ring,2000,ringcard);
182 ms_message("the local ring is already started");
185 /*TODO : play a tone within the context of the current call */
187 linphone_call_set_state(call,LinphoneCallIncomingReceived,"Incoming call");
189 sal_call_notify_ringing(h,propose_early_media || ringback_tone!=NULL);
191 if (propose_early_media || ringback_tone!=NULL){
192 linphone_call_set_state(call,LinphoneCallIncomingEarlyMedia,"Incoming call early media");
193 linphone_core_update_streams(lc,call,md);
199 if (sal_call_get_replaces(call->op)!=NULL && lp_config_get_int(lc->config,"sip","auto_answer_replacing_calls",1)){
200 linphone_core_accept_call(lc,call);
204 static void call_ringing(SalOp *h){
205 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
206 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
207 SalMediaDescription *md;
209 if (call==NULL) return;
211 if (lc->vtable.display_status)
212 lc->vtable.display_status(lc,_("Remote ringing."));
214 md=sal_call_get_final_media_description(h);
216 if (lc->ringstream && lc->dmfs_playing_start_time!=0){
217 ring_stop(lc->ringstream);
219 lc->dmfs_playing_start_time=0;
221 if (lc->ringstream!=NULL) return; /*already ringing !*/
222 if (lc->sound_conf.play_sndcard!=NULL){
223 MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
224 ms_message("Remote ringing...");
225 lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,ringcard);
226 linphone_call_set_state(call,LinphoneCallOutgoingRinging,"Remote ringing");
229 /*accept early media */
230 if (call->audiostream && call->audiostream->ticker!=NULL){
231 /*streams already started */
232 ms_message("Early media already started.");
235 if (lc->vtable.show) lc->vtable.show(lc);
236 if (lc->vtable.display_status)
237 lc->vtable.display_status(lc,_("Early media."));
238 linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
239 if (lc->ringstream!=NULL){
240 ring_stop(lc->ringstream);
243 ms_message("Doing early media...");
244 linphone_core_update_streams (lc,call,md);
250 * - when the call is accepted
251 * - when a request is accepted (pause, resume)
253 static void call_accepted(SalOp *op){
254 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
255 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
256 SalMediaDescription *md;
259 ms_warning("No call to accept.");
263 md=sal_call_get_final_media_description(op);
265 if (call->state==LinphoneCallOutgoingProgress ||
266 call->state==LinphoneCallOutgoingRinging ||
267 call->state==LinphoneCallOutgoingEarlyMedia){
268 linphone_call_set_state(call,LinphoneCallConnected,"Connected");
270 if (md && !sal_media_description_empty(md)){
271 if (sal_media_description_has_dir(md,SalStreamSendOnly) ||
272 sal_media_description_has_dir(md,SalStreamInactive)){
273 if (lc->vtable.display_status){
274 char *tmp=linphone_call_get_remote_address_as_string (call);
275 char *msg=ms_strdup_printf(_("Call with %s is paused."),tmp);
276 lc->vtable.display_status(lc,msg);
280 linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
281 }else if (sal_media_description_has_dir(md,SalStreamRecvOnly)){
282 /*we are put on hold when the call is initially accepted */
283 if (lc->vtable.display_status){
284 char *tmp=linphone_call_get_remote_address_as_string (call);
285 char *msg=ms_strdup_printf(_("Call answered by %s - on hold."),tmp);
286 lc->vtable.display_status(lc,msg);
290 linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
292 if (lc->vtable.display_status){
293 lc->vtable.display_status(lc,_("Call answered - connected."));
295 linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
297 linphone_core_update_streams (lc,call,md);
300 ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
301 linphone_core_abort_call(lc,call,"No codec intersection");
305 static void call_ack(SalOp *op){
306 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
307 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
309 ms_warning("No call to be ACK'd");
312 if (call->media_pending){
313 SalMediaDescription *md=sal_call_get_final_media_description(op);
314 if (md && !sal_media_description_empty(md)){
315 linphone_core_update_streams (lc,call,md);
316 linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
319 ms_error("Incompatible SDP response received in ACK, need to abort the call");
320 linphone_core_abort_call(lc,call,"No codec intersection");
326 /* this callback is called when an incoming re-INVITE modifies the session*/
327 static void call_updating(SalOp *op){
328 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
329 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
330 LinphoneCallState prevstate=LinphoneCallIdle;
331 SalMediaDescription *md;
333 md=sal_call_get_final_media_description(op);
335 if (md && !sal_media_description_empty(md))
337 if ((call->state==LinphoneCallPausedByRemote || call->state==LinphoneCallPaused) &&
338 sal_media_description_has_dir(md,SalStreamSendRecv) && strcmp(md->addr,"0.0.0.0")!=0){
339 /*make sure we can be resumed */
340 if (lc->current_call!=NULL && lc->current_call!=call){
341 ms_warning("Attempt to be resumed but already in call with somebody else!");
342 /*we are actively running another call, reject with a busy*/
343 sal_call_decline (op,SalReasonBusy,NULL);
346 if(lc->vtable.display_status)
347 lc->vtable.display_status(lc,_("We have been resumed..."));
348 linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
350 else if(call->state==LinphoneCallStreamsRunning &&
351 ( sal_media_description_has_dir(md,SalStreamRecvOnly)
352 || sal_media_description_has_dir(md,SalStreamInactive)
353 || strcmp(md->addr,"0.0.0.0")==0)){
354 if(lc->vtable.display_status)
355 lc->vtable.display_status(lc,_("We are being paused..."));
356 linphone_call_set_state (call,LinphoneCallPausedByRemote,"Call paused by remote");
357 if (lc->current_call!=call){
358 ms_error("Inconsitency detected: current call is %p but call %p is being paused !",lc->current_call,call);
361 prevstate=call->state;
362 linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
364 /*accept the modification (sends a 200Ok)*/
366 linphone_core_update_streams (lc,call,md);
367 if (prevstate!=LinphoneCallIdle){
368 linphone_call_set_state (call,prevstate,"Connected (streams running)");
373 static void call_terminated(SalOp *op, const char *from){
374 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
375 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
377 if (call==NULL) return;
379 if (linphone_call_get_state(call)==LinphoneCallEnd || linphone_call_get_state(call)==LinphoneCallError){
380 ms_warning("call_terminated: ignoring.");
383 ms_message("Current call terminated...");
384 //we stop the call only if we have this current call or if we are in call
385 if (lc->ringstream!=NULL && ( (ms_list_size(lc->calls) == 1) || linphone_core_in_call(lc) )) {
386 ring_stop(lc->ringstream);
389 linphone_call_stop_media_streams(call);
390 if (lc->vtable.show!=NULL)
392 if (lc->vtable.display_status!=NULL)
393 lc->vtable.display_status(lc,_("Call terminated."));
395 linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
398 static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
399 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
400 char *msg486=_("User is busy.");
401 char *msg480=_("User is temporarily unavailable.");
402 /*char *retrymsg=_("%s. Retry after %i minute(s).");*/
403 char *msg600=_("User does not want to be disturbed.");
404 char *msg603=_("Call declined.");
405 const char *msg=details;
406 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
409 ms_warning("Call faillure reported on already cleaned call ?");
413 if (lc->vtable.show) lc->vtable.show(lc);
415 if (error==SalErrorNoResponse){
416 msg=_("No response.");
417 if (lc->vtable.display_status)
418 lc->vtable.display_status(lc,msg);
419 }else if (error==SalErrorProtocol){
420 msg=details ? details : _("Protocol error.");
421 if (lc->vtable.display_status)
422 lc->vtable.display_status(lc, msg);
423 }else if (error==SalErrorFailure){
425 case SalReasonDeclined:
427 if (lc->vtable.display_status)
428 lc->vtable.display_status(lc,msg603);
432 if (lc->vtable.display_status)
433 lc->vtable.display_status(lc,msg486);
435 case SalReasonRedirect:
437 if (lc->vtable.display_status)
438 lc->vtable.display_status(lc,msg);
440 case SalReasonTemporarilyUnavailable:
442 if (lc->vtable.display_status)
443 lc->vtable.display_status(lc,msg480);
445 case SalReasonNotFound:
447 if (lc->vtable.display_status)
448 lc->vtable.display_status(lc,msg);
450 case SalReasonDoNotDisturb:
452 if (lc->vtable.display_status)
453 lc->vtable.display_status(lc,msg600);
456 msg=_("No common codecs");
457 if (lc->vtable.display_status)
458 lc->vtable.display_status(lc,msg);
461 if (lc->vtable.display_status)
462 lc->vtable.display_status(lc,_("Call failed."));
466 if (lc->ringstream!=NULL) {
467 ring_stop(lc->ringstream);
470 linphone_call_stop_media_streams (call);
471 if (sr!=SalReasonDeclined) linphone_call_set_state(call,LinphoneCallError,msg);
473 call->reason=LinphoneReasonDeclined;
474 linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
478 static void auth_requested(SalOp *h, const char *realm, const char *username){
479 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
480 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
481 ms_message("auth_requested() for realm=%s, username=%s",realm,username);
482 if (ai && (ai->works || ai->usecount<3)){
484 sai.username=ai->username;
485 sai.userid=ai->userid;
487 sai.password=ai->passwd;
488 ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
489 sal_op_authenticate(h,&sai);
492 if (ai && ai->works==FALSE) {
493 register_failure(h, SalErrorFailure, SalReasonForbidden, _("Authentication failure"));
495 if (lc->vtable.auth_info_requested)
496 lc->vtable.auth_info_requested(lc,realm,username);
500 static void auth_success(SalOp *h, const char *realm, const char *username){
501 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
502 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
504 ms_message("%s/%s authentication works.",realm,username);
509 static void register_success(SalOp *op, bool_t registered){
510 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
511 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
514 cfg->registered=registered;
515 linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
516 linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
517 registered ? "Registration sucessful" : "Unregistration done");
518 if (lc->vtable.display_status){
519 if (cfg->registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
520 else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
521 lc->vtable.display_status(lc,msg);
527 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
528 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
529 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
532 ms_warning("Registration failed for unknown proxy config.");
536 details=_("no response timeout");
538 if (lc->vtable.display_status) {
539 char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),details );
540 lc->vtable.display_status(lc,msg);
543 if (error== SalErrorFailure && reason == SalReasonForbidden) {
544 linphone_proxy_config_set_error(cfg, LinphoneReasonBadCredentials);
545 } else if (error == SalErrorNoResponse) {
546 linphone_proxy_config_set_error(cfg, LinphoneReasonNoResponse);
548 linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
551 static void vfu_request(SalOp *op){
553 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
555 ms_warning("VFU request but no call !");
558 if (call->videostream)
559 video_stream_send_vfu(call->videostream);
563 static void dtmf_received(SalOp *op, char dtmf){
564 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
565 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
566 if (lc->vtable.dtmf_received != NULL)
567 lc->vtable.dtmf_received(lc, call, dtmf);
570 static void refer_received(Sal *sal, SalOp *op, const char *referto){
571 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
572 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
574 if (call->refer_to!=NULL){
575 ms_free(call->refer_to);
577 call->refer_to=ms_strdup(referto);
578 call->refer_pending=TRUE;
579 linphone_call_set_state(call,LinphoneCallRefered,"Refered");
580 if (lc->vtable.display_status){
581 char *msg=ms_strdup_printf(_("We are transferred to %s"),referto);
582 lc->vtable.display_status(lc,msg);
585 if (call->state!=LinphoneCallPaused){
586 ms_message("Automatically pausing current call to accept transfer.");
587 linphone_core_pause_call(lc,call);
589 linphone_core_start_refered_call(lc,call);
590 sal_call_accept_refer(op);
591 }else if (lc->vtable.refer_received){
592 lc->vtable.refer_received(lc,referto);
593 sal_call_accept_refer(op);
597 static void text_received(Sal *sal, const char *from, const char *msg){
598 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
599 linphone_core_text_received(lc,from,msg);
602 static void notify(SalOp *op, const char *from, const char *msg){
603 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
604 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
605 ms_message("get a %s notify from %s",msg,from);
606 if(lc->vtable.notify_recv)
607 lc->vtable.notify_recv(lc,call,from,msg);
610 static void notify_presence(SalOp *op, SalSubscribeState ss, SalPresenceStatus status, const char *msg){
611 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
612 linphone_notify_recv(lc,op,ss,status);
615 static void subscribe_received(SalOp *op, const char *from){
616 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
617 linphone_subscription_new(lc,op,from);
620 static void subscribe_closed(SalOp *op, const char *from){
621 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
622 linphone_subscription_closed(lc,op);
625 static void internal_message(Sal *sal, const char *msg){
626 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
631 static void ping_reply(SalOp *op){
632 LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
633 ms_message("ping reply !");
635 if (call->state==LinphoneCallOutgoingInit){
636 linphone_core_start_invite(call->core,call,NULL);
641 ms_warning("ping reply without call attached...");
645 SalCallbacks linphone_sal_callbacks={