Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / media / base / android / media_codec_bridge.cc
blob53c59439c0a3196b2f9ee6071382986a1e88113c
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.
5 #include "media/base/android/media_codec_bridge.h"
7 #include <algorithm>
9 #include "base/android/build_info.h"
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_array.h"
12 #include "base/android/jni_string.h"
13 #include "base/basictypes.h"
14 #include "base/lazy_instance.h"
15 #include "base/logging.h"
16 #include "base/numerics/safe_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "jni/MediaCodecBridge_jni.h"
20 #include "media/base/bit_reader.h"
21 #include "media/base/decrypt_config.h"
23 using base::android::AttachCurrentThread;
24 using base::android::ConvertJavaStringToUTF8;
25 using base::android::ConvertUTF8ToJavaString;
26 using base::android::JavaIntArrayToIntVector;
27 using base::android::ScopedJavaLocalRef;
29 namespace media {
31 enum {
32 kBufferFlagSyncFrame = 1, // BUFFER_FLAG_SYNC_FRAME
33 kBufferFlagEndOfStream = 4, // BUFFER_FLAG_END_OF_STREAM
34 kConfigureFlagEncode = 1, // CONFIGURE_FLAG_ENCODE
37 static const std::string AudioCodecToAndroidMimeType(const AudioCodec& codec) {
38 switch (codec) {
39 case kCodecMP3:
40 return "audio/mpeg";
41 case kCodecVorbis:
42 return "audio/vorbis";
43 case kCodecOpus:
44 return "audio/opus";
45 case kCodecAAC:
46 return "audio/mp4a-latm";
47 default:
48 return std::string();
52 static const std::string VideoCodecToAndroidMimeType(const VideoCodec& codec) {
53 switch (codec) {
54 case kCodecH264:
55 return "video/avc";
56 case kCodecHEVC:
57 return "video/hevc";
58 case kCodecVP8:
59 return "video/x-vnd.on2.vp8";
60 case kCodecVP9:
61 return "video/x-vnd.on2.vp9";
62 default:
63 return std::string();
67 static const std::string CodecTypeToAndroidMimeType(const std::string& codec) {
68 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"?
69 if (codec == "avc1")
70 return "video/avc";
71 if (codec == "hvc1")
72 return "video/hevc";
73 if (codec == "mp4a")
74 return "audio/mp4a-latm";
75 if (codec == "vp8" || codec == "vp8.0")
76 return "video/x-vnd.on2.vp8";
77 if (codec == "vp9" || codec == "vp9.0")
78 return "video/x-vnd.on2.vp9";
79 if (codec == "vorbis")
80 return "audio/vorbis";
81 if (codec == "opus")
82 return "audio/opus";
83 return std::string();
86 // TODO(qinmin): using a map to help all the conversions in this class.
87 static const std::string AndroidMimeTypeToCodecType(const std::string& mime) {
88 if (mime == "video/mp4v-es")
89 return "mp4v";
90 if (mime == "video/avc")
91 return "avc1";
92 if (mime == "video/hevc")
93 return "hvc1";
94 if (mime == "video/x-vnd.on2.vp8")
95 return "vp8";
96 if (mime == "video/x-vnd.on2.vp9")
97 return "vp9";
98 if (mime == "audio/mp4a-latm")
99 return "mp4a";
100 if (mime == "audio/mpeg")
101 return "mp3";
102 if (mime == "audio/vorbis")
103 return "vorbis";
104 if (mime == "audio/opus")
105 return "opus";
106 return std::string();
109 static ScopedJavaLocalRef<jintArray>
110 ToJavaIntArray(JNIEnv* env, scoped_ptr<jint[]> native_array, int size) {
111 ScopedJavaLocalRef<jintArray> j_array(env, env->NewIntArray(size));
112 env->SetIntArrayRegion(j_array.obj(), 0, size, native_array.get());
113 return j_array;
116 // static
117 bool MediaCodecBridge::IsAvailable() {
118 // MediaCodec is only available on JB and greater.
119 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16)
120 return false;
121 // Blacklist some devices on Jellybean as for MediaCodec support is buggy.
122 // http://crbug.com/365494.
123 if (base::android::BuildInfo::GetInstance()->sdk_int() == 16) {
124 std::string model(base::android::BuildInfo::GetInstance()->model());
125 return model != "GT-I9100" && model != "GT-I9300" && model != "GT-N7000";
127 return true;
130 // static
131 bool MediaCodecBridge::SupportsSetParameters() {
132 // MediaCodec.setParameters() is only available starting with K.
133 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
136 // static
137 bool MediaCodecBridge::SupportsGetName() {
138 // MediaCodec.getName() is only available on JB MR2 and greater.
139 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18;
142 // static
143 std::vector<MediaCodecBridge::CodecsInfo> MediaCodecBridge::GetCodecsInfo() {
144 std::vector<CodecsInfo> codecs_info;
145 if (!IsAvailable())
146 return codecs_info;
148 JNIEnv* env = AttachCurrentThread();
149 std::string mime_type;
150 ScopedJavaLocalRef<jobjectArray> j_codec_info_array =
151 Java_MediaCodecBridge_getCodecsInfo(env);
152 jsize len = env->GetArrayLength(j_codec_info_array.obj());
153 for (jsize i = 0; i < len; ++i) {
154 ScopedJavaLocalRef<jobject> j_info(
155 env, env->GetObjectArrayElement(j_codec_info_array.obj(), i));
156 ScopedJavaLocalRef<jstring> j_codec_type =
157 Java_CodecInfo_codecType(env, j_info.obj());
158 ConvertJavaStringToUTF8(env, j_codec_type.obj(), &mime_type);
159 ScopedJavaLocalRef<jstring> j_codec_name =
160 Java_CodecInfo_codecName(env, j_info.obj());
161 CodecsInfo info;
162 info.codecs = AndroidMimeTypeToCodecType(mime_type);
163 ConvertJavaStringToUTF8(env, j_codec_name.obj(), &info.name);
164 info.direction = static_cast<MediaCodecDirection>(
165 Java_CodecInfo_direction(env, j_info.obj()));
166 codecs_info.push_back(info);
168 return codecs_info;
171 // static
172 std::string MediaCodecBridge::GetDefaultCodecName(
173 const std::string& mime_type,
174 MediaCodecDirection direction) {
175 if (!IsAvailable())
176 return std::string();
178 JNIEnv* env = AttachCurrentThread();
179 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type);
180 ScopedJavaLocalRef<jstring> j_codec_name =
181 Java_MediaCodecBridge_getDefaultCodecName(env, j_mime.obj(), direction);
182 return ConvertJavaStringToUTF8(env, j_codec_name.obj());
185 // static
186 std::set<int> MediaCodecBridge::GetEncoderColorFormats(
187 const std::string& mime_type) {
188 std::set<int> color_formats;
189 if (!IsAvailable())
190 return color_formats;
192 JNIEnv* env = AttachCurrentThread();
193 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type);
194 ScopedJavaLocalRef<jintArray> j_color_format_array =
195 Java_MediaCodecBridge_getEncoderColorFormatsForMime(env, j_mime.obj());
197 if (j_color_format_array.obj()) {
198 std::vector<int> formats;
199 JavaIntArrayToIntVector(env, j_color_format_array.obj(), &formats);
200 color_formats = std::set<int>(formats.begin(), formats.end());
203 return color_formats;
206 // static
207 bool MediaCodecBridge::CanDecode(const std::string& codec, bool is_secure) {
208 if (!IsAvailable())
209 return false;
211 JNIEnv* env = AttachCurrentThread();
212 std::string mime = CodecTypeToAndroidMimeType(codec);
213 if (mime.empty())
214 return false;
215 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
216 ScopedJavaLocalRef<jobject> j_media_codec_bridge =
217 Java_MediaCodecBridge_create(env, j_mime.obj(), is_secure, false);
218 if (!j_media_codec_bridge.is_null()) {
219 Java_MediaCodecBridge_release(env, j_media_codec_bridge.obj());
220 return true;
222 return false;
225 // static
226 bool MediaCodecBridge::IsKnownUnaccelerated(const std::string& mime_type,
227 MediaCodecDirection direction) {
228 if (!IsAvailable())
229 return true;
231 std::string codec_name;
232 if (SupportsGetName()) {
233 codec_name = GetDefaultCodecName(mime_type, direction);
234 } else {
235 std::string codec_type = AndroidMimeTypeToCodecType(mime_type);
236 std::vector<media::MediaCodecBridge::CodecsInfo> codecs_info =
237 MediaCodecBridge::GetCodecsInfo();
238 for (size_t i = 0; i < codecs_info.size(); ++i) {
239 if (codecs_info[i].codecs == codec_type &&
240 codecs_info[i].direction == direction) {
241 codec_name = codecs_info[i].name;
242 break;
246 DVLOG(1) << __PRETTY_FUNCTION__ << "Default codec for " << mime_type <<
247 " : " << codec_name;
248 // It would be nice if MediaCodecInfo externalized some notion of
249 // HW-acceleration but it doesn't. Android Media guidance is that the
250 // "OMX.google" prefix is always used for SW decoders, so that's what we
251 // use. "OMX.SEC.*" codec is Samsung software implementation - report it
252 // as unaccelerated as well. Also temporary blacklist Exynos and MediaTek
253 // devices while HW decoder video freezes and distortions are
254 // investigated - http://crbug.com/446974.
255 if (codec_name.length() > 0) {
256 return (base::StartsWith(codec_name, "OMX.google.",
257 base::CompareCase::SENSITIVE) ||
258 base::StartsWith(codec_name, "OMX.SEC.",
259 base::CompareCase::SENSITIVE) ||
260 base::StartsWith(codec_name, "OMX.MTK.",
261 base::CompareCase::SENSITIVE) ||
262 base::StartsWith(codec_name, "OMX.Exynos.",
263 base::CompareCase::SENSITIVE));
265 return true;
268 MediaCodecBridge::MediaCodecBridge(const std::string& mime,
269 bool is_secure,
270 MediaCodecDirection direction) {
271 JNIEnv* env = AttachCurrentThread();
272 CHECK(env);
273 DCHECK(!mime.empty());
274 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
275 j_media_codec_.Reset(
276 Java_MediaCodecBridge_create(env, j_mime.obj(), is_secure, direction));
279 MediaCodecBridge::~MediaCodecBridge() {
280 JNIEnv* env = AttachCurrentThread();
281 CHECK(env);
282 if (j_media_codec_.obj())
283 Java_MediaCodecBridge_release(env, j_media_codec_.obj());
286 bool MediaCodecBridge::StartInternal() {
287 JNIEnv* env = AttachCurrentThread();
288 return Java_MediaCodecBridge_start(env, j_media_codec_.obj());
291 MediaCodecStatus MediaCodecBridge::Reset() {
292 JNIEnv* env = AttachCurrentThread();
293 return static_cast<MediaCodecStatus>(
294 Java_MediaCodecBridge_flush(env, j_media_codec_.obj()));
297 void MediaCodecBridge::Stop() {
298 JNIEnv* env = AttachCurrentThread();
299 Java_MediaCodecBridge_stop(env, j_media_codec_.obj());
302 void MediaCodecBridge::GetOutputFormat(int* width, int* height) {
303 JNIEnv* env = AttachCurrentThread();
305 *width = Java_MediaCodecBridge_getOutputWidth(env, j_media_codec_.obj());
306 *height = Java_MediaCodecBridge_getOutputHeight(env, j_media_codec_.obj());
309 int MediaCodecBridge::GetOutputSamplingRate() {
310 JNIEnv* env = AttachCurrentThread();
312 return Java_MediaCodecBridge_getOutputSamplingRate(env, j_media_codec_.obj());
315 MediaCodecStatus MediaCodecBridge::QueueInputBuffer(
316 int index,
317 const uint8* data,
318 size_t data_size,
319 const base::TimeDelta& presentation_time) {
320 DVLOG(3) << __PRETTY_FUNCTION__ << index << ": " << data_size;
321 if (data_size > base::checked_cast<size_t>(kint32max))
322 return MEDIA_CODEC_ERROR;
323 if (data && !FillInputBuffer(index, data, data_size))
324 return MEDIA_CODEC_ERROR;
325 JNIEnv* env = AttachCurrentThread();
326 return static_cast<MediaCodecStatus>(
327 Java_MediaCodecBridge_queueInputBuffer(env,
328 j_media_codec_.obj(),
329 index,
331 data_size,
332 presentation_time.InMicroseconds(),
333 0));
336 MediaCodecStatus MediaCodecBridge::QueueSecureInputBuffer(
337 int index,
338 const uint8* data,
339 size_t data_size,
340 const uint8* key_id,
341 int key_id_size,
342 const uint8* iv,
343 int iv_size,
344 const SubsampleEntry* subsamples,
345 int subsamples_size,
346 const base::TimeDelta& presentation_time) {
347 DVLOG(3) << __PRETTY_FUNCTION__ << index << ": " << data_size;
348 if (data_size > base::checked_cast<size_t>(kint32max))
349 return MEDIA_CODEC_ERROR;
350 if (data && !FillInputBuffer(index, data, data_size))
351 return MEDIA_CODEC_ERROR;
353 JNIEnv* env = AttachCurrentThread();
354 ScopedJavaLocalRef<jbyteArray> j_key_id =
355 base::android::ToJavaByteArray(env, key_id, key_id_size);
356 ScopedJavaLocalRef<jbyteArray> j_iv =
357 base::android::ToJavaByteArray(env, iv, iv_size);
359 // MediaCodec.CryptoInfo documentations says passing NULL for |clear_array|
360 // to indicate that all data is encrypted. But it doesn't specify what
361 // |cypher_array| and |subsamples_size| should be in that case. Passing
362 // one subsample here just to be on the safe side.
363 int new_subsamples_size = subsamples_size == 0 ? 1 : subsamples_size;
365 scoped_ptr<jint[]> native_clear_array(new jint[new_subsamples_size]);
366 scoped_ptr<jint[]> native_cypher_array(new jint[new_subsamples_size]);
368 if (subsamples_size == 0) {
369 DCHECK(!subsamples);
370 native_clear_array[0] = 0;
371 native_cypher_array[0] = data_size;
372 } else {
373 DCHECK_GT(subsamples_size, 0);
374 DCHECK(subsamples);
375 for (int i = 0; i < subsamples_size; ++i) {
376 DCHECK(subsamples[i].clear_bytes <= std::numeric_limits<uint16>::max());
377 if (subsamples[i].cypher_bytes >
378 static_cast<uint32>(std::numeric_limits<jint>::max())) {
379 return MEDIA_CODEC_ERROR;
382 native_clear_array[i] = subsamples[i].clear_bytes;
383 native_cypher_array[i] = subsamples[i].cypher_bytes;
387 ScopedJavaLocalRef<jintArray> clear_array =
388 ToJavaIntArray(env, native_clear_array.Pass(), new_subsamples_size);
389 ScopedJavaLocalRef<jintArray> cypher_array =
390 ToJavaIntArray(env, native_cypher_array.Pass(), new_subsamples_size);
392 return static_cast<MediaCodecStatus>(
393 Java_MediaCodecBridge_queueSecureInputBuffer(
394 env,
395 j_media_codec_.obj(),
396 index,
398 j_iv.obj(),
399 j_key_id.obj(),
400 clear_array.obj(),
401 cypher_array.obj(),
402 new_subsamples_size,
403 presentation_time.InMicroseconds()));
406 void MediaCodecBridge::QueueEOS(int input_buffer_index) {
407 DVLOG(3) << __PRETTY_FUNCTION__ << ": " << input_buffer_index;
408 JNIEnv* env = AttachCurrentThread();
409 Java_MediaCodecBridge_queueInputBuffer(env,
410 j_media_codec_.obj(),
411 input_buffer_index,
415 kBufferFlagEndOfStream);
418 MediaCodecStatus MediaCodecBridge::DequeueInputBuffer(
419 const base::TimeDelta& timeout,
420 int* index) {
421 JNIEnv* env = AttachCurrentThread();
422 ScopedJavaLocalRef<jobject> result = Java_MediaCodecBridge_dequeueInputBuffer(
423 env, j_media_codec_.obj(), timeout.InMicroseconds());
424 *index = Java_DequeueInputResult_index(env, result.obj());
425 MediaCodecStatus status = static_cast<MediaCodecStatus>(
426 Java_DequeueInputResult_status(env, result.obj()));
427 DVLOG(3) << __PRETTY_FUNCTION__ << ": status: " << status
428 << ", index: " << *index;
429 return status;
432 MediaCodecStatus MediaCodecBridge::DequeueOutputBuffer(
433 const base::TimeDelta& timeout,
434 int* index,
435 size_t* offset,
436 size_t* size,
437 base::TimeDelta* presentation_time,
438 bool* end_of_stream,
439 bool* key_frame) {
440 JNIEnv* env = AttachCurrentThread();
441 ScopedJavaLocalRef<jobject> result =
442 Java_MediaCodecBridge_dequeueOutputBuffer(
443 env, j_media_codec_.obj(), timeout.InMicroseconds());
444 *index = Java_DequeueOutputResult_index(env, result.obj());
445 *offset = base::checked_cast<size_t>(
446 Java_DequeueOutputResult_offset(env, result.obj()));
447 *size = base::checked_cast<size_t>(
448 Java_DequeueOutputResult_numBytes(env, result.obj()));
449 if (presentation_time) {
450 *presentation_time = base::TimeDelta::FromMicroseconds(
451 Java_DequeueOutputResult_presentationTimeMicroseconds(env,
452 result.obj()));
454 int flags = Java_DequeueOutputResult_flags(env, result.obj());
455 if (end_of_stream)
456 *end_of_stream = flags & kBufferFlagEndOfStream;
457 if (key_frame)
458 *key_frame = flags & kBufferFlagSyncFrame;
459 MediaCodecStatus status = static_cast<MediaCodecStatus>(
460 Java_DequeueOutputResult_status(env, result.obj()));
461 DVLOG(3) << __PRETTY_FUNCTION__ << ": status: " << status
462 << ", index: " << *index << ", offset: " << *offset
463 << ", size: " << *size << ", flags: " << flags;
464 return status;
467 void MediaCodecBridge::ReleaseOutputBuffer(int index, bool render) {
468 DVLOG(3) << __PRETTY_FUNCTION__ << ": " << index;
469 JNIEnv* env = AttachCurrentThread();
470 CHECK(env);
472 Java_MediaCodecBridge_releaseOutputBuffer(
473 env, j_media_codec_.obj(), index, render);
476 int MediaCodecBridge::GetOutputBuffersCount() {
477 JNIEnv* env = AttachCurrentThread();
478 return Java_MediaCodecBridge_getOutputBuffersCount(env, j_media_codec_.obj());
481 size_t MediaCodecBridge::GetOutputBuffersCapacity() {
482 JNIEnv* env = AttachCurrentThread();
483 return Java_MediaCodecBridge_getOutputBuffersCapacity(env,
484 j_media_codec_.obj());
487 void MediaCodecBridge::GetInputBuffer(int input_buffer_index,
488 uint8** data,
489 size_t* capacity) {
490 JNIEnv* env = AttachCurrentThread();
491 ScopedJavaLocalRef<jobject> j_buffer(Java_MediaCodecBridge_getInputBuffer(
492 env, j_media_codec_.obj(), input_buffer_index));
493 *data = static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj()));
494 *capacity = base::checked_cast<size_t>(
495 env->GetDirectBufferCapacity(j_buffer.obj()));
498 bool MediaCodecBridge::CopyFromOutputBuffer(int index,
499 size_t offset,
500 void* dst,
501 int dst_size) {
502 void* src_data = nullptr;
503 int src_capacity = GetOutputBufferAddress(index, offset, &src_data);
504 if (src_capacity < dst_size)
505 return false;
506 memcpy(dst, src_data, dst_size);
507 return true;
510 int MediaCodecBridge::GetOutputBufferAddress(int index,
511 size_t offset,
512 void** addr) {
513 JNIEnv* env = AttachCurrentThread();
514 ScopedJavaLocalRef<jobject> j_buffer(
515 Java_MediaCodecBridge_getOutputBuffer(env, j_media_codec_.obj(), index));
516 *addr = reinterpret_cast<uint8*>(
517 env->GetDirectBufferAddress(j_buffer.obj())) + offset;
518 return env->GetDirectBufferCapacity(j_buffer.obj()) - offset;
521 bool MediaCodecBridge::FillInputBuffer(int index,
522 const uint8* data,
523 size_t size) {
524 uint8* dst = NULL;
525 size_t capacity = 0;
526 GetInputBuffer(index, &dst, &capacity);
527 CHECK(dst);
529 if (size > capacity) {
530 LOG(ERROR) << "Input buffer size " << size
531 << " exceeds MediaCodec input buffer capacity: " << capacity;
532 return false;
535 memcpy(dst, data, size);
536 return true;
539 AudioCodecBridge::AudioCodecBridge(const std::string& mime)
540 // Audio codec doesn't care about security level and there is no need for
541 // audio encoding yet.
542 : MediaCodecBridge(mime, false, MEDIA_CODEC_DECODER) {}
544 bool AudioCodecBridge::Start(const AudioCodec& codec,
545 int sample_rate,
546 int channel_count,
547 const uint8* extra_data,
548 size_t extra_data_size,
549 int64 codec_delay_ns,
550 int64 seek_preroll_ns,
551 bool play_audio,
552 jobject media_crypto) {
553 JNIEnv* env = AttachCurrentThread();
555 if (!media_codec())
556 return false;
558 std::string codec_string = AudioCodecToAndroidMimeType(codec);
559 if (codec_string.empty())
560 return false;
562 ScopedJavaLocalRef<jstring> j_mime =
563 ConvertUTF8ToJavaString(env, codec_string);
564 ScopedJavaLocalRef<jobject> j_format(Java_MediaCodecBridge_createAudioFormat(
565 env, j_mime.obj(), sample_rate, channel_count));
566 DCHECK(!j_format.is_null());
568 if (!ConfigureMediaFormat(j_format.obj(), codec, extra_data, extra_data_size,
569 codec_delay_ns, seek_preroll_ns)) {
570 return false;
573 if (!Java_MediaCodecBridge_configureAudio(
574 env, media_codec(), j_format.obj(), media_crypto, 0, play_audio)) {
575 return false;
578 return StartInternal();
581 bool AudioCodecBridge::ConfigureMediaFormat(jobject j_format,
582 const AudioCodec& codec,
583 const uint8* extra_data,
584 size_t extra_data_size,
585 int64 codec_delay_ns,
586 int64 seek_preroll_ns) {
587 if (extra_data_size == 0 && codec != kCodecOpus)
588 return true;
590 JNIEnv* env = AttachCurrentThread();
591 switch (codec) {
592 case kCodecVorbis: {
593 if (extra_data[0] != 2) {
594 LOG(ERROR) << "Invalid number of vorbis headers before the codec "
595 << "header: " << extra_data[0];
596 return false;
599 size_t header_length[2];
600 // |total_length| keeps track of the total number of bytes before the last
601 // header.
602 size_t total_length = 1;
603 const uint8* current_pos = extra_data;
604 // Calculate the length of the first 2 headers.
605 for (int i = 0; i < 2; ++i) {
606 header_length[i] = 0;
607 while (total_length < extra_data_size) {
608 size_t size = *(++current_pos);
609 total_length += 1 + size;
610 if (total_length > 0x80000000) {
611 LOG(ERROR) << "Vorbis header size too large";
612 return false;
614 header_length[i] += size;
615 if (size < 0xFF)
616 break;
618 if (total_length >= extra_data_size) {
619 LOG(ERROR) << "Invalid vorbis header size in the extra data";
620 return false;
623 current_pos++;
624 // The first header is identification header.
625 ScopedJavaLocalRef<jbyteArray> first_header =
626 base::android::ToJavaByteArray(env, current_pos, header_length[0]);
627 Java_MediaCodecBridge_setCodecSpecificData(
628 env, j_format, 0, first_header.obj());
629 // The last header is codec header.
630 ScopedJavaLocalRef<jbyteArray> last_header =
631 base::android::ToJavaByteArray(
632 env, extra_data + total_length, extra_data_size - total_length);
633 Java_MediaCodecBridge_setCodecSpecificData(
634 env, j_format, 1, last_header.obj());
635 break;
637 case kCodecAAC: {
638 media::BitReader reader(extra_data, extra_data_size);
640 // The following code is copied from aac.cc
641 // TODO(qinmin): refactor the code in aac.cc to make it more reusable.
642 uint8 profile = 0;
643 uint8 frequency_index = 0;
644 uint8 channel_config = 0;
645 if (!reader.ReadBits(5, &profile) ||
646 !reader.ReadBits(4, &frequency_index)) {
647 LOG(ERROR) << "Unable to parse AAC header";
648 return false;
650 if (0xf == frequency_index && !reader.SkipBits(24)) {
651 LOG(ERROR) << "Unable to parse AAC header";
652 return false;
654 if (!reader.ReadBits(4, &channel_config)) {
655 LOG(ERROR) << "Unable to parse AAC header";
656 return false;
659 if (profile < 1 || profile > 4 || frequency_index == 0xf ||
660 channel_config > 7) {
661 LOG(ERROR) << "Invalid AAC header";
662 return false;
664 const size_t kCsdLength = 2;
665 uint8 csd[kCsdLength];
666 csd[0] = profile << 3 | frequency_index >> 1;
667 csd[1] = (frequency_index & 0x01) << 7 | channel_config << 3;
668 ScopedJavaLocalRef<jbyteArray> byte_array =
669 base::android::ToJavaByteArray(env, csd, kCsdLength);
670 Java_MediaCodecBridge_setCodecSpecificData(
671 env, j_format, 0, byte_array.obj());
673 // TODO(qinmin): pass an extra variable to this function to determine
674 // whether we need to call this.
675 Java_MediaCodecBridge_setFrameHasADTSHeader(env, j_format);
676 break;
678 case kCodecOpus: {
679 if (!extra_data || extra_data_size == 0 ||
680 codec_delay_ns < 0 || seek_preroll_ns < 0) {
681 LOG(ERROR) << "Invalid Opus Header";
682 return false;
685 // csd0 - Opus Header
686 ScopedJavaLocalRef<jbyteArray> csd0 =
687 base::android::ToJavaByteArray(env, extra_data, extra_data_size);
688 Java_MediaCodecBridge_setCodecSpecificData(env, j_format, 0, csd0.obj());
690 // csd1 - Codec Delay
691 ScopedJavaLocalRef<jbyteArray> csd1 =
692 base::android::ToJavaByteArray(
693 env, reinterpret_cast<const uint8*>(&codec_delay_ns),
694 sizeof(int64_t));
695 Java_MediaCodecBridge_setCodecSpecificData(env, j_format, 1, csd1.obj());
697 // csd2 - Seek Preroll
698 ScopedJavaLocalRef<jbyteArray> csd2 =
699 base::android::ToJavaByteArray(
700 env, reinterpret_cast<const uint8*>(&seek_preroll_ns),
701 sizeof(int64_t));
702 Java_MediaCodecBridge_setCodecSpecificData(env, j_format, 2, csd2.obj());
703 break;
705 default:
706 LOG(ERROR) << "Invalid header encountered for codec: "
707 << AudioCodecToAndroidMimeType(codec);
708 return false;
710 return true;
713 int64 AudioCodecBridge::PlayOutputBuffer(int index,
714 size_t size,
715 size_t offset,
716 bool postpone) {
717 DCHECK_LE(0, index);
718 int numBytes = base::checked_cast<int>(size);
720 void* buffer = nullptr;
721 int capacity = GetOutputBufferAddress(index, offset, &buffer);
722 numBytes = std::min(capacity, numBytes);
723 CHECK_GE(numBytes, 0);
725 JNIEnv* env = AttachCurrentThread();
726 ScopedJavaLocalRef<jbyteArray> byte_array = base::android::ToJavaByteArray(
727 env, static_cast<uint8*>(buffer), numBytes);
728 return Java_MediaCodecBridge_playOutputBuffer(env, media_codec(),
729 byte_array.obj(), postpone);
732 void AudioCodecBridge::SetVolume(double volume) {
733 JNIEnv* env = AttachCurrentThread();
734 Java_MediaCodecBridge_setVolume(env, media_codec(), volume);
737 // static
738 AudioCodecBridge* AudioCodecBridge::Create(const AudioCodec& codec) {
739 if (!MediaCodecBridge::IsAvailable())
740 return NULL;
742 const std::string mime = AudioCodecToAndroidMimeType(codec);
743 return mime.empty() ? NULL : new AudioCodecBridge(mime);
746 // static
747 bool AudioCodecBridge::IsKnownUnaccelerated(const AudioCodec& codec) {
748 return MediaCodecBridge::IsKnownUnaccelerated(
749 AudioCodecToAndroidMimeType(codec), MEDIA_CODEC_DECODER);
752 // static
753 bool VideoCodecBridge::IsKnownUnaccelerated(const VideoCodec& codec,
754 MediaCodecDirection direction) {
755 return MediaCodecBridge::IsKnownUnaccelerated(
756 VideoCodecToAndroidMimeType(codec), direction);
759 // static
760 VideoCodecBridge* VideoCodecBridge::CreateDecoder(const VideoCodec& codec,
761 bool is_secure,
762 const gfx::Size& size,
763 jobject surface,
764 jobject media_crypto) {
765 if (!MediaCodecBridge::IsAvailable())
766 return NULL;
768 const std::string mime = VideoCodecToAndroidMimeType(codec);
769 if (mime.empty())
770 return NULL;
772 scoped_ptr<VideoCodecBridge> bridge(
773 new VideoCodecBridge(mime, is_secure, MEDIA_CODEC_DECODER));
774 if (!bridge->media_codec())
775 return NULL;
777 JNIEnv* env = AttachCurrentThread();
778 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
779 ScopedJavaLocalRef<jobject> j_format(
780 Java_MediaCodecBridge_createVideoDecoderFormat(
781 env, j_mime.obj(), size.width(), size.height()));
782 DCHECK(!j_format.is_null());
783 if (!Java_MediaCodecBridge_configureVideo(env,
784 bridge->media_codec(),
785 j_format.obj(),
786 surface,
787 media_crypto,
788 0)) {
789 return NULL;
792 return bridge->StartInternal() ? bridge.release() : NULL;
795 // static
796 VideoCodecBridge* VideoCodecBridge::CreateEncoder(const VideoCodec& codec,
797 const gfx::Size& size,
798 int bit_rate,
799 int frame_rate,
800 int i_frame_interval,
801 int color_format) {
802 if (!MediaCodecBridge::IsAvailable())
803 return NULL;
805 const std::string mime = VideoCodecToAndroidMimeType(codec);
806 if (mime.empty())
807 return NULL;
809 scoped_ptr<VideoCodecBridge> bridge(
810 new VideoCodecBridge(mime, false, MEDIA_CODEC_ENCODER));
811 if (!bridge->media_codec())
812 return NULL;
814 JNIEnv* env = AttachCurrentThread();
815 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
816 ScopedJavaLocalRef<jobject> j_format(
817 Java_MediaCodecBridge_createVideoEncoderFormat(env,
818 j_mime.obj(),
819 size.width(),
820 size.height(),
821 bit_rate,
822 frame_rate,
823 i_frame_interval,
824 color_format));
825 DCHECK(!j_format.is_null());
826 if (!Java_MediaCodecBridge_configureVideo(env,
827 bridge->media_codec(),
828 j_format.obj(),
829 NULL,
830 NULL,
831 kConfigureFlagEncode)) {
832 return NULL;
835 return bridge->StartInternal() ? bridge.release() : NULL;
838 VideoCodecBridge::VideoCodecBridge(const std::string& mime,
839 bool is_secure,
840 MediaCodecDirection direction)
841 : MediaCodecBridge(mime, is_secure, direction),
842 adaptive_playback_supported_for_testing_(-1) {}
844 void VideoCodecBridge::SetVideoBitrate(int bps) {
845 JNIEnv* env = AttachCurrentThread();
846 Java_MediaCodecBridge_setVideoBitrate(env, media_codec(), bps);
849 void VideoCodecBridge::RequestKeyFrameSoon() {
850 JNIEnv* env = AttachCurrentThread();
851 Java_MediaCodecBridge_requestKeyFrameSoon(env, media_codec());
854 bool VideoCodecBridge::IsAdaptivePlaybackSupported(int width, int height) {
855 if (adaptive_playback_supported_for_testing_ == 0)
856 return false;
857 else if (adaptive_playback_supported_for_testing_ > 0)
858 return true;
859 JNIEnv* env = AttachCurrentThread();
860 return Java_MediaCodecBridge_isAdaptivePlaybackSupported(
861 env, media_codec(), width, height);
864 bool MediaCodecBridge::RegisterMediaCodecBridge(JNIEnv* env) {
865 return RegisterNativesImpl(env);
868 } // namespace media