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
;
29 #define RETURN_ON_ERROR(condition) \
32 LOG(ERROR) << "Unable to parse AAC header: " #condition; \
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
) {
50 return "audio/vorbis";
54 return "audio/mp4a-latm";
60 static const std::string
VideoCodecToAndroidMimeType(const VideoCodec
& codec
) {
67 return "video/x-vnd.on2.vp8";
69 return "video/x-vnd.on2.vp9";
75 static const std::string
CodecTypeToAndroidMimeType(const std::string
& codec
) {
76 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"?
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";
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")
98 if (mime
== "video/avc")
100 if (mime
== "video/hevc")
102 if (mime
== "video/x-vnd.on2.vp8")
104 if (mime
== "video/x-vnd.on2.vp9")
106 if (mime
== "audio/mp4a-latm")
108 if (mime
== "audio/mpeg")
110 if (mime
== "audio/vorbis")
112 if (mime
== "audio/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());
125 bool MediaCodecBridge::IsAvailable() {
126 // MediaCodec is only available on JB and greater.
127 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16)
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";
139 bool MediaCodecBridge::SupportsSetParameters() {
140 // MediaCodec.setParameters() is only available starting with K.
141 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
145 bool MediaCodecBridge::SupportsGetName() {
146 // MediaCodec.getName() is only available on JB MR2 and greater.
147 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18;
151 std::vector
<MediaCodecBridge::CodecsInfo
> MediaCodecBridge::GetCodecsInfo() {
152 std::vector
<CodecsInfo
> 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());
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
);
180 std::string
MediaCodecBridge::GetDefaultCodecName(
181 const std::string
& mime_type
,
182 MediaCodecDirection direction
) {
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());
194 std::set
<int> MediaCodecBridge::GetEncoderColorFormats(
195 const std::string
& mime_type
) {
196 std::set
<int> color_formats
;
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
;
215 bool MediaCodecBridge::CanDecode(const std::string
& codec
, bool is_secure
) {
219 JNIEnv
* env
= AttachCurrentThread();
220 std::string mime
= CodecTypeToAndroidMimeType(codec
);
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());
234 bool MediaCodecBridge::IsKnownUnaccelerated(const std::string
& mime_type
,
235 MediaCodecDirection direction
) {
239 std::string codec_name
;
240 if (SupportsGetName()) {
241 codec_name
= GetDefaultCodecName(mime_type
, direction
);
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
;
254 DVLOG(1) << __PRETTY_FUNCTION__
<< "Default codec for " << mime_type
<<
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
));
276 MediaCodecBridge::MediaCodecBridge(const std::string
& mime
,
278 MediaCodecDirection direction
) {
279 JNIEnv
* env
= AttachCurrentThread();
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();
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(
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(),
340 presentation_time
.InMicroseconds(),
344 MediaCodecStatus
MediaCodecBridge::QueueSecureInputBuffer(
352 const SubsampleEntry
* subsamples
,
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) {
378 native_clear_array
[0] = 0;
379 native_cypher_array
[0] = data_size
;
381 DCHECK_GT(subsamples_size
, 0);
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(
403 j_media_codec_
.obj(),
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(),
423 kBufferFlagEndOfStream
);
426 MediaCodecStatus
MediaCodecBridge::DequeueInputBuffer(
427 const base::TimeDelta
& timeout
,
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
;
440 MediaCodecStatus
MediaCodecBridge::DequeueOutputBuffer(
441 const base::TimeDelta
& timeout
,
445 base::TimeDelta
* presentation_time
,
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
,
462 int flags
= Java_DequeueOutputResult_flags(env
, result
.obj());
464 *end_of_stream
= flags
& kBufferFlagEndOfStream
;
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
;
475 void MediaCodecBridge::ReleaseOutputBuffer(int index
, bool render
) {
476 DVLOG(3) << __PRETTY_FUNCTION__
<< ": " << index
;
477 JNIEnv
* env
= AttachCurrentThread();
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
,
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
,
510 void* src_data
= nullptr;
511 int src_capacity
= GetOutputBufferAddress(index
, offset
, &src_data
);
512 if (src_capacity
< dst_size
)
514 memcpy(dst
, src_data
, dst_size
);
518 int MediaCodecBridge::GetOutputBufferAddress(int index
,
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
,
534 GetInputBuffer(index
, &dst
, &capacity
);
537 if (size
> capacity
) {
538 LOG(ERROR
) << "Input buffer size " << size
539 << " exceeds MediaCodec input buffer capacity: " << capacity
;
543 memcpy(dst
, data
, size
);
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
,
555 const uint8
* extra_data
,
556 size_t extra_data_size
,
557 int64 codec_delay_ns
,
558 int64 seek_preroll_ns
,
560 jobject media_crypto
) {
561 JNIEnv
* env
= AttachCurrentThread();
566 std::string codec_string
= AudioCodecToAndroidMimeType(codec
);
567 if (codec_string
.empty())
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
)) {
581 if (!Java_MediaCodecBridge_configureAudio(
582 env
, media_codec(), j_format
.obj(), media_crypto
, 0, play_audio
)) {
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
)
598 JNIEnv
* env
= AttachCurrentThread();
601 if (extra_data
[0] != 2) {
602 LOG(ERROR
) << "Invalid number of vorbis headers before the codec "
603 << "header: " << extra_data
[0];
607 size_t header_length
[2];
608 // |total_length| keeps track of the total number of bytes before the last
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";
622 header_length
[i
] += size
;
626 if (total_length
>= extra_data_size
) {
627 LOG(ERROR
) << "Invalid vorbis header size in the extra data";
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());
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.
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";
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
);
688 if (!extra_data
|| extra_data_size
== 0 ||
689 codec_delay_ns
< 0 || seek_preroll_ns
< 0) {
690 LOG(ERROR
) << "Invalid Opus Header";
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
),
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
),
711 Java_MediaCodecBridge_setCodecSpecificData(env
, j_format
, 2, csd2
.obj());
715 LOG(ERROR
) << "Invalid header encountered for codec: "
716 << AudioCodecToAndroidMimeType(codec
);
722 int64
AudioCodecBridge::PlayOutputBuffer(int 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
);
747 AudioCodecBridge
* AudioCodecBridge::Create(const AudioCodec
& codec
) {
748 if (!MediaCodecBridge::IsAvailable())
751 const std::string mime
= AudioCodecToAndroidMimeType(codec
);
752 return mime
.empty() ? NULL
: new AudioCodecBridge(mime
);
756 bool AudioCodecBridge::IsKnownUnaccelerated(const AudioCodec
& codec
) {
757 return MediaCodecBridge::IsKnownUnaccelerated(
758 AudioCodecToAndroidMimeType(codec
), MEDIA_CODEC_DECODER
);
762 bool VideoCodecBridge::IsKnownUnaccelerated(const VideoCodec
& codec
,
763 MediaCodecDirection direction
) {
764 return MediaCodecBridge::IsKnownUnaccelerated(
765 VideoCodecToAndroidMimeType(codec
), direction
);
769 VideoCodecBridge
* VideoCodecBridge::CreateDecoder(const VideoCodec
& codec
,
771 const gfx::Size
& size
,
773 jobject media_crypto
) {
774 if (!MediaCodecBridge::IsAvailable())
777 const std::string mime
= VideoCodecToAndroidMimeType(codec
);
781 scoped_ptr
<VideoCodecBridge
> bridge(
782 new VideoCodecBridge(mime
, is_secure
, MEDIA_CODEC_DECODER
));
783 if (!bridge
->media_codec())
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(),
801 return bridge
->StartInternal() ? bridge
.release() : NULL
;
805 VideoCodecBridge
* VideoCodecBridge::CreateEncoder(const VideoCodec
& codec
,
806 const gfx::Size
& size
,
809 int i_frame_interval
,
811 if (!MediaCodecBridge::IsAvailable())
814 const std::string mime
= VideoCodecToAndroidMimeType(codec
);
818 scoped_ptr
<VideoCodecBridge
> bridge(
819 new VideoCodecBridge(mime
, false, MEDIA_CODEC_ENCODER
));
820 if (!bridge
->media_codec())
823 JNIEnv
* env
= AttachCurrentThread();
824 ScopedJavaLocalRef
<jstring
> j_mime
= ConvertUTF8ToJavaString(env
, mime
);
825 ScopedJavaLocalRef
<jobject
> j_format(
826 Java_MediaCodecBridge_createVideoEncoderFormat(env
,
834 DCHECK(!j_format
.is_null());
835 if (!Java_MediaCodecBridge_configureVideo(env
,
836 bridge
->media_codec(),
840 kConfigureFlagEncode
)) {
844 return bridge
->StartInternal() ? bridge
.release() : NULL
;
847 VideoCodecBridge::VideoCodecBridge(const std::string
& mime
,
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)
866 else if (adaptive_playback_supported_for_testing_
> 0)
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
);