callbacks.c 55.8 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
/*
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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Simon Morlat's avatar
Simon Morlat committed
18 19 20
*/


21
#include "sal/sal.h"
Simon Morlat's avatar
Simon Morlat committed
22

23
#include "linphone/core.h"
Simon Morlat's avatar
Simon Morlat committed
24
#include "private.h"
25
#include "mediastreamer2/mediastream.h"
26
#include "linphone/lpconfig.h"
27

28
// stat
29
#ifndef _WIN32
30 31 32 33 34
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

35
static void register_failure(SalOp *op);
Simon Morlat's avatar
Simon Morlat committed
36

37
static int media_parameters_changed(LinphoneCall *call, SalMediaDescription *oldmd, SalMediaDescription *newmd) {
jehan's avatar
jehan committed
38
	int result=0;
39 40 41
	int otherdesc_changed;
	char *tmp1=NULL;
	char *tmp2=NULL;
jehan's avatar
jehan committed
42 43
	if (call->params->in_conference != call->current_params->in_conference) return SAL_MEDIA_DESCRIPTION_FORCE_STREAM_RECONSTRUCTION;
	if (call->up_bw != linphone_core_get_upload_bandwidth(call->core)) return SAL_MEDIA_DESCRIPTION_FORCE_STREAM_RECONSTRUCTION;
44 45 46 47 48 49
	if (call->localdesc_changed) ms_message("Local description has changed: %s", tmp1 = sal_media_description_print_differences(call->localdesc_changed));
	otherdesc_changed = sal_media_description_equals(oldmd, newmd);
	if (otherdesc_changed) ms_message("Other description has changed: %s", tmp2 = sal_media_description_print_differences(otherdesc_changed));
	result = call->localdesc_changed | otherdesc_changed;
	if (tmp1) ms_free(tmp1);
	if (tmp2) ms_free(tmp2);
jehan's avatar
jehan committed
50
	return result;
51 52 53 54 55 56 57 58
}

void linphone_core_update_streams_destinations(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription *old_md, SalMediaDescription *new_md) {
	SalStreamDescription *new_audiodesc = NULL;
	SalStreamDescription *new_videodesc = NULL;
	char *rtp_addr, *rtcp_addr;
	int i;

59
	for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++) {
60
		if (!sal_stream_description_active(&new_md->streams[i])) continue;
61 62 63 64 65 66 67 68 69 70
		if (new_md->streams[i].type == SalAudio) {
			new_audiodesc = &new_md->streams[i];
		} else if (new_md->streams[i].type == SalVideo) {
			new_videodesc = &new_md->streams[i];
		}
	}
	if (call->audiostream && new_audiodesc) {
		rtp_addr = (new_audiodesc->rtp_addr[0] != '\0') ? new_audiodesc->rtp_addr : new_md->addr;
		rtcp_addr = (new_audiodesc->rtcp_addr[0] != '\0') ? new_audiodesc->rtcp_addr : new_md->addr;
		ms_message("Change audio stream destination: RTP=%s:%d RTCP=%s:%d", rtp_addr, new_audiodesc->rtp_port, rtcp_addr, new_audiodesc->rtcp_port);
71
		rtp_session_set_remote_addr_full(call->audiostream->ms.sessions.rtp_session, rtp_addr, new_audiodesc->rtp_port, rtcp_addr, new_audiodesc->rtcp_port);
72 73 74 75 76 77
	}
#ifdef VIDEO_ENABLED
	if (call->videostream && new_videodesc) {
		rtp_addr = (new_videodesc->rtp_addr[0] != '\0') ? new_videodesc->rtp_addr : new_md->addr;
		rtcp_addr = (new_videodesc->rtcp_addr[0] != '\0') ? new_videodesc->rtcp_addr : new_md->addr;
		ms_message("Change video stream destination: RTP=%s:%d RTCP=%s:%d", rtp_addr, new_videodesc->rtp_port, rtcp_addr, new_videodesc->rtcp_port);
78
		rtp_session_set_remote_addr_full(call->videostream->ms.sessions.rtp_session, rtp_addr, new_videodesc->rtp_port, rtcp_addr, new_videodesc->rtcp_port);
79
	}
80 81
#else
	(void)new_videodesc;
82
#endif
83 84
}

85 86 87
static void _clear_early_media_destinations(LinphoneCall *call, MediaStream *ms){
	RtpSession *session=ms->sessions.rtp_session;
	rtp_session_clear_aux_remote_addr(session);
88
	if (!call->ice_session) rtp_session_set_symmetric_rtp(session,linphone_core_symmetric_rtp_enabled(call->core));/*restore symmetric rtp if ICE is not used*/
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
}

static void clear_early_media_destinations(LinphoneCall *call){
	if (call->audiostream){
		_clear_early_media_destinations(call,(MediaStream*)call->audiostream);
	}
	if (call->videostream){
		_clear_early_media_destinations(call,(MediaStream*)call->videostream);
	}
}

static void prepare_early_media_forking(LinphoneCall *call){
	/*we need to disable symmetric rtp otherwise our outgoing streams will be switching permanently between the multiple destinations*/
	if (call->audiostream){
		rtp_session_set_symmetric_rtp(call->audiostream->ms.sessions.rtp_session,FALSE);
	}
	if (call->videostream){
		rtp_session_set_symmetric_rtp(call->videostream->ms.sessions.rtp_session,FALSE);
	}
108
}
109

110 111 112 113
void linphone_call_update_frozen_payloads(LinphoneCall *call, SalMediaDescription *result_desc){
	SalMediaDescription *local=call->localdesc;
	int i;
	for(i=0;i<result_desc->nb_streams;++i){
114
		bctbx_list_t *elem;
115 116 117 118
		for (elem=result_desc->streams[i].payloads;elem!=NULL;elem=elem->next){
			PayloadType *pt=(PayloadType*)elem->data;
			if (is_payload_type_number_available(local->streams[i].already_assigned_payloads, payload_type_get_number(pt), NULL)){
				/*new codec, needs to be added to the list*/
119
				local->streams[i].already_assigned_payloads=bctbx_list_append(local->streams[i].already_assigned_payloads, payload_type_clone(pt));
120
				ms_message("LinphoneCall[%p] : payload type %i %s/%i fmtp=%s added to frozen list.",
121
					   call, payload_type_get_number(pt), pt->mime_type, pt->clock_rate, pt->recv_fmtp ? pt->recv_fmtp : "");
122 123 124
			}
		}
	}
125 126
}

