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