| /* |
| * Copyright 2015 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. |
| */ |
| #include "sdk/android/src/jni/jni_helpers.h" |
| |
| #include <vector> |
| |
| #include "sdk/android/native_api/jni/java_types.h" |
| |
| namespace webrtc { |
| namespace jni { |
| |
| // Return a |jlong| that will correctly convert back to |ptr|. This is needed |
| // because the alternative (of silently passing a 32-bit pointer to a vararg |
| // function expecting a 64-bit param) picks up garbage in the high 32 bits. |
| jlong jlongFromPointer(void* ptr) { |
| static_assert(sizeof(intptr_t) <= sizeof(jlong), |
| "Time to rethink the use of jlongs"); |
| // Going through intptr_t to be obvious about the definedness of the |
| // conversion from pointer to integral type. intptr_t to jlong is a standard |
| // widening by the static_assert above. |
| jlong ret = reinterpret_cast<intptr_t>(ptr); |
| RTC_DCHECK(reinterpret_cast<void*>(ret) == ptr); |
| return ret; |
| } |
| |
| ScopedJavaLocalRef<jobject> NewDirectByteBuffer(JNIEnv* env, |
| void* address, |
| jlong capacity) { |
| ScopedJavaLocalRef<jobject> buffer( |
| env, env->NewDirectByteBuffer(address, capacity)); |
| CHECK_EXCEPTION(env) << "error NewDirectByteBuffer"; |
| return buffer; |
| } |
| |
| jobject NewGlobalRef(JNIEnv* jni, jobject o) { |
| jobject ret = jni->NewGlobalRef(o); |
| CHECK_EXCEPTION(jni) << "error during NewGlobalRef"; |
| RTC_CHECK(ret); |
| return ret; |
| } |
| |
| void DeleteGlobalRef(JNIEnv* jni, jobject o) { |
| jni->DeleteGlobalRef(o); |
| CHECK_EXCEPTION(jni) << "error during DeleteGlobalRef"; |
| } |
| |
| // Scope Java local references to the lifetime of this object. Use in all C++ |
| // callbacks (i.e. entry points that don't originate in a Java callstack |
| // through a "native" method call). |
| ScopedLocalRefFrame::ScopedLocalRefFrame(JNIEnv* jni) : jni_(jni) { |
| RTC_CHECK(!jni_->PushLocalFrame(0)) << "Failed to PushLocalFrame"; |
| } |
| ScopedLocalRefFrame::~ScopedLocalRefFrame() { |
| jni_->PopLocalFrame(nullptr); |
| } |
| |
| } // namespace jni |
| } // namespace webrtc |