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