blob: 8c30a0a3eca181cd16687cd246dc76ea9d5c04dd [file] [log] [blame]
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00001/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28#include "talk/app/webrtc/java/jni/classreferenceholder.h"
29
30#include "talk/app/webrtc/java/jni/jni_helpers.h"
31
32namespace webrtc_jni {
33
34// ClassReferenceHolder holds global reference to Java classes in app/webrtc.
35class ClassReferenceHolder {
36 public:
37 explicit ClassReferenceHolder(JNIEnv* jni);
38 ~ClassReferenceHolder();
39
40 void FreeReferences(JNIEnv* jni);
41 jclass GetClass(const std::string& name);
42
43 private:
44 void LoadClass(JNIEnv* jni, const std::string& name);
45
46 std::map<std::string, jclass> classes_;
47};
48
49// Allocated in LoadGlobalClassReferenceHolder(),
50// freed in FreeGlobalClassReferenceHolder().
51static ClassReferenceHolder* g_class_reference_holder = nullptr;
52
53void LoadGlobalClassReferenceHolder() {
54 CHECK(g_class_reference_holder == nullptr);
55 g_class_reference_holder = new ClassReferenceHolder(GetEnv());
56}
57
58void FreeGlobalClassReferenceHolder() {
59 g_class_reference_holder->FreeReferences(AttachCurrentThreadIfNeeded());
60 delete g_class_reference_holder;
61 g_class_reference_holder = nullptr;
62}
63
64ClassReferenceHolder::ClassReferenceHolder(JNIEnv* jni) {
65 LoadClass(jni, "java/nio/ByteBuffer");
66 LoadClass(jni, "org/webrtc/AudioTrack");
67 LoadClass(jni, "org/webrtc/DataChannel");
68 LoadClass(jni, "org/webrtc/DataChannel$Buffer");
69 LoadClass(jni, "org/webrtc/DataChannel$Init");
70 LoadClass(jni, "org/webrtc/DataChannel$State");
71 LoadClass(jni, "org/webrtc/IceCandidate");
72#if defined(ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
73 LoadClass(jni, "android/graphics/SurfaceTexture");
74 LoadClass(jni, "org/webrtc/VideoCapturerAndroid");
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000075 LoadClass(jni, "org/webrtc/VideoCapturerAndroid$NativeObserver");
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000076 LoadClass(jni, "org/webrtc/MediaCodecVideoEncoder");
77 LoadClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo");
78 LoadClass(jni, "org/webrtc/MediaCodecVideoDecoder");
79 LoadClass(jni, "org/webrtc/MediaCodecVideoDecoder$DecoderOutputBufferInfo");
80 jclass j_decoder_class = GetClass("org/webrtc/MediaCodecVideoDecoder");
81 jmethodID j_is_egl14_supported_method = jni->GetStaticMethodID(
82 j_decoder_class, "isEGL14Supported", "()Z");
83 bool is_egl14_supported = jni->CallStaticBooleanMethod(
84 j_decoder_class, j_is_egl14_supported_method);
85 CHECK_EXCEPTION(jni);
86 if (is_egl14_supported) {
87 LoadClass(jni, "android/opengl/EGLContext");
88 }
89#endif
90 LoadClass(jni, "org/webrtc/MediaSource$State");
91 LoadClass(jni, "org/webrtc/MediaStream");
92 LoadClass(jni, "org/webrtc/MediaStreamTrack$State");
93 LoadClass(jni, "org/webrtc/PeerConnection$IceConnectionState");
94 LoadClass(jni, "org/webrtc/PeerConnection$IceGatheringState");
95 LoadClass(jni, "org/webrtc/PeerConnection$SignalingState");
96 LoadClass(jni, "org/webrtc/SessionDescription");
97 LoadClass(jni, "org/webrtc/SessionDescription$Type");
98 LoadClass(jni, "org/webrtc/StatsReport");
99 LoadClass(jni, "org/webrtc/StatsReport$Value");
100 LoadClass(jni, "org/webrtc/VideoRenderer$I420Frame");
101 LoadClass(jni, "org/webrtc/VideoCapturer");
102 LoadClass(jni, "org/webrtc/VideoTrack");
103}
104
105ClassReferenceHolder::~ClassReferenceHolder() {
106 CHECK(classes_.empty()) << "Must call FreeReferences() before dtor!";
107}
108
109void ClassReferenceHolder::FreeReferences(JNIEnv* jni) {
110 for (std::map<std::string, jclass>::const_iterator it = classes_.begin();
111 it != classes_.end(); ++it) {
112 jni->DeleteGlobalRef(it->second);
113 }
114 classes_.clear();
115}
116
117jclass ClassReferenceHolder::GetClass(const std::string& name) {
118 std::map<std::string, jclass>::iterator it = classes_.find(name);
119 CHECK(it != classes_.end()) << "Unexpected GetClass() call for: " << name;
120 return it->second;
121}
122
123void ClassReferenceHolder::LoadClass(JNIEnv* jni, const std::string& name) {
124 jclass localRef = jni->FindClass(name.c_str());
125 CHECK_EXCEPTION(jni) << "error during FindClass: " << name;
126 CHECK(localRef) << name;
127 jclass globalRef = reinterpret_cast<jclass>(jni->NewGlobalRef(localRef));
128 CHECK_EXCEPTION(jni) << "error during NewGlobalRef: " << name;
129 CHECK(globalRef) << name;
130 bool inserted = classes_.insert(std::make_pair(name, globalRef)).second;
131 CHECK(inserted) << "Duplicate class name: " << name;
132}
133
134// Returns a global reference guaranteed to be valid for the lifetime of the
135// process.
136jclass FindClass(JNIEnv* jni, const char* name) {
137 return g_class_reference_holder->GetClass(name);
138}
139
140} // namespace webrtc_jni
141