callbacks.c 40.4 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
/*
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.
*/


Sylvain Berfini's avatar
Sylvain Berfini committed
21
#include "sal/sal.h"
Simon Morlat's avatar
Simon Morlat committed
22 23 24

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

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

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

37 38 39
static int media_parameters_changed(LinphoneCall *call, SalMediaDescription *oldmd, SalMediaDescription *newmd) {
	if (call->params.in_conference != call->current_params.in_conference) return SAL_MEDIA_DESCRIPTION_CHANGED;
	if (call->up_bw != linphone_core_get_upload_bandwidth(call->core)) return SAL_MEDIA_DESCRIPTION_CHANGED;
40 41
	if (call->localdesc_changed) ms_message("Local description has changed: %i", call->localdesc_changed);
	return call->localdesc_changed | sal_media_description_equals(oldmd, newmd);
42 43 44 45 46 47 48 49
}

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;

50
	for (i = 0; i < new_md->n_active_streams; i++) {
51 52 53 54 55 56 57 58 59 60
		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);
Ghislain MARY's avatar
Ghislain MARY committed
61
		rtp_session_set_remote_addr_full(call->audiostream->ms.session, rtp_addr, new_audiodesc->rtp_port, rtcp_addr, new_audiodesc->rtcp_port);
62 63 64 65 66 67
	}
#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);
Ghislain MARY's avatar
Ghislain MARY committed
68
		rtp_session_set_remote_addr_full(call->videostream->ms.session, rtp_addr, new_videodesc->rtp_port, rtcp_addr, new_videodesc->rtcp_port);
69
	}
70 71
#else
	(void)new_videodesc;
72
#endif
Simon Morlat's avatar
Simon Morlat committed
73 74
}

75 76
void linphone_core_update_streams(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription *new_md){
	SalMediaDescription *oldmd=call->resultdesc;
77 78
	bool_t all_muted=FALSE;
	bool_t send_ringbacktone=FALSE;
79
	
80
	linphone_core_stop_ringing(lc);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	if (!new_md) {
		ms_error("linphone_core_update_streams() called with null media description");
		return;
	}
	if (call->biggestdesc==NULL || new_md->n_total_streams>call->biggestdesc->n_total_streams){
		/*we have been offered and now are ready to proceed, or we added a new stream*/
		/*store the media description to remember the mapping of calls*/
		if (call->biggestdesc){
			sal_media_description_unref(call->biggestdesc);
			call->biggestdesc=NULL;
		}
		if (sal_call_is_offerer(call->op))
			call->biggestdesc=sal_media_description_ref(call->localdesc);
		else
			call->biggestdesc=sal_media_description_ref(sal_call_get_remote_media_description(call->op));
96
	}
97 98
	sal_media_description_ref(new_md);
	call->expect_media_in_ack=FALSE;
99
	call->resultdesc=new_md;
Ghislain MARY's avatar
Ghislain MARY committed
100
	if ((call->audiostream && call->audiostream->ms.ticker) || (call->videostream && call->videostream->ms.ticker)){
101 102
		/* we already started media: check if we really need to restart it*/
		if (oldmd){
103
			int md_changed = media_parameters_changed(call, oldmd, new_md);
104 105 106 107 108 109 110 111 112
			if ((md_changed & SAL_MEDIA_DESCRIPTION_CODEC_CHANGED) || call->playing_ringbacktone) {
				ms_message("Media descriptions are different, need to restart the streams.");
			} 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 */
						linphone_call_enable_camera (call,linphone_call_camera_enabled (call));
						if (call->audiostream)
113
							linphone_core_enable_mic(lc, linphone_core_mic_enabled(lc));
Simon Morlat's avatar
Simon Morlat committed
114
#ifdef VIDEO_ENABLED
115
						if (call->videostream && call->camera_enabled)
116
							video_stream_change_camera(call->videostream,lc->video_conf.device );
Simon Morlat's avatar
Simon Morlat committed
117
#endif
118 119
					}
					ms_message("No need to restart streams, SDP is unchanged.");
120
					goto end;
Simon Morlat's avatar
Simon Morlat committed
121
				}else {
122 123 124 125 126 127 128 129
					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);
					}
					if (md_changed & SAL_MEDIA_DESCRIPTION_CRYPTO_CHANGED) {
						ms_message("Crypto parameters have changed, update them.");
						linphone_call_update_crypto_parameters(call, oldmd, new_md);
					}
