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