callbacks.c 29.3 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
linphone
Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


#include "sal.h"

#include "linphonecore.h"
#include "private.h"
25
#include "mediastreamer2/mediastream.h"
26
#include "lpconfig.h"
27

jehan's avatar
jehan committed
28
static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details);
Simon Morlat's avatar
Simon Morlat committed
29

Simon Morlat's avatar
Simon Morlat committed
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;
Simon Morlat's avatar
Simon Morlat committed
32 33 34
	return !sal_media_description_equals(oldmd,newmd)  || call->up_bw!=linphone_core_get_upload_bandwidth(call->core);
}

35 36 37
void linphone_core_update_streams(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription *new_md){
	SalMediaDescription *oldmd=call->resultdesc;
	
Simon Morlat's avatar
Simon Morlat committed
38 39 40 41
	if (lc->ringstream!=NULL){
		ring_stop(lc->ringstream);
		lc->ringstream=NULL;
	}
42 43 44 45 46 47 48
	if (new_md!=NULL){
		sal_media_description_ref(new_md);
		call->media_pending=FALSE;
	}else{
		call->media_pending=TRUE;
	}
	call->resultdesc=new_md;
49 50 51
	if (call->ice_session != NULL) {
		linphone_core_deactivate_ice_for_deactivated_media_streams(call, call->resultdesc);
	}
52
	if ((call->audiostream && call->audiostream->ticker) || (call->videostream && call->videostream->ticker)){
53 54
		/* we already started media: check if we really need to restart it*/
		if (oldmd){
Simon Morlat's avatar
Simon Morlat committed
55
			if (!media_parameters_changed(call,oldmd,new_md) && !call->playing_ringbacktone){
56 57 58
				/*as nothing has changed, keep the oldmd */
				call->resultdesc=oldmd;
				sal_media_description_unref(new_md);
59 60 61 62 63 64
				if (call->all_muted){
					ms_message("Early media finished, unmuting inputs...");
					/*we were in early media, now we want to enable real media */
					linphone_call_enable_camera (call,linphone_call_camera_enabled (call));
					if (call->audiostream)
						linphone_core_mute_mic (lc, linphone_core_is_mic_muted(lc));
Simon Morlat's avatar
Simon Morlat committed
65
#ifdef VIDEO_ENABLED
66 67
					if (call->videostream && call->camera_active)
						video_stream_change_camera(call->videostream,lc->video_conf.device );
Simon Morlat's avatar
Simon Morlat committed
68
#endif
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
				}
				ms_message("No need to restart streams, SDP is unchanged.");
				return;
			}else{
				ms_message("Media descriptions are different, need to restart the streams.");
			}
		}
		linphone_call_stop_media_streams (call);
		linphone_call_init_media_streams (call);
	}
	if (oldmd) 
		sal_media_description_unref(oldmd);
	
	if (new_md) {
		bool_t all_muted=FALSE;
		bool_t send_ringbacktone=FALSE;
		
		if (call->audiostream==NULL){
			/*this happens after pausing the call locally. The streams is destroyed and then we wait the 200Ok to recreate it*/
			linphone_call_init_media_streams (call);
		}
90 91
		if (call->state==LinphoneCallIncomingEarlyMedia && linphone_core_get_remote_ringback_tone (lc)!=NULL){
			send_ringbacktone=TRUE;
92 93
		}
		if (call->state==LinphoneCallIncomingEarlyMedia ||
94 95 96 97 98
		    (call->state==LinphoneCallOutgoingEarlyMedia && !call->params.real_early_media)){
			all_muted=TRUE;
		}
		linphone_call_start_media_streams(call,all_muted,send_ringbacktone);
	}
Simon Morlat's avatar
Simon Morlat committed
99
}
100
#if 0
Simon Morlat's avatar
Simon Morlat committed
101 102 103 104
static bool_t is_duplicate_call(LinphoneCore *lc, const LinphoneAddress *from, const LinphoneAddress *to){
	MSList *elem;
	for(elem=lc->calls;elem!=NULL;elem=elem->next){
		LinphoneCall *call=(LinphoneCall*)elem->data;
105 106
		if (linphone_address_weak_equal(call->log->from,from) &&
		    linphone_address_weak_equal(call->log->to, to)){
Simon Morlat's avatar
Simon Morlat committed
107 108 109 110 111
			return TRUE;
		}
	}
	return FALSE;
}
112 113
#endif

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static bool_t already_a_call_with_remote_address(const LinphoneCore *lc, const LinphoneAddress *remote) {
	ms_warning(" searching for already_a_call_with_remote_address.");

	MSList *elem;
	for(elem=lc->calls;elem!=NULL;elem=elem->next){
		const LinphoneCall *call=(LinphoneCall*)elem->data;
		const LinphoneAddress *cRemote=linphone_call_get_remote_address(call);
		if (linphone_address_weak_equal(cRemote,remote)) {
			ms_warning("already_a_call_with_remote_address found.");
			return TRUE;
		}
	}
	return FALSE;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142
static bool_t already_a_call_pending(LinphoneCore *lc){
	MSList *elem;
	for(elem=lc->calls;elem!=NULL;elem=elem->next){
		LinphoneCall *call=(LinphoneCall*)elem->data;
		if (call->state==LinphoneCallIncomingReceived
		    || call->state==LinphoneCallOutgoingInit
		    || call->state==LinphoneCallOutgoingProgress
		    || call->state==LinphoneCallOutgoingEarlyMedia
		    || call->state==LinphoneCallOutgoingRinging){
			return TRUE;
		}
	}
	return FALSE;
}
Simon Morlat's avatar
Simon Morlat committed
143

144
static void call_received(SalOp *h){
145
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
Simon Morlat's avatar
Simon Morlat committed
146 147
	LinphoneCall *call;
	const char *from,*to;
Simon Morlat's avatar
Simon Morlat committed
148
	LinphoneAddress *from_addr, *to_addr;
149
	bool_t prevent_colliding_calls=lp_config_get_int(lc->config,"sip","prevent_colliding_calls",TRUE);
150
	
Simon Morlat's avatar
Simon Morlat committed
151
	/* first check if we can answer successfully to this invite */
152 153 154 155 156
	if (lc->presence_mode==LinphoneStatusBusy ||
	    lc->presence_mode==LinphoneStatusOffline ||
	    lc->presence_mode==LinphoneStatusDoNotDisturb ||
	    lc->presence_mode==LinphoneStatusMoved){
		if (lc->presence_mode==LinphoneStatusBusy )
Simon Morlat's avatar
Simon Morlat committed
157
			sal_call_decline(h,SalReasonBusy,NULL);
158
		else if (lc->presence_mode==LinphoneStatusOffline)
Simon Morlat's avatar
Simon Morlat committed
159
			sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
160
		else if (lc->presence_mode==LinphoneStatusDoNotDisturb)
Simon Morlat's avatar
Simon Morlat committed
161
			sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
162
		else if (lc->alt_contact!=NULL && lc->presence_mode==LinphoneStatusMoved)
Simon Morlat's avatar
Simon Morlat committed
163
			sal_call_decline(h,SalReasonRedirect,lc->alt_contact);
164
		sal_op_release(h);
Simon Morlat's avatar
Simon Morlat committed
165 166
		return;
	}
167
	if (!linphone_core_can_we_add_call(lc)){/*busy*/
Simon Morlat's avatar
Simon Morlat committed
168
		sal_call_decline(h,SalReasonBusy,NULL);
169
		sal_op_release(h);
Simon Morlat's avatar
Simon Morlat committed
170 171
		return;
	}
172 173
	from=sal_op_get_from(h);
	to=sal_op_get_to(h);
Simon Morlat's avatar
Simon Morlat committed
174 175 176
	from_addr=linphone_address_new(from);
	to_addr=linphone_address_new(to);

177
	if ((already_a_call_with_remote_address(lc,from_addr) && prevent_colliding_calls) || already_a_call_pending(lc)){
178
		ms_warning("Receiving another call while one is ringing or initiated, refusing this one with busy message.");
Simon Morlat's avatar
Simon Morlat committed
179
		sal_call_decline(h,SalReasonBusy,NULL);
Simon Morlat's avatar
Simon Morlat committed
180
		sal_op_release(h);
Simon Morlat's avatar
Simon Morlat committed
181 182 183 184
		linphone_address_destroy(from_addr);
		linphone_address_destroy(to_addr);
		return;
	}
Simon Morlat's avatar
Simon Morlat committed
185
	
Simon Morlat's avatar
Simon Morlat committed
186 187
	call=linphone_call_new_incoming(lc,from_addr,to_addr,h);
	
Simon Morlat's avatar
Simon Morlat committed
188
	/* the call is acceptable so we can now add it to our list */
Simon Morlat's avatar
Simon Morlat committed
189
	linphone_core_add_call(lc,call);
190
	linphone_call_ref(call); /*prevent the call from being destroyed while we are notifying, if the user declines within the state callback */
191

192
	if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && (call->ice_session != NULL)) {
193 194 195
		/* Defer ringing until the end of the ICE candidates gathering process. */
		ms_message("Defer ringing to gather ICE candidates");
		return;
196
	}
197

198
	linphone_core_notify_incoming_call(lc,call);
199 200 201
}