130
					goto end;
131 132 133 134 135 136 137
				}
			}
		}
		linphone_call_stop_media_streams (call);
		linphone_call_init_media_streams (call);
	}
	
138 139 140 141 142 143
	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);
	}
	if (call->state==LinphoneCallIncomingEarlyMedia && linphone_core_get_remote_ringback_tone (lc)!=NULL){
		send_ringbacktone=TRUE;
144
	}
145 146 147 148 149
	if (call->state==LinphoneCallIncomingEarlyMedia ||
		(call->state==LinphoneCallOutgoingEarlyMedia && !call->params.real_early_media)){
		all_muted=TRUE;
	}
	linphone_call_start_media_streams(call,all_muted,send_ringbacktone);
150 151 152
	if (call->state==LinphoneCallPausing && call->paused_by_app && ms_list_size(lc->calls)==1){
		linphone_core_play_named_tone(lc,LinphoneToneCallOnHold);
	}
153 154 155 156
	end:
	if (oldmd) 
		sal_media_description_unref(oldmd);
	
Simon Morlat's avatar
Simon Morlat committed
157
}
158
#if 0
Simon Morlat's avatar
Simon Morlat committed
159 160 161 162
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;
163 164
		if (linphone_address_weak_equal(call->log->from,from) &&
		    linphone_address_weak_equal(call->log->to, to)){
Simon Morlat's avatar
Simon Morlat committed
165 166 167 168 169
			return TRUE;
		}
	}
	return FALSE;
}
170 171
#endif

172
static bool_t already_a_call_with_remote_address(const LinphoneCore *lc, const LinphoneAddress *remote) {
Sylvain Berfini's avatar
Sylvain Berfini committed
173
	MSList *elem;
174 175 176 177 178 179 180 181 182 183 184 185 186
	ms_warning(" searching for already_a_call_with_remote_address.");

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

187 188 189 190 191 192 193 194 195 196 197 198 199 200
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
201

202
static void call_received(SalOp *h){
203
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
Simon Morlat's avatar
Simon Morlat committed
204 205
	LinphoneCall *call;
	const char *from,*to;
206
	char *alt_contact;
Simon Morlat's avatar
Simon Morlat committed
207
	LinphoneAddress *from_addr, *to_addr;
208
	bool_t prevent_colliding_calls=lp_config_get_int(lc->config,"sip","prevent_colliding_calls",TRUE);
209
	
Simon Morlat's avatar
Simon Morlat committed
210
	/* first check if we can answer successfully to this invite */
211
	if (linphone_presence_model_get_basic_status(lc->presence_model) == LinphonePresenceBasicStatusClosed) {
212 213 214 215 216 217 218 219 220 221 222 223
		LinphonePresenceActivity *activity = linphone_presence_model_get_activity(lc->presence_model);
		switch (linphone_presence_activity_get_type(activity)) {
			case LinphonePresenceActivityBusy:
				sal_call_decline(h,SalReasonBusy,NULL);
				break;
			case LinphonePresenceActivityAppointment:
			case LinphonePresenceActivityMeeting:
			case LinphonePresenceActivityOffline:
			case LinphonePresenceActivityWorship:
				sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
				break;
			case LinphonePresenceActivityPermanentAbsence:
224 225 226 227 228
				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);
				}
229 230 231
				break;
			default:
				break;
232
		}
233 234
		sal_op_release(h);
		return;
Simon Morlat's avatar
Simon Morlat committed
235
	}
