PR #26284 opened by bonkbeats
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26284
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26284.patch
# Summary of changes
The DTLS context reuses `rtc->udp` as an external socket, and the TLS backends
copy the `AVIO_FLAG_NONBLOCK` flag of the DTLS `URLContext` onto that socket on
every write. Since 69b6af0e68 the DTLS context is no longer marked
non-blocking. So when `whip_write_packet()` receives a DTLS record after the
handshake and passes it to `ffurl_write(rtc->dtls_uc, ...)`, `rtc->udp` becomes
blocking. The next `ffurl_read()` in `whip_write_packet()` (whip.c:975) then
blocks forever, and the consent freshness check (30 s) is never reached.
This patch marks `dtls_uc` non-blocking once the DTLS handshake has completed.
The handshake itself still runs in blocking mode.
### How I found / reproduced it
- Windows 11, MSYS2 UCRT64 build, TLS backend: **Schannel**. Server: MediaMTX
v1.21.4 on localhost.
- Command:
```
ffmpeg -re -f lavfi -i testsrc2=size=1280x720:rate=30 -f lavfi -i
sine=frequency=440:sample_rate=48000 \
-c:v libx264 -preset veryfast -tune zerolatency -bf 0 -g 60 -pix_fmt
yuv420p \
-c:a libopus -ar 48000 -ac 2 -whip_flags dtls_active \
-f whip http://localhost:8889/mystream/whip
```
- In this setup the server rejects the session right after the handshake
(Schannel doesn't negotiate `use_srtp`; I'll send a separate patch for that)
and sends a DTLS alert. FFmpeg passes the alert to `dtls_uc`, and the stream
freezes at frame=4.
- gdb on the hung process: the muxer thread is blocked in `ffurl_read()` at
whip.c:975, and `rtc->udp->flags == 0x3` (NONBLOCK cleared).
### Testing
| Build | Mode | Result |
|---|---|---|
| without patch | `dtls_active` | hangs forever at frame=4 (killed after 50 s) |
| with patch (3 runs) | `dtls_active` | no hang; streams until `Consent
Freshness expired after ~30000ms, terminate session`, then exits |
| with patch | default (passive) | unchanged behaviour |
FATE: `make fate` (without the sample suite) passes, 3257 tests (Windows 11 /
MSYS2 UCRT64 / gcc 16.2). FATE has no WHIP coverage, so the fix itself was
verified manually as described above.
### Notes / open questions
- Other backends (OpenSSL, GnuTLS) use the same flag-copy pattern in their
write paths, so this probably isn't Schannel-specific. I've only tested
Schannel so far.
- Separately, passing a *received* DTLS record to `ffurl_write()` sends it back
to the peer as encrypted application data. Is that intended (e.g. for handshake
retransmissions)? If not, I'd like to follow up with a patch that ends the
session on a received alert instead of echoing it.
<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).
Attached filenames must match the sample's filename:
```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->
>From 24df62f050d04fb7077b549b4ba0b0c34a3008db Mon Sep 17 00:00:00 2001
From: prateek rao <[email protected]>
Date: Mon, 23 Nov 2026 10:06:41 +0530
Subject: [PATCH] avformat/rtc: keep the shared UDP socket non-blocking after
DTLS handshake
The DTLS context reuses rtc->udp as an external socket, and the TLS
backends copy the NONBLOCK flag of the DTLS URLContext onto that socket
on every write. Commit 69b6af0e68 stopped marking the DTLS context as
non-blocking, so any write through it after the handshake (e.g. when
whip_write_packet() passes a received DTLS record to dtls_uc) turns
rtc->udp into a blocking socket. The next ffurl_read() in
whip_write_packet() then blocks forever, and the consent freshness
timeout is never reached.
Mark dtls_uc non-blocking once the handshake has completed.
Signed-off-by: prateek rao <[email protected]>
---
libavformat/rtc.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/libavformat/rtc.c b/libavformat/rtc.c
index 05f4f682a80..96150e40af9 100644
--- a/libavformat/rtc.c
+++ b/libavformat/rtc.c
@@ -832,6 +832,9 @@ next_packet:
if (!ret) {
rtc->state = RTC_STATE_DTLS_FINISHED;
rtc->dtls_time = av_gettime_relative();
+ /* dtls_uc shares rtc->udp; TLS backends copy dtls_uc's
NONBLOCK
+ * flag onto it on every write, so keep the socket
non-blocking. */
+ rtc->dtls_uc->flags |= AVIO_FLAG_NONBLOCK;
av_log(rtc->ctx, AV_LOG_VERBOSE, "DTLS handshake is done,
elapsed=%.2fms\n",
RTC_ELAPSED(rtc->starttime, rtc->dtls_time));
}
--
2.54.1
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]