Skip to content

Commit 93653eb

Browse files
authored
feat: choose the RTP header extensions a transceiver negotiates (#606)
* fix: return the codec preferences of a transceiver instead of null * feat: choose the RTP header extensions a transceiver negotiates
1 parent 1766e4e commit 93653eb

8 files changed

Lines changed: 347 additions & 20 deletions

File tree

‎docs/guide/media/directionality.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ pc.setLocalDescription(new SetSessionDescriptionObserver() {
134134
You can also control sending without renegotiation by replacing the sender’s track or disabling it via `MediaStreamTrack.setEnabled(false)`. However, the negotiated direction in SDP remains the same until you renegotiate.
135135
:::
136136

137+
## Choosing RTP header extensions
138+
139+
Each transceiver negotiates RTP header extensions, such as the audio level or the transport-wide sequence numbers that bandwidth estimation uses. Each extension has a direction of its own. To leave one out of the negotiation, set it to `STOPPED`:
140+
141+
```java
142+
List<RTCRtpHeaderExtensionCapability> extensions = transceiver.getHeaderExtensionsToNegotiate();
143+
List<RTCRtpHeaderExtensionCapability> changed = new ArrayList<>();
144+
145+
for (RTCRtpHeaderExtensionCapability extension : extensions) {
146+
if (extension.getUri().equals("urn:ietf:params:rtp-hdrext:ssrc-audio-level")) {
147+
extension = extension.withDirection(RTCRtpTransceiverDirection.STOPPED);
148+
}
149+
changed.add(extension);
150+
}
151+
152+
transceiver.setHeaderExtensionsToNegotiate(changed);
153+
```
154+
155+
The change takes effect with the next offer or answer. `getNegotiatedHeaderExtensions()` then shows what both ends agreed on, with `STOPPED` for the extensions left out.
156+
157+
The list must keep every extension, in the same order. Otherwise `setHeaderExtensionsToNegotiate` throws an `IllegalArgumentException`. The MID extension (`urn:ietf:params:rtp-hdrext:sdes:mid`) is needed for bundling and cannot be stopped.
158+
137159
## Common patterns and tips
138160

139161
- If you only need to receive a stream from a server (e.g., WHEP), use `RECV_ONLY` and avoid capturing local devices. This simplifies permissions and reduces CPU usage.

‎webrtc-jni/src/main/cpp/include/JNI_RTCRtpTransceiver.h‎

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ namespace jni
3535

3636
jclass cls;
3737
jmethodID ctor;
38+
jfieldID uri;
39+
jfieldID direction;
3840
};
3941

4042
JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::RtpHeaderExtensionCapability & capability);
43+
webrtc::RtpHeaderExtensionCapability toNative(JNIEnv * env, const JavaRef<jobject> & capability);
4144
}
4245
}
4346

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "JavaBoundary.h"
1919
#include "NativeHandle.h"
2020
#include "api/RTCRtpCodecCapability.h"
21+
#include "api/RTCRtpHeaderExtensionCapability.h"
2122
#include "JavaEnums.h"
2223
#include "JavaError.h"
2324
#include "JavaFactories.h"
@@ -192,7 +193,7 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_getCodecPrefe
192193

193194
auto capabilities = jni::JavaList::toArrayList(env, transceiver->codec_preferences(), jni::RTCRtpCodecCapability::toJava);
194195

195-
return capabilities.get();
196+
return capabilities.release();
196197
}
197198
catch (...) {
198199
ThrowCxxJavaException(env);
@@ -223,6 +224,57 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_setCodecPreferen
223224
});
224225
}
225226

