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