Skip to content

Commit 239d6d2

Browse files
authored
feat: map the remaining sender encoding parameters (#316)
RTCRtpEncodingParameters gains the encoding parameters WebRTC has and the Java API did not: rid for simulcast, scaleResolutionDownTo (with the new RTCResolutionRestriction), scalabilityMode and numTemporalLayers for SVC, bitratePriority and networkPriority, adaptivePtime for audio, and codec to send an encoding with a codec of its own. RTCRtpSendParameters gains degradationPreference, with the new RTCDegradationPreference. RTCRtpCodecCapability gains getScalabilityModes(), the modes a codec supports, which is what scalabilityMode accepts; unsupported modes make setParameters() fail. The priorities apply to the whole sender, and WebRTC accepts them on the first encoding only, which the Javadoc says. The media constraints guide covers the new parameters, with simulcast and scalability modes.
1 parent ec06cf8 commit 239d6d2

16 files changed

Lines changed: 807 additions & 8 deletions

‎docs/guide/media/constraints.md‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,89 @@ The `scaleResolutionDownBy` parameter specifies how much to scale down the video
113113
Note that these constraints are applied without requiring SDP renegotiation, making them suitable for dynamic adaptation to changing network conditions.
114114
:::
115115

116+
To cap the resolution at an absolute size instead, set `scaleResolutionDownTo`. The video is scaled down to fit, whatever size the source delivers; it takes precedence over `scaleResolutionDownBy` if both are set.
117+
118+
```java
119+
parameters.encodings.get(0).scaleResolutionDownTo = new RTCResolutionRestriction(1280, 720);
120+
sender.setParameters(parameters);
121+
```
122+
123+
## Degradation Preference
124+
125+
When the network or the CPU cannot keep up, a video sender lowers either its frame rate or its resolution. `degradationPreference` on the send parameters decides which:
126+
127+
```java
128+
RTCRtpSendParameters parameters = sender.getParameters();
129+
130+
// Keep text sharp when sharing a screen: lower the frame rate instead.
131+
parameters.degradationPreference = RTCDegradationPreference.MAINTAIN_RESOLUTION;
132+
133+
sender.setParameters(parameters);
134+
```
135+
136+
| Value | Gives up |
137+
|---|---|
138+
| `MAINTAIN_FRAMERATE` | Resolution; suits motion, such as camera video |
139+
| `MAINTAIN_RESOLUTION` | Frame rate; suits detail, such as screens with text |
140+
| `BALANCED` | Both, in turns |
141+
| `MAINTAIN_FRAMERATE_AND_RESOLUTION` | Neither; frames are dropped instead |
142+
143+
If unset, WebRTC keeps the resolution of screen shares and of tracks hinted as detailed or text, and the frame rate of other video.
144+
145+
## Simulcast
146+
147+
With simulcast, a video sender encodes the same video several times, at different resolutions, so that a server can forward to each receiver the one its connection can take. The encodings are given when the transceiver is created, each with an RTP stream ID (`rid`):
148+
149+
```java
150+
RTCRtpTransceiverInit init = new RTCRtpTransceiverInit();
151+
init.direction = RTCRtpTransceiverDirection.SEND_ONLY;
152+
153+
RTCRtpEncodingParameters quarter = new RTCRtpEncodingParameters();
154+
quarter.rid = "q";
155+
quarter.scaleResolutionDownBy = 4.0;
156+
quarter.maxBitrate = 150_000;
157+
158+
RTCRtpEncodingParameters half = new RTCRtpEncodingParameters();
159+
half.rid = "h";
160+
half.scaleResolutionDownBy = 2.0;
161+
half.maxBitrate = 500_000;
162+
163+
RTCRtpEncodingParameters full = new RTCRtpEncodingParameters();
164+
full.rid = "f";
165+
full.maxBitrate = 1_500_000;
166+
167+
init.sendEncodings = Arrays.asList(quarter, half, full);
168+
169+
RTCRtpTransceiver transceiver = peerConnection.addTransceiver(videoTrack, init);
170+
```
171+
172+
The `rid`s cannot be changed afterwards; the other parameters of each encoding can, through `setParameters()`, e.g. `active = false` to pause one of them. An encoding may also send a different codec than the others, by setting its `codec` to one of the negotiated codecs.
173+
174+
## Scalability Modes (SVC)
175+
176+
Instead of separate encodings, one encoding can carry layers, which a server can drop to lower the rate: temporal layers (fewer frames per second) and, with VP9 and AV1, spatial layers (lower resolutions). `scalabilityMode` takes the mode names of the [WebRTC SVC specification](https://www.w3.org/TR/webrtc-svc/), e.g. `L1T3` for three temporal layers, or `L3T3_KEY` for three spatial layers with three temporal layers each:
177+
178+
```java
179+
RTCRtpSendParameters parameters = sender.getParameters();
180+
parameters.encodings.get(0).scalabilityMode = "L1T3";
181+
sender.setParameters(parameters);
182+
```
183+
184+
The codec has to support the mode; `setParameters()` throws otherwise. `getScalabilityModes()` of a codec's capability, from `factory.getRtpSenderCapabilities(MediaType.VIDEO)`, lists the modes it supports. VP8 supports temporal layers only.
185+
186+
## Priorities
187+
188+
`bitratePriority` sets the share of the available bitrate a sender gets relative to the other senders of the peer connection (default 1.0), and `networkPriority` how its packets are marked on the network (DSCP), where the network honors it. Both apply to the whole sender, so only the first encoding may set them:
189+
190+
```java
191+
RTCRtpSendParameters parameters = sender.getParameters();
192+
parameters.encodings.get(0).bitratePriority = 2.0;
193+
parameters.encodings.get(0).networkPriority = RTCPriorityType.HIGH;
194+
sender.setParameters(parameters);
195+
```
196+
197+
For audio, `adaptivePtime` lets the encoder send longer packets when the bitrate is low, which saves the overhead of many small packets.
198+
116199
## Conclusion
117200

118201
In this guide, we've explored several important techniques for controlling media quality and bandwidth usage in WebRTC applications.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
#ifndef JNI_WEBRTC_API_RTC_RESOLUTION_RESTRICTION_H_
18+
#define JNI_WEBRTC_API_RTC_RESOLUTION_RESTRICTION_H_
19+
20+
#include "JavaClass.h"
21+
#include "JavaRef.h"
22+
23+
#include "api/video/resolution.h"
24+
25+
#include <jni.h>
26+
27+
namespace jni
28+
{
29+
namespace RTCResolutionRestriction
30+
{
31+
class JavaRTCResolutionRestrictionClass : public JavaClass
32+
{
33+
public:
34+
explicit JavaRTCResolutionRestrictionClass(JNIEnv * env);
35+
36+
jclass cls;
37+
jmethodID ctor;
38+
jfieldID maxWidth;
39+
jfieldID maxHeight;
40+
};
41+
42+
JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::Resolution & resolution);
43+
webrtc::Resolution toNative(JNIEnv * env, const JavaRef<jobject> & restriction);
44+
}
45+
}
46+
47+
#endif

‎webrtc-jni/src/main/cpp/include/api/RTCRtpCodecCapability.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace jni
4040
jfieldID clockRate;
4141
jfieldID channels;
4242
jfieldID sdpFmtp;
43+
jfieldID scalabilityModes;
4344
};
4445