236

237
	if (!linphone_core_can_we_add_call(lc)){/*busy*/
Simon Morlat's avatar
Simon Morlat committed
238
		sal_call_decline(h,SalReasonBusy,NULL);
239
		sal_op_release(h);
Simon Morlat's avatar
Simon Morlat committed
240 241
		return;
	}
242 243
	from=sal_op_get_from(h);
	to=sal_op_get_to(h);
Simon Morlat's avatar
Simon Morlat committed
244 245 246
	from_addr=linphone_address_new(from);
	to_addr=linphone_address_new(to);

247
	if ((already_a_call_with_remote_address(lc,from_addr) && prevent_colliding_calls) || already_a_call_pending(lc)){
248
		ms_warning("Receiving another call while one is ringing or initiated, refusing this one with busy message.");
Simon Morlat's avatar
Simon Morlat committed
249
		sal_call_decline(h,SalReasonBusy,NULL);
Simon Morlat's avatar
Simon Morlat committed
250
		sal_op_release(h);
Simon Morlat's avatar
Simon Morlat committed
251 252 253 254
		linphone_address_destroy(from_addr);
		linphone_address_destroy(to_addr);
		return;
	}
Simon Morlat's avatar
Simon Morlat committed
255
	
Simon Morlat's avatar
Simon Morlat committed
256 257
	call=linphone_call_new_incoming(lc,from_addr,to_addr,h);
	
Simon Morlat's avatar
Simon Morlat committed
258
	/* the call is acceptable so we can now add it to our list */
Simon Morlat's avatar
Simon Morlat committed
259
	linphone_core_add_call(lc,call);
260
	linphone_call_ref(call); /*prevent the call from being destroyed while we are notifying, if the user declines within the state callback */
261

262
	if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && (call->ice_session != NULL)) {
263 264 265
		/* Defer ringing until the end of the ICE candidates gathering process. */
		ms_message("Defer ringing to gather ICE candidates");
		return;
266
	}
267
#ifdef BUILD_UPNP
Yann Diorcet's avatar
Yann Diorcet committed
268 269 270 271 272
	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;
	}
273
#endif //BUILD_UPNP
274

275
	linphone_core_notify_incoming_call(lc,call);
276 277 278
}

