offeranswer.c 24.8 KB
Newer Older
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.
18 19
*/

20
#include "c-wrapper/internal/c-sal.h"
21
#include "sal/sal.h"
22
#include "offeranswer.h"
23
#include "private.h"
24

25 26
#include "utils/payload-type-handler.h"

27
static bool_t only_telephone_event(const bctbx_list_t *l){
28 29 30 31 32
	for(;l!=NULL;l=l->next){
		PayloadType *p=(PayloadType*)l->data;
		if (strcasecmp(p->mime_type,"telephone-event")!=0){
			return FALSE;
		}
33 34 35
	}
	return TRUE;
}
36

37

38
static PayloadType * opus_match(MSOfferAnswerContext *ctx, const bctbx_list_t *local_payloads, const PayloadType *refpt, const bctbx_list_t *remote_payloads, bool_t reading_response){
39
	PayloadType *pt;
40
	const bctbx_list_t *elem;
41
	PayloadType *legacy_opus=NULL;
42

43
	for (elem=local_payloads;elem!=NULL;elem=elem->next){
44
		pt=(PayloadType*)elem->data;
45 46
		
		/*workaround a bug in earlier versions of linphone where opus/48000/1 is offered, which is uncompliant with opus rtp draft*/
47 48
		if (strcasecmp(pt->mime_type,"opus")==0 ){
			if (refpt->channels==1){
49
				legacy_opus=pt;
50
			}else if (refpt->channels==2){
51
				return payload_type_clone(pt);
52
			}
53
		}
54
	}
55 56 57 58 59 60
	if (legacy_opus){
		legacy_opus = payload_type_clone(legacy_opus);
		legacy_opus->channels=1; /*so that we respond with same number of channels */
		return legacy_opus;
	}
	return NULL;
61 62
}

63
static MSOfferAnswerContext *opus_offer_answer_create_context(void){
64
	static MSOfferAnswerContext opus_oa = {opus_match, NULL, NULL};
65 66 67 68 69 70 71 72
	return &opus_oa;
}

MSOfferAnswerProvider opus_offer_answer_provider={
	"opus",
	opus_offer_answer_create_context
};

73
/* the reason for this matcher is for some stupid uncompliant phone that offer G729a mime type !*/
74
static PayloadType * g729A_match(MSOfferAnswerContext *ctx, const bctbx_list_t *local_payloads, const PayloadType *refpt, const bctbx_list_t *remote_payloads, bool_t reading_response){
75
	PayloadType *pt;
76
	const bctbx_list_t *elem;
77 78
	PayloadType *candidate=NULL;

79
	for (elem=local_payloads;elem!=NULL;elem=elem->next){
80
		pt=(PayloadType*)elem->data;
81
		
82
		if (strcasecmp(pt->mime_type,"G729")==0 && refpt->channels==pt->channels){
83 84 85
			candidate=pt;
		}
	}
86
	return candidate ? payload_type_clone(candidate) : NULL;
87 88
}

89
static MSOfferAnswerContext *g729a_offer_answer_create_context(void){
90
	static MSOfferAnswerContext g729_oa = {g729A_match, NULL, NULL};
91
	return &g729_oa;
92 93
}

94 95 96 97
MSOfferAnswerProvider g729a_offer_answer_provider={
	"G729A",
	g729a_offer_answer_create_context
};
98

99 100
static PayloadType * red_match(MSOfferAnswerContext *ctx, const bctbx_list_t *local_payloads, const PayloadType *refpt, const bctbx_list_t *remote_payloads, bool_t reading_response) {
	const bctbx_list_t *elem_local, *elem_remote;
101 102 103 104 105 106
	PayloadType *red = NULL;

	for (elem_local = local_payloads; elem_local != NULL; elem_local = elem_local->next) {
		PayloadType *pt = (PayloadType*)elem_local->data;
		
		if (strcasecmp(pt->mime_type, payload_type_t140_red.mime_type) == 0) {
107
			red = payload_type_clone(pt);
108 109 110 111 112
			
			for (elem_remote = remote_payloads; elem_remote != NULL; elem_remote = elem_remote->next) {
				PayloadType *pt2 = (PayloadType*)elem_remote->data;
				if (strcasecmp(pt2->mime_type, payload_type_t140.mime_type) == 0) {
					int t140_payload_number = payload_type_get_number(pt2);
113
					char *red_fmtp = ms_strdup_printf("%i/%i/%i", t140_payload_number, t140_payload_number, t140_payload_number);
114 115
					/*modify the local payload and the return value*/
					payload_type_set_recv_fmtp(pt, red_fmtp);
116
					payload_type_set_recv_fmtp(red, red_fmtp);
117
					ms_free(red_fmtp);
118 119 120 121 122 123 124 125 126
					break;
				}
			}
			break;
		}
	}
	return red;
}

