3 Copyright (C) 2010 Simon MORLAT (simon.morlat@free.fr)
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "ortp/ortp_srtp.h"
24 #include <eXosip2/eXosip.h>
26 #define keywordcmp(key,b) strncmp(key,b,sizeof(key))
30 static char *make_relay_session_id(const char *username, const char *relay){
31 /*ideally this should be a hash of the parameters with a random part*/
35 long long int res=((long long int)s1)<<32 | (long long int) s2;
37 b64_encode(src, sizeof(long long int), tmp, sizeof(tmp));
38 return osip_strdup(tmp);
42 static void add_relay_info(sdp_message_t *sdp, int mline, const char *relay, const char *relay_session_id){
44 if (relay) sdp_message_a_attribute_add(sdp, mline,
45 osip_strdup ("relay-addr"),osip_strdup(relay));
46 if (relay_session_id) sdp_message_a_attribute_add(sdp, mline,
47 osip_strdup ("relay-session-id"), osip_strdup(relay_session_id));
52 static char * int_2char(int a){
53 char *p=osip_malloc(16);
54 snprintf(p,16,"%i",a);
58 /* return the value of attr "field" for payload pt at line pos (field=rtpmap,fmtp...)*/
59 static const char *sdp_message_a_attr_value_get_with_pt(sdp_message_t *sdp,int pos,int pt,const char *field)
61 int i,tmppt=0,scanned=0;
63 sdp_attribute_t *attr;
64 for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
65 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
66 int nb = sscanf(attr->a_att_value,"%i %n",&tmppt,&scanned);
67 /* the return value may depend on how %n is interpreted by the libc: see manpage*/
68 if (nb == 1 || nb==2 ){
70 tmp=attr->a_att_value+scanned;
74 }else ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
81 /* return the value of attr "field" */
82 static const char *sdp_message_a_attr_value_get(sdp_message_t *sdp,int pos,const char *field)
85 sdp_attribute_t *attr;
86 for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
87 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
88 return attr->a_att_value;
95 static int _sdp_message_get_a_ptime(sdp_message_t *sdp, int mline){
97 sdp_attribute_t *attr;
98 for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
99 if (keywordcmp("ptime",attr->a_att_field)==0){
100 int nb = sscanf(attr->a_att_value,"%i",&ret);
101 /* the return value may depend on how %n is interpreted by the libc: see manpage*/
104 }else ms_warning("sdp has a strange a=ptime line (%s) ",attr->a_att_value);
110 static int _sdp_message_get_mline_dir(sdp_message_t *sdp, int mline){
112 sdp_attribute_t *attr;
113 for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
114 if (keywordcmp("sendrecv",attr->a_att_field)==0){
115 return SalStreamSendRecv;
116 }else if (keywordcmp("sendonly",attr->a_att_field)==0){
117 return SalStreamSendOnly;
118 }else if (keywordcmp("recvonly",attr->a_att_field)==0){
119 return SalStreamRecvOnly;
120 }else if (keywordcmp("inactive",attr->a_att_field)==0){
121 return SalStreamInactive;
124 return SalStreamSendRecv;
127 static sdp_message_t *create_generic_sdp(const SalMediaDescription *desc, const IceSession *ice_session)
129 sdp_message_t *local;
133 const char *rtp_addr = desc->addr;
135 snprintf(sessid,16,"%i",desc->session_id);
136 snprintf(sessver,16,"%i",desc->session_ver);
137 sdp_message_init (&local);
138 if (strchr(desc->addr,':')!=NULL){
141 sdp_message_v_version_set (local, osip_strdup ("0"));
142 sdp_message_o_origin_set (local, osip_strdup (desc->username),
143 osip_strdup (sessid), osip_strdup (sessver),
144 osip_strdup ("IN"), inet6 ? osip_strdup("IP6") : osip_strdup ("IP4"),
145 osip_strdup (desc->addr));
146 sdp_message_s_name_set (local, osip_strdup ("Talk"));
147 if ((ice_session != NULL) && (ice_session_check_list(ice_session, 0) != NULL)) {
148 if (ice_session_state(ice_session) == IS_Completed) {
149 ice_check_list_nominated_valid_local_candidate(ice_session_check_list(ice_session, 0), &rtp_addr, NULL, NULL, NULL);
152 ice_check_list_default_local_candidate(ice_session_check_list(ice_session, 0), &rtp_addr, NULL, NULL, NULL);
155 if(!sal_media_description_has_dir (desc,SalStreamSendOnly))
157 sdp_message_c_connection_add (local, -1,
158 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
159 osip_strdup (rtp_addr), NULL, NULL);
163 sdp_message_c_connection_add (local, -1,
164 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
165 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
167 sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
168 if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
169 int_2char(desc->bandwidth));
170 if ((ice_session != NULL) && (ice_session_check_list(ice_session, 0) != NULL)) {
172 switch (ice_session_state(ice_session)) {
174 sdp_message_a_attribute_add(local, -1, osip_strdup("nortpproxy"), osip_strdup("yes"));
175 /* No break to also include the ice-ufrag and ice-pwd attributes when ICE session is completed. */
178 snprintf(buffer, sizeof(buffer), "%s", ice_session_local_pwd(ice_session));
179 sdp_message_a_attribute_add(local, -1, osip_strdup("ice-pwd"), osip_strdup(buffer));
180 snprintf(buffer, sizeof(buffer), "%s", ice_session_local_ufrag(ice_session));
181 sdp_message_a_attribute_add(local, -1, osip_strdup("ice-ufrag"), osip_strdup(buffer));
192 static bool_t is_known_rtpmap(const PayloadType *pt){
193 switch(payload_type_get_number(pt)){
203 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
206 sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
208 if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
210 snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt),
211 pt->mime_type, pt->clock_rate,pt->channels);
213 snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt),
214 pt->mime_type, pt->clock_rate);
215 sdp_message_a_attribute_add (msg, line,
216 osip_strdup ("rtpmap"), osip_strdup(attr));
219 if (pt->recv_fmtp != NULL)
221 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
222 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
227 static void add_candidate_attribute(sdp_message_t *msg, int lineno, const IceCandidate *candidate)
232 nb = snprintf(buffer, sizeof(buffer), "%s %d UDP %d %s %d typ %s",
233 candidate->foundation, candidate->componentID, candidate->priority, candidate->taddr.ip, candidate->taddr.port, ice_candidate_type(candidate));
235 ms_error("Cannot add ICE candidate attribute!");
238 if (candidate->type != ICT_HostCandidate) {
239 nb = snprintf(buffer + nb, sizeof(buffer) - nb, " raddr %s rport %d", candidate->base->taddr.ip, candidate->base->taddr.port);
241 ms_error("Cannot add ICE candidate attribute!");
245 sdp_message_a_attribute_add(msg, lineno, osip_strdup("candidate"), osip_strdup(buffer));
248 static void add_ice_candidates(sdp_message_t *msg, int lineno, const IceCheckList *ice_cl, const char *rtp_addr, int rtp_port, const char *rtcp_addr, int rtcp_port)
250 const IceCandidate *candidate;
253 if ((ice_check_list_state(ice_cl) == ICL_Failed) && ice_check_list_is_mismatch(ice_cl)) {
254 sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-mismatch"), NULL);
257 for (i = 0; i < ms_list_size(ice_cl->local_candidates); i++) {
258 candidate = ms_list_nth_data(ice_cl->local_candidates, i);
259 switch (ice_check_list_state(ice_cl)) {
261 add_candidate_attribute(msg, lineno, candidate);
264 /* Only include the candidates matching the default destination for each component of the stream as specified in RFC5245 section 9.1.2.2. */
265 if (((candidate->taddr.port == rtp_port) && (strlen(candidate->taddr.ip) == strlen(rtp_addr)) && (strcmp(candidate->taddr.ip, rtp_addr) == 0))
266 || ((candidate->taddr.port == rtcp_port) && (strlen(candidate->taddr.ip) == strlen(rtcp_addr)) && (strcmp(candidate->taddr.ip, rtcp_addr) == 0))) {
267 add_candidate_attribute(msg, lineno, candidate);
276 static void add_ice_remote_candidates(sdp_message_t *msg, int lineno, const IceCheckList *ice_cl)
279 const char *rtp_addr = NULL;
280 const char *rtcp_addr = NULL;
285 if ((ice_session_role(ice_cl->session) == IR_Controlling) && (ice_check_list_state(ice_cl) == ICL_Completed)) {
286 ice_check_list_nominated_valid_remote_candidate(ice_cl, &rtp_addr, &rtp_port, &rtcp_addr, &rtcp_port);
287 nb = snprintf(buffer, sizeof(buffer), "1 %s %d", rtp_addr, rtp_port);
289 ms_error("Cannot add ICE remote-candidates attribute!");
292 if (rtcp_addr != NULL) {
293 nb = snprintf(buffer + nb, sizeof(buffer) - nb, " 2 %s %d", rtcp_addr, rtcp_port);
295 ms_error("Cannot add ICE remote-candidates attribute!");
299 sdp_message_a_attribute_add(msg, lineno, osip_strdup("remote-candidates"), osip_strdup(buffer));
303 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc, const IceCheckList *ice_cl){
306 const char *rtp_addr;
307 const char *rtcp_addr;
308 const char *dir="sendrecv";
311 bool_t strip_well_known_rtpmaps;
313 switch (desc->type) {
324 rtp_addr=rtcp_addr=desc->rtp_addr;
325 rtp_port=desc->rtp_port;
326 rtcp_port=desc->rtcp_port;
327 if (ice_cl != NULL) {
328 if (ice_check_list_state(ice_cl) == ICL_Completed) {
329 ice_check_list_nominated_valid_local_candidate(ice_cl, &rtp_addr, &rtp_port, &rtcp_addr, &rtcp_port);
331 ice_check_list_default_local_candidate(ice_cl, &rtp_addr, &rtp_port, &rtcp_addr, &rtcp_port);
333 } else if (desc->candidates[0].addr[0]!='\0'){
334 rtp_addr=desc->candidates[0].addr;
335 rtp_port=desc->candidates[0].port;
338 if (desc->proto == SalProtoRtpSavp) {
341 sdp_message_m_media_add (msg, osip_strdup (mt),
342 int_2char (rtp_port), NULL,
343 osip_strdup ("RTP/SAVP"));
345 /* add crypto lines */
346 for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
348 switch (desc->crypto[i].algo) {
349 case AES_128_SHA1_80:
350 snprintf(buffer, 1024, "%d %s inline:%s",
351 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_80", desc->crypto[i].master_key);
352 sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
353 osip_strdup(buffer));
355 case AES_128_SHA1_32:
356 snprintf(buffer, 1024, "%d %s inline:%s",
357 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_32", desc->crypto[i].master_key);
358 sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
359 osip_strdup(buffer));
361 case AES_128_NO_AUTH:
362 ms_warning("Unsupported crypto suite: AES_128_NO_AUTH");
364 case NO_CIPHER_SHA1_80:
365 ms_warning("Unsupported crypto suite: NO_CIPHER_SHA1_80");
368 i = SAL_CRYPTO_ALGO_MAX;
373 sdp_message_m_media_add (msg, osip_strdup (mt),
374 int_2char (rtp_port), NULL,
375 osip_strdup ("RTP/AVP"));
379 /*only add a c= line within the stream description if address are differents*/
380 if (rtp_addr[0]!='\0' && strcmp(rtp_addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
382 if (strchr(rtp_addr,':')!=NULL){
385 sdp_message_c_connection_add (msg, lineno,
386 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
387 osip_strdup (rtp_addr), NULL, NULL);
390 if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
391 int_2char(desc->bandwidth));
392 if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
393 int_2char(desc->ptime));
394 strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
396 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
397 add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
400 /* to comply with SDP we cannot have an empty payload type number list */
401 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
402 sdp_message_m_payload_add (msg,lineno, int_2char (0));
405 case SalStreamSendRecv:
409 case SalStreamRecvOnly:
412 case SalStreamSendOnly:
415 case SalStreamInactive:
419 if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
420 if (ice_cl != NULL) {
421 if (strcmp(rtp_addr, rtcp_addr) != 0) {
423 snprintf(buffer, sizeof(buffer), "%u IN IP4 %s", rtcp_port, rtcp_addr);
424 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), osip_strdup(buffer));
426 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), int_2char(rtcp_port));
428 add_ice_candidates(msg, lineno, ice_cl, rtp_addr, rtp_port, rtcp_addr, rtcp_port);
429 add_ice_remote_candidates(msg, lineno, ice_cl);
434 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc, const IceSession *ice_session){
435 IceCheckList *ice_cl = NULL;
437 sdp_message_t *msg=create_generic_sdp(desc, ice_session);
438 for(i=0;i<desc->nstreams;++i){
439 if (ice_session != NULL) ice_cl = ice_session_check_list(ice_session, i);
441 add_line(msg,i,&desc->streams[i], ice_cl);
446 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
448 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
450 pt->mime_type=ms_strdup(refpt->mime_type);
451 pt->clock_rate=refpt->clock_rate;
453 ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
454 payload_type_get_number(pt));
458 char *mime=ms_strdup(rtpmap);
459 char *p=strchr(mime,'/');
468 pt->channels=atoi(chans);
469 }else pt->channels=1;
470 pt->clock_rate=atoi(p);
477 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc, IceSession **ice_session){
479 const char *mtype,*proto,*rtp_port,*rtp_addr,*number;
480 const char *ice_ufrag, *ice_pwd, *ice_remote_candidates=NULL;
481 sdp_bandwidth_t *sbw=NULL;
482 sdp_attribute_t *attr;
483 int media_attribute_nb;
484 bool_t ice_session_just_created = FALSE;
485 bool_t ice_lite = FALSE;
487 rtp_addr=sdp_message_c_addr_get (msg, -1, 0);
489 strncpy(desc->addr,rtp_addr,sizeof(desc->addr));
490 for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
491 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
494 /* for each m= line */
495 for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
497 SalStreamDescription *stream=&desc->streams[i];
499 memset(stream,0,sizeof(*stream));
500 mtype = sdp_message_m_media_get(msg, i);
501 proto = sdp_message_m_proto_get (msg, i);
502 rtp_port = sdp_message_m_port_get(msg, i);
503 stream->proto=SalProtoUnknown;
505 if (strcasecmp(proto,"RTP/AVP")==0)
506 stream->proto=SalProtoRtpAvp;
507 else if (strcasecmp(proto,"RTP/SAVP")==0){
508 stream->proto=SalProtoRtpSavp;
511 rtp_addr = sdp_message_c_addr_get (msg, i, 0);
512 if (rtp_addr != NULL)
513 strncpy(stream->rtp_addr,rtp_addr,sizeof(stream->rtp_addr));
515 stream->rtp_port=atoi(rtp_port);
517 stream->ptime=_sdp_message_get_a_ptime(msg,i);
518 if (strcasecmp("audio", mtype) == 0){
519 stream->type=SalAudio;
520 }else if (strcasecmp("video", mtype) == 0){
521 stream->type=SalVideo;
523 stream->type=SalOther;
524 strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
526 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
527 if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
529 stream->dir=_sdp_message_get_mline_dir(msg,i);
530 media_attribute_nb = 0;
531 /* for each payload type */
532 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
533 const char *rtpmap,*fmtp;
534 int ptn=atoi(number);
535 PayloadType *pt=payload_type_new();
536 payload_type_set_number(pt,ptn);
537 /* get the rtpmap associated to this codec, if any */
538 rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
539 if (rtpmap != NULL) media_attribute_nb++;
540 if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
541 /* get the fmtp, if any */
542 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
543 if (fmtp != NULL) media_attribute_nb++;
544 payload_type_set_send_fmtp(pt,fmtp);
545 stream->payloads=ms_list_append(stream->payloads,pt);
546 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
547 pt->send_fmtp ? pt->send_fmtp : "");
551 /* Get media specific RTCP attribute */
552 stream->rtcp_port = stream->rtp_port + 1;
553 snprintf(stream->rtcp_addr, sizeof(stream->rtcp_addr), stream->rtp_addr);
554 for (j = 0; ((attr = sdp_message_attribute_get(msg, i, j)) != NULL); j++) {
555 if ((keywordcmp("rtcp", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
557 int nb = sscanf(attr->a_att_value, "%d IN IP4 %s", &stream->rtcp_port, tmp);
559 /* SDP rtcp attribute only contains the port */
560 } else if (nb == 2) {
561 strncpy(stream->rtcp_addr, tmp, sizeof(stream->rtcp_addr));
563 ms_warning("sdp has a strange a= line (%s) nb=%i", attr->a_att_value, nb);
568 /* read crypto lines if any */
569 if (stream->proto == SalProtoRtpSavp) {
570 int k, valid_count = 0;
572 memset(&stream->crypto, 0, sizeof(stream->crypto));
573 for (k=0;valid_count < SAL_CRYPTO_ALGO_MAX && (attr=sdp_message_attribute_get(msg,i,k))!=NULL;k++){
574 char tmp[256], tmp2[256];
575 if (keywordcmp("crypto",attr->a_att_field)==0 && attr->a_att_value!=NULL){
576 int nb = sscanf(attr->a_att_value, "%d %256s inline:%256s",
577 &stream->crypto[valid_count].tag,
580 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'",
581 stream->crypto[valid_count].tag,
585 if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_80") == 0)
586 stream->crypto[valid_count].algo = AES_128_SHA1_80;
587 else if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_32") == 0)
588 stream->crypto[valid_count].algo = AES_128_SHA1_32;
590 ms_warning("Failed to parse crypto-algo: '%s'", tmp);
591 stream->crypto[valid_count].algo = 0;
593 if (stream->crypto[valid_count].algo) {
594 strncpy(stream->crypto[valid_count].master_key, tmp2, 41);
595 stream->crypto[valid_count].master_key[40] = '\0';
596 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'",
597 stream->crypto[valid_count].tag,
599 stream->crypto[valid_count].master_key);
603 ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
607 ms_message("Found: %d valid crypto lines", valid_count);
610 /* Get ICE candidate attributes if any */
611 ice_ufrag = ice_pwd = NULL;
612 for (j = 0; (j < SAL_MEDIA_DESCRIPTION_MAX_ICE_CANDIDATES) && ((attr = sdp_message_attribute_get(msg, i, media_attribute_nb + j)) != NULL); j++) {
613 if ((keywordcmp("candidate", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
617 unsigned int priority;
618 unsigned int componentID;
622 /* Allocate the ICE session if it has not been done yet. */
623 if (*ice_session == NULL) {
624 *ice_session = ice_session_new();
625 ice_session_just_created = TRUE;
627 /* Allocate the ICE check list if it has not been done yet. */
628 if (ice_session_check_list(*ice_session, i) == NULL) {
629 ice_session_add_check_list(*ice_session, ice_check_list_new());
631 nb = sscanf(attr->a_att_value, "%s %u UDP %u %s %u typ %s",
632 foundation, &componentID, &priority, ip, &port, type);
634 char *default_ip = desc->addr;
635 unsigned int default_port = stream->rtp_port;
636 bool_t is_default_candidate = FALSE;
637 if (componentID == 1) {
638 if ((stream->rtp_addr == NULL) || (stream->rtp_addr[0] == '\0')) default_ip = desc->addr;
639 else default_ip = stream->rtp_addr;
640 default_port = stream->rtp_port;
641 } else if (componentID == 2) {
642 if ((stream->rtcp_addr == NULL) || (stream->rtcp_addr[0] == '\0')) default_ip = desc->addr;
643 else default_ip = stream->rtcp_addr;
644 default_port = stream->rtcp_port;
646 if ((port == default_port) && (strlen(ip) == strlen(default_ip)) && (strcmp(ip, default_ip) == 0)) is_default_candidate = TRUE;
647 ice_add_remote_candidate(ice_session_check_list(*ice_session, i), type, ip, port, componentID, priority, foundation, is_default_candidate);
649 } else if ((keywordcmp("remote-candidates", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
650 ice_remote_candidates = attr->a_att_value;
651 } else if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
652 ice_ufrag = attr->a_att_value;
653 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
654 ice_pwd = attr->a_att_value;
655 } else if (keywordcmp("ice-mismatch", attr->a_att_field) == 0) {
656 ice_check_list_set_state(ice_session_check_list(*ice_session, i), ICL_Failed);
659 if ((*ice_session != NULL) && ice_session_check_list(*ice_session, i)) {
660 if (ice_remote_candidates != NULL) {
663 unsigned int componentID;
666 while (3 == sscanf(ice_remote_candidates, "%u %s %u%n", &componentID, ip, &port, &offset)) {
667 if (componentID == 1) {
668 if ((stream->rtp_addr == NULL) || (stream->rtp_addr[0] == '\0')) rtp_addr = desc->addr;
669 else rtp_addr = stream->rtp_addr;
670 ice_add_losing_pair(ice_session_check_list(*ice_session, i), componentID, ip, port, rtp_addr, stream->rtp_port);
671 } else if (componentID == 2) {
672 if ((stream->rtcp_addr == NULL) || (stream->rtcp_addr[0] == '\0')) rtp_addr = desc->addr;
673 else rtp_addr = stream->rtcp_addr;
674 ice_add_losing_pair(ice_session_check_list(*ice_session, i), componentID, ip, port, rtp_addr, stream->rtcp_port);
676 ice_remote_candidates += offset;
677 if (ice_remote_candidates[offset] == ' ') ice_remote_candidates += 1;
680 if ((ice_ufrag != NULL) && (ice_pwd != NULL)) {
681 ice_check_list_set_remote_credentials(ice_session_check_list(*ice_session, i), ice_ufrag, ice_pwd);
683 if (stream->rtp_port == 0) {
684 /* This stream has been deactivated by the peer, delete the check list. */
685 ice_session_remove_check_list(*ice_session, ice_session_check_list(*ice_session, i));
687 ice_dump_candidates(ice_session_check_list(*ice_session, i));
692 /* Get ICE remote ufrag and remote pwd */
693 ice_ufrag = ice_pwd = NULL;
694 for (i = 0; (i < SAL_MEDIA_DESCRIPTION_MAX_MESSAGE_ATTRIBUTES) && ((attr = sdp_message_attribute_get(msg, -1, i)) != NULL); i++) {
695 if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
696 ice_ufrag = attr->a_att_value;
697 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
698 ice_pwd = attr->a_att_value;
699 } else if (keywordcmp("ice-lite", attr->a_att_field) == 0) {
703 if (*ice_session != NULL) {
705 if (ice_session_just_created == TRUE) {
706 if (ice_lite == TRUE) {
707 ice_session_set_role(*ice_session, IR_Controlling);
709 ice_session_set_role(*ice_session, IR_Controlled);
711 ice_session_check_mismatch(*ice_session);
713 while ((nb_check_lists = ice_session_nb_check_lists(*ice_session)) > desc->nstreams) {
714 ice_session_remove_check_list(*ice_session, ice_session_check_list(*ice_session, nb_check_lists - 1));
716 if ((ice_ufrag != NULL) && (ice_pwd != NULL)) {
717 ice_session_set_remote_credentials(*ice_session, ice_ufrag, ice_pwd);
718 ice_dump_session(*ice_session);
720 if (((ice_session_just_created == FALSE) && ((ice_ufrag == NULL) || (ice_pwd == NULL)))
721 || (ice_session_state(*ice_session) == IS_Failed)) {
722 /* We started with ICE activated but the peer apparently do not support ICE, so stop using it. */
723 ice_session_destroy(*ice_session);