127
void linphone_core_update_streams(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription *new_md, LinphoneCallState target_state){
128
	SalMediaDescription *oldmd=call->resultdesc;
François Grisez's avatar
François Grisez committed
129
	int md_changed=0;
130

131

132 133 134
	if (!((call->state == LinphoneCallIncomingEarlyMedia) && (linphone_core_get_ring_during_incoming_early_media(lc)))) {
		linphone_core_stop_ringing(lc);
	}
135 136 137 138
	if (!new_md) {
		ms_error("linphone_core_update_streams() called with null media description");
		return;
	}
139
	linphone_call_update_biggest_desc(call, call->localdesc);
140
	sal_media_description_ref(new_md);
141
	call->resultdesc=new_md;
142
	if ((call->audiostream && call->audiostream->ms.state==MSStreamStarted) || (call->videostream && call->videostream->ms.state==MSStreamStarted)){
143
		clear_early_media_destinations(call);
François Grisez's avatar
François Grisez committed
144

145 146
		/* we already started media: check if we really need to restart it*/
		if (oldmd){
147 148 149
			md_changed = media_parameters_changed(call, oldmd, new_md);
			if ((md_changed & (	SAL_MEDIA_DESCRIPTION_CODEC_CHANGED
								|SAL_MEDIA_DESCRIPTION_STREAMS_CHANGED
jehan's avatar
jehan committed
150
								|SAL_MEDIA_DESCRIPTION_NETWORK_XXXCAST_CHANGED
151
								|SAL_MEDIA_DESCRIPTION_ICE_RESTART_DETECTED
jehan's avatar
jehan committed
152
								|SAL_MEDIA_DESCRIPTION_FORCE_STREAM_RECONSTRUCTION ))){
153
				ms_message("Media descriptions are different, need to restart the streams.");
154 155
			} else if ( call->playing_ringbacktone) {
				ms_message("Playing ringback tone, will restart the streams.");
156 157 158 159 160
			} else {
				if (md_changed == SAL_MEDIA_DESCRIPTION_UNCHANGED) {
					if (call->all_muted){
						ms_message("Early media finished, unmuting inputs...");
						/*we were in early media, now we want to enable real media */
161
						call->all_muted = FALSE;
162
						if (call->audiostream)
163
							linphone_core_enable_mic(lc, linphone_core_mic_enabled(lc));
164
#ifdef VIDEO_ENABLED
165
						if (call->videostream && call->camera_enabled)
166
							linphone_call_enable_camera(call, linphone_call_camera_enabled(call));
167
#endif
168
					}
jehan's avatar
jehan committed
169
					/*FIXME ZRTP, might be restarted in any cases ? */
170
					ms_message("No need to restart streams, SDP is unchanged.");
171
					goto end;
Simon Morlat's avatar
Simon Morlat committed
172
				}else {
173 174 175 176
					if (md_changed & SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED) {
						ms_message("Network parameters have changed, update them.");
						linphone_core_update_streams_destinations(lc, call, oldmd, new_md);
					}
177
					if (md_changed & SAL_MEDIA_DESCRIPTION_CRYPTO_KEYS_CHANGED) {
178 179 180
						ms_message("Crypto parameters have changed, update them.");
						linphone_call_update_crypto_parameters(call, oldmd, new_md);
					}
181
					goto end;
182 183 184 185
				}
			}
		}
		linphone_call_stop_media_streams (call);
186 187
		if (md_changed & SAL_MEDIA_DESCRIPTION_NETWORK_XXXCAST_CHANGED){
			ms_message("Media ip type has changed, destroying sessions context on call [%p]",call);
188 189
			ms_media_stream_sessions_uninit(&call->sessions[call->main_audio_stream_index]);
			ms_media_stream_sessions_uninit(&call->sessions[call->main_video_stream_index]);
Simon Morlat's avatar
Simon Morlat committed
190
			ms_media_stream_sessions_uninit(&call->sessions[call->main_text_stream_index]);
191
		}
192 193
		linphone_call_init_media_streams (call);
	}
194

195 196 197 198
	if (call->audiostream==NULL){
		/*this happens after pausing the call locally. The streams are destroyed and then we wait the 200Ok to recreate them*/
		linphone_call_init_media_streams (call);
	}
199

200
	if (call->params->real_early_media && call->state==LinphoneCallOutgoingEarlyMedia){
201 202
		prepare_early_media_forking(call);
	}
203
	linphone_call_start_media_streams(call, target_state);
204
	if (call->state==LinphoneCallPausing && call->paused_by_app && bctbx_list_size(lc->calls)==1){
205 206
		linphone_core_play_named_tone(lc,LinphoneToneCallOnHold);
	}
207
	linphone_call_update_frozen_payloads(call, new_md);
208
	end:
209
	if (oldmd)
210
		sal_media_description_unref(oldmd);
211

Simon Morlat's avatar
Simon Morlat committed
212
}
213
#if 0
Simon Morlat's avatar
Simon Morlat committed
214
static bool_t is_duplicate_call(LinphoneCore *lc, const LinphoneAddress *from, const LinphoneAddress *to){
215
	bctbx_list_t *elem;
Simon Morlat's avatar
Simon Morlat committed
216 217
	for(elem=lc->calls;elem!=NULL;elem=elem->next){
		LinphoneCall *call=(LinphoneCall*)elem->data;
218
		if (linphone_address_weak_equal(call->log->from,from) &&
219
			linphone_address_weak_equal(call->log->to, to)){
Simon Morlat's avatar
Simon Morlat committed
220 221 222 223 224
			return TRUE;
		}
	}
	return FALSE;
}
225 226
#endif

227
static bool_t already_a_call_with_remote_address(const LinphoneCore *lc, const LinphoneAddress *remote) {
228
	bctbx_list_t *elem;
jehan's avatar
jehan committed
229
	ms_message("Searching for already_a_call_with_remote_address.");
230 231 232 233 234 235 236 237 238 239 240 241

	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;
}

Simon Morlat's avatar
Simon Morlat committed
242

243 244 245 246
static LinphoneCall * look_for_broken_call_to_replace(SalOp *h, LinphoneCore *lc) {
	const bctbx_list_t *calls = linphone_core_get_calls(lc);
	const bctbx_list_t *it = calls;
	while (it != NULL) {
247
		LinphoneCall *replaced_call = NULL;
248
		LinphoneCall *call = (LinphoneCall *)bctbx_list_get_data(it);
249 250
		SalOp *replaced_op = sal_call_get_replaces(h);
		if (replaced_op) replaced_call = (LinphoneCall*)sal_op_get_user_pointer(replaced_op);
251 252
		if ((call->broken && sal_call_compare_op(h, call->op))
			|| ((replaced_call == call) && (strcmp(sal_op_get_from(h), sal_op_get_from(replaced_op)) == 0) && (strcmp(sal_op_get_to(h), sal_op_get_to(replaced_op)) == 0))) {
253 254 255 256 257 258 259 260
			return call;
		}
		it = bctbx_list_next(it);
	}
	
	return NULL;
}