127
static MSOfferAnswerContext *red_offer_answer_create_context(void){
128
	static MSOfferAnswerContext red_oa = {red_match, NULL, NULL};
129 130 131 132 133 134
	return &red_oa;
}

MSOfferAnswerProvider red_offer_answer_provider={
	"red",
	red_offer_answer_create_context
135 136
};

137
static PayloadType * generic_match(const bctbx_list_t *local_payloads, const PayloadType *refpt, const bctbx_list_t *remote_payloads){
138
	PayloadType *pt;
139
	const bctbx_list_t *elem;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

	for (elem=local_payloads;elem!=NULL;elem=elem->next){
		pt=(PayloadType*)elem->data;
		
		if ( pt->mime_type && refpt->mime_type 
			&& strcasecmp(pt->mime_type, refpt->mime_type)==0
			&& pt->clock_rate==refpt->clock_rate
			&& pt->channels==refpt->channels)
			return payload_type_clone(pt);
	}
	return NULL;
}


void linphone_core_register_offer_answer_providers(LinphoneCore *lc){
155
	MSFactory *factory = lc->factory;
156 157 158 159
	ms_factory_register_offer_answer_provider(factory, &red_offer_answer_provider);
	ms_factory_register_offer_answer_provider(factory, &g729a_offer_answer_provider);
	ms_factory_register_offer_answer_provider(factory, &opus_offer_answer_provider);
}
160

161 162 163
/*
 * Returns a PayloadType from the local list that matches a PayloadType offered or answered in the remote list
*/
164 165
static PayloadType * find_payload_type_best_match(MSFactory *factory, const bctbx_list_t *local_payloads, const PayloadType *refpt,
						  const bctbx_list_t *remote_payloads, bool_t reading_response){
166
	PayloadType *ret = NULL;
167 168 169 170
	MSOfferAnswerContext *ctx = NULL;

	// When a stream is inactive, refpt->mime_type might be null
	if (refpt->mime_type && (ctx = ms_factory_create_offer_answer_context(factory, refpt->mime_type))) {
171 172 173 174
		ms_message("Doing offer/answer processing with specific provider for codec [%s]", refpt->mime_type); 
		ret = ms_offer_answer_context_match_payload(ctx, local_payloads, refpt, remote_payloads, reading_response);
		ms_offer_answer_context_destroy(ctx);
		return ret;
175
	}
176
	return generic_match(local_payloads, refpt, remote_payloads);
177 178 179
}


