|
| 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 | +} |
0 commit comments