OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 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 #include "webrtc/api/java/jni/classreferenceholder.h" | |
11 | |
12 #include "webrtc/api/java/jni/jni_helpers.h" | |
13 | |
14 namespace webrtc_jni { | |
15 | |
16 // ClassReferenceHolder holds global reference to Java classes in app/webrtc. | |
17 class ClassReferenceHolder { | |
18 public: | |
19 explicit ClassReferenceHolder(JNIEnv* jni); | |
20 ~ClassReferenceHolder(); | |
21 | |
22 void FreeReferences(JNIEnv* jni); | |
23 jclass GetClass(const std::string& name); | |
24 | |
25 private: | |
26 void LoadClass(JNIEnv* jni, const std::string& name); | |
27 | |
28 std::map<std::string, jclass> classes_; | |
29 }; | |
30 | |
31 // Allocated in LoadGlobalClassReferenceHolder(), | |
32 // freed in FreeGlobalClassReferenceHolder(). | |
33 static ClassReferenceHolder* g_class_reference_holder = nullptr; | |
34 | |
35 void LoadGlobalClassReferenceHolder() { | |
36 RTC_CHECK(g_class_reference_holder == nullptr); | |
37 g_class_reference_holder = new ClassReferenceHolder(GetEnv()); | |
38 } | |
39 | |
40 void FreeGlobalClassReferenceHolder() { | |
41 g_class_reference_holder->FreeReferences(AttachCurrentThreadIfNeeded()); | |
42 delete g_class_reference_holder; | |
43 g_class_reference_holder = nullptr; | |
44 } | |
45 | |
46 ClassReferenceHolder::ClassReferenceHolder(JNIEnv* jni) { | |
47 LoadClass(jni, "android/graphics/SurfaceTexture"); | |
48 LoadClass(jni, "java/nio/ByteBuffer"); | |
49 LoadClass(jni, "java/util/ArrayList"); | |
50 LoadClass(jni, "org/webrtc/AudioTrack"); | |
51 LoadClass(jni, "org/webrtc/Camera1Enumerator"); | |
52 LoadClass(jni, "org/webrtc/Camera2Enumerator"); | |
53 LoadClass(jni, "org/webrtc/CameraEnumerationAndroid"); | |
54 LoadClass(jni, "org/webrtc/DataChannel"); | |
55 LoadClass(jni, "org/webrtc/DataChannel$Buffer"); | |
56 LoadClass(jni, "org/webrtc/DataChannel$Init"); | |
57 LoadClass(jni, "org/webrtc/DataChannel$State"); | |
58 LoadClass(jni, "org/webrtc/EglBase"); | |
59 LoadClass(jni, "org/webrtc/EglBase$Context"); | |
60 LoadClass(jni, "org/webrtc/EglBase14$Context"); | |
61 LoadClass(jni, "org/webrtc/IceCandidate"); | |
62 LoadClass(jni, "org/webrtc/MediaCodecVideoEncoder"); | |
63 LoadClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo"); | |
64 LoadClass(jni, "org/webrtc/MediaCodecVideoEncoder$VideoCodecType"); | |
65 LoadClass(jni, "org/webrtc/MediaCodecVideoDecoder"); | |
66 LoadClass(jni, "org/webrtc/MediaCodecVideoDecoder$DecodedTextureBuffer"); | |
67 LoadClass(jni, "org/webrtc/MediaCodecVideoDecoder$DecodedOutputBuffer"); | |
68 LoadClass(jni, "org/webrtc/MediaCodecVideoDecoder$VideoCodecType"); | |
69 LoadClass(jni, "org/webrtc/MediaSource$State"); | |
70 LoadClass(jni, "org/webrtc/MediaStream"); | |
71 LoadClass(jni, "org/webrtc/MediaStreamTrack$State"); | |
72 LoadClass(jni, "org/webrtc/Metrics"); | |
73 LoadClass(jni, "org/webrtc/Metrics$HistogramInfo"); | |
74 LoadClass(jni, "org/webrtc/NetworkMonitor"); | |
75 LoadClass(jni, "org/webrtc/NetworkMonitorAutoDetect$ConnectionType"); | |
76 LoadClass(jni, "org/webrtc/NetworkMonitorAutoDetect$IPAddress"); | |
77 LoadClass(jni, "org/webrtc/NetworkMonitorAutoDetect$NetworkInformation"); | |
78 LoadClass(jni, "org/webrtc/PeerConnectionFactory"); | |
79 LoadClass(jni, "org/webrtc/PeerConnection$BundlePolicy"); | |
80 LoadClass(jni, "org/webrtc/PeerConnection$ContinualGatheringPolicy"); | |
81 LoadClass(jni, "org/webrtc/PeerConnection$RtcpMuxPolicy"); | |
82 LoadClass(jni, "org/webrtc/PeerConnection$IceConnectionState"); | |
83 LoadClass(jni, "org/webrtc/PeerConnection$IceGatheringState"); | |
84 LoadClass(jni, "org/webrtc/PeerConnection$IceTransportsType"); | |
85 LoadClass(jni, "org/webrtc/PeerConnection$TcpCandidatePolicy"); | |
86 LoadClass(jni, "org/webrtc/PeerConnection$CandidateNetworkPolicy"); | |
87 LoadClass(jni, "org/webrtc/PeerConnection$KeyType"); | |
88 LoadClass(jni, "org/webrtc/PeerConnection$SignalingState"); | |
89 LoadClass(jni, "org/webrtc/RtpReceiver"); | |
90 LoadClass(jni, "org/webrtc/RtpSender"); | |
91 LoadClass(jni, "org/webrtc/SessionDescription"); | |
92 LoadClass(jni, "org/webrtc/SessionDescription$Type"); | |
93 LoadClass(jni, "org/webrtc/StatsReport"); | |
94 LoadClass(jni, "org/webrtc/StatsReport$Value"); | |
95 LoadClass(jni, "org/webrtc/SurfaceTextureHelper"); | |
96 LoadClass(jni, "org/webrtc/VideoCapturer"); | |
97 LoadClass(jni, "org/webrtc/VideoCapturer$NativeObserver"); | |
98 LoadClass(jni, "org/webrtc/VideoRenderer$I420Frame"); | |
99 LoadClass(jni, "org/webrtc/VideoTrack"); | |
100 } | |
101 | |
102 ClassReferenceHolder::~ClassReferenceHolder() { | |
103 RTC_CHECK(classes_.empty()) << "Must call FreeReferences() before dtor!"; | |
104 } | |
105 | |
106 void ClassReferenceHolder::FreeReferences(JNIEnv* jni) { | |
107 for (std::map<std::string, jclass>::const_iterator it = classes_.begin(); | |
108 it != classes_.end(); ++it) { | |
109 jni->DeleteGlobalRef(it->second); | |
110 } | |
111 classes_.clear(); | |
112 } | |
113 | |
114 jclass ClassReferenceHolder::GetClass(const std::string& name) { | |
115 std::map<std::string, jclass>::iterator it = classes_.find(name); | |
116 RTC_CHECK(it != classes_.end()) << "Unexpected GetClass() call for: " << name; | |
117 return it->second; | |
118 } | |
119 | |
120 void ClassReferenceHolder::LoadClass(JNIEnv* jni, const std::string& name) { | |
121 jclass localRef = jni->FindClass(name.c_str()); | |
122 CHECK_EXCEPTION(jni) << "error during FindClass: " << name; | |
123 RTC_CHECK(localRef) << name; | |
124 jclass globalRef = reinterpret_cast<jclass>(jni->NewGlobalRef(localRef)); | |
125 CHECK_EXCEPTION(jni) << "error during NewGlobalRef: " << name; | |
126 RTC_CHECK(globalRef) << name; | |
127 bool inserted = classes_.insert(std::make_pair(name, globalRef)).second; | |
128 RTC_CHECK(inserted) << "Duplicate class name: " << name; | |
129 } | |
130 | |
131 // Returns a global reference guaranteed to be valid for the lifetime of the | |
132 // process. | |
133 jclass FindClass(JNIEnv* jni, const char* name) { | |
134 return g_class_reference_holder->GetClass(name); | |
135 } | |
136 | |
137 } // namespace webrtc_jni | |
OLD | NEW |