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 static int media_parameters_changed(LinphoneCall *call, SalMediaDescription *oldmd, SalMediaDescription *newmd) {
31 if (call->params.in_conference != call->current_params.in_conference) return SAL_MEDIA_DESCRIPTION_CHANGED;
32 if (call->up_bw != linphone_core_get_upload_bandwidth(call->core)) return SAL_MEDIA_DESCRIPTION_CHANGED;
33 return sal_media_description_equals(oldmd, newmd);
36 void linphone_core_update_streams_destinations(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription *old_md, SalMediaDescription *new_md) {
37 SalStreamDescription *old_audiodesc = NULL;
38 SalStreamDescription *old_videodesc = NULL;
39 SalStreamDescription *new_audiodesc = NULL;
40 SalStreamDescription *new_videodesc = NULL;
41 char *rtp_addr, *rtcp_addr;
44 for (i = 0; i < old_md->nstreams; i++) {
45 if (old_md->streams[i].type == SalAudio) {
46 old_audiodesc = &old_md->streams[i];
47 } else if (old_md->streams[i].type == SalVideo) {
48 old_videodesc = &old_md->streams[i];
51 for (i = 0; i < new_md->nstreams; i++) {
52 if (new_md->streams[i].type == SalAudio) {
53 new_audiodesc = &new_md->streams[i];
54 } else if (new_md->streams[i].type == SalVideo) {
55 new_videodesc = &new_md->streams[i];
58 if (call->audiostream && new_audiodesc) {
59 rtp_addr = (new_audiodesc->rtp_addr[0] != '\0') ? new_audiodesc->rtp_addr : new_md->addr;
60 rtcp_addr = (new_audiodesc->rtcp_addr[0] != '\0') ? new_audiodesc->rtcp_addr : new_md->addr;
61 ms_message("Change audio stream destination: RTP=%s:%d RTCP=%s:%d", rtp_addr, new_audiodesc->rtp_port, rtcp_addr, new_audiodesc->rtcp_port);
62 rtp_session_set_remote_addr_full(call->audiostream->session, rtp_addr, new_audiodesc->rtp_port, rtcp_addr, new_audiodesc->rtcp_port);
65 if (call->videostream && new_videodesc) {
66 rtp_addr = (new_videodesc->rtp_addr[0] != '\0') ? new_videodesc->rtp_addr : new_md->addr;
67 rtcp_addr = (new_videodesc->rtcp_addr[0] != '\0') ? new_videodesc->rtcp_addr : new_md->addr;
68 ms_message("Change video stream destination: RTP=%s:%d RTCP=%s:%d", rtp_addr, new_videodesc->rtp_port, rtcp_addr, new_videodesc->rtcp_port);
69 rtp_session_set_remote_addr_full(call->videostream->session, rtp_addr, new_videodesc->rtp_port, rtcp_addr, new_videodesc->rtcp_port);
73 /* Copy address and port values from new_md to old_md since we will keep old_md as resultdesc */
74 strcpy(old_md->addr, new_md->addr);
75 if (old_audiodesc && new_audiodesc) {
76 strcpy(old_audiodesc->rtp_addr, new_audiodesc->rtp_addr);
77 strcpy(old_audiodesc->rtcp_addr, new_audiodesc->rtcp_addr);
78 old_audiodesc->rtp_port = new_audiodesc->rtp_port;
79 old_audiodesc->rtcp_port = new_audiodesc->rtcp_port;
81 if (old_videodesc && new_videodesc) {
82 strcpy(old_videodesc->rtp_addr, new_videodesc->rtp_addr);
83 strcpy(old_videodesc->rtcp_addr, new_videodesc->rtcp_addr);
84 old_videodesc->rtp_port = new_videodesc->rtp_port;
85 old_videodesc->rtcp_port = new_videodesc->rtcp_port;
89 void linphone_core_update_streams(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription *new_md){
90 SalMediaDescription *oldmd=call->resultdesc;
92 if (lc->ringstream!=NULL){
93 ring_stop(lc->ringstream);
97 sal_media_description_ref(new_md);
98 call->media_pending=FALSE;
100 call->media_pending=TRUE;
102 call->resultdesc=new_md;
103 if ((call->audiostream && call->audiostream->ticker) || (call->videostream && call->videostream->ticker)){
104 /* we already started media: check if we really need to restart it*/
106 int md_changed = media_parameters_changed(call, oldmd, new_md);
107 if ((md_changed & SAL_MEDIA_DESCRIPTION_CODEC_CHANGED) || call->playing_ringbacktone) {
108 ms_message("Media descriptions are different, need to restart the streams.");
110 if (md_changed == SAL_MEDIA_DESCRIPTION_UNCHANGED) {
111 /*as nothing has changed, keep the oldmd */
112 call->resultdesc=oldmd;
113 sal_media_description_unref(new_md);
114 if (call->all_muted){
115 ms_message("Early media finished, unmuting inputs...");
116 /*we were in early media, now we want to enable real media */
117 linphone_call_enable_camera (call,linphone_call_camera_enabled (call));
118 if (call->audiostream)
119 linphone_core_mute_mic (lc, linphone_core_is_mic_muted(lc));
121 if (call->videostream && call->camera_active)
122 video_stream_change_camera(call->videostream,lc->video_conf.device );
125 ms_message("No need to restart streams, SDP is unchanged.");
129 if (md_changed & SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED) {
130 ms_message("Network parameters have changed, update them.");
131 linphone_core_update_streams_destinations(lc, call, oldmd, new_md);
133 if (md_changed & SAL_MEDIA_DESCRIPTION_CRYPTO_CHANGED) {
134 ms_message("Crypto parameters have changed, update them.");
135 linphone_call_update_crypto_parameters(call, oldmd, new_md);
137 call->resultdesc = oldmd;
138 sal_media_description_unref(new_md);
143 linphone_call_stop_media_streams (call);
144 linphone_call_init_media_streams (call);
147 sal_media_description_unref(oldmd);
150 bool_t all_muted=FALSE;
151 bool_t send_ringbacktone=FALSE;
153 if (call->audiostream==NULL){
154 /*this happens after pausing the call locally. The streams is destroyed and then we wait the 200Ok to recreate it*/
155 linphone_call_init_media_streams (call);
157 if (call->state==LinphoneCallIncomingEarlyMedia && linphone_core_get_remote_ringback_tone (lc)!=NULL){
158 send_ringbacktone=TRUE;
160 if (call->state==LinphoneCallIncomingEarlyMedia ||
161 (call->state==LinphoneCallOutgoingEarlyMedia && !call->params.real_early_media)){
164 linphone_call_start_media_streams(call,all_muted,send_ringbacktone);
168 static bool_t is_duplicate_call(LinphoneCore *lc, const LinphoneAddress *from, const LinphoneAddress *to){
170 for(elem=lc->calls;elem!=NULL;elem=elem->next){
171 LinphoneCall *call=(LinphoneCall*)elem->data;
172 if (linphone_address_weak_equal(call->log->from,from) &&
173 linphone_address_weak_equal(call->log->to, to)){
181 static bool_t already_a_call_with_remote_address(const LinphoneCore *lc, const LinphoneAddress *remote) {
182 ms_warning(" searching for already_a_call_with_remote_address.");
185 for(elem=lc->calls;elem!=NULL;elem=elem->next){
186 const LinphoneCall *call=(LinphoneCall*)elem->data;
187 const LinphoneAddress *cRemote=linphone_call_get_remote_address(call);
188 if (linphone_address_weak_equal(cRemote,remote)) {
189 ms_warning("already_a_call_with_remote_address found.");
196 static bool_t already_a_call_pending(LinphoneCore *lc){
198 for(elem=lc->calls;elem!=NULL;elem=elem->next){
199 LinphoneCall *call=(LinphoneCall*)elem->data;
200 if (call->state==LinphoneCallIncomingReceived
201 || call->state==LinphoneCallOutgoingInit
202 || call->state==LinphoneCallOutgoingProgress
203 || call->state==LinphoneCallOutgoingEarlyMedia
204 || call->state==LinphoneCallOutgoingRinging){
211 static void call_received(SalOp *h){
212 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
214 const char *from,*to;
215 LinphoneAddress *from_addr, *to_addr;
216 bool_t prevent_colliding_calls=lp_config_get_int(lc->config,"sip","prevent_colliding_calls",TRUE);
218 /* first check if we can answer successfully to this invite */
219 if (lc->presence_mode==LinphoneStatusBusy ||
220 lc->presence_mode==LinphoneStatusOffline ||
221 lc->presence_mode==LinphoneStatusDoNotDisturb ||
222 lc->presence_mode==LinphoneStatusMoved){
223 if (lc->presence_mode==LinphoneStatusBusy )
224 sal_call_decline(h,SalReasonBusy,NULL);
225 else if (lc->presence_mode==LinphoneStatusOffline)
226 sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
227 else if (lc->presence_mode==LinphoneStatusDoNotDisturb)
228 sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
229 else if (lc->alt_contact!=NULL && lc->presence_mode==LinphoneStatusMoved)
230 sal_call_decline(h,SalReasonRedirect,lc->alt_contact);
234 if (!linphone_core_can_we_add_call(lc)){/*busy*/
235 sal_call_decline(h,SalReasonBusy,NULL);
239 from=sal_op_get_from(h);
241 from_addr=linphone_address_new(from);
242 to_addr=linphone_address_new(to);
244 if ((already_a_call_with_remote_address(lc,from_addr) && prevent_colliding_calls) || already_a_call_pending(lc)){
245 ms_warning("Receiving another call while one is ringing or initiated, refusing this one with busy message.");
246 sal_call_decline(h,SalReasonBusy,NULL);
248 linphone_address_destroy(from_addr);
249 linphone_address_destroy(to_addr);
253 call=linphone_call_new_incoming(lc,from_addr,to_addr,h);
255 /* the call is acceptable so we can now add it to our list */
256 linphone_core_add_call(lc,call);
257 linphone_call_ref(call); /*prevent the call from being destroyed while we are notifying, if the user declines within the state callback */
259 if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && (call->ice_session != NULL)) {
260 /* Defer ringing until the end of the ICE candidates gathering process. */
261 ms_message("Defer ringing to gather ICE candidates");
265 linphone_core_notify_incoming_call(lc,call);
268 static void call_ringing(SalOp *h){
269 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
270 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
271 SalMediaDescription *md;
273 if (call==NULL) return;
275 if (lc->vtable.display_status)
276 lc->vtable.display_status(lc,_("Remote ringing."));
278 md=sal_call_get_final_media_description(h);
280 if (lc->ringstream && lc->dmfs_playing_start_time!=0){
281 ring_stop(lc->ringstream);
283 lc->dmfs_playing_start_time=0;
285 if (lc->ringstream!=NULL) return; /*already ringing !*/
286 if (lc->sound_conf.play_sndcard!=NULL){
287 MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
288 if (call->localdesc->streams[0].max_rate>0) ms_snd_card_set_preferred_sample_rate(ringcard, call->localdesc->streams[0].max_rate);
289 /*we release sound before playing ringback tone*/
290 if (call->audiostream)
291 audio_stream_unprepare_sound(call->audiostream);
292 lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,ringcard);
294 ms_message("Remote ringing...");
295 if (lc->vtable.display_status)
296 lc->vtable.display_status(lc,_("Remote ringing..."));
297 linphone_call_set_state(call,LinphoneCallOutgoingRinging,"Remote ringing");
299 /*accept early media */
300 if (call->audiostream && audio_stream_started(call->audiostream)){
301 /*streams already started */
302 ms_message("Early media already started.");
305 if (lc->vtable.show) lc->vtable.show(lc);
306 if (lc->vtable.display_status)
307 lc->vtable.display_status(lc,_("Early media."));
308 linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
309 if (lc->ringstream!=NULL){
310 ring_stop(lc->ringstream);
313 ms_message("Doing early media...");
314 linphone_core_update_streams(lc,call,md);
320 * - when the call is accepted
321 * - when a request is accepted (pause, resume)
323 static void call_accepted(SalOp *op){
324 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
325 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
326 SalMediaDescription *md;
329 ms_warning("No call to accept.");
333 /* Handle remote ICE attributes if any. */
334 if (call->ice_session != NULL) {
335 linphone_core_update_ice_from_remote_media_description(call, sal_call_get_remote_media_description(op));
338 md=sal_call_get_final_media_description(op);
339 call->params.has_video &= linphone_core_media_description_contains_video_stream(md);
341 if (call->state==LinphoneCallOutgoingProgress ||
342 call->state==LinphoneCallOutgoingRinging ||
343 call->state==LinphoneCallOutgoingEarlyMedia){
344 linphone_call_set_state(call,LinphoneCallConnected,"Connected");
345 if (call->referer) linphone_core_notify_refer_state(lc,call->referer,call);
347 if (md && !sal_media_description_empty(md)){
348 if (sal_media_description_has_dir(md,SalStreamSendOnly) ||
349 sal_media_description_has_dir(md,SalStreamInactive)){
350 if (lc->vtable.display_status){
351 char *tmp=linphone_call_get_remote_address_as_string (call);
352 char *msg=ms_strdup_printf(_("Call with %s is paused."),tmp);
353 lc->vtable.display_status(lc,msg);
357 linphone_core_update_streams (lc,call,md);
358 linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
359 if (call->refer_pending)
360 linphone_core_start_refered_call(lc,call);
361 }else if (sal_media_description_has_dir(md,SalStreamRecvOnly)){
362 /*we are put on hold when the call is initially accepted */
363 if (lc->vtable.display_status){
364 char *tmp=linphone_call_get_remote_address_as_string (call);
365 char *msg=ms_strdup_printf(_("Call answered by %s - on hold."),tmp);
366 lc->vtable.display_status(lc,msg);
370 linphone_core_update_streams (lc,call,md);
371 linphone_call_set_state(call,LinphoneCallPausedByRemote,"Call paused by remote");
373 if (call->state!=LinphoneCallUpdating){
374 if (call->state==LinphoneCallResuming){
375 if (lc->vtable.display_status){
376 lc->vtable.display_status(lc,_("Call resumed."));
379 if (lc->vtable.display_status){
380 char *tmp=linphone_call_get_remote_address_as_string (call);
381 char *msg=ms_strdup_printf(_("Call answered by %s."),tmp);
382 lc->vtable.display_status(lc,msg);
388 linphone_core_update_streams (lc,call,md);
389 if (!call->current_params.in_conference)
390 lc->current_call=call;
391 linphone_call_set_state(call, LinphoneCallStreamsRunning, "Streams running");
395 ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
396 linphone_core_abort_call(lc,call,_("Incompatible, check codecs..."));
400 static void call_ack(SalOp *op){
401 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
402 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
404 ms_warning("No call to be ACK'd");
407 if (call->media_pending){
408 SalMediaDescription *md=sal_call_get_final_media_description(op);
409 if (md && !sal_media_description_empty(md)){
410 linphone_core_update_streams (lc,call,md);
411 linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
414 ms_error("Incompatible SDP response received in ACK, need to abort the call");
415 linphone_core_abort_call(lc,call,"No codec intersection");
421 static void call_accept_update(LinphoneCore *lc, LinphoneCall *call){
422 SalMediaDescription *md;
423 SalMediaDescription *rmd=sal_call_get_remote_media_description(call->op);
424 if ((rmd!=NULL) && (call->ice_session!=NULL)) {
425 linphone_core_update_ice_from_remote_media_description(call,rmd);
426 linphone_core_update_local_media_description_from_ice(call->localdesc,call->ice_session);
428 sal_call_accept(call->op);
429 md=sal_call_get_final_media_description(call->op);
430 if (md && !sal_media_description_empty(md))
431 linphone_core_update_streams(lc,call,md);
434 static void call_resumed(LinphoneCore *lc, LinphoneCall *call){
435 call_accept_update(lc,call);
436 if(lc->vtable.display_status)
437 lc->vtable.display_status(lc,_("We have been resumed."));
438 linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
439 linphone_call_set_transfer_state(call, LinphoneCallIdle);
442 static void call_paused_by_remote(LinphoneCore *lc, LinphoneCall *call){
443 call_accept_update(lc,call);
444 /* we are being paused */
445 if(lc->vtable.display_status)
446 lc->vtable.display_status(lc,_("We are paused by other party."));
447 linphone_call_set_state (call,LinphoneCallPausedByRemote,"Call paused by remote");
450 static void call_updated_by_remote(LinphoneCore *lc, LinphoneCall *call){
451 if(lc->vtable.display_status)
452 lc->vtable.display_status(lc,_("Call is updated by remote."));
453 call->defer_update=FALSE;
454 linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
455 if (call->defer_update==FALSE){
456 linphone_core_accept_call_update(lc,call,NULL);
460 /* this callback is called when an incoming re-INVITE modifies the session*/
461 static void call_updating(SalOp *op){
462 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
463 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
464 SalMediaDescription *rmd=sal_call_get_remote_media_description(op);
467 /* case of a reINVITE without SDP */
468 call_accept_update(lc,call);
469 call->media_pending=TRUE;
474 case LinphoneCallPausedByRemote:
475 if (sal_media_description_has_dir(rmd,SalStreamSendRecv) || sal_media_description_has_dir(rmd,SalStreamRecvOnly)){
476 call_resumed(lc,call);
477 }else call_paused_by_remote(lc,call);
479 case LinphoneCallStreamsRunning:
480 case LinphoneCallConnected:
481 if (sal_media_description_has_dir(rmd,SalStreamSendOnly) || sal_media_description_has_dir(rmd,SalStreamInactive)){
482 call_paused_by_remote(lc,call);
484 call_updated_by_remote(lc,call);
488 call_accept_update(lc,call);
492 static void call_terminated(SalOp *op, const char *from){
493 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
494 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
496 if (call==NULL) return;
498 switch(linphone_call_get_state(call)){
499 case LinphoneCallEnd:
500 case LinphoneCallError:
501 ms_warning("call_terminated: ignoring.");
504 case LinphoneCallIncomingReceived:
505 case LinphoneCallIncomingEarlyMedia:
506 call->reason=LinphoneReasonNotAnswered;
511 ms_message("Current call terminated...");
512 //we stop the call only if we have this current call or if we are in call
513 if (lc->ringstream!=NULL && ( (ms_list_size(lc->calls) == 1) || linphone_core_in_call(lc) )) {
514 ring_stop(lc->ringstream);
517 linphone_call_stop_media_streams(call);
518 if (lc->vtable.show!=NULL)
520 if (lc->vtable.display_status!=NULL)
521 lc->vtable.display_status(lc,_("Call terminated."));
523 linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
526 static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
527 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
528 char *msg486=_("User is busy.");
529 char *msg480=_("User is temporarily unavailable.");
530 /*char *retrymsg=_("%s. Retry after %i minute(s).");*/
531 char *msg600=_("User does not want to be disturbed.");
532 char *msg603=_("Call declined.");
533 const char *msg=details;
534 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
537 ms_warning("Call faillure reported on already cleaned call ?");
541 if (lc->vtable.show) lc->vtable.show(lc);
543 if (error==SalErrorNoResponse){
544 msg=_("No response.");
545 if (lc->vtable.display_status)
546 lc->vtable.display_status(lc,msg);
547 }else if (error==SalErrorProtocol){
548 msg=details ? details : _("Protocol error.");
549 if (lc->vtable.display_status)
550 lc->vtable.display_status(lc, msg);
551 }else if (error==SalErrorFailure){
553 case SalReasonDeclined:
555 if (lc->vtable.display_status)
556 lc->vtable.display_status(lc,msg603);
560 if (lc->vtable.display_status)
561 lc->vtable.display_status(lc,msg486);
563 case SalReasonRedirect:
565 if (lc->vtable.display_status)
566 lc->vtable.display_status(lc,msg);
568 case SalReasonTemporarilyUnavailable:
570 if (lc->vtable.display_status)
571 lc->vtable.display_status(lc,msg480);
573 case SalReasonNotFound:
574 if (lc->vtable.display_status)
575 lc->vtable.display_status(lc,msg);
577 case SalReasonDoNotDisturb:
579 if (lc->vtable.display_status)
580 lc->vtable.display_status(lc,msg600);
583 //media_encryption_mandatory
584 if (call->params.media_encryption == LinphoneMediaEncryptionSRTP &&
585 !linphone_core_is_media_encryption_mandatory(lc)) {
587 ms_message("Outgoing call failed with SRTP (SAVP) enabled - retrying with AVP");
588 linphone_call_stop_media_streams(call);
589 if (call->state==LinphoneCallOutgoingInit || call->state==LinphoneCallOutgoingProgress){
590 /* clear SRTP local params */
591 call->params.media_encryption = LinphoneMediaEncryptionNone;
592 for(i=0; i<call->localdesc->nstreams; i++) {
593 call->localdesc->streams[i].proto = SalProtoRtpAvp;
594 memset(call->localdesc->streams[i].crypto, 0, sizeof(call->localdesc->streams[i].crypto));
596 linphone_core_start_invite(lc, call);
600 msg=_("Incompatible media parameters.");
601 if (lc->vtable.display_status)
602 lc->vtable.display_status(lc,msg);
605 if (lc->vtable.display_status)
606 lc->vtable.display_status(lc,_("Call failed."));
610 if (lc->ringstream!=NULL) {
611 ring_stop(lc->ringstream);
614 linphone_call_stop_media_streams (call);
615 if (call->referer && linphone_call_get_state(call->referer)==LinphoneCallPaused && call->referer->was_automatically_paused){
616 /*resume to the call that send us the refer automatically*/
617 linphone_core_resume_call(lc,call->referer);
619 if (sr == SalReasonDeclined) {
620 call->reason=LinphoneReasonDeclined;
621 linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
622 } else if (sr == SalReasonNotFound) {
623 call->reason=LinphoneReasonNotFound;
624 linphone_call_set_state(call,LinphoneCallError,"User not found.");
626 linphone_call_set_state(call,LinphoneCallError,msg);
630 static void call_released(SalOp *op){
631 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
633 linphone_call_set_state(call,LinphoneCallReleased,"Call released");
634 }else ms_error("call_released() for already destroyed call ?");
637 static void auth_requested(SalOp *h, const char *realm, const char *username){
638 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
639 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
640 LinphoneCall *call=is_a_linphone_call(sal_op_get_user_pointer(h));
642 if (call && call->ping_op==h){
643 /*don't request authentication for ping requests. Their purpose is just to get any
644 * answer to get the Via's received and rport parameters.
646 ms_message("auth_requested(): ignored for ping request.");
650 ms_message("auth_requested() for realm=%s, username=%s",realm,username);
652 if (ai && ai->works==FALSE && ai->usecount>=3){
653 /*case we tried 3 times to authenticate, without success */
654 /*Better is to stop (implemeted below in else statement), and retry later*/
655 if (ms_time(NULL)-ai->last_use_time>30){
656 ai->usecount=0; /*so that we can allow to retry */
660 if (ai && (ai->works || ai->usecount<3)){
662 sai.username=ai->username;
663 sai.userid=ai->userid;
665 sai.password=ai->passwd;
666 ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
667 sal_op_authenticate(h,&sai);
669 ai->last_use_time=ms_time(NULL);
671 if (ai && ai->works==FALSE) {
672 sal_op_cancel_authentication(h);
674 if (lc->vtable.auth_info_requested)
675 lc->vtable.auth_info_requested(lc,realm,username);
679 static void auth_success(SalOp *h, const char *realm, const char *username){
680 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
681 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
683 ms_message("%s/%s authentication works.",realm,username);
688 static void register_success(SalOp *op, bool_t registered){
689 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
690 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
693 if (cfg->deletion_date!=0){
694 ms_message("Registration success for removed proxy config, ignored");
697 linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
698 linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
699 registered ? "Registration sucessful" : "Unregistration done");
700 if (lc->vtable.display_status){
701 if (registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
702 else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
703 lc->vtable.display_status(lc,msg);
709 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
710 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
711 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
714 ms_warning("Registration failed for unknown proxy config.");
717 if (cfg->deletion_date!=0){
718 ms_message("Registration failed for removed proxy config, ignored");
722 details=_("no response timeout");
724 if (lc->vtable.display_status) {
725 char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),details );
726 lc->vtable.display_status(lc,msg);
729 if (error== SalErrorFailure && reason == SalReasonForbidden) {
730 linphone_proxy_config_set_error(cfg, LinphoneReasonBadCredentials);
731 } else if (error == SalErrorNoResponse) {
732 linphone_proxy_config_set_error(cfg, LinphoneReasonNoResponse);
734 linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
735 if (error== SalErrorFailure && reason == SalReasonForbidden) {
736 const char *realm=NULL,*username=NULL;
737 if (sal_op_get_auth_requested(op,&realm,&username)==0){
738 if (lc->vtable.auth_info_requested)
739 lc->vtable.auth_info_requested(lc,realm,username);
744 static void vfu_request(SalOp *op){
746 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
748 ms_warning("VFU request but no call !");
751 if (call->videostream)
752 video_stream_send_vfu(call->videostream);
756 static void dtmf_received(SalOp *op, char dtmf){
757 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
758 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
759 if (lc->vtable.dtmf_received != NULL)
760 lc->vtable.dtmf_received(lc, call, dtmf);
763 static void refer_received(Sal *sal, SalOp *op, const char *referto){
764 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
765 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
767 if (call->refer_to!=NULL){
768 ms_free(call->refer_to);
770 call->refer_to=ms_strdup(referto);
771 call->refer_pending=TRUE;
772 linphone_call_set_state(call,LinphoneCallRefered,"Refered");
773 if (lc->vtable.display_status){
774 char *msg=ms_strdup_printf(_("We are transferred to %s"),referto);
775 lc->vtable.display_status(lc,msg);
778 if (call->state!=LinphoneCallPaused){
779 ms_message("Automatically pausing current call to accept transfer.");
780 linphone_core_pause_call(lc,call);
781 call->was_automatically_paused=TRUE;
782 /*then we will start the refered when the pause is accepted, in order to serialize transactions within the dialog.
783 * Indeed we need to avoid to send a NOTIFY to inform about of state of the refered call while the pause isn't completed.
785 }else linphone_core_start_refered_call(lc,call);
786 }else if (lc->vtable.refer_received){
787 lc->vtable.refer_received(lc,referto);
791 static bool_t is_duplicate_msg(LinphoneCore *lc, const char *msg_id){
792 MSList *elem=lc->last_recv_msg_ids;
795 bool_t is_duplicate=FALSE;
796 for(i=0;elem!=NULL;elem=elem->next,i++){
797 if (strcmp((const char*)elem->data,msg_id)==0){
803 lc->last_recv_msg_ids=ms_list_prepend(lc->last_recv_msg_ids,ms_strdup(msg_id));
807 lc->last_recv_msg_ids=ms_list_remove_link(lc->last_recv_msg_ids,tail);
813 static void text_received(Sal *sal, const SalMessage *msg){
814 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
815 if (is_duplicate_msg(lc,msg->message_id)==FALSE){
816 linphone_core_message_received(lc,msg->from,msg->text,msg->url);
820 static void notify(SalOp *op, const char *from, const char *msg){
821 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
822 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
823 ms_message("get a %s notify from %s",msg,from);
824 if(lc->vtable.notify_recv)
825 lc->vtable.notify_recv(lc,call,from,msg);
828 static void notify_presence(SalOp *op, SalSubscribeStatus ss, SalPresenceStatus status, const char *msg){
829 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
830 linphone_notify_recv(lc,op,ss,status);
833 static void subscribe_received(SalOp *op, const char *from){
834 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
835 linphone_subscription_new(lc,op,from);
838 static void subscribe_closed(SalOp *op, const char *from){
839 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
840 linphone_subscription_closed(lc,op);
843 static void ping_reply(SalOp *op){
844 LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
845 ms_message("ping reply !");
847 if (call->state==LinphoneCallOutgoingInit){
848 call->ping_replied=TRUE;
849 linphone_core_proceed_with_invite_if_ready(call->core,call,NULL);
854 ms_warning("ping reply without call attached...");
858 static void notify_refer(SalOp *op, SalReferStatus status){
859 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
860 LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
861 LinphoneCallState cstate;
863 ms_warning("Receiving notify_refer for unknown call.");
868 cstate=LinphoneCallOutgoingProgress;
870 case SalReferSuccess:
871 cstate=LinphoneCallConnected;
874 cstate=LinphoneCallError;
877 cstate=LinphoneCallError;
879 linphone_call_set_transfer_state(call, cstate);
880 if (cstate==LinphoneCallConnected){
881 /*automatically terminate the call as the transfer is complete.*/
882 linphone_core_terminate_call(lc,call);
886 static LinphoneChatMessageState chatStatusSal2Linphone(SalTextDeliveryStatus status){
888 case SalTextDeliveryInProgress:
889 return LinphoneChatMessageStateInProgress;
890 case SalTextDeliveryDone:
891 return LinphoneChatMessageStateDelivered;
892 case SalTextDeliveryFailed:
893 return LinphoneChatMessageStateNotDelivered;
895 return LinphoneChatMessageStateIdle;
898 static int op_equals(LinphoneCall *a, SalOp *b) {
899 return a->op !=b; /*return 0 if equals*/
901 static void text_delivery_update(SalOp *op, SalTextDeliveryStatus status){
902 LinphoneChatMessage *chat_msg=(LinphoneChatMessage* )sal_op_get_user_pointer(op);
903 const MSList* calls = linphone_core_get_calls(chat_msg->chat_room->lc);
905 if (chat_msg && chat_msg->cb) {
906 chat_msg->cb(chat_msg
907 ,chatStatusSal2Linphone(status)
910 linphone_chat_message_destroy(chat_msg);
912 if (!ms_list_find_custom((MSList*)calls, (MSCompareFunc) op_equals, op)) {
913 /*op was only create for messaging purpose, destroying*/
918 SalCallbacks linphone_sal_callbacks={
935 text_delivery_update,