261
static void call_received(SalOp *h){
262
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
Simon Morlat's avatar
Simon Morlat committed
263
	LinphoneCall *call;
264
	LinphoneCall *replaced_call;
265
	char *alt_contact;
266 267
	LinphoneAddress *from_addr=NULL;
	LinphoneAddress  *to_addr=NULL;
268
	LinphoneAddress *from_address_to_search_if_me=NULL; /*address used to know if I'm the caller*/
269
	SalMediaDescription *md;
270
	const char * p_asserted_id;
271

272 273 274 275 276 277 278
	/* Look if this INVITE is for a call that has already been notified but broken because of network failure */
	replaced_call = look_for_broken_call_to_replace(h, lc);
	if (replaced_call != NULL) {
		linphone_call_replace_op(replaced_call, h);
		return;
	}

Simon Morlat's avatar
Simon Morlat committed
279
	/* first check if we can answer successfully to this invite */
280
	if (linphone_presence_model_get_basic_status(lc->presence_model) == LinphonePresenceBasicStatusClosed) {
281 282 283
		LinphonePresenceActivity *activity = linphone_presence_model_get_activity(lc->presence_model);
		switch (linphone_presence_activity_get_type(activity)) {
			case LinphonePresenceActivityPermanentAbsence:
284 285 286 287
				alt_contact = linphone_presence_model_get_contact(lc->presence_model);
				if (alt_contact != NULL) {
					sal_call_decline(h,SalReasonRedirect,alt_contact);
					ms_free(alt_contact);
288 289
					sal_op_release(h);
					return;
290
				}
291 292
				break;
			default:
293
				/*nothing special to be done*/
294
				break;
295
		}
Simon Morlat's avatar
Simon Morlat committed
296
	}
297

298
	if (!linphone_core_can_we_add_call(lc)){/*busy*/
Simon Morlat's avatar
Simon Morlat committed
299
		sal_call_decline(h,SalReasonBusy,NULL);
300
		sal_op_release(h);
Simon Morlat's avatar
Simon Morlat committed
301 302
		return;
	}
303
	p_asserted_id = sal_custom_header_find(sal_op_get_recv_custom_header(h),"P-Asserted-Identity");
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	/*in some situation, better to trust the network rather than the UAC*/
	if (lp_config_get_int(lc->config,"sip","call_logs_use_asserted_id_instead_of_from",0)) {
		LinphoneAddress *p_asserted_id_addr;
		if (!p_asserted_id) {
			ms_warning("No P-Asserted-Identity header found so cannot use it for op [%p] instead of from",h);
		} else {
			p_asserted_id_addr = linphone_address_new(p_asserted_id);
			if (!p_asserted_id_addr) {
				ms_warning("Unsupported P-Asserted-Identity header for op [%p] ",h);
			} else {
				ms_message("Using P-Asserted-Identity [%s] instead of from [%s] for op [%p]",p_asserted_id,sal_op_get_from(h),h);
				from_addr=p_asserted_id_addr;
			}
		}
	}

	if (!from_addr)
		from_addr=linphone_address_new(sal_op_get_from(h));
	to_addr=linphone_address_new(sal_op_get_to(h));
Simon Morlat's avatar
Simon Morlat committed
323

324 325 326 327 328 329 330 331 332 333 334
	if (sal_op_get_privacy(h) == SalPrivacyNone) {
		from_address_to_search_if_me=linphone_address_clone(from_addr);
	} else if (p_asserted_id) {
		from_address_to_search_if_me  = linphone_address_new(p_asserted_id);
	} else {
		ms_warning ("Hidden from identity, don't know if it's me");
	}

	if (from_address_to_search_if_me && already_a_call_with_remote_address(lc,from_address_to_search_if_me)){
		char *addr = linphone_address_as_string(from_addr);
		ms_warning("Receiving a call while one with same address [%s] is initiated, refusing this one with busy message.",addr);
Simon Morlat's avatar
Simon Morlat committed
335
		sal_call_decline(h,SalReasonBusy,NULL);
336
		sal_op_release(h);
Simon Morlat's avatar
Simon Morlat committed
337 338 339
		linphone_address_unref(from_addr);
		linphone_address_unref(to_addr);
		linphone_address_unref(from_address_to_search_if_me);
340
		ms_free(addr);
Simon Morlat's avatar
Simon Morlat committed
341
		return;
342
	} else if (from_address_to_search_if_me) {
Simon Morlat's avatar
Simon Morlat committed
343
		linphone_address_unref(from_address_to_search_if_me);
Simon Morlat's avatar
Simon Morlat committed
344
	}
345

Simon Morlat's avatar
Simon Morlat committed
346
	call=linphone_call_new_incoming(lc,from_addr,to_addr,h);
347

348
	linphone_call_make_local_media_description(call);
349 350 351 352 353 354 355 356 357
	sal_call_set_local_media_description(call->op,call->localdesc);
	md=sal_call_get_final_media_description(call->op);
	if (md){
		if (sal_media_description_empty(md) || linphone_core_incompatible_security(lc,md)){
			sal_call_decline(call->op,SalReasonNotAcceptable,NULL);
			linphone_call_unref(call);
			return;
		}
	}
358

359
	/* the call is acceptable so we can now add it to our list */
Simon Morlat's avatar
Simon Morlat committed
360
	linphone_core_add_call(lc,call);
361
	linphone_call_ref(call); /*prevent the call from being destroyed while we are notifying, if the user declines within the state callback */
362

Simon Morlat's avatar
Simon Morlat committed
363
	call->bg_task_id=sal_begin_background_task("liblinphone call notification", NULL, NULL);
364

365
	if (call->defer_notify_incoming) {
366 367 368
		/* Defer ringing until the end of the ICE candidates gathering process. */
		ms_message("Defer ringing to gather ICE candidates");
		return;
369
	}
370
#ifdef BUILD_UPNP
Yann Diorcet's avatar
Yann Diorcet committed
371 372 373 374 375
	if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseUpnp) && (call->upnp_session != NULL)) {
		/* Defer ringing until the end of the ICE candidates gathering process. */
		ms_message("Defer ringing to gather uPnP candidates");
		return;
	}
376
#endif //BUILD_UPNP
377

378
	linphone_core_notify_incoming_call(lc,call);
379 380
}