static void call_ringing(SalOp *h){
202
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
203
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
Simon Morlat's avatar
Simon Morlat committed
204
	SalMediaDescription *md;
205
	
Simon Morlat's avatar
Simon Morlat committed
206
	if (call==NULL) return;
207
	
Simon Morlat's avatar
Simon Morlat committed
208 209
	if (lc->vtable.display_status)
		lc->vtable.display_status(lc,_("Remote ringing."));
210
	
Simon Morlat's avatar
Simon Morlat committed
211 212
	md=sal_call_get_final_media_description(h);
	if (md==NULL){
213 214 215 216 217
		if (lc->ringstream && lc->dmfs_playing_start_time!=0){
			ring_stop(lc->ringstream);
			lc->ringstream=NULL;
			lc->dmfs_playing_start_time=0;
		}
Simon Morlat's avatar
Simon Morlat committed
218 219
		if (lc->ringstream!=NULL) return;	/*already ringing !*/
		if (lc->sound_conf.play_sndcard!=NULL){
220
			MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
221
			if (call->localdesc->streams[0].max_rate>0) ms_snd_card_set_preferred_sample_rate(ringcard, call->localdesc->streams[0].max_rate);
Simon Morlat's avatar
Simon Morlat committed
222 223 224
			/*we release sound before playing ringback tone*/
			if (call->audiostream)
				audio_stream_unprepare_sound(call->audiostream);
225
			lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,ringcard);
Simon Morlat's avatar
Simon Morlat committed
226
		}
227 228 229 230
		ms_message("Remote ringing...");
		if (lc->vtable.display_status) 
			lc->vtable.display_status(lc,_("Remote ringing..."));
		linphone_call_set_state(call,LinphoneCallOutgoingRinging,"Remote ringing");
Simon Morlat's avatar
Simon Morlat committed
231 232
	}else{
		/*accept early media */
Simon Morlat's avatar
Simon Morlat committed
233
		if (call->audiostream && audio_stream_started(call->audiostream)){
Simon Morlat's avatar
Simon Morlat committed
234 235 236 237 238 239 240
			/*streams already started */
			ms_message("Early media already started.");
			return;
		}
		if (lc->vtable.show) lc->vtable.show(lc);
		if (lc->vtable.display_status) 
			lc->vtable.display_status(lc,_("Early media."));
241
		linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
Simon Morlat's avatar
Simon Morlat committed
242 243 244 245 246
		if (lc->ringstream!=NULL){
			ring_stop(lc->ringstream);
			lc->ringstream=NULL;
		}
		ms_message("Doing early media...");
Simon Morlat's avatar
Simon Morlat committed
247
		linphone_core_update_streams(lc,call,md);
Simon Morlat's avatar
Simon Morlat committed
248
	}
