OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/sdk/android/src/jni/pc/datachannelobserver_jni.h" | |
12 | |
13 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" | |
14 | |
15 namespace webrtc_jni { | |
16 | |
17 // Convenience, used since callbacks occur on the signaling thread, which may | |
18 // be a non-Java thread. | |
19 static JNIEnv* jni() { | |
20 return AttachCurrentThreadIfNeeded(); | |
21 } | |
22 | |
23 DataChannelObserverJni::DataChannelObserverJni(JNIEnv* jni, jobject j_observer) | |
24 : j_observer_global_(jni, j_observer), | |
25 j_observer_class_(jni, GetObjectClass(jni, j_observer)), | |
26 j_buffer_class_(jni, FindClass(jni, "org/webrtc/DataChannel$Buffer")), | |
27 j_on_buffered_amount_change_mid_(GetMethodID(jni, | |
28 *j_observer_class_, | |
29 "onBufferedAmountChange", | |
30 "(J)V")), | |
31 j_on_state_change_mid_( | |
32 GetMethodID(jni, *j_observer_class_, "onStateChange", "()V")), | |
33 j_on_message_mid_(GetMethodID(jni, | |
34 *j_observer_class_, | |
35 "onMessage", | |
36 "(Lorg/webrtc/DataChannel$Buffer;)V")), | |
37 j_buffer_ctor_(GetMethodID(jni, | |
38 *j_buffer_class_, | |
39 "<init>", | |
40 "(Ljava/nio/ByteBuffer;Z)V")) {} | |
41 | |
42 void DataChannelObserverJni::OnBufferedAmountChange(uint64_t previous_amount) { | |
43 ScopedLocalRefFrame local_ref_frame(jni()); | |
44 jni()->CallVoidMethod(*j_observer_global_, j_on_buffered_amount_change_mid_, | |
45 previous_amount); | |
46 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
47 } | |
48 | |
49 void DataChannelObserverJni::OnStateChange() { | |
50 ScopedLocalRefFrame local_ref_frame(jni()); | |
51 jni()->CallVoidMethod(*j_observer_global_, j_on_state_change_mid_); | |
52 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
53 } | |
54 | |
55 void DataChannelObserverJni::OnMessage(const webrtc::DataBuffer& buffer) { | |
56 ScopedLocalRefFrame local_ref_frame(jni()); | |
57 jobject byte_buffer = jni()->NewDirectByteBuffer( | |
58 const_cast<char*>(buffer.data.data<char>()), buffer.data.size()); | |
59 jobject j_buffer = jni()->NewObject(*j_buffer_class_, j_buffer_ctor_, | |
60 byte_buffer, buffer.binary); | |
61 jni()->CallVoidMethod(*j_observer_global_, j_on_message_mid_, j_buffer); | |
62 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
63 } | |
64 | |
65 } // namespace webrtc_jni | |
OLD | NEW |