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_UNCHANGED) && !call->playing_ringbacktone) {
108 /*as nothing has changed, keep the oldmd */
109 call->resultdesc=oldmd;
110 sal_media_description_unref(new_md);
111 if (call->all_muted){
112 ms_message("Early media finished, unmuting inputs...");
113 /*we were in early media, now we want to enable real media */
114 linphone_call_enable_camera (call,linphone_call_camera_enabled (call));
115 if (call->audiostream)
116 linphone_core_mute_mic (lc, linphone_core_is_mic_muted(lc));
118 if (call->videostream && call->camera_active)
119 video_stream_change_camera(call->videostream,lc->video_conf.device );
122 ms_message("No need to restart streams, SDP is unchanged.");
124 } else if ((md_changed == SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED) && !call->playing_ringbacktone) {
125 call->resultdesc = oldmd;
126 ms_message("Network parameters have changed, update them.");
127 linphone_core_update_streams_destinations(lc, call, oldmd, new_md);
128 sal_media_description_unref(new_md);
131 ms_message("Media descriptions are different, need to restart the streams.");
134 linphone_call_stop_media_streams (call);
135 linphone_call_init_media_streams (call);
138 sal_media_description_unref(oldmd);
141 bool_t all_muted=FALSE;
142 bool_t send_ringbacktone=FALSE;
144 if (call->audiostream==NULL){
145 /*this happens after pausing the call locally. The streams is destroyed and then we wait the 200Ok to recreate it*/
146 linphone_call_init_media_streams (call);
148 if (call->state==LinphoneCallIncomingEarlyMedia && linphone_core_get_remote_ringback_tone (lc)!=NULL){
149 send_ringbacktone=TRUE;
151 if (call->state==LinphoneCallIncomingEarlyMedia ||
152 (call->state==LinphoneCallOutgoingEarlyMedia && !call->params.real_early_media)){
155 linphone_call_start_media_streams(call,all_muted,send_ringbacktone);
159 static bool_t is_duplicate_call(LinphoneCore *lc, const LinphoneAddress *from, const LinphoneAddress *to){
161 for(elem=lc->calls;elem!=NULL;elem=elem->next){
162 LinphoneCall *call=(LinphoneCall*)elem->data;
163 if (linphone_address_weak_equal(call->log->from,from) &&
164 linphone_address_weak_equal(call->log->to, to)){
172 static bool_t already_a_call_with_remote_address(const LinphoneCore *lc, const LinphoneAddress *remote) {
173 ms_warning(" searching for already_a_call_with_remote_address.");
176 for(elem=lc->calls;elem!=NULL;elem=elem->next){
177 const LinphoneCall *call=(LinphoneCall*)elem->data;
178 const LinphoneAddress *cRemote=linphone_call_get_remote_address(call);
179 if (linphone_address_weak_equal(cRemote,remote)) {
180 ms_warning("already_a_call_with_remote_address found.");
187 static bool_t already_a_call_pending(LinphoneCore *lc){
189 for(elem=lc->calls;elem!=NULL;elem=elem->next){
190 LinphoneCall *call=(LinphoneCall*)elem->data;
191 if (call->state==LinphoneCallIncomingReceived
192 || call->state==LinphoneCallOutgoingInit
193 || call->state==LinphoneCallOutgoingProgress
194 || call->state==LinphoneCallOutgoingEarlyMedia
195 || call->state==LinphoneCallOutgoingRinging){
202 static void call_received(SalOp *h){
203 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
205 const char *from,*to;
206 LinphoneAddress *from_addr, *to_addr;
207 bool_t prevent_colliding_calls=lp_config_get_int(lc->config,"sip","prevent_colliding_calls",TRUE);
209 /* first check if we can answer successfully to this invite */
210 if (lc->presence_mode==LinphoneStatusBusy ||
211 lc->presence_mode==LinphoneStatusOffline ||
212 lc->presence_mode==LinphoneStatusDoNotDisturb ||
213 lc->presence_mode==LinphoneStatusMoved){
214 if (lc->presence_mode==LinphoneStatusBusy )
215 sal_call_decline(h,SalReasonBusy,NULL);
216 else if (lc->presence_mode==LinphoneStatusOffline)
217 sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
218 else if (lc->presence_mode==LinphoneStatusDoNotDisturb)
219 sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
220 else if (lc->alt_contact!=NULL && lc->presence_mode==LinphoneStatusMoved)
221 sal_call_decline(h,SalReasonRedirect,lc->alt_contact);
225 if (!linphone_core_can_we_add_call(lc)){/*busy*/
226 sal_call_decline(h,SalReasonBusy,NULL);
230 from=sal_op_get_from(h);
232 from_addr=linphone_address_new(from);
233 to_addr=linphone_address_new(to);
235 if ((already_a_call_with_remote_address(lc,from_addr) && prevent_colliding_calls) || already_a_call_pending(lc)){
236 ms_warning("Receiving another call while one is ringing or initiated, refusing this one with busy message.");
237 sal_call_decline(h,SalReasonBusy,NULL);
239 linphone_address_destroy(from_addr);
240 linphone_address_destroy(to_addr);
244 call=linphone_call_new_incoming(lc,from_addr,to_addr,h);
246 /* the call is acceptable so we can now add it to our list */
247 linphone_core_add_call(lc,call);
248 linphone_call_ref(call); /*prevent the call from being destroyed while we are notifying, if the user declines within the state callback */
250 if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && (call->ice_session != NULL)) {
251 /* Defer ringing until the end of the ICE candidates gathering process. */
252 ms_message("Defer ringing to gather ICE candidates");
256 linphone_core_notify_incoming_call(lc,call);
259 static void call_ringing(SalOp *h){
260 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
261 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
262 SalMediaDescription *md;
264 if (call==NULL) return;
266 if (lc->vtable.display_status)
267 lc->vtable.display_status(lc,_("Remote ringing."));
269 md=sal_call_get_final_media_description(h);
271 if (lc->ringstream && lc->dmfs_playing_start_time!=0){
272 ring_stop(lc->ringstream);
274 lc->dmfs_playing_start_time=0;
276 if (lc->ringstream!=NULL) return; /*already ringing !*/
277 if (lc->sound_conf.play_sndcard!=NULL){
278 MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
279 if (call->localdesc->streams[0].max_rate>0) ms_snd_card_set_preferred_sample_rate(ringcard, call->localdesc->streams[0].max_rate);
280 /*we release sound before playing ringback tone*/
281 if (call->audiostream)
282 audio_stream_unprepare_sound(call->audiostream);
283 lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,ringcard);
285 ms_message("Remote ringing...");
286 if (lc->vtable.display_status)
287 lc->vtable.display_status(lc,_("Remote ringing..."));
288 linphone_call_set_state(call,LinphoneCallOutgoingRinging,"Remote ringing");
290 /*accept early media */
291 if (call->audiostream && audio_stream_started(call->audiostream)){
292 /*streams already started */
293 ms_message("Early media already started.");
296 if (lc->vtable.show) lc->vtable.show(lc);
297 if (lc->vtable.display_status)
298 lc->vtable.display_status(lc,_("Early media."));
299 linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
300 if (lc->ringstream!=NULL){
301 ring_stop(lc->ringstream);
304 ms_message("Doing early media...");
305 linphone_core_update_streams(lc,call,md);
311 * - when the call is accepted
312 * - when a request is accepted (pause, resume)
314 static void call_accepted(SalOp *op){
315 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
316 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
317 SalMediaDescription *md;
320 ms_warning("No call to accept.");
324 /* Handle remote ICE attributes if any. */
325 if (call->ice_session != NULL) {
326 linphone_core_update_ice_from_remote_media_description(call, sal_call_get_remote_media_description(op));
329 md=sal_call_get_final_media_description(op);
330 call->params.has_video &= linphone_core_media_description_contains_video_stream(md);
332 if (call->state==LinphoneCallOutgoingProgress ||
333 call->state==LinphoneCallOutgoingRinging ||
334 call->state==LinphoneCallOutgoingEarlyMedia){
335 linphone_call_set_state(call,LinphoneCallConnected,"Connected");
336 if (call->referer) linphone_core_notify_refer_state(lc,call->referer,call);
338 if (md && !sal_media_description_empty(md)){
339 if (sal_media_description_has_dir(md,SalStreamSendOnly) ||
340 sal_media_description_has_dir(md,SalStreamInactive)){
341 if (lc->vtable.display_status){
342 char *tmp=linphone_call_get_remote_address_as_string (call);
343 char *msg=ms_strdup_printf(_("Call with %s is paused."),tmp);
344 lc->vtable.display_status(lc,msg);
348 linphone_core_update_streams (lc,call,md);
349 linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
350 if (call->refer_pending)
351 linphone_core_start_refered_call(lc,call);
352 }else if (sal_media_description_has_dir(md,SalStreamRecvOnly)){
353 /*we are put on hold when the call is initially accepted */
354 if (lc->vtable.display_status){
355 char *tmp=linphone_call_get_remote_address_as_string (call);
356 char *msg=ms_strdup_printf(_("Call answered by %s - on hold."),tmp);
357 lc->vtable.display_status(lc,msg);
361 linphone_core_update_streams (lc,call,md);
362 linphone_call_set_state(call,LinphoneCallPausedByRemote,"Call paused by remote");
364 if (call->state!=LinphoneCallUpdating){
365 if (call->state==LinphoneCallResuming){
366 if (lc->vtable.display_status){
367 lc->vtable.display_status(lc,_("Call resumed."));
370 if (lc->vtable.display_status){
371 char *tmp=linphone_call_get_remote_address_as_string (call);
372 char *msg=ms_strdup_printf(_("Call answered by %s."),tmp);
373 lc->vtable.display_status(lc,msg);
379 linphone_core_update_streams (lc,call,md);
380 if (!call->current_params.in_conference)
381 lc->current_call=call;
382 linphone_call_set_state(call, LinphoneCallStreamsRunning, "Streams running");
386 ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
387 linphone_core_abort_call(lc,call,_("Incompatible, check codecs..."));
391 static void call_ack(SalOp *op){
392 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
393 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
395 ms_warning("No call to be ACK'd");
398 if (call->media_pending){
399 SalMediaDescription *md=sal_call_get_final_media_description(op);
400 if (md && !sal_media_description_empty(md)){
401 linphone_core_update_streams (lc,call,md);
402 linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
405 ms_error("Incompatible SDP response received in ACK, need to abort the call");
406 linphone_core_abort_call(lc,call,"No codec intersection");
412 static void call_accept_update(LinphoneCore *lc, LinphoneCall *call){
413 SalMediaDescription *md;
414 SalMediaDescription *rmd=sal_call_get_remote_media_description(call->op);
415 if ((rmd!=NULL) && (call->ice_session!=NULL)) {
416 linphone_core_update_ice_from_remote_media_description(call,rmd);
417 linphone_core_update_local_media_description_from_ice(call->localdesc,call->ice_session);
419 sal_call_accept(call->op);
420 md=sal_call_get_final_media_description(call->op);
421 if (md && !sal_media_description_empty(md))
422 linphone_core_update_streams(lc,call,md);
425 static void call_resumed(LinphoneCore *lc, LinphoneCall *call){
426 call_accept_update(lc,call);
427 if(lc->vtable.display_status)
428 lc->vtable.display_status(lc,_("We have been resumed."));
429 linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
430 linphone_call_set_transfer_state(call, LinphoneCallIdle);
433 static void call_paused_by_remote(LinphoneCore *lc, LinphoneCall *call){
434 call_accept_update(lc,call);
435 /* we are being paused */
436 if(lc->vtable.display_status)
437 lc->vtable.display_status(lc,_("We are paused by other party."));
438 linphone_call_set_state (call,LinphoneCallPausedByRemote,"Call paused by remote");
441 static void call_updated_by_remote(LinphoneCore *lc, LinphoneCall *call){
442 if(lc->vtable.display_status)
443 lc->vtable.display_status(lc,_("Call is updated by remote."));
444 call->defer_update=FALSE;
445 linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
446 if (call->defer_update==FALSE){
447 linphone_core_accept_call_update(lc,call,NULL);
451 /* this callback is called when an incoming re-INVITE modifies the session*/
452 static void call_updating(SalOp *op){
453 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
454 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
455 SalMediaDescription *rmd=sal_call_get_remote_media_description(op);
458 /* case of a reINVITE without SDP */
459 call_accept_update(lc,call);
460 call->media_pending=TRUE;
465 case LinphoneCallPausedByRemote:
466 if (sal_media_description_has_dir(rmd,SalStreamSendRecv) || sal_media_description_has_dir(rmd,SalStreamRecvOnly)){
467 call_resumed(lc,call);
468 }else call_paused_by_remote(lc,call);
470 case LinphoneCallStreamsRunning:
471 case LinphoneCallConnected:
472 if (sal_media_description_has_dir(rmd,SalStreamSendOnly) || sal_media_description_has_dir(rmd,SalStreamInactive)){
473 call_paused_by_remote(lc,call);
475 call_updated_by_remote(lc,call);
479 call_accept_update(lc,call);
483 static void call_terminated(SalOp *op, const char *from){
484 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
485 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
487 if (call==NULL) return;
489 switch(linphone_call_get_state(call)){
490 case LinphoneCallEnd:
491 case LinphoneCallError:
492 ms_warning("call_terminated: ignoring.");
495 case LinphoneCallIncomingReceived:
496 case LinphoneCallIncomingEarlyMedia:
497 call->reason=LinphoneReasonNotAnswered;
502 ms_message("Current call terminated...");
503 //we stop the call only if we have this current call or if we are in call
504 if (lc->ringstream!=NULL && ( (ms_list_size(lc->calls) == 1) || linphone_core_in_call(lc) )) {
505 ring_stop(lc->ringstream);
508 linphone_call_stop_media_streams(call);
509 if (lc->vtable.show!=NULL)
511 if (lc->vtable.display_status!=NULL)
512 lc->vtable.display_status(lc,_("Call terminated."));
514 linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
517 static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
518 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
519 char *msg486=_("User is busy.");
520 char *msg480=_("User is temporarily unavailable.");
521 /*char *retrymsg=_("%s. Retry after %i minute(s).");*/
522 char *msg600=_("User does not want to be disturbed.");
523 char *msg603=_("Call declined.");
524 const char *msg=details;
525 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
528 ms_warning("Call faillure reported on already cleaned call ?");
532 if (lc->vtable.show) lc->vtable.show(lc);
534 if (error==SalErrorNoResponse){
535 msg=_("No response.");
536 if (lc->vtable.display_status)
537 lc->vtable.display_status(lc,msg);
538 }else if (error==SalErrorProtocol){
539 msg=details ? details : _("Protocol error.");
540 if (lc->vtable.display_status)
541 lc->vtable.display_status(lc, msg);
542 }else if (error==SalErrorFailure){
544 case SalReasonDeclined:
546 if (lc->vtable.display_status)
547 lc->vtable.display_status(lc,msg603);
551 if (lc->vtable.display_status)
552 lc->vtable.display_status(lc,msg486);
554 case SalReasonRedirect:
556 if (lc->vtable.display_status)
557 lc->vtable.display_status(lc,msg);
559 case SalReasonTemporarilyUnavailable:
561 if (lc->vtable.display_status)
562 lc->vtable.display_status(lc,msg480);
564 case SalReasonNotFound:
565 if (lc->vtable.display_status)
566 lc->vtable.display_status(lc,msg);
568 case SalReasonDoNotDisturb:
570 if (lc->vtable.display_status)
571 lc->vtable.display_status(lc,msg600);
574 //media_encryption_mandatory
575 if (call->params.media_encryption == LinphoneMediaEncryptionSRTP &&
576 !linphone_core_is_media_encryption_mandatory(lc)) {
578 ms_message("Outgoing call failed with SRTP (SAVP) enabled - retrying with AVP");
579 linphone_call_stop_media_streams(call);
580 if (call->state==LinphoneCallOutgoingInit || call->state==LinphoneCallOutgoingProgress){
581 /* clear SRTP local params */
582 call->params.media_encryption = LinphoneMediaEncryptionNone;
583 for(i=0; i<call->localdesc->nstreams; i++) {
584 call->localdesc->streams[i].proto = SalProtoRtpAvp;
585 memset(call->localdesc->streams[i].crypto, 0, sizeof(call->localdesc->streams[i].crypto));
587 linphone_core_start_invite(lc, call);
591 msg=_("Incompatible media parameters.");
592 if (lc->vtable.display_status)
593 lc->vtable.display_status(lc,msg);
596 if (lc->vtable.display_status)
597 lc->vtable.display_status(lc,_("Call failed."));
601 if (lc->ringstream!=NULL) {
602 ring_stop(lc->ringstream);
605 linphone_call_stop_media_streams (call);
606 if (call->referer && linphone_call_get_state(call->referer)==LinphoneCallPaused && call->referer->was_automatically_paused){
607 /*resume to the call that send us the refer automatically*/
608 linphone_core_resume_call(lc,call->referer);
610 if (sr == SalReasonDeclined) {
611 call->reason=LinphoneReasonDeclined;
612 linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
613 } else if (sr == SalReasonNotFound) {
614 call->reason=LinphoneReasonNotFound;
615 linphone_call_set_state(call,LinphoneCallError,"User not found.");
617 linphone_call_set_state(call,LinphoneCallError,msg);
621 static void call_released(SalOp *op){
622 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
624 linphone_call_set_state(call,LinphoneCallReleased,"Call released");
625 }else ms_error("call_released() for already destroyed call ?");
628 static void auth_requested(SalOp *h, const char *realm, const char *username){
629 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
630 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
631 LinphoneCall *call=is_a_linphone_call(sal_op_get_user_pointer(h));
633 if (call && call->ping_op==h){
634 /*don't request authentication for ping requests. Their purpose is just to get any
635 * answer to get the Via's received and rport parameters.
637 ms_message("auth_requested(): ignored for ping request.");
641 ms_message("auth_requested() for realm=%s, username=%s",realm,username);
643 if (ai && ai->works==FALSE && ai->usecount>=3){
644 /*case we tried 3 times to authenticate, without success */
645 /*Better is to stop (implemeted below in else statement), and retry later*/
646 if (ms_time(NULL)-ai->last_use_time>30){
647 ai->usecount=0; /*so that we can allow to retry */
651 if (ai && (ai->works || ai->usecount<3)){
653 sai.username=ai->username;
654 sai.userid=ai->userid;
656 sai.password=ai->passwd;
657 ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
658 sal_op_authenticate(h,&sai);
660 ai->last_use_time=ms_time(NULL);
662 if (ai && ai->works==FALSE) {
663 sal_op_cancel_authentication(h);
665 if (lc->vtable.auth_info_requested)
666 lc->vtable.auth_info_requested(lc,realm,username);
670 static void auth_success(SalOp *h, const char *realm, const char *username){
671 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
672 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
674 ms_message("%s/%s authentication works.",realm,username);
679 static void register_success(SalOp *op, bool_t registered){
680 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
681 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
684 if (cfg->deletion_date!=0){
685 ms_message("Registration success for removed proxy config, ignored");
688 linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
689 linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
690 registered ? "Registration sucessful" : "Unregistration done");
691 if (lc->vtable.display_status){
692 if (registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
693 else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
694 lc->vtable.display_status(lc,msg);
700 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
701 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
702 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
705 ms_warning("Registration failed for unknown proxy config.");
708 if (cfg->deletion_date!=0){
709 ms_message("Registration failed for removed proxy config, ignored");
713 details=_("no response timeout");
715 if (lc->vtable.display_status) {
716 char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),details );
717 lc->vtable.display_status(lc,msg);
720 if (error== SalErrorFailure && reason == SalReasonForbidden) {
721 linphone_proxy_config_set_error(cfg, LinphoneReasonBadCredentials);
722 } else if (error == SalErrorNoResponse) {
723 linphone_proxy_config_set_error(cfg, LinphoneReasonNoResponse);
725 linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
726 if (error== SalErrorFailure && reason == SalReasonForbidden) {
727 const char *realm=NULL,*username=NULL;
728 if (sal_op_get_auth_requested(op,&realm,&username)==0){
729 if (lc->vtable.auth_info_requested)
730 lc->vtable.auth_info_requested(lc,realm,username);
735 static void vfu_request(SalOp *op){
737 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
739 ms_warning("VFU request but no call !");
742 if (call->videostream)
743 video_stream_send_vfu(call->videostream);
747 static void dtmf_received(SalOp *op, char dtmf){
748 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
749 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
750 if (lc->vtable.dtmf_received != NULL)
751 lc->vtable.dtmf_received(lc, call, dtmf);
754 static void refer_received(Sal *sal, SalOp *op, const char *referto){
755 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
756 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
758 if (call->refer_to!=NULL){
759 ms_free(call->refer_to);
761 call->refer_to=ms_strdup(referto);
762 call->refer_pending=TRUE;
763 linphone_call_set_state(call,LinphoneCallRefered,"Refered");
764 if (lc->vtable.display_status){
765 char *msg=ms_strdup_printf(_("We are transferred to %s"),referto);
766 lc->vtable.display_status(lc,msg);
769 if (call->state!=LinphoneCallPaused){
770 ms_message("Automatically pausing current call to accept transfer.");
771 linphone_core_pause_call(lc,call);
772 call->was_automatically_paused=TRUE;
773 /*then we will start the refered when the pause is accepted, in order to serialize transactions within the dialog.
774 * Indeed we need to avoid to send a NOTIFY to inform about of state of the refered call while the pause isn't completed.
776 }else linphone_core_start_refered_call(lc,call);
777 }else if (lc->vtable.refer_received){
778 lc->vtable.refer_received(lc,referto);
782 static bool_t is_duplicate_msg(LinphoneCore *lc, const char *msg_id){
783 MSList *elem=lc->last_recv_msg_ids;
786 bool_t is_duplicate=FALSE;
787 for(i=0;elem!=NULL;elem=elem->next,i++){
788 if (strcmp((const char*)elem->data,msg_id)==0){
794 lc->last_recv_msg_ids=ms_list_prepend(lc->last_recv_msg_ids,ms_strdup(msg_id));
798 lc->last_recv_msg_ids=ms_list_remove_link(lc->last_recv_msg_ids,tail);
804 static void text_received(Sal *sal, const SalMessage *msg){
805 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
806 if (is_duplicate_msg(lc,msg->message_id)==FALSE){
807 linphone_core_message_received(lc,msg->from,msg->text,msg->url);
811 static void notify(SalOp *op, const char *from, const char *msg){
812 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
813 LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
814 ms_message("get a %s notify from %s",msg,from);
815 if(lc->vtable.notify_recv)
816 lc->vtable.notify_recv(lc,call,from,msg);
819 static void notify_presence(SalOp *op, SalSubscribeStatus ss, SalPresenceStatus status, const char *msg){
820 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
821 linphone_notify_recv(lc,op,ss,status);
824 static void subscribe_received(SalOp *op, const char *from){
825 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
826 linphone_subscription_new(lc,op,from);
829 static void subscribe_closed(SalOp *op, const char *from){
830 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
831 linphone_subscription_closed(lc,op);
834 static void ping_reply(SalOp *op){
835 LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
836 ms_message("ping reply !");
838 if (call->state==LinphoneCallOutgoingInit){
839 call->ping_replied=TRUE;
840 linphone_core_proceed_with_invite_if_ready(call->core,call,NULL);
845 ms_warning("ping reply without call attached...");
849 static void notify_refer(SalOp *op, SalReferStatus status){
850 LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
851 LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
852 LinphoneCallState cstate;
854 ms_warning("Receiving notify_refer for unknown call.");
859 cstate=LinphoneCallOutgoingProgress;
861 case SalReferSuccess:
862 cstate=LinphoneCallConnected;
865 cstate=LinphoneCallError;
868 cstate=LinphoneCallError;
870 linphone_call_set_transfer_state(call, cstate);
871 if (cstate==LinphoneCallConnected){
872 /*automatically terminate the call as the transfer is complete.*/
873 linphone_core_terminate_call(lc,call);
877 static LinphoneChatMessageState chatStatusSal2Linphone(SalTextDeliveryStatus status){
879 case SalTextDeliveryInProgress:
880 return LinphoneChatMessageStateInProgress;
881 case SalTextDeliveryDone:
882 return LinphoneChatMessageStateDelivered;
883 case SalTextDeliveryFailed:
884 return LinphoneChatMessageStateNotDelivered;
886 return LinphoneChatMessageStateIdle;
889 static int op_equals(LinphoneCall *a, SalOp *b) {
890 return a->op !=b; /*return 0 if equals*/
892 static void text_delivery_update(SalOp *op, SalTextDeliveryStatus status){
893 LinphoneChatMessage *chat_msg=(LinphoneChatMessage* )sal_op_get_user_pointer(op);
894 const MSList* calls = linphone_core_get_calls(chat_msg->chat_room->lc);
896 if (chat_msg && chat_msg->cb) {
897 chat_msg->cb(chat_msg
898 ,chatStatusSal2Linphone(status)
901 linphone_chat_message_destroy(chat_msg);
903 if (!ms_list_find_custom((MSList*)calls, (MSCompareFunc) op_equals, op)) {
904 /*op was only create for messaging purpose, destroying*/
909 SalCallbacks linphone_sal_callbacks={
926 text_delivery_update,