static void call_ringing(SalOp *h){
279
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
280
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(h);
Simon Morlat's avatar
Simon Morlat committed
281
	SalMediaDescription *md;
282
	
Simon Morlat's avatar
Simon Morlat committed
283
	if (call==NULL) return;
284
	
jehan's avatar
jehan committed
285 286 287
	/*set privacy*/
	call->current_params.privacy=(LinphonePrivacyMask)sal_op_get_privacy(call->op);

Simon Morlat's avatar
Simon Morlat committed
288 289
	if (lc->vtable.display_status)
		lc->vtable.display_status(lc,_("Remote ringing."));
290
	
Simon Morlat's avatar
Simon Morlat committed
291 292
	md=sal_call_get_final_media_description(h);
	if (md==NULL){
293 294
		linphone_core_stop_dtmf_stream(lc);
		if (lc->ringstream!=NULL) return;/*already ringing !*/
Simon Morlat's avatar
Simon Morlat committed
295
		if (lc->sound_conf.play_sndcard!=NULL){
296
			MSSndCard *ringcard=lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
297
			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
298 299 300
			/*we release sound before playing ringback tone*/
			if (call->audiostream)
				audio_stream_unprepare_sound(call->audiostream);
301
			lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,ringcard);
Simon Morlat's avatar
Simon Morlat committed
302
		}
303 304 305 306
		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
307 308
	}else{
		/*accept early media */
Simon Morlat's avatar
Simon Morlat committed
309
		if (call->audiostream && audio_stream_started(call->audiostream)){
Simon Morlat's avatar
Simon Morlat committed
310 311 312 313 314 315 316
			/*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."));
317
		linphone_call_set_state(call,LinphoneCallOutgoingEarlyMedia,"Early media");
318
		linphone_core_stop_ringing(lc);
Simon Morlat's avatar
Simon Morlat committed
319
		ms_message("Doing early media...");
Simon Morlat's avatar
Simon Morlat committed
320
		linphone_core_update_streams(lc,call,md);
Simon Morlat's avatar
Simon Morlat committed
321
	}
322 323
}

Simon Morlat's avatar
Simon Morlat committed
324 325 326 327 328
/*
 * could be reach :
 *  - when the call is accepted
 *  - when a request is accepted (pause, resume)
 */
Simon Morlat's avatar
Simon Morlat committed
329
static void call_accepted(SalOp *op){
330
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
331
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
332 333
	SalMediaDescription *md;
	
Simon Morlat's avatar
Simon Morlat committed
334 335
	if (call==NULL){
		ms_warning("No call to accept.");
336
		return ;
Simon Morlat's avatar
Simon Morlat committed
337
	}
jehan's avatar
jehan committed
338 339
	/*set privacy*/
	call->current_params.privacy=(LinphonePrivacyMask)sal_op_get_privacy(call->op);
340

341 342 343
	/* 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));
344
	}
Yann Diorcet's avatar
Yann Diorcet committed
345 346 347 348 349
#ifdef BUILD_UPNP
	if (call->upnp_session != NULL) {
		linphone_core_update_upnp_from_remote_media_description(call, sal_call_get_remote_media_description(op));
	}
#endif //BUILD_UPNP
350

351
	md=sal_call_get_final_media_description(op);
352
	if (md) /*make sure re-invite will not propose video again*/
353
		call->params.has_video &= linphone_core_media_description_contains_video_stream(md);
354
	
Simon Morlat's avatar
Simon Morlat committed
355 356 357 358
	if (call->state==LinphoneCallOutgoingProgress ||
	    call->state==LinphoneCallOutgoingRinging ||
	    call->state==LinphoneCallOutgoingEarlyMedia){
		linphone_call_set_state(call,LinphoneCallConnected,"Connected");
359
		if (call->referer) linphone_core_notify_refer_state(lc,call->referer,call);
Simon Morlat's avatar
Simon Morlat committed
360
	}
361
	if (md && !sal_media_description_empty(md) && !linphone_core_incompatible_security(lc,md)){
362
		linphone_call_update_remote_session_id_and_ver(call);
363 364
		if (sal_media_description_has_dir(md,SalStreamSendOnly) ||
		    sal_media_description_has_dir(md,SalStreamInactive)){
Simon Morlat's avatar
Simon Morlat committed
365 366 367 368 369 370 371
			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);
			}
372
			linphone_core_update_streams (lc,call,md);
373
			linphone_call_set_state(call,LinphoneCallPaused,"Call paused");
374
			if (call->refer_pending)
375
				linphone_core_start_refered_call(lc,call,NULL);
376
		}else if (sal_media_description_has_dir(md,SalStreamRecvOnly)){
377 378 379 380 381 382 383 384
			/*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);
			}
385
			linphone_core_update_streams (lc,call,md);
386
			linphone_call_set_state(call,LinphoneCallPausedByRemote,"Call paused by remote");
387
		}else{
388
			if (call->state!=LinphoneCallUpdating){
Simon Morlat's avatar
Simon Morlat committed
389 390 391 392 393 394 395 396 397 398 399 400 401
				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);
					}
				}
402
			}
403 404 405 406
			linphone_core_update_streams(lc,call,md);
			/*also reflect the change if the "wished" params, in order to avoid to propose SAVP or video again
			* further in the call, for example during pause,resume, conferencing reINVITEs*/
			linphone_call_fix_call_parameters(call);
407 408
			if (!call->current_params.in_conference)
				lc->current_call=call;
409
			linphone_call_set_state(call, LinphoneCallStreamsRunning, "Streams running");
Simon Morlat's avatar
Simon Morlat committed
410
		}
Simon Morlat's avatar
Simon Morlat committed
411 412 413
	}else{
		/*send a bye*/
		ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
414
		linphone_core_abort_call(lc,call,_("Incompatible, check codecs or security settings..."));
Simon Morlat's avatar
Simon Morlat committed
415
	}
416 417
}

