|
| 1 | +/* |
| 2 | + * Copyright 2026 Alex Andres |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dev.onvoid.webrtc.client; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.EnumMap; |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.LinkedHashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Locale; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import dev.onvoid.webrtc.RTCRtpCapabilities; |
| 30 | +import dev.onvoid.webrtc.RTCRtpCodecCapability; |
| 31 | +import dev.onvoid.webrtc.RTCRtpReceiver; |
| 32 | +import dev.onvoid.webrtc.RTCRtpTransceiver; |
| 33 | +import dev.onvoid.webrtc.media.MediaStreamTrack; |
| 34 | +import dev.onvoid.webrtc.media.MediaType; |
| 35 | + |
| 36 | +/** |
| 37 | + * The codecs a peer negotiates, for the kinds of media a session named codecs |
| 38 | + * of. Confined to the control executor. |
| 39 | + */ |
| 40 | +final class CodecPreferences { |
| 41 | + |
| 42 | + /** |
| 43 | + * The codecs that go along with those a session names: retransmission, |
| 44 | + * redundancy, FEC, comfort noise and DTMF, which serve the media codecs. |
| 45 | + */ |
| 46 | + private static final Set<String> COMPANION_CODECS = Set.of("rtx", "red", "ulpfec", "flexfec-03", "cn", |
| 47 | + "telephone-event"); |
| 48 | + |
| 49 | + private final PeerImpl peer; |
| 50 | + |
| 51 | + /** The codecs each kind of media is negotiated with, in order. */ |
| 52 | + private final Map<MediaKind, List<RTCRtpCodecCapability>> codecs = new EnumMap<>(MediaKind.class); |
| 53 | + |
| 54 | + |
| 55 | + CodecPreferences(PeerImpl peer) { |
| 56 | + this.peer = peer; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Negotiates only the codecs of the given names, in that order, for each |
| 61 | + * kind of media given, together with the companion codecs WebRTC pairs |
| 62 | + * with them, and sets them on the transceivers there are. |
| 63 | + * |
| 64 | + * @param names the codec names of each kind, such as H264 or opus. |
| 65 | + * |
| 66 | + * @throws ConfigurationException if WebRTC has no codec of a name. |
| 67 | + */ |
| 68 | + void prefer(Map<MediaKind, List<String>> names) { |
| 69 | + for (Map.Entry<MediaKind, List<String>> entry : names.entrySet()) { |
| 70 | + codecs.put(entry.getKey(), named(entry.getKey(), entry.getValue())); |
| 71 | + } |
| 72 | + |
| 73 | + apply(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Sets the preferred codecs on every transceiver that has not stopped. |
| 78 | + */ |
| 79 | + void apply() { |
| 80 | + if (codecs.isEmpty()) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + for (RTCRtpTransceiver transceiver : peer.connection().getTransceivers()) { |
| 85 | + try { |
| 86 | + if (transceiver.stopped()) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + RTCRtpReceiver receiver = transceiver.getReceiver(); |
| 91 | + MediaStreamTrack track = receiver.getTrack(); |
| 92 | + MediaKind kind = MediaStreamTrack.AUDIO_TRACK_KIND.equals(track.getKind()) |
| 93 | + ? MediaKind.AUDIO |
| 94 | + : MediaKind.VIDEO; |
| 95 | + |
| 96 | + track.dispose(); |
| 97 | + receiver.dispose(); |
| 98 | + |
| 99 | + List<RTCRtpCodecCapability> preferred = codecs.get(kind); |
| 100 | + |
| 101 | + if (preferred != null) { |
| 102 | + transceiver.setCodecPreferences(preferred); |
| 103 | + } |
| 104 | + } |
| 105 | + finally { |
| 106 | + transceiver.dispose(); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Finds the codecs of the given names that WebRTC sends or receives, |
| 113 | + * followed by the companion codecs. |
| 114 | + * |
| 115 | + * @throws ConfigurationException if WebRTC has no codec of a name. |
| 116 | + */ |
| 117 | + private List<RTCRtpCodecCapability> named(MediaKind kind, List<String> names) { |
| 118 | + MediaType type = kind == MediaKind.AUDIO ? MediaType.AUDIO : MediaType.VIDEO; |
| 119 | + // Those WebRTC sends and receives, each once. WebRTC tells codecs |
| 120 | + // apart as this key does, not by their scalability modes, in which |
| 121 | + // the two lists differ; one codec twice would be offered twice. |
| 122 | + Map<List<Object>, RTCRtpCodecCapability> available = new LinkedHashMap<>(); |
| 123 | + |
| 124 | + for (RTCRtpCapabilities capabilities : List.of(peer.rtc.factory.getRtpSenderCapabilities(type), |
| 125 | + peer.rtc.factory.getRtpReceiverCapabilities(type))) { |
| 126 | + for (RTCRtpCodecCapability codec : capabilities.getCodecs()) { |
| 127 | + available.putIfAbsent(Arrays.asList(codec.getMimeType().toLowerCase(Locale.ROOT), |
| 128 | + codec.getClockRate(), codec.getSDPFmtp()), codec); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + List<RTCRtpCodecCapability> named = new ArrayList<>(); |
| 133 | + |
| 134 | + for (String name : names) { |
| 135 | + boolean found = false; |
| 136 | + |
| 137 | + for (RTCRtpCodecCapability codec : available.values()) { |
| 138 | + if ((codec.getName().equalsIgnoreCase(name) || codec.getMimeType().equalsIgnoreCase(name)) |
| 139 | + && !named.contains(codec)) { |
| 140 | + named.add(codec); |
| 141 | + found = true; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (!found) { |
| 146 | + Set<String> known = new LinkedHashSet<>(); |
| 147 | + available.values().forEach(codec -> known.add(codec.getName())); |
| 148 | + |
| 149 | + throw new ConfigurationException("WebRTC has no " + kind.name().toLowerCase(Locale.ROOT) |
| 150 | + + " codec " + name + " for " + peer.name + ", only " + known); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + for (RTCRtpCodecCapability codec : available.values()) { |
| 155 | + if (COMPANION_CODECS.contains(codec.getName().toLowerCase(Locale.ROOT)) && !named.contains(codec)) { |
| 156 | + named.add(codec); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return named; |
| 161 | + } |
| 162 | +} |
0 commit comments