381 382 383 384 385
static void try_early_media_forking(LinphoneCall *call, SalMediaDescription *md){
	SalMediaDescription *cur_md=call->resultdesc;
	int i;
	SalStreamDescription *ref_stream,*new_stream;
	ms_message("Early media response received from another branch, checking if media can be forked to this new destination.");
386

387
	for (i=0;i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS;++i){
388
		if (!sal_stream_description_active(&cur_md->streams[i])) continue;
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
		ref_stream=&cur_md->streams[i];
		new_stream=&md->streams[i];
		if (ref_stream->type==new_stream->type && ref_stream->payloads && new_stream->payloads){
			PayloadType *refpt, *newpt;
			refpt=(PayloadType*)ref_stream->payloads->data;
			newpt=(PayloadType*)new_stream->payloads->data;
			if (strcmp(refpt->mime_type,newpt->mime_type)==0 && refpt->clock_rate==newpt->clock_rate
				&& payload_type_get_number(refpt)==payload_type_get_number(newpt)){
				MediaStream *ms=NULL;
				if (ref_stream->type==SalAudio){
					ms=(MediaStream*)call->audiostream;
				}else if (ref_stream->type==SalVideo){
					ms=(MediaStream*)call->videostream;
				}
				if (ms){
					RtpSession *session=ms->sessions.rtp_session;
					const char *rtp_addr=new_stream->rtp_addr[0]!='\0' ? new_stream->rtp_addr : md->addr;
					const char *rtcp_addr=new_stream->rtcp_addr[0]!='\0' ? new_stream->rtcp_addr : md->addr;
407 408 409
					if (ms_is_multicast(rtp_addr))
						ms_message("Multicast addr [%s/%i] does not need auxiliary rtp's destination for call [%p]",
							   rtp_addr,new_stream->rtp_port,call);
410 411
					else
						rtp_session_add_aux_remote_addr_full(session,rtp_addr,new_stream->rtp_port,rtcp_addr,new_stream->rtcp_port);
412 413 414 415 416 417
				}
			}
		}
	}
}

418 419 420 421 422 423 424 425
static void start_remote_ring(LinphoneCore *lc, LinphoneCall *call) {
	if (lc->sound_conf.play_sndcard!=NULL){
		MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
		if (call->localdesc->streams[0].max_rate>0) ms_snd_card_set_preferred_sample_rate(ringcard, call->localdesc->streams[0].max_rate);
		/*we release sound before playing ringback tone*/
		if (call->audiostream)
			audio_stream_unprepare_sound(call->audiostream);
		if( lc->sound_conf.remote_ring ){
426
			lc->ringstream=ring_start(lc->factory, lc->sound_conf.remote_ring,2000,ringcard);
427 428 429 430
		}
	}
}

431
static void call_ringing(SalOp *h){
432
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
433
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
Simon Morlat's avatar
Simon Morlat committed
434
	SalMediaDescription *md;
435

Simon Morlat's avatar
Simon Morlat committed
436
	if (call==NULL) return;
437

438
	/*set privacy*/
439
	call->current_params->privacy=(LinphonePrivacyMask)sal_op_get_privacy(call->op);
440

441
	linphone_core_notify_display_status(lc,_("Remote ringing."));
442

Simon Morlat's avatar
Simon Morlat committed
443 444
	md=sal_call_get_final_media_description(h);
	if (md==NULL){
445
		linphone_core_stop_dtmf_stream(lc);
446 447 448 449
		if (call->state==LinphoneCallOutgoingEarlyMedia){
			/*already doing early media */
			return;
		}
450
		if (lc->ringstream == NULL) start_remote_ring(lc, call);
451
		ms_message("Remote ringing...");
452
		linphone_core_notify_display_status(lc,_("Remote ringing..."));
453
		linphone_call_set_state(call,LinphoneCallOutgoingRinging,"Remote ringing");
Simon Morlat's avatar
Simon Morlat committed
454
	}else{
455 456
		/*initialize the remote call params by invoking linphone_call_get_remote_params(). This is useful as the SDP may not be present in the 200Ok*/
		linphone_call_get_remote_params(call);
Simon Morlat's avatar
Simon Morlat committed
457
		/*accept early media */
458
		if ((call->audiostream && audio_stream_started(call->audiostream))
Simon Morlat's avatar
Simon Morlat committed
459 460 461 462
#ifdef VIDEO_ENABLED
			|| (call->videostream && video_stream_started(call->videostream))
#endif
			) {
Simon Morlat's avatar
Simon Morlat committed
463
			/*streams already started */
464
			try_early_media_forking(call,md);
465 466 467 468 469 470 471
			#ifdef VIDEO_ENABLED
			if (call->videostream){
				/*just request for iframe*/
				video_stream_send_vfu(call->videostream);
			}
			#endif
		return;
Simon Morlat's avatar
Simon Morlat committed
472
		}
473

474 475
		linphone_core_notify_show_interface(lc);
		linphone_core_notify_display_status(lc,_("Early media."));
476
		linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
477
		linphone_core_stop_ringing(lc);
Simon Morlat's avatar
Simon Morlat committed
478
		ms_message("Doing early media...");
479
		linphone_core_update_streams(lc,call,md, call->state);
480 481 482 483
		if ((linphone_call_params_get_audio_direction(linphone_call_get_current_params(call)) == LinphoneMediaDirectionInactive) && call->audiostream) {
			if (lc->ringstream != NULL) return; /* Already ringing! */
			start_remote_ring(lc, call);
		}
Simon Morlat's avatar
Simon Morlat committed
484
	}
485 486
}

487 488 489
static void start_pending_refer(LinphoneCall *call){
	linphone_core_start_refered_call(call->core, call,NULL);
}
490

