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