From 940ebfb734452bb082c4f0d8105bedb60c985eaf Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Wed, 28 Oct 2026 22:40:47 +0100 Subject: [PATCH] fix: send WHIP offers sendonly RFC 9725 4.2: a WHIP client should use sendonly. The peer's transceivers are made send-only before the offer, in the order of the negotiation steps; should WebRTC refuse, the offer stays sendrecv, which WHIP allows. --- docs/guide/client-api.md | 2 +- .../dev/onvoid/webrtc/client/PeerImpl.java | 22 +++++++++++++++++++ .../java/dev/onvoid/webrtc/client/Whip.java | 5 +++++ .../webrtc/client/HttpSessionTests.java | 3 +++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/guide/client-api.md b/docs/guide/client-api.md index de676fc4f..6c2844e43 100644 --- a/docs/guide/client-api.md +++ b/docs/guide/client-api.md @@ -598,7 +598,7 @@ Whep.Session viewer = Whep.from(URI.create("https://example.com/live/whep")) viewer.peer().onTrack(track -> System.out.println("Receiving " + track.kind())); ``` -By default, the offer goes to the endpoint at once, before any candidate is gathered, and the future completes once the peer is connected. As RFC 9725 asks, the peer bundles all media on one transport (`max-bundle`), and the descriptions it sends mark each m-line `rtcp-mux-only`. If the endpoint refuses the offer, or the peer does not connect within the timeout (15 seconds by default), the future fails, and the session is deleted on the server again. An endpoint that is busy, with status 503, or a WHEP endpoint whose stream has yet to start, with status 409, gets the offer again after the wait its `Retry-After` names, at least a second, as long as that fits within the timeout; without `Retry-After` the session fails at once. A WHEP endpoint may also reject the offer and counter it with one of its own, with status 406: `Whep` then takes its offer back, answers the endpoint's, receiving only, and sends the answer by `PATCH` to the session the endpoint named, before the counter-offer's `valid-until`, 30 seconds by default. The answer rejects the audio or video of the counter-offer that `receive(...)` leaves out, and the session fails if the counter-offer holds none of what it asks for. Data channels of a counter-offer are accepted. Cancelling the future, say once `get` gave up waiting, ends the session the same way. `onTrack` hands over tracks that already arrived, so a handler added once the future completed misses none. +By default, the offer goes to the endpoint at once, before any candidate is gathered, and the future completes once the peer is connected. As RFC 9725 asks, the peer bundles all media on one transport (`max-bundle`), the descriptions it sends mark each m-line `rtcp-mux-only`, and a WHIP offer only sends (`sendonly`). If the endpoint refuses the offer, or the peer does not connect within the timeout (15 seconds by default), the future fails, and the session is deleted on the server again. An endpoint that is busy, with status 503, or a WHEP endpoint whose stream has yet to start, with status 409, gets the offer again after the wait its `Retry-After` names, at least a second, as long as that fits within the timeout; without `Retry-After` the session fails at once. A WHEP endpoint may also reject the offer and counter it with one of its own, with status 406: `Whep` then takes its offer back, answers the endpoint's, receiving only, and sends the answer by `PATCH` to the session the endpoint named, before the counter-offer's `valid-until`, 30 seconds by default. The answer rejects the audio or video of the counter-offer that `receive(...)` leaves out, and the session fails if the counter-offer holds none of what it asks for. Data channels of a counter-offer are accepted. Cancelling the future, say once `get` gave up waiting, ends the session the same way. `onTrack` hands over tracks that already arrived, so a handler added once the future completed misses none. With the default `Gathering.TRICKLE`, only once the endpoint answered does the peer gather its candidates, with the STUN and TURN servers the endpoint names in `Link` headers with `rel="ice-server"`, unless `iceServer(...)` or `turnServer(...)` names servers of its own, which take precedence. The candidates follow the offer by `PATCH` to the session, as `application/trickle-ice-sdpfrag`, a few at a time and with the session's `ETag` as `If-Match`, as RFC 9725 describes. An endpoint that takes no such `PATCH`, with status 405, 415, 422 or 501, gets no more; it may still learn peer-reflexive candidates from incoming connectivity checks, but that does not cover every network topology. A 412 or 428 for the current ICE generation reaches the error handler and stops candidate PATCHes; responses to a generation replaced by a restart are ignored. diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java b/webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java index dedc3bd09..889400fb5 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java @@ -457,6 +457,28 @@ CompletableFuture applyLocal(RTCSessionDescription description) { return negotiator.run(() -> setLocal(description)); } + /** + * Makes every transceiver send only, as a WHIP client's offer should be + * (RFC 9725, 4.2). Ordered with the negotiation steps, so it takes effect + * on the next offer. + */ + CompletableFuture sendOnly() { + return negotiator.run(() -> { + for (RTCRtpTransceiver transceiver : connection.getTransceivers()) { + try { + if (!transceiver.stopped()) { + transceiver.setDirection(RTCRtpTransceiverDirection.SEND_ONLY); + } + } + finally { + transceiver.dispose(); + } + } + + return CompletableFuture.completedFuture(null); + }); + } + /** * Gathers candidates from now on with the given ICE servers, unless the * peer was built with servers of its own, which take precedence. diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/client/Whip.java b/webrtc/src/main/java/dev/onvoid/webrtc/client/Whip.java index b97ac5cba..1c6bd931d 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/client/Whip.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/client/Whip.java @@ -232,6 +232,11 @@ public CompletableFuture publish(WebRtc rtc) { peer.addTrack(track); } + // Before the offer, which follows in the same order: WHIP only + // sends (RFC 9725, 4.2). Should WebRTC refuse, the offer stays + // sendrecv, which WHIP allows as well. + peer.sendOnly(); + return HttpSession.start(rtc, peer, endpoint, token, limit, gathering, PROTOCOL, Session::new); } catch (RuntimeException | Error e) { diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/client/HttpSessionTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/client/HttpSessionTests.java index 0be9cdfda..f85985770 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/client/HttpSessionTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/client/HttpSessionTests.java @@ -360,6 +360,9 @@ void offerBundledAndMuxed() throws Exception { assertEquals(RTCBundlePolicy.MAX_BUNDLE, policy); assertEquals(2, offer.split("\r\na=rtcp-mux-only\r\n", -1).length - 1, offer); assertEquals(2, offer.split("\r\nm=", -1).length - 1, offer); + // RFC 9725, 4.2: a WHIP client should only send. + assertEquals(2, offer.split("\r\na=sendonly\r\n", -1).length - 1, offer); + assertFalse(offer.contains("a=sendrecv"), offer); await(session.closeAsync()); }