227+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_getHeaderExtensionsToNegotiate
228+
(JNIEnv * env, jobject caller)
229+
{
230+
return jni::CallFromJava(env, [&]() -> jobject {
231+
jni::NativeLease<webrtc::RtpTransceiverInterface> transceiver(env, caller);
232+
CHECK_HANDLEV(transceiver, nullptr);
233+
234+
return jni::JavaList::toArrayList(env, transceiver->GetHeaderExtensionsToNegotiate(),
235+
jni::RTCRtpHeaderExtensionCapability::toJava).release();
236+
});
237+
}
238+
239+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_getNegotiatedHeaderExtensions
240+
(JNIEnv * env, jobject caller)
241+
{
242+
return jni::CallFromJava(env, [&]() -> jobject {
243+
jni::NativeLease<webrtc::RtpTransceiverInterface> transceiver(env, caller);
244+
CHECK_HANDLEV(transceiver, nullptr);
245+
246+
return jni::JavaList::toArrayList(env, transceiver->GetNegotiatedHeaderExtensions(),
247+
jni::RTCRtpHeaderExtensionCapability::toJava).release();
248+
});
249+
}
250+
251+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_applyHeaderExtensionsToNegotiate
252+
(JNIEnv * env, jobject caller, jobject jExtensions)
253+
{
254+
jni::CallFromJava(env, [&]() {
255+
jni::NativeLease<webrtc::RtpTransceiverInterface> transceiver(env, caller);
256+
CHECK_HANDLE(transceiver);
257+
258+
try {
259+
auto ref = jni::JavaLocalRef<jobject>(env, jExtensions);
260+
auto extensions = jni::JavaList::toVector(env, ref, &jni::RTCRtpHeaderExtensionCapability::toNative);
261+
262+
webrtc::RTCError result = transceiver->SetHeaderExtensionsToNegotiate(extensions);
263+
264+
if (result.type() == webrtc::RTCErrorType::INVALID_MODIFICATION) {
265+
ThrowJavaException(env, "java/lang/IllegalArgumentException", result.message());
266+
}
267+
else if (!result.ok()) {
268+
env->Throw(jni::JavaRuntimeException(env, "Set header extensions to negotiate failed: %s",
269+
jni::RTCErrorToString(result).c_str()));
270+
}
271+
}
272+
catch (...) {
273+
ThrowCxxJavaException(env);
274+
}
275+
});
276+
}
277+
226278
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_dispose
227279
(JNIEnv * env, jobject caller)
228280
{

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "api/RTCRtpHeaderExtensionCapability.h"
1818
#include "JavaClasses.h"
19+
#include "JavaEnums.h"
1920
#include "JavaObject.h"
2021
#include "JavaString.h"
2122
#include "JavaUtils.h"
@@ -30,18 +31,37 @@ namespace jni
3031
const auto javaClass = JavaClasses::get<JavaRTCRtpHeaderExtensionCapabilityClass>(env);
3132

3233
JavaLocalRef<jstring> uri = JavaString::toJava(env, capability.uri);
34+
JavaLocalRef<jobject> direction = JavaEnums::toJava(env, capability.direction);
3335

34-
jobject object = env->NewObject(javaClass->cls, javaClass->ctor, uri.get());
36+
jobject object = env->NewObject(javaClass->cls, javaClass->ctor, uri.get(), direction.get());
3537
ExceptionCheck(env);
3638

3739
return JavaLocalRef<jobject>(env, object);
3840
}
3941

42+
webrtc::RtpHeaderExtensionCapability toNative(JNIEnv * env, const JavaRef<jobject> & capability)
43+
{
44+
const auto javaClass = JavaClasses::get<JavaRTCRtpHeaderExtensionCapabilityClass>(env);
45+
46+
JavaObject obj(env, capability);
47+
48+
// Only the URI and the direction are the application's to set;
49+
// the transceiver keeps the IDs it chose.
50+
webrtc::RtpHeaderExtensionCapability extension;
51+
extension.uri = JavaString::toNative(env, obj.getString(javaClass->uri));
52+
extension.direction = JavaEnums::toNative<webrtc::RtpTransceiverDirection>(env, obj.getObject(javaClass->direction));
53+
54+
return extension;
55+
}
56+
4057
JavaRTCRtpHeaderExtensionCapabilityClass::JavaRTCRtpHeaderExtensionCapabilityClass(JNIEnv * env)
4158
{
4259
cls = FindClass(env, PKG"RTCRtpHeaderExtensionCapability");
4360

44-
ctor = GetMethod(env, cls, "<init>", "(" STRING_SIG ")V");
61+
ctor = GetMethod(env, cls, "<init>", "(" STRING_SIG "L" PKG "RTCRtpTransceiverDirection;)V");
62+
63+
uri = GetFieldID(env, cls, "uri", STRING_SIG);
64+
direction = GetFieldID(env, cls, "direction", "L" PKG "RTCRtpTransceiverDirection;");
4565
}
4666
}
4767
}

