aboutsummaryrefslogtreecommitdiff
path: root/webrtc/examples/android/media_demo/jni/jni_helpers.h
blob: 3d8ff481115e08c63d32ce424760f5a02f4d16e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_EXAMPLES_ANDROID_MEDIA_DEMO_JNI_JNI_HELPERS_H_
#define WEBRTC_EXAMPLES_ANDROID_MEDIA_DEMO_JNI_JNI_HELPERS_H_

// TODO(henrike): this file contains duplication with regards to
// talk/app/webrtc/java/jni/peerconnection_jni.cc. When/if code can be shared
// between trunk/talk and trunk/webrtc remove the duplication.

#include <android/log.h>
#include <jni.h>

#include <assert.h>
#include <map>
#include <string>

#define TAG "WEBRTC-NATIVE"

// Abort the process if |x| is false, emitting |msg| to logcat.
#define CHECK(x, msg)                                                  \
  if (x) {                                                             \
  } else {                                                             \
    __android_log_print(ANDROID_LOG_ERROR, TAG, "%s:%d: %s", __FILE__, \
                        __LINE__, msg);                                \
    assert(false);                                                     \
  }

// Abort the process if |jni| has a Java exception pending, emitting |msg| to
// logcat.
#define CHECK_JNI_EXCEPTION(jni, msg) \
  if (0) {                        \
  } else {                        \
    if (jni->ExceptionCheck()) {  \
      jni->ExceptionDescribe();   \
      jni->ExceptionClear();      \
      CHECK(0, msg);              \
    }                             \
  }

// JNIEnv-helper methods that CHECK success: no Java exception thrown and found
// object/class/method/field is non-null.
jmethodID GetMethodID(JNIEnv* jni, jclass c, const std::string& name,
                      const char* signature);

// Return a |jlong| that will automatically convert back to |ptr| when assigned
// to a |uint64_t|
jlong jlongFromPointer(void* ptr);

// Given a (UTF-16) jstring return a new UTF-8 native string.
std::string JavaToStdString(JNIEnv* jni, const jstring& j_string);

// Android's FindClass() is trickier than usual because the app-specific
// ClassLoader is not consulted when there is no app-specific frame on the
// stack.  Consequently, we only look up classes once in JNI_OnLoad.
// http://developer.android.com/training/articles/perf-jni.html#faq_FindClass
class ClassReferenceHolder {
 public:
  ClassReferenceHolder(JNIEnv* jni, const char** classes, int size);
  ~ClassReferenceHolder();

  void FreeReferences(JNIEnv* jni);

  jclass GetClass(const std::string& name);

 private:
  void LoadClass(JNIEnv* jni, const std::string& name);

  std::map<std::string, jclass> classes_;
};

#endif  // WEBRTC_EXAMPLES_ANDROID_MEDIA_DEMO_JNI_JNI_HELPERS_H_