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