‎webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpHeaderExtensionCapability.java‎

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,99 @@
1616

1717
package dev.onvoid.webrtc;
1818

19+
import static java.util.Objects.requireNonNull;
20+
21+
import java.util.Objects;
22+
1923
/**
20-
* Describes supported RTP header extensions.
24+
* Describes an RTP header extension: one that WebRTC supports, as {@link
25+
* RTCRtpCapabilities#getHeaderExtensions()} lists them, or one of a
26+
* transceiver, with the direction it is to be negotiated in.
2127
*
2228
* @author Alex Andres
29+
*
30+
* @see RTCRtpTransceiver#setHeaderExtensionsToNegotiate(java.util.List)
2331
*/
2432
public class RTCRtpHeaderExtensionCapability {
2533

26-
/**
27-
* The URI of the RTP header extension.
28-
*/
2934
private final String uri;
3035

36+
private final RTCRtpTransceiverDirection direction;
37+
3138

3239
/**
33-
* Creates an instance of RTCRtpHeaderExtensionCapability with the specified
34-
* RTP header extension URI.
40+
* Creates a header extension to be negotiated in both directions.
3541
*
36-
* @param uri The URI of the RTP header extension.
42+
* @param uri The URI of the header extension.
3743
*/
3844
public RTCRtpHeaderExtensionCapability(String uri) {
39-
this.uri = uri;
45+
this(uri, RTCRtpTransceiverDirection.SEND_RECV);
4046
}
4147

4248
/**
43-
* Returns the URI of the supported RTP header extension by the system.
49+
* Creates a header extension to be negotiated in the given direction.
50+
* {@link RTCRtpTransceiverDirection#STOPPED} keeps it out of the
51+
* negotiation.
4452
*
45-
* @return The URI of the RTP header extension.
53+
* @param uri The URI of the header extension.
54+
* @param direction The direction to negotiate it in.
55+
*/
56+
public RTCRtpHeaderExtensionCapability(String uri, RTCRtpTransceiverDirection direction) {
57+
this.uri = requireNonNull(uri, "uri");
58+
this.direction = requireNonNull(direction, "direction");
59+
}
60+
61+
/**
62+
* @return The URI of the header extension, e.g. {@code
63+
* urn:ietf:params:rtp-hdrext:sdes:mid}.
4664
*/
4765
public String getUri() {
4866
return uri;
4967
}
5068

69+
/**
70+
* @return The direction the header extension is negotiated in, or {@link
71+
* RTCRtpTransceiverDirection#STOPPED} if it is not negotiated.
72+
*/
73+
public RTCRtpTransceiverDirection getDirection() {
74+
return direction;
75+
}
76+
77+
/**
78+
* Returns the same header extension with another direction, e.g. to stop
79+
* one in the list of {@link
80+
* RTCRtpTransceiver#getHeaderExtensionsToNegotiate()}.
81+
*
82+
* @param direction The direction to negotiate it in.
83+
*
84+
* @return The header extension with that direction.
85+
*/
86+
public RTCRtpHeaderExtensionCapability withDirection(RTCRtpTransceiverDirection direction) {
87+
return new RTCRtpHeaderExtensionCapability(uri, direction);
88+
}
89+
90+
@Override
91+
public boolean equals(Object obj) {
92+
if (this == obj) {
93+
return true;
94+
}
95+
if (!(obj instanceof RTCRtpHeaderExtensionCapability)) {
96+
return false;
97+
}
98+
99+
RTCRtpHeaderExtensionCapability other = (RTCRtpHeaderExtensionCapability) obj;
100+
101+
return uri.equals(other.uri) && direction == other.direction;
102+
}
103+
104+
@Override
105+
public int hashCode() {
106+
return Objects.hash(uri, direction);
107+
}
108+
51109
@Override
52110
public String toString() {
53-
return String.format("%s [uri=%s]",
54-
RTCRtpHeaderExtensionCapability.class.getSimpleName(), uri);
111+
return String.format("%s [uri=%s, direction=%s]",
112+
RTCRtpHeaderExtensionCapability.class.getSimpleName(), uri, direction);
55113
}
56114
}

