Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
|
xhwang
2015/11/30 23:11:50
2015
qinmin
2015/12/03 22:46:23
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/android/media_codec_util.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 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/logging.h" | |
| 15 #include "base/strings/string_util.h" | |
| 16 #include "jni/MediaCodecUtil_jni.h" | |
| 17 | |
| 18 using base::android::AttachCurrentThread; | |
| 19 using base::android::ConvertJavaStringToUTF8; | |
| 20 using base::android::ConvertUTF8ToJavaString; | |
| 21 using base::android::JavaIntArrayToIntVector; | |
| 22 using base::android::ScopedJavaLocalRef; | |
| 23 | |
| 24 namespace media { | |
| 25 | |
| 26 const std::string CodecTypeToAndroidMimeType(const std::string& codec) { | |
| 27 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"? | |
| 28 if (codec == "avc1") | |
| 29 return "video/avc"; | |
| 30 if (codec == "hvc1") | |
| 31 return "video/hevc"; | |
| 32 if (codec == "mp4a") | |
| 33 return "audio/mp4a-latm"; | |
| 34 if (codec == "vp8" || codec == "vp8.0") | |
| 35 return "video/x-vnd.on2.vp8"; | |
| 36 if (codec == "vp9" || codec == "vp9.0") | |
| 37 return "video/x-vnd.on2.vp9"; | |
| 38 if (codec == "vorbis") | |
| 39 return "audio/vorbis"; | |
| 40 if (codec == "opus") | |
| 41 return "audio/opus"; | |
| 42 return std::string(); | |
| 43 } | |
| 44 | |
| 45 // TODO(qinmin): using a map to help all the conversions in this class. | |
| 46 const std::string AndroidMimeTypeToCodecType(const std::string& mime) { | |
| 47 if (mime == "video/mp4v-es") | |
| 48 return "mp4v"; | |
| 49 if (mime == "video/avc") | |
| 50 return "avc1"; | |
| 51 if (mime == "video/hevc") | |
| 52 return "hvc1"; | |
| 53 if (mime == "video/x-vnd.on2.vp8") | |
| 54 return "vp8"; | |
| 55 if (mime == "video/x-vnd.on2.vp9") | |
| 56 return "vp9"; | |
| 57 if (mime == "audio/mp4a-latm") | |
| 58 return "mp4a"; | |
| 59 if (mime == "audio/mpeg") | |
| 60 return "mp3"; | |
| 61 if (mime == "audio/vorbis") | |
| 62 return "vorbis"; | |
| 63 if (mime == "audio/opus") | |
| 64 return "opus"; | |
| 65 return std::string(); | |
| 66 } | |
| 67 | |
| 68 bool IsMediaCodecAvailable() { | |
| 69 // MediaCodec is only available on JB and greater. | |
| 70 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16) | |
| 71 return false; | |
| 72 // Blacklist some devices on Jellybean as for MediaCodec support is buggy. | |
| 73 // http://crbug.com/365494. | |
| 74 if (base::android::BuildInfo::GetInstance()->sdk_int() == 16) { | |
| 75 std::string model(base::android::BuildInfo::GetInstance()->model()); | |
| 76 return model != "GT-I9100" && model != "GT-I9300" && model != "GT-N7000"; | |
| 77 } | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 bool SupportsSetParameters() { | |
| 82 // MediaCodec.setParameters() is only available starting with K. | |
| 83 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19; | |
| 84 } | |
| 85 | |
| 86 bool SupportsGetName() { | |
| 87 // MediaCodec.getName() is only available on JB MR2 and greater. | |
| 88 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18; | |
| 89 } | |
| 90 | |
| 91 std::vector<CodecsInfo> GetCodecsInfo() { | |
| 92 std::vector<CodecsInfo> codecs_info; | |
| 93 if (!IsMediaCodecAvailable()) | |
| 94 return codecs_info; | |
| 95 | |
| 96 JNIEnv* env = AttachCurrentThread(); | |
| 97 std::string mime_type; | |
| 98 ScopedJavaLocalRef<jobjectArray> j_codec_info_array = | |
| 99 Java_MediaCodecUtil_getCodecsInfo(env); | |
| 100 jsize len = env->GetArrayLength(j_codec_info_array.obj()); | |
| 101 for (jsize i = 0; i < len; ++i) { | |
| 102 ScopedJavaLocalRef<jobject> j_info( | |
| 103 env, env->GetObjectArrayElement(j_codec_info_array.obj(), i)); | |
| 104 ScopedJavaLocalRef<jstring> j_codec_type = | |
| 105 Java_CodecInfo_codecType(env, j_info.obj()); | |
| 106 ConvertJavaStringToUTF8(env, j_codec_type.obj(), &mime_type); | |
| 107 ScopedJavaLocalRef<jstring> j_codec_name = | |
| 108 Java_CodecInfo_codecName(env, j_info.obj()); | |
| 109 CodecsInfo info; | |
| 110 info.codecs = AndroidMimeTypeToCodecType(mime_type); | |
| 111 ConvertJavaStringToUTF8(env, j_codec_name.obj(), &info.name); | |
| 112 info.direction = static_cast<MediaCodecDirection>( | |
| 113 Java_CodecInfo_direction(env, j_info.obj())); | |
| 114 codecs_info.push_back(info); | |
| 115 } | |
| 116 return codecs_info; | |
| 117 } | |
| 118 | |
| 119 std::string GetDefaultCodecName( | |
| 120 const std::string& mime_type, | |
| 121 MediaCodecDirection direction) { | |
| 122 if (!IsMediaCodecAvailable()) | |
| 123 return std::string(); | |
| 124 | |
| 125 JNIEnv* env = AttachCurrentThread(); | |
| 126 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type); | |
| 127 ScopedJavaLocalRef<jstring> j_codec_name = | |
| 128 Java_MediaCodecUtil_getDefaultCodecName(env, j_mime.obj(), direction); | |
| 129 return ConvertJavaStringToUTF8(env, j_codec_name.obj()); | |
| 130 } | |
| 131 | |
| 132 std::set<int> GetEncoderColorFormats( | |
| 133 const std::string& mime_type) { | |
| 134 std::set<int> color_formats; | |
| 135 if (!IsMediaCodecAvailable()) | |
| 136 return color_formats; | |
| 137 | |
| 138 JNIEnv* env = AttachCurrentThread(); | |
| 139 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type); | |
| 140 ScopedJavaLocalRef<jintArray> j_color_format_array = | |
| 141 Java_MediaCodecUtil_getEncoderColorFormatsForMime(env, j_mime.obj()); | |
| 142 | |
| 143 if (j_color_format_array.obj()) { | |
| 144 std::vector<int> formats; | |
| 145 JavaIntArrayToIntVector(env, j_color_format_array.obj(), &formats); | |
| 146 color_formats = std::set<int>(formats.begin(), formats.end()); | |
| 147 } | |
| 148 | |
| 149 return color_formats; | |
| 150 } | |
| 151 | |
| 152 bool CanDecode(const std::string& codec, bool is_secure) { | |
| 153 if (!IsMediaCodecAvailable()) | |
| 154 return false; | |
| 155 | |
| 156 JNIEnv* env = AttachCurrentThread(); | |
| 157 std::string mime = CodecTypeToAndroidMimeType(codec); | |
| 158 if (mime.empty()) | |
| 159 return false; | |
| 160 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime); | |
| 161 return Java_MediaCodecUtil_canDecode(env, j_mime.obj(), is_secure); | |
| 162 } | |
| 163 | |
| 164 bool RegisterMediaCodecUtil(JNIEnv* env) { | |
| 165 return RegisterNativesImpl(env); | |
| 166 } | |
| 167 | |
| 168 } // namespace media | |
| OLD | NEW |