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