491 492 493 494 495
static void process_call_accepted(LinphoneCore *lc, LinphoneCall *call, SalOp *op){
	SalMediaDescription *md, *rmd;
	LinphoneCallState next_state = LinphoneCallIdle;
	const char *next_state_str = NULL;
	LinphoneTaskList tl;
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	switch (call->state){/*immediately notify the connected state, even if errors occur after*/
		case LinphoneCallOutgoingProgress:
		case LinphoneCallOutgoingRinging:
		case LinphoneCallOutgoingEarlyMedia:
			/*immediately notify the connected state*/
			linphone_call_set_state(call,LinphoneCallConnected,"Connected");
			{
				char *tmp=linphone_call_get_remote_address_as_string (call);
				char *msg=ms_strdup_printf(_("Call answered by %s"),tmp);
				linphone_core_notify_display_status(lc,msg);
				ms_free(tmp);
				ms_free(msg);
			}
		break;
		default:
		break;
Simon Morlat's avatar
Simon Morlat committed
513
	}
514

515
	linphone_task_list_init(&tl);
516
	rmd=sal_call_get_remote_media_description(op);
517
	/*set privacy*/
518
	call->current_params->privacy=(LinphonePrivacyMask)sal_op_get_privacy(call->op);
519 520 521
	/*reset the internal call update flag, so it doesn't risk to be copied and used in further re-INVITEs*/
	if (call->params->internal_call_update)
		call->params->internal_call_update = FALSE;
522

523

Yann Diorcet's avatar
Yann Diorcet committed
524
#ifdef BUILD_UPNP
525 526
	if (call->upnp_session != NULL && rmd) {
		linphone_core_update_upnp_from_remote_media_description(call, rmd);
Yann Diorcet's avatar
Yann Diorcet committed
527 528
	}
#endif //BUILD_UPNP
529

530
	md=sal_call_get_final_media_description(op);
531 532 533 534 535 536 537 538
	if (md == NULL && call->prevstate == LinphoneCallOutgoingEarlyMedia && call->resultdesc != NULL){
		ms_message("Using early media SDP since none was received with the 200 OK");
		md = call->resultdesc;
	}
	if (md && (sal_media_description_empty(md) || linphone_core_incompatible_security(lc,md))){
		md = NULL;
	}
	if (md){ /*there is a valid SDP in the response, either offer or answer, and we're able to start/update the streams*/
539

Simon Morlat's avatar
Simon Morlat committed
540 541 542 543
		/* Handle remote ICE attributes if any. */
		if (call->ice_session != NULL && rmd) {
			linphone_call_update_ice_from_remote_media_description(call, rmd, FALSE);
		}
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558
		switch (call->state){
			case LinphoneCallResuming:
				linphone_core_notify_display_status(lc,_("Call resumed."));
			/*intentionally no break*/
			case LinphoneCallConnected:
				if (call->referer) linphone_core_notify_refer_state(lc,call->referer,call);
			/*intentionally no break*/
			case LinphoneCallUpdating:
			case LinphoneCallUpdatedByRemote:
				if (!sal_media_description_has_dir(call->localdesc, SalStreamInactive) &&
					(sal_media_description_has_dir(md,SalStreamRecvOnly) ||
					sal_media_description_has_dir(md,SalStreamInactive))){
					next_state = LinphoneCallPausedByRemote;
					next_state_str = "Call paused by remote";
Simon Morlat's avatar
Simon Morlat committed
559
				}else{
560
					if (!call->params->in_conference)
561 562 563
						lc->current_call=call;
					next_state = LinphoneCallStreamsRunning;
					next_state_str = "Streams running";
Simon Morlat's avatar
Simon Morlat committed
564
				}
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
			break;
			case LinphoneCallEarlyUpdating:
				next_state_str = "Early update accepted";
				next_state = call->prevstate;
			break;
			case LinphoneCallPausing:
				/*when we entered the pausing state, we always reach the paused state whatever the content of the remote SDP is.
					Our streams are all send-only (with music), soundcard and camera are never used*/
				next_state = LinphoneCallPaused;
				next_state_str = "Call paused";
				if (call->refer_pending)
					linphone_task_list_add(&tl, (LinphoneCoreIterateHook)start_pending_refer, call);
			break;
			default:
				ms_error("call_accepted(): don't know what to do in state [%s]", linphone_call_state_to_string(call->state));
			break;
		}
582

583 584 585 586
		if (next_state != LinphoneCallIdle){
			linphone_call_update_remote_session_id_and_ver(call);
			linphone_core_update_ice_state_in_call_stats(call);
			linphone_core_update_streams(lc, call, md, next_state);
587
			linphone_call_fix_call_parameters(call, rmd);
588 589 590
			linphone_call_set_state(call, next_state, next_state_str);
		}else{
			ms_error("BUG: next_state is not set in call_accepted(), current state is %s", linphone_call_state_to_string(call->state));
Simon Morlat's avatar
Simon Morlat committed
591
		}
592
	}else{ /*invalid or no SDP*/
593
		switch (call->prevstate){
594
			/*send a bye only in case of early states*/
595 596 597 598
			case LinphoneCallOutgoingInit:
			case LinphoneCallOutgoingProgress:
			case LinphoneCallOutgoingRinging:
			case LinphoneCallOutgoingEarlyMedia:
599 600 601
			case LinphoneCallIncomingReceived:
			case LinphoneCallIncomingEarlyMedia:
				ms_error("Incompatible SDP answer received, need to abort the call");
602 603 604 605
				linphone_core_abort_call(lc,call,_("Incompatible, check codecs or security settings..."));
				break;
			/*otherwise we are able to resume previous state*/
			default:
606 607 608
				ms_error("Incompatible SDP answer received");
				switch(call->state) {
					case LinphoneCallPausedByRemote:
609
						break;
610
					case LinphoneCallPaused:
611
						break;
612
					case LinphoneCallStreamsRunning:
613
						break;
614 615 616 617 618
					default:
						ms_message("Incompatible SDP answer received, restoring previous state [%s]",linphone_call_state_to_string(call->prevstate));
						linphone_call_set_state(call,call->prevstate,_("Incompatible media parameters."));
						break;
				}
619 620
				break;
		}
Simon Morlat's avatar
Simon Morlat committed
621
	}
622 623
	linphone_task_list_run(&tl);
	linphone_task_list_free(&tl);
624 625
}

626 627 628 629 630 631
/*
 * could be reach :
 *  - when the call is accepted
 *  - when a request is accepted (pause, resume)
 */
static void call_accepted(SalOp *op){
632
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
633
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
634

635 636
	if (call == NULL){
		ms_warning("call_accepted: call does no longer exist.");
Simon Morlat's avatar
Simon Morlat committed
637 638
		return ;
	}
639
	process_call_accepted(lc, call, op);
640 641
}

642
static void call_resumed(LinphoneCore *lc, LinphoneCall *call){
643
	linphone_core_notify_display_status(lc,_("We have been resumed."));
644
	_linphone_core_accept_call_update(lc,call,NULL,LinphoneCallStreamsRunning,"Connected (streams running)");
645 646 647
}

static void call_paused_by_remote(LinphoneCore *lc, LinphoneCall *call){
648
	LinphoneCallParams *params;
649

650
	/* we are being paused */
651
	linphone_core_notify_display_status(lc,_("We are paused by other party."));
652 653 654 655 656 657
	params = linphone_call_params_copy(call->params);
	if (lp_config_get_int(lc->config, "sip", "inactive_video_on_pause", 0)) {
		linphone_call_params_set_video_direction(params, LinphoneMediaDirectionInactive);
	}
	_linphone_core_accept_call_update(lc,call,params,LinphoneCallPausedByRemote,"Call paused by remote");
	linphone_call_params_unref(params);
658 659
}

660 661 662 663
static void call_updated_by_remote(LinphoneCore *lc, LinphoneCall *call){
	linphone_core_notify_display_status(lc,_("Call is updated by remote."));
	linphone_call_set_state(call, LinphoneCallUpdatedByRemote,"Call updated by remote");
	if (call->defer_update == FALSE){
664 665 666 667 668 669
		if (call->state == LinphoneCallUpdatedByRemote){
			linphone_core_accept_call_update(lc,call,NULL);
		}else{
			/*otherwise it means that the app responded by linphone_core_accept_call_update
			 * within the callback, so job is already done.*/
		}
670 671 672 673 674 675 676 677
	}else{
		if (call->state == LinphoneCallUpdatedByRemote){
			ms_message("LinphoneCall [%p]: UpdatedByRemoted was signaled but defered. LinphoneCore expects the application to call "
				"linphone_core_accept_call_update() later.", call);
		}
	}
}