249 250
}

Simon Morlat's avatar
Simon Morlat committed
251 252 253 254 255
/*
 * could be reach :
 *  - when the call is accepted
 *  - when a request is accepted (pause, resume)
 */
Simon Morlat's avatar
Simon Morlat committed
256
static void call_accepted(SalOp *op){
257
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
258
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
259 260
	SalMediaDescription *md;
	
Simon Morlat's avatar
Simon Morlat committed
261 262
	if (call==NULL){
		ms_warning("No call to accept.");
263
		return ;
Simon Morlat's avatar
Simon Morlat committed
264
	}
265

266 267 268
	/* Handle remote ICE attributes if any. */
	if (call->ice_session != NULL) {
		linphone_core_update_ice_from_remote_media_description(call, sal_call_get_remote_media_description(op));
269 270
	}

271 272
	md=sal_call_get_final_media_description(op);
	
Simon Morlat's avatar
Simon Morlat committed
273 274 275 276
	if (call->state==LinphoneCallOutgoingProgress ||
	    call->state==LinphoneCallOutgoingRinging ||
	    call->state==LinphoneCallOutgoingEarlyMedia){
		linphone_call_set_state(call,LinphoneCallConnected,"Connected");
277
		if (call->referer) linphone_core_notify_refer_state(lc,call->referer,call);
Simon Morlat's avatar
Simon Morlat committed
278
	}
279 280 281
	if (md && !sal_media_description_empty(md)){
		if (sal_media_description_has_dir(md,SalStreamSendOnly) ||
		    sal_media_description_has_dir(md,SalStreamInactive)){
Simon Morlat's avatar
Simon Morlat committed
282 283 284 285 286 287 288
			if (lc->vtable.display_status){
				char *tmp=linphone_call_get_remote_address_as_string (call);
				char *msg=ms_strdup_printf(_("Call with %s is paused."),tmp);
				lc->vtable.display_status(lc,msg);
				ms_free(tmp);
				ms_free(msg);
			}
289
			linphone_core_update_streams (lc,call,md);
290
			linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
291 292
			if (call->refer_pending)
				linphone_core_start_refered_call(lc,call);
293
		}else if (sal_media_description_has_dir(md,SalStreamRecvOnly)){
294 295 296 297 298 299 300 301
			/*we are put on hold when the call is initially accepted */
			if (lc->vtable.display_status){
				char *tmp=linphone_call_get_remote_address_as_string (call);
				char *msg=ms_strdup_printf(_("Call answered by %s - on hold."),tmp);
				lc->vtable.display_status(lc,msg);
				ms_free(tmp);
				ms_free(msg);
			}
302
			linphone_core_update_streams (lc,call,md);
303
			linphone_call_set_state(call,LinphoneCallPausedByRemote,"Call paused by remote");
304
		}else{
305 306 307 308
			if (call->state==LinphoneCallStreamsRunning){
				/*media was running before, the remote as acceted a call modification (that is
					a reinvite made by us. We must notify the application this reinvite was accepted*/
				linphone_call_set_state(call, LinphoneCallUpdated, "Call updated");
Simon Morlat's avatar
Simon Morlat committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322
			}else{
				if (call->state==LinphoneCallResuming){
					if (lc->vtable.display_status){
						lc->vtable.display_status(lc,_("Call resumed."));
					}
				}else{
					if (lc->vtable.display_status){
						char *tmp=linphone_call_get_remote_address_as_string (call);
						char *msg=ms_strdup_printf(_("Call answered by %s."),tmp);
						lc->vtable.display_status(lc,msg);
						ms_free(tmp);
						ms_free(msg);
					}
				}
323
			}
324
			linphone_core_update_streams (lc,call,md);
325 326
			if (!call->current_params.in_conference)
				lc->current_call=call;
327
			linphone_call_set_state(call, LinphoneCallStreamsRunning, "Streams running");
Simon Morlat's avatar
Simon Morlat committed
328
		}
Simon Morlat's avatar
Simon Morlat committed
329 330 331
	}else{
		/*send a bye*/
		ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
332
		linphone_core_abort_call(lc,call,_("Incompatible, check codecs..."));
Simon Morlat's avatar
Simon Morlat committed
333
	}
334 335
}

