|
21 | 21 | import java.time.Duration; |
22 | 22 | import java.util.ArrayDeque; |
23 | 23 | import java.util.ArrayList; |
| 24 | +import java.util.Arrays; |
24 | 25 | import java.util.Collections; |
25 | 26 | import java.util.HashMap; |
| 27 | +import java.util.EnumMap; |
26 | 28 | import java.util.EnumSet; |
27 | 29 | import java.util.HashSet; |
| 30 | +import java.util.LinkedHashMap; |
| 31 | +import java.util.LinkedHashSet; |
28 | 32 | import java.util.List; |
| 33 | +import java.util.Locale; |
29 | 34 | import java.util.Map; |
30 | 35 | import java.util.Set; |
31 | 36 | import java.util.UUID; |
|
56 | 61 | import dev.onvoid.webrtc.RTCOfferOptions; |
57 | 62 | import dev.onvoid.webrtc.RTCPeerConnection; |
58 | 63 | import dev.onvoid.webrtc.RTCPeerConnectionState; |
| 64 | +import dev.onvoid.webrtc.RTCRtpCapabilities; |
| 65 | +import dev.onvoid.webrtc.RTCRtpCodecCapability; |
59 | 66 | import dev.onvoid.webrtc.RTCRtpReceiver; |
60 | 67 | import dev.onvoid.webrtc.RTCRtpTransceiver; |
61 | 68 | import dev.onvoid.webrtc.RTCRtpTransceiverDirection; |
@@ -96,6 +103,13 @@ final class PeerImpl implements Peer { |
96 | 103 | /** How long an impolite peer waits for an answer after it ignored an offer. */ |
97 | 104 | private static final long COLLISION_TIMEOUT_MS = 5000; |
98 | 105 |
|
| 106 | + /** |
| 107 | + * The codecs that go along with those a session names: retransmission, |
| 108 | + * redundancy, FEC, comfort noise and DTMF, which serve the media codecs. |
| 109 | + */ |
| 110 | + private static final Set<String> COMPANION_CODECS = Set.of("rtx", "red", "ulpfec", "flexfec-03", "cn", |
| 111 | + "telephone-event"); |
| 112 | + |
99 | 113 | final WebRtc rtc; |
100 | 114 |
|
101 | 115 | final String name; |
@@ -180,6 +194,12 @@ final class PeerImpl implements Peer { |
180 | 194 | */ |
181 | 195 | private final Set<MediaKind> declinedKinds = EnumSet.noneOf(MediaKind.class); |
182 | 196 |
|
| 197 | + /** |
| 198 | + * The codecs each kind of media is negotiated with, in order, for the |
| 199 | + * kinds a session named codecs of. Only on the control executor. |
| 200 | + */ |
| 201 | + private final Map<MediaKind, List<RTCRtpCodecCapability>> codecs = new EnumMap<>(MediaKind.class); |
| 202 | + |
183 | 203 | /** Gets the local candidates as well, or null. Called on the control executor. */ |
184 | 204 | private volatile Consumer<RTCIceCandidate> candidateListener; |
185 | 205 |
|
@@ -479,6 +499,135 @@ CompletableFuture<Void> sendOnly() { |
479 | 499 | }); |
480 | 500 | } |
481 | 501 |
|
| 502 | + /** |
| 503 | + * Adds video that only sends, without a track, to the stream of the |
| 504 | + * tracks added without stream ids, for endpoints that take no offer |
| 505 | + * without video. Ordered with the negotiation steps. |
| 506 | + */ |
| 507 | + CompletableFuture<Void> addVideoWithoutTrack() { |
| 508 | + return negotiator.run(() -> { |
| 509 | + RTCRtpTransceiverInit init = new RTCRtpTransceiverInit(); |
| 510 | + init.direction = RTCRtpTransceiverDirection.SEND_ONLY; |
| 511 | + init.streamIds.add(defaultStreamId); |
| 512 | + |
| 513 | + connection.addTransceiver(MediaType.VIDEO, init).dispose(); |
| 514 | + |
| 515 | + return CompletableFuture.completedFuture(null); |
| 516 | + }); |
| 517 | + } |
| 518 | + |
| 519 | + /** |
| 520 | + * Negotiates only the codecs of the given names, in that order, for each |
| 521 | + * kind of media given, together with the companion codecs WebRTC pairs |
| 522 | + * with them. This holds for the transceivers there are, and for those an |
| 523 | + * offer adds later, as a counter-offer this peer answers instead of its |
| 524 | + * own. Ordered with the negotiation steps, so it takes effect on the next |
| 525 | + * offer or answer. |
| 526 | + * |
| 527 | + * @param names the codec names of each kind, such as H264 or opus. |
| 528 | + * |
| 529 | + * @return a future that fails if WebRTC has no codec of a name. |
| 530 | + */ |
| 531 | + CompletableFuture<Void> preferCodecs(Map<MediaKind, List<String>> names) { |
| 532 | + return negotiator.run(() -> { |
| 533 | + for (Map.Entry<MediaKind, List<String>> entry : names.entrySet()) { |
| 534 | + codecs.put(entry.getKey(), codecsNamed(entry.getKey(), entry.getValue())); |
| 535 | + } |
| 536 | + |
| 537 | + applyCodecs(); |
| 538 | + |
| 539 | + return CompletableFuture.completedFuture(null); |
| 540 | + }); |
| 541 | + } |
| 542 | + |
| 543 | + /** |
| 544 | + * Finds the codecs of the given names that WebRTC sends or receives, |
| 545 | + * followed by the companion codecs. |
| 546 | + * |
| 547 | + * @throws ConfigurationException if WebRTC has no codec of a name. |
| 548 | + */ |
| 549 | + private List<RTCRtpCodecCapability> codecsNamed(MediaKind kind, List<String> names) { |
| 550 | + MediaType type = kind == MediaKind.AUDIO ? MediaType.AUDIO : MediaType.VIDEO; |
| 551 | + // Those WebRTC sends and receives, each once. WebRTC tells codecs |
| 552 | + // apart as this key does, not by their scalability modes, in which |
| 553 | + // the two lists differ; one codec twice would be offered twice. |
| 554 | + Map<List<Object>, RTCRtpCodecCapability> available = new LinkedHashMap<>(); |
| 555 | + |
| 556 | + for (RTCRtpCapabilities capabilities : List.of(rtc.factory.getRtpSenderCapabilities(type), |
| 557 | + rtc.factory.getRtpReceiverCapabilities(type))) { |
| 558 | + for (RTCRtpCodecCapability codec : capabilities.getCodecs()) { |
| 559 | + available.putIfAbsent(Arrays.asList(codec.getMimeType().toLowerCase(Locale.ROOT), |
| 560 | + codec.getClockRate(), codec.getSDPFmtp()), codec); |
| 561 | + } |
| 562 | + } |
| 563 | + |
| 564 | + List<RTCRtpCodecCapability> named = new ArrayList<>(); |
| 565 | + |
| 566 | + for (String name : names) { |
| 567 | + boolean found = false; |
| 568 | + |
| 569 | + for (RTCRtpCodecCapability codec : available.values()) { |
| 570 | + if ((codec.getName().equalsIgnoreCase(name) || codec.getMimeType().equalsIgnoreCase(name)) |
| 571 | + && !named.contains(codec)) { |
| 572 | + named.add(codec); |
| 573 | + found = true; |
| 574 | + } |
| 575 | + } |
| 576 | + |
| 577 | + if (!found) { |
| 578 | + Set<String> known = new LinkedHashSet<>(); |
| 579 | + available.values().forEach(codec -> known.add(codec.getName())); |
| 580 | + |
| 581 | + throw new ConfigurationException("WebRTC has no " + kind.name().toLowerCase(Locale.ROOT) |
| 582 | + + " codec " + name + " for " + this.name + ", only " + known); |
| 583 | + } |
| 584 | + } |
| 585 | + |
| 586 | + for (RTCRtpCodecCapability codec : available.values()) { |
| 587 | + if (COMPANION_CODECS.contains(codec.getName().toLowerCase(Locale.ROOT)) && !named.contains(codec)) { |
| 588 | + named.add(codec); |
| 589 | + } |
| 590 | + } |
| 591 | + |
| 592 | + return named; |
| 593 | + } |
| 594 | + |
| 595 | + /** |
| 596 | + * Sets the codecs of {@link #preferCodecs} on every transceiver that has |
| 597 | + * not stopped. |
| 598 | + */ |
| 599 | + private void applyCodecs() { |
| 600 | + if (codecs.isEmpty()) { |
| 601 | + return; |
| 602 | + } |
| 603 | + |
| 604 | + for (RTCRtpTransceiver transceiver : connection.getTransceivers()) { |
| 605 | + try { |
| 606 | + if (transceiver.stopped()) { |
| 607 | + continue; |
| 608 | + } |
| 609 | + |
| 610 | + RTCRtpReceiver receiver = transceiver.getReceiver(); |
| 611 | + MediaStreamTrack track = receiver.getTrack(); |
| 612 | + MediaKind kind = MediaStreamTrack.AUDIO_TRACK_KIND.equals(track.getKind()) |
| 613 | + ? MediaKind.AUDIO |
| 614 | + : MediaKind.VIDEO; |
| 615 | + |
| 616 | + track.dispose(); |
| 617 | + receiver.dispose(); |
| 618 | + |
| 619 | + List<RTCRtpCodecCapability> preferred = codecs.get(kind); |
| 620 | + |
| 621 | + if (preferred != null) { |
| 622 | + transceiver.setCodecPreferences(preferred); |
| 623 | + } |
| 624 | + } |
| 625 | + finally { |
| 626 | + transceiver.dispose(); |
| 627 | + } |
| 628 | + } |
| 629 | + } |
| 630 | + |
482 | 631 | /** |
483 | 632 | * Gathers candidates from now on with the given ICE servers, unless the |
484 | 633 | * peer was built with servers of its own, which take precedence. |
@@ -658,6 +807,8 @@ CompletableFuture<RTCSessionDescription> answerInstead(RTCSessionDescription off |
658 | 807 | .thenCompose(ignored -> setRemote(offer)) |
659 | 808 | .thenCompose(ignored -> { |
660 | 809 | stopTransceivers(rejected); |
| 810 | + // The offer made transceivers of its own. |
| 811 | + applyCodecs(); |
661 | 812 | releaseHeldCandidates(); |
662 | 813 | return createAnswer(); |
663 | 814 | }) |
|
0 commit comments