jehan's avatar
jehan committed
678
/* this callback is called when an incoming re-INVITE/ SIP UPDATE modifies the session*/
679
static void call_updated(LinphoneCore *lc, LinphoneCall *call, SalOp *op, bool_t is_update){
680
	SalMediaDescription *rmd=sal_call_get_remote_media_description(op);
681

682
	call->defer_update = lp_config_get_int(lc->config, "sip", "defer_update_default", FALSE);
683

684 685 686 687
	switch(call->state){
		case LinphoneCallPausedByRemote:
			if (sal_media_description_has_dir(rmd,SalStreamSendRecv) || sal_media_description_has_dir(rmd,SalStreamRecvOnly)){
				call_resumed(lc,call);
688
			}else{
689
				call_updated_by_remote(lc, call);
690
			}
691
		break;
jehan's avatar
jehan committed
692
		/*SIP UPDATE CASE*/
693
		case LinphoneCallOutgoingRinging:
jehan's avatar
jehan committed
694
		case LinphoneCallOutgoingEarlyMedia:
695
		case LinphoneCallIncomingEarlyMedia:
696 697 698 699
			if (is_update) {
				linphone_call_set_state(call, LinphoneCallEarlyUpdatedByRemote, "EarlyUpdatedByRemote");
				_linphone_core_accept_call_update(lc,call,NULL,call->prevstate,linphone_call_state_to_string(call->prevstate));
			}
jehan's avatar
jehan committed
700
			break;
701 702
		case LinphoneCallStreamsRunning:
		case LinphoneCallConnected:
703
		case LinphoneCallUpdatedByRemote: // Can happen on UAC connectivity loss
704 705
			if (sal_media_description_has_dir(rmd,SalStreamSendOnly) || sal_media_description_has_dir(rmd,SalStreamInactive)){
				call_paused_by_remote(lc,call);
706
			}else{
707
				call_updated_by_remote(lc, call);
708
			}
709
		break;
710
		case LinphoneCallPaused:
711 712
			/*we'll remain in pause state but accept the offer anyway according to default parameters*/
			_linphone_core_accept_call_update(lc,call,NULL,call->state,linphone_call_state_to_string(call->state));
713 714 715 716
		break;
		case LinphoneCallUpdating:
		case LinphoneCallPausing:
		case LinphoneCallResuming:
717
			sal_call_decline(call->op,SalReasonInternalError,NULL);
718 719 720 721 722 723 724 725 726
			/*no break*/
		case LinphoneCallIdle:
		case LinphoneCallOutgoingInit:
		case LinphoneCallEnd:
		case LinphoneCallIncomingReceived:
		case LinphoneCallOutgoingProgress:
		case LinphoneCallRefered:
		case LinphoneCallError:
		case LinphoneCallReleased:
727 728
		case LinphoneCallEarlyUpdatedByRemote:
		case LinphoneCallEarlyUpdating:
729 730
			ms_warning("Receiving reINVITE or UPDATE while in state [%s], should not happen.",linphone_call_state_to_string(call->state));
		break;
Simon Morlat's avatar
Simon Morlat committed
731
	}
732 733
}

734 735 736 737 738 739 740 741 742 743
/* this callback is called when an incoming re-INVITE/ SIP UPDATE modifies the session*/
static void call_updating(SalOp *op, bool_t is_update){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
	SalMediaDescription *rmd=sal_call_get_remote_media_description(op);

	if (!call) {
		ms_error("call_updating(): call doesn't exist anymore");
		return ;
	}
744
	linphone_call_fix_call_parameters(call, rmd);
745 746
	if (call->state!=LinphoneCallPaused){
		/*Refresh the local description, but in paused state, we don't change anything.*/
747
		if (rmd == NULL && lp_config_get_int(call->core->config,"sip","sdp_200_ack_follow_video_policy",0)) {
748
			LinphoneCallParams *p=linphone_core_create_call_params(lc, NULL);
Ghislain MARY's avatar
Ghislain MARY committed
749
			ms_message("Applying default policy for offering SDP on call [%p]",call);
750
			linphone_call_set_new_params(call, p);
Simon Morlat's avatar
Simon Morlat committed
751
			linphone_call_params_unref(p);
752
		}
753
		linphone_call_make_local_media_description(call);
754 755 756 757 758 759 760 761 762 763
		sal_call_set_local_media_description(call->op,call->localdesc);
	}
	if (rmd == NULL){
		/* case of a reINVITE or UPDATE without SDP */
		call->expect_media_in_ack = TRUE;
		sal_call_accept(op); /*respond with an offer*/
		/*don't do anything else in this case, wait for the ACK to receive to notify the app*/
	}else {
		SalMediaDescription *md;
		SalMediaDescription *prev_result_desc=call->resultdesc;
764

765
		call->expect_media_in_ack = FALSE;
766

767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
		md=sal_call_get_final_media_description(call->op);
		if (md && (sal_media_description_empty(md) || linphone_core_incompatible_security(lc,md))){
			sal_call_decline(call->op,SalReasonNotAcceptable,NULL);
			return;
		}
		if (is_update && prev_result_desc && md){
			int diff=sal_media_description_equals(prev_result_desc,md);
			if (diff & (SAL_MEDIA_DESCRIPTION_CRYPTO_POLICY_CHANGED|SAL_MEDIA_DESCRIPTION_STREAMS_CHANGED)){
				ms_warning("Cannot accept this update, it is changing parameters that require user approval");
				sal_call_decline(call->op,SalReasonNotAcceptable,NULL); /*FIXME should send 504 Cannot change the session parameters without prompting the user"*/
				return;
			}
		}
		call_updated(lc, call, op, is_update);
	}
}


static void call_ack(SalOp *op){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
788

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
	if (call == NULL){
		ms_warning("call_ack(): no call for which an ack is expected");
		return;
	}
	if (call->expect_media_in_ack){
		switch(call->state){
			case LinphoneCallStreamsRunning:
			case LinphoneCallPausedByRemote:
				linphone_call_set_state(call, LinphoneCallUpdatedByRemote, "UpdatedByRemote");
			break;
			default:
			break;
		}
		process_call_accepted(lc, call, op);
	}
}

