]> sjero.net Git - linphone/blob - coreapi/callbacks.c
Can now use ICE and ping OPTIONS in parallel.
[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         LinphoneCall *call;
144         const char *from,*to;
145         LinphoneAddress *from_addr, *to_addr;
146         bool_t prevent_colliding_calls=lp_config_get_int(lc->config,"sip","prevent_colliding_calls",TRUE);
147         
148         /* first check if we can answer successfully to this invite */
149         if (lc->presence_mode==LinphoneStatusBusy ||
150             lc->presence_mode==LinphoneStatusOffline ||
151             lc->presence_mode==LinphoneStatusDoNotDisturb ||
152             lc->presence_mode==LinphoneStatusMoved){
153                 if (lc->presence_mode==LinphoneStatusBusy )
154                         sal_call_decline(h,SalReasonBusy,NULL);
155                 else if (lc->presence_mode==LinphoneStatusOffline)
156                         sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
157                 else if (lc->presence_mode==LinphoneStatusDoNotDisturb)
158                         sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
159                 else if (lc->alt_contact!=NULL && lc->presence_mode==LinphoneStatusMoved)
160                         sal_call_decline(h,SalReasonRedirect,lc->alt_contact);
161                 sal_op_release(h);
162                 return;
163         }
164         if (!linphone_core_can_we_add_call(lc)){/*busy*/
165                 sal_call_decline(h,SalReasonBusy,NULL);
166                 sal_op_release(h);
167                 return;
168         }
169         from=sal_op_get_from(h);
170         to=sal_op_get_to(h);
171         from_addr=linphone_address_new(from);
172         to_addr=linphone_address_new(to);
173
174         if ((already_a_call_with_remote_address(lc,from_addr) && prevent_colliding_calls) || already_a_call_pending(lc)){
175                 ms_warning("Receiving another call while one is ringing or initiated, refusing this one with busy message.");
176                 sal_call_decline(h,SalReasonBusy,NULL);
177                 sal_op_release(h);
178                 linphone_address_destroy(from_addr);
179                 linphone_address_destroy(to_addr);
180                 return;
181         }
182         
183         call=linphone_call_new_incoming(lc,from_addr,to_addr,h);
184         sal_call_set_local_media_description(h,call->localdesc);
185         
186         /* the call is acceptable so we can now add it to our list */
187         linphone_core_add_call(lc,call);
188         linphone_call_ref(call); /*prevent the call from being destroyed while we are notifying, if the user declines within the state callback */
189
190         if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && (call->ice_session != NULL)) {
191                 /* Defer ringing until the end of the ICE candidates gathering process. */
192                 ms_message("Defer ringing to gather ICE candidates");
193                 return;
194         }
195
196         linphone_core_notify_incoming_call(lc,call);
197 }
198
199 static void call_ringing(SalOp *h){
200         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
201         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
202         SalMediaDescription *md;
203         
204         if (call==NULL) return;
205         
206         if (lc->vtable.display_status)
207                 lc->vtable.display_status(lc,_("Remote ringing."));
208         
209         md=sal_call_get_final_media_description(h);
210         if (md==NULL){
211                 if (lc->ringstream && lc->dmfs_playing_start_time!=0){
212                         ring_stop(lc->ringstream);
213                         lc->ringstream=NULL;
214                         lc->dmfs_playing_start_time=0;
215                 }
216                 if (lc->ringstream!=NULL) return;       /*already ringing !*/
217                 if (lc->sound_conf.play_sndcard!=NULL){
218                         MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
219                         if (call->localdesc->streams[0].max_rate>0) ms_snd_card_set_preferred_sample_rate(ringcard, call->localdesc->streams[0].max_rate);
220                         /*we release sound before playing ringback tone*/
221                         if (call->audiostream)
222                                 audio_stream_unprepare_sound(call->audiostream);
223                         lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,ringcard);
224                 }
225                 ms_message("Remote ringing...");
226                 if (lc->vtable.display_status) 
227                         lc->vtable.display_status(lc,_("Remote ringing..."));
228                 linphone_call_set_state(call,LinphoneCallOutgoingRinging,"Remote ringing");
229         }else{
230                 /*accept early media */
231                 if (call->audiostream && audio_stream_started(call->audiostream)){
232                         /*streams already started */
233                         ms_message("Early media already started.");
234                         return;
235                 }
236                 if (lc->vtable.show) lc->vtable.show(lc);
237                 if (lc->vtable.display_status) 
238                         lc->vtable.display_status(lc,_("Early media."));
239                 linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
240                 if (lc->ringstream!=NULL){
241                         ring_stop(lc->ringstream);
242                         lc->ringstream=NULL;
243                 }
244                 ms_message("Doing early media...");
245                 linphone_core_update_streams(lc,call,md);
246         }
247 }
248
249 /*
250  * could be reach :
251  *  - when the call is accepted
252  *  - when a request is accepted (pause, resume)
253  */
254 static void call_accepted(SalOp *op){
255         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
256         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
257         SalMediaDescription *md;
258         
259         if (call==NULL){
260                 ms_warning("No call to accept.");
261                 return ;
262         }
263
264         /* Handle remote ICE attributes if any. */
265         if (call->ice_session != NULL) {
266                 linphone_core_update_ice_from_remote_media_description(call, sal_call_get_remote_media_description(op));
267         }
268
269         md=sal_call_get_final_media_description(op);
270         
271         if (call->state==LinphoneCallOutgoingProgress ||
272             call->state==LinphoneCallOutgoingRinging ||
273             call->state==LinphoneCallOutgoingEarlyMedia){
274                 linphone_call_set_state(call,LinphoneCallConnected,"Connected");
275                 if (call->referer) linphone_core_notify_refer_state(lc,call->referer,call);
276         }
277         if (md && !sal_media_description_empty(md)){
278                 if (sal_media_description_has_dir(md,SalStreamSendOnly) ||
279                     sal_media_description_has_dir(md,SalStreamInactive)){
280                         if (lc->vtable.display_status){
281                                 char *tmp=linphone_call_get_remote_address_as_string (call);
282                                 char *msg=ms_strdup_printf(_("Call with %s is paused."),tmp);
283                                 lc->vtable.display_status(lc,msg);
284                                 ms_free(tmp);
285                                 ms_free(msg);
286                         }
287                         linphone_core_update_streams (lc,call,md);
288                         linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
289                         if (call->refer_pending)
290                                 linphone_core_start_refered_call(lc,call);
291                 }else if (sal_media_description_has_dir(md,SalStreamRecvOnly)){
292                         /*we are put on hold when the call is initially accepted */
293                         if (lc->vtable.display_status){
294                                 char *tmp=linphone_call_get_remote_address_as_string (call);
295                                 char *msg=ms_strdup_printf(_("Call answered by %s - on hold."),tmp);
296                                 lc->vtable.display_status(lc,msg);
297                                 ms_free(tmp);
298                                 ms_free(msg);
299                         }
300                         linphone_core_update_streams (lc,call,md);
301                         linphone_call_set_state(call,LinphoneCallPausedByRemote,"Call paused by remote");
302                 }else{
303                         if (call->state==LinphoneCallStreamsRunning){
304                                 /*media was running before, the remote as acceted a call modification (that is
305                                         a reinvite made by us. We must notify the application this reinvite was accepted*/
306                                 linphone_call_set_state(call, LinphoneCallUpdated, "Call updated");
307                         }else{
308                                 if (call->state==LinphoneCallResuming){
309                                         if (lc->vtable.display_status){
310                                                 lc->vtable.display_status(lc,_("Call resumed."));
311                                         }
312                                 }else{
313                                         if (lc->vtable.display_status){
314                                                 char *tmp=linphone_call_get_remote_address_as_string (call);
315                                                 char *msg=ms_strdup_printf(_("Call answered by %s."),tmp);
316                                                 lc->vtable.display_status(lc,msg);
317                                                 ms_free(tmp);
318                                                 ms_free(msg);
319                                         }
320                                 }
321                         }
322                         linphone_core_update_streams (lc,call,md);
323                         if (!call->current_params.in_conference)
324                                 lc->current_call=call;
325                         linphone_call_set_state(call, LinphoneCallStreamsRunning, "Streams running");
326                 }
327         }else{
328                 /*send a bye*/
329                 ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
330                 linphone_core_abort_call(lc,call,_("Incompatible, check codecs..."));
331         }
332 }
333
334 static void call_ack(SalOp *op){
335         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
336         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
337         if (call==NULL){
338                 ms_warning("No call to be ACK'd");
339                 return ;
340         }
341         if (call->media_pending){
342                 SalMediaDescription *md=sal_call_get_final_media_description(op);
343                 if (md && !sal_media_description_empty(md)){
344                         if (call->state==LinphoneCallStreamsRunning){
345                                 /*media was running before, the remote as acceted a call modification (that is
346                                         a reinvite made by us. We must notify the application this reinvite was accepted*/
347                                 linphone_call_set_state(call, LinphoneCallUpdated, "Call updated");
348                         }
349                         linphone_core_update_streams (lc,call,md);
350                         linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
351                 }else{
352                         /*send a bye*/
353                         ms_error("Incompatible SDP response received in ACK, need to abort the call");
354                         linphone_core_abort_call(lc,call,"No codec intersection");
355                         return;
356                 }
357         }
358 }
359
360 static void call_accept_update(LinphoneCore *lc, LinphoneCall *call){
361         SalMediaDescription *md;
362         sal_call_accept(call->op);
363         md=sal_call_get_final_media_description(call->op);
364         if (md && !sal_media_description_empty(md))
365                 linphone_core_update_streams(lc,call,md);
366 }
367
368 static void call_resumed(LinphoneCore *lc, LinphoneCall *call){
369         call_accept_update(lc,call);
370         if(lc->vtable.display_status)
371                 lc->vtable.display_status(lc,_("We have been resumed."));
372         linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
373         linphone_call_set_transfer_state(call, LinphoneCallIdle);
374 }
375
376 static void call_paused_by_remote(LinphoneCore *lc, LinphoneCall *call){
377         call_accept_update(lc,call);
378         /* we are being paused */
379         if(lc->vtable.display_status)
380                 lc->vtable.display_status(lc,_("We are paused by other party."));
381         linphone_call_set_state (call,LinphoneCallPausedByRemote,"Call paused by remote");
382 }
383
384 static void call_updated_by_remote(LinphoneCore *lc, LinphoneCall *call){
385         if(lc->vtable.display_status)
386                 lc->vtable.display_status(lc,_("Call is updated by remote."));
387         call->defer_update=FALSE;
388         linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
389         if (call->defer_update==FALSE){
390                 linphone_core_accept_call_update(lc,call,NULL);
391         }
392 }
393
394 /* this callback is called when an incoming re-INVITE modifies the session*/
395 static void call_updating(SalOp *op){
396         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
397         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
398         SalMediaDescription *rmd=sal_call_get_remote_media_description(op);
399
400         if (rmd==NULL){
401                 /* case of a reINVITE without SDP */
402                 call_accept_update(lc,call);
403                 call->media_pending=TRUE;
404                 return;
405         }
406
407         switch(call->state){
408                 case LinphoneCallPausedByRemote:
409                         if (sal_media_description_has_dir(rmd,SalStreamSendRecv) || sal_media_description_has_dir(rmd,SalStreamRecvOnly)){
410                                 call_resumed(lc,call);
411                         }else call_paused_by_remote(lc,call);
412                 break;
413                 case LinphoneCallStreamsRunning:
414                 case LinphoneCallConnected:
415                         if (sal_media_description_has_dir(rmd,SalStreamSendOnly) || sal_media_description_has_dir(rmd,SalStreamInactive)){
416                                 call_paused_by_remote(lc,call);
417                         }else{
418                                 call_updated_by_remote(lc,call);
419                         }
420                 break;
421                 default:
422                         call_accept_update(lc,call);
423         }
424 }
425
426 static void call_terminated(SalOp *op, const char *from){
427         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
428         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
429
430         if (call==NULL) return;
431         
432         switch(linphone_call_get_state(call)){
433                 case LinphoneCallEnd:
434                 case LinphoneCallError:
435                         ms_warning("call_terminated: ignoring.");
436                         return;
437                 break;
438                 case LinphoneCallIncomingReceived:
439                 case LinphoneCallIncomingEarlyMedia:
440                         call->reason=LinphoneReasonNotAnswered;
441                 break;
442                 default:
443                 break;
444         }
445         ms_message("Current call terminated...");
446         //we stop the call only if we have this current call or if we are in call
447         if (lc->ringstream!=NULL && ( (ms_list_size(lc->calls)  == 1) || linphone_core_in_call(lc) )) {
448                 ring_stop(lc->ringstream);
449                 lc->ringstream=NULL;
450         }
451         linphone_call_stop_media_streams(call);
452         if (lc->vtable.show!=NULL)
453                 lc->vtable.show(lc);
454         if (lc->vtable.display_status!=NULL)
455                 lc->vtable.display_status(lc,_("Call terminated."));
456
457         linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
458 }
459
460 static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
461         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
462         char *msg486=_("User is busy.");
463         char *msg480=_("User is temporarily unavailable.");
464         /*char *retrymsg=_("%s. Retry after %i minute(s).");*/
465         char *msg600=_("User does not want to be disturbed.");
466         char *msg603=_("Call declined.");
467         const char *msg=details;
468         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
469
470         if (call==NULL){
471                 ms_warning("Call faillure reported on already cleaned call ?");
472                 return ;
473         }
474         
475         if (lc->vtable.show) lc->vtable.show(lc);
476
477         if (error==SalErrorNoResponse){
478                 msg=_("No response.");
479                 if (lc->vtable.display_status)
480                         lc->vtable.display_status(lc,msg);
481         }else if (error==SalErrorProtocol){
482                 msg=details ? details : _("Protocol error.");
483                 if (lc->vtable.display_status)
484                         lc->vtable.display_status(lc, msg);
485         }else if (error==SalErrorFailure){
486                 switch(sr){
487                         case SalReasonDeclined:
488                                 msg=msg603;
489                                 if (lc->vtable.display_status)
490                                         lc->vtable.display_status(lc,msg603);
491                         break;
492                         case SalReasonBusy:
493                                 msg=msg486;
494                                 if (lc->vtable.display_status)
495                                         lc->vtable.display_status(lc,msg486);
496                         break;
497                         case SalReasonRedirect:
498                                 msg=_("Redirected");
499                                 if (lc->vtable.display_status)
500                                         lc->vtable.display_status(lc,msg);
501                         break;
502                         case SalReasonTemporarilyUnavailable:
503                                 msg=msg480;
504                                 if (lc->vtable.display_status)
505                                         lc->vtable.display_status(lc,msg480);
506                         break;
507                         case SalReasonNotFound:
508                                 if (lc->vtable.display_status)
509                                         lc->vtable.display_status(lc,msg);
510                         break;
511                         case SalReasonDoNotDisturb:
512                                 msg=msg600;
513                                 if (lc->vtable.display_status)
514                                         lc->vtable.display_status(lc,msg600);
515                         break;
516                         case SalReasonMedia:
517                         //media_encryption_mandatory
518                                 if (call->params.media_encryption == LinphoneMediaEncryptionSRTP && 
519                                         !linphone_core_is_media_encryption_mandatory(lc)) {
520                                         int i;
521                                         ms_message("Outgoing call failed with SRTP (SAVP) enabled - retrying with AVP");
522                                         linphone_call_stop_media_streams(call);
523                                         if (call->state==LinphoneCallOutgoingInit || call->state==LinphoneCallOutgoingProgress){
524                                                 /* clear SRTP local params */
525                                                 call->params.media_encryption = LinphoneMediaEncryptionNone;
526                                                 for(i=0; i<call->localdesc->nstreams; i++) {
527                                                         call->localdesc->streams[i].proto = SalProtoRtpAvp;
528                                                         memset(call->localdesc->streams[i].crypto, 0, sizeof(call->localdesc->streams[i].crypto));
529                                                 }
530                                                 linphone_core_start_invite(lc, call, NULL);
531                                         }
532                                         return;
533                                 }
534                                 msg=_("Incompatible media parameters.");
535                                 if (lc->vtable.display_status)
536                                         lc->vtable.display_status(lc,msg);
537                         break;
538                         default:
539                                 if (lc->vtable.display_status)
540                                         lc->vtable.display_status(lc,_("Call failed."));
541                 }
542         }
543
544         if (lc->ringstream!=NULL) {
545                 ring_stop(lc->ringstream);
546                 lc->ringstream=NULL;
547         }
548         linphone_call_stop_media_streams (call);
549         if (call->referer && linphone_call_get_state(call->referer)==LinphoneCallPaused && call->referer->was_automatically_paused){
550                 /*resume to the call that send us the refer automatically*/
551                 linphone_core_resume_call(lc,call->referer);
552         }
553         if (sr == SalReasonDeclined) {
554                 call->reason=LinphoneReasonDeclined;
555                 linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
556         } else if (sr == SalReasonNotFound) {
557                 call->reason=LinphoneReasonNotFound;
558                 linphone_call_set_state(call,LinphoneCallError,"User not found.");
559         } else {
560                 linphone_call_set_state(call,LinphoneCallError,msg);
561         }
562 }
563
564 static void call_released(SalOp *op){
565         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
566         if (call!=NULL){
567                 linphone_call_set_state(call,LinphoneCallReleased,"Call released");
568         }else ms_error("call_released() for already destroyed call ?");
569 }
570
571 static void auth_requested(SalOp *h, const char *realm, const char *username){
572         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
573         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
574         LinphoneCall *call=is_a_linphone_call(sal_op_get_user_pointer(h));
575
576         if (call && call->ping_op==h){
577                 /*don't request authentication for ping requests. Their purpose is just to get any
578                  * answer to get the Via's received and rport parameters.
579                  */
580                 ms_message("auth_requested(): ignored for ping request.");
581                 return;
582         }
583         
584         ms_message("auth_requested() for realm=%s, username=%s",realm,username);
585
586         if (ai && ai->works==FALSE && ai->usecount>=3){
587                 /*case we tried 3 times to authenticate, without success */
588                 /*Better is to stop (implemeted below in else statement), and retry later*/
589                 if (ms_time(NULL)-ai->last_use_time>30){
590                         ai->usecount=0; /*so that we can allow to retry */
591                 }
592         }
593         
594         if (ai && (ai->works || ai->usecount<3)){
595                 SalAuthInfo sai;
596                 sai.username=ai->username;
597                 sai.userid=ai->userid;
598                 sai.realm=ai->realm;
599                 sai.password=ai->passwd;
600                 ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
601                 sal_op_authenticate(h,&sai);
602                 ai->usecount++;
603                 ai->last_use_time=ms_time(NULL);
604         }else{
605                 if (ai && ai->works==FALSE) {
606                         sal_op_cancel_authentication(h);
607                 } 
608                 if (lc->vtable.auth_info_requested)
609                         lc->vtable.auth_info_requested(lc,realm,username);
610         }
611 }
612
613 static void auth_success(SalOp *h, const char *realm, const char *username){
614         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
615         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
616         if (ai){
617                 ms_message("%s/%s authentication works.",realm,username);
618                 ai->works=TRUE;
619         }
620 }
621
622 static void register_success(SalOp *op, bool_t registered){
623         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
624         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
625         char *msg;
626         
627         if (cfg->deletion_date!=0){
628                 ms_message("Registration success for removed proxy config, ignored");
629                 return;
630         }
631         linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
632         linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
633                                         registered ? "Registration sucessful" : "Unregistration done");
634         if (lc->vtable.display_status){
635                 if (registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
636                 else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
637                 lc->vtable.display_status(lc,msg);
638                 ms_free(msg);
639         }
640         
641 }
642
643 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
644         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
645         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
646
647         if (cfg==NULL){
648                 ms_warning("Registration failed for unknown proxy config.");
649                 return ;
650         }
651         if (cfg->deletion_date!=0){
652                 ms_message("Registration failed for removed proxy config, ignored");
653                 return;
654         }
655         if (details==NULL)
656                 details=_("no response timeout");
657         
658         if (lc->vtable.display_status) {
659                 char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),details  );
660                 lc->vtable.display_status(lc,msg);
661                 ms_free(msg);
662         }
663         if (error== SalErrorFailure && reason == SalReasonForbidden) {
664                 linphone_proxy_config_set_error(cfg, LinphoneReasonBadCredentials);
665         } else if (error == SalErrorNoResponse) {
666                 linphone_proxy_config_set_error(cfg, LinphoneReasonNoResponse);
667         }
668         linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
669         if (error== SalErrorFailure && reason == SalReasonForbidden) {
670                 const char *realm=NULL,*username=NULL;
671                 if (sal_op_get_auth_requested(op,&realm,&username)==0){
672                         if (lc->vtable.auth_info_requested)
673                                 lc->vtable.auth_info_requested(lc,realm,username);
674                 }
675         }
676 }
677
678 static void vfu_request(SalOp *op){
679 #ifdef VIDEO_ENABLED
680         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
681         if (call==NULL){
682                 ms_warning("VFU request but no call !");
683                 return ;
684         }
685         if (call->videostream)
686                 video_stream_send_vfu(call->videostream);
687 #endif
688 }
689
690 static void dtmf_received(SalOp *op, char dtmf){
691         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
692         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
693         if (lc->vtable.dtmf_received != NULL)
694                 lc->vtable.dtmf_received(lc, call, dtmf);
695 }
696
697 static void refer_received(Sal *sal, SalOp *op, const char *referto){
698         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
699         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
700         if (call){
701                 if (call->refer_to!=NULL){
702                         ms_free(call->refer_to);
703                 }
704                 call->refer_to=ms_strdup(referto);
705                 call->refer_pending=TRUE;
706                 linphone_call_set_state(call,LinphoneCallRefered,"Refered");
707                 if (lc->vtable.display_status){
708                         char *msg=ms_strdup_printf(_("We are transferred to %s"),referto);
709                         lc->vtable.display_status(lc,msg);
710                         ms_free(msg);
711                 }
712                 if (call->state!=LinphoneCallPaused){
713                         ms_message("Automatically pausing current call to accept transfer.");
714                         linphone_core_pause_call(lc,call);
715                         call->was_automatically_paused=TRUE;
716                         /*then we will start the refered when the pause is accepted, in order to serialize transactions within the dialog.
717                          * Indeed we need to avoid to send a NOTIFY to inform about of state of the refered call while the pause isn't completed.
718                         **/
719                 }else linphone_core_start_refered_call(lc,call);
720         }else if (lc->vtable.refer_received){
721                 lc->vtable.refer_received(lc,referto);
722         }
723 }
724
725 static void text_received(Sal *sal, const char *from, const char *msg){
726         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
727         linphone_core_text_received(lc,from,msg);
728 }
729
730 static void notify(SalOp *op, const char *from, const char *msg){
731         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
732         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
733         ms_message("get a %s notify from %s",msg,from);
734         if(lc->vtable.notify_recv)
735                 lc->vtable.notify_recv(lc,call,from,msg);
736 }
737
738 static void notify_presence(SalOp *op, SalSubscribeStatus ss, SalPresenceStatus status, const char *msg){
739         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
740         linphone_notify_recv(lc,op,ss,status);
741 }
742
743 static void subscribe_received(SalOp *op, const char *from){
744         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
745         linphone_subscription_new(lc,op,from);
746 }
747
748 static void subscribe_closed(SalOp *op, const char *from){
749         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
750         linphone_subscription_closed(lc,op);
751 }
752
753 static void ping_reply(SalOp *op){
754         LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
755         ms_message("ping reply !");
756         if (call){
757                 if (call->state==LinphoneCallOutgoingInit){
758                         call->ping_replied=TRUE;
759                         linphone_core_proceed_with_invite_if_ready(call->core,call,NULL);
760                 }
761         }
762         else
763         {
764                 ms_warning("ping reply without call attached...");
765         }
766 }
767
768 static void notify_refer(SalOp *op, SalReferStatus status){
769         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
770         LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
771         LinphoneCallState cstate;
772         if (call==NULL) {
773                 ms_warning("Receiving notify_refer for unknown call.");
774                 return ;
775         }
776         switch(status){
777                 case SalReferTrying:
778                         cstate=LinphoneCallOutgoingProgress;
779                 break;
780                 case SalReferSuccess:
781                         cstate=LinphoneCallConnected;
782                 break;
783                 case SalReferFailed:
784                         cstate=LinphoneCallError;
785                 break;
786                 default:
787                         cstate=LinphoneCallError;
788         }
789         linphone_call_set_transfer_state(call, cstate);
790         if (cstate==LinphoneCallConnected){
791                 /*automatically terminate the call as the transfer is complete.*/
792                 linphone_core_terminate_call(lc,call);
793         }
794 }
795
796 SalCallbacks linphone_sal_callbacks={
797         call_received,
798         call_ringing,
799         call_accepted,
800         call_ack,
801         call_updating,
802         call_terminated,
803         call_failure,
804         call_released,
805         auth_requested,
806         auth_success,
807         register_success,
808         register_failure,
809         vfu_request,
810         dtmf_received,
811         refer_received,
812         text_received,
813         notify,
814         notify_presence,
815         notify_refer,
816         subscribe_received,
817         subscribe_closed,
818         ping_reply
819 };
820
821