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
17 changes: 17 additions & 0 deletions docs/guide/data/data-channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions webrtc-jni/src/main/cpp/include/JNI_RTCPeerConnection.h

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

53 changes: 53 additions & 0 deletions webrtc-jni/src/main/cpp/include/JNI_RTCSctpTransport.h

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

19 changes: 19 additions & 0 deletions webrtc-jni/src/main/cpp/src/JNI_RTCPeerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<webrtc::PeerConnectionInterface> pc(env, caller);
CHECK_HANDLEV(pc, nullptr);

webrtc::scoped_refptr<webrtc::SctpTransportInterface> 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)
{
Expand Down
111 changes: 111 additions & 0 deletions webrtc-jni/src/main/cpp/src/JNI_RTCSctpTransport.cpp
Original file line number Diff line number Diff line change
@@ -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<webrtc::SctpTransportInterface> transport(env, caller);
CHECK_HANDLEV(transport, nullptr);

webrtc::scoped_refptr<webrtc::DtlsTransportInterface> 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<webrtc::SctpTransportInterface> 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<webrtc::SctpTransportInterface> transport(env, caller);
CHECK_HANDLEV(transport, nullptr);

std::optional<double> 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<webrtc::SctpTransportInterface> transport(env, caller);
CHECK_HANDLEV(transport, nullptr);

std::optional<int> 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<webrtc::SctpTransportInterface>(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();
});
});
}
2 changes: 2 additions & 0 deletions webrtc-jni/src/main/cpp/src/WebRTCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ namespace jni
JavaEnums::add<webrtc::DesktopCapturer::Result>(env, PKG_DESKTOP"DesktopCapturer$Result");
JavaEnums::add<webrtc::DtlsTransportState>(env, PKG"RTCDtlsTransportState");
JavaEnums::add<webrtc::DtxStatus>(env, PKG"RTCDtxStatus");
JavaEnums::add<webrtc::SctpTransportState>(env, PKG"RTCSctpTransportState");
JavaEnums::add<webrtc::MediaSourceInterface::SourceState>(env, PKG_MEDIA"MediaSource$State");
JavaEnums::add<webrtc::MediaStreamTrackInterface::TrackState>(env, PKG_MEDIA"MediaStreamTrackState");
JavaEnums::add<webrtc::VideoTrackInterface::ContentHint>(env, PKG_MEDIA"video/VideoContentHint");
Expand Down Expand Up @@ -132,6 +133,7 @@ namespace jni
JavaFactories::add<webrtc::RtpReceiverInterface>(env, PKG"RTCRtpReceiver");
JavaFactories::add<webrtc::RtpSenderInterface>(env, PKG"RTCRtpSender");
JavaFactories::add<webrtc::RtpTransceiverInterface>(env, PKG"RTCRtpTransceiver");
JavaFactories::add<webrtc::SctpTransportInterface>(env, PKG"RTCSctpTransport");

initializeClassLoader(env, PKG_INTERNAL"NativeClassLoader");
}
Expand Down
12 changes: 12 additions & 0 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCPeerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
105 changes: 105 additions & 0 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCSctpTransport.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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();
}

}
Loading
Loading