806 807
static void call_terminated(SalOp *op, const char *from){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
808
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
Simon Morlat's avatar
Simon Morlat committed
809 810

	if (call==NULL) return;
811

812 813 814
	switch(linphone_call_get_state(call)){
		case LinphoneCallEnd:
		case LinphoneCallError:
815
			ms_warning("call_terminated: already terminated, ignoring.");
816 817 818 819
			return;
		break;
		case LinphoneCallIncomingReceived:
		case LinphoneCallIncomingEarlyMedia:
820
			sal_error_info_set(&call->non_op_error,SalReasonRequestTimeout,0,"Incoming call cancelled",NULL);
821 822 823
		break;
		default:
		break;
Simon Morlat's avatar
Simon Morlat committed
824 825
	}
	ms_message("Current call terminated...");
826
	if (call->refer_pending){
827
		linphone_core_start_refered_call(lc,call,NULL);
828
	}
Simon Morlat's avatar
Simon Morlat committed
829
	//we stop the call only if we have this current call or if we are in call
830
	if ((bctbx_list_size(lc->calls)  == 1) || linphone_core_in_call(lc)) {
831
		linphone_core_stop_ringing(lc);
Simon Morlat's avatar
Simon Morlat committed
832
	}
833
	linphone_call_stop_media_streams(call);
834 835
	linphone_core_notify_show_interface(lc);
	linphone_core_notify_display_status(lc,_("Call terminated."));
836

Yann Diorcet's avatar
Yann Diorcet committed
837 838 839 840
#ifdef BUILD_UPNP
	linphone_call_delete_upnp_session(call);
#endif //BUILD_UPNP

841
	linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
842 843
}

844 845 846
static int resume_call_after_failed_transfer(LinphoneCall *call){
	if (call->was_automatically_paused && call->state==LinphoneCallPausing)
		return BELLE_SIP_CONTINUE; /*was still in pausing state*/
847

848 849 850 851
	if (call->was_automatically_paused && call->state==LinphoneCallPaused){
		if (sal_op_is_idle(call->op)){
			linphone_core_resume_call(call->core,call);
		}else {
852
			ms_message("resume_call_after_failed_transfer(), salop was busy");
853 854 855 856 857 858 859
			return BELLE_SIP_CONTINUE;
		}
	}
	linphone_call_unref(call);
	return BELLE_SIP_STOP;
}

860
static void call_failure(SalOp *op){
861
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
862
	const SalErrorInfo *ei=sal_op_get_error_info(op);
Simon Morlat's avatar
Simon Morlat committed
863 864 865 866 867
	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.");
868
	const char *msg=ei->full_string;
Simon Morlat's avatar
Simon Morlat committed
869
	LinphoneCall *referer;
870
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
Simon Morlat's avatar
Simon Morlat committed
871
	
872
	if (call==NULL){
Simon Morlat's avatar
Simon Morlat committed
873
		ms_warning("Call faillure reported on already terminated call.");
874 875
		return ;
	}
Simon Morlat's avatar
Simon Morlat committed
876 877
	
	referer=call->referer;
878

879
	linphone_core_notify_show_interface(lc);
880 881 882 883 884
	switch(ei->reason){
		case SalReasonNone:
		break;
		case SalReasonRequestTimeout:
			msg=_("Request timeout.");
885
			linphone_core_notify_display_status(lc,msg);
886 887 888
		break;
		case SalReasonDeclined:
			msg=msg603;
889
			linphone_core_notify_display_status(lc,msg603);
890 891 892
		break;
		case SalReasonBusy:
			msg=msg486;
893
			linphone_core_notify_display_status(lc,msg486);
894 895 896 897 898 899 900 901 902 903 904 905 906
		break;
		case SalReasonRedirect:
		{
			linphone_call_stop_media_streams(call);
			if (	call->state==LinphoneCallOutgoingInit
					|| call->state==LinphoneCallOutgoingProgress
					|| call->state==LinphoneCallOutgoingRinging /*push case*/
					|| call->state==LinphoneCallOutgoingEarlyMedia){
				LinphoneAddress* redirection_to = (LinphoneAddress*)sal_op_get_remote_contact_address(call->op);
				if( redirection_to ){
					char* url = linphone_address_as_string(redirection_to);
					ms_warning("Redirecting call [%p] to %s",call, url);
					ms_free(url);
907 908 909 910 911
					if( call->log->to != NULL ) {
						linphone_address_unref(call->log->to);
					}
					call->log->to = linphone_address_ref(redirection_to);
					linphone_core_restart_invite(lc, call);
912 913 914 915
					return;
				}
			}
			msg=_("Redirected");
916
			linphone_core_notify_display_status(lc,msg);
917 918 919 920
		}
		break;
		case SalReasonTemporarilyUnavailable:
			msg=msg480;
921
			linphone_core_notify_display_status(lc,msg480);
922 923
		break;
		case SalReasonNotFound:
924
			linphone_core_notify_display_status(lc,msg);
925 926 927
		break;
		case SalReasonDoNotDisturb:
			msg=msg600;
928
			linphone_core_notify_display_status(lc,msg600);
929 930 931
		break;
		case SalReasonUnsupportedContent: /*<this is for compatibility: linphone sent 415 because of SDP offer answer failure*/
		case SalReasonNotAcceptable:
932 933 934 935 936
			ms_message("Outgoing call [%p] failed with SRTP and/or AVPF enabled", call);
			if ((call->state == LinphoneCallOutgoingInit)
				|| (call->state == LinphoneCallOutgoingProgress)
				|| (call->state == LinphoneCallOutgoingRinging) /* Push notification case */
				|| (call->state == LinphoneCallOutgoingEarlyMedia)) {
937
				int i;
938 939
				for (i = 0; i < call->localdesc->nb_streams; i++) {
					if (!sal_stream_description_active(&call->localdesc->streams[i])) continue;
940 941
					if (call->params->media_encryption == LinphoneMediaEncryptionSRTP) {
						if (call->params->avpf_enabled == TRUE) {
942
							if (i == 0) ms_message("Retrying call [%p] with SAVP", call);
943
							call->params->avpf_enabled = FALSE;
944 945 946 947
							linphone_core_restart_invite(lc, call);
							return;
						} else if (!linphone_core_is_media_encryption_mandatory(lc)) {
							if (i == 0) ms_message("Retrying call [%p] with AVP", call);
948
							call->params->media_encryption = LinphoneMediaEncryptionNone;
949 950 951 952
							memset(call->localdesc->streams[i].crypto, 0, sizeof(call->localdesc->streams[i].crypto));
							linphone_core_restart_invite(lc, call);
							return;
						}
953
					} else if (call->params->avpf_enabled == TRUE) {
954
						if (i == 0) ms_message("Retrying call [%p] with AVP", call);
955
						call->params->avpf_enabled = FALSE;
956 957
						linphone_core_restart_invite(lc, call);
						return;
958 959 960
					}
				}
			}
961
			msg=_("Incompatible media parameters.");
962
			linphone_core_notify_display_status(lc,msg);
963 964
		break;
		default:
965
			linphone_core_notify_display_status(lc,_("Call failed."));
Simon Morlat's avatar
Simon Morlat committed
966
	}
967

968
	/*some call errors are not fatal*/
969 970 971 972
	switch (call->state) {
	case LinphoneCallUpdating:
	case LinphoneCallPausing:
	case LinphoneCallResuming:
973 974 975 976 977
		if (ei->reason != SalReasonNoMatch){
			ms_message("Call error on state [%s], restoring previous state",linphone_call_state_to_string(call->prevstate));
			linphone_call_set_state(call, call->prevstate,ei->full_string);
			return;
		}
978 979 980 981
	default:
		break; /*nothing to do*/
	}

982
	linphone_core_stop_ringing(lc);
983
	linphone_call_stop_media_streams(call);
984 985 986 987

#ifdef BUILD_UPNP
	linphone_call_delete_upnp_session(call);
#endif //BUILD_UPNP
988

989 990 991 992
	if (call->state!=LinphoneCallEnd && call->state!=LinphoneCallError){
		if (ei->reason==SalReasonDeclined){
			linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
		}else{
993 994 995 996 997
			if (linphone_call_state_is_early(call->state)){
				linphone_call_set_state(call,LinphoneCallError,ei->full_string);
			}else{
				linphone_call_set_state(call, LinphoneCallEnd, ei->full_string);
			}
998 999
		}
		if (ei->reason!=SalReasonNone) linphone_core_play_call_error_tone(lc,linphone_reason_from_sal(ei->reason));
1000
	}
1001

1002
	if (referer){
1003
		/*notify referer of the failure*/
1004
		linphone_core_notify_refer_state(lc,referer,call);
1005 1006
		/*schedule automatic resume of the call. This must be done only after the notifications are completed due to dialog serialization of requests.*/
		linphone_core_queue_task(lc,(belle_sip_source_func_t)resume_call_after_failed_transfer,linphone_call_ref(referer),"Automatic call resuming after failed transfer");
1007
	}
1008 1009
}