336 337
static void call_ack(SalOp *op){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
338
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
Simon Morlat's avatar
Simon Morlat committed
339 340 341 342
	if (call==NULL){
		ms_warning("No call to be ACK'd");
		return ;
	}
343
	if (call->media_pending){
344 345
		SalMediaDescription *md=sal_call_get_final_media_description(op);
		if (md && !sal_media_description_empty(md)){
346 347 348 349 350
			if (call->state==LinphoneCallStreamsRunning){
				/*media was running before, the remote as acceted a call modification (that is
					a reinvite made by us. We must notify the application this reinvite was accepted*/
				linphone_call_set_state(call, LinphoneCallUpdated, "Call updated");
			}
351
			linphone_core_update_streams (lc,call,md);
Simon Morlat's avatar
bugfix  
Simon Morlat committed
352
			linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
353 354 355
		}else{
			/*send a bye*/
			ms_error("Incompatible SDP response received in ACK, need to abort the call");
356 357
			linphone_core_abort_call(lc,call,"No codec intersection");
			return;
358
		}
Simon Morlat's avatar
Simon Morlat committed
359
	}
360 361
}

362 363
static void call_accept_update(LinphoneCore *lc, LinphoneCall *call){
	SalMediaDescription *md;
364 365 366 367 368
	SalMediaDescription *rmd=sal_call_get_remote_media_description(call->op);
	if ((rmd!=NULL) && (call->ice_session!=NULL)) {
		linphone_core_update_ice_from_remote_media_description(call,rmd);
		linphone_core_update_local_media_description_from_ice(call->localdesc,call->ice_session);
	}
369 370 371 372 373 374 375 376 377 378 379
	sal_call_accept(call->op);
	md=sal_call_get_final_media_description(call->op);
	if (md && !sal_media_description_empty(md))
		linphone_core_update_streams(lc,call,md);
}

static void call_resumed(LinphoneCore *lc, LinphoneCall *call){
	call_accept_update(lc,call);
	if(lc->vtable.display_status)
		lc->vtable.display_status(lc,_("We have been resumed."));
	linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
380
	linphone_call_set_transfer_state(call, LinphoneCallIdle);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
}