180 181 182
static bctbx_list_t *match_payloads(MSFactory *factory, const bctbx_list_t *local, const bctbx_list_t *remote, bool_t reading_response, bool_t one_matching_codec){
	const bctbx_list_t *e2,*e1;
	bctbx_list_t *res=NULL;
183
	PayloadType *matched;
184
	bool_t found_codec=FALSE;
185

186 187
	for(e2=remote;e2!=NULL;e2=e2->next){
		PayloadType *p2=(PayloadType*)e2->data;
188
		matched=find_payload_type_best_match(factory, local, p2, remote, reading_response);
189
		if (matched){
190 191
			int local_number=payload_type_get_number(matched);
			int remote_number=payload_type_get_number(p2);
192 193 194 195 196 197 198 199

			if (one_matching_codec){
				if (strcasecmp(matched->mime_type,"telephone-event")!=0){
					if (found_codec){/* we have found a real codec already*/
						continue; /*this codec won't be added*/
					}else found_codec=TRUE;
				}
			}
200

201
			if (p2->send_fmtp){
202
				payload_type_append_send_fmtp(matched,p2->send_fmtp);
203
			}
204
			payload_type_set_flag(matched, PAYLOAD_TYPE_FLAG_CAN_RECV|PAYLOAD_TYPE_FLAG_CAN_SEND);
205
			if (matched->flags & PAYLOAD_TYPE_RTCP_FEEDBACK_ENABLED && p2->flags & PAYLOAD_TYPE_RTCP_FEEDBACK_ENABLED) {
206
				payload_type_set_flag(matched, PAYLOAD_TYPE_RTCP_FEEDBACK_ENABLED);
207
				/* Negotiation of AVPF features (keep common features) */
208 209
				matched->avpf.features &= p2->avpf.features;
				matched->avpf.rpsi_compatibility = p2->avpf.rpsi_compatibility;
210
				/* Take bigger AVPF trr interval */
211 212
				if (p2->avpf.trr_interval > matched->avpf.trr_interval) {
					matched->avpf.trr_interval = p2->avpf.trr_interval;
213
				}
214 215
			}else{
				payload_type_unset_flag(matched, PAYLOAD_TYPE_RTCP_FEEDBACK_ENABLED);
216
			}
217
			res=bctbx_list_append(res,matched);
218
			/* we should use the remote numbering even when parsing a response */
219 220
			payload_type_set_number(matched,remote_number);
			payload_type_set_flag(matched, PAYLOAD_TYPE_FROZEN_NUMBER);
221 222
			if (reading_response && remote_number!=local_number){
				ms_warning("For payload type %s, proposed number was %i but the remote phone answered %i",
223
						  matched->mime_type, local_number, remote_number);
224 225 226 227 228
				/*
				 We must add this payload type with our local numbering in order to be able to receive it.
				 Indeed despite we must sent with the remote numbering, we must be able to receive with
				 our local one.
				*/
229 230
				matched=payload_type_clone(matched);
				payload_type_set_number(matched,local_number);
Simon Morlat's avatar
Simon Morlat committed
231
				payload_type_set_flag(matched, PAYLOAD_TYPE_FLAG_CAN_RECV);
232
				payload_type_set_flag(matched, PAYLOAD_TYPE_FROZEN_NUMBER);
233
				res=bctbx_list_append(res,matched);
234
			}
235
		}else{
236 237 238
			if (p2->channels>0)
				ms_message("No match for %s/%i/%i",p2->mime_type,p2->clock_rate,p2->channels);
			else ms_message("No match for %s/%i",p2->mime_type,p2->clock_rate);
239 240
		}
	}
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	if (reading_response){
		/* add remaning local payload as CAN_RECV only so that if we are in front of a non-compliant equipment we are still able to decode the RTP stream*/
		for(e1=local;e1!=NULL;e1=e1->next){
			PayloadType *p1=(PayloadType*)e1->data;
			bool_t found=FALSE;
			for(e2=res;e2!=NULL;e2=e2->next){
				PayloadType *p2=(PayloadType*)e2->data;
				if (payload_type_get_number(p2)==payload_type_get_number(p1)){
					found=TRUE;
					break;
				}
			}
			if (!found){
				ms_message("Adding %s/%i for compatibility, just in case.",p1->mime_type,p1->clock_rate);
				p1=payload_type_clone(p1);
256 257
				payload_type_set_flag(p1, PAYLOAD_TYPE_FLAG_CAN_RECV);
				payload_type_set_flag(p1, PAYLOAD_TYPE_FROZEN_NUMBER);
258
				res=bctbx_list_append(res,p1);
259 260 261
			}
		}
	}
262 263 264
	return res;
}