418 419
static void call_ack(SalOp *op){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
420
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
Simon Morlat's avatar
Simon Morlat committed
421 422 423 424
	if (call==NULL){
		ms_warning("No call to be ACK'd");
		return ;
	}
425
	if (call->expect_media_in_ack){
426 427
		SalMediaDescription *md=sal_call_get_final_media_description(op);
		if (md && !sal_media_description_empty(md)){
428
			linphone_core_update_streams(lc,call,md);
Simon Morlat's avatar
bugfix  
Simon Morlat committed
429
			linphone_call_set_state (call,LinphoneCallStreamsRunning,"Connected (streams running)");
430 431 432
		}else{
			/*send a bye*/
			ms_error("Incompatible SDP response received in ACK, need to abort the call");
433 434
			linphone_core_abort_call(lc,call,"No codec intersection");
			return;
435
		}
Simon Morlat's avatar
Simon Morlat committed
436
	}
437 438
}

439 440
static void call_accept_update(LinphoneCore *lc, LinphoneCall *call){
	SalMediaDescription *md;
441
	SalMediaDescription *rmd=sal_call_get_remote_media_description(call->op);
442
	if (rmd!=NULL && call->ice_session!=NULL) {
443 444 445
		linphone_core_update_ice_from_remote_media_description(call,rmd);
		linphone_core_update_local_media_description_from_ice(call->localdesc,call->ice_session);
	}
Yann Diorcet's avatar
Yann Diorcet committed
446 447
#ifdef BUILD_UPNP
	if(call->upnp_session != NULL) {
Yann Diorcet's avatar
Yann Diorcet committed
448
		linphone_core_update_upnp_from_remote_media_description(call, rmd);
Yann Diorcet's avatar
Yann Diorcet committed
449 450
		linphone_core_update_local_media_description_from_upnp(call->localdesc,call->upnp_session);
	}
451
#endif //BUILD_UPNP
452
	linphone_call_update_remote_session_id_and_ver(call);
453 454
	sal_call_accept(call->op);
	md=sal_call_get_final_media_description(call->op);
455
	if (md && !sal_media_description_empty(md)){
456
		linphone_core_update_streams(lc,call,md);
457
	}
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

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

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){
476 477 478 479 480 481 482 483 484 485
	/*first check if media capabilities are compatible*/
	SalMediaDescription* md;
	linphone_call_make_local_media_description(lc,call);
	sal_call_set_local_media_description(call->op,call->localdesc);
	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;
	}

486 487 488 489 490 491 492 493
	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);
	}
}
494

495
/* this callback is called when an incoming re-INVITE modifies the session*/
496
static void call_updating(SalOp *op){
497 498
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
499
	SalMediaDescription *rmd=sal_call_get_remote_media_description(op);
500

501 502 503
	if (rmd==NULL){
		/* case of a reINVITE without SDP */
		call_accept_update(lc,call);
504
		call->expect_media_in_ack=TRUE;
505 506 507
		return;
	}

508 509 510 511
	switch(call->state){
		case LinphoneCallPausedByRemote:
			if (sal_media_description_has_dir(rmd,SalStreamSendRecv) || sal_media_description_has_dir(rmd,SalStreamRecvOnly)){
				call_resumed(lc,call);
512
			}else call_paused_by_remote(lc,call);
513 514 515 516 517
		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);
518
			}else{
519
				call_updated_by_remote(lc,call);
520
			}
521 522 523
		break;
		default:
			call_accept_update(lc,call);
Simon Morlat's avatar
Simon Morlat committed
524
	}
525 526
}