static void call_paused_by_remote(LinphoneCore *lc, LinphoneCall *call){
	call_accept_update(lc,call);
	/* we are being paused */
	if(lc->vtable.display_status)
		lc->vtable.display_status(lc,_("We are paused by other party."));
	linphone_call_set_state (call,LinphoneCallPausedByRemote,"Call paused by remote");
}

static void call_updated_by_remote(LinphoneCore *lc, LinphoneCall *call){
	if(lc->vtable.display_status)
		lc->vtable.display_status(lc,_("Call is updated by remote."));
	call->defer_update=FALSE;
	linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
	if (call->defer_update==FALSE){
		linphone_core_accept_call_update(lc,call,NULL);
	}
}
400

401
/* this callback is called when an incoming re-INVITE modifies the session*/
402
static void call_updating(SalOp *op){
403 404
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
405
	SalMediaDescription *rmd=sal_call_get_remote_media_description(op);
406

407 408 409 410 411 412 413
	if (rmd==NULL){
		/* case of a reINVITE without SDP */
		call_accept_update(lc,call);
		call->media_pending=TRUE;
		return;
	}

414 415 416 417
	switch(call->state){
		case LinphoneCallPausedByRemote:
			if (sal_media_description_has_dir(rmd,SalStreamSendRecv) || sal_media_description_has_dir(rmd,SalStreamRecvOnly)){
				call_resumed(lc,call);
418
			}else call_paused_by_remote(lc,call);
419 420 421 422 423
		break;
		case LinphoneCallStreamsRunning:
		case LinphoneCallConnected:
			if (sal_media_description_has_dir(rmd,SalStreamSendOnly) || sal_media_description_has_dir(rmd,SalStreamInactive)){
				call_paused_by_remote(lc,call);
424
			}else{
425
				call_updated_by_remote(lc,call);
426
			}
427 428 429
		break;
		default:
			call_accept_update(lc,call);
Simon Morlat's avatar
Simon Morlat committed
430
	}
431 432
}

433 434
static void call_terminated(SalOp *op, const char *from){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
435
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
Simon Morlat's avatar
Simon Morlat committed
436 437

	if (call==NULL) return;
438
	
439 440 441 442 443 444 445 446 447 448 449 450
	switch(linphone_call_get_state(call)){
		case LinphoneCallEnd:
		case LinphoneCallError:
			ms_warning("call_terminated: ignoring.");
			return;
		break;
		case LinphoneCallIncomingReceived:
		case LinphoneCallIncomingEarlyMedia:
			call->reason=LinphoneReasonNotAnswered;
		break;
		default:
		break;
Simon Morlat's avatar
Simon Morlat committed
451 452
	}
	ms_message("Current call terminated...");
Simon Morlat's avatar
Simon Morlat committed
453 454
	//we stop the call only if we have this current call or if we are in call
	if (lc->ringstream!=NULL && ( (ms_list_size(lc->calls)  == 1) || linphone_core_in_call(lc) )) {
Simon Morlat's avatar
Simon Morlat committed
455 456 457
		ring_stop(lc->ringstream);
		lc->ringstream=NULL;
	}
458
	linphone_call_stop_media_streams(call);
Simon Morlat's avatar
Simon Morlat committed
459 460 461 462
	if (lc->vtable.show!=NULL)
		lc->vtable.show(lc);
	if (lc->vtable.display_status!=NULL)
		lc->vtable.display_status(lc,_("Call terminated."));
463 464

	linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
465 466
}

467
static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
468
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
Simon Morlat's avatar
Simon Morlat committed
469 470 471 472 473
	char *msg486=_("User is busy.");
	char *msg480=_("User is temporarily unavailable.");
	/*char *retrymsg=_("%s. Retry after %i minute(s).");*/
	char *msg600=_("User does not want to be disturbed.");
	char *msg603=_("Call declined.");
474
	const char *msg=details;
475
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
476

477 478 479 480 481
	if (call==NULL){
		ms_warning("Call faillure reported on already cleaned call ?");
		return ;
	}
	
Simon Morlat's avatar
Simon Morlat committed
482 483 484
	if (lc->vtable.show) lc->vtable.show(lc);

	if (error==SalErrorNoResponse){
485
		msg=_("No response.");
486
		if (lc->vtable.display_status)
487
			lc->vtable.display_status(lc,msg);
Simon Morlat's avatar
Simon Morlat committed
488
	}else if (error==SalErrorProtocol){
489
		msg=details ? details : _("Protocol error.");
490
		if (lc->vtable.display_status)
491
			lc->vtable.display_status(lc, msg);
Simon Morlat's avatar
Simon Morlat committed
492 493 494
	}else if (error==SalErrorFailure){
		switch(sr){
			case SalReasonDeclined:
Simon Morlat's avatar
Simon Morlat committed
495
				msg=msg603;
Simon Morlat's avatar
Simon Morlat committed
496 497 498 499
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,msg603);
			break;
			case SalReasonBusy:
Simon Morlat's avatar
Simon Morlat committed
500
				msg=msg486;
Simon Morlat's avatar
Simon Morlat committed
501 502 503 504
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,msg486);
			break;
			case SalReasonRedirect:
Simon Morlat's avatar
Simon Morlat committed
505
				msg=_("Redirected");
Simon Morlat's avatar
Simon Morlat committed
506
				if (lc->vtable.display_status)
Simon Morlat's avatar
Simon Morlat committed
507
					lc->vtable.display_status(lc,msg);
Simon Morlat's avatar
Simon Morlat committed
508 509
			break;
			case SalReasonTemporarilyUnavailable:
Simon Morlat's avatar
Simon Morlat committed
510
				msg=msg480;
Simon Morlat's avatar
Simon Morlat committed
511 512 513 514 515
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,msg480);
			break;
			case SalReasonNotFound:
				if (lc->vtable.display_status)
Simon Morlat's avatar
Simon Morlat committed
516
					lc->vtable.display_status(lc,msg);
Simon Morlat's avatar
Simon Morlat committed
517 518
			break;
			case SalReasonDoNotDisturb:
Simon Morlat's avatar
Simon Morlat committed
519
				msg=msg600;
Simon Morlat's avatar
Simon Morlat committed
520 521 522 523
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,msg600);
			break;
			case SalReasonMedia:
524 525 526 527 528
			//media_encryption_mandatory
				if (call->params.media_encryption == LinphoneMediaEncryptionSRTP && 
					!linphone_core_is_media_encryption_mandatory(lc)) {
					int i;
					ms_message("Outgoing call failed with SRTP (SAVP) enabled - retrying with AVP");
529
					linphone_call_stop_media_streams(call);
530 531 532 533 534 535 536 537
					if (call->state==LinphoneCallOutgoingInit || call->state==LinphoneCallOutgoingProgress){
						/* clear SRTP local params */
						call->params.media_encryption = LinphoneMediaEncryptionNone;
						for(i=0; i<call->localdesc->nstreams; i++) {
							call->localdesc->streams[i].proto = SalProtoRtpAvp;
							memset(call->localdesc->streams[i].crypto, 0, sizeof(call->localdesc->streams[i].crypto));
						}
						linphone_core_start_invite(lc, call, NULL);
538 539 540
					}
					return;
				}
541
				msg=_("Incompatible media parameters.");
Simon Morlat's avatar
Simon Morlat committed
542
				if (lc->vtable.display_status)
Simon Morlat's avatar
Simon Morlat committed
543
					lc->vtable.display_status(lc,msg);
Simon Morlat's avatar
Simon Morlat committed
544 545 546 547 548 549
			break;
			default:
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,_("Call failed."));
		}
	}
550

Simon Morlat's avatar
Simon Morlat committed
551 552 553 554
	if (lc->ringstream!=NULL) {
		ring_stop(lc->ringstream);
		lc->ringstream=NULL;
	}
555
	linphone_call_stop_media_streams (call);
556 557 558 559
	if (call->referer && linphone_call_get_state(call->referer)==LinphoneCallPaused && call->referer->was_automatically_paused){
		/*resume to the call that send us the refer automatically*/
		linphone_core_resume_call(lc,call->referer);
	}
560
	if (sr == SalReasonDeclined) {
561 562
		call->reason=LinphoneReasonDeclined;
		linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
563 564 565 566 567
	} else if (sr == SalReasonNotFound) {
		call->reason=LinphoneReasonNotFound;
		linphone_call_set_state(call,LinphoneCallError,"User not found.");
	} else {
		linphone_call_set_state(call,LinphoneCallError,msg);
568
	}
569 570
}

571 572
static void call_released(SalOp *op){
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
573 574 575
	if (call!=NULL){
		linphone_call_set_state(call,LinphoneCallReleased,"Call released");
	}else ms_error("call_released() for already destroyed call ?");
576 577
}