265
static bool_t match_crypto_algo(const SalSrtpCryptoAlgo* local, const SalSrtpCryptoAlgo* remote,
266
	SalSrtpCryptoAlgo* result, unsigned int* choosen_local_tag, bool_t use_local_key) {
267 268 269 270
	int i,j;
	for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
		if (remote[i].algo == 0)
			break;
271

272
		/* Look for a local enabled crypto algo that matches one of the proposed by remote */
273 274 275
		for(j=0; j<SAL_CRYPTO_ALGO_MAX; j++) {
			if (remote[i].algo == local[j].algo) {
				result->algo = remote[i].algo;
276
				/* We're answering an SDP offer. Supply our master key, associated with the remote supplied tag */
277
				if (use_local_key) {
278
					strncpy(result->master_key, local[j].master_key, sizeof(result->master_key) );
279
					result->tag = remote[i].tag;
280
					*choosen_local_tag = local[j].tag;
281 282
				}
				/* We received an answer to our SDP crypto proposal. Copy matching algo remote master key to result, and memorize local tag */
283
				else {
284
					strncpy(result->master_key, remote[i].master_key, sizeof(result->master_key));
285
					result->tag = local[j].tag;
286
					*choosen_local_tag = local[j].tag;
287 288 289 290 291 292 293 294
				}
				return TRUE;
			}
		}
	}
	return FALSE;
}

295

296

297
static SalStreamDir compute_dir_outgoing(SalStreamDir local, SalStreamDir answered){
298 299 300 301 302 303 304 305 306 307 308 309 310 311
	SalStreamDir res=local;
	if (local==SalStreamSendRecv){
		if (answered==SalStreamRecvOnly){
			res=SalStreamSendOnly;
		}else if (answered==SalStreamSendOnly){
			res=SalStreamRecvOnly;
		}
	}
	if (answered==SalStreamInactive){
		res=SalStreamInactive;
	}
	return res;
}

312 313 314 315 316
static SalStreamDir compute_dir_incoming(SalStreamDir local, SalStreamDir offered){
	SalStreamDir res=SalStreamSendRecv;
	if (local==SalStreamSendRecv){
		if (offered==SalStreamSendOnly)
			res=SalStreamRecvOnly;
Simon Morlat's avatar
Simon Morlat committed
317
		else if (offered==SalStreamRecvOnly)
318
			res=SalStreamSendOnly;
Simon Morlat's avatar
Simon Morlat committed
319
		else if (offered==SalStreamInactive)
320
			res=SalStreamInactive;
Simon Morlat's avatar
Simon Morlat committed
321 322
		else
			res=SalStreamSendRecv;
323
	}else if (local==SalStreamSendOnly){
324
		if (offered==SalStreamRecvOnly || offered==SalStreamSendRecv)
325
			res=SalStreamSendOnly;
Simon Morlat's avatar
Simon Morlat committed
326
		else res=SalStreamInactive;
327
	}else if (local==SalStreamRecvOnly){
Simon Morlat's avatar
Simon Morlat committed
328
		if (offered==SalStreamSendOnly || offered==SalStreamSendRecv)
329
			res=SalStreamRecvOnly;
Simon Morlat's avatar
Simon Morlat committed
330
		else
331 332 333 334 335
			res=SalStreamInactive;
	}else res=SalStreamInactive;
	return res;
}

