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