578
static void auth_requested(SalOp *h, const char *realm, const char *username){
579
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
580
	LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
581 582 583 584 585 586 587 588 589 590
	LinphoneCall *call=is_a_linphone_call(sal_op_get_user_pointer(h));

	if (call && call->ping_op==h){
		/*don't request authentication for ping requests. Their purpose is just to get any
		 * answer to get the Via's received and rport parameters.
		 */
		ms_message("auth_requested(): ignored for ping request.");
		return;
	}
	
591
	ms_message("auth_requested() for realm=%s, username=%s",realm,username);
592 593 594 595 596 597 598 599 600

	if (ai && ai->works==FALSE && ai->usecount>=3){
		/*case we tried 3 times to authenticate, without success */
		/*Better is to stop (implemeted below in else statement), and retry later*/
		if (ms_time(NULL)-ai->last_use_time>30){
			ai->usecount=0; /*so that we can allow to retry */
		}
	}
	
601 602 603 604 605 606
	if (ai && (ai->works || ai->usecount<3)){
		SalAuthInfo sai;
		sai.username=ai->username;
		sai.userid=ai->userid;
		sai.realm=ai->realm;
		sai.password=ai->passwd;
607
		ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
608 609
		sal_op_authenticate(h,&sai);
		ai->usecount++;
610
		ai->last_use_time=ms_time(NULL);
611
	}else{
jehan's avatar
jehan committed
612
		if (ai && ai->works==FALSE) {
jehan's avatar
jehan committed
613
			sal_op_cancel_authentication(h);
jehan's avatar
jehan committed
614
		} 
615 616 617
		if (lc->vtable.auth_info_requested)
			lc->vtable.auth_info_requested(lc,realm,username);
	}
618 619 620
}

static void auth_success(SalOp *h, const char *realm, const char *username){
621
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
622
	LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
623 624
	if (ai){
		ms_message("%s/%s authentication works.",realm,username);
625
		ai->works=TRUE;
626
	}
627 628 629
}

static void register_success(SalOp *op, bool_t registered){
630 631 632
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
	char *msg;
633
	
634 635 636 637
	if (cfg->deletion_date!=0){
		ms_message("Registration success for removed proxy config, ignored");
		return;
	}
638
	linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
639 640 641
	linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
	                                registered ? "Registration sucessful" : "Unregistration done");
	if (lc->vtable.display_status){
642
		if (registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
643
		else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
644
		lc->vtable.display_status(lc,msg);
645 646 647
		ms_free(msg);
	}
	
648 649 650
}

static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
651
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
652 653 654 655 656 657
	LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);

	if (cfg==NULL){
		ms_warning("Registration failed for unknown proxy config.");
		return ;
	}
658 659 660 661
	if (cfg->deletion_date!=0){
		ms_message("Registration failed for removed proxy config, ignored");
		return;
	}
662 663 664 665 666 667 668 669
	if (details==NULL)
		details=_("no response timeout");
	
	if (lc->vtable.display_status) {
		char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),details  );
		lc->vtable.display_status(lc,msg);
		ms_free(msg);
	}
jehan's avatar
jehan committed
670
	if (error== SalErrorFailure && reason == SalReasonForbidden) {
671
		linphone_proxy_config_set_error(cfg, LinphoneReasonBadCredentials);
jehan's avatar
jehan committed
672
	} else if (error == SalErrorNoResponse) {
673
		linphone_proxy_config_set_error(cfg, LinphoneReasonNoResponse);
jehan's avatar
jehan committed
674
	}
675
	linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
676 677 678 679 680 681 682
	if (error== SalErrorFailure && reason == SalReasonForbidden) {
		const char *realm=NULL,*username=NULL;
		if (sal_op_get_auth_requested(op,&realm,&username)==0){
			if (lc->vtable.auth_info_requested)
				lc->vtable.auth_info_requested(lc,realm,username);
		}
	}
683 684 685
}

static void vfu_request(SalOp *op){
686
#ifdef VIDEO_ENABLED
687
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
688 689 690 691 692 693
	if (call==NULL){
		ms_warning("VFU request but no call !");
		return ;
	}
	if (call->videostream)
		video_stream_send_vfu(call->videostream);
694
#endif
695 696 697
}

static void dtmf_received(SalOp *op, char dtmf){
698
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
699
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
700
	if (lc->vtable.dtmf_received != NULL)
701
		lc->vtable.dtmf_received(lc, call, dtmf);
702 703
}

