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