Skip to content

Commit 0b8b0e6

Browse files
committed
feat: add simulcast and per-layer encodings to the client API
Peer.addTrack(track, Simulcast) sends a video track in layers with rids of the caller's choice, and Sender.configure(rid, encoding) changes one layer.
1 parent a234882 commit 0b8b0e6

10 files changed

Lines changed: 537 additions & 46 deletions

File tree

‎docs/guide/client-api-design.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ public interface Peer extends AutoCloseable {
340340

341341
// Sending. In AUTOMATIC mode each change renegotiates by itself.
342342
Sender addTrack(LocalTrack track, String... streamIds);
343+
Sender addTrack(LocalTrack track, Simulcast simulcast, String... streamIds); // video, two or three layers with rids
343344
List<Sender> senders();
344345

345346
// Receiving. Late subscribers get the tracks that already arrived.
@@ -635,6 +636,8 @@ public interface Sender {
635636
LocalTrack track();
636637
CompletableFuture<Void> replaceTrack(LocalTrack track); // no renegotiation, null sends nothing
637638
CompletableFuture<Void> configure(Encoding encoding); // max bitrate, max frame rate, scale down, active
639+
CompletableFuture<Void> configure(String rid, Encoding encoding); // one simulcast layer
640+
List<String> rids(); // the simulcast layers, none without
638641
CompletableFuture<Void> transform(RTCEncodedFrameTransformer transform); // null sends frames unchanged
639642
CompletableFuture<Void> generateKeyFrame();
640643
CompletableFuture<Void> remove(); // renegotiates in AUTOMATIC mode

‎docs/guide/client/sending-media.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ audioSender.sendTones("*99,1", Duration.ofMillis(200), Duration.ofMillis(100));
134134

135135
The characters 0 to 9, A to D, `#` and `*` are tones, and a comma is a pause of 2 seconds. What happens to queued and stopped tones is described under [Details](#dtmf-tones), and the [DTMF Sender](/guide/audio/dtmf-sender) guide has more.
136136

137+
## Simulcast
138+
139+
With simulcast, a video track is sent in several layers at once, each at its own resolution and bitrate. A server that forwards the stream, such as an SFU, then gives each receiver the layer its connection takes. Each layer has an RTP stream id, its `rid`:
140+
141+
```java
142+
Sender sender = peer.addTrack(camera, Simulcast.builder()
143+
.layer("q", Encoding.builder().scaleDownBy(4).maxBitrate(150_000).build())
144+
.layer("h", Encoding.builder().scaleDownBy(2).maxBitrate(500_000).build())
145+
.layer("f", Encoding.builder().maxBitrate(1_500_000).build())
146+
.build());
147+
```
148+
149+
`Simulcast.standard()` gives three layers in full, half and quarter resolution, with the rids `f`, `h` and `q`.
150+
151+
`configure(rid, encoding)` changes one layer, and `configure(encoding)` every layer alike. A server may ask the sender to pause the layers no receiver wants:
152+
153+
```java
154+
sender.configure("f", Encoding.builder().active(false).build()); // stop sending full resolution
155+
sender.configure("f", Encoding.builder().active(true).build()); // and start again
156+
```
157+
158+
`rids()` lists the layers of a sender, in their order.
159+
137160
## Switching Cameras
138161

139162
A `LocalCamera` switches to another camera without renegotiating. Every peer that sends it keeps sending it, and preview handlers keep getting frames:
@@ -184,6 +207,14 @@ If the thread stalls, as in a long garbage collection pause, it makes up for at
184207
- A video sender, or a sender whose remote peer accepts no telephone events, fails the future with an `IllegalStateException`.
185208
- So do tones that WebRTC stops sending midway, for example because the sender was paused with `configure`. The future then fails a few seconds after their planned end.
186209

210+
### Simulcast Layers
211+
212+
- A simulcast takes two or three layers, the most WebRTC sends.
213+
- A rid has 1 to 16 letters and digits, and is unique among the layers. It cannot change once the track is added.
214+
- Only video tracks take simulcast. `addTrack` throws an `IllegalArgumentException` for an audio track.
215+
- A remote peer without simulcast leaves it out of its answer and gets only the first layer. A plain WebRTC peer, such as another `Peer`, is one of them.
216+
- `configure(rid, encoding)` throws an `IllegalArgumentException` for a rid the sender does not have.
217+
187218
### When a Camera Switch Fails
188219

189220
- If the new camera cannot be opened, the future fails with a `MediaException`, and the track keeps sending the camera it had.

‎docs/guide/client/whip-whep.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Whip.Session live = Whip.to(URI.create("https://example.com/live/whip"))
9595
.get(15, TimeUnit.SECONDS);
9696
```
9797

98-
Simulcast needs a video track. `Sender.configure` on its sender changes every layer alike.
98+
Simulcast needs a video track. `Sender.configure(Encoding)` on its sender changes every layer alike, and `configure(rid, Encoding)` one layer. Simulcast outside WHIP is described under [Simulcast](/guide/client/sending-media#simulcast).
9999

100100
## Errors
101101

‎webrtc/src/main/java/dev/onvoid/webrtc/client/Peer.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ public interface Peer extends AutoCloseable {
7575
*/
7676
Sender addTrack(LocalTrack track, String... streamIds);
7777

78+
/**
79+
* Sends a video track in the layers of the given simulcast, each with
80+
* its rid. Otherwise as {@link #addTrack(LocalTrack, String...)}. Should
81+
* WebRTC refuse the layers, the error handlers of the peer hear of it,
82+
* and every operation of the sender fails.
83+
*
84+
* @param track a video track of the same runtime.
85+
* @param simulcast the layers to send the track in.
86+
* @param streamIds the streams the remote peer sees the track in.
87+
*
88+
* @return the handle of the new sender, at once.
89+
*
90+
* @throws IllegalArgumentException if the track is not a video track.
91+
*/
92+
Sender addTrack(LocalTrack track, Simulcast simulcast, String... streamIds);
93+
7894
/**
7995
* @return the senders that were added and not removed.
8096
*/

‎webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,27 @@ public Sender addTrack(LocalTrack track, String... streamIds) {
290290
return addSender(local, ids, Collections.emptyList(), new CompletableFuture<>());
291291
}
292292

293+
@Override
294+
public Sender addTrack(LocalTrack track, Simulcast simulcast, String... streamIds) {
295+
LocalTrackImpl local = checkTrack(track);
296+
requireNonNull(simulcast, "simulcast");
297+
298+
if (local.kind() != MediaKind.VIDEO) {
299+
throw new IllegalArgumentException("Simulcast takes a video track, got " + local.kind());
300+
}
301+
302+
List<String> ids = new ArrayList<>();
303+
304+
for (String id : streamIds) {
305+
ids.add(requireNonNull(id, "streamId"));
306+
}
307+
if (ids.isEmpty()) {
308+
ids.add(defaultStreamId);
309+
}
310+
311+
return addSender(local, ids, simulcast.encodings(), new CompletableFuture<>());
312+
}
313+
293314
/**
294315
* Adds a track to the stream of the tracks added without stream ids, sent
295316
* in the given encodings, as simulcast (RFC 8853) does with one for each
@@ -308,7 +329,14 @@ CompletableFuture<Void> addTrack(LocalTrack track, List<RTCRtpEncodingParameters
308329

309330
private SenderImpl addSender(LocalTrackImpl local, List<String> ids, List<RTCRtpEncodingParameters> encodings,
310331
CompletableFuture<Void> added) {
311-
SenderImpl sender = new SenderImpl(this, local);
332+
List<String> rids = new ArrayList<>();
333+
for (RTCRtpEncodingParameters encoding : encodings) {
334+
if (encoding.rid != null) {
335+
rids.add(encoding.rid);
336+
}
337+
}
338+
339+
SenderImpl sender = new SenderImpl(this, local, rids);
312340
senders.add(sender);
313341

314342
control.execute(() -> {

‎webrtc/src/main/java/dev/onvoid/webrtc/client/Sender.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package dev.onvoid.webrtc.client;
1818

1919
import java.time.Duration;
20+
import java.util.List;
2021
import java.util.concurrent.CompletableFuture;
2122
import java.util.function.Consumer;
2223

@@ -42,14 +43,36 @@ public interface Sender {
4243
CompletableFuture<Void> replaceTrack(LocalTrack track);
4344

4445
/**
45-
* Changes how the track is encoded. Values left unset stay as they are.
46+
* Changes how the track is encoded, in every layer alike if it is sent
47+
* as {@link Simulcast}. Values left unset stay as they are.
4648
*
4749
* @param encoding the values to change.
4850
*
4951
* @return a future that completes once the values are applied.
5052
*/
5153
CompletableFuture<Void> configure(Encoding encoding);
5254

55+
/**
56+
* Changes how one layer of a {@link Simulcast} track is encoded, such as
57+
* pausing it with {@code active(false)} while no receiver wants it.
58+
* Values left unset stay as they are.
59+
*
60+
* @param rid the rid of the layer, one of {@link #rids()}.
61+
* @param encoding the values to change.
62+
*
63+
* @return a future that completes once the values are applied.
64+
*
65+
* @throws IllegalArgumentException if the sender has no layer of that
66+
* rid.
67+
*/
68+
CompletableFuture<Void> configure(String rid, Encoding encoding);
69+
70+
/**
71+
* @return the rids of the layers the track is sent in, in their order,
72+
* or none if it is sent without simulcast.
73+
*/
74+
List<String> rids();
75+
5376
/**
5477
* Sets the transform that every encoded frame of this sender passes
5578
* through, after the encoder and before the packetizer, in place of the

‎webrtc/src/main/java/dev/onvoid/webrtc/client/SenderImpl.java‎

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ final class SenderImpl implements Sender {
5353

5454
private final MediaKind kind;
5555

56+
private final List<String> rids;
57+
5658
private volatile LocalTrackImpl track;
5759

5860
/**
@@ -85,10 +87,15 @@ final class SenderImpl implements Sender {
8587
private final Handlers<String> toneHandlers;
8688

8789

88-
SenderImpl(PeerImpl peer, LocalTrackImpl track) {
90+
/**
91+
* @param rids the rids of the simulcast layers the track is sent in, or
92+
* none without simulcast.
93+
*/
94+
SenderImpl(PeerImpl peer, LocalTrackImpl track, List<String> rids) {
8995
this.peer = peer;
9096
this.track = track;
9197
this.kind = track.kind();
98+
this.rids = List.copyOf(rids);
9299
this.toneHandlers = new Handlers<>(peer.rtc::reportError);
93100
}
94101

@@ -205,26 +212,68 @@ public CompletableFuture<Void> configure(Encoding encoding) {
205212
RTCRtpSendParameters parameters = sender.getParameters();
206213

207214
for (RTCRtpEncodingParameters values : parameters.encodings) {
208-
if (encoding.active != null) {
209-
values.active = encoding.active;
210-
}
211-
if (encoding.maxBitrate != null) {
212-
values.maxBitrate = encoding.maxBitrate;
213-
}
214-
if (kind == MediaKind.VIDEO) {
215-
if (encoding.maxFrameRate != null) {
216-
values.maxFramerate = encoding.maxFrameRate;
217-
}
218-
if (encoding.scaleDownBy != null) {
219-
values.scaleResolutionDownBy = encoding.scaleDownBy;
220-
}
215+
apply(encoding, values);
216+
}
217+
218+
sender.setParameters(parameters);
219+
});
220+
}
221+
222+
@Override
223+
public CompletableFuture<Void> configure(String rid, Encoding encoding) {
224+
if (rid == null) {
225+
throw new NullPointerException("rid");
226+
}
227+
if (encoding == null) {
228+
throw new NullPointerException("encoding");
229+
}
230+
if (!rids.contains(rid)) {
231+
throw new IllegalArgumentException(rids.isEmpty()
232+
? "The track is sent without simulcast, so it has no layer " + rid
233+
: "The track is sent in the layers " + rids + ", not in " + rid);
234+
}
235+
236+
return run(() -> {
237+
RTCRtpSendParameters parameters = sender.getParameters();
238+
boolean found = false;
239+
240+
for (RTCRtpEncodingParameters values : parameters.encodings) {
241+
if (rid.equals(values.rid)) {
242+
apply(encoding, values);
243+
found = true;
221244
}
222245
}
223246

247+
if (!found) {
248+
throw new WebRtcException("WebRTC reports no layer " + rid);
249+
}
250+
224251
sender.setParameters(parameters);
225252
});
226253
}
227254

255+
@Override
256+
public List<String> rids() {
257+
return rids;
258+
}
259+
260+
private void apply(Encoding encoding, RTCRtpEncodingParameters values) {
261+
if (encoding.active != null) {
262+
values.active = encoding.active;
263+
}
264+
if (encoding.maxBitrate != null) {
265+
values.maxBitrate = encoding.maxBitrate;
266+
}
267+
if (kind == MediaKind.VIDEO) {
268+
if (encoding.maxFrameRate != null) {
269+
values.maxFramerate = encoding.maxFrameRate;
270+
}
271+
if (encoding.scaleDownBy != null) {
272+
values.scaleResolutionDownBy = encoding.scaleDownBy;
273+
}
274+
}
275+
}
276+
228277
@Override
229278
public CompletableFuture<Void> transform(RTCEncodedFrameTransformer transform) {
230279
return run(() -> sender.setTransform(peer.guard(transform, "a sender of " + peer.name)));

0 commit comments

Comments
 (0)