Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / base / android / media_codec_bridge.cc
blobf076b218e1a6f3baf45eac97276dc656f4b7f0dd
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;
28 namespace media {
30 enum {
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) {
37 switch (codec) {
38 case kCodecMP3:
39 return "audio/mpeg";
40 case kCodecVorbis:
41 return "audio/vorbis";
42 case kCodecOpus:
43 return "audio/opus";
44 case kCodecAAC:
45 return "audio/mp4a-latm";
46 default:
47 return std::string();
51 static const std::string VideoCodecToAndroidMimeType(const VideoCodec& codec) {
52 switch (codec) {
53 case kCodecH264:
54 return "video/avc";
55 case kCodecVP8:
56 return "video/x-vnd.on2.vp8";
57 case kCodecVP9:
58 return "video/x-vnd.on2.vp9";
59 default:
60 return std::string();
64 static const std::string CodecTypeToAndroidMimeType(const std::string& codec) {
65 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"?
66 if (codec == "avc1")
67 return "video/avc";
68 if (codec == "mp4a")
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";
76 if (codec == "opus")
77 return "audio/opus";
78 return std::string();
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")
84 return "mp4v";
85 if (mime == "video/avc")
86 return "avc1";
87 if (mime == "video/x-vnd.on2.vp8")
88 return "vp8";
89 if (mime == "video/x-vnd.on2.vp9")
90 return "vp9";
91 if (mime == "audio/mp4a-latm")
92 return "mp4a";
93 if (mime == "audio/mpeg")
94 return "mp3";
95 if (mime == "audio/vorbis")
96 return "vorbis";
97 if (mime == "audio/opus")
98 return "opus";
99 return std::string();
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());
106 return j_array;
109 // static
110 bool MediaCodecBridge::IsAvailable() {
111 // MediaCodec is only available on JB and greater.
112 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16)
113 return false;
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";
120 return true;
123 // static
124 bool MediaCodecBridge::SupportsSetParameters() {
125 // MediaCodec.setParameters() is only available starting with K.
126 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
129 // static
130 bool MediaCodecBridge::SupportsGetName() {
131 // MediaCodec.getName() is only available on JB MR2 and greater.
132 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18;
135 // static
136 std::vector<MediaCodecBridge::CodecsInfo> MediaCodecBridge::GetCodecsInfo() {
137 std::vector<CodecsInfo> codecs_info;
138 if (!IsAvailable())
139 return 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());
154 CodecsInfo info;
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);
161 return codecs_info;
164 // static
165 std::string MediaCodecBridge::GetDefaultCodecName(
166 const std::string& mime_type,
167 MediaCodecDirection direction) {
168 if (!IsAvailable())
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());
178 // static
179 std::set<int> MediaCodecBridge::GetEncoderColorFormats(
180 const std::string& mime_type) {
181 std::set<int> color_formats;
182 if (!IsAvailable())
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;
199 // static
200 bool MediaCodecBridge::CanDecode(const std::string& codec, bool is_secure) {
201 if (!IsAvailable())
202 return false;
204 JNIEnv* env = AttachCurrentThread();
205 std::string mime = CodecTypeToAndroidMimeType(codec);
206 if (mime.empty())
207 return false;
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());
213 return true;
215 return false;
218 // static
219 bool MediaCodecBridge::IsKnownUnaccelerated(const std::string& mime_type,
220 MediaCodecDirection direction) {
221 if (!IsAvailable())
222 return true;
224 std::string codec_name;
225 if (SupportsGetName()) {
226 codec_name = GetDefaultCodecName(mime_type, direction);
227 } else {
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;
235 break;
239 DVLOG(1) << __PRETTY_FUNCTION__ << "Default codec for " << mime_type <<
240 " : " << codec_name;
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));
258 return true;
261 MediaCodecBridge::MediaCodecBridge(const std::string& mime,
262 bool is_secure,
263 MediaCodecDirection direction) {
264 JNIEnv* env = AttachCurrentThread();
265 CHECK(env);
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();
274 CHECK(env);
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(
309 int index,
310 const uint8* data,
311 size_t data_size,
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(),
322 index,
324 data_size,
325 presentation_time.InMicroseconds(),
326 0));
329 MediaCodecStatus MediaCodecBridge::QueueSecureInputBuffer(
330 int index,
331 const uint8* data,
332 size_t data_size,
333 const uint8* key_id,
334 int key_id_size,
335 const uint8* iv,
336 int iv_size,
337 const SubsampleEntry* subsamples,
338 int subsamples_size,
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) {
362 DCHECK(!subsamples);
363 native_clear_array[0] = 0;
364 native_cypher_array[0] = data_size;
365 } else {
366 DCHECK_GT(subsamples_size, 0);
367 DCHECK(subsamples);
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(
387 env,
388 j_media_codec_.obj(),
389 index,
391 j_iv.obj(),
392 j_key_id.obj(),
393 clear_array.obj(),
394 cypher_array.obj(),
395 new_subsamples_size,
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(),
404 input_buffer_index,
408 kBufferFlagEndOfStream);
411 MediaCodecStatus MediaCodecBridge::DequeueInputBuffer(
412 const base::TimeDelta& timeout,
413 int* index) {
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;
422 return status;
425 MediaCodecStatus MediaCodecBridge::DequeueOutputBuffer(
426 const base::TimeDelta& timeout,
427 int* index,
428 size_t* offset,
429 size_t* size,
430 base::TimeDelta* presentation_time,
431 bool* end_of_stream,
432 bool* key_frame) {
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,
445 result.obj()));
447 int flags = Java_DequeueOutputResult_flags(env, result.obj());
448 if (end_of_stream)
449 *end_of_stream = flags & kBufferFlagEndOfStream;
450 if (key_frame)
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;
457 return status;
460 void MediaCodecBridge::ReleaseOutputBuffer(int index, bool render) {
461 DVLOG(3) << __PRETTY_FUNCTION__ << ": " << index;
462 JNIEnv* env = AttachCurrentThread();
463 CHECK(env);
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,
481 uint8** data,
482 size_t* capacity) {
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,
492 size_t offset,
493 void* dst,
494 int dst_size) {
495 JNIEnv* env = AttachCurrentThread();
496 ScopedJavaLocalRef<jobject> j_buffer(
497 Java_MediaCodecBridge_getOutputBuffer(env, j_media_codec_.obj(), index));
498 void* src_data =
499 reinterpret_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())) +
500 offset;
501 int src_capacity = env->GetDirectBufferCapacity(j_buffer.obj()) - offset;
502 if (src_capacity < dst_size)
503 return false;
504 memcpy(dst, src_data, dst_size);
505 return true;
508 bool MediaCodecBridge::FillInputBuffer(int index,
509 const uint8* data,
510 size_t size) {
511 uint8* dst = NULL;
512 size_t capacity = 0;
513 GetInputBuffer(index, &dst, &capacity);
514 CHECK(dst);
516 if (size > capacity) {
517 LOG(ERROR) << "Input buffer size " << size
518 << " exceeds MediaCodec input buffer capacity: " << capacity;
519 return false;
522 memcpy(dst, data, size);
523 return true;
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,
532 int sample_rate,
533 int channel_count,
534 const uint8* extra_data,
535 size_t extra_data_size,
536 int64 codec_delay_ns,
537 int64 seek_preroll_ns,
538 bool play_audio,
539 jobject media_crypto) {
540 JNIEnv* env = AttachCurrentThread();
542 if (!media_codec())
543 return false;
545 std::string codec_string = AudioCodecToAndroidMimeType(codec);
546 if (codec_string.empty())
547 return false;
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)) {
557 return false;
560 if (!Java_MediaCodecBridge_configureAudio(
561 env, media_codec(), j_format.obj(), media_crypto, 0, play_audio)) {
562 return false;
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)
575 return true;
577 JNIEnv* env = AttachCurrentThread();
578 switch (codec) {
579 case kCodecVorbis: {
580 if (extra_data[0] != 2) {
581 LOG(ERROR) << "Invalid number of vorbis headers before the codec "
582 << "header: " << extra_data[0];
583 return false;
586 size_t header_length[2];
587 // |total_length| keeps track of the total number of bytes before the last
588 // header.
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";
599 return false;
601 header_length[i] += size;
602 if (size < 0xFF)
603 break;
605 if (total_length >= extra_data_size) {
606 LOG(ERROR) << "Invalid vorbis header size in the extra data";
607 return false;
610 current_pos++;
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());
622 break;
624 case kCodecAAC: {
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.
629 uint8 profile = 0;
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";
635 return false;
637 if (0xf == frequency_index && !reader.SkipBits(24)) {
638 LOG(ERROR) << "Unable to parse AAC header";
639 return false;
641 if (!reader.ReadBits(4, &channel_config)) {
642 LOG(ERROR) << "Unable to parse AAC header";
643 return false;
646 if (profile < 1 || profile > 4 || frequency_index == 0xf ||
647 channel_config > 7) {
648 LOG(ERROR) << "Invalid AAC header";
649 return false;
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);
663 break;
665 case kCodecOpus: {
666 if (!extra_data || extra_data_size == 0 ||
667 codec_delay_ns < 0 || seek_preroll_ns < 0) {
668 LOG(ERROR) << "Invalid Opus Header";
669 return false;
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),
681 sizeof(int64_t));
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),
688 sizeof(int64_t));
689 Java_MediaCodecBridge_setCodecSpecificData(env, j_format, 2, csd2.obj());
690 break;
692 default:
693 LOG(ERROR) << "Invalid header encountered for codec: "
694 << AudioCodecToAndroidMimeType(codec);
695 return false;
697 return true;
700 int64 AudioCodecBridge::PlayOutputBuffer(int index, size_t size) {
701 DCHECK_LE(0, index);
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);
719 // static
720 AudioCodecBridge* AudioCodecBridge::Create(const AudioCodec& codec) {
721 if (!MediaCodecBridge::IsAvailable())
722 return NULL;
724 const std::string mime = AudioCodecToAndroidMimeType(codec);
725 return mime.empty() ? NULL : new AudioCodecBridge(mime);
728 // static
729 bool AudioCodecBridge::IsKnownUnaccelerated(const AudioCodec& codec) {
730 return MediaCodecBridge::IsKnownUnaccelerated(
731 AudioCodecToAndroidMimeType(codec), MEDIA_CODEC_DECODER);
734 // static
735 bool VideoCodecBridge::IsKnownUnaccelerated(const VideoCodec& codec,
736 MediaCodecDirection direction) {
737 return MediaCodecBridge::IsKnownUnaccelerated(
738 VideoCodecToAndroidMimeType(codec), direction);
741 // static
742 VideoCodecBridge* VideoCodecBridge::CreateDecoder(const VideoCodec& codec,
743 bool is_secure,
744 const gfx::Size& size,
745 jobject surface,
746 jobject media_crypto) {
747 if (!MediaCodecBridge::IsAvailable())
748 return NULL;
750 const std::string mime = VideoCodecToAndroidMimeType(codec);
751 if (mime.empty())
752 return NULL;
754 scoped_ptr<VideoCodecBridge> bridge(
755 new VideoCodecBridge(mime, is_secure, MEDIA_CODEC_DECODER));
756 if (!bridge->media_codec())
757 return NULL;
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(),
767 j_format.obj(),
768 surface,
769 media_crypto,
770 0)) {
771 return NULL;
774 return bridge->StartInternal() ? bridge.release() : NULL;
777 // static
778 VideoCodecBridge* VideoCodecBridge::CreateEncoder(const VideoCodec& codec,
779 const gfx::Size& size,
780 int bit_rate,
781 int frame_rate,
782 int i_frame_interval,
783 int color_format) {
784 if (!MediaCodecBridge::IsAvailable())
785 return NULL;
787 const std::string mime = VideoCodecToAndroidMimeType(codec);
788 if (mime.empty())
789 return NULL;
791 scoped_ptr<VideoCodecBridge> bridge(
792 new VideoCodecBridge(mime, false, MEDIA_CODEC_ENCODER));
793 if (!bridge->media_codec())
794 return NULL;
796 JNIEnv* env = AttachCurrentThread();
797 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
798 ScopedJavaLocalRef<jobject> j_format(
799 Java_MediaCodecBridge_createVideoEncoderFormat(env,
800 j_mime.obj(),
801 size.width(),
802 size.height(),
803 bit_rate,
804 frame_rate,
805 i_frame_interval,
806 color_format));
807 DCHECK(!j_format.is_null());
808 if (!Java_MediaCodecBridge_configureVideo(env,
809 bridge->media_codec(),
810 j_format.obj(),
811 NULL,
812 NULL,
813 kConfigureFlagEncode)) {
814 return NULL;
817 return bridge->StartInternal() ? bridge.release() : NULL;
820 VideoCodecBridge::VideoCodecBridge(const std::string& mime,
821 bool is_secure,
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)
838 return false;
839 else if (adaptive_playback_supported_for_testing_ > 0)
840 return true;
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);
850 } // namespace media