‎webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpTransceiver.java‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package dev.onvoid.webrtc;
1818

19+
import static java.util.Objects.requireNonNull;
20+
1921
import dev.onvoid.webrtc.internal.DisposableNativeObject;
2022

2123
import java.util.List;
@@ -123,10 +125,10 @@ private RTCRtpTransceiver() {
123125
public native boolean stopped();
124126

125127
/**
126-
* Returns the default codec preferences used by WebRTC for this
127-
* transceiver.
128+
* Returns the codec preferences set with {@link
129+
* #setCodecPreferences(List)}.
128130
*
129-
* @return The default codec preferences.
131+
* @return The codec preferences, empty if none are set.
130132
*/
131133
public native List<RTCRtpCodecCapability> getCodecPreferences();
132134

@@ -150,6 +152,56 @@ private RTCRtpTransceiver() {
150152
public native void setCodecPreferences(
151153
List<RTCRtpCodecCapability> preferences);
152154

155+
/**
156+
* Returns the header extensions that the next negotiation of this
157+
* transceiver offers or answers, each with the direction it is negotiated
158+
* in. Until {@link #setHeaderExtensionsToNegotiate(List)} is called they
159+
* are the ones WebRTC supports; once negotiated they follow what was
160+
* negotiated. An extension that is not negotiated has the direction
161+
* {@link RTCRtpTransceiverDirection#STOPPED}.
162+
*
163+
* @return The header extensions to negotiate, in their order.
164+
*/
165+
public native List<RTCRtpHeaderExtensionCapability> getHeaderExtensionsToNegotiate();
166+
167+
/**
168+
* Returns the header extensions of this transceiver with the directions
169+
* that the last negotiation agreed on. Those it left out have the
170+
* direction {@link RTCRtpTransceiverDirection#STOPPED}, as do all of them
171+
* before the first negotiation.
172+
*
173+
* @return The header extensions with their negotiated directions.
174+
*/
175+
public native List<RTCRtpHeaderExtensionCapability> getNegotiatedHeaderExtensions();
176+
177+
/**
178+
* Changes which header extensions the next negotiation of this
179+
* transceiver offers or answers. Take the list of {@link
180+
* #getHeaderExtensionsToNegotiate()}, change the directions of the
181+
* extensions to change, e.g. to {@link RTCRtpTransceiverDirection#STOPPED}
182+
* with {@link RTCRtpHeaderExtensionCapability#withDirection}, and pass it
183+
* here. The change takes effect with the next negotiation, which it
184+
* asks for.
185+
* <p>
186+
* The list must have the same extensions in the same order. Mandatory
187+
* extensions, such as the one that carries the MID, cannot be stopped.
188+
*
189+
* @param extensions The header extensions to negotiate.
190+
*
191+
* @throws IllegalArgumentException if the list does not have the same
192+
* extensions in the same order, or stops
193+
* a mandatory extension.
194+
*/
195+
public void setHeaderExtensionsToNegotiate(List<RTCRtpHeaderExtensionCapability> extensions) {
196+
requireNonNull(extensions, "extensions");
197+
198+
for (RTCRtpHeaderExtensionCapability extension : extensions) {
199+
requireNonNull(extension, "extension");
200+
}
201+
202+
applyHeaderExtensionsToNegotiate(extensions);
203+
}
204+
153205
/**
154206
* Releases the native reference held by this RTCRtpTransceiver instance.
155207
* <p>
@@ -188,4 +240,6 @@ public int hashCode() {
188240
return nativeHashCode();
189241
}
190242

243+
private native void applyHeaderExtensionsToNegotiate(List<RTCRtpHeaderExtensionCapability> extensions);
244+
191245
}

0 commit comments

Comments
 (0)