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