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