704 705
static void refer_received(Sal *sal, SalOp *op, const char *referto){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
706
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
Simon Morlat's avatar
Simon Morlat committed
707
	if (call){
708 709 710 711 712
		if (call->refer_to!=NULL){
			ms_free(call->refer_to);
		}
		call->refer_to=ms_strdup(referto);
		call->refer_pending=TRUE;
Simon Morlat's avatar
Simon Morlat committed
713 714 715 716 717 718
		linphone_call_set_state(call,LinphoneCallRefered,"Refered");
		if (lc->vtable.display_status){
			char *msg=ms_strdup_printf(_("We are transferred to %s"),referto);
			lc->vtable.display_status(lc,msg);
			ms_free(msg);
		}
Simon Morlat's avatar
Simon Morlat committed
719 720 721
		if (call->state!=LinphoneCallPaused){
			ms_message("Automatically pausing current call to accept transfer.");
			linphone_core_pause_call(lc,call);
722
			call->was_automatically_paused=TRUE;
723 724 725 726
			/*then we will start the refered when the pause is accepted, in order to serialize transactions within the dialog.
			 * Indeed we need to avoid to send a NOTIFY to inform about of state of the refered call while the pause isn't completed.
			**/
		}else linphone_core_start_refered_call(lc,call);
Simon Morlat's avatar
Simon Morlat committed
727 728
	}else if (lc->vtable.refer_received){
		lc->vtable.refer_received(lc,referto);
729
	}
730 731 732
}

static void text_received(Sal *sal, const char *from, const char *msg){
733
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
734 735 736 737 738
	linphone_core_message_received(lc,from,msg,NULL);
}
void message_external_body_received(Sal *sal, const char *from, const char *url) {
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
	linphone_core_message_received(lc,from,NULL,url);
739
}
740 741
static void notify(SalOp *op, const char *from, const char *msg){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
742
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
743 744
	ms_message("get a %s notify from %s",msg,from);
	if(lc->vtable.notify_recv)
745
		lc->vtable.notify_recv(lc,call,from,msg);
746 747
}

748
static void notify_presence(SalOp *op, SalSubscribeStatus ss, SalPresenceStatus status, const char *msg){
749 750
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	linphone_notify_recv(lc,op,ss,status);
751 752 753
}

static void subscribe_received(SalOp *op, const char *from){
754 755
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	linphone_subscription_new(lc,op,from);
756 757
}

758 759 760
static void subscribe_closed(SalOp *op, const char *from){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	linphone_subscription_closed(lc,op);
761 762
}

763 764 765 766
static void ping_reply(SalOp *op){
	LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
	ms_message("ping reply !");
	if (call){
767
		if (call->state==LinphoneCallOutgoingInit){
768 769
			call->ping_replied=TRUE;
			linphone_core_proceed_with_invite_if_ready(call->core,call,NULL);
770 771
		}
	}
Simon Morlat's avatar
Simon Morlat committed
772 773 774 775
	else
	{
		ms_warning("ping reply without call attached...");
	}
776 777
}

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
static void notify_refer(SalOp *op, SalReferStatus status){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
	LinphoneCallState cstate;
	if (call==NULL) {
		ms_warning("Receiving notify_refer for unknown call.");
		return ;
	}
	switch(status){
		case SalReferTrying:
			cstate=LinphoneCallOutgoingProgress;
		break;
		case SalReferSuccess:
			cstate=LinphoneCallConnected;
		break;
		case SalReferFailed:
			cstate=LinphoneCallError;
		break;
		default:
			cstate=LinphoneCallError;
	}
799
	linphone_call_set_transfer_state(call, cstate);
800 801 802 803 804 805
	if (cstate==LinphoneCallConnected){
		/*automatically terminate the call as the transfer is complete.*/
		linphone_core_terminate_call(lc,call);
	}
}

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
static LinphoneChatMessageState chatStatusSal2Linphone(SalTextDeliveryStatus status){
	switch(status){
		case SalTextDeliveryInProgress:
			return LinphoneChatMessageStateInProgress;
		case SalTextDeliveryDone:
			return LinphoneChatMessageStateDelivered;
		case SalTextDeliveryFailed:
			return LinphoneChatMessageStateNotDelivered;
	}
	return LinphoneChatMessageStateIdle;
}

static void text_delivery_update(SalOp *op, SalTextDeliveryStatus status){
	LinphoneChatMessage *chat_msg=(LinphoneChatMessage* )sal_op_get_user_pointer(op);
	if (chat_msg && chat_msg->cb) {
		chat_msg->cb(chat_msg
			,chatStatusSal2Linphone(status)
			,chat_msg->cb_ud);
	}
	linphone_chat_message_destroy(chat_msg);
}

828 829 830 831 832
SalCallbacks linphone_sal_callbacks={
	call_received,
	call_ringing,
	call_accepted,
	call_ack,
833
	call_updating,
834 835
	call_terminated,
	call_failure,
836
	call_released,
837 838 839 840 841 842 843 844
	auth_requested,
	auth_success,
	register_success,
	register_failure,
	vfu_request,
	dtmf_received,
	refer_received,
	text_received,
845
	message_external_body_received,
846
	text_delivery_update,
847
	notify,
848
	notify_presence,
849
	notify_refer,
850
	subscribe_received,
851
	subscribe_closed,
852
	ping_reply,
853 854 855
};