336
static void initiate_outgoing(MSFactory* factory, const SalStreamDescription *local_offer,
337 338
						const SalStreamDescription *remote_answer,
						SalStreamDescription *result){
339
	if (remote_answer->rtp_port!=0)
340
		result->payloads=match_payloads(factory, local_offer->payloads,remote_answer->payloads,TRUE,FALSE);
jehan's avatar
jehan committed
341 342 343 344 345
	else {
		ms_message("Local stream description [%p] rejected by peer",local_offer);
		result->rtp_port=0;
		return;
	}
346
	result->proto=remote_answer->proto;
347
	result->type=local_offer->type;
jehan's avatar
jehan committed
348

349
	if (local_offer->rtp_addr[0]!='\0' && ms_is_multicast(local_offer->rtp_addr)) {
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
			/*6.2 Multicast Streams
			...
		If a multicast stream is accepted, the address and port information
		in the answer MUST match that of the offer.  Similarly, the
		directionality information in the answer (sendonly, recvonly, or
		sendrecv) MUST equal that of the offer.  This is because all
		participants in a multicast session need to have equivalent views of
		the parameters of the session, an underlying assumption of the
		multicast bias of RFC 2327.*/
		if (strcmp(local_offer->rtp_addr,remote_answer->rtp_addr) !=0 ) {
			ms_message("Remote answered IP [%s] does not match offered [%s] for local stream description [%p]"
																,remote_answer->rtp_addr
																,local_offer->rtp_addr
																,local_offer);
			result->rtp_port=0;
			return;
		}
		if (local_offer->rtp_port!=remote_answer->rtp_port) {
			ms_message("Remote answered rtp port [%i] does not match offered [%i] for local stream description [%p]"
																,remote_answer->rtp_port
																,local_offer->rtp_port
																,local_offer);
			result->rtp_port=0;
			return;
		}
		if (local_offer->dir!=remote_answer->dir) {
			ms_message("Remote answered dir [%s] does not match offered [%s] for local stream description [%p]"
																,sal_stream_dir_to_string(remote_answer->dir)
																,sal_stream_dir_to_string(local_offer->dir)
																,local_offer);
			result->rtp_port=0;
			return;
		}
		if (local_offer->bandwidth!=remote_answer->bandwidth) {
			ms_message("Remote answered bandwidth [%i] does not match offered [%i] for local stream description [%p]"
																,remote_answer->bandwidth
																,local_offer->bandwidth
																,local_offer);
			result->rtp_port=0;
			return;
		}
		if (local_offer->ptime > 0 && local_offer->ptime!=remote_answer->ptime) {
			ms_message("Remote answered ptime [%i] does not match offered [%i] for local stream description [%p]"
																,remote_answer->ptime
																,local_offer->ptime
																,local_offer);
			result->rtp_port=0;
			return;
		}
		if (local_offer->ttl > 0 && local_offer->ttl!=remote_answer->ttl) {
			ms_message("Remote answered ttl [%i] does not match offered [%i] for local stream description [%p]"
																		,remote_answer->ttl
																		,local_offer->ttl
																		,local_offer);
			result->rtp_port=0;
			return;
		}
		result->ttl=local_offer->ttl;
		result->dir=local_offer->dir;
		result->multicast_role = SalMulticastSender;
jehan's avatar
jehan committed
410 411 412 413 414
	} else {
		result->dir=compute_dir_outgoing(local_offer->dir,remote_answer->dir);
	}


Simon Morlat's avatar
Simon Morlat committed
415

416
	if (result->payloads && !only_telephone_event(result->payloads)){
417
		strcpy(result->rtp_addr,remote_answer->rtp_addr);
418
		strcpy(result->rtcp_addr,remote_answer->rtcp_addr);
419
		result->rtp_port=remote_answer->rtp_port;
420
		result->rtcp_port=remote_answer->rtcp_port;
421 422 423
		result->bandwidth=remote_answer->bandwidth;
		result->ptime=remote_answer->ptime;
	}else{
424
		result->rtp_port=0;
425
	}
426
	if (sal_stream_description_has_srtp(result) == TRUE) {
427 428
		/* verify crypto algo */
		memset(result->crypto, 0, sizeof(result->crypto));
429
		if (!match_crypto_algo(local_offer->crypto, remote_answer->crypto, &result->crypto[0], &result->crypto_local_tag, FALSE))
430
			result->rtp_port = 0;
431
	}
432 433 434
	result->rtp_ssrc=local_offer->rtp_ssrc;
	strncpy(result->rtcp_cname,local_offer->rtcp_cname,sizeof(result->rtcp_cname));

435 436 437 438 439 440 441 442 443 444 445 446 447
	// Handle dtls session attribute: if both local and remote have a dtls fingerprint and a dtls setup, get the remote fingerprint into the result
	if ((local_offer->dtls_role!=SalDtlsRoleInvalid) && (remote_answer->dtls_role!=SalDtlsRoleInvalid)
			&&(strlen(local_offer->dtls_fingerprint)>0) && (strlen(remote_answer->dtls_fingerprint)>0)) {
		strncpy(result->dtls_fingerprint, remote_answer->dtls_fingerprint,sizeof(result->dtls_fingerprint));
		if (remote_answer->dtls_role==SalDtlsRoleIsClient) {
			result->dtls_role = SalDtlsRoleIsServer;
		} else {
			result->dtls_role = SalDtlsRoleIsClient;
		}
	} else {
		result->dtls_fingerprint[0] = '\0';
		result->dtls_role = SalDtlsRoleInvalid;
	}
448
	result->rtcp_mux = remote_answer->rtcp_mux && local_offer->rtcp_mux;
449
	result->implicit_rtcp_fb = local_offer->implicit_rtcp_fb && remote_answer->implicit_rtcp_fb;
450 451 452
}


