| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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. | |
| 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 // static | |
| 27 const std::string CodecTypeToAndroidMimeType(const std::string& codec) { | |
| 28 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"? | |
| 29 if (codec == "avc1") | |
| 30 return "video/avc"; | |
| 31 if (codec == "hvc1") | |
| 32 return "video/hevc"; | |
| 33 if (codec == "mp4a") | |
| 34 return "audio/mp4a-latm"; | |
| 35 if (codec == "vp8" || codec == "vp8.0") | |
| 36 return "video/x-vnd.on2.vp8"; | |
| 37 if (codec == "vp9" || codec == "vp9.0") | |
| 38 return "video/x-vnd.on2.vp9"; | |
| 39 if (codec == "vorbis") | |
| 40 return "audio/vorbis"; | |
| 41 if (codec == "opus") | |
| 42 return "audio/opus"; | |
| 43 return std::string(); | |
| 44 } | |
| 45 | |
| 46 // TODO(qinmin): using a map to help all the conversions in this class. | |
| 47 const std::string AndroidMimeTypeToCodecType(const std::string& mime) { | |
| 48 if (mime == "video/mp4v-es") | |
| 49 return "mp4v"; | |
| 50 if (mime == "video/avc") | |
| 51 return "avc1"; | |
| 52 if (mime == "video/hevc") | |
| 53 return "hvc1"; | |
| 54 if (mime == "video/x-vnd.on2.vp8") | |
| 55 return "vp8"; | |
| 56 if (mime == "video/x-vnd.on2.vp9") | |
| 57 return "vp9"; | |
| 58 if (mime == "audio/mp4a-latm") | |
| 59 return "mp4a"; | |
| 60 if (mime == "audio/mpeg") | |
| 61 return "mp3"; | |
| 62 if (mime == "audio/vorbis") | |
| 63 return "vorbis"; | |
| 64 if (mime == "audio/opus") | |
| 65 return "opus"; | |
| 66 return std::string(); | |
| 67 } | |
| 68 | |
| 69 std::string GetDefaultCodecName(const std::string& mime_type, | |
| 70 MediaCodecDirection direction) { | |
| 71 if (!MediaCodecUtil::IsMediaCodecAvailable()) | |
| 72 return std::string(); | |
| 73 | |
| 74 JNIEnv* env = AttachCurrentThread(); | |
| 75 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type); | |
| 76 ScopedJavaLocalRef<jstring> j_codec_name = | |
| 77 Java_MediaCodecUtil_getDefaultCodecName(env, j_mime.obj(), direction); | |
| 78 return ConvertJavaStringToUTF8(env, j_codec_name.obj()); | |
| 79 } | |
| 80 | |
| 81 bool SupportsGetName() { | |
| 82 // MediaCodec.getName() is only available on JB MR2 and greater. | |
| 83 return base::android::BuildInfo::GetInstance()->sdk_int() >= 18; | |
| 84 } | |
| 85 | |
| 86 // Represents supported codecs on android. | |
| 87 // TODO(qinmin): Currently the codecs string only contains one codec. Do we | |
| 88 // need to support codecs separated by comma. (e.g. "vp8" -> "vp8, vp8.0")? | |
| 89 struct CodecsInfo { | |
| 90 std::string codecs; // E.g. "vp8" or "avc1". | |
| 91 std::string name; // E.g. "OMX.google.vp8.decoder". | |
| 92 MediaCodecDirection direction; | |
| 93 }; | |
| 94 | |
| 95 // Get a list of supported codecs. | |
| 96 std::vector<CodecsInfo> GetCodecsInfo() { | |
| 97 std::vector<CodecsInfo> codecs_info; | |
| 98 if (!MediaCodecUtil::IsMediaCodecAvailable()) | |
| 99 return codecs_info; | |
| 100 | |
| 101 JNIEnv* env = AttachCurrentThread(); | |
| 102 std::string mime_type; | |
| 103 ScopedJavaLocalRef<jobjectArray> j_codec_info_array = | |
| 104 Java_MediaCodecUtil_getCodecsInfo(env); | |
| 105 jsize len = env->GetArrayLength(j_codec_info_array.obj()); | |
| 106 for (jsize i = 0; i < len; ++i) { | |
| 107 ScopedJavaLocalRef<jobject> j_info( | |
| 108 env, env->GetObjectArrayElement(j_codec_info_array.obj(), i)); | |
| 109 ScopedJavaLocalRef<jstring> j_codec_type = | |
| 110 Java_CodecInfo_codecType(env, j_info.obj()); | |
| 111 ConvertJavaStringToUTF8(env, j_codec_type.obj(), &mime_type); | |
| 112 ScopedJavaLocalRef<jstring> j_codec_name = | |
| 113 Java_CodecInfo_codecName(env, j_info.obj()); | |
| 114 CodecsInfo info; | |
| 115 info.codecs = AndroidMimeTypeToCodecType(mime_type); | |
| 116 ConvertJavaStringToUTF8(env, j_codec_name.obj(), &info.name); | |
| 117 info.direction = static_cast<MediaCodecDirection>( | |
| 118 Java_CodecInfo_direction(env, j_info.obj())); | |
| 119 codecs_info.push_back(info); | |
| 120 } | |
| 121 return codecs_info; | |
| 122 } | |
| 123 | |
| 124 // static | |
| 125 bool MediaCodecUtil::IsMediaCodecAvailable() { | |
| 126 // MediaCodec is only available on JB and greater. | |
| 127 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16) | |
| 128 return false; | |
| 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"; | |
| 134 } | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 bool MediaCodecUtil::SupportsSetParameters() { | |
| 140 // MediaCodec.setParameters() is only available starting with K. | |
| 141 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19; | |
| 142 } | |
| 143 | |
| 144 // static | |
| 145 std::set<int> MediaCodecUtil::GetEncoderColorFormats( | |
| 146 const std::string& mime_type) { | |
| 147 std::set<int> color_formats; | |
| 148 if (!IsMediaCodecAvailable()) | |
| 149 return color_formats; | |
| 150 | |
| 151 JNIEnv* env = AttachCurrentThread(); | |
| 152 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type); | |
| 153 ScopedJavaLocalRef<jintArray> j_color_format_array = | |
| 154 Java_MediaCodecUtil_getEncoderColorFormatsForMime(env, j_mime.obj()); | |
| 155 | |
| 156 if (j_color_format_array.obj()) { | |
| 157 std::vector<int> formats; | |
| 158 JavaIntArrayToIntVector(env, j_color_format_array.obj(), &formats); | |
| 159 color_formats = std::set<int>(formats.begin(), formats.end()); | |
| 160 } | |
| 161 | |
| 162 return color_formats; | |
| 163 } | |
| 164 | |
| 165 // static | |
| 166 bool MediaCodecUtil::CanDecode(const std::string& codec, bool is_secure) { | |
| 167 if (!IsMediaCodecAvailable()) | |
| 168 return false; | |
| 169 | |
| 170 JNIEnv* env = AttachCurrentThread(); | |
| 171 std::string mime = CodecTypeToAndroidMimeType(codec); | |
| 172 if (mime.empty()) | |
| 173 return false; | |
| 174 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime); | |
| 175 return Java_MediaCodecUtil_canDecode(env, j_mime.obj(), is_secure); | |
| 176 } | |
| 177 | |
| 178 // static | |
| 179 bool MediaCodecUtil::IsKnownUnaccelerated(const std::string& mime_type, | |
| 180 MediaCodecDirection direction) { | |
| 181 if (!IsMediaCodecAvailable()) | |
| 182 return true; | |
| 183 | |
| 184 std::string codec_name; | |
| 185 if (SupportsGetName()) { | |
| 186 codec_name = GetDefaultCodecName(mime_type, direction); | |
| 187 } else { | |
| 188 std::string codec_type = AndroidMimeTypeToCodecType(mime_type); | |
| 189 std::vector<CodecsInfo> codecs_info = GetCodecsInfo(); | |
| 190 for (size_t i = 0; i < codecs_info.size(); ++i) { | |
| 191 if (codecs_info[i].codecs == codec_type && | |
| 192 codecs_info[i].direction == direction) { | |
| 193 codec_name = codecs_info[i].name; | |
| 194 break; | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 DVLOG(1) << __PRETTY_FUNCTION__ << "Default codec for " << mime_type << " : " | |
| 199 << codec_name; | |
| 200 // It would be nice if MediaCodecInfo externalized some notion of | |
| 201 // HW-acceleration but it doesn't. Android Media guidance is that the | |
| 202 // "OMX.google" prefix is always used for SW decoders, so that's what we | |
| 203 // use. "OMX.SEC.*" codec is Samsung software implementation - report it | |
| 204 // as unaccelerated as well. Also temporary blacklist Exynos and MediaTek | |
| 205 // devices while HW decoder video freezes and distortions are | |
| 206 // investigated - http://crbug.com/446974. | |
| 207 if (codec_name.length() > 0) { | |
| 208 return (base::StartsWith(codec_name, "OMX.google.", | |
| 209 base::CompareCase::SENSITIVE) || | |
| 210 base::StartsWith(codec_name, "OMX.SEC.", | |
| 211 base::CompareCase::SENSITIVE) || | |
| 212 base::StartsWith(codec_name, "OMX.MTK.", | |
| 213 base::CompareCase::SENSITIVE) || | |
| 214 base::StartsWith(codec_name, "OMX.Exynos.", | |
| 215 base::CompareCase::SENSITIVE)); | |
| 216 } | |
| 217 return true; | |
| 218 } | |
| 219 | |
| 220 // static | |
| 221 bool MediaCodecUtil::RegisterMediaCodecUtil(JNIEnv* env) { | |
| 222 return RegisterNativesImpl(env); | |
| 223 } | |
| 224 | |
| 225 } // namespace media | |
| OLD | NEW |