| 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 #ifndef WEBRTC_EXAMPLES_ANDROID_MEDIA_DEMO_JNI_JNI_HELPERS_H_ | |
| 12 #define WEBRTC_EXAMPLES_ANDROID_MEDIA_DEMO_JNI_JNI_HELPERS_H_ | |
| 13 | |
| 14 // TODO(henrike): this file contains duplication with regards to | |
| 15 // talk/app/webrtc/java/jni/peerconnection_jni.cc. When/if code can be shared | |
| 16 // between trunk/talk and trunk/webrtc remove the duplication. | |
| 17 | |
| 18 #include <android/log.h> | |
| 19 #include <jni.h> | |
| 20 | |
| 21 #include <assert.h> | |
| 22 #include <map> | |
| 23 #include <string> | |
| 24 | |
| 25 #define TAG "WEBRTC-NATIVE" | |
| 26 | |
| 27 // Abort the process if |x| is false, emitting |msg| to logcat. | |
| 28 #define CHECK(x, msg) \ | |
| 29 if (x) { \ | |
| 30 } else { \ | |
| 31 __android_log_print(ANDROID_LOG_ERROR, TAG, "%s:%d: %s", __FILE__, \ | |
| 32 __LINE__, msg); \ | |
| 33 assert(false); \ | |
| 34 } | |
| 35 | |
| 36 // Abort the process if |jni| has a Java exception pending, emitting |msg| to | |
| 37 // logcat. | |
| 38 #define CHECK_JNI_EXCEPTION(jni, msg) \ | |
| 39 if (0) { \ | |
| 40 } else { \ | |
| 41 if (jni->ExceptionCheck()) { \ | |
| 42 jni->ExceptionDescribe(); \ | |
| 43 jni->ExceptionClear(); \ | |
| 44 CHECK(0, msg); \ | |
| 45 } \ | |
| 46 } | |
| 47 | |
| 48 // JNIEnv-helper methods that CHECK success: no Java exception thrown and found | |
| 49 // object/class/method/field is non-null. | |
| 50 jmethodID GetMethodID(JNIEnv* jni, jclass c, const std::string& name, | |
| 51 const char* signature); | |
| 52 | |
| 53 // Return a |jlong| that will automatically convert back to |ptr| when assigned | |
| 54 // to a |uint64_t| | |
| 55 jlong jlongFromPointer(void* ptr); | |
| 56 | |
| 57 // Given a (UTF-16) jstring return a new UTF-8 native string. | |
| 58 std::string JavaToStdString(JNIEnv* jni, const jstring& j_string); | |
| 59 | |
| 60 // Android's FindClass() is trickier than usual because the app-specific | |
| 61 // ClassLoader is not consulted when there is no app-specific frame on the | |
| 62 // stack. Consequently, we only look up classes once in JNI_OnLoad. | |
| 63 // http://developer.android.com/training/articles/perf-jni.html#faq_FindClass | |
| 64 class ClassReferenceHolder { | |
| 65 public: | |
| 66 ClassReferenceHolder(JNIEnv* jni, const char** classes, int size); | |
| 67 ~ClassReferenceHolder(); | |
| 68 | |
| 69 void FreeReferences(JNIEnv* jni); | |
| 70 | |
| 71 jclass GetClass(const std::string& name); | |
| 72 | |
| 73 private: | |
| 74 void LoadClass(JNIEnv* jni, const std::string& name); | |
| 75 | |
| 76 std::map<std::string, jclass> classes_; | |
| 77 }; | |
| 78 | |
| 79 #endif // WEBRTC_EXAMPLES_ANDROID_MEDIA_DEMO_JNI_JNI_HELPERS_H_ | |
| OLD | NEW |