453
static void initiate_incoming(MSFactory *factory, const SalStreamDescription *local_cap,
454 455
						const SalStreamDescription *remote_offer,
						SalStreamDescription *result, bool_t one_matching_codec){
456
	result->payloads=match_payloads(factory, local_cap->payloads,remote_offer->payloads, FALSE, one_matching_codec);
457
	result->proto=remote_offer->proto;
458
	result->type=local_cap->type;
459
	result->dir=compute_dir_incoming(local_cap->dir,remote_offer->dir);
460
	if (!result->payloads || only_telephone_event(result->payloads) || remote_offer->rtp_port==0){
jehan's avatar
jehan committed
461 462 463
		result->rtp_port=0;
		return;
	}
464
	if (remote_offer->rtp_addr[0]!='\0' && ms_is_multicast(remote_offer->rtp_addr)) {
jehan's avatar
jehan committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478
		if (sal_stream_description_has_srtp(result) == TRUE) {
			ms_message("SAVP not supported for multicast address for remote stream [%p]",remote_offer);
			result->rtp_port=0;
			return;
		}
		result->dir=remote_offer->dir;
		strcpy(result->rtp_addr,remote_offer->rtp_addr);
		strcpy(result->rtcp_addr,remote_offer->rtcp_addr);
		result->rtp_port=remote_offer->rtp_port;
		/*result->rtcp_port=remote_offer->rtcp_port;*/
		result->rtcp_port=0; /* rtcp not supported yet*/
		result->bandwidth=remote_offer->bandwidth;
		result->ptime=remote_offer->ptime;
		result->ttl=remote_offer->ttl;
479
		result->multicast_role = SalMulticastReceiver;
jehan's avatar
jehan committed
480
	} else {
481
		strcpy(result->rtp_addr,local_cap->rtp_addr);
482
		strcpy(result->rtcp_addr,local_cap->rtcp_addr);
483
		result->rtp_port=local_cap->rtp_port;
484
		result->rtcp_port=local_cap->rtcp_port;
485
		result->bandwidth=local_cap->bandwidth;
486
		result->ptime=local_cap->ptime;
487
	}
johan's avatar
johan committed
488

489
	if (sal_stream_description_has_srtp(result) == TRUE) {
490 491
		/* select crypto algo */
		memset(result->crypto, 0, sizeof(result->crypto));
492
		if (!match_crypto_algo(local_cap->crypto, remote_offer->crypto, &result->crypto[0], &result->crypto_local_tag, TRUE)) {
493
			result->rtp_port = 0;
494 495
			ms_message("No matching crypto algo for remote stream's offer [%p]",remote_offer);
		}
496

497
	}
johan's avatar
johan committed
498 499

	if (remote_offer->haveZrtpHash == 1) {
500
		if (local_cap->zrtphash[0] != 0) { /* if ZRTP is available, set the zrtp hash even if it is not selected */
johan's avatar
johan committed
501 502 503 504 505
			strncpy((char *)(result->zrtphash), (char *)(local_cap->zrtphash), sizeof(local_cap->zrtphash));
			result->haveZrtpHash =  1;
		}
	}

506 507 508
	strcpy(result->ice_pwd, local_cap->ice_pwd);
	strcpy(result->ice_ufrag, local_cap->ice_ufrag);
	result->ice_mismatch = local_cap->ice_mismatch;
509
	result->set_nortpproxy = local_cap->set_nortpproxy;
510 511
	memcpy(result->ice_candidates, local_cap->ice_candidates, sizeof(result->ice_candidates));
	memcpy(result->ice_remote_candidates, local_cap->ice_remote_candidates, sizeof(result->ice_remote_candidates));
512
	strcpy(result->name,local_cap->name);
513 514 515
	result->rtp_ssrc=local_cap->rtp_ssrc;
	strncpy(result->rtcp_cname,local_cap->rtcp_cname,sizeof(result->rtcp_cname));

516 517 518 519 520 521 522 523 524 525 526 527
	// Handle dtls stream attribute: if both local and remote have a dtls fingerprint and a dtls setup, add the local fingerprint to the answer
	// Note: local description usually stores dtls config at session level which means it apply to all streams, check this too
	if (((local_cap->dtls_role!=SalDtlsRoleInvalid)) && (remote_offer->dtls_role!=SalDtlsRoleInvalid)
			&& (strlen(local_cap->dtls_fingerprint)>0) && (strlen(remote_offer->dtls_fingerprint)>0)) {
		strncpy(result->dtls_fingerprint, local_cap->dtls_fingerprint,sizeof(result->dtls_fingerprint));
		if (remote_offer->dtls_role==SalDtlsRoleUnset) {
			result->dtls_role = SalDtlsRoleIsClient;
		}
	} else {
		result->dtls_fingerprint[0] = '\0';
		result->dtls_role = SalDtlsRoleInvalid;
	}
528
	result->rtcp_mux = remote_offer->rtcp_mux && local_cap->rtcp_mux;
529
	result->implicit_rtcp_fb = local_cap->implicit_rtcp_fb && remote_offer->implicit_rtcp_fb;
530 531
}

