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