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"
10 #include "base/android/build_info.h"
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_array.h"
13 #include "base/android/jni_string.h"
14 #include "base/basictypes.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/numerics/safe_conversions.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "jni/MediaCodecBridge_jni.h"
21 #include "media/base/bit_reader.h"
22 #include "media/base/decrypt_config.h"
24 using base::android::AttachCurrentThread
;
25 using base::android::ConvertJavaStringToUTF8
;
26 using base::android::ConvertUTF8ToJavaString
;
27 using base::android::JavaIntArrayToIntVector
;
28 using base::android::ScopedJavaLocalRef
;
33 kBufferFlagSyncFrame
= 1, // BUFFER_FLAG_SYNC_FRAME
34 kBufferFlagEndOfStream
= 4, // BUFFER_FLAG_END_OF_STREAM
35 kConfigureFlagEncode
= 1, // CONFIGURE_FLAG_ENCODE
38 static const std::string
AudioCodecToAndroidMimeType(const AudioCodec
& codec
) {
43 return "audio/vorbis";
47 return "audio/mp4a-latm";
53 static const std::string
VideoCodecToAndroidMimeType(const VideoCodec
& codec
) {
58 return "video/x-vnd.on2.vp8";
60 return "video/x-vnd.on2.vp9";
66 static const std::string
CodecTypeToAndroidMimeType(const std::string
& codec
) {
67 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"?
71 return "audio/mp4a-latm";
72 if (codec
== "vp8" || codec
== "vp8.0")
73 return "video/x-vnd.on2.vp8";
74 if (codec
== "vp9" || codec
== "vp9.0")
75 return "video/x-vnd.on2.vp9";
76 if (codec
== "vorbis")
77 return "audio/vorbis";
83 // TODO(qinmin): using a map to help all the conversions in this class.
84 static const std::string
AndroidMimeTypeToCodecType(const std::string
& mime
) {
85 if (mime
== "video/mp4v-es")
87 if (mime
== "video/avc")
89 if (mime
== "video/x-vnd.on2.vp8")
91 if (mime
== "video/x-vnd.on2.vp9")
93 if (mime
== "audio/mp4a-latm")
95 if (mime
== "audio/mpeg")
97 if (mime
== "audio/vorbis")
99 if (mime
== "audio/opus")
101 return std::string();
104 static ScopedJavaLocalRef
<jintArray
>
105 ToJavaIntArray(JNIEnv
* env
, scoped_ptr
<jint
[]> native_array
, int size
) {
106 ScopedJavaLocalRef
<jintArray
> j_array(env
, env
->NewIntArray(size
));
107 env
->SetIntArrayRegion(j_array
.obj(), 0, size
, native_array
.get());
112 bool MediaCodecBridge::IsAvailable() {
113 // MediaCodec is only available on JB and greater.
114 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16)
116 // Blacklist some devices on Jellybean as for MediaCodec support is buggy.
117 // http://crbug.com/365494.
118 if (base::android::BuildInfo::GetInstance()->sdk_int() == 16) {
119 std::string
model(base::android::BuildInfo::GetInstance()->model());
120 return model
!= "GT-I9100" && model
!= "GT-I9300" && model
!= "GT-N7000";
126 bool MediaCodecBridge::SupportsSetParameters() {
127 // MediaCodec.setParameters() is only available starting with K.
128 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
132 bool MediaCodecBridge::SupportsGetName() {
133 // MediaCodec.getName() is only available on JB MR2 and greater.
134 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18;
138 std::vector
<MediaCodecBridge::CodecsInfo
> MediaCodecBridge::GetCodecsInfo() {
139 std::vector
<CodecsInfo
> codecs_info
;
143 JNIEnv
* env
= AttachCurrentThread();
144 std::string mime_type
;
145 ScopedJavaLocalRef
<jobjectArray
> j_codec_info_array
=
146 Java_MediaCodecBridge_getCodecsInfo(env
);
147 jsize len
= env
->GetArrayLength(j_codec_info_array
.obj());
148 for (jsize i
= 0; i
< len
; ++i
) {
149 ScopedJavaLocalRef
<jobject
> j_info(
150 env
, env
->GetObjectArrayElement(j_codec_info_array
.obj(), i
));
151 ScopedJavaLocalRef
<jstring
> j_codec_type
=
152 Java_CodecInfo_codecType(env
, j_info
.obj());
153 ConvertJavaStringToUTF8(env
, j_codec_type
.obj(), &mime_type
);
154 ScopedJavaLocalRef
<jstring
> j_codec_name
=
155 Java_CodecInfo_codecName(env
, j_info
.obj());
157 info
.codecs
= AndroidMimeTypeToCodecType(mime_type
);
158 ConvertJavaStringToUTF8(env
, j_codec_name
.obj(), &info
.name
);
159 info
.direction
= static_cast<MediaCodecDirection
>(
160 Java_CodecInfo_direction(env
, j_info
.obj()));
161 codecs_info
.push_back(info
);
167 std::string
MediaCodecBridge::GetDefaultCodecName(
168 const std::string
& mime_type
,
169 MediaCodecDirection direction
) {
171 return std::string();
173 JNIEnv
* env
= AttachCurrentThread();
174 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime_type
);
175 ScopedJavaLocalRef
<jstring
> j_codec_name
=
176 Java_MediaCodecBridge_getDefaultCodecName(env
, j_mime
.obj(), direction
);
177 return ConvertJavaStringToUTF8(env
, j_codec_name
.obj());
181 std::set
<int> MediaCodecBridge::GetEncoderColorFormats(
182 const std::string
& mime_type
) {
183 std::set
<int> color_formats
;
185 return color_formats
;
187 JNIEnv
* env
= AttachCurrentThread();
188 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime_type
);
189 ScopedJavaLocalRef
<jintArray
> j_color_format_array
=
190 Java_MediaCodecBridge_getEncoderColorFormatsForMime(env
, j_mime
.obj());
192 if (j_color_format_array
.obj()) {
193 std::vector
<int> formats
;
194 JavaIntArrayToIntVector(env
, j_color_format_array
.obj(), &formats
);
195 color_formats
= std::set
<int>(formats
.begin(), formats
.end());
198 return color_formats
;
202 bool MediaCodecBridge::CanDecode(const std::string
& codec
, bool is_secure
) {
206 JNIEnv
* env
= AttachCurrentThread();
207 std::string mime
= CodecTypeToAndroidMimeType(codec
);
210 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
211 ScopedJavaLocalRef
<jobject
> j_media_codec_bridge
=
212 Java_MediaCodecBridge_create(env
, j_mime
.obj(), is_secure
, false);
213 if (!j_media_codec_bridge
.is_null()) {
214 Java_MediaCodecBridge_release(env
, j_media_codec_bridge
.obj());
221 bool MediaCodecBridge::IsKnownUnaccelerated(const std::string
& mime_type
,
222 MediaCodecDirection direction
) {
226 std::string codec_name
;
227 if (SupportsGetName()) {
228 codec_name
= GetDefaultCodecName(mime_type
, direction
);
230 std::string codec_type
= AndroidMimeTypeToCodecType(mime_type
);
231 std::vector
<media::MediaCodecBridge::CodecsInfo
> codecs_info
=
232 MediaCodecBridge::GetCodecsInfo();
233 for (size_t i
= 0; i
< codecs_info
.size(); ++i
) {
234 if (codecs_info
[i
].codecs
== codec_type
&&
235 codecs_info
[i
].direction
== direction
) {
236 codec_name
= codecs_info
[i
].name
;
241 DVLOG(1) << __PRETTY_FUNCTION__
<< "Default codec for " << mime_type
<<
243 // It would be nice if MediaCodecInfo externalized some notion of
244 // HW-acceleration but it doesn't. Android Media guidance is that the
245 // "OMX.google" prefix is always used for SW decoders, so that's what we
246 // use. "OMX.SEC.*" codec is Samsung software implementation - report it
247 // as unaccelerated as well. Also temporary blacklist Exynos and MediaTek
248 // devices while HW decoder video freezes and distortions are
249 // investigated - http://crbug.com/446974.
250 if (codec_name
.length() > 0) {
251 return (StartsWithASCII(codec_name
, "OMX.google.", true) ||
252 StartsWithASCII(codec_name
, "OMX.SEC.", true) ||
253 StartsWithASCII(codec_name
, "OMX.MTK.", true) ||
254 StartsWithASCII(codec_name
, "OMX.Exynos.", true));
259 MediaCodecBridge::MediaCodecBridge(const std::string
& mime
,
261 MediaCodecDirection direction
) {
262 JNIEnv
* env
= AttachCurrentThread();
264 DCHECK(!mime
.empty());
265 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
266 j_media_codec_
.Reset(
267 Java_MediaCodecBridge_create(env
, j_mime
.obj(), is_secure
, direction
));
270 MediaCodecBridge::~MediaCodecBridge() {
271 JNIEnv
* env
= AttachCurrentThread();
273 if (j_media_codec_
.obj())
274 Java_MediaCodecBridge_release(env
, j_media_codec_
.obj());
277 bool MediaCodecBridge::StartInternal() {
278 JNIEnv
* env
= AttachCurrentThread();
279 return Java_MediaCodecBridge_start(env
, j_media_codec_
.obj());
282 MediaCodecStatus
MediaCodecBridge::Reset() {
283 JNIEnv
* env
= AttachCurrentThread();
284 return static_cast<MediaCodecStatus
>(
285 Java_MediaCodecBridge_flush(env
, j_media_codec_
.obj()));
288 void MediaCodecBridge::Stop() {
289 JNIEnv
* env
= AttachCurrentThread();
290 Java_MediaCodecBridge_stop(env
, j_media_codec_
.obj());
293 void MediaCodecBridge::GetOutputFormat(int* width
, int* height
) {
294 JNIEnv
* env
= AttachCurrentThread();
296 *width
= Java_MediaCodecBridge_getOutputWidth(env
, j_media_codec_
.obj());
297 *height
= Java_MediaCodecBridge_getOutputHeight(env
, j_media_codec_
.obj());
300 int MediaCodecBridge::GetOutputSamplingRate() {
301 JNIEnv
* env
= AttachCurrentThread();
303 return Java_MediaCodecBridge_getOutputSamplingRate(env
, j_media_codec_
.obj());
306 MediaCodecStatus
MediaCodecBridge::QueueInputBuffer(
310 const base::TimeDelta
& presentation_time
) {
311 DVLOG(3) << __PRETTY_FUNCTION__
<< index
<< ": " << data_size
;
312 if (data_size
> base::checked_cast
<size_t>(kint32max
))
313 return MEDIA_CODEC_ERROR
;
314 if (data
&& !FillInputBuffer(index
, data
, data_size
))
315 return MEDIA_CODEC_ERROR
;
316 JNIEnv
* env
= AttachCurrentThread();
317 return static_cast<MediaCodecStatus
>(
318 Java_MediaCodecBridge_queueInputBuffer(env
,
319 j_media_codec_
.obj(),
323 presentation_time
.InMicroseconds(),
327 MediaCodecStatus
MediaCodecBridge::QueueSecureInputBuffer(
335 const SubsampleEntry
* subsamples
,
337 const base::TimeDelta
& presentation_time
) {
338 DVLOG(3) << __PRETTY_FUNCTION__
<< index
<< ": " << data_size
;
339 if (data_size
> base::checked_cast
<size_t>(kint32max
))
340 return MEDIA_CODEC_ERROR
;
341 if (data
&& !FillInputBuffer(index
, data
, data_size
))
342 return MEDIA_CODEC_ERROR
;
344 JNIEnv
* env
= AttachCurrentThread();
345 ScopedJavaLocalRef
<jbyteArray
> j_key_id
=
346 base::android::ToJavaByteArray(env
, key_id
, key_id_size
);
347 ScopedJavaLocalRef
<jbyteArray
> j_iv
=
348 base::android::ToJavaByteArray(env
, iv
, iv_size
);
350 // MediaCodec.CryptoInfo documentations says passing NULL for |clear_array|
351 // to indicate that all data is encrypted. But it doesn't specify what
352 // |cypher_array| and |subsamples_size| should be in that case. Passing
353 // one subsample here just to be on the safe side.
354 int new_subsamples_size
= subsamples_size
== 0 ? 1 : subsamples_size
;
356 scoped_ptr
<jint
[]> native_clear_array(new jint
[new_subsamples_size
]);
357 scoped_ptr
<jint
[]> native_cypher_array(new jint
[new_subsamples_size
]);
359 if (subsamples_size
== 0) {
361 native_clear_array
[0] = 0;
362 native_cypher_array
[0] = data_size
;
364 DCHECK_GT(subsamples_size
, 0);
366 for (int i
= 0; i
< subsamples_size
; ++i
) {
367 DCHECK(subsamples
[i
].clear_bytes
<= std::numeric_limits
<uint16
>::max());
368 if (subsamples
[i
].cypher_bytes
>
369 static_cast<uint32
>(std::numeric_limits
<jint
>::max())) {
370 return MEDIA_CODEC_ERROR
;
373 native_clear_array
[i
] = subsamples
[i
].clear_bytes
;
374 native_cypher_array
[i
] = subsamples
[i
].cypher_bytes
;
378 ScopedJavaLocalRef
<jintArray
> clear_array
=
379 ToJavaIntArray(env
, native_clear_array
.Pass(), new_subsamples_size
);
380 ScopedJavaLocalRef
<jintArray
> cypher_array
=
381 ToJavaIntArray(env
, native_cypher_array
.Pass(), new_subsamples_size
);
383 return static_cast<MediaCodecStatus
>(
384 Java_MediaCodecBridge_queueSecureInputBuffer(
386 j_media_codec_
.obj(),
394 presentation_time
.InMicroseconds()));
397 void MediaCodecBridge::QueueEOS(int input_buffer_index
) {
398 DVLOG(3) << __PRETTY_FUNCTION__
<< ": " << input_buffer_index
;
399 JNIEnv
* env
= AttachCurrentThread();
400 Java_MediaCodecBridge_queueInputBuffer(env
,
401 j_media_codec_
.obj(),
406 kBufferFlagEndOfStream
);
409 MediaCodecStatus
MediaCodecBridge::DequeueInputBuffer(
410 const base::TimeDelta
& timeout
,
412 JNIEnv
* env
= AttachCurrentThread();
413 ScopedJavaLocalRef
<jobject
> result
= Java_MediaCodecBridge_dequeueInputBuffer(
414 env
, j_media_codec_
.obj(), timeout
.InMicroseconds());
415 *index
= Java_DequeueInputResult_index(env
, result
.obj());
416 MediaCodecStatus status
= static_cast<MediaCodecStatus
>(
417 Java_DequeueInputResult_status(env
, result
.obj()));
418 DVLOG(3) << __PRETTY_FUNCTION__
<< ": status: " << status
419 << ", index: " << *index
;
423 MediaCodecStatus
MediaCodecBridge::DequeueOutputBuffer(
424 const base::TimeDelta
& timeout
,
428 base::TimeDelta
* presentation_time
,
431 JNIEnv
* env
= AttachCurrentThread();
432 ScopedJavaLocalRef
<jobject
> result
=
433 Java_MediaCodecBridge_dequeueOutputBuffer(
434 env
, j_media_codec_
.obj(), timeout
.InMicroseconds());
435 *index
= Java_DequeueOutputResult_index(env
, result
.obj());
436 *offset
= base::checked_cast
<size_t>(
437 Java_DequeueOutputResult_offset(env
, result
.obj()));
438 *size
= base::checked_cast
<size_t>(
439 Java_DequeueOutputResult_numBytes(env
, result
.obj()));
440 if (presentation_time
) {
441 *presentation_time
= base::TimeDelta::FromMicroseconds(
442 Java_DequeueOutputResult_presentationTimeMicroseconds(env
,
445 int flags
= Java_DequeueOutputResult_flags(env
, result
.obj());
447 *end_of_stream
= flags
& kBufferFlagEndOfStream
;
449 *key_frame
= flags
& kBufferFlagSyncFrame
;
450 MediaCodecStatus status
= static_cast<MediaCodecStatus
>(
451 Java_DequeueOutputResult_status(env
, result
.obj()));
452 DVLOG(3) << __PRETTY_FUNCTION__
<< ": status: " << status
453 << ", index: " << *index
<< ", offset: " << *offset
454 << ", size: " << *size
<< ", flags: " << flags
;
458 void MediaCodecBridge::ReleaseOutputBuffer(int index
, bool render
) {
459 DVLOG(3) << __PRETTY_FUNCTION__
<< ": " << index
;
460 JNIEnv
* env
= AttachCurrentThread();
463 Java_MediaCodecBridge_releaseOutputBuffer(
464 env
, j_media_codec_
.obj(), index
, render
);
467 int MediaCodecBridge::GetOutputBuffersCount() {
468 JNIEnv
* env
= AttachCurrentThread();
469 return Java_MediaCodecBridge_getOutputBuffersCount(env
, j_media_codec_
.obj());
472 size_t MediaCodecBridge::GetOutputBuffersCapacity() {
473 JNIEnv
* env
= AttachCurrentThread();
474 return Java_MediaCodecBridge_getOutputBuffersCapacity(env
,
475 j_media_codec_
.obj());
478 void MediaCodecBridge::GetInputBuffer(int input_buffer_index
,
481 JNIEnv
* env
= AttachCurrentThread();
482 ScopedJavaLocalRef
<jobject
> j_buffer(Java_MediaCodecBridge_getInputBuffer(
483 env
, j_media_codec_
.obj(), input_buffer_index
));
484 *data
= static_cast<uint8
*>(env
->GetDirectBufferAddress(j_buffer
.obj()));
485 *capacity
= base::checked_cast
<size_t>(
486 env
->GetDirectBufferCapacity(j_buffer
.obj()));
489 bool MediaCodecBridge::CopyFromOutputBuffer(int index
,
493 JNIEnv
* env
= AttachCurrentThread();
494 ScopedJavaLocalRef
<jobject
> j_buffer(
495 Java_MediaCodecBridge_getOutputBuffer(env
, j_media_codec_
.obj(), index
));
497 reinterpret_cast<uint8
*>(env
->GetDirectBufferAddress(j_buffer
.obj())) +
499 int src_capacity
= env
->GetDirectBufferCapacity(j_buffer
.obj()) - offset
;
500 if (src_capacity
< dst_size
)
502 memcpy(dst
, src_data
, dst_size
);
506 bool MediaCodecBridge::FillInputBuffer(int index
,
511 GetInputBuffer(index
, &dst
, &capacity
);
514 if (size
> capacity
) {
515 LOG(ERROR
) << "Input buffer size " << size
516 << " exceeds MediaCodec input buffer capacity: " << capacity
;
520 memcpy(dst
, data
, size
);
524 AudioCodecBridge::AudioCodecBridge(const std::string
& mime
)
525 // Audio codec doesn't care about security level and there is no need for
526 // audio encoding yet.
527 : MediaCodecBridge(mime
, false, MEDIA_CODEC_DECODER
) {}
529 bool AudioCodecBridge::Start(const AudioCodec
& codec
,
532 const uint8
* extra_data
,
533 size_t extra_data_size
,
534 int64 codec_delay_ns
,
535 int64 seek_preroll_ns
,
537 jobject media_crypto
) {
538 JNIEnv
* env
= AttachCurrentThread();
543 std::string codec_string
= AudioCodecToAndroidMimeType(codec
);
544 if (codec_string
.empty())
547 ScopedJavaLocalRef
<jstring
> j_mime
=
548 ConvertUTF8ToJavaString(env
, codec_string
);
549 ScopedJavaLocalRef
<jobject
> j_format(Java_MediaCodecBridge_createAudioFormat(
550 env
, j_mime
.obj(), sample_rate
, channel_count
));
551 DCHECK(!j_format
.is_null());
553 if (!ConfigureMediaFormat(j_format
.obj(), codec
, extra_data
, extra_data_size
,
554 codec_delay_ns
, seek_preroll_ns
)) {
558 if (!Java_MediaCodecBridge_configureAudio(
559 env
, media_codec(), j_format
.obj(), media_crypto
, 0, play_audio
)) {
563 return StartInternal();
566 bool AudioCodecBridge::ConfigureMediaFormat(jobject j_format
,
567 const AudioCodec
& codec
,
568 const uint8
* extra_data
,
569 size_t extra_data_size
,
570 int64 codec_delay_ns
,
571 int64 seek_preroll_ns
) {
572 if (extra_data_size
== 0 && codec
!= kCodecOpus
)
575 JNIEnv
* env
= AttachCurrentThread();
578 if (extra_data
[0] != 2) {
579 LOG(ERROR
) << "Invalid number of vorbis headers before the codec "
580 << "header: " << extra_data
[0];
584 size_t header_length
[2];
585 // |total_length| keeps track of the total number of bytes before the last
587 size_t total_length
= 1;
588 const uint8
* current_pos
= extra_data
;
589 // Calculate the length of the first 2 headers.
590 for (int i
= 0; i
< 2; ++i
) {
591 header_length
[i
] = 0;
592 while (total_length
< extra_data_size
) {
593 size_t size
= *(++current_pos
);
594 total_length
+= 1 + size
;
595 if (total_length
> 0x80000000) {
596 LOG(ERROR
) << "Vorbis header size too large";
599 header_length
[i
] += size
;
603 if (total_length
>= extra_data_size
) {
604 LOG(ERROR
) << "Invalid vorbis header size in the extra data";
609 // The first header is identification header.
610 ScopedJavaLocalRef
<jbyteArray
> first_header
=
611 base::android::ToJavaByteArray(env
, current_pos
, header_length
[0]);
612 Java_MediaCodecBridge_setCodecSpecificData(
613 env
, j_format
, 0, first_header
.obj());
614 // The last header is codec header.
615 ScopedJavaLocalRef
<jbyteArray
> last_header
=
616 base::android::ToJavaByteArray(
617 env
, extra_data
+ total_length
, extra_data_size
- total_length
);
618 Java_MediaCodecBridge_setCodecSpecificData(
619 env
, j_format
, 1, last_header
.obj());
623 media::BitReader
reader(extra_data
, extra_data_size
);
625 // The following code is copied from aac.cc
626 // TODO(qinmin): refactor the code in aac.cc to make it more reusable.
628 uint8 frequency_index
= 0;
629 uint8 channel_config
= 0;
630 if (!reader
.ReadBits(5, &profile
) ||
631 !reader
.ReadBits(4, &frequency_index
)) {
632 LOG(ERROR
) << "Unable to parse AAC header";
635 if (0xf == frequency_index
&& !reader
.SkipBits(24)) {
636 LOG(ERROR
) << "Unable to parse AAC header";
639 if (!reader
.ReadBits(4, &channel_config
)) {
640 LOG(ERROR
) << "Unable to parse AAC header";
644 if (profile
< 1 || profile
> 4 || frequency_index
== 0xf ||
645 channel_config
> 7) {
646 LOG(ERROR
) << "Invalid AAC header";
649 const size_t kCsdLength
= 2;
650 uint8 csd
[kCsdLength
];
651 csd
[0] = profile
<< 3 | frequency_index
>> 1;
652 csd
[1] = (frequency_index
& 0x01) << 7 | channel_config
<< 3;
653 ScopedJavaLocalRef
<jbyteArray
> byte_array
=
654 base::android::ToJavaByteArray(env
, csd
, kCsdLength
);
655 Java_MediaCodecBridge_setCodecSpecificData(
656 env
, j_format
, 0, byte_array
.obj());
658 // TODO(qinmin): pass an extra variable to this function to determine
659 // whether we need to call this.
660 Java_MediaCodecBridge_setFrameHasADTSHeader(env
, j_format
);
664 if (!extra_data
|| extra_data_size
== 0 ||
665 codec_delay_ns
< 0 || seek_preroll_ns
< 0) {
666 LOG(ERROR
) << "Invalid Opus Header";
670 // csd0 - Opus Header
671 ScopedJavaLocalRef
<jbyteArray
> csd0
=
672 base::android::ToJavaByteArray(env
, extra_data
, extra_data_size
);
673 Java_MediaCodecBridge_setCodecSpecificData(env
, j_format
, 0, csd0
.obj());
675 // csd1 - Codec Delay
676 ScopedJavaLocalRef
<jbyteArray
> csd1
=
677 base::android::ToJavaByteArray(
678 env
, reinterpret_cast<const uint8
*>(&codec_delay_ns
),
680 Java_MediaCodecBridge_setCodecSpecificData(env
, j_format
, 1, csd1
.obj());
682 // csd2 - Seek Preroll
683 ScopedJavaLocalRef
<jbyteArray
> csd2
=
684 base::android::ToJavaByteArray(
685 env
, reinterpret_cast<const uint8
*>(&seek_preroll_ns
),
687 Java_MediaCodecBridge_setCodecSpecificData(env
, j_format
, 2, csd2
.obj());
691 LOG(ERROR
) << "Invalid header encountered for codec: "
692 << AudioCodecToAndroidMimeType(codec
);
698 int64
AudioCodecBridge::PlayOutputBuffer(int index
, size_t size
) {
700 int numBytes
= base::checked_cast
<int>(size
);
701 JNIEnv
* env
= AttachCurrentThread();
702 ScopedJavaLocalRef
<jobject
> buf
=
703 Java_MediaCodecBridge_getOutputBuffer(env
, media_codec(), index
);
704 uint8
* buffer
= static_cast<uint8
*>(env
->GetDirectBufferAddress(buf
.obj()));
706 ScopedJavaLocalRef
<jbyteArray
> byte_array
=
707 base::android::ToJavaByteArray(env
, buffer
, numBytes
);
708 return Java_MediaCodecBridge_playOutputBuffer(
709 env
, media_codec(), byte_array
.obj());
712 void AudioCodecBridge::SetVolume(double volume
) {
713 JNIEnv
* env
= AttachCurrentThread();
714 Java_MediaCodecBridge_setVolume(env
, media_codec(), volume
);
718 AudioCodecBridge
* AudioCodecBridge::Create(const AudioCodec
& codec
) {
719 if (!MediaCodecBridge::IsAvailable())
722 const std::string mime
= AudioCodecToAndroidMimeType(codec
);
723 return mime
.empty() ? NULL
: new AudioCodecBridge(mime
);
727 bool AudioCodecBridge::IsKnownUnaccelerated(const AudioCodec
& codec
) {
728 return MediaCodecBridge::IsKnownUnaccelerated(
729 AudioCodecToAndroidMimeType(codec
), MEDIA_CODEC_DECODER
);
733 bool VideoCodecBridge::IsKnownUnaccelerated(const VideoCodec
& codec
,
734 MediaCodecDirection direction
) {
735 return MediaCodecBridge::IsKnownUnaccelerated(
736 VideoCodecToAndroidMimeType(codec
), direction
);
740 VideoCodecBridge
* VideoCodecBridge::CreateDecoder(const VideoCodec
& codec
,
742 const gfx::Size
& size
,
744 jobject media_crypto
) {
745 if (!MediaCodecBridge::IsAvailable())
748 const std::string mime
= VideoCodecToAndroidMimeType(codec
);
752 scoped_ptr
<VideoCodecBridge
> bridge(
753 new VideoCodecBridge(mime
, is_secure
, MEDIA_CODEC_DECODER
));
754 if (!bridge
->media_codec())
757 JNIEnv
* env
= AttachCurrentThread();
758 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
759 ScopedJavaLocalRef
<jobject
> j_format(
760 Java_MediaCodecBridge_createVideoDecoderFormat(
761 env
, j_mime
.obj(), size
.width(), size
.height()));
762 DCHECK(!j_format
.is_null());
763 if (!Java_MediaCodecBridge_configureVideo(env
,
764 bridge
->media_codec(),
772 return bridge
->StartInternal() ? bridge
.release() : NULL
;
776 VideoCodecBridge
* VideoCodecBridge::CreateEncoder(const VideoCodec
& codec
,
777 const gfx::Size
& size
,
780 int i_frame_interval
,
782 if (!MediaCodecBridge::IsAvailable())
785 const std::string mime
= VideoCodecToAndroidMimeType(codec
);
789 scoped_ptr
<VideoCodecBridge
> bridge(
790 new VideoCodecBridge(mime
, false, MEDIA_CODEC_ENCODER
));
791 if (!bridge
->media_codec())
794 JNIEnv
* env
= AttachCurrentThread();
795 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
796 ScopedJavaLocalRef
<jobject
> j_format(
797 Java_MediaCodecBridge_createVideoEncoderFormat(env
,
805 DCHECK(!j_format
.is_null());
806 if (!Java_MediaCodecBridge_configureVideo(env
,
807 bridge
->media_codec(),
811 kConfigureFlagEncode
)) {
815 return bridge
->StartInternal() ? bridge
.release() : NULL
;
818 VideoCodecBridge::VideoCodecBridge(const std::string
& mime
,
820 MediaCodecDirection direction
)
821 : MediaCodecBridge(mime
, is_secure
, direction
),
822 adaptive_playback_supported_for_testing_(-1) {}
824 void VideoCodecBridge::SetVideoBitrate(int bps
) {
825 JNIEnv
* env
= AttachCurrentThread();
826 Java_MediaCodecBridge_setVideoBitrate(env
, media_codec(), bps
);
829 void VideoCodecBridge::RequestKeyFrameSoon() {
830 JNIEnv
* env
= AttachCurrentThread();
831 Java_MediaCodecBridge_requestKeyFrameSoon(env
, media_codec());
834 bool VideoCodecBridge::IsAdaptivePlaybackSupported(int width
, int height
) {
835 if (adaptive_playback_supported_for_testing_
== 0)
837 else if (adaptive_playback_supported_for_testing_
> 0)
839 JNIEnv
* env
= AttachCurrentThread();
840 return Java_MediaCodecBridge_isAdaptivePlaybackSupported(
841 env
, media_codec(), width
, height
);
844 bool MediaCodecBridge::RegisterMediaCodecBridge(JNIEnv
* env
) {
845 return RegisterNativesImpl(env
);