jehan's avatar
jehan committed
532

533 534 535 536
/**
 * Returns a media description to run the streams with, based on a local offer
 * and the returned response (remote).
**/
537
int offer_answer_initiate_outgoing(MSFactory *factory, const SalMediaDescription *local_offer,
538 539
					const SalMediaDescription *remote_answer,
					SalMediaDescription *result){
540
	int i;
541
	const SalStreamDescription *ls,*rs;
542

543
	for(i=0;i<local_offer->nb_streams;++i){
544
		ms_message("Processing for stream %i",i);
545 546 547
		ls=&local_offer->streams[i];
		rs=&remote_answer->streams[i];
		if (rs && ls->proto == rs->proto && rs->type == ls->type) {
548
			initiate_outgoing(factory, ls,rs,&result->streams[i]);
549
			memcpy(&result->streams[i].rtcp_xr, &ls->rtcp_xr, sizeof(result->streams[i].rtcp_xr));
550
			if ((ls->rtcp_xr.enabled == TRUE) && (rs->rtcp_xr.enabled == FALSE)) {
551
				result->streams[i].rtcp_xr.enabled = FALSE;
552
			}
553 554
			result->streams[i].rtcp_fb.generic_nack_enabled = ls->rtcp_fb.generic_nack_enabled & rs->rtcp_fb.generic_nack_enabled;
			result->streams[i].rtcp_fb.tmmbr_enabled = ls->rtcp_fb.tmmbr_enabled & rs->rtcp_fb.tmmbr_enabled;
555 556
		}
		else ms_warning("No matching stream for %i",i);
557
	}
558
	result->nb_streams=local_offer->nb_streams;
Simon Morlat's avatar
Simon Morlat committed
559
	result->bandwidth=remote_answer->bandwidth;
560
	strcpy(result->addr,remote_answer->addr);
561 562
	strcpy(result->ice_pwd, local_offer->ice_pwd);
	strcpy(result->ice_ufrag, local_offer->ice_ufrag);
563
	memcpy(&result->rtcp_xr, &local_offer->rtcp_xr, sizeof(result->rtcp_xr));
564
	if ((local_offer->rtcp_xr.enabled == TRUE) && (remote_answer->rtcp_xr.enabled == FALSE)) {
565
		result->rtcp_xr.enabled = FALSE;
566 567
	}

568 569 570 571 572 573 574 575
	return 0;
}

