Index: media/base/android/media_codec_bridge.cc |
diff --git a/media/base/android/media_codec_bridge.cc b/media/base/android/media_codec_bridge.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c267aefff34806d82e1b39dff28064fe5c157106 |
--- /dev/null |
+++ b/media/base/android/media_codec_bridge.cc |
@@ -0,0 +1,305 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/base/android/media_codec_bridge.h" |
+ |
+#include <jni.h> |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_array.h" |
+#include "base/android/jni_string.h" |
+#include "base/basictypes.h" |
+#include "base/logging.h" |
+#include "base/stringprintf.h" |
+ |
+#include "jni/MediaFormat_jni.h" |
+#include "jni/MediaCodec_jni.h" |
+ |
+ |
+using base::android::AttachCurrentThread; |
+using base::android::ConvertUTF8ToJavaString; |
+using base::android::ScopedJavaLocalRef; |
+ |
+namespace { |
+ |
+static jclass g_MediaCodecBufferInfo_clazz = NULL; |
+ |
+static const char kMediaCodecBufferInfoClassPath[] = |
+ "android/media/MediaCodec$BufferInfo"; |
+ |
+static bool MediaCodecBufferInfo_RegisterNativesImpl(JNIEnv* env) { |
+ g_MediaCodecBufferInfo_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( |
+ base::android::GetUnscopedClass(env, kMediaCodecBufferInfoClassPath))); |
+ base::android::CheckException(env); |
+ return true; |
+} |
+ |
+static void GetBufferInfo(JNIEnv* env, jobject buffer_info, int* offset, |
+ int* size, int64* presentation_time, int* flags) { |
+ static jfieldID offset_id = |
+ env->GetFieldID(g_MediaCodecBufferInfo_clazz, "offset", "I"); |
+ static jfieldID size_id = |
+ env->GetFieldID(g_MediaCodecBufferInfo_clazz, "size", "I"); |
+ static jfieldID presentation_time_id = |
+ env->GetFieldID(g_MediaCodecBufferInfo_clazz, "presentationTimeUs", "J"); |
+ static jfieldID flags_id = |
+ env->GetFieldID(g_MediaCodecBufferInfo_clazz, "flags", "I"); |
+ |
+ *offset = static_cast<int>(env->GetIntField(buffer_info, offset_id)); |
+ *size = static_cast<int>(env->GetIntField(buffer_info, size_id)); |
+ *presentation_time = |
+ static_cast<int64>(env->GetLongField(buffer_info, presentation_time_id)); |
+ *flags = static_cast<int>(env->GetIntField(buffer_info, flags_id)); |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
are the static_cast<int>'s really necessary?
dwkang1
2013/02/04 14:08:26
Removed.
dwkang1
2013/02/04 14:08:26
Removed.
|
+} |
+ |
+static base::subtle::AtomicWord g_MediaCodecBufferInfo_Constructor = 0; |
+static ScopedJavaLocalRef<jobject> Java_MediaCodecBufferInfo_Constructor( |
+ JNIEnv* env) { |
+ /* Must call RegisterNativesImpl() */ |
+ DCHECK(g_MediaCodecBufferInfo_clazz); |
+ jmethodID method_id = |
+ base::android::MethodID::LazyGet<base::android::MethodID::TYPE_INSTANCE>( |
+ env, g_MediaCodecBufferInfo_clazz, |
+ "<init>", "()V", |
+ &g_MediaCodecBufferInfo_Constructor); |
+ |
+ jobject ret = env->NewObject(g_MediaCodecBufferInfo_clazz, method_id); |
+ base::android::CheckException(env); |
+ return ScopedJavaLocalRef<jobject>(env, ret); |
+} |
+ |
+void RegisterNativesIfNeeded(JNIEnv* env) { |
+ static bool jni_initialized = false; |
+ if (!jni_initialized) { |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
This is both racy and unreadable :)
You have:
st
dwkang1
2013/02/04 14:08:26
Thanks for the advise. btw, do you have any refere
|
+ jni_initialized = ( |
+ JNI_MediaCodec::RegisterNativesImpl(env) |
+ && MediaCodecBufferInfo_RegisterNativesImpl(env) |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
operator goes on previous line (here and elsewhere
dwkang1
2013/02/04 14:08:26
Done.
|
+ && JNI_MediaFormat::RegisterNativesImpl(env)); |
+ DCHECK(jni_initialized); |
+ } |
+} |
+} // namespace |
+ |
+namespace media { |
+ |
+typedef struct { |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
This is C++;
typedef struct { ... } Name;
is more
dwkang1
2013/02/04 14:08:26
Done.
|
+ MediaCodecBridge::Codec codec_type_; |
+ const char* mime_type_; |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
de-indent 2 spaces
dwkang1
2013/02/04 14:08:26
Done.
|
+} CodecMapEntry; |
+ |
+static |
+const CodecMapEntry kCodecMimeTypeTable[] = { |
+ { MediaCodecBridge::AUDIO_MPEG, "audio/mpeg" }, |
+ { MediaCodecBridge::VIDEO_H264, "video/avc" }, |
+ { MediaCodecBridge::VIDEO_VP8, "video/x-vnd.on2.vp8" }, |
+ { MediaCodecBridge::UNKNOWN, NULL }, |
+}; |
+ |
+#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
Drop in favor of arraysize()
https://code.google.c
dwkang1
2013/02/04 14:08:26
Thanks for the link.
|
+ |
+static |
+const char* CodecToMimeType(const MediaCodecBridge::Codec codec) { |
+ for (int i = 0; i < NELEM(kCodecMimeTypeTable); ++i) { |
+ if (kCodecMimeTypeTable[i].codec_type_ == codec) { |
+ return kCodecMimeTypeTable[i].mime_type_; |
+ } |
+ } |
+ return NULL; |
+} |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
l.86-109 can be replaced with the 9 lines:
static
dwkang1
2013/02/04 14:08:26
Done.
|
+ |
+MediaCodecBridge::MediaCodecBridge(const Codec codec) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ RegisterNativesIfNeeded(env); |
+ DCHECK(CodecToMimeType(codec)); |
+ |
+ ScopedJavaLocalRef<jstring> j_type = |
+ ConvertUTF8ToJavaString(env, CodecToMimeType(codec)); |
+ |
+ ScopedJavaLocalRef<jobject> tmp( |
+ JNI_MediaCodec::Java_MediaCodec_createDecoderByType(env, j_type.obj())); |
+ DCHECK(!tmp.is_null()); |
+ j_media_codec_.Reset(tmp); |
+} |
+ |
+MediaCodecBridge::~MediaCodecBridge() { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_release(env, j_media_codec_.obj()); |
+} |
+ |
+void MediaCodecBridge::ConfigureAudio( |
+ const Codec codec, int sample_rate, int channel_count) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ DCHECK(CodecToMimeType(codec)); |
+ |
+ ScopedJavaLocalRef<jstring> j_mime = |
+ ConvertUTF8ToJavaString(env, CodecToMimeType(codec)); |
+ ScopedJavaLocalRef<jobject> j_format( |
+ JNI_MediaFormat::Java_MediaFormat_createAudioFormat( |
+ env, j_mime.obj(), sample_rate, channel_count)); |
+ DCHECK(!j_format.is_null()); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_configure( |
+ env, j_media_codec_.obj(), j_format.obj(), NULL, NULL, 0); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_start(env, j_media_codec_.obj()); |
+} |
+ |
+void MediaCodecBridge::ConfigureVideo( |
+ const Codec codec, const gfx::Size& size, jobject surface) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ DCHECK(CodecToMimeType(codec)); |
+ |
+ ScopedJavaLocalRef<jstring> j_mime = |
+ ConvertUTF8ToJavaString(env, CodecToMimeType(codec)); |
+ ScopedJavaLocalRef<jobject> j_format( |
+ JNI_MediaFormat::Java_MediaFormat_createVideoFormat( |
+ env, j_mime.obj(), size.width(), size.height())); |
+ DCHECK(!j_format.is_null()); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_configure( |
+ env, j_media_codec_.obj(), j_format.obj(), surface, NULL, 0); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_start(env, j_media_codec_.obj()); |
+} |
+ |
+void MediaCodecBridge::Flush() { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_flush(env, j_media_codec_.obj()); |
+} |
+ |
+void MediaCodecBridge::Stop() { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_stop(env, j_media_codec_.obj()); |
+} |
+ |
+void MediaCodecBridge::GetOutputFormat(int* format, int* width, int* height) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ ScopedJavaLocalRef<jobject> j_format( |
+ JNI_MediaCodec::Java_MediaCodec_getOutputFormat( |
+ env, j_media_codec_.obj())); |
+ if (!j_format.is_null()) { |
+ ScopedJavaLocalRef<jstring> j_key_format = |
+ ConvertUTF8ToJavaString(env, "color-format"); |
+ *format = static_cast<int>(JNI_MediaFormat::Java_MediaFormat_getInteger( |
+ env, j_format.obj(), j_key_format.obj())); |
+ |
+ ScopedJavaLocalRef<jstring> j_key_width = |
+ ConvertUTF8ToJavaString(env, "width"); |
+ *width = static_cast<int>(JNI_MediaFormat::Java_MediaFormat_getInteger( |
+ env, j_format.obj(), j_key_width.obj())); |
+ |
+ ScopedJavaLocalRef<jstring> j_key_height = |
+ ConvertUTF8ToJavaString(env, "height"); |
+ *height = static_cast<int>(JNI_MediaFormat::Java_MediaFormat_getInteger( |
+ env, j_format.obj(), j_key_height.obj())); |
+ } |
+} |
+ |
+void MediaCodecBridge::QueueInputBuffer( |
+ int index, int offset, int size, int64 presentation_time_us, int flags) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_queueInputBuffer( |
+ env, j_media_codec_.obj(), |
+ static_cast<jint>(index), static_cast<jint>(offset), |
+ static_cast<jint>(size), static_cast<jlong>(presentation_time_us), |
+ static_cast<jint>(flags)); |
+} |
+ |
+int MediaCodecBridge::DequeueInputBuffer(int64 timeout_us) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ return JNI_MediaCodec::Java_MediaCodec_dequeueInputBuffer( |
+ env, j_media_codec_.obj(), static_cast<jlong>(timeout_us)); |
+} |
+ |
+int MediaCodecBridge::DequeueOutputBuffer( |
+ int64 timeout_us, int* offset, int* size, int64* presentation_time_us, |
+ int* flags) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ ScopedJavaLocalRef<jobject> j_info( |
+ Java_MediaCodecBufferInfo_Constructor(env)); |
+ jint j_buffer = JNI_MediaCodec::Java_MediaCodec_dequeueOutputBuffer( |
+ env, j_media_codec_.obj(), j_info.obj(), static_cast<jlong>(timeout_us)); |
+ |
+ if (j_buffer >= 0) { |
+ GetBufferInfo(env, j_info.obj(), offset, size, presentation_time_us, flags); |
+ } |
+ return static_cast<int>(j_buffer); |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
drop the cast? (here and elsewhere)
dwkang1
2013/02/04 14:08:26
Done.
|
+} |
+ |
+void MediaCodecBridge::ReleaseOutputBuffer(int index, bool render) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ JNI_MediaCodec::Java_MediaCodec_releaseOutputBuffer( |
+ env, j_media_codec_.obj(), static_cast<jint>(index), |
+ static_cast<jboolean>(render)); |
+} |
+ |
+int MediaCodecBridge::GetInputBuffers() { |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
Why do you need to expose this as a public API of
dwkang1
2013/02/04 14:08:26
According to the api doc we need to call getOutput
|
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ j_input_buffers_.Reset( |
+ JNI_MediaCodec::Java_MediaCodec_getInputBuffers( |
+ env, j_media_codec_.obj())); |
+ |
+ return env->GetArrayLength(j_input_buffers_.obj()); |
+} |
+ |
+int MediaCodecBridge::GetOutputBuffers() { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ j_output_buffers_.Reset( |
+ JNI_MediaCodec::Java_MediaCodec_getOutputBuffers( |
+ env, j_media_codec_.obj())); |
+ |
+ return env->GetArrayLength(j_output_buffers_.obj()); |
+} |
+ |
+void MediaCodecBridge::PutToInputBuffer( |
+ int index, const uint8* data, int size) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ ScopedJavaLocalRef<jobject> j_buffer( |
+ env, env->GetObjectArrayElement(j_input_buffers_.obj(), index)); |
+ |
+ uint8* direct_buffer = |
+ static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())); |
+ memcpy(direct_buffer, data, size); |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
I think you missed my comment about |size| being t
dwkang1
2013/01/29 03:58:48
Sorry for the missing your previous comment. Will
dwkang1
2013/02/04 14:08:26
Changed to return # of bytes written.
|
+} |
+ |
+void MediaCodecBridge::GetFromOutputBuffer( |
+ int index, int offset, uint8* data, int size) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ |
+ ScopedJavaLocalRef<jobject> j_buffer( |
+ env, env->GetObjectArrayElement(j_output_buffers_.obj(), index)); |
+ |
+ uint8* direct_buffer = |
+ static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())); |
+ memcpy(data, direct_buffer + offset, size); |
Ami GONE FROM CHROMIUM
2013/01/28 19:49:45
Ditto you need some way to guarantee the requested
dwkang1
2013/02/04 14:08:26
Dropped. :)
|
+} |
+ |
+} // namespace media |
+ |