1010 1011
static void call_released(SalOp *op){
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
1012 1013
	if (call!=NULL){
		linphone_call_set_state(call,LinphoneCallReleased,"Call released");
Simon Morlat's avatar
Simon Morlat committed
1014 1015 1016 1017 1018
	}else{
		/*we can arrive here when the core manages call at Sal level without creating a LinphoneCall object. Typicially:
		 * - when declining an incoming call with busy because maximum number of calls is reached.
		 */
	}
1019 1020
}

1021 1022 1023 1024 1025 1026 1027 1028
static void call_cancel_done(SalOp *op) {
	LinphoneCall *call = (LinphoneCall *)sal_op_get_user_pointer(op);
	if (call->reinvite_on_cancel_response_requested == TRUE) {
		call->reinvite_on_cancel_response_requested = FALSE;
		linphone_call_reinvite_to_recover_from_connection_loss(call);
	}
}

jehan's avatar
jehan committed
1029
static void auth_failure(SalOp *op, SalAuthInfo* info) {
1030 1031
	LinphoneCore *lc = (LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneAuthInfo *ai = NULL;
1032

1033 1034
	if (info != NULL) {
		ai = (LinphoneAuthInfo*)_linphone_core_find_auth_info(lc, info->realm, info->username, info->domain, TRUE);
1035
		if (ai){
1036
			LinphoneAuthMethod method = info->mode == SalAuthModeHttpDigest ? LinphoneAuthHttpDigest : LinphoneAuthTls;
1037
			LinphoneAuthInfo *auth_info = linphone_core_create_auth_info(lc, info->username, NULL, NULL, NULL, info->realm, info->domain);
1038
			ms_message("%s/%s/%s/%s authentication fails.", info->realm, info->username, info->domain, info->mode == SalAuthModeHttpDigest ? "HttpDigest" : "Tls");
1039
			/*ask again for password if auth info was already supplied but apparently not working*/
1040 1041 1042 1043
			linphone_core_notify_authentication_requested(lc, auth_info, method);
			linphone_auth_info_destroy(auth_info);
			// Deprecated
			linphone_core_notify_auth_info_requested(lc, info->realm, info->username, info->domain);
1044
		}
jehan's avatar
jehan committed
1045
	}
1046
}
1047

1048
static void register_success(SalOp *op, bool_t registered){
1049 1050 1051
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
	char *msg;
1052

1053 1054
	if (!cfg){
		ms_message("Registration success for deleted proxy config, ignored");
1055 1056
		return;
	}
1057
	linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
1058
									registered ? "Registration successful" : "Unregistration done");
1059
	{
1060
		if (registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
1061
		else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
1062
		linphone_core_notify_display_status(lc,msg);
1063 1064
		ms_free(msg);
	}
1065

1066 1067
}

1068
static void register_failure(SalOp *op){
1069
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
1070
	LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
1071 1072
	const SalErrorInfo *ei=sal_op_get_error_info(op);
	const char *details=ei->full_string;
1073 1074 1075 1076 1077 1078 1079

	if (cfg==NULL){
		ms_warning("Registration failed for unknown proxy config.");
		return ;
	}
	if (details==NULL)
		details=_("no response timeout");
1080

1081
	{
1082
		char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op), details);
1083
		linphone_core_notify_display_status(lc,msg);
1084 1085
		ms_free(msg);
	}
1086

1087
	if ((ei->reason == SalReasonServiceUnavailable || ei->reason == SalReasonIOError)
1088 1089 1090 1091 1092
			&& linphone_proxy_config_get_state(cfg) == LinphoneRegistrationOk) {
		linphone_proxy_config_set_state(cfg,LinphoneRegistrationProgress,_("Service unavailable, retrying"));
	} else {
		linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
	}
1093
	if (cfg->long_term_event){
1094
		/*prevent publish to be sent now until registration gets successful*/
1095 1096 1097
		linphone_event_terminate(cfg->long_term_event);
		linphone_event_unref(cfg->long_term_event);
		cfg->long_term_event=NULL;
1098 1099
		cfg->send_publish=cfg->publish;
	}
1100 1101 1102
}

static void vfu_request(SalOp *op){
1103
#ifdef VIDEO_ENABLED
1104
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
1105 1106 1107 1108 1109 1110
	if (call==NULL){
		ms_warning("VFU request but no call !");
		return ;
	}
	if (call->videostream)
		video_stream_send_vfu(call->videostream);
1111
#endif
1112 1113 1114
}

static void dtmf_received(SalOp *op, char dtmf){
1115
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
1116
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
1117
	if (!call) return;
1118
	linphone_core_notify_dtmf_received(lc, call, dtmf);
1119 1120
}

1121 1122
static void refer_received(Sal *sal, SalOp *op, const char *referto){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
1123
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
1124 1125
	LinphoneAddress *refer_to_addr = linphone_address_new(referto);
	char method[20] = "";
1126

1127 1128 1129
	if(refer_to_addr) {
		const char *tmp = linphone_address_get_method_param(refer_to_addr);
		if(tmp) strncpy(method, tmp, sizeof(method));
Simon Morlat's avatar
Simon Morlat committed
1130
		linphone_address_unref(refer_to_addr);
1131 1132
	}
	if (call && (strlen(method) == 0 || strcmp(method, "INVITE") == 0)) {