Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / base / android / media_codec_bridge.cc
blob51df6d4ad2f8010f257911e802a2ea94ffc4e9aa
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 #define RETURN_ON_ERROR(condition) \
30 do { \
31 if (!(condition)) { \
32 LOG(ERROR) << "Unable to parse AAC header: " #condition; \
33 return false; \
34 } \
35 } while (0)
37 namespace media {
39 enum {
40 kBufferFlagSyncFrame = 1, // BUFFER_FLAG_SYNC_FRAME
41 kBufferFlagEndOfStream = 4, // BUFFER_FLAG_END_OF_STREAM
42 kConfigureFlagEncode = 1, // CONFIGURE_FLAG_ENCODE
45 static const std::string AudioCodecToAndroidMimeType(const AudioCodec& codec) {
46 switch (codec) {
47 case kCodecMP3:
48 return "audio/mpeg";
49 case kCodecVorbis:
50 return "audio/vorbis";
51 case kCodecOpus:
52 return "audio/opus";
53 case kCodecAAC:
54 return "audio/mp4a-latm";
55 default:
56 return std::string();
60 static const std::string VideoCodecToAndroidMimeType(const VideoCodec& codec) {
61 switch (codec) {
62 case kCodecH264:
63 return "video/avc";
64 case kCodecHEVC:
65 return "video/hevc";
66 case kCodecVP8:
67 return "video/x-vnd.on2.vp8";
68 case kCodecVP9:
69 return "video/x-vnd.on2.vp9";
70 default:
71 return std::string();
75 static const std::string CodecTypeToAndroidMimeType(const std::string& codec) {
76 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"?
77 if (codec == "avc1")
78 return "video/avc";
79 if (codec == "hvc1")
80 return "video/hevc";
81 if (codec == "mp4a")
82 return "audio/mp4a-latm";
83 if (codec == "vp8" || codec == "vp8.0")
84 return "video/x-vnd.on2.vp8";
85 if (codec == "vp9" || codec == "vp9.0")
86 return "video/x-vnd.on2.vp9";
87 if (codec == "vorbis")
88 return "audio/vorbis";
89 if (codec == "opus")
90 return "audio/opus";
91 return std::string();
94 // TODO(qinmin): using a map to help all the conversions in this class.
95 static const std::string AndroidMimeTypeToCodecType(const std::string& mime) {
96 if (mime == "video/mp4v-es")
97 return "mp4v";
98 if (mime == "video/avc")
99 return "avc1";
100 if (mime == "video/hevc")
101 return "hvc1";
102 if (mime == "video/x-vnd.on2.vp8")
103 return "vp8";
104 if (mime == "video/x-vnd.on2.vp9")
105 return "vp9";
106 if (mime == "audio/mp4a-latm")
107 return "mp4a";
108 if (mime == "audio/mpeg")
109 return "mp3";
110 if (mime == "audio/vorbis")
111 return "vorbis";
112 if (mime == "audio/opus")
113 return "opus";
114 return std::string();
117 static ScopedJavaLocalRef<jintArray>
118 ToJavaIntArray(JNIEnv* env, scoped_ptr<jint[]> native_array, int size) {
119 ScopedJavaLocalRef<jintArray> j_array(env, env->NewIntArray(size));
120 env->SetIntArrayRegion(j_array.obj(), 0, size, native_array.get());
121 return j_array;
124 // static
125 bool MediaCodecBridge::IsAvailable() {
126 // MediaCodec is only available on JB and greater.
127 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16)
128 return false;
129 // Blacklist some devices on Jellybean as for MediaCodec support is buggy.
130 // http://crbug.com/365494.
131 if (base::android::BuildInfo::GetInstance()->sdk_int() == 16) {
132 std::string model(base::android::BuildInfo::GetInstance()->model());
133 return model != "GT-I9100" && model != "GT-I9300" && model != "GT-N7000";
135 return true;
138 // static
139 bool MediaCodecBridge::SupportsSetParameters() {
140 // MediaCodec.setParameters() is only available starting with K.
141 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
144 // static
145 bool MediaCodecBridge::SupportsGetName() {
146 // MediaCodec.getName() is only available on JB MR2 and greater.
147 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18;
150 // static
151 std::vector<MediaCodecBridge::CodecsInfo> MediaCodecBridge::GetCodecsInfo() {
152 std::vector<CodecsInfo> codecs_info;
153 if (!IsAvailable())
154 return codecs_info;
156 JNIEnv* env = AttachCurrentThread();
157 std::string mime_type;
158 ScopedJavaLocalRef<jobjectArray> j_codec_info_array =
159 Java_MediaCodecBridge_getCodecsInfo(env);
160 jsize len = env->GetArrayLength(j_codec_info_array.obj());
161 for (jsize i = 0; i < len; ++i) {
162 ScopedJavaLocalRef<jobject> j_info(
163 env, env->GetObjectArrayElement(j_codec_info_array.obj(), i));
164 ScopedJavaLocalRef<jstring> j_codec_type =
165 Java_CodecInfo_codecType(env, j_info.obj());
166 ConvertJavaStringToUTF8(env, j_codec_type.obj(), &mime_type);
167 ScopedJavaLocalRef<jstring> j_codec_name =
168 Java_CodecInfo_codecName(env, j_info.obj());
169 CodecsInfo info;
170 info.codecs = AndroidMimeTypeToCodecType(mime_type);
171 ConvertJavaStringToUTF8(env, j_codec_name.obj(), &info.name);
172 info.direction = static_cast<MediaCodecDirection>(
173 Java_CodecInfo_direction(env, j_info.obj()));
174 codecs_info.push_back(info);
176 return codecs_info;
179 // static
180 std::string MediaCodecBridge::GetDefaultCodecName(
181 const std::string& mime_type,
182 MediaCodecDirection direction) {
183 if (!IsAvailable())
184 return std::string();
186 JNIEnv* env = AttachCurrentThread();
187 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type);
188 ScopedJavaLocalRef<jstring> j_codec_name =
189 Java_MediaCodecBridge_getDefaultCodecName(env, j_mime.obj(), direction);
190 return ConvertJavaStringToUTF8(env, j_codec_name.obj());
193 // static
194 std::set<int> MediaCodecBridge::GetEncoderColorFormats(
195 const std::string& mime_type) {
196 std::set<int> color_formats;
197 if (!IsAvailable())
198 return color_formats;
200 JNIEnv* env = AttachCurrentThread();
201 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type);
202 ScopedJavaLocalRef<jintArray> j_color_format_array =
203 Java_MediaCodecBridge_getEncoderColorFormatsForMime(env, j_mime.obj());
205 if (j_color_format_array.obj()) {
206 std::vector<int> formats;
207 JavaIntArrayToIntVector(env, j_color_format_array.obj(), &formats);
208 color_formats = std::set<int>(formats.begin(), formats.end());
211 return color_formats;
214 // static
215 bool MediaCodecBridge::CanDecode(const std::string& codec, bool is_secure) {
216 if (!IsAvailable())
217 return false;
219 JNIEnv* env = AttachCurrentThread();
220 std::string mime = CodecTypeToAndroidMimeType(codec);
221 if (mime.empty())
222 return false;
223 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
224 ScopedJavaLocalRef<jobject> j_media_codec_bridge =
225 Java_MediaCodecBridge_create(env, j_mime.obj(), is_secure, false);
226 if (!j_media_codec_bridge.is_null()) {
227 Java_MediaCodecBridge_release(env, j_media_codec_bridge.obj());
228 return true;
230 return false;
233 // static
234 bool MediaCodecBridge::IsKnownUnaccelerated(const std::string& mime_type,
235 MediaCodecDirection direction) {
236 if (!IsAvailable())
237 return true;
239 std::string codec_name;
240 if (SupportsGetName()) {
241 codec_name = GetDefaultCodecName(mime_type, direction);
242 } else {
243 std::string codec_type = AndroidMimeTypeToCodecType(mime_type);
244 std::vector<media::MediaCodecBridge::CodecsInfo> codecs_info =
245 MediaCodecBridge::GetCodecsInfo();
246 for (size_t i = 0; i < codecs_info.size(); ++i) {
247 if (codecs_info[i].codecs == codec_type &&
248 codecs_info[i].direction == direction) {
249 codec_name = codecs_info[i].name;
250 break;
254 DVLOG(1) << __PRETTY_FUNCTION__ << "Default codec for " << mime_type <<
255 " : " << codec_name;
256 // It would be nice if MediaCodecInfo externalized some notion of
257 // HW-acceleration but it doesn't. Android Media guidance is that the
258 // "OMX.google" prefix is always used for SW decoders, so that's what we
259 // use. "OMX.SEC.*" codec is Samsung software implementation - report it
260 // as unaccelerated as well. Also temporary blacklist Exynos and MediaTek
261 // devices while HW decoder video freezes and distortions are
262 // investigated - http://crbug.com/446974.
263 if (codec_name.length() > 0) {
264 return (base::StartsWith(codec_name, "OMX.google.",
265 base::CompareCase::SENSITIVE) ||
266 base::StartsWith(codec_name, "OMX.SEC.",
267 base::CompareCase::SENSITIVE) ||
268 base::StartsWith(codec_name, "OMX.MTK.",
269 base::CompareCase::SENSITIVE) ||
270 base::StartsWith(codec_name, "OMX.Exynos.",
271 base::CompareCase::SENSITIVE));
273 return true;
276 MediaCodecBridge::MediaCodecBridge(const std::string& mime,
277 bool is_secure,
278 MediaCodecDirection direction) {
279 JNIEnv* env = AttachCurrentThread();
280 CHECK(env);
281 DCHECK(!mime.empty());
282 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
283 j_media_codec_.Reset(
284 Java_MediaCodecBridge_create(env, j_mime.obj(), is_secure, direction));
287 MediaCodecBridge::~MediaCodecBridge() {
288 JNIEnv* env = AttachCurrentThread();
289 CHECK(env);
290 if (j_media_codec_.obj())
291 Java_MediaCodecBridge_release(env, j_media_codec_.obj());
294 bool MediaCodecBridge::StartInternal() {
295 JNIEnv* env = AttachCurrentThread();
296 return Java_MediaCodecBridge_start(env, j_media_codec_.obj());
299 MediaCodecStatus MediaCodecBridge::Reset() {
300 JNIEnv* env = AttachCurrentThread();
301 return static_cast<MediaCodecStatus>(
302 Java_MediaCodecBridge_flush(env, j_media_codec_.obj()));
305 void MediaCodecBridge::Stop() {
306 JNIEnv* env = AttachCurrentThread();
307 Java_MediaCodecBridge_stop(env, j_media_codec_.obj());
310 void MediaCodecBridge::GetOutputFormat(int* width, int* height) {
311 JNIEnv* env = AttachCurrentThread();
313 *width = Java_MediaCodecBridge_getOutputWidth(env, j_media_codec_.obj());
314 *height = Java_MediaCodecBridge_getOutputHeight(env, j_media_codec_.obj());
317 int MediaCodecBridge::GetOutputSamplingRate() {
318 JNIEnv* env = AttachCurrentThread();
320 return Java_MediaCodecBridge_getOutputSamplingRate(env, j_media_codec_.obj());
323 MediaCodecStatus MediaCodecBridge::QueueInputBuffer(
324 int index,
325 const uint8* data,
326 size_t data_size,
327 const base::TimeDelta& presentation_time) {
328 DVLOG(3) << __PRETTY_FUNCTION__ << index << ": " << data_size;
329 if (data_size > base::checked_cast<size_t>(kint32max))
330 return MEDIA_CODEC_ERROR;
331 if (data && !FillInputBuffer(index, data, data_size))
332 return MEDIA_CODEC_ERROR;
333 JNIEnv* env = AttachCurrentThread();
334 return static_cast<MediaCodecStatus>(
335 Java_MediaCodecBridge_queueInputBuffer(env,
336 j_media_codec_.obj(),
337 index,
339 data_size,
340 presentation_time.InMicroseconds(),
341 0));
344 MediaCodecStatus MediaCodecBridge::QueueSecureInputBuffer(
345 int index,
346 const uint8* data,
347 size_t data_size,
348 const uint8* key_id,
349 int key_id_size,
350 const uint8* iv,
351 int iv_size,
352 const SubsampleEntry* subsamples,
353 int subsamples_size,
354 const base::TimeDelta& presentation_time) {
355 DVLOG(3) << __PRETTY_FUNCTION__ << index << ": " << data_size;
356 if (data_size > base::checked_cast<size_t>(kint32max))
357 return MEDIA_CODEC_ERROR;
358 if (data && !FillInputBuffer(index, data, data_size))
359 return MEDIA_CODEC_ERROR;
361 JNIEnv* env = AttachCurrentThread();
362 ScopedJavaLocalRef<jbyteArray> j_key_id =
363 base::android::ToJavaByteArray(env, key_id, key_id_size);
364 ScopedJavaLocalRef<jbyteArray> j_iv =
365 base::android::ToJavaByteArray(env, iv, iv_size);
367 // MediaCodec.CryptoInfo documentations says passing NULL for |clear_array|
368 // to indicate that all data is encrypted. But it doesn't specify what
369 // |cypher_array| and |subsamples_size| should be in that case. Passing
370 // one subsample here just to be on the safe side.
371 int new_subsamples_size = subsamples_size == 0 ? 1 : subsamples_size;
373 scoped_ptr<jint[]> native_clear_array(new jint[new_subsamples_size]);
374 scoped_ptr<jint[]> native_cypher_array(new jint[new_subsamples_size]);
376 if (subsamples_size == 0) {
377 DCHECK(!subsamples);
378 native_clear_array[0] = 0;
379 native_cypher_array[0] = data_size;
380 } else {
381 DCHECK_GT(subsamples_size, 0);
382 DCHECK(subsamples);
383 for (int i = 0; i < subsamples_size; ++i) {
384 DCHECK(subsamples[i].clear_bytes <= std::numeric_limits<uint16>::max());
385 if (subsamples[i].cypher_bytes >
386 static_cast<uint32>(std::numeric_limits<jint>::max())) {
387 return MEDIA_CODEC_ERROR;
390 native_clear_array[i] = subsamples[i].clear_bytes;
391 native_cypher_array[i] = subsamples[i].cypher_bytes;
395 ScopedJavaLocalRef<jintArray> clear_array =
396 ToJavaIntArray(env, native_clear_array.Pass(), new_subsamples_size);
397 ScopedJavaLocalRef<jintArray> cypher_array =
398 ToJavaIntArray(env, native_cypher_array.Pass(), new_subsamples_size);
400 return static_cast<MediaCodecStatus>(
401 Java_MediaCodecBridge_queueSecureInputBuffer(
402 env,
403 j_media_codec_.obj(),
404 index,
406 j_iv.obj(),
407 j_key_id.obj(),
408 clear_array.obj(),
409 cypher_array.obj(),
410 new_subsamples_size,
411 presentation_time.InMicroseconds()));
414 void MediaCodecBridge::QueueEOS(int input_buffer_index) {
415 DVLOG(3) << __PRETTY_FUNCTION__ << ": " << input_buffer_index;
416 JNIEnv* env = AttachCurrentThread();
417 Java_MediaCodecBridge_queueInputBuffer(env,
418 j_media_codec_.obj(),
419 input_buffer_index,
423 kBufferFlagEndOfStream);
426 MediaCodecStatus MediaCodecBridge::DequeueInputBuffer(
427 const base::TimeDelta& timeout,
428 int* index) {
429 JNIEnv* env = AttachCurrentThread();
430 ScopedJavaLocalRef<jobject> result = Java_MediaCodecBridge_dequeueInputBuffer(
431 env, j_media_codec_.obj(), timeout.InMicroseconds());
432 *index = Java_DequeueInputResult_index(env, result.obj());
433 MediaCodecStatus status = static_cast<MediaCodecStatus>(
434 Java_DequeueInputResult_status(env, result.obj()));
435 DVLOG(3) << __PRETTY_FUNCTION__ << ": status: " << status
436 << ", index: " << *index;
437 return status;
440 MediaCodecStatus MediaCodecBridge::DequeueOutputBuffer(
441 const base::TimeDelta& timeout,
442 int* index,
443 size_t* offset,
444 size_t* size,
445 base::TimeDelta* presentation_time,
446 bool* end_of_stream,
447 bool* key_frame) {
448 JNIEnv* env = AttachCurrentThread();
449 ScopedJavaLocalRef<jobject> result =
450 Java_MediaCodecBridge_dequeueOutputBuffer(
451 env, j_media_codec_.obj(), timeout.InMicroseconds());
452 *index = Java_DequeueOutputResult_index(env, result.obj());
453 *offset = base::checked_cast<size_t>(
454 Java_DequeueOutputResult_offset(env, result.obj()));
455 *size = base::checked_cast<size_t>(
456 Java_DequeueOutputResult_numBytes(env, result.obj()));
457 if (presentation_time) {
458 *presentation_time = base::TimeDelta::FromMicroseconds(
459 Java_DequeueOutputResult_presentationTimeMicroseconds(env,
460 result.obj()));
462 int flags = Java_DequeueOutputResult_flags(env, result.obj());
463 if (end_of_stream)
464 *end_of_stream = flags & kBufferFlagEndOfStream;
465 if (key_frame)
466 *key_frame = flags & kBufferFlagSyncFrame;
467 MediaCodecStatus status = static_cast<MediaCodecStatus>(
468 Java_DequeueOutputResult_status(env, result.obj()));
469 DVLOG(3) << __PRETTY_FUNCTION__ << ": status: " << status
470 << ", index: " << *index << ", offset: " << *offset
471 << ", size: " << *size << ", flags: " << flags;
472 return status;
475 void MediaCodecBridge::ReleaseOutputBuffer(int index, bool render) {
476 DVLOG(3) << __PRETTY_FUNCTION__ << ": " << index;
477 JNIEnv* env = AttachCurrentThread();
478 CHECK(env);
480 Java_MediaCodecBridge_releaseOutputBuffer(
481 env, j_media_codec_.obj(), index, render);
484 int MediaCodecBridge::GetOutputBuffersCount() {
485 JNIEnv* env = AttachCurrentThread();
486 return Java_MediaCodecBridge_getOutputBuffersCount(env, j_media_codec_.obj());
489 size_t MediaCodecBridge::GetOutputBuffersCapacity() {
490 JNIEnv* env = AttachCurrentThread();
491 return Java_MediaCodecBridge_getOutputBuffersCapacity(env,
492 j_media_codec_.obj());
495 void MediaCodecBridge::GetInputBuffer(int input_buffer_index,
496 uint8** data,
497 size_t* capacity) {
498 JNIEnv* env = AttachCurrentThread();
499 ScopedJavaLocalRef<jobject> j_buffer(Java_MediaCodecBridge_getInputBuffer(
500 env, j_media_codec_.obj(), input_buffer_index));
501 *data = static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj()));
502 *capacity = base::checked_cast<size_t>(
503 env->GetDirectBufferCapacity(j_buffer.obj()));
506 bool MediaCodecBridge::CopyFromOutputBuffer(int index,
507 size_t offset,
508 void* dst,
509 int dst_size) {
510 void* src_data = nullptr;
511 int src_capacity = GetOutputBufferAddress(index, offset, &src_data);
512 if (src_capacity < dst_size)
513 return false;
514 memcpy(dst, src_data, dst_size);
515 return true;
518 int MediaCodecBridge::GetOutputBufferAddress(int index,
519 size_t offset,
520 void** addr) {
521 JNIEnv* env = AttachCurrentThread();
522 ScopedJavaLocalRef<jobject> j_buffer(
523 Java_MediaCodecBridge_getOutputBuffer(env, j_media_codec_.obj(), index));
524 *addr = reinterpret_cast<uint8*>(
525 env->GetDirectBufferAddress(j_buffer.obj())) + offset;
526 return env->GetDirectBufferCapacity(j_buffer.obj()) - offset;
529 bool MediaCodecBridge::FillInputBuffer(int index,
530 const uint8* data,
531 size_t size) {
532 uint8* dst = NULL;
533 size_t capacity = 0;
534 GetInputBuffer(index, &dst, &capacity);
535 CHECK(dst);
537 if (size > capacity) {
538 LOG(ERROR) << "Input buffer size " << size
539 << " exceeds MediaCodec input buffer capacity: " << capacity;
540 return false;
543 memcpy(dst, data, size);
544 return true;
547 AudioCodecBridge::AudioCodecBridge(const std::string& mime)
548 // Audio codec doesn't care about security level and there is no need for
549 // audio encoding yet.
550 : MediaCodecBridge(mime, false, MEDIA_CODEC_DECODER) {}
552 bool AudioCodecBridge::Start(const AudioCodec& codec,
553 int sample_rate,
554 int channel_count,
555 const uint8* extra_data,
556 size_t extra_data_size,
557 int64 codec_delay_ns,
558 int64 seek_preroll_ns,
559 bool play_audio,
560 jobject media_crypto) {
561 JNIEnv* env = AttachCurrentThread();
563 if (!media_codec())
564 return false;
566 std::string codec_string = AudioCodecToAndroidMimeType(codec);
567 if (codec_string.empty())
568 return false;
570 ScopedJavaLocalRef<jstring> j_mime =
571 ConvertUTF8ToJavaString(env, codec_string);
572 ScopedJavaLocalRef<jobject> j_format(Java_MediaCodecBridge_createAudioFormat(
573 env, j_mime.obj(), sample_rate, channel_count));
574 DCHECK(!j_format.is_null());
576 if (!ConfigureMediaFormat(j_format.obj(), codec, extra_data, extra_data_size,
577 codec_delay_ns, seek_preroll_ns)) {
578 return false;
581 if (!Java_MediaCodecBridge_configureAudio(
582 env, media_codec(), j_format.obj(), media_crypto, 0, play_audio)) {
583 return false;
586 return StartInternal();
589 bool AudioCodecBridge::ConfigureMediaFormat(jobject j_format,
590 const AudioCodec& codec,
591 const uint8* extra_data,
592 size_t extra_data_size,
593 int64 codec_delay_ns,
594 int64 seek_preroll_ns) {
595 if (extra_data_size == 0 && codec != kCodecOpus)
596 return true;
598 JNIEnv* env = AttachCurrentThread();
599 switch (codec) {
600 case kCodecVorbis: {
601 if (extra_data[0] != 2) {
602 LOG(ERROR) << "Invalid number of vorbis headers before the codec "
603 << "header: " << extra_data[0];
604 return false;
607 size_t header_length[2];
608 // |total_length| keeps track of the total number of bytes before the last
609 // header.
610 size_t total_length = 1;
611 const uint8* current_pos = extra_data;
612 // Calculate the length of the first 2 headers.
613 for (int i = 0; i < 2; ++i) {
614 header_length[i] = 0;
615 while (total_length < extra_data_size) {
616 size_t size = *(++current_pos);
617 total_length += 1 + size;
618 if (total_length > 0x80000000) {
619 LOG(ERROR) << "Vorbis header size too large";
620 return false;
622 header_length[i] += size;
623 if (size < 0xFF)
624 break;
626 if (total_length >= extra_data_size) {
627 LOG(ERROR) << "Invalid vorbis header size in the extra data";
628 return false;
631 current_pos++;
632 // The first header is identification header.
633 ScopedJavaLocalRef<jbyteArray> first_header =
634 base::android::ToJavaByteArray(env, current_pos, header_length[0]);
635 Java_MediaCodecBridge_setCodecSpecificData(
636 env, j_format, 0, first_header.obj());
637 // The last header is codec header.
638 ScopedJavaLocalRef<jbyteArray> last_header =
639 base::android::ToJavaByteArray(
640 env, extra_data + total_length, extra_data_size - total_length);
641 Java_MediaCodecBridge_setCodecSpecificData(
642 env, j_format, 1, last_header.obj());
643 break;
645 case kCodecAAC: {
646 media::BitReader reader(extra_data, extra_data_size);
648 // The following code is copied from aac.cc
649 // TODO(qinmin): refactor the code in aac.cc to make it more reusable.
650 uint8 profile = 0;
651 uint8 frequency_index = 0;
652 uint8 channel_config = 0;
653 RETURN_ON_ERROR(reader.ReadBits(5, &profile));
654 RETURN_ON_ERROR(reader.ReadBits(4, &frequency_index));
656 if (0xf == frequency_index)
657 RETURN_ON_ERROR(reader.SkipBits(24));
658 RETURN_ON_ERROR(reader.ReadBits(4, &channel_config));
660 if (profile == 5 || profile == 29) {
661 // Read extension config.
662 RETURN_ON_ERROR(reader.ReadBits(4, &frequency_index));
663 if (frequency_index == 0xf)
664 RETURN_ON_ERROR(reader.SkipBits(24));
665 RETURN_ON_ERROR(reader.ReadBits(5, &profile));
668 if (profile < 1 || profile > 4 || frequency_index == 0xf ||
669 channel_config > 7) {
670 LOG(ERROR) << "Invalid AAC header";
671 return false;
673 const size_t kCsdLength = 2;
674 uint8 csd[kCsdLength];
675 csd[0] = profile << 3 | frequency_index >> 1;
676 csd[1] = (frequency_index & 0x01) << 7 | channel_config << 3;
677 ScopedJavaLocalRef<jbyteArray> byte_array =
678 base::android::ToJavaByteArray(env, csd, kCsdLength);
679 Java_MediaCodecBridge_setCodecSpecificData(
680 env, j_format, 0, byte_array.obj());
682 // TODO(qinmin): pass an extra variable to this function to determine
683 // whether we need to call this.
684 Java_MediaCodecBridge_setFrameHasADTSHeader(env, j_format);
685 break;
687 case kCodecOpus: {
688 if (!extra_data || extra_data_size == 0 ||
689 codec_delay_ns < 0 || seek_preroll_ns < 0) {
690 LOG(ERROR) << "Invalid Opus Header";
691 return false;
694 // csd0 - Opus Header
695 ScopedJavaLocalRef<jbyteArray> csd0 =
696 base::android::ToJavaByteArray(env, extra_data, extra_data_size);
697 Java_MediaCodecBridge_setCodecSpecificData(env, j_format, 0, csd0.obj());
699 // csd1 - Codec Delay
700 ScopedJavaLocalRef<jbyteArray> csd1 =
701 base::android::ToJavaByteArray(
702 env, reinterpret_cast<const uint8*>(&codec_delay_ns),
703 sizeof(int64_t));
704 Java_MediaCodecBridge_setCodecSpecificData(env, j_format, 1, csd1.obj());
706 // csd2 - Seek Preroll
707 ScopedJavaLocalRef<jbyteArray> csd2 =
708 base::android::ToJavaByteArray(
709 env, reinterpret_cast<const uint8*>(&seek_preroll_ns),
710 sizeof(int64_t));
711 Java_MediaCodecBridge_setCodecSpecificData(env, j_format, 2, csd2.obj());
712 break;
714 default:
715 LOG(ERROR) << "Invalid header encountered for codec: "
716 << AudioCodecToAndroidMimeType(codec);
717 return false;
719 return true;
722 int64 AudioCodecBridge::PlayOutputBuffer(int index,
723 size_t size,
724 size_t offset,
725 bool postpone) {
726 DCHECK_LE(0, index);
727 int numBytes = base::checked_cast<int>(size);
729 void* buffer = nullptr;
730 int capacity = GetOutputBufferAddress(index, offset, &buffer);
731 numBytes = std::min(capacity, numBytes);
732 CHECK_GE(numBytes, 0);
734 JNIEnv* env = AttachCurrentThread();
735 ScopedJavaLocalRef<jbyteArray> byte_array = base::android::ToJavaByteArray(
736 env, static_cast<uint8*>(buffer), numBytes);
737 return Java_MediaCodecBridge_playOutputBuffer(env, media_codec(),
738 byte_array.obj(), postpone);
741 void AudioCodecBridge::SetVolume(double volume) {
742 JNIEnv* env = AttachCurrentThread();
743 Java_MediaCodecBridge_setVolume(env, media_codec(), volume);
746 // static
747 AudioCodecBridge* AudioCodecBridge::Create(const AudioCodec& codec) {
748 if (!MediaCodecBridge::IsAvailable())
749 return NULL;
751 const std::string mime = AudioCodecToAndroidMimeType(codec);
752 return mime.empty() ? NULL : new AudioCodecBridge(mime);
755 // static
756 bool AudioCodecBridge::IsKnownUnaccelerated(const AudioCodec& codec) {
757 return MediaCodecBridge::IsKnownUnaccelerated(
758 AudioCodecToAndroidMimeType(codec), MEDIA_CODEC_DECODER);
761 // static
762 bool VideoCodecBridge::IsKnownUnaccelerated(const VideoCodec& codec,
763 MediaCodecDirection direction) {
764 return MediaCodecBridge::IsKnownUnaccelerated(
765 VideoCodecToAndroidMimeType(codec), direction);
768 // static
769 VideoCodecBridge* VideoCodecBridge::CreateDecoder(const VideoCodec& codec,
770 bool is_secure,
771 const gfx::Size& size,
772 jobject surface,
773 jobject media_crypto) {
774 if (!MediaCodecBridge::IsAvailable())
775 return NULL;
777 const std::string mime = VideoCodecToAndroidMimeType(codec);
778 if (mime.empty())
779 return NULL;
781 scoped_ptr<VideoCodecBridge> bridge(
782 new VideoCodecBridge(mime, is_secure, MEDIA_CODEC_DECODER));
783 if (!bridge->media_codec())
784 return NULL;
786 JNIEnv* env = AttachCurrentThread();
787 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
788 ScopedJavaLocalRef<jobject> j_format(
789 Java_MediaCodecBridge_createVideoDecoderFormat(
790 env, j_mime.obj(), size.width(), size.height()));
791 DCHECK(!j_format.is_null());
792 if (!Java_MediaCodecBridge_configureVideo(env,
793 bridge->media_codec(),
794 j_format.obj(),
795 surface,
796 media_crypto,
797 0)) {
798 return NULL;
801 return bridge->StartInternal() ? bridge.release() : NULL;
804 // static
805 VideoCodecBridge* VideoCodecBridge::CreateEncoder(const VideoCodec& codec,
806 const gfx::Size& size,
807 int bit_rate,
808 int frame_rate,
809 int i_frame_interval,
810 int color_format) {
811 if (!MediaCodecBridge::IsAvailable())
812 return NULL;
814 const std::string mime = VideoCodecToAndroidMimeType(codec);
815 if (mime.empty())
816 return NULL;
818 scoped_ptr<VideoCodecBridge> bridge(
819 new VideoCodecBridge(mime, false, MEDIA_CODEC_ENCODER));
820 if (!bridge->media_codec())
821 return NULL;
823 JNIEnv* env = AttachCurrentThread();
824 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
825 ScopedJavaLocalRef<jobject> j_format(
826 Java_MediaCodecBridge_createVideoEncoderFormat(env,
827 j_mime.obj(),
828 size.width(),
829 size.height(),
830 bit_rate,
831 frame_rate,
832 i_frame_interval,
833 color_format));
834 DCHECK(!j_format.is_null());
835 if (!Java_MediaCodecBridge_configureVideo(env,
836 bridge->media_codec(),
837 j_format.obj(),
838 NULL,
839 NULL,
840 kConfigureFlagEncode)) {
841 return NULL;
844 return bridge->StartInternal() ? bridge.release() : NULL;
847 VideoCodecBridge::VideoCodecBridge(const std::string& mime,
848 bool is_secure,
849 MediaCodecDirection direction)
850 : MediaCodecBridge(mime, is_secure, direction),
851 adaptive_playback_supported_for_testing_(-1) {}
853 void VideoCodecBridge::SetVideoBitrate(int bps) {
854 JNIEnv* env = AttachCurrentThread();
855 Java_MediaCodecBridge_setVideoBitrate(env, media_codec(), bps);
858 void VideoCodecBridge::RequestKeyFrameSoon() {
859 JNIEnv* env = AttachCurrentThread();
860 Java_MediaCodecBridge_requestKeyFrameSoon(env, media_codec());
863 bool VideoCodecBridge::IsAdaptivePlaybackSupported(int width, int height) {
864 if (adaptive_playback_supported_for_testing_ == 0)
865 return false;
866 else if (adaptive_playback_supported_for_testing_ > 0)
867 return true;
868 JNIEnv* env = AttachCurrentThread();
869 return Java_MediaCodecBridge_isAdaptivePlaybackSupported(
870 env, media_codec(), width, height);
873 bool MediaCodecBridge::RegisterMediaCodecBridge(JNIEnv* env) {
874 return RegisterNativesImpl(env);
877 } // namespace media