]> sjero.net Git - linphone/blob - coreapi/callbacks.c
Merge branch 'dev_videoios' into local_videoios
[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         
399         md=sal_call_get_final_media_description(op);
400
401         /*accept the modification (sends a 200Ok)*/
402         sal_call_accept(op);
403         
404         if (md && !sal_media_description_empty(md))
405         {
406                 linphone_core_update_streams (lc,call,md);
407
408                 if (sal_media_description_has_dir(call->localdesc,SalStreamSendRecv)){
409                         ms_message("Our local status is SalStreamSendRecv");
410                         if (sal_media_description_has_dir (md,SalStreamRecvOnly) || sal_media_description_has_dir(md,SalStreamInactive)){
411                                 /* we are being paused */
412                                 if(lc->vtable.display_status)
413                                         lc->vtable.display_status(lc,_("We are being paused..."));
414                                 linphone_call_set_state (call,LinphoneCallPausedByRemote,"Call paused by remote");
415                         }else if (!sal_media_description_has_dir(call->resultdesc,SalStreamSendRecv) && sal_media_description_has_dir(md,SalStreamSendRecv)){
416                                 if(lc->vtable.display_status)
417                                         lc->vtable.display_status(lc,_("We have been resumed..."));
418                                 linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
419                                 if (!call->current_params.in_conference)
420                                         lc->current_call=call;
421                         }else{
422                                 prevstate=call->state;
423                                 if(lc->vtable.display_status)
424                                         lc->vtable.display_status(lc,_("Call has been updated by remote..."));
425                                 linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
426                         }
427                 }
428
429                 if (prevstate!=LinphoneCallIdle){
430                         linphone_call_set_state (call,prevstate,"Connected (streams running)");
431                 }
432         }
433 }
434
435 static void call_terminated(SalOp *op, const char *from){
436         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
437         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
438
439         if (call==NULL) return;
440         
441         if (linphone_call_get_state(call)==LinphoneCallEnd || linphone_call_get_state(call)==LinphoneCallError){
442                 ms_warning("call_terminated: ignoring.");
443                 return;
444         }
445         ms_message("Current call terminated...");
446         //we stop the call only if we have this current call or if we are in call
447         if (lc->ringstream!=NULL && ( (ms_list_size(lc->calls)  == 1) || linphone_core_in_call(lc) )) {
448                 ring_stop(lc->ringstream);
449                 lc->ringstream=NULL;
450         }
451         linphone_call_stop_media_streams(call);
452         if (lc->vtable.show!=NULL)
453                 lc->vtable.show(lc);
454         if (lc->vtable.display_status!=NULL)
455                 lc->vtable.display_status(lc,_("Call terminated."));
456
457         linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
458 }
459
460 static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
461         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
462         char *msg486=_("User is busy.");
463         char *msg480=_("User is temporarily unavailable.");
464         /*char *retrymsg=_("%s. Retry after %i minute(s).");*/
465         char *msg600=_("User does not want to be disturbed.");
466         char *msg603=_("Call declined.");
467         const char *msg=details;
468         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
469
470         if (call==NULL){
471                 ms_warning("Call faillure reported on already cleaned call ?");
472                 return ;
473         }
474         
475         if (lc->vtable.show) lc->vtable.show(lc);
476
477         if (error==SalErrorNoResponse){
478                 msg=_("No response.");
479                 if (lc->vtable.display_status)
480                         lc->vtable.display_status(lc,msg);
481         }else if (error==SalErrorProtocol){
482                 msg=details ? details : _("Protocol error.");
483                 if (lc->vtable.display_status)
484                         lc->vtable.display_status(lc, msg);
485         }else if (error==SalErrorFailure){
486                 switch(sr){
487                         case SalReasonDeclined:
488                                 msg=msg603;
489                                 if (lc->vtable.display_status)
490                                         lc->vtable.display_status(lc,msg603);
491                         break;
492                         case SalReasonBusy:
493                                 msg=msg486;
494                                 if (lc->vtable.display_status)
495                                         lc->vtable.display_status(lc,msg486);
496                         break;
497                         case SalReasonRedirect:
498                                 msg=_("Redirected");
499                                 if (lc->vtable.display_status)
500                                         lc->vtable.display_status(lc,msg);
501                         break;
502                         case SalReasonTemporarilyUnavailable:
503                                 msg=msg480;
504                                 if (lc->vtable.display_status)
505                                         lc->vtable.display_status(lc,msg480);
506                         break;
507                         case SalReasonNotFound:
508                                 msg=_("Not found");
509                                 if (lc->vtable.display_status)
510                                         lc->vtable.display_status(lc,msg);
511                         break;
512                         case SalReasonDoNotDisturb:
513                                 msg=msg600;
514                                 if (lc->vtable.display_status)
515                                         lc->vtable.display_status(lc,msg600);
516                         break;
517                         case SalReasonMedia:
518                                 msg=_("No common codecs");
519                                 if (lc->vtable.display_status)
520                                         lc->vtable.display_status(lc,msg);
521                         break;
522                         default:
523                                 if (lc->vtable.display_status)
524                                         lc->vtable.display_status(lc,_("Call failed."));
525                 }
526         }
527
528         if (lc->ringstream!=NULL) {
529                 ring_stop(lc->ringstream);
530                 lc->ringstream=NULL;
531         }
532         linphone_call_stop_media_streams (call);
533         if (sr!=SalReasonDeclined) linphone_call_set_state(call,LinphoneCallError,msg);
534         else{
535                 call->reason=LinphoneReasonDeclined;
536                 linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
537         }
538 }
539
540 static void call_released(SalOp *op){
541         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
542         if (call!=NULL){
543                 linphone_call_set_state(call,LinphoneCallReleased,"Call released");
544         }else ms_error("call_released() for already destroyed call ?");
545 }
546
547 static void auth_requested(SalOp *h, const char *realm, const char *username){
548         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
549         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
550         ms_message("auth_requested() for realm=%s, username=%s",realm,username);
551
552         if (ai && ai->works==FALSE && ai->usecount>=3){
553                 /*case we tried 3 times to authenticate, without success */
554                 /*Better is to stop (implemeted below in else statement), and retry later*/
555                 if (ms_time(NULL)-ai->last_use_time>30){
556                         ai->usecount=0; /*so that we can allow to retry */
557                 }
558         }
559         
560         if (ai && (ai->works || ai->usecount<3)){
561                 SalAuthInfo sai;
562                 sai.username=ai->username;
563                 sai.userid=ai->userid;
564                 sai.realm=ai->realm;
565                 sai.password=ai->passwd;
566                 ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
567                 sal_op_authenticate(h,&sai);
568                 ai->usecount++;
569                 ai->last_use_time=ms_time(NULL);
570         }else{
571                 if (ai && ai->works==FALSE) {
572                         sal_op_cancel_authentication(h);
573                 } 
574                 if (lc->vtable.auth_info_requested)
575                         lc->vtable.auth_info_requested(lc,realm,username);
576         }
577 }
578
579 static void auth_success(SalOp *h, const char *realm, const char *username){
580         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
581         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
582         if (ai){
583                 ms_message("%s/%s authentication works.",realm,username);
584                 ai->works=TRUE;
585         }
586 }
587
588 static void register_success(SalOp *op, bool_t registered){
589         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
590         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
591         char *msg;
592         
593         cfg->registered=registered;
594         linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
595         linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
596                                         registered ? "Registration sucessful" : "Unregistration done");
597         if (lc->vtable.display_status){
598                 if (cfg->registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
599                 else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
600                 lc->vtable.display_status(lc,msg);
601                 ms_free(msg);
602         }
603         
604 }
605
606 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
607         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
608         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
609
610         if (cfg==NULL){
611                 ms_warning("Registration failed for unknown proxy config.");
612                 return ;
613         }
614         if (details==NULL)
615                 details=_("no response timeout");
616         
617         if (lc->vtable.display_status) {
618                 char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),details  );
619                 lc->vtable.display_status(lc,msg);
620                 ms_free(msg);
621         }
622         if (error== SalErrorFailure && reason == SalReasonForbidden) {
623                 linphone_proxy_config_set_error(cfg, LinphoneReasonBadCredentials);
624         } else if (error == SalErrorNoResponse) {
625                 linphone_proxy_config_set_error(cfg, LinphoneReasonNoResponse);
626         }
627         linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
628         if (error== SalErrorFailure && reason == SalReasonForbidden) {
629                 const char *realm=NULL,*username=NULL;
630                 if (sal_op_get_auth_requested(op,&realm,&username)==0){
631                         if (lc->vtable.auth_info_requested)
632                                 lc->vtable.auth_info_requested(lc,realm,username);
633                 }
634         }
635 }
636
637 static void vfu_request(SalOp *op){
638 #ifdef VIDEO_ENABLED
639         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
640         if (call==NULL){
641                 ms_warning("VFU request but no call !");
642                 return ;
643         }
644         if (call->videostream)
645                 video_stream_send_vfu(call->videostream);
646 #endif
647 }
648
649 static void dtmf_received(SalOp *op, char dtmf){
650         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
651         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
652         if (lc->vtable.dtmf_received != NULL)
653                 lc->vtable.dtmf_received(lc, call, dtmf);
654 }
655
656 static void refer_received(Sal *sal, SalOp *op, const char *referto){
657         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
658         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
659         if (call){
660                 if (call->refer_to!=NULL){
661                         ms_free(call->refer_to);
662                 }
663                 call->refer_to=ms_strdup(referto);
664                 call->refer_pending=TRUE;
665                 linphone_call_set_state(call,LinphoneCallRefered,"Refered");
666                 if (lc->vtable.display_status){
667                         char *msg=ms_strdup_printf(_("We are transferred to %s"),referto);
668                         lc->vtable.display_status(lc,msg);
669                         ms_free(msg);
670                 }
671                 if (call->state!=LinphoneCallPaused){
672                         ms_message("Automatically pausing current call to accept transfer.");
673                         linphone_core_pause_call(lc,call);
674                 }
675                 linphone_core_start_refered_call(lc,call);
676                 sal_call_accept_refer(op);
677         }else if (lc->vtable.refer_received){
678                 lc->vtable.refer_received(lc,referto);
679                 sal_call_accept_refer(op);
680         }
681 }
682
683 static void text_received(Sal *sal, const char *from, const char *msg){
684         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
685         linphone_core_text_received(lc,from,msg);
686 }
687
688 static void notify(SalOp *op, const char *from, const char *msg){
689         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
690         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
691         ms_message("get a %s notify from %s",msg,from);
692         if(lc->vtable.notify_recv)
693                 lc->vtable.notify_recv(lc,call,from,msg);
694 }
695
696 static void notify_presence(SalOp *op, SalSubscribeState ss, SalPresenceStatus status, const char *msg){
697         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
698         linphone_notify_recv(lc,op,ss,status);
699 }
700
701 static void subscribe_received(SalOp *op, const char *from){
702         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
703         linphone_subscription_new(lc,op,from);
704 }
705
706 static void subscribe_closed(SalOp *op, const char *from){
707         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
708         linphone_subscription_closed(lc,op);
709 }
710
711 static void ping_reply(SalOp *op){
712         LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
713         ms_message("ping reply !");
714         if (call){
715                 if (call->state==LinphoneCallOutgoingInit){
716                         linphone_core_start_invite(call->core,call,NULL);
717                 }
718         }
719         else
720         {
721                 ms_warning("ping reply without call attached...");
722         }
723 }
724
725 SalCallbacks linphone_sal_callbacks={
726         call_received,
727         call_ringing,
728         call_accepted,
729         call_ack,
730         call_updating,
731         call_terminated,
732         call_failure,
733         call_released,
734         auth_requested,
735         auth_success,
736         register_success,
737         register_failure,
738         vfu_request,
739         dtmf_received,
740         refer_received,
741         text_received,
742         notify,
743         notify_presence,
744         subscribe_received,
745         subscribe_closed,
746         ping_reply
747 };
748
749