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