]> sjero.net Git - linphone/blob - coreapi/callbacks.c
Fix some crashes related to asynchronous ICE candidates gathering.
[linphone] / coreapi / callbacks.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
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.
9
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.
14
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.
18 */
19
20
21 #include "sal.h"
22
23 #include "linphonecore.h"
24 #include "private.h"
25 #include "mediastreamer2/mediastream.h"
26 #include "lpconfig.h"
27
28 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details);
29
30 static bool_t media_parameters_changed(LinphoneCall *call, SalMediaDescription *oldmd, SalMediaDescription *newmd){
31         if (call->params.in_conference!=call->current_params.in_conference) return TRUE;
32         return !sal_media_description_equals(oldmd,newmd)  || call->up_bw!=linphone_core_get_upload_bandwidth(call->core);
33 }
34
35 void linphone_core_update_streams(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription *new_md){
36         SalMediaDescription *oldmd=call->resultdesc;
37         
38         if (lc->ringstream!=NULL){
39                 ring_stop(lc->ringstream);
40                 lc->ringstream=NULL;
41         }
42         if (new_md!=NULL){
43                 sal_media_description_ref(new_md);
44                 call->media_pending=FALSE;
45         }else{
46                 call->media_pending=TRUE;
47         }
48         call->resultdesc=new_md;
49         if (call->audiostream && call->audiostream->ticker){
50                 /* we already started media: check if we really need to restart it*/
51                 if (oldmd){
52                         if (!media_parameters_changed(call,oldmd,new_md) && !call->playing_ringbacktone){
53                                 /*as nothing has changed, keep the oldmd */
54                                 call->resultdesc=oldmd;
55                                 sal_media_description_unref(new_md);
56                                 if (call->all_muted){
57                                         ms_message("Early media finished, unmuting inputs...");
58                                         /*we were in early media, now we want to enable real media */
59                                         linphone_call_enable_camera (call,linphone_call_camera_enabled (call));
60                                         if (call->audiostream)
61                                                 linphone_core_mute_mic (lc, linphone_core_is_mic_muted(lc));
62 #ifdef VIDEO_ENABLED
63                                         if (call->videostream && call->camera_active)
64                                                 video_stream_change_camera(call->videostream,lc->video_conf.device );
65 #endif
66                                 }
67                                 ms_message("No need to restart streams, SDP is unchanged.");
68                                 return;
69                         }else{
70                                 ms_message("Media descriptions are different, need to restart the streams.");
71                         }
72                 }
73                 linphone_call_stop_media_streams (call);
74                 linphone_call_init_media_streams (call);
75         }
76         if (oldmd) 
77                 sal_media_description_unref(oldmd);
78         
79         if (new_md) {
80                 bool_t all_muted=FALSE;
81                 bool_t send_ringbacktone=FALSE;
82                 
83                 if (call->audiostream==NULL){
84                         /*this happens after pausing the call locally. The streams is destroyed and then we wait the 200Ok to recreate it*/
85                         linphone_call_init_media_streams (call);
86                 }
87                 if (call->state==LinphoneCallIncomingEarlyMedia && linphone_core_get_remote_ringback_tone (lc)!=NULL){
88                         send_ringbacktone=TRUE;
89                 }
90                 if (call->state==LinphoneCallIncomingEarlyMedia ||
91                     (call->state==LinphoneCallOutgoingEarlyMedia && !call->params.real_early_media)){
92                         all_muted=TRUE;
93                 }
94                 linphone_call_start_media_streams(call,all_muted,send_ringbacktone);
95         }
96 }
97 #if 0
98 static bool_t is_duplicate_call(LinphoneCore *lc, const LinphoneAddress *from, const LinphoneAddress *to){
99         MSList *elem;
100         for(elem=lc->calls;elem!=NULL;elem=elem->next){
101                 LinphoneCall *call=(LinphoneCall*)elem->data;
102                 if (linphone_address_weak_equal(call->log->from,from) &&
103                     linphone_address_weak_equal(call->log->to, to)){
104                         return TRUE;
105                 }
106         }
107         return FALSE;
108 }
109 #endif
110
111 static bool_t already_a_call_with_remote_address(const LinphoneCore *lc, const LinphoneAddress *remote) {
112         ms_warning(" searching for already_a_call_with_remote_address.");
113
114         MSList *elem;
115         for(elem=lc->calls;elem!=NULL;elem=elem->next){
116                 const LinphoneCall *call=(LinphoneCall*)elem->data;
117                 const LinphoneAddress *cRemote=linphone_call_get_remote_address(call);
118                 if (linphone_address_weak_equal(cRemote,remote)) {
119                         ms_warning("already_a_call_with_remote_address found.");
120                         return TRUE;
121                 }
122         }
123         return FALSE;
124 }
125
126 static bool_t already_a_call_pending(LinphoneCore *lc){
127         MSList *elem;
128         for(elem=lc->calls;elem!=NULL;elem=elem->next){
129                 LinphoneCall *call=(LinphoneCall*)elem->data;
130                 if (call->state==LinphoneCallIncomingReceived
131                     || call->state==LinphoneCallOutgoingInit
132                     || call->state==LinphoneCallOutgoingProgress
133                     || call->state==LinphoneCallOutgoingEarlyMedia
134                     || call->state==LinphoneCallOutgoingRinging){
135                         return TRUE;
136                 }
137         }
138         return FALSE;
139 }
140
141 static void call_received(SalOp *h){
142         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
143         LinphoneCall *call;
144         const char *from,*to;
145         LinphoneAddress *from_addr, *to_addr;
146         bool_t prevent_colliding_calls=lp_config_get_int(lc->config,"sip","prevent_colliding_calls",TRUE);
147         
148         /* first check if we can answer successfully to this invite */
149         if (lc->presence_mode==LinphoneStatusBusy ||
150             lc->presence_mode==LinphoneStatusOffline ||
151             lc->presence_mode==LinphoneStatusDoNotDisturb ||
152             lc->presence_mode==LinphoneStatusMoved){
153                 if (lc->presence_mode==LinphoneStatusBusy )
154                         sal_call_decline(h,SalReasonBusy,NULL);
155                 else if (lc->presence_mode==LinphoneStatusOffline)
156                         sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
157                 else if (lc->presence_mode==LinphoneStatusDoNotDisturb)
158                         sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
159                 else if (lc->alt_contact!=NULL && lc->presence_mode==LinphoneStatusMoved)
160                         sal_call_decline(h,SalReasonRedirect,lc->alt_contact);
161                 sal_op_release(h);
162                 return;
163         }
164         if (!linphone_core_can_we_add_call(lc)){/*busy*/
165                 sal_call_decline(h,SalReasonBusy,NULL);
166                 sal_op_release(h);
167                 return;
168         }
169         from=sal_op_get_from(h);
170         to=sal_op_get_to(h);
171         from_addr=linphone_address_new(from);
172         to_addr=linphone_address_new(to);
173
174         if ((already_a_call_with_remote_address(lc,from_addr) && prevent_colliding_calls) || already_a_call_pending(lc)){
175                 ms_warning("Receiving another call while one is ringing or initiated, refusing this one with busy message.");
176                 sal_call_decline(h,SalReasonBusy,NULL);
177                 sal_op_release(h);
178                 linphone_address_destroy(from_addr);
179                 linphone_address_destroy(to_addr);
180                 return;
181         }
182         
183         call=linphone_call_new_incoming(lc,from_addr,to_addr,h);
184         sal_call_set_local_media_description(h,call->localdesc);
185         
186         /* the call is acceptable so we can now add it to our list */
187         linphone_core_add_call(lc,call);
188         linphone_call_ref(call); /*prevent the call from being destroyed while we are notifying, if the user declines within the state callback */
189
190         if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && sal_op_get_ice_session(call->op)) {
191                 /* Defer ringing until the end of the ICE candidates gathering process. */
192                 ms_message("Defer ringing to gather ICE candidates");
193                 return;
194         }
195
196         linphone_core_notify_incoming_call(lc,call);
197 }
198
199 static void call_ringing(SalOp *h){
200         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
201         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
202         SalMediaDescription *md;
203         
204         if (call==NULL) return;
205         
206         if (lc->vtable.display_status)
207                 lc->vtable.display_status(lc,_("Remote ringing."));
208         
209         md=sal_call_get_final_media_description(h);
210         if (md==NULL){
211                 if (lc->ringstream && lc->dmfs_playing_start_time!=0){
212                         ring_stop(lc->ringstream);
213                         lc->ringstream=NULL;
214                         lc->dmfs_playing_start_time=0;
215                 }
216                 if (lc->ringstream!=NULL) return;       /*already ringing !*/
217                 if (lc->sound_conf.play_sndcard!=NULL){
218                         MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
219                         if (call->localdesc->streams[0].max_rate>0) ms_snd_card_set_preferred_sample_rate(ringcard, call->localdesc->streams[0].max_rate);
220                         /*we release sound before playing ringback tone*/
221                         if (call->audiostream)
222                                 audio_stream_unprepare_sound(call->audiostream);
223                         lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,ringcard);
224                 }
225                 ms_message("Remote ringing...");
226                 if (lc->vtable.display_status) 
227                         lc->vtable.display_status(lc,_("Remote ringing..."));
228                 linphone_call_set_state(call,LinphoneCallOutgoingRinging,"Remote ringing");
229         }else{
230                 /*accept early media */
231                 if (call->audiostream && audio_stream_started(call->audiostream)){
232                         /*streams already started */
233                         ms_message("Early media already started.");
234                         return;
235                 }
236                 if (lc->vtable.show) lc->vtable.show(lc);
237                 if (lc->vtable.display_status) 
238                         lc->vtable.display_status(lc,_("Early media."));
239                 linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
240                 if (lc->ringstream!=NULL){
241                         ring_stop(lc->ringstream);
242                         lc->ringstream=NULL;
243                 }
244                 ms_message("Doing early media...");
245                 linphone_core_update_streams(lc,call,md);
246         }
247 }
248
249 /*
250  * could be reach :
251  *  - when the call is accepted
252  *  - when a request is accepted (pause, resume)
253  */
254 static void call_accepted(SalOp *op){
255         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
256         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
257         IceSession *ice_session=sal_op_get_ice_session(op);
258         SalMediaDescription *md;
259         
260         if (call==NULL){
261                 ms_warning("No call to accept.");
262                 return ;
263         }
264
265         if (ice_session == NULL) {
266                 /* Ensure the ICE check list pointers for the call streams are resetted to prevent crashes */
267                 if (call->audiostream != NULL) call->audiostream->ice_check_list = NULL;
268                 if (call->videostream != NULL) call->videostream->ice_check_list = NULL;
269         }
270
271         md=sal_call_get_final_media_description(op);
272         
273         if (call->state==LinphoneCallOutgoingProgress ||
274             call->state==LinphoneCallOutgoingRinging ||
275             call->state==LinphoneCallOutgoingEarlyMedia){
276                 linphone_call_set_state(call,LinphoneCallConnected,"Connected");
277                 if (call->referer) linphone_core_notify_refer_state(lc,call->referer,call);
278         }
279         if (md && !sal_media_description_empty(md)){
280                 if (sal_media_description_has_dir(md,SalStreamSendOnly) ||
281                     sal_media_description_has_dir(md,SalStreamInactive)){
282                         if (lc->vtable.display_status){
283                                 char *tmp=linphone_call_get_remote_address_as_string (call);
284                                 char *msg=ms_strdup_printf(_("Call with %s is paused."),tmp);
285                                 lc->vtable.display_status(lc,msg);
286                                 ms_free(tmp);
287                                 ms_free(msg);
288                         }
289                         linphone_core_update_streams (lc,call,md);
290                         linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
291                         if (call->refer_pending)
292                                 linphone_core_start_refered_call(lc,call);
293                 }else if (sal_media_description_has_dir(md,SalStreamRecvOnly)){
294                         /*we are put on hold when the call is initially accepted */
295                         if (lc->vtable.display_status){
296                                 char *tmp=linphone_call_get_remote_address_as_string (call);
297                                 char *msg=ms_strdup_printf(_("Call answered by %s - on hold."),tmp);
298                                 lc->vtable.display_status(lc,msg);
299                                 ms_free(tmp);
300                                 ms_free(msg);
301                         }
302                         linphone_core_update_streams (lc,call,md);
303                         linphone_call_set_state(call,LinphoneCallPausedByRemote,"Call paused by remote");
304                 }else{
305                         if (call->state==LinphoneCallStreamsRunning){
306                                 /*media was running before, the remote as acceted a call modification (that is
307                                         a reinvite made by us. We must notify the application this reinvite was accepted*/
308                                 linphone_call_set_state(call, LinphoneCallUpdated, "Call updated");
309                         }else{
310                                 if (call->state==LinphoneCallResuming){
311                                         if (lc->vtable.display_status){
312                                                 lc->vtable.display_status(lc,_("Call resumed."));
313                                         }
314                                 }else{
315                                         if (lc->vtable.display_status){
316                                                 char *tmp=linphone_call_get_remote_address_as_string (call);
317                                                 char *msg=ms_strdup_printf(_("Call answered by %s."),tmp);
318                                                 lc->vtable.display_status(lc,msg);
319                                                 ms_free(tmp);
320                                                 ms_free(msg);
321                                         }
322                                 }
323                         }
324                         linphone_core_update_streams (lc,call,md);
325                         if (!call->current_params.in_conference)
326                                 lc->current_call=call;
327                         linphone_call_set_state(call, LinphoneCallStreamsRunning, "Streams running");
328                 }
329         }else{
330                 /*send a bye*/
331                 ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
332                 linphone_core_abort_call(lc,call,_("Incompatible, check codecs..."));
333         }
334 }
335
336 static void call_ack(SalOp *op){
337         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
338         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
339         if (call==NULL){
340                 ms_warning("No call to be ACK'd");
341                 return ;
342         }
343         if (call->media_pending){
344                 SalMediaDescription *md=sal_call_get_final_media_description(op);
345                 if (md && !sal_media_description_empty(md)){
346                         if (call->state==LinphoneCallStreamsRunning){
347                                 /*media was running before, the remote as acceted a call modification (that is
348                                         a reinvite made by us. We must notify the application this reinvite was accepted*/
349                                 linphone_call_set_state(call, LinphoneCallUpdated, "Call updated");
350                         }
351                         linphone_core_update_streams (lc,call,md);
352                         linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
353                 }else{
354                         /*send a bye*/
355                         ms_error("Incompatible SDP response received in ACK, need to abort the call");
356                         linphone_core_abort_call(lc,call,"No codec intersection");
357                         return;
358                 }
359         }
360 }
361
362 static void call_accept_update(LinphoneCore *lc, LinphoneCall *call){
363         SalMediaDescription *md;
364         sal_call_accept(call->op);
365         md=sal_call_get_final_media_description(call->op);
366         if (md && !sal_media_description_empty(md))
367                 linphone_core_update_streams(lc,call,md);
368 }
369
370 static void call_resumed(LinphoneCore *lc, LinphoneCall *call){
371         call_accept_update(lc,call);
372         if(lc->vtable.display_status)
373                 lc->vtable.display_status(lc,_("We have been resumed."));
374         linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
375         linphone_call_set_transfer_state(call, LinphoneCallIdle);
376 }
377
378 static void call_paused_by_remote(LinphoneCore *lc, LinphoneCall *call){
379         call_accept_update(lc,call);
380         /* we are being paused */
381         if(lc->vtable.display_status)
382                 lc->vtable.display_status(lc,_("We are paused by other party."));
383         linphone_call_set_state (call,LinphoneCallPausedByRemote,"Call paused by remote");
384 }
385
386 static void call_updated_by_remote(LinphoneCore *lc, LinphoneCall *call){
387         if(lc->vtable.display_status)
388                 lc->vtable.display_status(lc,_("Call is updated by remote."));
389         call->defer_update=FALSE;
390         linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
391         if (call->defer_update==FALSE){
392                 linphone_core_accept_call_update(lc,call,NULL);
393         }
394 }
395
396 /* this callback is called when an incoming re-INVITE modifies the session*/
397 static void call_updating(SalOp *op){
398         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
399         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
400         SalMediaDescription *rmd=sal_call_get_remote_media_description(op);
401
402         if (rmd==NULL){
403                 /* case of a reINVITE without SDP */
404                 call_accept_update(lc,call);
405                 call->media_pending=TRUE;
406                 return;
407         }
408
409         switch(call->state){
410                 case LinphoneCallPausedByRemote:
411                         if (sal_media_description_has_dir(rmd,SalStreamSendRecv) || sal_media_description_has_dir(rmd,SalStreamRecvOnly)){
412                                 call_resumed(lc,call);
413                         }else call_paused_by_remote(lc,call);
414                 break;
415                 case LinphoneCallStreamsRunning:
416                 case LinphoneCallConnected:
417                         if (sal_media_description_has_dir(rmd,SalStreamSendOnly) || sal_media_description_has_dir(rmd,SalStreamInactive)){
418                                 call_paused_by_remote(lc,call);
419                         }else{
420                                 call_updated_by_remote(lc,call);
421                         }
422                 break;
423                 default:
424                         call_accept_update(lc,call);
425         }
426 }
427
428 static void call_terminated(SalOp *op, const char *from){
429         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
430         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
431
432         if (call==NULL) return;
433         
434         switch(linphone_call_get_state(call)){
435                 case LinphoneCallEnd:
436                 case LinphoneCallError:
437                         ms_warning("call_terminated: ignoring.");
438                         return;
439                 break;
440                 case LinphoneCallIncomingReceived:
441                 case LinphoneCallIncomingEarlyMedia:
442                         call->reason=LinphoneReasonNotAnswered;
443                 break;
444                 default:
445                 break;
446         }
447         ms_message("Current call terminated...");
448         //we stop the call only if we have this current call or if we are in call
449         if (lc->ringstream!=NULL && ( (ms_list_size(lc->calls)  == 1) || linphone_core_in_call(lc) )) {
450                 ring_stop(lc->ringstream);
451                 lc->ringstream=NULL;
452         }
453         linphone_call_stop_media_streams(call);
454         if (lc->vtable.show!=NULL)
455                 lc->vtable.show(lc);
456         if (lc->vtable.display_status!=NULL)
457                 lc->vtable.display_status(lc,_("Call terminated."));
458
459         linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
460 }
461
462 static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
463         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
464         char *msg486=_("User is busy.");
465         char *msg480=_("User is temporarily unavailable.");
466         /*char *retrymsg=_("%s. Retry after %i minute(s).");*/
467         char *msg600=_("User does not want to be disturbed.");
468         char *msg603=_("Call declined.");
469         const char *msg=details;
470         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
471
472         if (call==NULL){
473                 ms_warning("Call faillure reported on already cleaned call ?");
474                 return ;
475         }
476         
477         if (lc->vtable.show) lc->vtable.show(lc);
478
479         if (error==SalErrorNoResponse){
480                 msg=_("No response.");
481                 if (lc->vtable.display_status)
482                         lc->vtable.display_status(lc,msg);
483         }else if (error==SalErrorProtocol){
484                 msg=details ? details : _("Protocol error.");
485                 if (lc->vtable.display_status)
486                         lc->vtable.display_status(lc, msg);
487         }else if (error==SalErrorFailure){
488                 switch(sr){
489                         case SalReasonDeclined:
490                                 msg=msg603;
491                                 if (lc->vtable.display_status)
492                                         lc->vtable.display_status(lc,msg603);
493                         break;
494                         case SalReasonBusy:
495                                 msg=msg486;
496                                 if (lc->vtable.display_status)
497                                         lc->vtable.display_status(lc,msg486);
498                         break;
499                         case SalReasonRedirect:
500                                 msg=_("Redirected");
501                                 if (lc->vtable.display_status)
502                                         lc->vtable.display_status(lc,msg);
503                         break;
504                         case SalReasonTemporarilyUnavailable:
505                                 msg=msg480;
506                                 if (lc->vtable.display_status)
507                                         lc->vtable.display_status(lc,msg480);
508                         break;
509                         case SalReasonNotFound:
510                                 if (lc->vtable.display_status)
511                                         lc->vtable.display_status(lc,msg);
512                         break;
513                         case SalReasonDoNotDisturb:
514                                 msg=msg600;
515                                 if (lc->vtable.display_status)
516                                         lc->vtable.display_status(lc,msg600);
517                         break;
518                         case SalReasonMedia:
519                         //media_encryption_mandatory
520                                 if (call->params.media_encryption == LinphoneMediaEncryptionSRTP && 
521                                         !linphone_core_is_media_encryption_mandatory(lc)) {
522                                         int i;
523                                         ms_message("Outgoing call failed with SRTP (SAVP) enabled - retrying with AVP");
524                                         linphone_call_stop_media_streams(call);
525                                         if (call->state==LinphoneCallOutgoingInit || call->state==LinphoneCallOutgoingProgress){
526                                                 /* clear SRTP local params */
527                                                 call->params.media_encryption = LinphoneMediaEncryptionNone;
528                                                 for(i=0; i<call->localdesc->nstreams; i++) {
529                                                         call->localdesc->streams[i].proto = SalProtoRtpAvp;
530                                                         memset(call->localdesc->streams[i].crypto, 0, sizeof(call->localdesc->streams[i].crypto));
531                                                 }
532                                                 linphone_core_start_invite(lc, call, NULL);
533                                         }
534                                         return;
535                                 }
536                                 msg=_("Incompatible media parameters.");
537                                 if (lc->vtable.display_status)
538                                         lc->vtable.display_status(lc,msg);
539                         break;
540                         default:
541                                 if (lc->vtable.display_status)
542                                         lc->vtable.display_status(lc,_("Call failed."));
543                 }
544         }
545
546         if (lc->ringstream!=NULL) {
547                 ring_stop(lc->ringstream);
548                 lc->ringstream=NULL;
549         }
550         linphone_call_stop_media_streams (call);
551         if (call->referer && linphone_call_get_state(call->referer)==LinphoneCallPaused && call->referer->was_automatically_paused){
552                 /*resume to the call that send us the refer automatically*/
553                 linphone_core_resume_call(lc,call->referer);
554         }
555         if (sr == SalReasonDeclined) {
556                 call->reason=LinphoneReasonDeclined;
557                 linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
558         } else if (sr == SalReasonNotFound) {
559                 call->reason=LinphoneReasonNotFound;
560                 linphone_call_set_state(call,LinphoneCallError,"User not found.");
561         } else {
562                 linphone_call_set_state(call,LinphoneCallError,msg);
563         }
564 }
565
566 static void call_released(SalOp *op){
567         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
568         if (call!=NULL){
569                 linphone_call_set_state(call,LinphoneCallReleased,"Call released");
570         }else ms_error("call_released() for already destroyed call ?");
571 }
572
573 static void auth_requested(SalOp *h, const char *realm, const char *username){
574         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
575         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
576         LinphoneCall *call=is_a_linphone_call(sal_op_get_user_pointer(h));
577
578         if (call && call->ping_op==h){
579                 /*don't request authentication for ping requests. Their purpose is just to get any
580                  * answer to get the Via's received and rport parameters.
581                  */
582                 ms_message("auth_requested(): ignored for ping request.");
583                 return;
584         }
585         
586         ms_message("auth_requested() for realm=%s, username=%s",realm,username);
587
588         if (ai && ai->works==FALSE && ai->usecount>=3){
589                 /*case we tried 3 times to authenticate, without success */
590                 /*Better is to stop (implemeted below in else statement), and retry later*/
591                 if (ms_time(NULL)-ai->last_use_time>30){
592                         ai->usecount=0; /*so that we can allow to retry */
593                 }
594         }
595         
596         if (ai && (ai->works || ai->usecount<3)){
597                 SalAuthInfo sai;
598                 sai.username=ai->username;
599                 sai.userid=ai->userid;
600                 sai.realm=ai->realm;
601                 sai.password=ai->passwd;
602                 ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
603                 sal_op_authenticate(h,&sai);
604                 ai->usecount++;
605                 ai->last_use_time=ms_time(NULL);
606         }else{
607                 if (ai && ai->works==FALSE) {
608                         sal_op_cancel_authentication(h);
609                 } 
610                 if (lc->vtable.auth_info_requested)
611                         lc->vtable.auth_info_requested(lc,realm,username);
612         }
613 }
614
615 static void auth_success(SalOp *h, const char *realm, const char *username){
616         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
617         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
618         if (ai){
619                 ms_message("%s/%s authentication works.",realm,username);
620                 ai->works=TRUE;
621         }
622 }
623
624 static void register_success(SalOp *op, bool_t registered){
625         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
626         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
627         char *msg;
628         
629         if (cfg->deletion_date!=0){
630                 ms_message("Registration success for removed proxy config, ignored");
631                 return;
632         }
633         linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
634         linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
635                                         registered ? "Registration sucessful" : "Unregistration done");
636         if (lc->vtable.display_status){
637                 if (registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
638                 else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
639                 lc->vtable.display_status(lc,msg);
640                 ms_free(msg);
641         }
642         
643 }
644
645 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
646         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
647         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
648
649         if (cfg==NULL){
650                 ms_warning("Registration failed for unknown proxy config.");
651                 return ;
652         }
653         if (cfg->deletion_date!=0){
654                 ms_message("Registration failed for removed proxy config, ignored");
655                 return;
656         }
657         if (details==NULL)
658                 details=_("no response timeout");
659         
660         if (lc->vtable.display_status) {
661                 char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),details  );
662                 lc->vtable.display_status(lc,msg);
663                 ms_free(msg);
664         }
665         if (error== SalErrorFailure && reason == SalReasonForbidden) {
666                 linphone_proxy_config_set_error(cfg, LinphoneReasonBadCredentials);
667         } else if (error == SalErrorNoResponse) {
668                 linphone_proxy_config_set_error(cfg, LinphoneReasonNoResponse);
669         }
670         linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
671         if (error== SalErrorFailure && reason == SalReasonForbidden) {
672                 const char *realm=NULL,*username=NULL;
673                 if (sal_op_get_auth_requested(op,&realm,&username)==0){
674                         if (lc->vtable.auth_info_requested)
675                                 lc->vtable.auth_info_requested(lc,realm,username);
676                 }
677         }
678 }
679
680 static void vfu_request(SalOp *op){
681 #ifdef VIDEO_ENABLED
682         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
683         if (call==NULL){
684                 ms_warning("VFU request but no call !");
685                 return ;
686         }
687         if (call->videostream)
688                 video_stream_send_vfu(call->videostream);
689 #endif
690 }
691
692 static void dtmf_received(SalOp *op, char dtmf){
693         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
694         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
695         if (lc->vtable.dtmf_received != NULL)
696                 lc->vtable.dtmf_received(lc, call, dtmf);
697 }
698
699 static void refer_received(Sal *sal, SalOp *op, const char *referto){
700         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
701         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
702         if (call){
703                 if (call->refer_to!=NULL){
704                         ms_free(call->refer_to);
705                 }
706                 call->refer_to=ms_strdup(referto);
707                 call->refer_pending=TRUE;
708                 linphone_call_set_state(call,LinphoneCallRefered,"Refered");
709                 if (lc->vtable.display_status){
710                         char *msg=ms_strdup_printf(_("We are transferred to %s"),referto);
711                         lc->vtable.display_status(lc,msg);
712                         ms_free(msg);
713                 }
714                 if (call->state!=LinphoneCallPaused){
715                         ms_message("Automatically pausing current call to accept transfer.");
716                         linphone_core_pause_call(lc,call);
717                         call->was_automatically_paused=TRUE;
718                         /*then we will start the refered when the pause is accepted, in order to serialize transactions within the dialog.
719                          * Indeed we need to avoid to send a NOTIFY to inform about of state of the refered call while the pause isn't completed.
720                         **/
721                 }else linphone_core_start_refered_call(lc,call);
722         }else if (lc->vtable.refer_received){
723                 lc->vtable.refer_received(lc,referto);
724         }
725 }
726
727 static void text_received(Sal *sal, const char *from, const char *msg){
728         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
729         linphone_core_text_received(lc,from,msg);
730 }
731
732 static void notify(SalOp *op, const char *from, const char *msg){
733         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
734         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
735         ms_message("get a %s notify from %s",msg,from);
736         if(lc->vtable.notify_recv)
737                 lc->vtable.notify_recv(lc,call,from,msg);
738 }
739
740 static void notify_presence(SalOp *op, SalSubscribeStatus ss, SalPresenceStatus status, const char *msg){
741         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
742         linphone_notify_recv(lc,op,ss,status);
743 }
744
745 static void subscribe_received(SalOp *op, const char *from){
746         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
747         linphone_subscription_new(lc,op,from);
748 }
749
750 static void subscribe_closed(SalOp *op, const char *from){
751         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
752         linphone_subscription_closed(lc,op);
753 }
754
755 static void ping_reply(SalOp *op){
756         LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
757         ms_message("ping reply !");
758         if (call){
759                 if (call->state==LinphoneCallOutgoingInit){
760                         linphone_core_start_invite(call->core,call,NULL);
761                 }
762         }
763         else
764         {
765                 ms_warning("ping reply without call attached...");
766         }
767 }
768
769 static void notify_refer(SalOp *op, SalReferStatus status){
770         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
771         LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
772         LinphoneCallState cstate;
773         if (call==NULL) {
774                 ms_warning("Receiving notify_refer for unknown call.");
775                 return ;
776         }
777         switch(status){
778                 case SalReferTrying:
779                         cstate=LinphoneCallOutgoingProgress;
780                 break;
781                 case SalReferSuccess:
782                         cstate=LinphoneCallConnected;
783                 break;
784                 case SalReferFailed:
785                         cstate=LinphoneCallError;
786                 break;
787                 default:
788                         cstate=LinphoneCallError;
789         }
790         linphone_call_set_transfer_state(call, cstate);
791         if (cstate==LinphoneCallConnected){
792                 /*automatically terminate the call as the transfer is complete.*/
793                 linphone_core_terminate_call(lc,call);
794         }
795 }
796
797 SalCallbacks linphone_sal_callbacks={
798         call_received,
799         call_ringing,
800         call_accepted,
801         call_ack,
802         call_updating,
803         call_terminated,
804         call_failure,
805         call_released,
806         auth_requested,
807         auth_success,
808         register_success,
809         register_failure,
810         vfu_request,
811         dtmf_received,
812         refer_received,
813         text_received,
814         notify,
815         notify_presence,
816         notify_refer,
817         subscribe_received,
818         subscribe_closed,
819         ping_reply
820 };
821
822