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