Skip to content

Commit c5b04ce

Browse files
authored
feat: read the SCTP transport of a peer connection (#608)
1 parent d88f95c commit c5b04ce

13 files changed

Lines changed: 546 additions & 0 deletions

File tree

‎docs/guide/data/data-channels.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,23 @@ RTCDataChannelState state = dataChannel.getState();
309309
long bufferedAmount = dataChannel.getBufferedAmount();
310310
```
311311

312+
### Message Size and Channel Limits
313+
314+
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:
315+
316+
```java
317+
RTCSctpTransport transport = peerConnection.getSctpTransport();
318+
319+
if (transport != null) {
320+
Double maxMessageSize = transport.getMaxMessageSize(); // in bytes, null while unknown
321+
Integer maxChannels = transport.getMaxChannels(); // null until connected
322+
323+
transport.dispose();
324+
}
325+
```
326+
327+
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.
328+
312329
## Closing and Cleanup
313330

314331
When you're done with a data channel, you should properly clean it up:

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

Lines changed: 8 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/JNI_RTCSctpTransport.h‎

Lines changed: 53 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/src/JNI_RTCPeerConnection.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,25 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_getStats__Ldev_o
864864
}
865865
}
866866

867+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_getSctpTransport
868+
(JNIEnv * env, jobject caller)
869+
{
870+
return jni::CallFromJava(env, [&]() -> jobject {
871+
jni::NativeLease<webrtc::PeerConnectionInterface> pc(env, caller);
872+
CHECK_HANDLEV(pc, nullptr);
873+
874+
webrtc::scoped_refptr<webrtc::SctpTransportInterface> transport = pc->GetSctpTransport();
875+
876+
if (!transport) {
877+
return nullptr;
878+
}
879+
880+
// The Java object holds a reference until it is disposed, since the
881+
// connection drops the transport once it closes.
882+
return jni::JavaFactories::createOwning(env, std::move(transport)).release();
883+
});
884+
}
885+
867886
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_restartIce
868887
(JNIEnv * env, jobject caller)
869888
{
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "JNI_RTCSctpTransport.h"
18+
#include "JavaBoundary.h"
19+
#include "NativeHandle.h"
20+
#include "JavaEnums.h"
21+
#include "JavaFactories.h"
22+
#include "JavaPrimitive.h"
23+
#include "JavaRef.h"
24+
#include "JavaUtils.h"
25+
26+
#include "api/sctp_transport_interface.h"
27+
28+
// The transport has no proxy, and most of its methods must run on the network
29+
// thread. Information() is the exception: it calls over to that thread itself,
30+
// so everything here goes through it.
31+
32+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getTransport
33+
(JNIEnv * env, jobject caller)
34+
{
35+
return jni::CallFromJava(env, [&]() -> jobject {
36+
jni::NativeLease<webrtc::SctpTransportInterface> transport(env, caller);
37+
CHECK_HANDLEV(transport, nullptr);
38+
39+
webrtc::scoped_refptr<webrtc::DtlsTransportInterface> dtlsTransport = transport->Information().dtls_transport();
40+
41+
if (!dtlsTransport) {
42+
return nullptr;
43+
}
44+
45+
return jni::JavaFactories::createOwning(env, std::move(dtlsTransport)).release();
46+
});
47+
}
48+
49+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getState
50+
(JNIEnv * env, jobject caller)
51+
{
52+
return jni::CallFromJava(env, [&]() -> jobject {
53+
jni::NativeLease<webrtc::SctpTransportInterface> transport(env, caller);
54+
CHECK_HANDLEV(transport, nullptr);
55+
56+
return jni::JavaEnums::toJava(env, transport->Information().state()).release();
57+
});
58+
}
59+
60+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getMaxMessageSize
61+
(JNIEnv * env, jobject caller)
62+
{
63+
return jni::CallFromJava(env, [&]() -> jobject {
64+
jni::NativeLease<webrtc::SctpTransportInterface> transport(env, caller);
65+
CHECK_HANDLEV(transport, nullptr);
66+
67+
std::optional<double> size = transport->Information().MaxMessageSize();
68+
69+
if (!size) {
70+
return nullptr;
71+
}
72+
73+
return jni::Double::create(env, *size).release();
74+
});
75+
}
76+
77+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_getMaxChannels
78+
(JNIEnv * env, jobject caller)
79+
{
80+
return jni::CallFromJava(env, [&]() -> jobject {
81+
jni::NativeLease<webrtc::SctpTransportInterface> transport(env, caller);
82+
CHECK_HANDLEV(transport, nullptr);
83+
84+
std::optional<int> channels = transport->Information().MaxChannels();
85+
86+
if (!channels) {
87+
return nullptr;
88+
}
89+
90+
return jni::Integer::create(env, *channels).release();
91+
});
92+
}
93+
94+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCSctpTransport_dispose
95+
(JNIEnv * env, jobject caller)
96+
{
97+
jni::CallFromJava(env, [&]() {
98+
webrtc::SctpTransportInterface * transport = jni::TakeHandle<webrtc::SctpTransportInterface>(env, caller);
99+
100+
if (transport == nullptr) {
101+
return;
102+
}
103+
104+
// Runs once no other thread uses the transport through this object any
105+
// more. The connection and other Java objects keep their own
106+
// references, so this one need not be the last.
107+
jni::ReleaseHandle(env, transport, [transport](bool) {
108+
transport->Release();
109+
});
110+
});
111+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ namespace jni
8787
JavaEnums::add<webrtc::DesktopCapturer::Result>(env, PKG_DESKTOP"DesktopCapturer$Result");
8888
JavaEnums::add<webrtc::DtlsTransportState>(env, PKG"RTCDtlsTransportState");
8989
JavaEnums::add<webrtc::DtxStatus>(env, PKG"RTCDtxStatus");
90+
JavaEnums::add<webrtc::SctpTransportState>(env, PKG"RTCSctpTransportState");
9091
JavaEnums::add<webrtc::MediaSourceInterface::SourceState>(env, PKG_MEDIA"MediaSource$State");
9192
JavaEnums::add<webrtc::MediaStreamTrackInterface::TrackState>(env, PKG_MEDIA"MediaStreamTrackState");
9293
JavaEnums::add<webrtc::VideoTrackInterface::ContentHint>(env, PKG_MEDIA"video/VideoContentHint");
@@ -132,6 +133,7 @@ namespace jni
132133
JavaFactories::add<webrtc::RtpReceiverInterface>(env, PKG"RTCRtpReceiver");
133134
JavaFactories::add<webrtc::RtpSenderInterface>(env, PKG"RTCRtpSender");
134135
JavaFactories::add<webrtc::RtpTransceiverInterface>(env, PKG"RTCRtpTransceiver");
136+
JavaFactories::add<webrtc::SctpTransportInterface>(env, PKG"RTCSctpTransport");
135137

136138
initializeClassLoader(env, PKG_INTERNAL"NativeClassLoader");
137139
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,18 @@ public native void getStats(RTCRtpSender sender,
462462
*/
463463
public native void restartIce();
464464

465+
/**
466+
* Returns the SCTP transport that carries the data channels of this
467+
* connection. There is one only once a session description with data
468+
* channels has been applied, e.g. after {@link #setLocalDescription}
469+
* of an offer made with a data channel. The instance is the caller's to
470+
* dispose.
471+
*
472+
* @return The SCTP transport, or {@code null} if the connection has
473+
* none.
474+
*/
475+
public native RTCSctpTransport getSctpTransport();
476+
465477
/**
466478
* Closes the peer connection, terminates all media and releases any used
467479
* resources.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.onvoid.webrtc;
18+
19+
import dev.onvoid.webrtc.internal.DisposableNativeObject;
20+
21+
/**
22+
* The SCTP transport that carries the data channels of a peer connection,
23+
* as {@link RTCPeerConnection#getSctpTransport()} returns it. It tells how
24+
* large a message the remote peer takes and how many data channels can be
25+
* open at once.
26+
* <p>
27+
* Each call of {@link RTCPeerConnection#getSctpTransport()} returns a new
28+
* instance, which holds a reference to the transport until it is disposed.
29+
*
30+
* @author Alex Andres
31+
*/
32+
public class RTCSctpTransport extends DisposableNativeObject {
33+
34+
/**
35+
* Used by the native api.
36+
*/
37+
private RTCSctpTransport() {
38+
39+
}
40+
41+
/**
42+
* Returns the DTLS transport over which the SCTP packets are sent and
43+
* received. The instance is the caller's to dispose.
44+
*
45+
* @return The DTLS transport, or {@code null} once the transport is
46+
* closed.
47+
*/
48+
public native RTCDtlsTransport getTransport();
49+
50+
/**
51+
* @return The current state of the transport.
52+
*/
53+
public native RTCSctpTransportState getState();
54+
55+
/**
56+
* Returns the largest message in bytes that can be sent with {@link
57+
* RTCDataChannel#send(RTCDataChannelBuffer)}, as the two peers agreed on
58+
* in their session descriptions. A message larger than that is refused.
59+
*
60+
* @return The largest message size in bytes, or {@code null} while it is
61+
* not known yet.
62+
*/
63+
public native Double getMaxMessageSize();
64+
65+
/**
66+
* Returns how many data channels can be open at the same time, as the
67+
* SCTP association agreed on.
68+
*
69+
* @return The number of channels, or {@code null} before the transport
70+
* is {@link RTCSctpTransportState#CONNECTED}.
71+
*/
72+
public native Integer getMaxChannels();
73+
74+
/**
75+
* Releases the reference this instance holds. The transport itself stays
76+
* with its peer connection, as do other instances of it.
77+
*/
78+
@Override
79+
public native void dispose();
80+
81+
/**
82+
* Two instances are equal if they refer to the same transport, e.g. when
83+
* obtained from two calls of {@link RTCPeerConnection#getSctpTransport()}.
84+
* A disposed instance is never equal to anything but itself.
85+
*/
86+
@Override
87+
public boolean equals(Object obj) {
88+
if (this == obj) {
89+
return true;
90+
}
91+
if (!(obj instanceof RTCSctpTransport)) {
92+
return false;
93+
}
94+
95+
long handle = getNativeHandle();
96+
97+
return handle != 0 && handle == ((RTCSctpTransport) obj).getNativeHandle();
98+
}
99+
100+
@Override
101+
public int hashCode() {
102+
return nativeHashCode();
103+
}
104+
105+
}

0 commit comments

Comments
 (0)