/**
 * Returns a media description to run the streams with, based on the local capabilities and
 * and the received offer.
 * The returned media description is an answer and should be sent to the offerer.
**/
576
int offer_answer_initiate_incoming(MSFactory *factory, const SalMediaDescription *local_capabilities,
577 578
					const SalMediaDescription *remote_offer,
					SalMediaDescription *result, bool_t one_matching_codec){
579
	int i;
Yann Diorcet's avatar
Yann Diorcet committed
580
	const SalStreamDescription *ls=NULL,*rs;
581 582 583 584 585

	for(i=0;i<remote_offer->nb_streams;++i){
		rs = &remote_offer->streams[i];
		ls = &local_capabilities->streams[i];
		if (ls && rs->type == ls->type && rs->proto == ls->proto){
586
			initiate_incoming(factory, ls,rs,&result->streams[i],one_matching_codec);
587 588 589
			// Handle global RTCP FB attributes
			result->streams[i].rtcp_fb.generic_nack_enabled = rs->rtcp_fb.generic_nack_enabled;
			result->streams[i].rtcp_fb.tmmbr_enabled = rs->rtcp_fb.tmmbr_enabled;
590 591 592
			// Handle media RTCP XR attribute
			memset(&result->streams[i].rtcp_xr, 0, sizeof(result->streams[i].rtcp_xr));
			if (rs->rtcp_xr.enabled == TRUE) {
593 594 595 596 597
				const OrtpRtcpXrConfiguration *rtcp_xr_conf = NULL;
				if (ls->rtcp_xr.enabled == TRUE) rtcp_xr_conf = &ls->rtcp_xr;
				else if (local_capabilities->rtcp_xr.enabled == TRUE) rtcp_xr_conf = &local_capabilities->rtcp_xr;
				if ((rtcp_xr_conf != NULL) && (ls->dir == SalStreamSendRecv)) {
					memcpy(&result->streams[i].rtcp_xr, rtcp_xr_conf, sizeof(result->streams[i].rtcp_xr));
598 599 600 601
				} else {
					result->streams[i].rtcp_xr.enabled = TRUE;
				}
			}
602
		}else {
603
			ms_message("Declining mline %i, no corresponding stream in local capabilities description.",i);
604
			/* create an inactive stream for the answer, as there where no matching stream in local capabilities */
605
			result->streams[i].dir=SalStreamInactive;
606
			result->streams[i].rtp_port=0;
607
			result->streams[i].type=rs->type;
Simon Morlat's avatar
Simon Morlat committed
608
			result->streams[i].proto=rs->proto;
609 610 611
			if (rs->type==SalOther){
				strncpy(result->streams[i].typeother,rs->typeother,sizeof(rs->typeother)-1);
			}
612 613 614
			if (rs->proto==SalProtoOther){
				strncpy(result->streams[i].proto_other,rs->proto_other,sizeof(rs->proto_other)-1);
			}
615
		}
616
		result->streams[i].custom_sdp_attributes = sal_custom_sdp_attribute_clone(ls->custom_sdp_attributes);
617
	}
618
	result->nb_streams=i;
619
	strcpy(result->username, local_capabilities->username);
620
	strcpy(result->addr,local_capabilities->addr);
Simon Morlat's avatar
Simon Morlat committed
621
	result->bandwidth=local_capabilities->bandwidth;
622 623
	result->session_ver=local_capabilities->session_ver;
	result->session_id=local_capabilities->session_id;
624 625 626
	strcpy(result->ice_pwd, local_capabilities->ice_pwd);
	strcpy(result->ice_ufrag, local_capabilities->ice_ufrag);
	result->ice_lite = local_capabilities->ice_lite;
627
	result->set_nortpproxy = local_capabilities->set_nortpproxy;
628
	result->custom_sdp_attributes = sal_custom_sdp_attribute_clone(local_capabilities->custom_sdp_attributes);
629

630
	strcpy(result->name,local_capabilities->name);
631 632 633 634

	// Handle session RTCP XR attribute
	memset(&result->rtcp_xr, 0, sizeof(result->rtcp_xr));
	if (remote_offer->rtcp_xr.enabled == TRUE) {
635
		if ((local_capabilities->rtcp_xr.enabled == TRUE) && (local_capabilities->dir == SalStreamSendRecv)) {
636 637 638 639 640 641
			memcpy(&result->rtcp_xr, &local_capabilities->rtcp_xr, sizeof(result->rtcp_xr));
		} else {
			result->rtcp_xr.enabled = TRUE;
		}
	}

642 643
	return 0;
}