Skip to content

Commit 00b0daf

Browse files
committed
fix: apply data channel priority and reject a malformed certificate
RTCDataChannelInit.priority was looked up natively but never copied into the native init, so every data channel was created with low priority and announced as such to the remote peer. Copy it, leaving it unset when null, and add RTCDataChannel.getPriority(), which rounds a priority the remote peer announced between two levels up to the next. A certificate in RTCConfiguration.certificates that did not parse hit an RTC_CHECK and aborted the JVM. Throw a RuntimeException instead, and a NullPointerException for a null entry, from both createPeerConnection and setConfiguration, which now catch conversion errors rather than letting a C++ exception leave the JNI call.
1 parent 4d205e9 commit 00b0daf

11 files changed

Lines changed: 159 additions & 12 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ config.id = -1; // Let WebRTC assign an ID
6262
// Configure sub-protocol (default: null)
6363
config.protocol = "my-protocol"; // Optional sub-protocol name
6464

65-
// Configure priority (default: LOW)
66-
config.priority = RTCPriorityType.LOW;
65+
// Configure priority (default: LOW), announced to the remote peer
66+
config.priority = RTCPriorityType.HIGH;
6767

6868
// Create the data channel with this configuration
6969
RTCDataChannel dataChannel = peerConnection.createDataChannel("myChannel", config);
7070
```
7171

72+
`getPriority()` returns a channel's priority; a channel the remote peer opened has the priority it announced. WebRTC only uses the priority to share the connection between channels when SCTP message interleaving is on, which needs the field trial `WebRTC-DataChannelMessageInterleaving/Enabled/` on both peers (see [Field Trials](/guide/advanced/field-trials)). Without it, channels take turns regardless of priority.
73+
7274
## Handling Data Channel Events
7375

7476
To receive events from a data channel, you need to implement the `RTCDataChannelObserver` interface and register it with the data channel:

‎webrtc-jni/src/main/cpp/include/JNI_RTCDataChannel.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/src/JNI_PeerConnectionFactory.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,16 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_PeerConnectionFactory_createPee
355355
webrtc::PeerConnectionFactoryInterface * factory = GetHandle<webrtc::PeerConnectionFactoryInterface>(env, caller);
356356
CHECK_HANDLEV(factory, nullptr);
357357

358-
webrtc::PeerConnectionInterface::RTCConfiguration configuration = jni::RTCConfiguration::toNative(env, jni::JavaLocalRef<jobject>(env, jConfig));
358+
webrtc::PeerConnectionInterface::RTCConfiguration configuration;
359+
360+
try {
361+
configuration = jni::RTCConfiguration::toNative(env, jni::JavaLocalRef<jobject>(env, jConfig));
362+
}
363+
catch (...) {
364+
ThrowCxxJavaException(env);
365+
return nullptr;
366+
}
367+
359368
webrtc::PeerConnectionObserver * observer = new jni::PeerConnectionObserver(env, jni::JavaGlobalRef<jobject>(env, jobserver));
360369
webrtc::PeerConnectionDependencies dependencies(observer);
361370

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "api/data_channel_interface.h"
2828
#include "rtc_base/logging.h"
2929

30+
#include <initializer_list>
3031
#include <memory>
3132

3233
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCDataChannel_registerObserver
@@ -119,6 +120,27 @@ JNIEXPORT jboolean JNICALL Java_dev_onvoid_webrtc_RTCDataChannel_isNegotiated
119120
return static_cast<jboolean>(channel->negotiated());
120121
}
121122

123+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCDataChannel_getPriority
124+
(JNIEnv * env, jobject caller)
125+
{
126+
webrtc::DataChannelInterface * channel = GetHandle<webrtc::DataChannelInterface>(env, caller);
127+
CHECK_HANDLEV(channel, nullptr);
128+
129+
// The priority is a number on the wire; a remote peer may announce one
130+
// between the four levels, which is rounded up to the next level.
131+
const uint16_t value = channel->priority().value();
132+
webrtc::Priority priority = webrtc::Priority::kHigh;
133+
134+
for (auto level : { webrtc::Priority::kVeryLow, webrtc::Priority::kLow, webrtc::Priority::kMedium }) {
135+
if (value <= webrtc::PriorityValue(level).value()) {
136+
priority = level;
137+
break;
138+
}
139+
}
140+
141+
return jni::JavaEnums::toJava(env, priority).release();
142+
}
143+
122144
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_RTCDataChannel_getId
123145
(JNIEnv * env, jobject caller)
124146
{

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,17 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCPeerConnection_setConfiguration
487487
webrtc::PeerConnectionInterface * pc = GetHandle<webrtc::PeerConnectionInterface>(env, caller);
488488
CHECK_HANDLE(pc);
489489

490-
auto config = jni::RTCConfiguration::toNative(env, jni::JavaLocalRef<jobject>(env, jConfig));
490+
try {
491+
auto config = jni::RTCConfiguration::toNative(env, jni::JavaLocalRef<jobject>(env, jConfig));
491492

492-
webrtc::RTCError error = pc->SetConfiguration(config);
493+
webrtc::RTCError error = pc->SetConfiguration(config);
493494

494-
if (!error.ok()) {
495-
env->Throw(jni::JavaRuntimeException(env, jni::RTCErrorToString(error).c_str()));
495+
if (!error.ok()) {
496+
env->Throw(jni::JavaRuntimeException(env, jni::RTCErrorToString(error).c_str()));
497+
}
498+
}
499+
catch (...) {
500+
ThrowCxxJavaException(env);
496501
}
497502
}
498503

‎webrtc-jni/src/main/cpp/src/api/RTCConfiguration.cpp‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
#include "JavaEnums.h"
2525
#include "JavaIterable.h"
2626
#include "JavaList.h"
27+
#include "JavaNullPointerException.h"
2728
#include "JavaRef.h"
2829
#include "JavaObject.h"
2930
#include "JavaPrimitive.h"
31+
#include "JavaRuntimeException.h"
3032
#include "JavaString.h"
3133
#include "JavaUtils.h"
34+
#include "JavaWrappedException.h"
3235
#include "JNI_WebRTC.h"
3336

3437
namespace jni
@@ -187,13 +190,19 @@ namespace jni
187190
configuration.rtcp_mux_policy = JavaEnums::toNative<webrtc::PeerConnectionInterface::RtcpMuxPolicy>(env, mp);
188191

189192
for (auto & item : JavaIterable(env, cr)) {
190-
auto certificate = webrtc::RTCCertificate::FromPEM(jni::RTCCertificatePEM::toNative(env, item));
193+
if (item.get() == nullptr) {
194+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
195+
JavaNullPointerException(env, "RTCCertificatePEM must not be null")));
196+
}
191197

192-
RTC_CHECK(certificate != nullptr) << "Supplied certificate is malformed";
198+
auto certificate = webrtc::RTCCertificate::FromPEM(jni::RTCCertificatePEM::toNative(env, item));
193199

194-
if (certificate != nullptr) {
195-
configuration.certificates.push_back(certificate);
200+
if (certificate == nullptr) {
201+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
202+
JavaRuntimeException(env, "Supplied certificate is malformed")));
196203
}
204+
205+
configuration.certificates.push_back(certificate);
197206
}
198207

199208
if (pac.get() != nullptr) {

‎webrtc-jni/src/main/cpp/src/api/RTCDataChannelInit.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "api/RTCDataChannelInit.h"
1818
#include "JavaClasses.h"
19+
#include "JavaEnums.h"
1920
#include "JavaObject.h"
2021
#include "JavaString.h"
2122
#include "JNI_WebRTC.h"
@@ -38,6 +39,13 @@ namespace jni
3839
init.id = obj.getInt(javaClass->id);
3940
init.protocol = JavaString::toNative(env, obj.getString(javaClass->protocol));
4041

42+
// Left unset when null, which WebRTC treats as low priority.
43+
JavaLocalRef<jobject> priority = obj.getObject(javaClass->priority);
44+
45+
if (priority.get()) {
46+
init.priority = webrtc::PriorityValue(JavaEnums::toNative<webrtc::Priority>(env, priority));
47+
}
48+
4149
return init;
4250
}
4351

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ private RTCDataChannel() {
115115
*/
116116
public native boolean isNegotiated();
117117

118+
/**
119+
* Returns the priority of this RTCDataChannel, as set in {@link
120+
* RTCDataChannelInit#priority}. A channel that is announced by the remote
121+
* peer has the priority the remote peer gave it; a negotiated channel has
122+
* the one it was created with on each side.
123+
* A numeric priority that lies between two levels is reported as the
124+
* nearest level above it.
125+
*
126+
* @return The priority of this data channel.
127+
*/
128+
public native RTCPriorityType getPriority();
129+
118130
/**
119131
* Returns the ID for this RTCDataChannel. The value is initially {@code
120132
* null}, which is what will be returned if the ID was not provided at

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ public class RTCDataChannelInit {
6666
public String protocol;
6767

6868
/**
69-
* Priority of this channel.
69+
* Priority of this channel. It is announced to the remote peer when the
70+
* channel opens. WebRTC only uses it to share the SCTP transport between
71+
* channels when SCTP message interleaving is enabled with the field trial
72+
* "WebRTC-DataChannelMessageInterleaving/Enabled/" on both peers; otherwise
73+
* channels take turns regardless of priority. {@code null} means {@link
74+
* RTCPriorityType#LOW}.
7075
*/
7176
public RTCPriorityType priority = RTCPriorityType.LOW;
7277

‎webrtc/src/test/java/dev/onvoid/webrtc/RTCConfigurationFieldsTests.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,41 @@ void invalidConfigurationFailsReadably() {
151151
assertTrue(error.getMessage().matches("Create PeerConnection failed: [A-Z_]+ .+"), error.getMessage());
152152
}
153153

154+
@Test
155+
void malformedCertificateThrows() {
156+
// Used to abort the JVM through an RTC_CHECK.
157+
RTCConfiguration config = new RTCConfiguration();
158+
config.certificates.add(new RTCCertificatePEM("no key", "no certificate", 0));
159+
160+
RuntimeException error = assertThrows(RuntimeException.class,
161+
() -> factory.createPeerConnection(config, new Observer()));
162+
163+
assertEquals("Supplied certificate is malformed", error.getMessage());
164+
165+
config.certificates.set(0, null);
166+
167+
assertThrows(NullPointerException.class,
168+
() -> factory.createPeerConnection(config, new Observer()));
169+
}
170+
171+
@Test
172+
void setConfigurationRejectsMalformedCertificate() {
173+
RTCPeerConnection connection = factory.createPeerConnection(new RTCConfiguration(), new Observer());
174+
175+
try {
176+
RTCConfiguration config = connection.getConfiguration();
177+
config.certificates.add(new RTCCertificatePEM("no key", "no certificate", 0));
178+
179+
RuntimeException error = assertThrows(RuntimeException.class,
180+
() -> connection.setConfiguration(config));
181+
182+
assertEquals("Supplied certificate is malformed", error.getMessage());
183+
}
184+
finally {
185+
connection.close();
186+
}
187+
}
188+
154189
@Test
155190
void setConfigurationApplies() {
156191
RTCPeerConnection connection = factory.createPeerConnection(new RTCConfiguration(), new Observer());

0 commit comments

Comments
 (0)