527 528
static void call_terminated(SalOp *op, const char *from){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
529
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
Simon Morlat's avatar
Simon Morlat committed
530 531

	if (call==NULL) return;
532
	
533 534 535 536 537 538 539 540 541 542 543 544
	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
545 546
	}
	ms_message("Current call terminated...");
547
	if (call->refer_pending){
548
		linphone_core_start_refered_call(lc,call,NULL);
549
	}
Simon Morlat's avatar
Simon Morlat committed
550 551
	//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) )) {
552
		linphone_core_stop_ringing(lc);
Simon Morlat's avatar
Simon Morlat committed
553
	}
554
	linphone_call_stop_media_streams(call);
Simon Morlat's avatar
Simon Morlat committed
555 556 557 558
	if (lc->vtable.show!=NULL)
		lc->vtable.show(lc);
	if (lc->vtable.display_status!=NULL)
		lc->vtable.display_status(lc,_("Call terminated."));
559

Yann Diorcet's avatar
Yann Diorcet committed
560 561 562 563
#ifdef BUILD_UPNP
	linphone_call_delete_upnp_session(call);
#endif //BUILD_UPNP

564
	linphone_call_set_state(call, LinphoneCallEnd,"Call ended");
565 566
}

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
static int resume_call_after_failed_transfer(LinphoneCall *call){
	ms_message("!!!!!!!!!!resume_call_after_failed_transfer");
	if (call->was_automatically_paused && call->state==LinphoneCallPausing)
		return BELLE_SIP_CONTINUE; /*was still in pausing state*/
	
	if (call->was_automatically_paused && call->state==LinphoneCallPaused){
		if (sal_op_is_idle(call->op)){
			linphone_core_resume_call(call->core,call);
		}else {
			ms_message("!!!!!!!!!!resume_call_after_failed_transfer, salop was busy");
			return BELLE_SIP_CONTINUE;
		}
	}
	linphone_call_unref(call);
	return BELLE_SIP_STOP;
}

584
static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details, int code){
585
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
Simon Morlat's avatar
Simon Morlat committed
586 587 588 589 590
	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.");
591
	const char *msg=details;
592
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
593
	LinphoneCall *referer=call->referer;
594

595
	if (call==NULL){
Simon Morlat's avatar
Simon Morlat committed
596
		ms_warning("Call faillure reported on already terminated call.");
597 598 599
		return ;
	}
	
Simon Morlat's avatar
Simon Morlat committed
600 601 602
	if (lc->vtable.show) lc->vtable.show(lc);

	if (error==SalErrorNoResponse){
603
		msg=_("No response.");
604
		if (lc->vtable.display_status)
605
			lc->vtable.display_status(lc,msg);
Simon Morlat's avatar
Simon Morlat committed
606
	}else if (error==SalErrorProtocol){
607
		msg=details ? details : _("Protocol error.");
608
		if (lc->vtable.display_status)
609
			lc->vtable.display_status(lc, msg);
Simon Morlat's avatar
Simon Morlat committed
610 611 612
	}else if (error==SalErrorFailure){
		switch(sr){
			case SalReasonDeclined:
Simon Morlat's avatar
Simon Morlat committed
613
				msg=msg603;
Simon Morlat's avatar
Simon Morlat committed
614 615 616 617
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,msg603);
			break;
			case SalReasonBusy:
Simon Morlat's avatar
Simon Morlat committed
618
				msg=msg486;
Simon Morlat's avatar
Simon Morlat committed
619 620 621 622
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,msg486);
			break;
			case SalReasonRedirect:
Simon Morlat's avatar
Simon Morlat committed
623
				msg=_("Redirected");
Simon Morlat's avatar
Simon Morlat committed
624
				if (lc->vtable.display_status)
Simon Morlat's avatar
Simon Morlat committed
625
					lc->vtable.display_status(lc,msg);
Simon Morlat's avatar
Simon Morlat committed
626 627
			break;
			case SalReasonTemporarilyUnavailable:
Simon Morlat's avatar
Simon Morlat committed
628
				msg=msg480;
Simon Morlat's avatar
Simon Morlat committed
629 630 631 632 633
				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
634
					lc->vtable.display_status(lc,msg);
Simon Morlat's avatar
Simon Morlat committed
635 636
			break;
			case SalReasonDoNotDisturb:
Simon Morlat's avatar
Simon Morlat committed
637
				msg=msg600;
Simon Morlat's avatar
Simon Morlat committed
638 639 640
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,msg600);
			break;