4546
JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::RtpCodecCapability & capability);

‎webrtc-jni/src/main/cpp/include/api/RTCRtpEncodingParameters.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ namespace jni
4141
jfieldID maxBitrate;
4242
jfieldID maxFramerate;
4343
jfieldID scaleResolution;
44+
jfieldID rid;
45+
jfieldID scaleResolutionDownTo;
46+
jfieldID scalabilityMode;
47+
jfieldID numTemporalLayers;
48+
jfieldID bitratePriority;
49+
jfieldID networkPriority;
50+
jfieldID adaptivePtime;
51+
jfieldID codec;
4452
};
4553

4654
JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::RtpEncodingParameters & parameters);

‎webrtc-jni/src/main/cpp/include/api/RTCRtpSendParameters.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace jni
3737
jmethodID ctor;
3838
jfieldID transactionId;
3939
jfieldID encodings;
40+
jfieldID degradationPreference;
4041
};
4142

4243
JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::RtpParameters & parameters);

‎webrtc-jni/src/main/cpp/src/WebRTCContext.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include "api/environment/environment_factory.h"
2929
#include "api/peer_connection_interface.h"
30+
#include "api/priority.h"
31+
#include "api/rtp_parameters.h"
3032
#include "modules/desktop_capture/desktop_capturer.h"
3133
#include "rtc_base/ssl_adapter.h"
3234

