Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

jni: Add a templated helper for retrieving jobjectArrays.

This takes care of ensuring global references, and skips a copy by using
emplace_back rather than push_back.
  • Loading branch information...
commit 157e32a438d738aad5ada56d0ddda8f29fd21e3b 1 parent 5757df4
Cory Fields authored davilla committed
View
38 xbmc/android/jni/MediaCodec.cpp
@@ -164,44 +164,14 @@ const CJNIMediaFormat CJNIMediaCodec::getOutputFormat()
std::vector<CJNIByteBuffer> CJNIMediaCodec::getInputBuffers()
{
- jhobjectArray oByteBuffers = call_method<jhobjectArray>(m_object,
- "getInputBuffers", "()[Ljava/nio/ByteBuffer;");
-
- JNIEnv *env = xbmc_jnienv();
- jsize size = env->GetArrayLength(oByteBuffers.get());
-
- CJNIByteBuffers buffers;
- buffers.reserve(size);
-
- for (int i = 0; i < size; i++)
- {
- jobject j_object = env->GetObjectArrayElement(oByteBuffers.get(), i);
- CJNIByteBuffer buffer = CJNIByteBuffer(jhobject(xbmc_jnienv()->NewGlobalRef(j_object)));
- buffers.push_back(buffer);
- }
-
- return buffers;
+ return jcast<CJNIByteBuffers>(call_method<jhobjectArray>(m_object,
+ "getInputBuffers", "()[Ljava/nio/ByteBuffer;"));
}
std::vector<CJNIByteBuffer> CJNIMediaCodec::getOutputBuffers()
{
- jhobjectArray oByteBuffers = call_method<jhobjectArray>(m_object,
- "getOutputBuffers", "()[Ljava/nio/ByteBuffer;");
-
- JNIEnv *env = xbmc_jnienv();
- jsize size = env->GetArrayLength(oByteBuffers.get());
-
- CJNIByteBuffers buffers;
- buffers.reserve(size);
-
- for (int i = 0; i < size; i++)
- {
- jobject j_object = env->GetObjectArrayElement(oByteBuffers.get(), i);
- CJNIByteBuffer buffer = CJNIByteBuffer(jhobject(xbmc_jnienv()->NewGlobalRef(j_object)));
- buffers.push_back(buffer);
- }
-
- return buffers;
+ return jcast<CJNIByteBuffers>(call_method<jhobjectArray>(m_object,
+ "getOutputBuffers", "()[Ljava/nio/ByteBuffer;"));
}
int CJNIMediaCodec::getInputBufferSize()
View
35 xbmc/android/jni/jutils/jutils-details.hpp
@@ -71,6 +71,12 @@ struct jcast_helper<std::vector<std::string>, jobjectArray>
static std::vector<std::string> cast(jobjectArray const &v);
};
+template <typename T>
+struct jcast_helper<std::vector<T>, jobjectArray>
+{
+ static std::vector<T> cast(jobjectArray const &v);
+};
+
template <>
struct jcast_helper<jhstring, std::string>
{
@@ -101,6 +107,35 @@ struct jcast_helper<std::vector<std::string>, jhobjectArray>
}
};
+template <typename T>
+struct jcast_helper<std::vector<T>, jhobjectArray>
+{
+ static std::vector<T> cast(jhobjectArray const &v)
+ {
+ return jcast_helper<std::vector<T>, jobjectArray>::cast(v);
+ }
+};
+
+
+template <typename T>
+std::vector<T> jcast_helper<std::vector<T>, jobjectArray>::cast(jobjectArray const &v)
+{
+ JNIEnv *env = xbmc_jnienv();
+ jsize size = 0;
+ if(v)
+ size = env->GetArrayLength(v);
+
+ std::vector<T> vec;
+ vec.reserve(size);
+
+ for (int i = 0; i < size; i++)
+ {
+ jhobject element = (jhobject)env->GetObjectArrayElement(v, i);
+ vec.emplace_back(element);
+ }
+ return vec;
+}
+
} // namespace details

2 comments on commit 157e32a

@theuni
Owner

Heh, you beat me to this by seconds. Docs updated after your push.

@davilla
Collaborator

There's a problem here. While it does handle the creation phase of the life time, The death phase will result in a spew of ;

D/dalvikvm(13490): Attempt to remove invalid index 10d (bottom=0d top=4d)
W/dalvikvm(13490): JNI WARNING: DeleteLocalRef(0x22800029) failed to find entry

because the jhobject really belongs to the original array element and we just have a vec.emplace_back copy.

Please sign in to comment.
Something went wrong with that request. Please try again.