Skip to content

Commit a10ffe5

Browse files
murillo128JackLau1222
authored andcommitted
Implement ICE consent freshness support
Signed-off-by: Sergio Garcia Murillo <[email protected]>
1 parent 7b13d17 commit a10ffe5

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

libavformat/whip.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@
143143
#define WHIP_RTCP_PT_START 192
144144
#define WHIP_RTCP_PT_END 223
145145

146+
/**
147+
* Consent-freshness constants
148+
*/
149+
#define WHIP_CONSENT_DEF_INTERVAL 15000 /* ms - RFC 7675 default */
150+
#define WHIP_CONSENT_MAX_FAILURES 3
151+
146152
/**
147153
* In the case of ICE-LITE, these fields are not used; instead, they are defined
148154
* as constant values.
@@ -303,6 +309,11 @@ typedef struct WHIPContext {
303309
/* The certificate and private key used for DTLS handshake. */
304310
char* cert_file;
305311
char* key_file;
312+
313+
/* Consent-freshness state */
314+
int consent_interval;
315+
int64_t last_consent_tx;
316+
int consent_failures;
306317
} WHIPContext;
307318

308319
/**
@@ -417,6 +428,12 @@ static av_cold int initialize(AVFormatContext *s)
417428
seed = av_get_random_seed();
418429
av_lfg_init(&whip->rnd, seed);
419430

431+
/* Initialise consent-freshness timers */
432+
if (whip->consent_interval <= 0)
433+
whip->consent_interval = WHIP_CONSENT_DEF_INTERVAL;
434+
whip->last_consent_tx = av_gettime();
435+
whip->consent_failures = 0;
436+
420437
if (whip->pkt_size < ideal_pkt_size)
421438
av_log(whip, AV_LOG_WARNING, "pkt_size=%d(<%d) is too small, may cause packet loss\n",
422439
whip->pkt_size, ideal_pkt_size);
@@ -1784,14 +1801,27 @@ static int whip_write_packet(AVFormatContext *s, AVPacket *pkt)
17841801
AVStream *st = s->streams[pkt->stream_index];
17851802
AVFormatContext *rtp_ctx = st->priv_data;
17861803

1787-
/* TODO: Send binding request every 1s as WebRTC heartbeat. */
1804+
/* Periodic consent-freshness STUN Binding Request */
1805+
int64_t now = av_gettime();
1806+
if (now - whip->last_consent_tx >= (int64_t)whip->consent_interval * 1000) {
1807+
int req_sz;
1808+
if (ice_create_request(s, whip->buf, sizeof(whip->buf), &req_sz) >= 0 && ffurl_write(whip->udp, whip->buf, req_sz) == req_sz) {
1809+
whip->consent_failures++;
1810+
whip->last_consent_tx = now;
1811+
av_log(whip, AV_LOG_VERBOSE, "WHIP: consent-freshness request %d sent\n", whip->consent_failures);
1812+
}
1813+
}
17881814

17891815
/**
17901816
* Receive packets from the server such as ICE binding requests, DTLS messages,
17911817
* and RTCP like PLI requests, then respond to them.
17921818
*/
17931819
ret = ffurl_read(whip->udp, whip->buf, sizeof(whip->buf));
17941820
if (ret > 0) {
1821+
if (ice_is_binding_response(whip->buf, ret)) {
1822+
whip->consent_failures = 0;
1823+
av_log(whip, AV_LOG_VERBOSE, "WHIP: consent-freshness response received, counter reset\n");
1824+
}
17951825
if (is_dtls_packet(whip->buf, ret)) {
17961826
if ((ret = ffurl_write(whip->dtls_uc, whip->buf, ret)) < 0) {
17971827
av_log(whip, AV_LOG_ERROR, "Failed to handle DTLS message\n");
@@ -1901,6 +1931,7 @@ static const AVOption options[] = {
19011931
{ "authorization", "The optional Bearer token for WHIP Authorization", OFFSET(authorization), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, ENC },
19021932
{ "cert_file", "The optional certificate file path for DTLS", OFFSET(cert_file), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, ENC },
19031933
{ "key_file", "The optional private key file path for DTLS", OFFSET(key_file), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, ENC },
1934+
{ "consent_interval", "STUN consent refresh interval in ms (RFC 7675)", OFFSET(consent_interval), AV_OPT_TYPE_INT, { .i64 = WHIP_CONSENT_DEF_INTERVAL }, 5000, 30000, ENC },
19041935
{ NULL },
19051936
};
19061937

0 commit comments

Comments
 (0)