|
39 | 39 | import java.util.regex.Matcher; |
40 | 40 | import java.util.regex.Pattern; |
41 | 41 |
|
| 42 | +import dev.onvoid.webrtc.RTCBundlePolicy; |
| 43 | +import dev.onvoid.webrtc.RTCConfiguration; |
42 | 44 | import dev.onvoid.webrtc.RTCIceServer; |
43 | 45 | import dev.onvoid.webrtc.RTCSdpType; |
44 | 46 | import dev.onvoid.webrtc.RTCSessionDescription; |
@@ -273,7 +275,7 @@ private static CompletableFuture<URI> negotiate(WebRtc rtc, PeerImpl peer, URI e |
273 | 275 | }); |
274 | 276 |
|
275 | 277 | peer.offer() |
276 | | - .thenCompose(offer -> post(offerRequest, endpoint, offer.sdp, protocol, resource) |
| 278 | + .thenCompose(offer -> post(offerRequest, endpoint, withRtcpMuxOnly(offer.sdp), protocol, resource) |
277 | 279 | .whenComplete((reply, error) -> { |
278 | 280 | if (finished.get()) { |
279 | 281 | // The session ended while the offer was out, |
@@ -400,7 +402,8 @@ private static CompletableFuture<Void> answerCounterOffer(WebRtc rtc, HttpClient |
400 | 402 | resource.set(reply.session); |
401 | 403 | } |
402 | 404 |
|
403 | | - return answerRequest.send("application/sdp", answer.sdp.getBytes(StandardCharsets.UTF_8), 0, |
| 405 | + return answerRequest.send("application/sdp", |
| 406 | + withRtcpMuxOnly(answer.sdp).getBytes(StandardCharsets.UTF_8), 0, |
404 | 407 | head -> { }).handle((response, error) -> { |
405 | 408 | if (error != null) { |
406 | 409 | throw new WebRtcException("Sending the answer to " + reply.session + " failed: " |
@@ -632,6 +635,56 @@ static URI resourceOf(URI endpoint, String location) { |
632 | 635 | return resource; |
633 | 636 | } |
634 | 637 |
|
| 638 | + /** |
| 639 | + * Bundles all media on one transport, as RFC 9725 asks (4.4.1), so that |
| 640 | + * only the m-line that tags the BUNDLE group gathers candidates, the ones |
| 641 | + * that go to the session. |
| 642 | + */ |
| 643 | + static void requireBundle(RTCConfiguration config) { |
| 644 | + config.bundlePolicy = RTCBundlePolicy.MAX_BUNDLE; |
| 645 | + } |
| 646 | + |
| 647 | + /** |
| 648 | + * Marks each m-line that multiplexes RTP and RTCP as rtcp-mux-only, as RFC |
| 649 | + * 9725 asks of what a client sends (4.4.1). WebRTC multiplexes them anyway, |
| 650 | + * by {@code RTCRtcpMuxPolicy.REQUIRE}, but does not say so; only the |
| 651 | + * description that goes out says it, the one applied stays as WebRTC made |
| 652 | + * it. |
| 653 | + * |
| 654 | + * @param sdp a description WebRTC made. |
| 655 | + * |
| 656 | + * @return the description with the attribute, where it was missing. |
| 657 | + */ |
| 658 | + static String withRtcpMuxOnly(String sdp) { |
| 659 | + String[] lines = sdp.split("\r?\n", -1); |
| 660 | + StringBuilder marked = new StringBuilder(sdp.length() + 64); |
| 661 | + boolean present = false; |
| 662 | + |
| 663 | + for (int i = 0; i < lines.length; i++) { |
| 664 | + String line = lines[i]; |
| 665 | + |
| 666 | + if (line.startsWith("m=")) { |
| 667 | + present = false; |
| 668 | + |
| 669 | + for (int j = i + 1; j < lines.length && !lines[j].startsWith("m="); j++) { |
| 670 | + present |= lines[j].equals("a=rtcp-mux-only"); |
| 671 | + } |
| 672 | + } |
| 673 | + |
| 674 | + marked.append(line); |
| 675 | + |
| 676 | + if (i < lines.length - 1) { |
| 677 | + marked.append("\r\n"); |
| 678 | + } |
| 679 | + if (line.equals("a=rtcp-mux") && !present) { |
| 680 | + marked.append("a=rtcp-mux-only\r\n"); |
| 681 | + present = true; |
| 682 | + } |
| 683 | + } |
| 684 | + |
| 685 | + return marked.toString(); |
| 686 | + } |
| 687 | + |
635 | 688 | static URI checkEndpoint(URI endpoint) { |
636 | 689 | requireNonNull(endpoint, "endpoint"); |
637 | 690 |
|
|
0 commit comments