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