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