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