Skip to content

Commit 2f93976

Browse files
murillo128JackLau1222
authored andcommitted
Implement ICE lite support
Signed-off-by: Sergio Garcia Murillo <[email protected]>
1 parent 544578b commit 2f93976

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

libavformat/whip.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@
156156
/* STUN Attribute, comprehension-required range (0x0000-0x7FFF) */
157157
enum STUNAttr {
158158
STUN_ATTR_USERNAME = 0x0006, /// shared secret response/bind request
159+
STUN_ATTR_PRIORITY = 0x0024, /// ICE controlling/controlled
159160
STUN_ATTR_USE_CANDIDATE = 0x0025, /// bind request
160161
STUN_ATTR_MESSAGE_INTEGRITY = 0x0008, /// bind request/response
161162
STUN_ATTR_FINGERPRINT = 0x8028, /// rfc5389
163+
STUN_ATTR_ICE_CONTROLLING = 0x802A, /// full agent talking to ice-lite
162164
};
163165

164166
enum WHIPState {
@@ -303,6 +305,11 @@ typedef struct WHIPContext {
303305
/* The certificate and private key used for DTLS handshake. */
304306
char* cert_file;
305307
char* key_file;
308+
309+
310+
/* ICE-lite support */
311+
int ice_lite_remote;
312+
uint64_t ice_tie_breaker; /* random 64-bit, for ICE-CONTROLLING */
306313
} WHIPContext;
307314

308315
/**
@@ -417,6 +424,9 @@ static av_cold int initialize(AVFormatContext *s)
417424
seed = av_get_random_seed();
418425
av_lfg_init(&whip->rnd, seed);
419426

427+
/* 64-bit tie-breaker for ICE-CONTROLLING (RFC 8445 6.1.1) */
428+
whip->ice_tie_breaker = ((uint64_t)av_lfg_get(&whip->rnd) << 32) | (uint64_t)av_lfg_get(&whip->rnd);
429+
420430
if (whip->pkt_size < ideal_pkt_size)
421431
av_log(whip, AV_LOG_WARNING, "pkt_size=%d(<%d) is too small, may cause packet loss\n",
422432
whip->pkt_size, ideal_pkt_size);
@@ -907,6 +917,8 @@ static int parse_answer(AVFormatContext *s)
907917
goto end;
908918
}
909919
}
920+
} else if (av_strstart(line, "a=ice-lite", NULL)) {
921+
whip->ice_lite_remote = 1;
910922
}
911923
}
912924

@@ -998,6 +1010,22 @@ static int ice_create_request(AVFormatContext *s, uint8_t *buf, int buf_size, in
9981010
avio_wb16(pb, STUN_ATTR_USE_CANDIDATE); /* attribute type use-candidate */
9991011
avio_wb16(pb, 0); /* size of use-candidate */
10001012

1013+
/**
1014+
* For ICE-lite peers we are *always* the controlling agent (RFC 8445 6.1.3.1).
1015+
* Add PRIORITY + ICE-CONTROLLING attributes.
1016+
*/
1017+
if (whip->ice_lite_remote) {
1018+
/* we are controlling, use host-candidate priority 126 << 24 | 65535 << 8 | 255 = 2130706431 */
1019+
avio_wb16(pb, STUN_ATTR_PRIORITY);
1020+
avio_wb16(pb, 4);
1021+
avio_wb32(pb, 2130706431);
1022+
1023+
avio_wb16(pb, STUN_ATTR_ICE_CONTROLLING);
1024+
avio_wb16(pb, 8);
1025+
avio_wb32(pb, (uint32_t)(whip->ice_tie_breaker >> 32));
1026+
avio_wb32(pb, (uint32_t)(whip->ice_tie_breaker & 0xffffffff));
1027+
}
1028+
10011029
/* Build and update message integrity */
10021030
avio_wb16(pb, STUN_ATTR_MESSAGE_INTEGRITY); /* attribute type message integrity */
10031031
avio_wb16(pb, 20); /* size of message integrity */

0 commit comments

Comments
 (0)