Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / android / media_codec_bridge.cc
blob9128df32f1fd9a36930ef3d4f83a63d4faa85ccc
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"
7 #include <jni.h>
8 #include <string>
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;
30 namespace media {
32 enum {
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) {
39 switch (codec) {
40 case kCodecMP3:
41 return "audio/mpeg";
42 case kCodecVorbis:
43 return "audio/vorbis";
44 case kCodecOpus:
45 return "audio/opus";
46 case kCodecAAC:
47 return "audio/mp4a-latm";
48 default:
49 return std::string();
53 static const std::string VideoCodecToAndroidMimeType(const VideoCodec& codec) {
54 switch (codec) {
55 case kCodecH264:
56 return "video/avc";
57 case kCodecVP8:
58 return "video/x-vnd.on2.vp8";
59 case kCodecVP9:
60 return "video/x-vnd.on2.vp9";
61 default:
62 return std::string();
66 static const std::string CodecTypeToAndroidMimeType(const std::string& codec) {
67 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"?
68 if (codec == "avc1")
69 return "video/avc";
70 if (codec == "mp4a")
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";
78 if (codec == "opus")
79 return "audio/opus";
80 return std::string();
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")
86 return "mp4v";
87 if (mime == "video/avc")
88 return "avc1";
89 if (mime == "video/x-vnd.on2.vp8")
90 return "vp8";
91 if (mime == "video/x-vnd.on2.vp9")
92 return "vp9";
93 if (mime == "audio/mp4a-latm")
94 return "mp4a";
95 if (mime == "audio/mpeg")
96 return "mp3";
97 if (mime == "audio/vorbis")
98 return "vorbis";
99 if (mime == "audio/opus")
100 return "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());
108 return j_array;
111 // static
112 bool MediaCodecBridge::IsAvailable() {
113 // MediaCodec is only available on JB and greater.
114 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16)
115 return false;
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";
122 return true;
125 // static
126 bool MediaCodecBridge::SupportsSetParameters() {
127 // MediaCodec.setParameters() is only available starting with K.
128 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
131 // static
132 bool MediaCodecBridge::SupportsGetName() {
133 // MediaCodec.getName() is only available on JB MR2 and greater.
134 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18;
137 // static
138 std::vector<MediaCodecBridge::CodecsInfo> MediaCodecBridge::GetCodecsInfo() {
139 std::vector<CodecsInfo> codecs_info;
140 if (!IsAvailable())
141 return 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());
156 CodecsInfo info;
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);
163 return codecs_info;
166 // static
167 std::string MediaCodecBridge::GetDefaultCodecName(
168 const std::string& mime_type,
169 MediaCodecDirection direction) {
170 if (!IsAvailable())
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());
180 // static
181 std::set<int> MediaCodecBridge::GetEncoderColorFormats(
182 const std::string& mime_type) {
183 std::set<int> color_formats;
184 if (!IsAvailable())
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;
201 // static
202 bool MediaCodecBridge::CanDecode(const std::string& codec, bool is_secure) {
203 if (!IsAvailable())
204 return false;
206 JNIEnv* env = AttachCurrentThread();
207 std::string mime = CodecTypeToAndroidMimeType(codec);
208 if (mime.empty())
209 return false;
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());
215 return true;
217 return false;
220 // static
221 bool MediaCodecBridge::IsKnownUnaccelerated(const std::string& mime_type,
222 MediaCodecDirection direction) {
223 if (!IsAvailable())
224 return true;
226 std::string codec_name;
227 if (SupportsGetName()) {
228 codec_name = GetDefaultCodecName(mime_type, direction);
229 } else {
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;
237 break;
241 DVLOG(1) << __PRETTY_FUNCTION__ << "Default codec for " << mime_type <<
242 " : " << codec_name;
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));
256 return true;
259 MediaCodecBridge::MediaCodecBridge(const std::string& mime,
260 bool is_secure,
261 MediaCodecDirection direction) {
262 JNIEnv* env = AttachCurrentThread();
263 CHECK(env);
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();
272 CHECK(env);
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(
307 int index,
308 const uint8* data,
309 size_t data_size,
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(),
320 index,
322 data_size,
323 presentation_time.InMicroseconds(),
324 0));
327 MediaCodecStatus MediaCodecBridge::QueueSecureInputBuffer(
328 int index,
329 const uint8* data,
330 size_t data_size,
331 const uint8* key_id,
332 int key_id_size,
333 const uint8* iv,
334 int iv_size,
335 const SubsampleEntry* subsamples,
336 int subsamples_size,
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) {
360 DCHECK(!subsamples);
361 native_clear_array[0] = 0;
362 native_cypher_array[0] = data_size;
363 } else {
364 DCHECK_GT(subsamples_size, 0);
365 DCHECK(subsamples);
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(
385 env,
386 j_media_codec_.obj(),
387 index,
389 j_iv.obj(),
390 j_key_id.obj(),
391 clear_array.obj(),
392 cypher_array.obj(),
393 new_subsamples_size,
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(),
402 input_buffer_index,
406 kBufferFlagEndOfStream);
409 MediaCodecStatus MediaCodecBridge::DequeueInputBuffer(
410 const base::TimeDelta& timeout,
411 int* index) {
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;
420 return status;
423 MediaCodecStatus MediaCodecBridge::DequeueOutputBuffer(
424 const base::TimeDelta& timeout,
425 int* index,
426 size_t* offset,
427 size_t* size,
428 base::TimeDelta* presentation_time,
429 bool* end_of_stream,
430 bool* key_frame) {
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,
443 result.obj()));
445 int flags = Java_DequeueOutputResult_flags(env, result.obj());
446 if (end_of_stream)
447 *end_of_stream = flags & kBufferFlagEndOfStream;
448 if (key_frame)
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;
455 return status;
458 void MediaCodecBridge::ReleaseOutputBuffer(int index, bool render) {
459 DVLOG(3) << __PRETTY_FUNCTION__ << ": " << index;
460 JNIEnv* env = AttachCurrentThread();
461 CHECK(env);
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,
479 uint8** data,
480 size_t* capacity) {
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,
490 size_t offset,
491 void* dst,
492 int dst_size) {
493 JNIEnv* env = AttachCurrentThread();
494 ScopedJavaLocalRef<jobject> j_buffer(
495 Java_MediaCodecBridge_getOutputBuffer(env, j_media_codec_.obj(), index));
496 void* src_data =
497 reinterpret_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())) +
498 offset;
499 int src_capacity = env->GetDirectBufferCapacity(j_buffer.obj()) - offset;
500 if (src_capacity < dst_size)
501 return false;
502 memcpy(dst, src_data, dst_size);
503 return true;
506 bool MediaCodecBridge::FillInputBuffer(int index,
507 const uint8* data,
508 size_t size) {
509 uint8* dst = NULL;
510 size_t capacity = 0;
511 GetInputBuffer(index, &dst, &capacity);
512 CHECK(dst);
514 if (size > capacity) {
515 LOG(ERROR) << "Input buffer size " << size
516 << " exceeds MediaCodec input buffer capacity: " << capacity;
517 return false;
520 memcpy(dst, data, size);
521 return true;
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,
530 int sample_rate,
531 int channel_count,
532 const uint8* extra_data,
533 size_t extra_data_size,
534 int64 codec_delay_ns,
535 int64 seek_preroll_ns,
536 bool play_audio,
537 jobject media_crypto) {
538 JNIEnv* env = AttachCurrentThread();
540 if (!media_codec())
541 return false;
543 std::string codec_string = AudioCodecToAndroidMimeType(codec);
544 if (codec_string.empty())
545 return false;
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)) {
555 return false;
558 if (!Java_MediaCodecBridge_configureAudio(
559 env, media_codec(), j_format.obj(), media_crypto, 0, play_audio)) {
560 return false;
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)
573 return true;
575 JNIEnv* env = AttachCurrentThread();
576 switch (codec) {
577 case kCodecVorbis: {
578 if (extra_data[0] != 2) {
579 LOG(ERROR) << "Invalid number of vorbis headers before the codec "
580 << "header: " << extra_data[0];
581 return false;
584 size_t header_length[2];
585 // |total_length| keeps track of the total number of bytes before the last
586 // header.
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";
597 return false;
599 header_length[i] += size;
600 if (size < 0xFF)
601 break;
603 if (total_length >= extra_data_size) {
604 LOG(ERROR) << "Invalid vorbis header size in the extra data";
605 return false;
608 current_pos++;
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());
620 break;
622 case kCodecAAC: {
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.
627 uint8 profile = 0;
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";
633 return false;
635 if (0xf == frequency_index && !reader.SkipBits(24)) {
636 LOG(ERROR) << "Unable to parse AAC header";
637 return false;
639 if (!reader.ReadBits(4, &channel_config)) {
640 LOG(ERROR) << "Unable to parse AAC header";
641 return false;
644 if (profile < 1 || profile > 4 || frequency_index == 0xf ||
645 channel_config > 7) {
646 LOG(ERROR) << "Invalid AAC header";
647 return false;
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);
661 break;
663 case kCodecOpus: {
664 if (!extra_data || extra_data_size == 0 ||
665 codec_delay_ns < 0 || seek_preroll_ns < 0) {
666 LOG(ERROR) << "Invalid Opus Header";
667 return false;
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),
679 sizeof(int64_t));
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),
686 sizeof(int64_t));
687 Java_MediaCodecBridge_setCodecSpecificData(env, j_format, 2, csd2.obj());
688 break;
690 default:
691 LOG(ERROR) << "Invalid header encountered for codec: "
692 << AudioCodecToAndroidMimeType(codec);
693 return false;
695 return true;
698 int64 AudioCodecBridge::PlayOutputBuffer(int index, size_t size) {
699 DCHECK_LE(0, index);
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);
717 // static
718 AudioCodecBridge* AudioCodecBridge::Create(const AudioCodec& codec) {
719 if (!MediaCodecBridge::IsAvailable())
720 return NULL;
722 const std::string mime = AudioCodecToAndroidMimeType(codec);
723 return mime.empty() ? NULL : new AudioCodecBridge(mime);
726 // static
727 bool AudioCodecBridge::IsKnownUnaccelerated(const AudioCodec& codec) {
728 return MediaCodecBridge::IsKnownUnaccelerated(
729 AudioCodecToAndroidMimeType(codec), MEDIA_CODEC_DECODER);
732 // static
733 bool VideoCodecBridge::IsKnownUnaccelerated(const VideoCodec& codec,
734 MediaCodecDirection direction) {
735 return MediaCodecBridge::IsKnownUnaccelerated(
736 VideoCodecToAndroidMimeType(codec), direction);
739 // static
740 VideoCodecBridge* VideoCodecBridge::CreateDecoder(const VideoCodec& codec,
741 bool is_secure,
742 const gfx::Size& size,
743 jobject surface,
744 jobject media_crypto) {
745 if (!MediaCodecBridge::IsAvailable())
746 return NULL;
748 const std::string mime = VideoCodecToAndroidMimeType(codec);
749 if (mime.empty())
750 return NULL;
752 scoped_ptr<VideoCodecBridge> bridge(
753 new VideoCodecBridge(mime, is_secure, MEDIA_CODEC_DECODER));
754 if (!bridge->media_codec())
755 return NULL;
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(),
765 j_format.obj(),
766 surface,
767 media_crypto,
768 0)) {
769 return NULL;
772 return bridge->StartInternal() ? bridge.release() : NULL;
775 // static
776 VideoCodecBridge* VideoCodecBridge::CreateEncoder(const VideoCodec& codec,
777 const gfx::Size& size,
778 int bit_rate,
779 int frame_rate,
780 int i_frame_interval,
781 int color_format) {
782 if (!MediaCodecBridge::IsAvailable())
783 return NULL;
785 const std::string mime = VideoCodecToAndroidMimeType(codec);
786 if (mime.empty())
787 return NULL;
789 scoped_ptr<VideoCodecBridge> bridge(
790 new VideoCodecBridge(mime, false, MEDIA_CODEC_ENCODER));
791 if (!bridge->media_codec())
792 return NULL;
794 JNIEnv* env = AttachCurrentThread();
795 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime);
796 ScopedJavaLocalRef<jobject> j_format(
797 Java_MediaCodecBridge_createVideoEncoderFormat(env,
798 j_mime.obj(),
799 size.width(),
800 size.height(),
801 bit_rate,
802 frame_rate,
803 i_frame_interval,
804 color_format));
805 DCHECK(!j_format.is_null());
806 if (!Java_MediaCodecBridge_configureVideo(env,
807 bridge->media_codec(),
808 j_format.obj(),
809 NULL,
810 NULL,
811 kConfigureFlagEncode)) {
812 return NULL;
815 return bridge->StartInternal() ? bridge.release() : NULL;
818 VideoCodecBridge::VideoCodecBridge(const std::string& mime,
819 bool is_secure,
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)
836 return false;
837 else if (adaptive_playback_supported_for_testing_ > 0)
838 return true;
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);
848 } // namespace media