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"
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
;
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
) {
42 return "audio/vorbis";
46 return "audio/mp4a-latm";
52 static const std::string
VideoCodecToAndroidMimeType(const VideoCodec
& codec
) {
59 return "video/x-vnd.on2.vp8";
61 return "video/x-vnd.on2.vp9";
67 static const std::string
CodecTypeToAndroidMimeType(const std::string
& codec
) {
68 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"?
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";
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")
90 if (mime
== "video/avc")
92 if (mime
== "video/hevc")
94 if (mime
== "video/x-vnd.on2.vp8")
96 if (mime
== "video/x-vnd.on2.vp9")
98 if (mime
== "audio/mp4a-latm")
100 if (mime
== "audio/mpeg")
102 if (mime
== "audio/vorbis")
104 if (mime
== "audio/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());
117 bool MediaCodecBridge::IsAvailable() {
118 // MediaCodec is only available on JB and greater.
119 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16)
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";
131 bool MediaCodecBridge::SupportsSetParameters() {
132 // MediaCodec.setParameters() is only available starting with K.
133 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
137 bool MediaCodecBridge::SupportsGetName() {
138 // MediaCodec.getName() is only available on JB MR2 and greater.
139 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18;
143 std::vector
<MediaCodecBridge::CodecsInfo
> MediaCodecBridge::GetCodecsInfo() {
144 std::vector
<CodecsInfo
> 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());
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
);
172 std::string
MediaCodecBridge::GetDefaultCodecName(
173 const std::string
& mime_type
,
174 MediaCodecDirection direction
) {
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());
186 std::set
<int> MediaCodecBridge::GetEncoderColorFormats(
187 const std::string
& mime_type
) {
188 std::set
<int> color_formats
;
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
;
207 bool MediaCodecBridge::CanDecode(const std::string
& codec
, bool is_secure
) {
211 JNIEnv
* env
= AttachCurrentThread();
212 std::string mime
= CodecTypeToAndroidMimeType(codec
);
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());
226 bool MediaCodecBridge::IsKnownUnaccelerated(const std::string
& mime_type
,
227 MediaCodecDirection direction
) {
231 std::string codec_name
;
232 if (SupportsGetName()) {
233 codec_name
= GetDefaultCodecName(mime_type
, direction
);
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
;
246 DVLOG(1) << __PRETTY_FUNCTION__
<< "Default codec for " << mime_type
<<
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
));
268 MediaCodecBridge::MediaCodecBridge(const std::string
& mime
,
270 MediaCodecDirection direction
) {
271 JNIEnv
* env
= AttachCurrentThread();
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();
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(
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(),
332 presentation_time
.InMicroseconds(),
336 MediaCodecStatus
MediaCodecBridge::QueueSecureInputBuffer(
344 const SubsampleEntry
* subsamples
,
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) {
370 native_clear_array
[0] = 0;
371 native_cypher_array
[0] = data_size
;
373 DCHECK_GT(subsamples_size
, 0);
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(
395 j_media_codec_
.obj(),
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(),
415 kBufferFlagEndOfStream
);
418 MediaCodecStatus
MediaCodecBridge::DequeueInputBuffer(
419 const base::TimeDelta
& timeout
,
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
;
432 MediaCodecStatus
MediaCodecBridge::DequeueOutputBuffer(
433 const base::TimeDelta
& timeout
,
437 base::TimeDelta
* presentation_time
,
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
,
454 int flags
= Java_DequeueOutputResult_flags(env
, result
.obj());
456 *end_of_stream
= flags
& kBufferFlagEndOfStream
;
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
;
467 void MediaCodecBridge::ReleaseOutputBuffer(int index
, bool render
) {
468 DVLOG(3) << __PRETTY_FUNCTION__
<< ": " << index
;
469 JNIEnv
* env
= AttachCurrentThread();
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
,
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
,
502 void* src_data
= nullptr;
503 int src_capacity
= GetOutputBufferAddress(index
, offset
, &src_data
);
504 if (src_capacity
< dst_size
)
506 memcpy(dst
, src_data
, dst_size
);
510 int MediaCodecBridge::GetOutputBufferAddress(int index
,
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
,
526 GetInputBuffer(index
, &dst
, &capacity
);
529 if (size
> capacity
) {
530 LOG(ERROR
) << "Input buffer size " << size
531 << " exceeds MediaCodec input buffer capacity: " << capacity
;
535 memcpy(dst
, data
, size
);
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
,
547 const uint8
* extra_data
,
548 size_t extra_data_size
,
549 int64 codec_delay_ns
,
550 int64 seek_preroll_ns
,
552 jobject media_crypto
) {
553 JNIEnv
* env
= AttachCurrentThread();
558 std::string codec_string
= AudioCodecToAndroidMimeType(codec
);
559 if (codec_string
.empty())
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
)) {
573 if (!Java_MediaCodecBridge_configureAudio(
574 env
, media_codec(), j_format
.obj(), media_crypto
, 0, play_audio
)) {
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
)
590 JNIEnv
* env
= AttachCurrentThread();
593 if (extra_data
[0] != 2) {
594 LOG(ERROR
) << "Invalid number of vorbis headers before the codec "
595 << "header: " << extra_data
[0];
599 size_t header_length
[2];
600 // |total_length| keeps track of the total number of bytes before the last
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";
614 header_length
[i
] += size
;
618 if (total_length
>= extra_data_size
) {
619 LOG(ERROR
) << "Invalid vorbis header size in the extra data";
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());
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.
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";
650 if (0xf == frequency_index
&& !reader
.SkipBits(24)) {
651 LOG(ERROR
) << "Unable to parse AAC header";
654 if (!reader
.ReadBits(4, &channel_config
)) {
655 LOG(ERROR
) << "Unable to parse AAC header";
659 if (profile
< 1 || profile
> 4 || frequency_index
== 0xf ||
660 channel_config
> 7) {
661 LOG(ERROR
) << "Invalid AAC header";
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
);
679 if (!extra_data
|| extra_data_size
== 0 ||
680 codec_delay_ns
< 0 || seek_preroll_ns
< 0) {
681 LOG(ERROR
) << "Invalid Opus Header";
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
),
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
),
702 Java_MediaCodecBridge_setCodecSpecificData(env
, j_format
, 2, csd2
.obj());
706 LOG(ERROR
) << "Invalid header encountered for codec: "
707 << AudioCodecToAndroidMimeType(codec
);
713 int64
AudioCodecBridge::PlayOutputBuffer(int 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
);
738 AudioCodecBridge
* AudioCodecBridge::Create(const AudioCodec
& codec
) {
739 if (!MediaCodecBridge::IsAvailable())
742 const std::string mime
= AudioCodecToAndroidMimeType(codec
);
743 return mime
.empty() ? NULL
: new AudioCodecBridge(mime
);
747 bool AudioCodecBridge::IsKnownUnaccelerated(const AudioCodec
& codec
) {
748 return MediaCodecBridge::IsKnownUnaccelerated(
749 AudioCodecToAndroidMimeType(codec
), MEDIA_CODEC_DECODER
);
753 bool VideoCodecBridge::IsKnownUnaccelerated(const VideoCodec
& codec
,
754 MediaCodecDirection direction
) {
755 return MediaCodecBridge::IsKnownUnaccelerated(
756 VideoCodecToAndroidMimeType(codec
), direction
);
760 VideoCodecBridge
* VideoCodecBridge::CreateDecoder(const VideoCodec
& codec
,
762 const gfx::Size
& size
,
764 jobject media_crypto
) {
765 if (!MediaCodecBridge::IsAvailable())
768 const std::string mime
= VideoCodecToAndroidMimeType(codec
);
772 scoped_ptr
<VideoCodecBridge
> bridge(
773 new VideoCodecBridge(mime
, is_secure
, MEDIA_CODEC_DECODER
));
774 if (!bridge
->media_codec())
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(),
792 return bridge
->StartInternal() ? bridge
.release() : NULL
;
796 VideoCodecBridge
* VideoCodecBridge::CreateEncoder(const VideoCodec
& codec
,
797 const gfx::Size
& size
,
800 int i_frame_interval
,
802 if (!MediaCodecBridge::IsAvailable())
805 const std::string mime
= VideoCodecToAndroidMimeType(codec
);
809 scoped_ptr
<VideoCodecBridge
> bridge(
810 new VideoCodecBridge(mime
, false, MEDIA_CODEC_ENCODER
));
811 if (!bridge
->media_codec())
814 JNIEnv
* env
= AttachCurrentThread();
815 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
816 ScopedJavaLocalRef
<jobject
> j_format(
817 Java_MediaCodecBridge_createVideoEncoderFormat(env
,
825 DCHECK(!j_format
.is_null());
826 if (!Java_MediaCodecBridge_configureVideo(env
,
827 bridge
->media_codec(),
831 kConfigureFlagEncode
)) {
835 return bridge
->StartInternal() ? bridge
.release() : NULL
;
838 VideoCodecBridge::VideoCodecBridge(const std::string
& mime
,
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)
857 else if (adaptive_playback_supported_for_testing_
> 0)
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
);