641 642
			case SalReasonUnsupportedContent: /*<this is for compatibility: linphone sent 415 because of SDP offer answer failure*/
			case SalReasonNotAcceptable:
643 644 645 646
			//media_encryption_mandatory
				if (call->params.media_encryption == LinphoneMediaEncryptionSRTP && 
					!linphone_core_is_media_encryption_mandatory(lc)) {
					int i;
647
					ms_message("Outgoing call [%p] failed with SRTP (SAVP) enabled",call);
648
					linphone_call_stop_media_streams(call);
649 650 651 652 653
					if (	call->state==LinphoneCallOutgoingInit
							|| call->state==LinphoneCallOutgoingProgress
							|| call->state==LinphoneCallOutgoingRinging /*push case*/
							|| call->state==LinphoneCallOutgoingEarlyMedia){
						ms_message("Retrying call [%p] with AVP",call);
654 655
						/* clear SRTP local params */
						call->params.media_encryption = LinphoneMediaEncryptionNone;
656
						for(i=0; i<call->localdesc->n_active_streams; i++) {
657 658 659
							call->localdesc->streams[i].proto = SalProtoRtpAvp;
							memset(call->localdesc->streams[i].crypto, 0, sizeof(call->localdesc->streams[i].crypto));
						}
Simon Morlat's avatar
Simon Morlat committed
660
						linphone_core_restart_invite(lc, call);
661
						return;
662
					}
663

664
				}
665
				msg=_("Incompatible media parameters.");
Simon Morlat's avatar
Simon Morlat committed
666
				if (lc->vtable.display_status)
Simon Morlat's avatar
Simon Morlat committed
667
					lc->vtable.display_status(lc,msg);
Simon Morlat's avatar
Simon Morlat committed
668
			break;
669 670
			case SalReasonRequestPending:
				/*restore previous state, the application will decide to resubmit the action if relevant*/
671
				call->reason=linphone_reason_from_sal(sr);
672 673 674
				linphone_call_set_state(call,call->prevstate,msg);
				return;
			break;
Simon Morlat's avatar
Simon Morlat committed
675 676 677 678 679
			default:
				if (lc->vtable.display_status)
					lc->vtable.display_status(lc,_("Call failed."));
		}
	}
680

681 682 683 684 685 686 687
	/*some call error are not fatal*/
	switch (call->state) {
	case LinphoneCallUpdating:
	case LinphoneCallPausing:
	case LinphoneCallResuming:
		ms_message("Call error on state [%s], restoring previous state",linphone_call_state_to_string(call->prevstate));
		call->reason=linphone_reason_from_sal(sr);
688
		linphone_call_set_state(call, call->prevstate,details);
689 690 691 692 693
		return;
	default:
		break; /*nothing to do*/
	}

694
	linphone_core_stop_ringing(lc);
695
	linphone_call_stop_media_streams(call);
696 697 698 699

#ifdef BUILD_UPNP
	linphone_call_delete_upnp_session(call);
#endif //BUILD_UPNP
700 701 702
	
	call->reason=linphone_reason_from_sal(sr);
	if (sr==SalReasonDeclined){
703
		linphone_call_set_state(call,LinphoneCallEnd,"Call declined.");
704 705 706 707
	}else{
		linphone_call_set_state(call,LinphoneCallError,details);
		if (sr==SalReasonBusy)
			linphone_core_play_named_tone(lc,LinphoneToneBusy);
708
	}
