OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/sdk/android/src/jni/ownedfactoryandthreads.h" | 11 #include <memory> |
12 | 12 |
13 #include "webrtc/rtc_base/logging.h" | 13 #include "webrtc/api/datachannelinterface.h" |
14 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" | |
15 #include "webrtc/sdk/android/src/jni/jni_helpers.h" | 14 #include "webrtc/sdk/android/src/jni/jni_helpers.h" |
| 15 #include "webrtc/sdk/android/src/jni/pc/datachannelobserver_jni.h" |
16 | 16 |
17 namespace webrtc_jni { | 17 namespace webrtc_jni { |
18 | 18 |
19 PeerConnectionFactoryInterface* factoryFromJava(jlong j_p) { | 19 static webrtc::DataChannelInterface* ExtractNativeDC(JNIEnv* jni, |
20 return reinterpret_cast<OwnedFactoryAndThreads*>(j_p)->factory(); | 20 jobject j_dc) { |
| 21 jfieldID native_dc_id = |
| 22 GetFieldID(jni, GetObjectClass(jni, j_dc), "nativeDataChannel", "J"); |
| 23 jlong j_d = GetLongField(jni, j_dc, native_dc_id); |
| 24 return reinterpret_cast<webrtc::DataChannelInterface*>(j_d); |
21 } | 25 } |
22 | 26 |
23 OwnedFactoryAndThreads::~OwnedFactoryAndThreads() { | 27 JOW(jlong, DataChannel_registerObserverNative) |
24 CHECK_RELEASE(factory_); | 28 (JNIEnv* jni, jobject j_dc, jobject j_observer) { |
25 if (network_monitor_factory_ != nullptr) { | 29 std::unique_ptr<DataChannelObserverJni> observer( |
26 rtc::NetworkMonitorFactory::ReleaseFactory(network_monitor_factory_); | 30 new DataChannelObserverJni(jni, j_observer)); |
27 } | 31 ExtractNativeDC(jni, j_dc)->RegisterObserver(observer.get()); |
| 32 return jlongFromPointer(observer.release()); |
28 } | 33 } |
29 | 34 |
30 void OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads() { | 35 JOW(void, DataChannel_unregisterObserverNative) |
31 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 36 (JNIEnv* jni, jobject j_dc, jlong native_observer) { |
32 ScopedLocalRefFrame local_ref_frame(jni); | 37 ExtractNativeDC(jni, j_dc)->UnregisterObserver(); |
33 jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory"); | 38 delete reinterpret_cast<DataChannelObserverJni*>(native_observer); |
34 jmethodID m = nullptr; | |
35 if (network_thread_->IsCurrent()) { | |
36 LOG(LS_INFO) << "Network thread JavaCallback"; | |
37 m = GetStaticMethodID(jni, j_factory_class, "onNetworkThreadReady", "()V"); | |
38 } | |
39 if (worker_thread_->IsCurrent()) { | |
40 LOG(LS_INFO) << "Worker thread JavaCallback"; | |
41 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V"); | |
42 } | |
43 if (signaling_thread_->IsCurrent()) { | |
44 LOG(LS_INFO) << "Signaling thread JavaCallback"; | |
45 m = GetStaticMethodID(jni, j_factory_class, "onSignalingThreadReady", | |
46 "()V"); | |
47 } | |
48 if (m != nullptr) { | |
49 jni->CallStaticVoidMethod(j_factory_class, m); | |
50 CHECK_EXCEPTION(jni) << "error during JavaCallback::CallStaticVoidMethod"; | |
51 } | |
52 } | 39 } |
53 | 40 |
54 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() { | 41 JOW(jstring, DataChannel_label)(JNIEnv* jni, jobject j_dc) { |
55 LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads."; | 42 return JavaStringFromStdString(jni, ExtractNativeDC(jni, j_dc)->label()); |
56 network_thread_->Invoke<void>(RTC_FROM_HERE, | 43 } |
57 [this] { JavaCallbackOnFactoryThreads(); }); | 44 |
58 worker_thread_->Invoke<void>(RTC_FROM_HERE, | 45 JOW(jint, DataChannel_id)(JNIEnv* jni, jobject j_dc) { |
59 [this] { JavaCallbackOnFactoryThreads(); }); | 46 int id = ExtractNativeDC(jni, j_dc)->id(); |
60 signaling_thread_->Invoke<void>(RTC_FROM_HERE, | 47 RTC_CHECK_LE(id, std::numeric_limits<int32_t>::max()) |
61 [this] { JavaCallbackOnFactoryThreads(); }); | 48 << "id overflowed jint!"; |
| 49 return static_cast<jint>(id); |
| 50 } |
| 51 |
| 52 JOW(jobject, DataChannel_state)(JNIEnv* jni, jobject j_dc) { |
| 53 return JavaEnumFromIndexAndClassName(jni, "DataChannel$State", |
| 54 ExtractNativeDC(jni, j_dc)->state()); |
| 55 } |
| 56 |
| 57 JOW(jlong, DataChannel_bufferedAmount)(JNIEnv* jni, jobject j_dc) { |
| 58 uint64_t buffered_amount = ExtractNativeDC(jni, j_dc)->buffered_amount(); |
| 59 RTC_CHECK_LE(buffered_amount, std::numeric_limits<int64_t>::max()) |
| 60 << "buffered_amount overflowed jlong!"; |
| 61 return static_cast<jlong>(buffered_amount); |
| 62 } |
| 63 |
| 64 JOW(void, DataChannel_close)(JNIEnv* jni, jobject j_dc) { |
| 65 ExtractNativeDC(jni, j_dc)->Close(); |
| 66 } |
| 67 |
| 68 JOW(jboolean, DataChannel_sendNative) |
| 69 (JNIEnv* jni, jobject j_dc, jbyteArray data, jboolean binary) { |
| 70 jbyte* bytes = jni->GetByteArrayElements(data, NULL); |
| 71 bool ret = ExtractNativeDC(jni, j_dc)->Send(webrtc::DataBuffer( |
| 72 rtc::CopyOnWriteBuffer(bytes, jni->GetArrayLength(data)), binary)); |
| 73 jni->ReleaseByteArrayElements(data, bytes, JNI_ABORT); |
| 74 return ret; |
| 75 } |
| 76 |
| 77 JOW(void, DataChannel_dispose)(JNIEnv* jni, jobject j_dc) { |
| 78 CHECK_RELEASE(ExtractNativeDC(jni, j_dc)); |
62 } | 79 } |
63 | 80 |
64 } // namespace webrtc_jni | 81 } // namespace webrtc_jni |
OLD | NEW |