OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2013 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/examples/android/media_demo/jni/jni_helpers.h" | |
12 | |
13 #include <limits> | |
14 | |
15 jmethodID GetMethodID(JNIEnv* jni, jclass c, const std::string& name, | |
16 const char* signature) { | |
17 jmethodID m = jni->GetMethodID(c, name.c_str(), signature); | |
18 CHECK_JNI_EXCEPTION(jni, "error during GetMethodID"); | |
19 return m; | |
20 } | |
21 | |
22 jlong jlongFromPointer(void* ptr) { | |
23 CHECK(sizeof(intptr_t) <= sizeof(jlong), "Time to rethink the use of jlongs"); | |
24 // Going through intptr_t to be obvious about the definedness of the | |
25 // conversion from pointer to integral type. intptr_t to jlong is a standard | |
26 // widening by the COMPILE_ASSERT above. | |
27 jlong ret = reinterpret_cast<intptr_t>(ptr); | |
28 CHECK(reinterpret_cast<void*>(ret) == ptr, | |
29 "jlong does not convert back to pointer"); | |
30 return ret; | |
31 } | |
32 | |
33 // Given a (UTF-16) jstring return a new UTF-8 native string. | |
34 std::string JavaToStdString(JNIEnv* jni, const jstring& j_string) { | |
35 const char* chars = jni->GetStringUTFChars(j_string, NULL); | |
36 CHECK_JNI_EXCEPTION(jni, "Error during GetStringUTFChars"); | |
37 std::string str(chars, jni->GetStringUTFLength(j_string)); | |
38 CHECK_JNI_EXCEPTION(jni, "Error during GetStringUTFLength"); | |
39 jni->ReleaseStringUTFChars(j_string, chars); | |
40 CHECK_JNI_EXCEPTION(jni, "Error during ReleaseStringUTFChars"); | |
41 return str; | |
42 } | |
43 | |
44 ClassReferenceHolder::ClassReferenceHolder(JNIEnv* jni, const char** classes, | |
45 int size) { | |
46 for (int i = 0; i < size; ++i) { | |
47 LoadClass(jni, classes[i]); | |
48 } | |
49 } | |
50 ClassReferenceHolder::~ClassReferenceHolder() { | |
51 CHECK(classes_.empty(), "Must call FreeReferences() before dtor!"); | |
52 } | |
53 | |
54 void ClassReferenceHolder::FreeReferences(JNIEnv* jni) { | |
55 for (std::map<std::string, jclass>::const_iterator it = classes_.begin(); | |
56 it != classes_.end(); ++it) { | |
57 jni->DeleteGlobalRef(it->second); | |
58 } | |
59 classes_.clear(); | |
60 } | |
61 | |
62 jclass ClassReferenceHolder::GetClass(const std::string& name) { | |
63 std::map<std::string, jclass>::iterator it = classes_.find(name); | |
64 CHECK(it != classes_.end(), "Could not find class"); | |
65 return it->second; | |
66 } | |
67 | |
68 void ClassReferenceHolder::LoadClass(JNIEnv* jni, const std::string& name) { | |
69 jclass localRef = jni->FindClass(name.c_str()); | |
70 CHECK_JNI_EXCEPTION(jni, "Could not load class"); | |
71 CHECK(localRef, name.c_str()); | |
72 jclass globalRef = reinterpret_cast<jclass>(jni->NewGlobalRef(localRef)); | |
73 CHECK_JNI_EXCEPTION(jni, "error during NewGlobalRef"); | |
74 CHECK(globalRef, name.c_str()); | |
75 bool inserted = classes_.insert(std::make_pair(name, globalRef)).second; | |
76 CHECK(inserted, "Duplicate class name"); | |
77 } | |
OLD | NEW |