709 710
	
	if (referer){
711
		/*notify referer of the failure*/
712
		linphone_core_notify_refer_state(lc,referer,call);
713 714
		/*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");
715
	}
716 717
}

718 719
static void call_released(SalOp *op){
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
720 721 722
	if (call!=NULL){
		linphone_call_set_state(call,LinphoneCallReleased,"Call released");
	}else ms_error("call_released() for already destroyed call ?");
723 724
}

jehan's avatar
jehan committed
725 726
static void auth_failure(SalOp *op, SalAuthInfo* info) {
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
727
	LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username,info->domain);
jehan's avatar
jehan committed
728
	if (ai){
729
		ms_message("%s/%s/%s authentication fails.",info->realm,info->username,info->domain);
730 731 732 733
		/*ask again for password if auth info was already supplied but apparently not working*/
		if (lc->vtable.auth_info_requested) {
			lc->vtable.auth_info_requested(lc,info->realm,info->username,info->domain);
		}
jehan's avatar
jehan committed
734
	}
735
	
736
}
737

738
static void register_success(SalOp *op, bool_t registered){
739 740 741
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
	LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
	char *msg;
742
	
743
	if (!cfg || cfg->deletion_date!=0){
744 745 746
		ms_message("Registration success for removed proxy config, ignored");
		return;
	}
747
	linphone_proxy_config_set_error(cfg,LinphoneReasonNone);
748 749 750
	linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
	                                registered ? "Registration sucessful" : "Unregistration done");
	if (lc->vtable.display_status){
751
		if (registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
752
		else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
753
		lc->vtable.display_status(lc,msg);
754 755 756
		ms_free(msg);
	}
	
757 758 759
}

static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
760
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
761 762 763 764 765 766
	LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);

	if (cfg==NULL){
		ms_warning("Registration failed for unknown proxy config.");
		return ;
	}
767 768 769 770
	if (cfg->deletion_date!=0){
		ms_message("Registration failed for removed proxy config, ignored");
		return;
	}
771 772 773 774 775 776 777 778
	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);
	}
779 780 781

	linphone_proxy_config_set_error(cfg,linphone_reason_from_sal(reason));

782 783 784 785 786 787 788
	if (error== SalErrorFailure
			&& reason == SalReasonServiceUnavailable
			&& 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);
	}
789 790 791 792 793 794
	if (cfg->publish_op){
		/*prevent publish to be sent now until registration gets successful*/
		sal_op_release(cfg->publish_op);
		cfg->publish_op=NULL;
		cfg->send_publish=cfg->publish;
	}
795 796 797
}

static void vfu_request(SalOp *op){
798
#ifdef VIDEO_ENABLED
799
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer (op);
800 801 802 803 804 805
	if (call==NULL){
		ms_warning("VFU request but no call !");
		return ;
	}
	if (call->videostream)
		video_stream_send_vfu(call->videostream);
806
#endif
807 808 809
}

static void dtmf_received(SalOp *op, char dtmf){
810
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
811
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
812
	if (lc->vtable.dtmf_received != NULL)
813
		lc->vtable.dtmf_received(lc, call, dtmf);
814 815
}

816 817
static void refer_received(Sal *sal, SalOp *op, const char *referto){
	LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
818
	LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
Simon Morlat's avatar
Simon Morlat committed
819
	if (call){
820 821 822 823 824
		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
825 826 827 828 829 830
		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);
		}
831
		if (call->refer_pending) linphone_core_start_refered_call(lc,call,NULL);
Simon Morlat's avatar
Simon Morlat committed
832 833
	}else if (lc->vtable.refer_received){
		lc->vtable.refer_received(lc,referto);
834
	}
835 836
}

837 838
static bool_t is_duplicate_msg(LinphoneCore *lc, const char *msg_id){
	MSList *elem=lc->last_recv_msg_ids;
839
	MSList *tail=NULL;
<