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