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