diff --git a/docs/guide/data/data-channels.md b/docs/guide/data/data-channels.md index 26598fca..0247a57b 100644 --- a/docs/guide/data/data-channels.md +++ b/docs/guide/data/data-channels.md @@ -285,6 +285,23 @@ RTCDataChannelState state = dataChannel.getState(); long bufferedAmount = dataChannel.getBufferedAmount(); ``` +### Message Size and Channel Limits + +All data channels of a connection share one SCTP transport. It knows how large a message the remote peer accepts, and how many channels can be open at once: + +```java +RTCSctpTransport transport = peerConnection.getSctpTransport(); + +if (transport != null) { + Double maxMessageSize = transport.getMaxMessageSize(); // in bytes, null while unknown + Integer maxChannels = transport.getMaxChannels(); // null until connected + + transport.dispose(); +} +``` + +Split a message larger than `getMaxMessageSize()` into parts, since the channel refuses it. Two WebRTC peers agree on 256 KiB. `getSctpTransport()` returns `null` until a description with data channels is applied. Each call returns a new `RTCSctpTransport`, which you dispose. `getState()` tells whether the association is `CONNECTING`, `CONNECTED` or `CLOSED`, and `getTransport()` returns the DTLS transport beneath it. + ## Closing and Cleanup When you're done with a data channel, you should properly clean it up: diff --git a/webrtc-jni/src/main/cpp/include/JNI_RTCPeerConnection.h b/webrtc-jni/src/main/cpp/include/JNI_RTCPeerConnection.h index 0e0feee7..05917237 100644 --- a/webrtc-jni/src/main/cpp/include/JNI_RTCPeerConnection.h +++ b/webrtc-jni/src/main/cpp/include/JNI_RTCPeerConnection.h @@ -279,6 +279,14 @@ extern "C" { JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_getStats__Ldev_onvoid_webrtc_RTCRtpSender_2Ldev_onvoid_webrtc_RTCStatsCollectorCallback_2 (JNIEnv *, jobject, jobject, jobject); + /* + * Class: dev_onvoid_webrtc_RTCPeerConnection + * Method: getSctpTransport + * Signature: ()Ldev/onvoid/webrtc/RTCSctpTransport; + */ + JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_getSctpTransport + (JNIEnv *, jobject); + /* * Class: dev_onvoid_webrtc_RTCPeerConnection * Method: restartIce diff --git a/webrtc-jni/src/main/cpp/include/JNI_RTCSctpTransport.h b/webrtc-jni/src/main/cpp/include/JNI_RTCSctpTransport.h new file mode 100644 index 00000000..ee56a58e --- /dev/null +++ b/webrtc-jni/src/main/cpp/include/JNI_RTCSctpTransport.h @@ -0,0 +1,53 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class dev_onvoid_webrtc_RTCSctpTransport */ + +#ifndef _Included_dev_onvoid_webrtc_RTCSctpTransport +#define _Included_dev_onvoid_webrtc_RTCSctpTransport +#ifdef __cplusplus +extern "C" { +#endif + /* + * Class: dev_onvoid_webrtc_RTCSctpTransport + * Method: getTransport + * Signature: ()Ldev/onvoid/webrtc/RTCDtlsTransport; + */ + JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getTransport + (JNIEnv *, jobject); + + /* + * Class: dev_onvoid_webrtc_RTCSctpTransport + * Method: getState + * Signature: ()Ldev/onvoid/webrtc/RTCSctpTransportState; + */ + JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getState + (JNIEnv *, jobject); + + /* + * Class: dev_onvoid_webrtc_RTCSctpTransport + * Method: getMaxMessageSize + * Signature: ()Ljava/lang/Double; + */ + JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getMaxMessageSize + (JNIEnv *, jobject); + + /* + * Class: dev_onvoid_webrtc_RTCSctpTransport + * Method: getMaxChannels + * Signature: ()Ljava/lang/Integer; + */ + JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getMaxChannels + (JNIEnv *, jobject); + + /* + * Class: dev_onvoid_webrtc_RTCSctpTransport + * Method: dispose + * Signature: ()V + */ + JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_dispose + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/webrtc-jni/src/main/cpp/src/JNI_RTCPeerConnection.cpp b/webrtc-jni/src/main/cpp/src/JNI_RTCPeerConnection.cpp index becbe0ff..cdf69ecb 100644 --- a/webrtc-jni/src/main/cpp/src/JNI_RTCPeerConnection.cpp +++ b/webrtc-jni/src/main/cpp/src/JNI_RTCPeerConnection.cpp @@ -864,6 +864,25 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_getStats__Ldev_o } } +JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_getSctpTransport +(JNIEnv * env, jobject caller) +{ + return jni::CallFromJava(env, [&]() -> jobject { + jni::NativeLease pc(env, caller); + CHECK_HANDLEV(pc, nullptr); + + webrtc::scoped_refptr transport = pc->GetSctpTransport(); + + if (!transport) { + return nullptr; + } + + // The Java object holds a reference until it is disposed, since the + // connection drops the transport once it closes. + return jni::JavaFactories::createOwning(env, std::move(transport)).release(); + }); +} + JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_restartIce (JNIEnv * env, jobject caller) { diff --git a/webrtc-jni/src/main/cpp/src/JNI_RTCSctpTransport.cpp b/webrtc-jni/src/main/cpp/src/JNI_RTCSctpTransport.cpp new file mode 100644 index 00000000..0d0c21ce --- /dev/null +++ b/webrtc-jni/src/main/cpp/src/JNI_RTCSctpTransport.cpp @@ -0,0 +1,111 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "JNI_RTCSctpTransport.h" +#include "JavaBoundary.h" +#include "NativeHandle.h" +#include "JavaEnums.h" +#include "JavaFactories.h" +#include "JavaPrimitive.h" +#include "JavaRef.h" +#include "JavaUtils.h" + +#include "api/sctp_transport_interface.h" + +// The transport has no proxy, and most of its methods must run on the network +// thread. Information() is the exception: it calls over to that thread itself, +// so everything here goes through it. + +JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getTransport +(JNIEnv * env, jobject caller) +{ + return jni::CallFromJava(env, [&]() -> jobject { + jni::NativeLease transport(env, caller); + CHECK_HANDLEV(transport, nullptr); + + webrtc::scoped_refptr dtlsTransport = transport->Information().dtls_transport(); + + if (!dtlsTransport) { + return nullptr; + } + + return jni::JavaFactories::createOwning(env, std::move(dtlsTransport)).release(); + }); +} + +JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getState +(JNIEnv * env, jobject caller) +{ + return jni::CallFromJava(env, [&]() -> jobject { + jni::NativeLease transport(env, caller); + CHECK_HANDLEV(transport, nullptr); + + return jni::JavaEnums::toJava(env, transport->Information().state()).release(); + }); +} + +JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getMaxMessageSize +(JNIEnv * env, jobject caller) +{ + return jni::CallFromJava(env, [&]() -> jobject { + jni::NativeLease transport(env, caller); + CHECK_HANDLEV(transport, nullptr); + + std::optional size = transport->Information().MaxMessageSize(); + + if (!size) { + return nullptr; + } + + return jni::Double::create(env, *size).release(); + }); +} + +JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getMaxChannels +(JNIEnv * env, jobject caller) +{ + return jni::CallFromJava(env, [&]() -> jobject { + jni::NativeLease transport(env, caller); + CHECK_HANDLEV(transport, nullptr); + + std::optional channels = transport->Information().MaxChannels(); + + if (!channels) { + return nullptr; + } + + return jni::Integer::create(env, *channels).release(); + }); +} + +JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_dispose +(JNIEnv * env, jobject caller) +{ + jni::CallFromJava(env, [&]() { + webrtc::SctpTransportInterface * transport = jni::TakeHandle(env, caller); + + if (transport == nullptr) { + return; + } + + // Runs once no other thread uses the transport through this object any + // more. The connection and other Java objects keep their own + // references, so this one need not be the last. + jni::ReleaseHandle(env, transport, [transport](bool) { + transport->Release(); + }); + }); +} diff --git a/webrtc-jni/src/main/cpp/src/WebRTCContext.cpp b/webrtc-jni/src/main/cpp/src/WebRTCContext.cpp index 3b3d6304..08ffb0dd 100644 --- a/webrtc-jni/src/main/cpp/src/WebRTCContext.cpp +++ b/webrtc-jni/src/main/cpp/src/WebRTCContext.cpp @@ -87,6 +87,7 @@ namespace jni JavaEnums::add(env, PKG_DESKTOP"DesktopCapturer$Result"); JavaEnums::add(env, PKG"RTCDtlsTransportState"); JavaEnums::add(env, PKG"RTCDtxStatus"); + JavaEnums::add(env, PKG"RTCSctpTransportState"); JavaEnums::add(env, PKG_MEDIA"MediaSource$State"); JavaEnums::add(env, PKG_MEDIA"MediaStreamTrackState"); JavaEnums::add(env, PKG_MEDIA"video/VideoContentHint"); @@ -132,6 +133,7 @@ namespace jni JavaFactories::add(env, PKG"RTCRtpReceiver"); JavaFactories::add(env, PKG"RTCRtpSender"); JavaFactories::add(env, PKG"RTCRtpTransceiver"); + JavaFactories::add(env, PKG"RTCSctpTransport"); initializeClassLoader(env, PKG_INTERNAL"NativeClassLoader"); } diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/RTCPeerConnection.java b/webrtc/src/main/java/dev/onvoid/webrtc/RTCPeerConnection.java index 6974ce0c..3d5eb10c 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/RTCPeerConnection.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/RTCPeerConnection.java @@ -462,6 +462,18 @@ public native void getStats(RTCRtpSender sender, */ public native void restartIce(); + /** + * Returns the SCTP transport that carries the data channels of this + * connection. There is one only once a session description with data + * channels has been applied, e.g. after {@link #setLocalDescription} + * of an offer made with a data channel. The instance is the caller's to + * dispose. + * + * @return The SCTP transport, or {@code null} if the connection has + * none. + */ + public native RTCSctpTransport getSctpTransport(); + /** * Closes the peer connection, terminates all media and releases any used * resources. diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/RTCSctpTransport.java b/webrtc/src/main/java/dev/onvoid/webrtc/RTCSctpTransport.java new file mode 100644 index 00000000..4599425b --- /dev/null +++ b/webrtc/src/main/java/dev/onvoid/webrtc/RTCSctpTransport.java @@ -0,0 +1,105 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc; + +import dev.onvoid.webrtc.internal.DisposableNativeObject; + +/** + * The SCTP transport that carries the data channels of a peer connection, + * as {@link RTCPeerConnection#getSctpTransport()} returns it. It tells how + * large a message the remote peer takes and how many data channels can be + * open at once. + *

+ * Each call of {@link RTCPeerConnection#getSctpTransport()} returns a new + * instance, which holds a reference to the transport until it is disposed. + * + * @author Alex Andres + */ +public class RTCSctpTransport extends DisposableNativeObject { + + /** + * Used by the native api. + */ + private RTCSctpTransport() { + + } + + /** + * Returns the DTLS transport over which the SCTP packets are sent and + * received. The instance is the caller's to dispose. + * + * @return The DTLS transport, or {@code null} once the transport is + * closed. + */ + public native RTCDtlsTransport getTransport(); + + /** + * @return The current state of the transport. + */ + public native RTCSctpTransportState getState(); + + /** + * Returns the largest message in bytes that can be sent with {@link + * RTCDataChannel#send(RTCDataChannelBuffer)}, as the two peers agreed on + * in their session descriptions. A message larger than that is refused. + * + * @return The largest message size in bytes, or {@code null} while it is + * not known yet. + */ + public native Double getMaxMessageSize(); + + /** + * Returns how many data channels can be open at the same time, as the + * SCTP association agreed on. + * + * @return The number of channels, or {@code null} before the transport + * is {@link RTCSctpTransportState#CONNECTED}. + */ + public native Integer getMaxChannels(); + + /** + * Releases the reference this instance holds. The transport itself stays + * with its peer connection, as do other instances of it. + */ + @Override + public native void dispose(); + + /** + * Two instances are equal if they refer to the same transport, e.g. when + * obtained from two calls of {@link RTCPeerConnection#getSctpTransport()}. + * A disposed instance is never equal to anything but itself. + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof RTCSctpTransport)) { + return false; + } + + long handle = getNativeHandle(); + + return handle != 0 && handle == ((RTCSctpTransport) obj).getNativeHandle(); + } + + @Override + public int hashCode() { + return nativeHashCode(); + } + +} diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/RTCSctpTransportState.java b/webrtc/src/main/java/dev/onvoid/webrtc/RTCSctpTransportState.java new file mode 100644 index 00000000..235a8b91 --- /dev/null +++ b/webrtc/src/main/java/dev/onvoid/webrtc/RTCSctpTransportState.java @@ -0,0 +1,49 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc; + +/** + * The state of an {@link RTCSctpTransport}. + * + * @author Alex Andres + */ +public enum RTCSctpTransportState { + + /** + * The transport has not started to set up its association yet. Not a + * state of the WebRTC specification, which starts with {@link + * #CONNECTING}. + */ + NEW, + + /** + * The transport is setting up its association. + */ + CONNECTING, + + /** + * The association is set up, and data channels can open. + */ + CONNECTED, + + /** + * The association was closed, by either peer or because the DTLS + * transport closed. + */ + CLOSED + +} diff --git a/webrtc/src/main/resources/META-INF/native-image/dev.onvoid.webrtc/webrtc-java/jni-config.json b/webrtc/src/main/resources/META-INF/native-image/dev.onvoid.webrtc/webrtc-java/jni-config.json index f5120de5..641229ae 100644 --- a/webrtc/src/main/resources/META-INF/native-image/dev.onvoid.webrtc/webrtc-java/jni-config.json +++ b/webrtc/src/main/resources/META-INF/native-image/dev.onvoid.webrtc/webrtc-java/jni-config.json @@ -621,6 +621,18 @@ "allDeclaredMethods": true, "allDeclaredFields": true }, + { + "name": "dev.onvoid.webrtc.RTCSctpTransport", + "allDeclaredConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "dev.onvoid.webrtc.RTCSctpTransportState", + "allDeclaredConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, { "name": "dev.onvoid.webrtc.RTCSdpType", "allDeclaredConstructors": true, diff --git a/webrtc/src/main/resources/META-INF/native-image/dev.onvoid.webrtc/webrtc-java/reflect-config.json b/webrtc/src/main/resources/META-INF/native-image/dev.onvoid.webrtc/webrtc-java/reflect-config.json index faa614dc..578c27d5 100644 --- a/webrtc/src/main/resources/META-INF/native-image/dev.onvoid.webrtc/webrtc-java/reflect-config.json +++ b/webrtc/src/main/resources/META-INF/native-image/dev.onvoid.webrtc/webrtc-java/reflect-config.json @@ -371,6 +371,14 @@ "name": "dev.onvoid.webrtc.RTCRtpTransceiverInit", "queryAllPublicMethods": true }, + { + "name": "dev.onvoid.webrtc.RTCSctpTransport", + "queryAllPublicMethods": true + }, + { + "name": "dev.onvoid.webrtc.RTCSctpTransportState", + "queryAllPublicMethods": true + }, { "name": "dev.onvoid.webrtc.RTCSdpType", "queryAllPublicMethods": true @@ -1104,6 +1112,12 @@ { "name": "dev.onvoid.webrtc.RTCRtpTransceiverInit[]" }, + { + "name": "dev.onvoid.webrtc.RTCSctpTransport[]" + }, + { + "name": "dev.onvoid.webrtc.RTCSctpTransportState[]" + }, { "name": "dev.onvoid.webrtc.RTCSdpType[]" }, diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/RTCSctpTransportTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/RTCSctpTransportTests.java new file mode 100644 index 00000000..2abf4d38 --- /dev/null +++ b/webrtc/src/test/java/dev/onvoid/webrtc/RTCSctpTransportTests.java @@ -0,0 +1,137 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +/** + * Tests the SCTP transport that carries the data channels of a connection. + */ +@Execution(ExecutionMode.SAME_THREAD) +class RTCSctpTransportTests extends TestBase { + + @Test + void noneWithoutDataChannels() { + RTCPeerConnection connection = factory.createPeerConnection(new RTCConfiguration(), candidate -> { }); + + try { + assertNull(connection.getSctpTransport()); + } + finally { + connection.close(); + } + } + + @Test + void unconnectedTransport() throws Exception { + RTCPeerConnection connection = factory.createPeerConnection(new RTCConfiguration(), candidate -> { }); + RTCDataChannel channel = connection.createDataChannel("data", new RTCDataChannelInit()); + + try { + // Not before the offer is applied. + assertNull(connection.getSctpTransport()); + + TestCreateDescObserver offer = new TestCreateDescObserver(); + connection.createOffer(new RTCOfferOptions(), offer); + + TestSetDescObserver applied = new TestSetDescObserver(); + connection.setLocalDescription(offer.get(5, TimeUnit.SECONDS), applied); + applied.get(5, TimeUnit.SECONDS); + + RTCSctpTransport transport = connection.getSctpTransport(); + + try { + assertNotNull(transport); + RTCSctpTransportState state = transport.getState(); + + // The association starts once the offer is applied. + assertTrue(state == RTCSctpTransportState.NEW || state == RTCSctpTransportState.CONNECTING, + "state: " + state); + assertNull(transport.getMaxChannels()); + } + finally { + transport.dispose(); + } + } + finally { + channel.dispose(); + connection.close(); + } + } + + @Test + void connectedTransport() throws Exception { + try (TestDataChannelPair pair = new TestDataChannelPair(factory)) { + pair.connect(); + + RTCSctpTransport transport = pair.getCaller().getSctpTransport(); + + try { + assertEquals(RTCSctpTransportState.CONNECTED, transport.getState()); + + // What both WebRTC peers announce in their descriptions. + assertEquals(262144.0, transport.getMaxMessageSize()); + assertTrue(transport.getMaxChannels() > 0, "channels: " + transport.getMaxChannels()); + + RTCDtlsTransport dtls = transport.getTransport(); + + try { + assertEquals(RTCDtlsTransportState.CONNECTED, dtls.getState()); + } + finally { + dtls.dispose(); + } + } + finally { + transport.dispose(); + } + } + } + + @Test + void instancesDisposeIndependently() throws Exception { + try (TestDataChannelPair pair = new TestDataChannelPair(factory)) { + pair.connect(); + + RTCSctpTransport first = pair.getCaller().getSctpTransport(); + RTCSctpTransport second = pair.getCaller().getSctpTransport(); + + assertNotSame(first, second); + assertEquals(first, second); + assertEquals(first.hashCode(), second.hashCode()); + + first.dispose(); + + assertThrows(IllegalStateException.class, first::getState); + assertEquals(RTCSctpTransportState.CONNECTED, second.getState()); + + second.dispose(); + } + } + +} diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/TestDataChannelPair.java b/webrtc/src/test/java/dev/onvoid/webrtc/TestDataChannelPair.java index c5afa1e0..444fa395 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/TestDataChannelPair.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/TestDataChannelPair.java @@ -51,6 +51,13 @@ void connect() throws Exception { assertTrue(open.await(5, TimeUnit.SECONDS), "Data channels did not open"); } + /** + * Returns the connection of the sender, which the pair closes. + */ + RTCPeerConnection getCaller() { + return caller; + } + private static void setDescription(RTCPeerConnection peer, RTCSessionDescription description, boolean local) throws Exception { TestSetDescObserver observer = new TestSetDescObserver();