]> sjero.net Git - linphone/blob - coreapi/callbacks.c
Remove useless code.
[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) || (call->videostream && call->videostream->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         
185         /* the call is acceptable so we can now add it to our list */
186         linphone_core_add_call(lc,call);
187         linphone_call_ref(call); /*prevent the call from being destroyed while we are notifying, if the user declines within the state callback */
188
189         if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && (call->ice_session != NULL)) {
190                 /* Defer ringing until the end of the ICE candidates gathering process. */
191                 ms_message("Defer ringing to gather ICE candidates");
192                 return;
193         }
194
195         linphone_core_notify_incoming_call(lc,call);
196 }
197
198 static void call_ringing(SalOp *h){
199         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
200         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
201         SalMediaDescription *md;
202         
203         if (call==NULL) return;
204         
205         if (lc->vtable.display_status)
206                 lc->vtable.display_status(lc,_("Remote ringing."));
207         
208         md=sal_call_get_final_media_description(h);
209         if (md==NULL){
210                 if (lc->ringstream && lc->dmfs_playing_start_time!=0){
211                         ring_stop(lc->ringstream);
212                         lc->ringstream=NULL;
213                         lc->dmfs_playing_start_time=0;
214                 }
215                 if (lc->ringstream!=NULL) return;       /*already ringing !*/
216                 if (lc->sound_conf.play_sndcard!=NULL){
217                         MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
218                         if (call->localdesc->streams[0].max_rate>0) ms_snd_card_set_preferred_sample_rate(ringcard, call->localdesc->streams[0].max_rate);
219                         /*we release sound before playing ringback tone*/
220                         if (call->audiostream)
221                                 audio_stream_unprepare_sound(call->audiostream);
222                         lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,ringcard);
223                 }
224                 ms_message("Remote ringing...");
225                 if (lc->vtable.display_status) 
226                         lc->vtable.display_status(lc,_("Remote ringing..."));
227                 linphone_call_set_state(call,LinphoneCallOutgoingRinging,"Remote ringing");
228         }else{
229                 /*accept early media */
230                 if (call->audiostream && audio_stream_started(call->audiostream)){
231                         /*streams already started */
232                         ms_message("Early media already started.");
233                         return;
234                 }
235                 if (lc->vtable.show) lc->vtable.show(lc);
236                 if (lc->vtable.display_status) 
237                         lc->vtable.display_status(lc,_("Early media."));
238                 linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
239                 if (lc->ringstream!=NULL){
240                         ring_stop(lc->ringstream);
241                         lc->ringstream=NULL;
242                 }
243                 ms_message("Doing early media...");
244                 linphone_core_update_streams(lc,call,md);
245         }
246 }
247
248 /*
249  * could be reach :
250  *  - when the call is accepted
251  *  - when a request is accepted (pause, resume)
252  */
253 static void call_accepted(SalOp *op){
254         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
255         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
256         SalMediaDescription *md;
257         
258         if (call==NULL){
259                 ms_warning("No call to accept.");
260                 return ;
261         }
262
263         /* Handle remote ICE attributes if any. */
264         if (call->ice_session != NULL) {
265                 linphone_core_update_ice_from_remote_media_description(call, sal_call_get_remote_media_description(op));
266         }
267
268         md=sal_call_get_final_media_description(op);
269         
270         if (call->state==LinphoneCallOutgoingProgress ||
271             call->state==LinphoneCallOutgoingRinging ||
272             call->state==LinphoneCallOutgoingEarlyMedia){
273                 linphone_call_set_state(call,LinphoneCallConnected,"Connected");
274                 if (call->referer) linphone_core_notify_refer_state(lc,call->referer,call);
275         }
276         if (md && !sal_media_description_empty(md)){
277                 if (sal_media_description_has_dir(md,SalStreamSendOnly) ||
278                     sal_media_description_has_dir(md,SalStreamInactive)){
279                         if (lc->vtable.display_status){
280                                 char *tmp=linphone_call_get_remote_address_as_string (call);
281                                 char *msg=ms_strdup_printf(_("Call with %s is paused."),tmp);
282                                 lc->vtable.display_status(lc,msg);
283                                 ms_free(tmp);
284                                 ms_free(msg);
285                         }
286                         linphone_core_update_streams (lc,call,md);
287                         linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
288                         if (call->refer_pending)
289                                 linphone_core_start_refered_call(lc,call);
290                 }else if (sal_media_description_has_dir(md,SalStreamRecvOnly)){
291                         /*we are put on hold when the call is initially accepted */
292                         if (lc->vtable.display_status){
293                                 char *tmp=linphone_call_get_remote_address_as_string (call);
294                                 char *msg=ms_strdup_printf(_("Call answered by %s - on hold."),tmp);
295                                 lc->vtable.display_status(lc,msg);
296                                 ms_free(tmp);
297                                 ms_free(msg);
298                         }
299                         linphone_core_update_streams (lc,call,md);
300                         linphone_call_set_state(call,LinphoneCallPausedByRemote,"Call paused by remote");
301                 }else{
302                         if (call->state!=LinphoneCallUpdating){
303                                 if (call->state==LinphoneCallResuming){
304                                         if (lc->vtable.display_status){
305                                                 lc->vtable.display_status(lc,_("Call resumed."));
306                                         }
307                                 }else{
308                                         if (lc->vtable.display_status){
309                                                 char *tmp=linphone_call_get_remote_address_as_string (call);
310                                                 char *msg=ms_strdup_printf(_("Call answered by %s."),tmp);
311                                                 lc->vtable.display_status(lc,msg);
312                                                 ms_free(tmp);
313                                                 ms_free(msg);
314                                         }
315                                 }
316                         }
317                         linphone_core_update_streams (lc,call,md);
318                         if (!call->current_params.in_conference)
319                                 lc->current_call=call;
320                         linphone_call_set_state(call, LinphoneCallStreamsRunning, "Streams running");
321                 }
322         }else{
323                 /*send a bye*/
324                 ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
325                 linphone_core_abort_call(lc,call,_("Incompatible, check codecs..."));
326         }
327 }
328
329 static void call_ack(SalOp *op){
330         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
331         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
332         if (call==NULL){
333                 ms_warning("No call to be ACK'd");
334                 return ;
335         }
336         if (call->media_pending){
337                 SalMediaDescription *md=sal_call_get_final_media_description(op);
338                 if (md && !sal_media_description_empty(md)){
339                         linphone_core_update_streams (lc,call,md);
340                         linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
341                 }else{
342                         /*send a bye*/
343                         ms_error("Incompatible SDP response received in ACK, need to abort the call");
344                         linphone_core_abort_call(lc,call,"No codec intersection");
345                         return;
346                 }
347         }
348 }
349
350 static void call_accept_update(LinphoneCore *lc, LinphoneCall *call){
351         SalMediaDescription *md;
352         SalMediaDescription *rmd=sal_call_get_remote_media_description(call->op);
353         if ((rmd!=NULL) && (call->ice_session!=NULL)) {
354                 linphone_core_update_ice_from_remote_media_description(call,rmd);
355                 linphone_core_update_local_media_description_from_ice(call->localdesc,call->ice_session);
356         }
357         sal_call_accept(call->op);
358         md=sal_call_get_final_media_description(call->op);
359         if (md && !sal_media_description_empty(md))
360                 linphone_core_update_streams(lc,call,md);
361 }
362
363 static void call_resumed(LinphoneCore *lc, LinphoneCall *call){
364         call_accept_update(lc,call);
365         if(lc->vtable.display_status)
366                 lc->vtable.display_status(lc,_("We have been resumed."));
367         linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
368         linphone_call_set_transfer_state(call, LinphoneCallIdle);
369 }
370
371 static void call_paused_by_remote(LinphoneCore *lc, LinphoneCall *call){
372         call_accept_update(lc,call);
373         /* we are being paused */
374         if(lc->vtable.display_status)
375                 lc->vtable.display_status(lc,_("We are paused by other party."));
376         linphone_call_set_state (call,LinphoneCallPausedByRemote,"Call paused by remote");
377 }
378
379 static void call_updated_by_remote(LinphoneCore *lc, LinphoneCall *call){
380         if(lc->vtable.display_status)
381                 lc->vtable.display_status(lc,_("Call is updated by remote."));
382         call->defer_update=FALSE;
383         linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
384         if (call->defer_update==FALSE){
385                 linphone_core_accept_call_update(lc,call,NULL);
386         }
387 }
388
389 /* this callback is called when an incoming re-INVITE modifies the session*/
390 static void call_updating(SalOp *op){
391         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
392         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
393         SalMediaDescription *rmd=sal_call_get_remote_media_description(op);
394
395         if (rmd==NULL){
396                 /* case of a reINVITE without SDP */
397                 call_accept_update(lc,call);
398                 call->media_pending=TRUE;
399                 return;
400         }
401
402         switch(call->state){
403                 case LinphoneCallPausedByRemote:
404                         if (sal_media_description_has_dir(rmd,SalStreamSendRecv) || sal_media_description_has_dir(rmd,SalStreamRecvOnly)){
405                                 call_resumed(lc,call);
406                         }else call_paused_by_remote(lc,call);
407                 break;
408                 case LinphoneCallStreamsRunning:
409                 case LinphoneCallConnected:
410                         if (sal_media_description_has_dir(rmd,SalStreamSendOnly) || sal_media_description_has_dir(rmd,SalStreamInactive)){
411                                 call_paused_by_remote(lc,call);
412                         }else{
413                                 call_updated_by_remote(lc,call);
414                         }
415                 break;
416                 default:
417                         call_accept_update(lc,call);
418         }
419 }
420
421 static void call_terminated(SalOp *op, const char *from){
422         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
423         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
424
425         if (call==NULL) return;
426         
427         switch(linphone_call_get_state(call)){
428                 case LinphoneCallEnd:
429                 case LinphoneCallError:
430                         ms_warning("call_terminated: ignoring.");
431                         return;
432                 break;
433                 case LinphoneCallIncomingReceived:
434                 case LinphoneCallIncomingEarlyMedia:
435                         call->reason=LinphoneReasonNotAnswered;
436                 break;
437                 default:
438                 break;
439         }
440         ms_message("Current call terminated...");
441         //we stop the call only if we have this current call or if we are in call
442         if (lc->ringstream!=NULL && ( (ms_list_size(lc->calls)  == 1) || linphone_core_in_call(lc) )) {
443                 ring_stop(lc->ringstream);
444                 lc->ringstream=NULL;
445         }
446         linphone_call_stop_media_streams(call);
447         if (lc->vtable.show!=NULL)
448                 lc->vtable.show(lc);
449         if (lc->vtable.display_status!=NULL)
450                 lc->vtable.display_status(lc,_("Call terminated."));
451
452         linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
453 }
454
455 static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
456         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
457         char *msg486=_("User is busy.");
458         char *msg480=_("User is temporarily unavailable.");
459         /*char *retrymsg=_("%s. Retry after %i minute(s).");*/
460         char *msg600=_("User does not want to be disturbed.");
461         char *msg603=_("Call declined.");
462         const char *msg=details;
463         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
464
465         if (call==NULL){
466                 ms_warning("Call faillure reported on already cleaned call ?");
467                 return ;
468         }
469         
470         if (lc->vtable.show) lc->vtable.show(lc);
471
472         if (error==SalErrorNoResponse){
473                 msg=_("No response.");
474                 if (lc->vtable.display_status)
475                         lc->vtable.display_status(lc,msg);
476         }else if (error==SalErrorProtocol){
477                 msg=details ? details : _("Protocol error.");
478                 if (lc->vtable.display_status)
479                         lc->vtable.display_status(lc, msg);
480         }else if (error==SalErrorFailure){
481                 switch(sr){
482                         case SalReasonDeclined:
483                                 msg=msg603;
484                                 if (lc->vtable.display_status)
485                                         lc->vtable.display_status(lc,msg603);
486                         break;
487                         case SalReasonBusy:
488                                 msg=msg486;
489                                 if (lc->vtable.display_status)
490                                         lc->vtable.display_status(lc,msg486);
491                         break;
492                         case SalReasonRedirect:
493                                 msg=_("Redirected");
494                                 if (lc->vtable.display_status)
495                                         lc->vtable.display_status(lc,msg);
496                         break;
497                         case SalReasonTemporarilyUnavailable:
498                                 msg=msg480;
499                                 if (lc->vtable.display_status)
500                                         lc->vtable.display_status(lc,msg480);
501                         break;
502                         case SalReasonNotFound:
503                                 if (lc->vtable.display_status)
504                                         lc->vtable.display_status(lc,msg);
505                         break;
506                         case SalReasonDoNotDisturb:
507                                 msg=msg600;
508                                 if (lc->vtable.display_status)
509                                         lc->vtable.display_status(lc,msg600);
510                         break;
511                         case SalReasonMedia:
512                         //media_encryption_mandatory
513                                 if (call->params.media_encryption == LinphoneMediaEncryptionSRTP && 
514                                         !linphone_core_is_media_encryption_mandatory(lc)) {
515                                         int i;
516                                         ms_message("Outgoing call failed with SRTP (SAVP) enabled - retrying with AVP");
517                                         linphone_call_stop_media_streams(call);
518                                         if (call->state==LinphoneCallOutgoingInit || call->state==LinphoneCallOutgoingProgress){
519                                                 /* clear SRTP local params */
520                                                 call->params.media_encryption = LinphoneMediaEncryptionNone;
521                                                 for(i=0; i<call->localdesc->nstreams; i++) {
522                                                         call->localdesc->streams[i].proto = SalProtoRtpAvp;
523                                                         memset(call->localdesc->streams[i].crypto, 0, sizeof(call->localdesc->streams[i].crypto));
524                                                 }
525                                                 linphone_core_start_invite(lc, call);
526                                         }
527                                         return;
528                                 }
529                                 msg=_("Incompatible media parameters.");
530                                 if (lc->vtable.display_status)
531                                         lc->vtable.display_status(lc,msg);
532                         break;
533                         default:
534                                 if (lc->vtable.display_status)
535                                         lc->vtable.display_status(lc,_("Call failed."));
536                 }
537         }
538
539         if (lc->ringstream!=NULL) {
540                 ring_stop(lc->ringstream);
541                 lc->ringstream=NULL;
542         }
543         linphone_call_stop_media_streams (call);
544         if (call->referer && linphone_call_get_state(call->referer)==LinphoneCallPaused && call->referer->was_automatically_paused){
545                 /*resume to the call that send us the refer automatically*/
546                 linphone_core_resume_call(lc,call->referer);
547         }
548         if (sr == SalReasonDeclined) {
549                 call->reason=LinphoneReasonDeclined;
550                 linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
551         } else if (sr == SalReasonNotFound) {
552                 call->reason=LinphoneReasonNotFound;
553                 linphone_call_set_state(call,LinphoneCallError,"User not found.");
554         } else {
555                 linphone_call_set_state(call,LinphoneCallError,msg);
556         }
557 }
558
559 static void call_released(SalOp *op){
560         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
561         if (call!=NULL){
562                 linphone_call_set_state(call,LinphoneCallReleased,"Call released");
563         }else ms_error("call_released() for already destroyed call ?");
564 }
565
566 static void auth_requested(SalOp *h, const char *realm, const char *username){
567         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
568         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
569         LinphoneCall *call=is_a_linphone_call(sal_op_get_user_pointer(h));
570
571         if (call && call->ping_op==h){
572                 /*don't request authentication for ping requests. Their purpose is just to get any
573                  * answer to get the Via's received and rport parameters.
574                  */
575                 ms_message("auth_requested(): ignored for ping request.");
576                 return;
577         }
578         
579         ms_message("auth_requested() for realm=%s, username=%s",realm,username);
580
581         if (ai && ai->works==FALSE && ai->usecount>=3){
582                 /*case we tried 3 times to authenticate, without success */
583                 /*Better is to stop (implemeted below in else statement), and retry later*/
584                 if (ms_time(NULL)-ai->last_use_time>30){
585                         ai->usecount=0; /*so that we can allow to retry */
586                 }
587         }
588         
589         if (ai && (ai->works || ai->usecount<3)){
590                 SalAuthInfo sai;
591                 sai.username=ai->username;
592                 sai.userid=ai->userid;
593                 sai.realm=ai->realm;
594                 sai.password=ai->passwd;
595                 ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
596                 sal_op_authenticate(h,&sai);
597                 ai->usecount++;
598                 ai->last_use_time=ms_time(NULL);
599         }else{
600                 if (ai && ai->works==FALSE) {
601                         sal_op_cancel_authentication(h);
602                 } 
603                 if (lc->vtable.auth_info_requested)
604                         lc->vtable.auth_info_requested(lc,realm,username);
605         }
606 }
607
608 static void auth_success(SalOp *h, const char *realm, const char *username){
609         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
610         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
611         if (ai){
612                 ms_message("%s/%s authentication works.",realm,username);
613                 ai->works=TRUE;
614         }
615 }
616
617 static void register_success(SalOp *op, bool_t registered){
618         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
619         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
620         char *msg;
621         
622         if (cfg->deletion_date!=0){
623                 ms_message("Registration success for removed proxy config, ignored");
624                 return;
625         }
626         linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
627         linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
628                                         registered ? "Registration sucessful" : "Unregistration done");
629         if (lc->vtable.display_status){
630                 if (registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
631                 else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
632                 lc->vtable.display_status(lc,msg);
633                 ms_free(msg);
634         }
635         
636 }
637
638 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
639         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
640         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
641
642         if (cfg==NULL){
643                 ms_warning("Registration failed for unknown proxy config.");
644                 return ;
645         }
646         if (cfg->deletion_date!=0){
647                 ms_message("Registration failed for removed proxy config, ignored");
648                 return;
649         }
650         if (details==NULL)
651                 details=_("no response timeout");
652         
653         if (lc->vtable.display_status) {
654                 char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),details  );
655                 lc->vtable.display_status(lc,msg);
656                 ms_free(msg);
657         }
658         if (error== SalErrorFailure && reason == SalReasonForbidden) {
659                 linphone_proxy_config_set_error(cfg, LinphoneReasonBadCredentials);
660         } else if (error == SalErrorNoResponse) {
661                 linphone_proxy_config_set_error(cfg, LinphoneReasonNoResponse);
662         }
663         linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
664         if (error== SalErrorFailure && reason == SalReasonForbidden) {
665                 const char *realm=NULL,*username=NULL;
666                 if (sal_op_get_auth_requested(op,&realm,&username)==0){
667                         if (lc->vtable.auth_info_requested)
668                                 lc->vtable.auth_info_requested(lc,realm,username);
669                 }
670         }
671 }
672
673 static void vfu_request(SalOp *op){
674 #ifdef VIDEO_ENABLED
675         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
676         if (call==NULL){
677                 ms_warning("VFU request but no call !");
678                 return ;
679         }
680         if (call->videostream)
681                 video_stream_send_vfu(call->videostream);
682 #endif
683 }
684
685 static void dtmf_received(SalOp *op, char dtmf){
686         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
687         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
688         if (lc->vtable.dtmf_received != NULL)
689                 lc->vtable.dtmf_received(lc, call, dtmf);
690 }
691
692 static void refer_received(Sal *sal, SalOp *op, const char *referto){
693         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
694         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
695         if (call){
696                 if (call->refer_to!=NULL){
697                         ms_free(call->refer_to);
698                 }
699                 call->refer_to=ms_strdup(referto);
700                 call->refer_pending=TRUE;
701                 linphone_call_set_state(call,LinphoneCallRefered,"Refered");
702                 if (lc->vtable.display_status){
703                         char *msg=ms_strdup_printf(_("We are transferred to %s"),referto);
704                         lc->vtable.display_status(lc,msg);
705                         ms_free(msg);
706                 }
707                 if (call->state!=LinphoneCallPaused){
708                         ms_message("Automatically pausing current call to accept transfer.");
709                         linphone_core_pause_call(lc,call);
710                         call->was_automatically_paused=TRUE;
711                         /*then we will start the refered when the pause is accepted, in order to serialize transactions within the dialog.
712                          * Indeed we need to avoid to send a NOTIFY to inform about of state of the refered call while the pause isn't completed.
713                         **/
714                 }else linphone_core_start_refered_call(lc,call);
715         }else if (lc->vtable.refer_received){
716                 lc->vtable.refer_received(lc,referto);
717         }
718 }
719
720 static void text_received(Sal *sal, const char *from, const char *msg){
721         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
722         linphone_core_message_received(lc,from,msg,NULL);
723 }
724 void message_external_body_received(Sal *sal, const char *from, const char *url) {
725         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
726         linphone_core_message_received(lc,from,NULL,url);
727 }
728 static void notify(SalOp *op, const char *from, const char *msg){
729         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
730         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
731         ms_message("get a %s notify from %s",msg,from);
732         if(lc->vtable.notify_recv)
733                 lc->vtable.notify_recv(lc,call,from,msg);
734 }
735
736 static void notify_presence(SalOp *op, SalSubscribeStatus ss, SalPresenceStatus status, const char *msg){
737         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
738         linphone_notify_recv(lc,op,ss,status);
739 }
740
741 static void subscribe_received(SalOp *op, const char *from){
742         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
743         linphone_subscription_new(lc,op,from);
744 }
745
746 static void subscribe_closed(SalOp *op, const char *from){
747         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
748         linphone_subscription_closed(lc,op);
749 }
750
751 static void ping_reply(SalOp *op){
752         LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
753         ms_message("ping reply !");
754         if (call){
755                 if (call->state==LinphoneCallOutgoingInit){
756                         call->ping_replied=TRUE;
757                         linphone_core_proceed_with_invite_if_ready(call->core,call,NULL);
758                 }
759         }
760         else
761         {
762                 ms_warning("ping reply without call attached...");
763         }
764 }
765
766 static void notify_refer(SalOp *op, SalReferStatus status){
767         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
768         LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
769         LinphoneCallState cstate;
770         if (call==NULL) {
771                 ms_warning("Receiving notify_refer for unknown call.");
772                 return ;
773         }
774         switch(status){
775                 case SalReferTrying:
776                         cstate=LinphoneCallOutgoingProgress;
777                 break;
778                 case SalReferSuccess:
779                         cstate=LinphoneCallConnected;
780                 break;
781                 case SalReferFailed:
782                         cstate=LinphoneCallError;
783                 break;
784                 default:
785                         cstate=LinphoneCallError;
786         }
787         linphone_call_set_transfer_state(call, cstate);
788         if (cstate==LinphoneCallConnected){
789                 /*automatically terminate the call as the transfer is complete.*/
790                 linphone_core_terminate_call(lc,call);
791         }
792 }
793
794 static LinphoneChatMessageState chatStatusSal2Linphone(SalTextDeliveryStatus status){
795         switch(status){
796                 case SalTextDeliveryInProgress:
797                         return LinphoneChatMessageStateInProgress;
798                 case SalTextDeliveryDone:
799                         return LinphoneChatMessageStateDelivered;
800                 case SalTextDeliveryFailed:
801                         return LinphoneChatMessageStateNotDelivered;
802         }
803         return LinphoneChatMessageStateIdle;
804 }
805
806 static void text_delivery_update(SalOp *op, SalTextDeliveryStatus status){
807         LinphoneChatMessage *chat_msg=(LinphoneChatMessage* )sal_op_get_user_pointer(op);
808         if (chat_msg && chat_msg->cb) {
809                 chat_msg->cb(chat_msg
810                         ,chatStatusSal2Linphone(status)
811                         ,chat_msg->cb_ud);
812         }
813         linphone_chat_message_destroy(chat_msg);
814 }
815
816 SalCallbacks linphone_sal_callbacks={
817         call_received,
818         call_ringing,
819         call_accepted,
820         call_ack,
821         call_updating,
822         call_terminated,
823         call_failure,
824         call_released,
825         auth_requested,
826         auth_success,
827         register_success,
828         register_failure,
829         vfu_request,
830         dtmf_received,
831         refer_received,
832         text_received,
833         message_external_body_received,
834         text_delivery_update,
835         notify,
836         notify_presence,
837         notify_refer,
838         subscribe_received,
839         subscribe_closed,
840         ping_reply,
841 };
842
843