1 /*
2 * Copyright 2015 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 // This file contain convenience functions and classes for JNI.
12 // Before using any of the methods, InitGlobalJniVariables must be called.
13
14 #ifndef SDK_ANDROID_SRC_JNI_JNI_HELPERS_H_
15 #define SDK_ANDROID_SRC_JNI_JNI_HELPERS_H_
16
17 #include <jni.h>
18 #include <string>
19
20 #include "sdk/android/native_api/jni/java_types.h"
21 #include "sdk/android/native_api/jni/scoped_java_ref.h"
22 #include "sdk/android/src/jni/jvm.h"
23
24 // Convenience macro defining JNI-accessible methods in the org.webrtc package.
25 // Eliminates unnecessary boilerplate and line-wraps, reducing visual clutter.
26 #if defined(WEBRTC_ARCH_X86)
27 // Dalvik JIT generated code doesn't guarantee 16-byte stack alignment on
28 // x86 - use force_align_arg_pointer to realign the stack at the JNI
29 // boundary. crbug.com/655248
30 #define JNI_FUNCTION_DECLARATION(rettype, name, ...) \
31 __attribute__((force_align_arg_pointer)) extern "C" JNIEXPORT rettype \
32 JNICALL Java_org_webrtc_##name(__VA_ARGS__)
33 #else
34 #define JNI_FUNCTION_DECLARATION(rettype, name, ...) \
35 extern "C" JNIEXPORT rettype JNICALL Java_org_webrtc_##name(__VA_ARGS__)
36 #endif
37
38 namespace webrtc {
39 namespace jni {
40
41 // TODO(sakal): Remove once clients have migrated.
42 using ::webrtc::JavaToStdMapStrings;
43
44 // Deprecated, use NativeToJavaPointer.
jlongFromPointer(void * ptr)45 inline long jlongFromPointer(void* ptr) {
46 return NativeToJavaPointer(ptr);
47 }
48
49 ScopedJavaLocalRef<jobject> NewDirectByteBuffer(JNIEnv* env,
50 void* address,
51 jlong capacity);
52
53 jobject NewGlobalRef(JNIEnv* jni, jobject o);
54
55 void DeleteGlobalRef(JNIEnv* jni, jobject o);
56
57 // Scope Java local references to the lifetime of this object. Use in all C++
58 // callbacks (i.e. entry points that don't originate in a Java callstack
59 // through a "native" method call).
60 class ScopedLocalRefFrame {
61 public:
62 explicit ScopedLocalRefFrame(JNIEnv* jni);
63 ~ScopedLocalRefFrame();
64
65 private:
66 JNIEnv* jni_;
67 };
68
69 } // namespace jni
70 } // namespace webrtc
71
72 // TODO(magjed): Remove once external clients are updated.
73 namespace webrtc_jni {
74
75 using webrtc::AttachCurrentThreadIfNeeded;
76 using webrtc::jni::InitGlobalJniVariables;
77
78 } // namespace webrtc_jni
79
80 #endif // SDK_ANDROID_SRC_JNI_JNI_HELPERS_H_
81