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"
8 #include "base/android/build_info.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
12 #include "base/basictypes.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/numerics/safe_conversions.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "jni/MediaCodecBridge_jni.h"
19 #include "media/base/bit_reader.h"
20 #include "media/base/decrypt_config.h"
22 using base::android::AttachCurrentThread
;
23 using base::android::ConvertJavaStringToUTF8
;
24 using base::android::ConvertUTF8ToJavaString
;
25 using base::android::JavaIntArrayToIntVector
;
26 using base::android::ScopedJavaLocalRef
;
31 kBufferFlagSyncFrame
= 1, // BUFFER_FLAG_SYNC_FRAME
32 kBufferFlagEndOfStream
= 4, // BUFFER_FLAG_END_OF_STREAM
33 kConfigureFlagEncode
= 1, // CONFIGURE_FLAG_ENCODE
36 static const std::string
AudioCodecToAndroidMimeType(const AudioCodec
& codec
) {
41 return "audio/vorbis";
45 return "audio/mp4a-latm";
51 static const std::string
VideoCodecToAndroidMimeType(const VideoCodec
& codec
) {
56 return "video/x-vnd.on2.vp8";
58 return "video/x-vnd.on2.vp9";
64 static const std::string
CodecTypeToAndroidMimeType(const std::string
& codec
) {
65 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"?
69 return "audio/mp4a-latm";
70 if (codec
== "vp8" || codec
== "vp8.0")
71 return "video/x-vnd.on2.vp8";
72 if (codec
== "vp9" || codec
== "vp9.0")
73 return "video/x-vnd.on2.vp9";
74 if (codec
== "vorbis")
75 return "audio/vorbis";
81 // TODO(qinmin): using a map to help all the conversions in this class.
82 static const std::string
AndroidMimeTypeToCodecType(const std::string
& mime
) {
83 if (mime
== "video/mp4v-es")
85 if (mime
== "video/avc")
87 if (mime
== "video/x-vnd.on2.vp8")
89 if (mime
== "video/x-vnd.on2.vp9")
91 if (mime
== "audio/mp4a-latm")
93 if (mime
== "audio/mpeg")
95 if (mime
== "audio/vorbis")
97 if (mime
== "audio/opus")
102 static ScopedJavaLocalRef
<jintArray
>
103 ToJavaIntArray(JNIEnv
* env
, scoped_ptr
<jint
[]> native_array
, int size
) {
104 ScopedJavaLocalRef
<jintArray
> j_array(env
, env
->NewIntArray(size
));
105 env
->SetIntArrayRegion(j_array
.obj(), 0, size
, native_array
.get());
110 bool MediaCodecBridge::IsAvailable() {
111 // MediaCodec is only available on JB and greater.
112 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16)
114 // Blacklist some devices on Jellybean as for MediaCodec support is buggy.
115 // http://crbug.com/365494.
116 if (base::android::BuildInfo::GetInstance()->sdk_int() == 16) {
117 std::string
model(base::android::BuildInfo::GetInstance()->model());
118 return model
!= "GT-I9100" && model
!= "GT-I9300" && model
!= "GT-N7000";
124 bool MediaCodecBridge::SupportsSetParameters() {
125 // MediaCodec.setParameters() is only available starting with K.
126 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
130 bool MediaCodecBridge::SupportsGetName() {
131 // MediaCodec.getName() is only available on JB MR2 and greater.
132 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18;
136 std::vector
<MediaCodecBridge::CodecsInfo
> MediaCodecBridge::GetCodecsInfo() {
137 std::vector
<CodecsInfo
> codecs_info
;
141 JNIEnv
* env
= AttachCurrentThread();
142 std::string mime_type
;
143 ScopedJavaLocalRef
<jobjectArray
> j_codec_info_array
=
144 Java_MediaCodecBridge_getCodecsInfo(env
);
145 jsize len
= env
->GetArrayLength(j_codec_info_array
.obj());
146 for (jsize i
= 0; i
< len
; ++i
) {
147 ScopedJavaLocalRef
<jobject
> j_info(
148 env
, env
->GetObjectArrayElement(j_codec_info_array
.obj(), i
));
149 ScopedJavaLocalRef
<jstring
> j_codec_type
=
150 Java_CodecInfo_codecType(env
, j_info
.obj());
151 ConvertJavaStringToUTF8(env
, j_codec_type
.obj(), &mime_type
);
152 ScopedJavaLocalRef
<jstring
> j_codec_name
=
153 Java_CodecInfo_codecName(env
, j_info
.obj());
155 info
.codecs
= AndroidMimeTypeToCodecType(mime_type
);
156 ConvertJavaStringToUTF8(env
, j_codec_name
.obj(), &info
.name
);
157 info
.direction
= static_cast<MediaCodecDirection
>(
158 Java_CodecInfo_direction(env
, j_info
.obj()));
159 codecs_info
.push_back(info
);
165 std::string
MediaCodecBridge::GetDefaultCodecName(
166 const std::string
& mime_type
,
167 MediaCodecDirection direction
) {
169 return std::string();
171 JNIEnv
* env
= AttachCurrentThread();
172 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime_type
);
173 ScopedJavaLocalRef
<jstring
> j_codec_name
=
174 Java_MediaCodecBridge_getDefaultCodecName(env
, j_mime
.obj(), direction
);
175 return ConvertJavaStringToUTF8(env
, j_codec_name
.obj());
179 std::set
<int> MediaCodecBridge::GetEncoderColorFormats(
180 const std::string
& mime_type
) {
181 std::set
<int> color_formats
;
183 return color_formats
;
185 JNIEnv
* env
= AttachCurrentThread();
186 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime_type
);
187 ScopedJavaLocalRef
<jintArray
> j_color_format_array
=
188 Java_MediaCodecBridge_getEncoderColorFormatsForMime(env
, j_mime
.obj());
190 if (j_color_format_array
.obj()) {
191 std::vector
<int> formats
;
192 JavaIntArrayToIntVector(env
, j_color_format_array
.obj(), &formats
);
193 color_formats
= std::set
<int>(formats
.begin(), formats
.end());
196 return color_formats
;
200 bool MediaCodecBridge::CanDecode(const std::string
& codec
, bool is_secure
) {
204 JNIEnv
* env
= AttachCurrentThread();
205 std::string mime
= CodecTypeToAndroidMimeType(codec
);
208 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
209 ScopedJavaLocalRef
<jobject
> j_media_codec_bridge
=
210 Java_MediaCodecBridge_create(env
, j_mime
.obj(), is_secure
, false);
211 if (!j_media_codec_bridge
.is_null()) {
212 Java_MediaCodecBridge_release(env
, j_media_codec_bridge
.obj());
219 bool MediaCodecBridge::IsKnownUnaccelerated(const std::string
& mime_type
,
220 MediaCodecDirection direction
) {
224 std::string codec_name
;
225 if (SupportsGetName()) {
226 codec_name
= GetDefaultCodecName(mime_type
, direction
);
228 std::string codec_type
= AndroidMimeTypeToCodecType(mime_type
);
229 std::vector
<media::MediaCodecBridge::CodecsInfo
> codecs_info
=
230 MediaCodecBridge::GetCodecsInfo();
231 for (size_t i
= 0; i
< codecs_info
.size(); ++i
) {
232 if (codecs_info
[i
].codecs
== codec_type
&&
233 codecs_info
[i
].direction
== direction
) {
234 codec_name
= codecs_info
[i
].name
;
239 DVLOG(1) << __PRETTY_FUNCTION__
<< "Default codec for " << mime_type
<<
241 // It would be nice if MediaCodecInfo externalized some notion of
242 // HW-acceleration but it doesn't. Android Media guidance is that the
243 // "OMX.google" prefix is always used for SW decoders, so that's what we
244 // use. "OMX.SEC.*" codec is Samsung software implementation - report it
245 // as unaccelerated as well. Also temporary blacklist Exynos and MediaTek
246 // devices while HW decoder video freezes and distortions are
247 // investigated - http://crbug.com/446974.
248 if (codec_name
.length() > 0) {
249 return (base::StartsWith(codec_name
, "OMX.google.",
250 base::CompareCase::SENSITIVE
) ||
251 base::StartsWith(codec_name
, "OMX.SEC.",
252 base::CompareCase::SENSITIVE
) ||
253 base::StartsWith(codec_name
, "OMX.MTK.",
254 base::CompareCase::SENSITIVE
) ||
255 base::StartsWith(codec_name
, "OMX.Exynos.",
256 base::CompareCase::SENSITIVE
));
261 MediaCodecBridge::MediaCodecBridge(const std::string
& mime
,
263 MediaCodecDirection direction
) {
264 JNIEnv
* env
= AttachCurrentThread();
266 DCHECK(!mime
.empty());
267 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
268 j_media_codec_
.Reset(
269 Java_MediaCodecBridge_create(env
, j_mime
.obj(), is_secure
, direction
));
272 MediaCodecBridge::~MediaCodecBridge() {
273 JNIEnv
* env
= AttachCurrentThread();
275 if (j_media_codec_
.obj())
276 Java_MediaCodecBridge_release(env
, j_media_codec_
.obj());
279 bool MediaCodecBridge::StartInternal() {
280 JNIEnv
* env
= AttachCurrentThread();
281 return Java_MediaCodecBridge_start(env
, j_media_codec_
.obj());
284 MediaCodecStatus
MediaCodecBridge::Reset() {
285 JNIEnv
* env
= AttachCurrentThread();
286 return static_cast<MediaCodecStatus
>(
287 Java_MediaCodecBridge_flush(env
, j_media_codec_
.obj()));
290 void MediaCodecBridge::Stop() {
291 JNIEnv
* env
= AttachCurrentThread();
292 Java_MediaCodecBridge_stop(env
, j_media_codec_
.obj());
295 void MediaCodecBridge::GetOutputFormat(int* width
, int* height
) {
296 JNIEnv
* env
= AttachCurrentThread();
298 *width
= Java_MediaCodecBridge_getOutputWidth(env
, j_media_codec_
.obj());
299 *height
= Java_MediaCodecBridge_getOutputHeight(env
, j_media_codec_
.obj());
302 int MediaCodecBridge::GetOutputSamplingRate() {
303 JNIEnv
* env
= AttachCurrentThread();
305 return Java_MediaCodecBridge_getOutputSamplingRate(env
, j_media_codec_
.obj());
308 MediaCodecStatus
MediaCodecBridge::QueueInputBuffer(
312 const base::TimeDelta
& presentation_time
) {
313 DVLOG(3) << __PRETTY_FUNCTION__
<< index
<< ": " << data_size
;
314 if (data_size
> base::checked_cast
<size_t>(kint32max
))
315 return MEDIA_CODEC_ERROR
;
316 if (data
&& !FillInputBuffer(index
, data
, data_size
))
317 return MEDIA_CODEC_ERROR
;
318 JNIEnv
* env
= AttachCurrentThread();
319 return static_cast<MediaCodecStatus
>(
320 Java_MediaCodecBridge_queueInputBuffer(env
,
321 j_media_codec_
.obj(),
325 presentation_time
.InMicroseconds(),
329 MediaCodecStatus
MediaCodecBridge::QueueSecureInputBuffer(
337 const SubsampleEntry
* subsamples
,
339 const base::TimeDelta
& presentation_time
) {
340 DVLOG(3) << __PRETTY_FUNCTION__
<< index
<< ": " << data_size
;
341 if (data_size
> base::checked_cast
<size_t>(kint32max
))
342 return MEDIA_CODEC_ERROR
;
343 if (data
&& !FillInputBuffer(index
, data
, data_size
))
344 return MEDIA_CODEC_ERROR
;
346 JNIEnv
* env
= AttachCurrentThread();
347 ScopedJavaLocalRef
<jbyteArray
> j_key_id
=
348 base::android::ToJavaByteArray(env
, key_id
, key_id_size
);
349 ScopedJavaLocalRef
<jbyteArray
> j_iv
=
350 base::android::ToJavaByteArray(env
, iv
, iv_size
);
352 // MediaCodec.CryptoInfo documentations says passing NULL for |clear_array|
353 // to indicate that all data is encrypted. But it doesn't specify what
354 // |cypher_array| and |subsamples_size| should be in that case. Passing
355 // one subsample here just to be on the safe side.
356 int new_subsamples_size
= subsamples_size
== 0 ? 1 : subsamples_size
;
358 scoped_ptr
<jint
[]> native_clear_array(new jint
[new_subsamples_size
]);
359 scoped_ptr
<jint
[]> native_cypher_array(new jint
[new_subsamples_size
]);
361 if (subsamples_size
== 0) {
363 native_clear_array
[0] = 0;
364 native_cypher_array
[0] = data_size
;
366 DCHECK_GT(subsamples_size
, 0);
368 for (int i
= 0; i
< subsamples_size
; ++i
) {
369 DCHECK(subsamples
[i
].clear_bytes
<= std::numeric_limits
<uint16
>::max());
370 if (subsamples
[i
].cypher_bytes
>
371 static_cast<uint32
>(std::numeric_limits
<jint
>::max())) {
372 return MEDIA_CODEC_ERROR
;
375 native_clear_array
[i
] = subsamples
[i
].clear_bytes
;
376 native_cypher_array
[i
] = subsamples
[i
].cypher_bytes
;
380 ScopedJavaLocalRef
<jintArray
> clear_array
=
381 ToJavaIntArray(env
, native_clear_array
.Pass(), new_subsamples_size
);
382 ScopedJavaLocalRef
<jintArray
> cypher_array
=
383 ToJavaIntArray(env
, native_cypher_array
.Pass(), new_subsamples_size
);
385 return static_cast<MediaCodecStatus
>(
386 Java_MediaCodecBridge_queueSecureInputBuffer(
388 j_media_codec_
.obj(),
396 presentation_time
.InMicroseconds()));
399 void MediaCodecBridge::QueueEOS(int input_buffer_index
) {
400 DVLOG(3) << __PRETTY_FUNCTION__
<< ": " << input_buffer_index
;
401 JNIEnv
* env
= AttachCurrentThread();
402 Java_MediaCodecBridge_queueInputBuffer(env
,
403 j_media_codec_
.obj(),
408 kBufferFlagEndOfStream
);
411 MediaCodecStatus
MediaCodecBridge::DequeueInputBuffer(
412 const base::TimeDelta
& timeout
,
414 JNIEnv
* env
= AttachCurrentThread();
415 ScopedJavaLocalRef
<jobject
> result
= Java_MediaCodecBridge_dequeueInputBuffer(
416 env
, j_media_codec_
.obj(), timeout
.InMicroseconds());
417 *index
= Java_DequeueInputResult_index(env
, result
.obj());
418 MediaCodecStatus status
= static_cast<MediaCodecStatus
>(
419 Java_DequeueInputResult_status(env
, result
.obj()));
420 DVLOG(3) << __PRETTY_FUNCTION__
<< ": status: " << status
421 << ", index: " << *index
;
425 MediaCodecStatus
MediaCodecBridge::DequeueOutputBuffer(
426 const base::TimeDelta
& timeout
,
430 base::TimeDelta
* presentation_time
,
433 JNIEnv
* env
= AttachCurrentThread();
434 ScopedJavaLocalRef
<jobject
> result
=
435 Java_MediaCodecBridge_dequeueOutputBuffer(
436 env
, j_media_codec_
.obj(), timeout
.InMicroseconds());
437 *index
= Java_DequeueOutputResult_index(env
, result
.obj());
438 *offset
= base::checked_cast
<size_t>(
439 Java_DequeueOutputResult_offset(env
, result
.obj()));
440 *size
= base::checked_cast
<size_t>(
441 Java_DequeueOutputResult_numBytes(env
, result
.obj()));
442 if (presentation_time
) {
443 *presentation_time
= base::TimeDelta::FromMicroseconds(
444 Java_DequeueOutputResult_presentationTimeMicroseconds(env
,
447 int flags
= Java_DequeueOutputResult_flags(env
, result
.obj());
449 *end_of_stream
= flags
& kBufferFlagEndOfStream
;
451 *key_frame
= flags
& kBufferFlagSyncFrame
;
452 MediaCodecStatus status
= static_cast<MediaCodecStatus
>(
453 Java_DequeueOutputResult_status(env
, result
.obj()));
454 DVLOG(3) << __PRETTY_FUNCTION__
<< ": status: " << status
455 << ", index: " << *index
<< ", offset: " << *offset
456 << ", size: " << *size
<< ", flags: " << flags
;
460 void MediaCodecBridge::ReleaseOutputBuffer(int index
, bool render
) {
461 DVLOG(3) << __PRETTY_FUNCTION__
<< ": " << index
;
462 JNIEnv
* env
= AttachCurrentThread();
465 Java_MediaCodecBridge_releaseOutputBuffer(
466 env
, j_media_codec_
.obj(), index
, render
);
469 int MediaCodecBridge::GetOutputBuffersCount() {
470 JNIEnv
* env
= AttachCurrentThread();
471 return Java_MediaCodecBridge_getOutputBuffersCount(env
, j_media_codec_
.obj());
474 size_t MediaCodecBridge::GetOutputBuffersCapacity() {
475 JNIEnv
* env
= AttachCurrentThread();
476 return Java_MediaCodecBridge_getOutputBuffersCapacity(env
,
477 j_media_codec_
.obj());
480 void MediaCodecBridge::GetInputBuffer(int input_buffer_index
,
483 JNIEnv
* env
= AttachCurrentThread();
484 ScopedJavaLocalRef
<jobject
> j_buffer(Java_MediaCodecBridge_getInputBuffer(
485 env
, j_media_codec_
.obj(), input_buffer_index
));
486 *data
= static_cast<uint8
*>(env
->GetDirectBufferAddress(j_buffer
.obj()));
487 *capacity
= base::checked_cast
<size_t>(
488 env
->GetDirectBufferCapacity(j_buffer
.obj()));
491 bool MediaCodecBridge::CopyFromOutputBuffer(int index
,
495 JNIEnv
* env
= AttachCurrentThread();
496 ScopedJavaLocalRef
<jobject
> j_buffer(
497 Java_MediaCodecBridge_getOutputBuffer(env
, j_media_codec_
.obj(), index
));
499 reinterpret_cast<uint8
*>(env
->GetDirectBufferAddress(j_buffer
.obj())) +
501 int src_capacity
= env
->GetDirectBufferCapacity(j_buffer
.obj()) - offset
;
502 if (src_capacity
< dst_size
)
504 memcpy(dst
, src_data
, dst_size
);
508 bool MediaCodecBridge::FillInputBuffer(int index
,
513 GetInputBuffer(index
, &dst
, &capacity
);
516 if (size
> capacity
) {
517 LOG(ERROR
) << "Input buffer size " << size
518 << " exceeds MediaCodec input buffer capacity: " << capacity
;
522 memcpy(dst
, data
, size
);
526 AudioCodecBridge::AudioCodecBridge(const std::string
& mime
)
527 // Audio codec doesn't care about security level and there is no need for
528 // audio encoding yet.
529 : MediaCodecBridge(mime
, false, MEDIA_CODEC_DECODER
) {}
531 bool AudioCodecBridge::Start(const AudioCodec
& codec
,
534 const uint8
* extra_data
,
535 size_t extra_data_size
,
536 int64 codec_delay_ns
,
537 int64 seek_preroll_ns
,
539 jobject media_crypto
) {
540 JNIEnv
* env
= AttachCurrentThread();
545 std::string codec_string
= AudioCodecToAndroidMimeType(codec
);
546 if (codec_string
.empty())
549 ScopedJavaLocalRef
<jstring
> j_mime
=
550 ConvertUTF8ToJavaString(env
, codec_string
);
551 ScopedJavaLocalRef
<jobject
> j_format(Java_MediaCodecBridge_createAudioFormat(
552 env
, j_mime
.obj(), sample_rate
, channel_count
));
553 DCHECK(!j_format
.is_null());
555 if (!ConfigureMediaFormat(j_format
.obj(), codec
, extra_data
, extra_data_size
,
556 codec_delay_ns
, seek_preroll_ns
)) {
560 if (!Java_MediaCodecBridge_configureAudio(
561 env
, media_codec(), j_format
.obj(), media_crypto
, 0, play_audio
)) {
565 return StartInternal();
568 bool AudioCodecBridge::ConfigureMediaFormat(jobject j_format
,
569 const AudioCodec
& codec
,
570 const uint8
* extra_data
,
571 size_t extra_data_size
,
572 int64 codec_delay_ns
,
573 int64 seek_preroll_ns
) {
574 if (extra_data_size
== 0 && codec
!= kCodecOpus
)
577 JNIEnv
* env
= AttachCurrentThread();
580 if (extra_data
[0] != 2) {
581 LOG(ERROR
) << "Invalid number of vorbis headers before the codec "
582 << "header: " << extra_data
[0];
586 size_t header_length
[2];
587 // |total_length| keeps track of the total number of bytes before the last
589 size_t total_length
= 1;
590 const uint8
* current_pos
= extra_data
;
591 // Calculate the length of the first 2 headers.
592 for (int i
= 0; i
< 2; ++i
) {
593 header_length
[i
] = 0;
594 while (total_length
< extra_data_size
) {
595 size_t size
= *(++current_pos
);
596 total_length
+= 1 + size
;
597 if (total_length
> 0x80000000) {
598 LOG(ERROR
) << "Vorbis header size too large";
601 header_length
[i
] += size
;
605 if (total_length
>= extra_data_size
) {
606 LOG(ERROR
) << "Invalid vorbis header size in the extra data";
611 // The first header is identification header.
612 ScopedJavaLocalRef
<jbyteArray
> first_header
=
613 base::android::ToJavaByteArray(env
, current_pos
, header_length
[0]);
614 Java_MediaCodecBridge_setCodecSpecificData(
615 env
, j_format
, 0, first_header
.obj());
616 // The last header is codec header.
617 ScopedJavaLocalRef
<jbyteArray
> last_header
=
618 base::android::ToJavaByteArray(
619 env
, extra_data
+ total_length
, extra_data_size
- total_length
);
620 Java_MediaCodecBridge_setCodecSpecificData(
621 env
, j_format
, 1, last_header
.obj());
625 media::BitReader
reader(extra_data
, extra_data_size
);
627 // The following code is copied from aac.cc
628 // TODO(qinmin): refactor the code in aac.cc to make it more reusable.
630 uint8 frequency_index
= 0;
631 uint8 channel_config
= 0;
632 if (!reader
.ReadBits(5, &profile
) ||
633 !reader
.ReadBits(4, &frequency_index
)) {
634 LOG(ERROR
) << "Unable to parse AAC header";
637 if (0xf == frequency_index
&& !reader
.SkipBits(24)) {
638 LOG(ERROR
) << "Unable to parse AAC header";
641 if (!reader
.ReadBits(4, &channel_config
)) {
642 LOG(ERROR
) << "Unable to parse AAC header";
646 if (profile
< 1 || profile
> 4 || frequency_index
== 0xf ||
647 channel_config
> 7) {
648 LOG(ERROR
) << "Invalid AAC header";
651 const size_t kCsdLength
= 2;
652 uint8 csd
[kCsdLength
];
653 csd
[0] = profile
<< 3 | frequency_index
>> 1;
654 csd
[1] = (frequency_index
& 0x01) << 7 | channel_config
<< 3;
655 ScopedJavaLocalRef
<jbyteArray
> byte_array
=
656 base::android::ToJavaByteArray(env
, csd
, kCsdLength
);
657 Java_MediaCodecBridge_setCodecSpecificData(
658 env
, j_format
, 0, byte_array
.obj());
660 // TODO(qinmin): pass an extra variable to this function to determine
661 // whether we need to call this.
662 Java_MediaCodecBridge_setFrameHasADTSHeader(env
, j_format
);
666 if (!extra_data
|| extra_data_size
== 0 ||
667 codec_delay_ns
< 0 || seek_preroll_ns
< 0) {
668 LOG(ERROR
) << "Invalid Opus Header";
672 // csd0 - Opus Header
673 ScopedJavaLocalRef
<jbyteArray
> csd0
=
674 base::android::ToJavaByteArray(env
, extra_data
, extra_data_size
);
675 Java_MediaCodecBridge_setCodecSpecificData(env
, j_format
, 0, csd0
.obj());
677 // csd1 - Codec Delay
678 ScopedJavaLocalRef
<jbyteArray
> csd1
=
679 base::android::ToJavaByteArray(
680 env
, reinterpret_cast<const uint8
*>(&codec_delay_ns
),
682 Java_MediaCodecBridge_setCodecSpecificData(env
, j_format
, 1, csd1
.obj());
684 // csd2 - Seek Preroll
685 ScopedJavaLocalRef
<jbyteArray
> csd2
=
686 base::android::ToJavaByteArray(
687 env
, reinterpret_cast<const uint8
*>(&seek_preroll_ns
),
689 Java_MediaCodecBridge_setCodecSpecificData(env
, j_format
, 2, csd2
.obj());
693 LOG(ERROR
) << "Invalid header encountered for codec: "
694 << AudioCodecToAndroidMimeType(codec
);
700 int64
AudioCodecBridge::PlayOutputBuffer(int index
, size_t size
) {
702 int numBytes
= base::checked_cast
<int>(size
);
703 JNIEnv
* env
= AttachCurrentThread();
704 ScopedJavaLocalRef
<jobject
> buf
=
705 Java_MediaCodecBridge_getOutputBuffer(env
, media_codec(), index
);
706 uint8
* buffer
= static_cast<uint8
*>(env
->GetDirectBufferAddress(buf
.obj()));
708 ScopedJavaLocalRef
<jbyteArray
> byte_array
=
709 base::android::ToJavaByteArray(env
, buffer
, numBytes
);
710 return Java_MediaCodecBridge_playOutputBuffer(
711 env
, media_codec(), byte_array
.obj());
714 void AudioCodecBridge::SetVolume(double volume
) {
715 JNIEnv
* env
= AttachCurrentThread();
716 Java_MediaCodecBridge_setVolume(env
, media_codec(), volume
);
720 AudioCodecBridge
* AudioCodecBridge::Create(const AudioCodec
& codec
) {
721 if (!MediaCodecBridge::IsAvailable())
724 const std::string mime
= AudioCodecToAndroidMimeType(codec
);
725 return mime
.empty() ? NULL
: new AudioCodecBridge(mime
);
729 bool AudioCodecBridge::IsKnownUnaccelerated(const AudioCodec
& codec
) {
730 return MediaCodecBridge::IsKnownUnaccelerated(
731 AudioCodecToAndroidMimeType(codec
), MEDIA_CODEC_DECODER
);
735 bool VideoCodecBridge::IsKnownUnaccelerated(const VideoCodec
& codec
,
736 MediaCodecDirection direction
) {
737 return MediaCodecBridge::IsKnownUnaccelerated(
738 VideoCodecToAndroidMimeType(codec
), direction
);
742 VideoCodecBridge
* VideoCodecBridge::CreateDecoder(const VideoCodec
& codec
,
744 const gfx::Size
& size
,
746 jobject media_crypto
) {
747 if (!MediaCodecBridge::IsAvailable())
750 const std::string mime
= VideoCodecToAndroidMimeType(codec
);
754 scoped_ptr
<VideoCodecBridge
> bridge(
755 new VideoCodecBridge(mime
, is_secure
, MEDIA_CODEC_DECODER
));
756 if (!bridge
->media_codec())
759 JNIEnv
* env
= AttachCurrentThread();
760 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
761 ScopedJavaLocalRef
<jobject
> j_format(
762 Java_MediaCodecBridge_createVideoDecoderFormat(
763 env
, j_mime
.obj(), size
.width(), size
.height()));
764 DCHECK(!j_format
.is_null());
765 if (!Java_MediaCodecBridge_configureVideo(env
,
766 bridge
->media_codec(),
774 return bridge
->StartInternal() ? bridge
.release() : NULL
;
778 VideoCodecBridge
* VideoCodecBridge::CreateEncoder(const VideoCodec
& codec
,
779 const gfx::Size
& size
,
782 int i_frame_interval
,
784 if (!MediaCodecBridge::IsAvailable())
787 const std::string mime
= VideoCodecToAndroidMimeType(codec
);
791 scoped_ptr
<VideoCodecBridge
> bridge(
792 new VideoCodecBridge(mime
, false, MEDIA_CODEC_ENCODER
));
793 if (!bridge
->media_codec())
796 JNIEnv
* env
= AttachCurrentThread();
797 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
798 ScopedJavaLocalRef
<jobject
> j_format(
799 Java_MediaCodecBridge_createVideoEncoderFormat(env
,
807 DCHECK(!j_format
.is_null());
808 if (!Java_MediaCodecBridge_configureVideo(env
,
809 bridge
->media_codec(),
813 kConfigureFlagEncode
)) {
817 return bridge
->StartInternal() ? bridge
.release() : NULL
;
820 VideoCodecBridge::VideoCodecBridge(const std::string
& mime
,
822 MediaCodecDirection direction
)
823 : MediaCodecBridge(mime
, is_secure
, direction
),
824 adaptive_playback_supported_for_testing_(-1) {}
826 void VideoCodecBridge::SetVideoBitrate(int bps
) {
827 JNIEnv
* env
= AttachCurrentThread();
828 Java_MediaCodecBridge_setVideoBitrate(env
, media_codec(), bps
);
831 void VideoCodecBridge::RequestKeyFrameSoon() {
832 JNIEnv
* env
= AttachCurrentThread();
833 Java_MediaCodecBridge_requestKeyFrameSoon(env
, media_codec());
836 bool VideoCodecBridge::IsAdaptivePlaybackSupported(int width
, int height
) {
837 if (adaptive_playback_supported_for_testing_
== 0)
839 else if (adaptive_playback_supported_for_testing_
> 0)
841 JNIEnv
* env
= AttachCurrentThread();
842 return Java_MediaCodecBridge_isAdaptivePlaybackSupported(
843 env
, media_codec(), width
, height
);
846 bool MediaCodecBridge::RegisterMediaCodecBridge(JNIEnv
* env
) {
847 return RegisterNativesImpl(env
);