Skip to content

Commit f1b33e7

Browse files
committed
Implement ICE lite support and consent freshness
Signed-off-by: Sergio Garcia Murillo <[email protected]>
1 parent 86490ff commit f1b33e7

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
/**
@@ -412,6 +419,9 @@ static av_cold int initialize(AVFormatContext *s)
412419
seed = av_get_random_seed();
413420
av_lfg_init(&whip->rnd, seed);
414421

422+
/* 64-bit tie-breaker for ICE-CONTROLLING (RFC 8445 6.1.1) */
423+
whip->ice_tie_breaker = ((uint64_t)av_lfg_get(&whip->rnd) << 32) | (uint64_t)av_lfg_get(&whip->rnd);
424+
415425
if (whip->pkt_size < ideal_pkt_size)
416426
av_log(whip, AV_LOG_WARNING, "WHIP: pkt_size=%d(<%d) is too small, may cause packet loss\n",
417427
whip->pkt_size, ideal_pkt_size);
@@ -894,6 +904,8 @@ static int parse_answer(AVFormatContext *s)
894904
goto end;
895905
}
896906
}
907+
} else if (av_strstart(line, "a=ice-lite", NULL)) {
908+
whip->ice_lite_remote = 1;
897909
}
898910
}
899911

@@ -985,6 +997,22 @@ static int ice_create_request(AVFormatContext *s, uint8_t *buf, int buf_size, in
985997
avio_wb16(pb, STUN_ATTR_USE_CANDIDATE); /* attribute type use-candidate */
986998
avio_wb16(pb, 0); /* size of use-candidate */
987999

1000+
/**
1001+
* For ICE-lite peers we are *always* the controlling agent (RFC 8445 6.1.3.1).
1002+
* Add PRIORITY + ICE-CONTROLLING attributes.
1003+
*/
1004+
if (whip->ice_lite_remote) {
1005+
/* we are controlling, use host-candidate priority 126 << 24 | 65535 << 8 | 255 = 2130706431 */
1006+
avio_wb16(pb, STUN_ATTR_PRIORITY);
1007+
avio_wb16(pb, 4);
1008+
avio_wb32(pb, 2130706431);
1009+
1010+
avio_wb16(pb, STUN_ATTR_ICE_CONTROLLING);
1011+
avio_wb16(pb, 8);
1012+
avio_wb32(pb, (uint32_t)(whip->ice_tie_breaker >> 32));
1013+
avio_wb32(pb, (uint32_t)(whip->ice_tie_breaker & 0xffffffff));
1014+
}
1015+
9881016
/* Build and update message integrity */
9891017
avio_wb16(pb, STUN_ATTR_MESSAGE_INTEGRITY); /* attribute type message integrity */
9901018
avio_wb16(pb, 20); /* size of message integrity */

0 commit comments

Comments
 (0)