diff --git a/docs/guide/media/directionality.md b/docs/guide/media/directionality.md index 1f0c6e17..a8c0fe9a 100644 --- a/docs/guide/media/directionality.md +++ b/docs/guide/media/directionality.md @@ -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 extensions = transceiver.getHeaderExtensionsToNegotiate(); +List 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. diff --git a/webrtc-jni/src/main/cpp/include/JNI_RTCRtpTransceiver.h b/webrtc-jni/src/main/cpp/include/JNI_RTCRtpTransceiver.h index b492439f..b27b879a 100644 --- a/webrtc-jni/src/main/cpp/include/JNI_RTCRtpTransceiver.h +++ b/webrtc-jni/src/main/cpp/include/JNI_RTCRtpTransceiver.h @@ -95,6 +95,30 @@ extern "C" { JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_setCodecPreferences (JNIEnv *, jobject, jobject); + /* + * Class: dev_onvoid_webrtc_RTCRtpTransceiver + * Method: getHeaderExtensionsToNegotiate + * Signature: ()Ljava/util/List; + */ + JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_getHeaderExtensionsToNegotiate + (JNIEnv *, jobject); + + /* + * Class: dev_onvoid_webrtc_RTCRtpTransceiver + * Method: getNegotiatedHeaderExtensions + * Signature: ()Ljava/util/List; + */ + JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_getNegotiatedHeaderExtensions + (JNIEnv *, jobject); + + /* + * Class: dev_onvoid_webrtc_RTCRtpTransceiver + * Method: applyHeaderExtensionsToNegotiate + * Signature: (Ljava/util/List;)V + */ + JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCRtpTransceiver_applyHeaderExtensionsToNegotiate + (JNIEnv *, jobject, jobject); + /* * Class: dev_onvoid_webrtc_RTCRtpTransceiver * Method: dispose diff --git a/webrtc-jni/src/main/cpp/include/api/RTCRtpHeaderExtensionCapability.h b/webrtc-jni/src/main/cpp/include/api/RTCRtpHeaderExtensionCapability.h index cd7c68f1..7f733ceb 100644 --- a/webrtc-jni/src/main/cpp/include/api/RTCRtpHeaderExtensionCapability.h +++ b/webrtc-jni/src/main/cpp/include/api/RTCRtpHeaderExtensionCapability.h @@ -35,9 +35,12 @@ namespace jni jclass cls; jmethodID ctor; + jfieldID uri; + jfieldID direction; }; JavaLocalRef toJava(JNIEnv * env, const webrtc::RtpHeaderExtensionCapability & capability); + webrtc::RtpHeaderExtensionCapability toNative(JNIEnv * env, const JavaRef & capability); } } diff --git a/webrtc-jni/src/main/cpp/src/JNI_RTCRtpTransceiver.cpp b/webrtc-jni/src/main/cpp/src/JNI_RTCRtpTransceiver.cpp index 3951c7a4..7c6c04e9 100644 --- a/webrtc-jni/src/main/cpp/src/JNI_RTCRtpTransceiver.cpp +++ b/webrtc-jni/src/main/cpp/src/JNI_RTCRtpTransceiver.cpp @@ -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" @@ -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); @@ -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 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 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 transceiver(env, caller); + CHECK_HANDLE(transceiver); + + try { + auto ref = jni::JavaLocalRef(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) { diff --git a/webrtc-jni/src/main/cpp/src/api/RTCRtpHeaderExtensionCapability.cpp b/webrtc-jni/src/main/cpp/src/api/RTCRtpHeaderExtensionCapability.cpp index 526e876f..ab7d21fb 100644 --- a/webrtc-jni/src/main/cpp/src/api/RTCRtpHeaderExtensionCapability.cpp +++ b/webrtc-jni/src/main/cpp/src/api/RTCRtpHeaderExtensionCapability.cpp @@ -16,6 +16,7 @@ #include "api/RTCRtpHeaderExtensionCapability.h" #include "JavaClasses.h" +#include "JavaEnums.h" #include "JavaObject.h" #include "JavaString.h" #include "JavaUtils.h" @@ -30,18 +31,37 @@ namespace jni const auto javaClass = JavaClasses::get(env); JavaLocalRef uri = JavaString::toJava(env, capability.uri); + JavaLocalRef 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(env, object); } + webrtc::RtpHeaderExtensionCapability toNative(JNIEnv * env, const JavaRef & capability) + { + const auto javaClass = JavaClasses::get(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(env, obj.getObject(javaClass->direction)); + + return extension; + } + JavaRTCRtpHeaderExtensionCapabilityClass::JavaRTCRtpHeaderExtensionCapabilityClass(JNIEnv * env) { cls = FindClass(env, PKG"RTCRtpHeaderExtensionCapability"); - ctor = GetMethod(env, cls, "", "(" STRING_SIG ")V"); + ctor = GetMethod(env, cls, "", "(" STRING_SIG "L" PKG "RTCRtpTransceiverDirection;)V"); + + uri = GetFieldID(env, cls, "uri", STRING_SIG); + direction = GetFieldID(env, cls, "direction", "L" PKG "RTCRtpTransceiverDirection;"); } } } \ No newline at end of file diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpHeaderExtensionCapability.java b/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpHeaderExtensionCapability.java index 32639046..c1a4a342 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpHeaderExtensionCapability.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpHeaderExtensionCapability.java @@ -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); } } diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpTransceiver.java b/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpTransceiver.java index 80ac96a7..4465bda5 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpTransceiver.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpTransceiver.java @@ -16,6 +16,8 @@ package dev.onvoid.webrtc; +import static java.util.Objects.requireNonNull; + import dev.onvoid.webrtc.internal.DisposableNativeObject; import java.util.List; @@ -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 getCodecPreferences(); @@ -150,6 +152,56 @@ private RTCRtpTransceiver() { public native void setCodecPreferences( List 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 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 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. + *

+ * 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 extensions) { + requireNonNull(extensions, "extensions"); + + for (RTCRtpHeaderExtensionCapability extension : extensions) { + requireNonNull(extension, "extension"); + } + + applyHeaderExtensionsToNegotiate(extensions); + } + /** * Releases the native reference held by this RTCRtpTransceiver instance. *

@@ -188,4 +240,6 @@ public int hashCode() { return nativeHashCode(); } + private native void applyHeaderExtensionsToNegotiate(List extensions); + } diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/RTCRtpTransceiverTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/RTCRtpTransceiverTests.java index aadc5a1a..924b6304 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/RTCRtpTransceiverTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/RTCRtpTransceiverTests.java @@ -19,6 +19,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -161,8 +162,101 @@ public Set> entrySet() { assertEquals("Parameters cannot be read", e.getMessage()); List preferences = transceiver.getCodecPreferences(); - // Still none set, which comes back as null. - assertTrue(preferences == null || preferences.isEmpty(), String.valueOf(preferences)); + // Still none set. + assertTrue(preferences.isEmpty(), String.valueOf(preferences)); + } + + @Test + void codecPreferencesReadBack() { + RTCRtpTransceiver transceiver = audioTransceiver(); + + assertTrue(transceiver.getCodecPreferences().isEmpty()); + + RTCRtpCodecCapability codec = factory.getRtpReceiverCapabilities(MediaType.AUDIO).getCodecs().get(0); + transceiver.setCodecPreferences(List.of(codec)); + + List preferences = transceiver.getCodecPreferences(); + + assertEquals(1, preferences.size()); + assertEquals(codec.getName(), preferences.get(0).getName()); + assertEquals(codec.getClockRate(), preferences.get(0).getClockRate()); + } + + @Test + void stoppedHeaderExtensionNotOffered() throws Exception { + RTCRtpTransceiver transceiver = audioTransceiver(); + List extensions = transceiver.getHeaderExtensionsToNegotiate(); + + assertTrue(extensions.contains(new RTCRtpHeaderExtensionCapability(AUDIO_LEVEL)), String.valueOf(extensions)); + + transceiver.setHeaderExtensionsToNegotiate(withDirection(extensions, AUDIO_LEVEL, + RTCRtpTransceiverDirection.STOPPED)); + + assertTrue(transceiver.getHeaderExtensionsToNegotiate().contains( + new RTCRtpHeaderExtensionCapability(AUDIO_LEVEL, RTCRtpTransceiverDirection.STOPPED))); + // Nothing negotiated yet. + assertTrue(transceiver.getNegotiatedHeaderExtensions().stream() + .allMatch(e -> e.getDirection() == RTCRtpTransceiverDirection.STOPPED)); + + TestPeerConnection callee = new TestPeerConnection(factory); + + try { + RTCSessionDescription offer = connection.createOffer(); + + assertFalse(offer.sdp.contains(AUDIO_LEVEL), offer.sdp); + assertTrue(offer.sdp.contains(MID), offer.sdp); + + callee.setRemoteDescription(offer); + connection.setRemoteDescription(callee.createAnswer()); + + Map negotiated = transceiver.getNegotiatedHeaderExtensions() + .stream() + .collect(Collectors.toMap(RTCRtpHeaderExtensionCapability::getUri, + RTCRtpHeaderExtensionCapability::getDirection)); + + assertEquals(RTCRtpTransceiverDirection.SEND_RECV, negotiated.get(MID), String.valueOf(negotiated)); + assertEquals(RTCRtpTransceiverDirection.STOPPED, negotiated.get(AUDIO_LEVEL), String.valueOf(negotiated)); + } + finally { + callee.close(); + } + } + + @Test + void invalidHeaderExtensionsRejected() { + RTCRtpTransceiver transceiver = audioTransceiver(); + List extensions = transceiver.getHeaderExtensionsToNegotiate(); + + List reordered = new ArrayList<>(extensions); + java.util.Collections.reverse(reordered); + + assertThrows(IllegalArgumentException.class, () -> transceiver.setHeaderExtensionsToNegotiate(reordered)); + assertThrows(IllegalArgumentException.class, () -> transceiver.setHeaderExtensionsToNegotiate( + extensions.subList(1, extensions.size()))); + assertThrows(IllegalArgumentException.class, () -> transceiver.setHeaderExtensionsToNegotiate( + withDirection(extensions, MID, RTCRtpTransceiverDirection.STOPPED))); + assertThrows(NullPointerException.class, () -> transceiver.setHeaderExtensionsToNegotiate(null)); + + // Nothing changed. + assertEquals(extensions, transceiver.getHeaderExtensionsToNegotiate()); + } + + private static final String AUDIO_LEVEL = "urn:ietf:params:rtp-hdrext:ssrc-audio-level"; + + private static final String MID = "urn:ietf:params:rtp-hdrext:sdes:mid"; + + private RTCRtpTransceiver audioTransceiver() { + AudioTrackSource audioSource = factory.createAudioSource(new AudioOptions()); + AudioTrack audioTrack = factory.createAudioTrack("audioTrack", audioSource); + + return connection.getPeerConnection().addTransceiver(audioTrack, new RTCRtpTransceiverInit()); + } + + private static List withDirection( + List extensions, String uri, RTCRtpTransceiverDirection direction) { + return extensions.stream() + .map(e -> e.getUri().equals(uri) ? e.withDirection(direction) : e) + .collect(Collectors.toList()); } }