@@ -83,6 +85,8 @@ namespace jni
8385
JavaEnums::add<webrtc::PeerConnectionInterface::SignalingState>(env, PKG"RTCSignalingState");
8486
JavaEnums::add<webrtc::PeerConnectionInterface::TlsCertPolicy>(env, PKG"TlsCertPolicy");
8587
JavaEnums::add<webrtc::RtpTransceiverDirection>(env, PKG"RTCRtpTransceiverDirection");
88+
JavaEnums::add<webrtc::Priority>(env, PKG"RTCPriorityType");
89+
JavaEnums::add<webrtc::DegradationPreference>(env, PKG"RTCDegradationPreference");
8690
JavaEnums::add<webrtc::SdpType>(env, PKG"RTCSdpType");
8791
JavaEnums::add<webrtc::AudioDeviceModule::AudioLayer>(env, PKG_AUDIO"AudioLayer");
8892
JavaEnums::add<webrtc::AudioProcessing::Config::GainController1::Mode>(env, PKG_AUDIO"AudioProcessingConfig$GainController$Mode");
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
#include "api/RTCResolutionRestriction.h"
18+
#include "JavaClasses.h"
19+
#include "JavaObject.h"
20+
#include "JavaUtils.h"
21+
#include "JNI_WebRTC.h"
22+
23+
namespace jni
24+
{
25+
namespace RTCResolutionRestriction
26+
{
27+
JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::Resolution & resolution)
28+
{
29+
const auto javaClass = JavaClasses::get<JavaRTCResolutionRestrictionClass>(env);
30+
31+
jobject object = env->NewObject(javaClass->cls, javaClass->ctor,
32+
static_cast<jint>(resolution.width), static_cast<jint>(resolution.height));
33+
34+
ExceptionCheck(env);
35+
36+
return JavaLocalRef<jobject>(env, object);
37+
}
38+
39+
webrtc::Resolution toNative(JNIEnv * env, const JavaRef<jobject> & restriction)
40+
{
41+
const auto javaClass = JavaClasses::get<JavaRTCResolutionRestrictionClass>(env);
42+
43+
JavaObject obj(env, restriction);
44+
45+
webrtc::Resolution resolution;
46+
resolution.width = obj.getInt(javaClass->maxWidth);
47+
resolution.height = obj.getInt(javaClass->maxHeight);
48+
49+
return resolution;
50+
}
51+
52+
JavaRTCResolutionRestrictionClass::JavaRTCResolutionRestrictionClass(JNIEnv * env)
53+
{
54+
cls = FindClass(env, PKG"RTCResolutionRestriction");
55+
56+
ctor = GetMethod(env, cls, "<init>", "(II)V");
57+
58+
maxWidth = GetFieldID(env, cls, "maxWidth", "I");
59+
maxHeight = GetFieldID(env, cls, "maxHeight", "I");
60+
}
61+
}
62+
}

‎webrtc-jni/src/main/cpp/src/api/RTCRtpCodecCapability.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#include "JavaUtils.h"
2424
#include "JNI_WebRTC.h"
2525

26+
#include "api/video_codecs/scalability_mode.h"
27+
#include "modules/video_coding/svc/scalability_mode_util.h"
28+
29+
#include <string>
30+
#include <vector>
31+
2632
namespace jni
2733
{
2834
namespace RTCRtpCodecCapability
@@ -59,6 +65,16 @@ namespace jni
5965

6066
ExceptionCheck(env);
6167

68+
if (!capability.scalability_modes.empty()) {
69+
std::vector<std::string> modes;
70+
71+
for (webrtc::ScalabilityMode mode : capability.scalability_modes) {
72+
modes.emplace_back(webrtc::ScalabilityModeToString(mode));
73+
}
74+
75+
env->SetObjectField(object, javaClass->scalabilityModes, JavaString::createArray(env, modes).get());
76+
}
77+
6278
return JavaLocalRef<jobject>(env, object);
6379
}
6480

@@ -90,6 +106,18 @@ namespace jni
90106
codecCapability.num_channels = Integer::getValue(env, channels);
91107
}
92108

109+
JavaLocalRef<jobjectArray> modes = obj.getObjectArray(javaClass->scalabilityModes);
110+
const jsize count = modes.get() != nullptr ? env->GetArrayLength(modes.get()) : 0;
111+
112+
for (jsize i = 0; i < count; i++) {
113+
JavaLocalRef<jstring> mode(env, static_cast<jstring>(env->GetObjectArrayElement(modes.get(), i)));
114+
auto scalabilityMode = webrtc::ScalabilityModeFromString(JavaString::toNative(env, mode));
115+
116+
if (scalabilityMode.has_value()) {
117+
codecCapability.scalability_modes.push_back(*scalabilityMode);
118+
}
119+
}
120+
93121
return codecCapability;
94122
}
95123

@@ -104,6 +132,7 @@ namespace jni
104132
clockRate = GetFieldID(env, cls, "clockRate", INTEGER_SIG);
105133
channels = GetFieldID(env, cls, "channels", INTEGER_SIG);
106134
sdpFmtp = GetFieldID(env, cls, "sdpFmtp", MAP_SIG);
135+
scalabilityModes = GetFieldID(env, cls, "scalabilityModes", "[" STRING_SIG);
107136
}
108137
}
109138
}

0 commit comments

Comments
 (0)