Skip to content

Commit 6f0ed41

Browse files
committed
avfotmat/whip: add support to handle multi NACK in a bundled packet
Signed-off-by: Jack Lau <jacklau1222@qq.com>
1 parent 809f0a5 commit 6f0ed41

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

libavformat/whip.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
/**
155155
* Retransmission / NACK support
156156
*/
157-
#define HISTORY_SIZE_DEFAULT 1024
157+
#define HISTORY_SIZE_DEFAULT 4096
158158

159159
/* Calculate the elapsed time from starttime to endtime in milliseconds. */
160160
#define ELAPSED(starttime, endtime) ((int)(endtime - starttime) / 1000)
@@ -1931,6 +1931,7 @@ static int whip_write_packet(AVFormatContext *s, AVPacket *pkt)
19311931
int ptr = 0;
19321932
while (ptr + 4 <= ret) {
19331933
uint8_t pt = whip->buf[ptr + 1];
1934+
uint8_t fmt = (whip->buf[ptr] & 0x1f);
19341935
/**
19351936
* Refer to RFC 3550, Section 6.4.1.
19361937
* The length of this RTCP packet in 32-bit words minus one,
@@ -1939,9 +1940,8 @@ static int whip_write_packet(AVFormatContext *s, AVPacket *pkt)
19391940
int len = (AV_RB16(&whip->buf[ptr + 2]) + 1) * 4;
19401941
if (ptr + len > ret) break;
19411942

1942-
if (pt == 205) { /* PT=RTPFB */
1943-
uint8_t fmt = (whip->buf[ptr] & 0x1f);
1944-
if (fmt == 1 && len >= 12) { /* FMT=1 */
1943+
if (pt == 205 && fmt == 1 && len >= 12) { /* PT=RTPFB, FMT=1 */
1944+
int i;
19451945
/* SRTCP index(4 bytes) + HMAC (SRTP_AES128_CM_SHA1_80 10bytes) */
19461946
int srtcp_len = len + 4 + 10;
19471947
int ret = ff_srtp_decrypt(&whip->srtp_recv, whip->buf, &srtcp_len);
@@ -1950,12 +1950,13 @@ static int whip_write_packet(AVFormatContext *s, AVPacket *pkt)
19501950
// packet is invalid or authentication failed
19511951
break;
19521952
}
1953+
for (i = 0 ; 14 + i <= len; i = i + 4) {
19531954
/**
19541955
* See https://datatracker.ietf.org/doc/html/rfc4585#section-6.1
1955-
* TODO: Handle multi NACKs in bundled packet.
1956+
* Handle multi NACKs in bundled packet.
19561957
*/
1957-
uint16_t pid = AV_RB16(&whip->buf[ptr + 12]);
1958-
uint16_t blp = AV_RB16(&whip->buf[ptr + 14]);
1958+
uint16_t pid = AV_RB16(&whip->buf[ptr + 12 + i]);
1959+
uint16_t blp = AV_RB16(&whip->buf[ptr + 14 + i]);
19591960

19601961
/* retransmit pid + any bit set in blp */
19611962
for (int bit = -1; bit < 16; bit++) {
@@ -1968,11 +1969,11 @@ static int whip_write_packet(AVFormatContext *s, AVPacket *pkt)
19681969
send_rtx_packet(s, it->pkt, it->size);
19691970
av_log(whip, AV_LOG_INFO, "WHIP: NACK packet found: size: %d, seq=%d, blp=%d\n", it->size, seq, blp);
19701971
} else
1971-
av_log(whip, AV_LOG_INFO, "WHIP: NACK packet, seq=%d, blp=%d, not found\n", seq, blp);
1972+
av_log(whip, AV_LOG_INFO, "WHIP: NACK packet, seq=%d, blp=%d, not found, the latest packet seq: %d\n", seq, blp, whip->history[whip->hist_head-1].seq);
1973+
}
19721974
}
1973-
}
19741975
}
1975-
ptr += len;
1976+
break;
19761977
}
19771978

19781979
}
@@ -2079,7 +2080,7 @@ static const AVOption options[] = {
20792080
{ "authorization", "The optional Bearer token for WHIP Authorization", OFFSET(authorization), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
20802081
{ "cert_file", "The optional certificate file path for DTLS", OFFSET(cert_file), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
20812082
{ "key_file", "The optional private key file path for DTLS", OFFSET(key_file), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
2082-
{ "rtx_history", "Packet history size", OFFSET(history_size), AV_OPT_TYPE_INT, { .i64 = HISTORY_SIZE_DEFAULT }, 64, 2048, DEC },
2083+
{ "rtx_history", "Packet history size", OFFSET(history_size), AV_OPT_TYPE_INT, { .i64 = HISTORY_SIZE_DEFAULT }, 64, INT_MAX, DEC },
20832084
{ NULL },
20842085
};
20852086

0 commit comments

Comments
 (0)