Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/guide/media/directionality.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ pc.setLocalDescription(new SetSessionDescriptionObserver() {
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.
:::

## Choosing RTP header extensions

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`:

```java
List<RTCRtpHeaderExtensionCapability> extensions = transceiver.getHeaderExtensionsToNegotiate();
List<RTCRtpHeaderExtensionCapability> changed = new ArrayList<>();

for (RTCRtpHeaderExtensionCapability extension : extensions) {
if (extension.getUri().equals("urn:ietf:params:rtp-hdrext:ssrc-audio-level")) {
extension = extension.withDirection(RTCRtpTransceiverDirection.STOPPED);
}
changed.add(extension);
}

transceiver.setHeaderExtensionsToNegotiate(changed);
```

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.

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.

## Common patterns and tips

- 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.
Expand Down
24 changes: 24 additions & 0 deletions webrtc-jni/src/main/cpp/include/JNI_RTCRtpTransceiver.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ namespace jni

jclass cls;
jmethodID ctor;
jfieldID uri;
jfieldID direction;
};

JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::RtpHeaderExtensionCapability & capability);
webrtc::RtpHeaderExtensionCapability toNative(JNIEnv * env, const JavaRef<jobject> & capability);
}
}

Expand Down
54 changes: 53 additions & 1 deletion webrtc-jni/src/main/cpp/src/JNI_RTCRtpTransceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "JavaBoundary.h"
#include "NativeHandle.h"
#include "api/RTCRtpCodecCapability.h"
#include "api/RTCRtpHeaderExtensionCapability.h"
#include "JavaEnums.h"
#include "JavaError.h"
#include "JavaFactories.h"
Expand Down Expand Up @@ -192,7 +193,7 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_getCodecPrefe

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

return capabilities.get();
return capabilities.release();
}
catch (...) {
ThrowCxxJavaException(env);
Expand Down Expand Up @@ -223,6 +224,57 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_setCodecPreferen
});
}

JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_getHeaderExtensionsToNegotiate
(JNIEnv * env, jobject caller)
{
return jni::CallFromJava(env, [&]() -> jobject {
jni::NativeLease<webrtc::RtpTransceiverInterface> transceiver(env, caller);
CHECK_HANDLEV(transceiver, nullptr);

return jni::JavaList::toArrayList(env, transceiver->GetHeaderExtensionsToNegotiate(),
jni::RTCRtpHeaderExtensionCapability::toJava).release();
});
}

JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_getNegotiatedHeaderExtensions
(JNIEnv * env, jobject caller)
{
return jni::CallFromJava(env, [&]() -> jobject {
jni::NativeLease<webrtc::RtpTransceiverInterface> transceiver(env, caller);
CHECK_HANDLEV(transceiver, nullptr);

return jni::JavaList::toArrayList(env, transceiver->GetNegotiatedHeaderExtensions(),
jni::RTCRtpHeaderExtensionCapability::toJava).release();
});
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_applyHeaderExtensionsToNegotiate
(JNIEnv * env, jobject caller, jobject jExtensions)
{
jni::CallFromJava(env, [&]() {
jni::NativeLease<webrtc::RtpTransceiverInterface> transceiver(env, caller);
CHECK_HANDLE(transceiver);

try {
auto ref = jni::JavaLocalRef<jobject>(env, jExtensions);
auto extensions = jni::JavaList::toVector(env, ref, &jni::RTCRtpHeaderExtensionCapability::toNative);

webrtc::RTCError result = transceiver->SetHeaderExtensionsToNegotiate(extensions);

if (result.type() == webrtc::RTCErrorType::INVALID_MODIFICATION) {
ThrowJavaException(env, "java/lang/IllegalArgumentException", result.message());
}
else if (!result.ok()) {
env->Throw(jni::JavaRuntimeException(env, "Set header extensions to negotiate failed: %s",
jni::RTCErrorToString(result).c_str()));
}
}
catch (...) {
ThrowCxxJavaException(env);
}
});
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_dispose
(JNIEnv * env, jobject caller)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "api/RTCRtpHeaderExtensionCapability.h"
#include "JavaClasses.h"
#include "JavaEnums.h"
#include "JavaObject.h"
#include "JavaString.h"
#include "JavaUtils.h"
Expand All @@ -30,18 +31,37 @@ namespace jni
const auto javaClass = JavaClasses::get<JavaRTCRtpHeaderExtensionCapabilityClass>(env);

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

jobject object = env->NewObject(javaClass->cls, javaClass->ctor, uri.get());
jobject object = env->NewObject(javaClass->cls, javaClass->ctor, uri.get(), direction.get());
ExceptionCheck(env);

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

webrtc::RtpHeaderExtensionCapability toNative(JNIEnv * env, const JavaRef<jobject> & capability)
{
const auto javaClass = JavaClasses::get<JavaRTCRtpHeaderExtensionCapabilityClass>(env);

JavaObject obj(env, capability);

// Only the URI and the direction are the application's to set;
// the transceiver keeps the IDs it chose.
webrtc::RtpHeaderExtensionCapability extension;
extension.uri = JavaString::toNative(env, obj.getString(javaClass->uri));
extension.direction = JavaEnums::toNative<webrtc::RtpTransceiverDirection>(env, obj.getObject(javaClass->direction));

return extension;
}

JavaRTCRtpHeaderExtensionCapabilityClass::JavaRTCRtpHeaderExtensionCapabilityClass(JNIEnv * env)
{
cls = FindClass(env, PKG"RTCRtpHeaderExtensionCapability");

ctor = GetMethod(env, cls, "<init>", "(" STRING_SIG ")V");
ctor = GetMethod(env, cls, "<init>", "(" STRING_SIG "L" PKG "RTCRtpTransceiverDirection;)V");

uri = GetFieldID(env, cls, "uri", STRING_SIG);
direction = GetFieldID(env, cls, "direction", "L" PKG "RTCRtpTransceiverDirection;");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,99 @@

package dev.onvoid.webrtc;

import static java.util.Objects.requireNonNull;

import java.util.Objects;

/**
* Describes supported RTP header extensions.
* Describes an RTP header extension: one that WebRTC supports, as {@link
* RTCRtpCapabilities#getHeaderExtensions()} lists them, or one of a
* transceiver, with the direction it is to be negotiated in.
*
* @author Alex Andres
*
* @see RTCRtpTransceiver#setHeaderExtensionsToNegotiate(java.util.List)
*/
public class RTCRtpHeaderExtensionCapability {

/**
* The URI of the RTP header extension.
*/
private final String uri;

private final RTCRtpTransceiverDirection direction;


/**
* Creates an instance of RTCRtpHeaderExtensionCapability with the specified
* RTP header extension URI.
* Creates a header extension to be negotiated in both directions.
*
* @param uri The URI of the RTP header extension.
* @param uri The URI of the header extension.
*/
public RTCRtpHeaderExtensionCapability(String uri) {
this.uri = uri;
this(uri, RTCRtpTransceiverDirection.SEND_RECV);
}

/**
* Returns the URI of the supported RTP header extension by the system.
* Creates a header extension to be negotiated in the given direction.
* {@link RTCRtpTransceiverDirection#STOPPED} keeps it out of the
* negotiation.
*
* @return The URI of the RTP header extension.
* @param uri The URI of the header extension.
* @param direction The direction to negotiate it in.
*/
public RTCRtpHeaderExtensionCapability(String uri, RTCRtpTransceiverDirection direction) {
this.uri = requireNonNull(uri, "uri");
this.direction = requireNonNull(direction, "direction");
}

/**
* @return The URI of the header extension, e.g. {@code
* urn:ietf:params:rtp-hdrext:sdes:mid}.
*/
public String getUri() {
return uri;
}

/**
* @return The direction the header extension is negotiated in, or {@link
* RTCRtpTransceiverDirection#STOPPED} if it is not negotiated.
*/
public RTCRtpTransceiverDirection getDirection() {
return direction;
}

/**
* Returns the same header extension with another direction, e.g. to stop
* one in the list of {@link
* RTCRtpTransceiver#getHeaderExtensionsToNegotiate()}.
*
* @param direction The direction to negotiate it in.
*
* @return The header extension with that direction.
*/
public RTCRtpHeaderExtensionCapability withDirection(RTCRtpTransceiverDirection direction) {
return new RTCRtpHeaderExtensionCapability(uri, direction);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof RTCRtpHeaderExtensionCapability)) {
return false;
}

RTCRtpHeaderExtensionCapability other = (RTCRtpHeaderExtensionCapability) obj;

return uri.equals(other.uri) && direction == other.direction;
}

@Override
public int hashCode() {
return Objects.hash(uri, direction);
}

@Override
public String toString() {
return String.format("%s [uri=%s]",
RTCRtpHeaderExtensionCapability.class.getSimpleName(), uri);
return String.format("%s [uri=%s, direction=%s]",
RTCRtpHeaderExtensionCapability.class.getSimpleName(), uri, direction);
}
}
60 changes: 57 additions & 3 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpTransceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package dev.onvoid.webrtc;

import static java.util.Objects.requireNonNull;

import dev.onvoid.webrtc.internal.DisposableNativeObject;

import java.util.List;
Expand Down Expand Up @@ -123,10 +125,10 @@ private RTCRtpTransceiver() {
public native boolean stopped();

/**
* Returns the default codec preferences used by WebRTC for this
* transceiver.
* Returns the codec preferences set with {@link
* #setCodecPreferences(List)}.
*
* @return The default codec preferences.
* @return The codec preferences, empty if none are set.
*/
public native List<RTCRtpCodecCapability> getCodecPreferences();

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

/**
* Returns the header extensions that the next negotiation of this
* transceiver offers or answers, each with the direction it is negotiated
* in. Until {@link #setHeaderExtensionsToNegotiate(List)} is called they
* are the ones WebRTC supports; once negotiated they follow what was
* negotiated. An extension that is not negotiated has the direction
* {@link RTCRtpTransceiverDirection#STOPPED}.
*
* @return The header extensions to negotiate, in their order.
*/
public native List<RTCRtpHeaderExtensionCapability> getHeaderExtensionsToNegotiate();

/**
* Returns the header extensions of this transceiver with the directions
* that the last negotiation agreed on. Those it left out have the
* direction {@link RTCRtpTransceiverDirection#STOPPED}, as do all of them
* before the first negotiation.
*
* @return The header extensions with their negotiated directions.
*/
public native List<RTCRtpHeaderExtensionCapability> getNegotiatedHeaderExtensions();

/**
* Changes which header extensions the next negotiation of this
* transceiver offers or answers. Take the list of {@link
* #getHeaderExtensionsToNegotiate()}, change the directions of the
* extensions to change, e.g. to {@link RTCRtpTransceiverDirection#STOPPED}
* with {@link RTCRtpHeaderExtensionCapability#withDirection}, and pass it
* here. The change takes effect with the next negotiation, which it
* asks for.
* <p>
* The list must have the same extensions in the same order. Mandatory
* extensions, such as the one that carries the MID, cannot be stopped.
*
* @param extensions The header extensions to negotiate.
*
* @throws IllegalArgumentException if the list does not have the same
* extensions in the same order, or stops
* a mandatory extension.
*/
public void setHeaderExtensionsToNegotiate(List<RTCRtpHeaderExtensionCapability> extensions) {
requireNonNull(extensions, "extensions");

for (RTCRtpHeaderExtensionCapability extension : extensions) {
requireNonNull(extension, "extension");
}

applyHeaderExtensionsToNegotiate(extensions);
}

/**
* Releases the native reference held by this RTCRtpTransceiver instance.
* <p>
Expand Down Expand Up @@ -188,4 +240,6 @@ public int hashCode() {
return nativeHashCode();
}

private native void applyHeaderExtensionsToNegotiate(List<RTCRtpHeaderExtensionCapability> extensions);

}
Loading
Loading