Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/android/media_codec_bridge.h" | |
| 6 | |
| 7 #include <jni.h> | |
| 8 | |
| 9 #include "base/android/jni_android.h" | |
| 10 #include "base/android/jni_array.h" | |
| 11 #include "base/android/jni_string.h" | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/lazy_instance.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/stringprintf.h" | |
| 16 | |
| 17 #include "jni/MediaFormat_jni.h" | |
| 18 #include "jni/MediaCodec_jni.h" | |
| 19 | |
| 20 | |
| 21 using base::android::AttachCurrentThread; | |
| 22 using base::android::ConvertUTF8ToJavaString; | |
| 23 using base::android::ScopedJavaLocalRef; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 static jclass g_MediaCodecBufferInfo_clazz = NULL; | |
| 28 | |
| 29 static const char kMediaCodecBufferInfoClassPath[] = | |
| 30 "android/media/MediaCodec$BufferInfo"; | |
| 31 | |
| 32 static bool MediaCodecBufferInfo_RegisterNativesImpl(JNIEnv* env) { | |
| 33 g_MediaCodecBufferInfo_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( | |
| 34 base::android::GetUnscopedClass(env, kMediaCodecBufferInfoClassPath))); | |
| 35 base::android::CheckException(env); | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 static void GetBufferInfo(JNIEnv* env, jobject buffer_info, int* offset, | |
| 40 int* size, int64* presentation_time, int* flags) { | |
| 41 static jfieldID offset_id = | |
| 42 env->GetFieldID(g_MediaCodecBufferInfo_clazz, "offset", "I"); | |
| 43 static jfieldID size_id = | |
| 44 env->GetFieldID(g_MediaCodecBufferInfo_clazz, "size", "I"); | |
| 45 static jfieldID presentation_time_id = | |
| 46 env->GetFieldID(g_MediaCodecBufferInfo_clazz, "presentationTimeUs", "J"); | |
| 47 static jfieldID flags_id = | |
| 48 env->GetFieldID(g_MediaCodecBufferInfo_clazz, "flags", "I"); | |
| 49 | |
| 50 *offset = env->GetIntField(buffer_info, offset_id); | |
| 51 *size = env->GetIntField(buffer_info, size_id); | |
| 52 *presentation_time = env->GetLongField(buffer_info, presentation_time_id); | |
| 53 *flags = env->GetIntField(buffer_info, flags_id); | |
| 54 } | |
| 55 | |
| 56 static base::subtle::AtomicWord g_MediaCodecBufferInfo_Constructor = 0; | |
| 57 static ScopedJavaLocalRef<jobject> Java_MediaCodecBufferInfo_Constructor( | |
| 58 JNIEnv* env) { | |
| 59 /* Must call RegisterNativesImpl() */ | |
| 60 DCHECK(g_MediaCodecBufferInfo_clazz); | |
| 61 jmethodID method_id = | |
| 62 base::android::MethodID::LazyGet<base::android::MethodID::TYPE_INSTANCE>( | |
| 63 env, g_MediaCodecBufferInfo_clazz, | |
| 64 "<init>", "()V", | |
| 65 &g_MediaCodecBufferInfo_Constructor); | |
| 66 | |
| 67 jobject ret = env->NewObject(g_MediaCodecBufferInfo_clazz, method_id); | |
| 68 base::android::CheckException(env); | |
| 69 return ScopedJavaLocalRef<jobject>(env, ret); | |
| 70 } | |
| 71 | |
| 72 class MediaCodecNativeRegisterer { | |
| 73 public: | |
| 74 MediaCodecNativeRegisterer() { | |
| 75 JNIEnv* env = AttachCurrentThread(); | |
| 76 jni_initialized_ = | |
| 77 MediaCodecBufferInfo_RegisterNativesImpl(env) && | |
| 78 JNI_MediaCodec::RegisterNativesImpl(env) && | |
| 79 JNI_MediaFormat::RegisterNativesImpl(env); | |
| 80 } | |
| 81 | |
| 82 bool IsRegistered() { | |
| 83 return jni_initialized_; | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 bool jni_initialized_; | |
| 88 }; | |
| 89 | |
| 90 static base::LazyInstance<MediaCodecNativeRegisterer> g_native_registerer = | |
| 91 LAZY_INSTANCE_INITIALIZER; | |
| 92 | |
| 93 } // namespace | |
| 94 | |
| 95 namespace media { | |
| 96 | |
| 97 static const char* CodecToMimeType(const MediaCodecBridge::Codec codec) { | |
| 98 switch (codec) { | |
| 99 case MediaCodecBridge::AUDIO_MPEG: return "audio/mpeg"; | |
| 100 case MediaCodecBridge::VIDEO_H264: return "video/avc"; | |
| 101 case MediaCodecBridge::VIDEO_VP8: return "video/x-vnd.on2.vp8"; | |
| 102 default: return NULL; | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 // static | |
| 107 const base::TimeDelta MediaCodecBridge::kTimeOutInfinity = | |
| 108 base::TimeDelta::FromMicroseconds(-1); | |
| 109 | |
| 110 // static | |
| 111 const base::TimeDelta MediaCodecBridge::kTimeOutNoWait = | |
| 112 base::TimeDelta::FromMicroseconds(0); | |
| 113 | |
| 114 MediaCodecBridge::MediaCodecBridge(const Codec codec) { | |
| 115 JNIEnv* env = AttachCurrentThread(); | |
| 116 CHECK(env); | |
| 117 CHECK(g_native_registerer.Pointer()->IsRegistered()); | |
| 118 DCHECK(CodecToMimeType(codec)); | |
| 119 | |
| 120 ScopedJavaLocalRef<jstring> j_type = | |
| 121 ConvertUTF8ToJavaString(env, CodecToMimeType(codec)); | |
| 122 | |
| 123 ScopedJavaLocalRef<jobject> tmp( | |
| 124 JNI_MediaCodec::Java_MediaCodec_createDecoderByType(env, j_type.obj())); | |
| 125 DCHECK(!tmp.is_null()); | |
| 126 j_media_codec_.Reset(tmp); | |
| 127 } | |
| 128 | |
| 129 MediaCodecBridge::~MediaCodecBridge() { | |
| 130 JNIEnv* env = AttachCurrentThread(); | |
| 131 CHECK(env); | |
| 132 | |
| 133 JNI_MediaCodec::Java_MediaCodec_release(env, j_media_codec_.obj()); | |
| 134 } | |
| 135 | |
| 136 void MediaCodecBridge::StartAudio( | |
| 137 const Codec codec, int sample_rate, int channel_count) { | |
| 138 JNIEnv* env = AttachCurrentThread(); | |
| 139 CHECK(env); | |
| 140 DCHECK(CodecToMimeType(codec)); | |
| 141 | |
| 142 ScopedJavaLocalRef<jstring> j_mime = | |
| 143 ConvertUTF8ToJavaString(env, CodecToMimeType(codec)); | |
| 144 ScopedJavaLocalRef<jobject> j_format( | |
| 145 JNI_MediaFormat::Java_MediaFormat_createAudioFormat( | |
| 146 env, j_mime.obj(), sample_rate, channel_count)); | |
| 147 DCHECK(!j_format.is_null()); | |
| 148 | |
| 149 JNI_MediaCodec::Java_MediaCodec_configure( | |
| 150 env, j_media_codec_.obj(), j_format.obj(), NULL, NULL, 0); | |
| 151 | |
| 152 Start(); | |
| 153 } | |
| 154 | |
| 155 void MediaCodecBridge::StartVideo( | |
| 156 const Codec codec, const gfx::Size& size, jobject surface) { | |
| 157 JNIEnv* env = AttachCurrentThread(); | |
| 158 CHECK(env); | |
| 159 DCHECK(CodecToMimeType(codec)); | |
| 160 | |
| 161 ScopedJavaLocalRef<jstring> j_mime = | |
| 162 ConvertUTF8ToJavaString(env, CodecToMimeType(codec)); | |
| 163 ScopedJavaLocalRef<jobject> j_format( | |
| 164 JNI_MediaFormat::Java_MediaFormat_createVideoFormat( | |
| 165 env, j_mime.obj(), size.width(), size.height())); | |
| 166 DCHECK(!j_format.is_null()); | |
| 167 | |
| 168 JNI_MediaCodec::Java_MediaCodec_configure( | |
| 169 env, j_media_codec_.obj(), j_format.obj(), surface, NULL, 0); | |
| 170 | |
| 171 Start(); | |
| 172 } | |
| 173 | |
| 174 void MediaCodecBridge::Start() { | |
| 175 JNIEnv* env = AttachCurrentThread(); | |
| 176 CHECK(env); | |
| 177 | |
| 178 JNI_MediaCodec::Java_MediaCodec_start(env, j_media_codec_.obj()); | |
| 179 GetInputBuffers(); | |
| 180 } | |
| 181 | |
| 182 void MediaCodecBridge::Reset() { | |
| 183 JNIEnv* env = AttachCurrentThread(); | |
| 184 CHECK(env); | |
| 185 | |
| 186 JNI_MediaCodec::Java_MediaCodec_flush(env, j_media_codec_.obj()); | |
| 187 } | |
| 188 | |
| 189 void MediaCodecBridge::Stop() { | |
| 190 JNIEnv* env = AttachCurrentThread(); | |
| 191 CHECK(env); | |
| 192 | |
| 193 JNI_MediaCodec::Java_MediaCodec_stop(env, j_media_codec_.obj()); | |
| 194 } | |
| 195 | |
| 196 void MediaCodecBridge::GetOutputFormat(int* width, int* height) { | |
| 197 JNIEnv* env = AttachCurrentThread(); | |
| 198 CHECK(env); | |
| 199 | |
| 200 ScopedJavaLocalRef<jobject> j_format( | |
| 201 JNI_MediaCodec::Java_MediaCodec_getOutputFormat( | |
| 202 env, j_media_codec_.obj())); | |
| 203 if (!j_format.is_null()) { | |
| 204 ScopedJavaLocalRef<jstring> j_key_width = | |
| 205 ConvertUTF8ToJavaString(env, "width"); | |
| 206 *width = JNI_MediaFormat::Java_MediaFormat_getInteger( | |
| 207 env, j_format.obj(), j_key_width.obj()); | |
| 208 | |
| 209 ScopedJavaLocalRef<jstring> j_key_height = | |
| 210 ConvertUTF8ToJavaString(env, "height"); | |
| 211 *height = JNI_MediaFormat::Java_MediaFormat_getInteger( | |
| 212 env, j_format.obj(), j_key_height.obj()); | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 size_t MediaCodecBridge::QueueInputBuffer( | |
| 217 int index, const uint8* data, int size, | |
| 218 const base::TimeDelta& presentation_time) { | |
| 219 JNIEnv* env = AttachCurrentThread(); | |
| 220 CHECK(env); | |
|
Ami GONE FROM CHROMIUM
2013/02/13 18:07:11
unnecessary
dwkang1
2013/02/16 11:30:31
Done.
| |
| 221 | |
| 222 ScopedJavaLocalRef<jobject> j_buffer( | |
| 223 env, env->GetObjectArrayElement(j_input_buffers_.obj(), index)); | |
| 224 | |
| 225 uint8* direct_buffer = | |
| 226 static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())); | |
| 227 int64 buffer_capacity = env->GetDirectBufferCapacity(j_buffer.obj()); | |
| 228 | |
| 229 size_t size_to_copy = (buffer_capacity < size) ? buffer_capacity : size; | |
| 230 if (size_to_copy > 0) | |
| 231 memcpy(direct_buffer, data, size_to_copy); | |
| 232 | |
| 233 JNI_MediaCodec::Java_MediaCodec_queueInputBuffer( | |
| 234 env, j_media_codec_.obj(), | |
| 235 index, 0, size_to_copy, presentation_time.InMicroseconds(), 0); | |
| 236 return size_to_copy; | |
| 237 } | |
| 238 | |
| 239 void MediaCodecBridge::QueueEOS(int input_buffer_index) { | |
| 240 JNIEnv* env = AttachCurrentThread(); | |
| 241 CHECK(env); | |
| 242 | |
| 243 JNI_MediaCodec::Java_MediaCodec_queueInputBuffer( | |
| 244 env, j_media_codec_.obj(), | |
| 245 input_buffer_index, 0, 0, 0, BUFFER_FLAG_END_OF_STREAM); | |
| 246 } | |
| 247 | |
| 248 int MediaCodecBridge::DequeueInputBuffer(base::TimeDelta timeout) { | |
| 249 JNIEnv* env = AttachCurrentThread(); | |
| 250 CHECK(env); | |
| 251 | |
| 252 return JNI_MediaCodec::Java_MediaCodec_dequeueInputBuffer( | |
| 253 env, j_media_codec_.obj(), timeout.InMicroseconds()); | |
| 254 } | |
| 255 | |
| 256 int MediaCodecBridge::DequeueOutputBuffer( | |
| 257 base::TimeDelta timeout, int* offset, int* size, | |
| 258 base::TimeDelta* presentation_time, bool* end_of_stream) { | |
| 259 JNIEnv* env = AttachCurrentThread(); | |
| 260 CHECK(env); | |
| 261 | |
| 262 ScopedJavaLocalRef<jobject> j_info( | |
| 263 Java_MediaCodecBufferInfo_Constructor(env)); | |
| 264 jint j_buffer = JNI_MediaCodec::Java_MediaCodec_dequeueOutputBuffer( | |
| 265 env, j_media_codec_.obj(), j_info.obj(), timeout.InMicroseconds()); | |
| 266 | |
| 267 if (j_buffer >= 0) { | |
| 268 int64 presentation_time_us; | |
| 269 int flags; | |
| 270 GetBufferInfo( | |
| 271 env, j_info.obj(), offset, size, &presentation_time_us, &flags); | |
| 272 *presentation_time = | |
| 273 base::TimeDelta::FromMicroseconds(presentation_time_us); | |
| 274 *end_of_stream = flags & BUFFER_FLAG_END_OF_STREAM; | |
| 275 } | |
| 276 return j_buffer; | |
| 277 } | |
| 278 | |
| 279 void MediaCodecBridge::ReleaseOutputBuffer(int index, bool render) { | |
| 280 JNIEnv* env = AttachCurrentThread(); | |
| 281 CHECK(env); | |
| 282 | |
| 283 JNI_MediaCodec::Java_MediaCodec_releaseOutputBuffer( | |
| 284 env, j_media_codec_.obj(), index, render); | |
| 285 } | |
| 286 | |
| 287 int MediaCodecBridge::GetInputBuffers() { | |
| 288 JNIEnv* env = AttachCurrentThread(); | |
| 289 CHECK(env); | |
| 290 | |
| 291 j_input_buffers_.Reset( | |
| 292 JNI_MediaCodec::Java_MediaCodec_getInputBuffers( | |
| 293 env, j_media_codec_.obj())); | |
| 294 | |
| 295 return env->GetArrayLength(j_input_buffers_.obj()); | |
| 296 } | |
| 297 | |
| 298 int MediaCodecBridge::GetOutputBuffers() { | |
| 299 JNIEnv* env = AttachCurrentThread(); | |
| 300 CHECK(env); | |
| 301 | |
| 302 j_output_buffers_.Reset( | |
| 303 JNI_MediaCodec::Java_MediaCodec_getOutputBuffers( | |
| 304 env, j_media_codec_.obj())); | |
| 305 | |
| 306 return env->GetArrayLength(j_output_buffers_.obj()); | |
| 307 } | |
| 308 | |
| 309 } // namespace media | |
| 310 | |
| OLD | NEW |