Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/base/android/media_codec_util.h" | 5 #include "media/base/android/media_codec_util.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/android/build_info.h" | 11 #include "base/android/build_info.h" |
| 12 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 13 #include "base/android/jni_array.h" | 13 #include "base/android/jni_array.h" |
| 14 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "jni/MediaCodecUtil_jni.h" | 17 #include "jni/MediaCodecUtil_jni.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 using base::android::AttachCurrentThread; | 20 using base::android::AttachCurrentThread; |
| 21 using base::android::ConvertJavaStringToUTF8; | 21 using base::android::ConvertJavaStringToUTF8; |
| 22 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
| 23 using base::android::JavaIntArrayToIntVector; | 23 using base::android::JavaIntArrayToIntVector; |
| 24 using base::android::ScopedJavaLocalRef; | 24 using base::android::ScopedJavaLocalRef; |
| 25 | 25 |
| 26 namespace media { | 26 namespace media { |
| 27 | 27 |
| 28 namespace { | |
| 29 | |
| 30 const char kMp4aMimeType[] = "audio/mp4a-latm"; | |
| 31 const char kOpusMimeType[] = "audio/opus"; | |
| 32 const char kVorbisMimeType[] = "audio/vorbis"; | |
| 33 const char kAvcMimeType[] = "video/avc"; | |
| 34 const char kHevcMimeType[] = "video/hevc"; | |
| 35 const char kVp8MimeType[] = "video/x-vnd.on2.vp8"; | |
| 36 const char kVp9MimeType[] = "video/x-vnd.on2.vp9"; | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 28 static std::string CodecTypeToAndroidMimeType(const std::string& codec) { | 40 static std::string CodecTypeToAndroidMimeType(const std::string& codec) { |
| 29 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"? | 41 // TODO(xhwang): Shall we handle more detailed strings like "mp4a.40.2"? |
| 30 if (codec == "avc1") | 42 if (codec == "avc1") |
| 31 return "video/avc"; | 43 return kAvcMimeType; |
| 32 if (codec == "hvc1") | 44 if (codec == "hvc1") |
| 33 return "video/hevc"; | 45 return kHevcMimeType; |
| 34 if (codec == "mp4a") | 46 if (codec == "mp4a") |
| 35 return "audio/mp4a-latm"; | 47 return kMp4aMimeType; |
| 36 if (codec == "vp8" || codec == "vp8.0") | 48 if (codec == "vp8" || codec == "vp8.0") |
| 37 return "video/x-vnd.on2.vp8"; | 49 return kVp8MimeType; |
| 38 if (codec == "vp9" || codec == "vp9.0") | 50 if (codec == "vp9" || codec == "vp9.0") |
| 39 return "video/x-vnd.on2.vp9"; | 51 return kVp9MimeType; |
| 40 if (codec == "vorbis") | 52 if (codec == "vorbis") |
| 41 return "audio/vorbis"; | 53 return kVorbisMimeType; |
| 42 if (codec == "opus") | 54 if (codec == "opus") |
| 43 return "audio/opus"; | 55 return kOpusMimeType; |
| 56 DLOG(WARNING) << "Cannot convert codec to Android MIME type: " << codec; | |
| 44 return std::string(); | 57 return std::string(); |
| 45 } | 58 } |
| 46 | 59 |
| 60 static bool IsMimeTypeSupported(const std::string& android_mime_type) { | |
|
ddorwin
2016/04/19 23:54:22
Since the point is to confirm that this is a suppo
qinmin
2016/04/21 22:12:32
Done.
| |
| 61 std::vector<std::string> supported{ | |
| 62 kMp4aMimeType, kOpusMimeType, kVorbisMimeType, kAvcMimeType, | |
| 63 kHevcMimeType, kVp8MimeType, kVp9MimeType}; | |
| 64 return std::find(supported.begin(), supported.end(), android_mime_type) != | |
| 65 supported.end(); | |
| 66 } | |
| 67 | |
| 47 static std::string GetDefaultCodecName(const std::string& mime_type, | 68 static std::string GetDefaultCodecName(const std::string& mime_type, |
| 48 MediaCodecDirection direction) { | 69 MediaCodecDirection direction) { |
| 49 DCHECK(MediaCodecUtil::IsMediaCodecAvailable()); | 70 DCHECK(MediaCodecUtil::IsMediaCodecAvailable()); |
| 50 JNIEnv* env = AttachCurrentThread(); | 71 JNIEnv* env = AttachCurrentThread(); |
| 51 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type); | 72 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type); |
| 52 ScopedJavaLocalRef<jstring> j_codec_name = | 73 ScopedJavaLocalRef<jstring> j_codec_name = |
| 53 Java_MediaCodecUtil_getDefaultCodecName(env, j_mime.obj(), direction); | 74 Java_MediaCodecUtil_getDefaultCodecName(env, j_mime.obj(), direction); |
| 54 return ConvertJavaStringToUTF8(env, j_codec_name.obj()); | 75 return ConvertJavaStringToUTF8(env, j_codec_name.obj()); |
| 55 } | 76 } |
| 56 | 77 |
| 57 static bool IsDecoderSupportedByDevice(const std::string& mime_type) { | 78 static bool IsDecoderSupportedByDevice(const std::string& android_mime_type) { |
| 58 DCHECK(MediaCodecUtil::IsMediaCodecAvailable()); | 79 DCHECK(MediaCodecUtil::IsMediaCodecAvailable()); |
| 80 DCHECK(IsMimeTypeSupported(android_mime_type)); | |
| 59 JNIEnv* env = AttachCurrentThread(); | 81 JNIEnv* env = AttachCurrentThread(); |
| 60 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime_type); | 82 ScopedJavaLocalRef<jstring> j_mime = |
| 83 ConvertUTF8ToJavaString(env, android_mime_type); | |
| 61 return Java_MediaCodecUtil_isDecoderSupportedForDevice(env, j_mime.obj()); | 84 return Java_MediaCodecUtil_isDecoderSupportedForDevice(env, j_mime.obj()); |
| 62 } | 85 } |
| 63 | 86 |
| 64 // static | 87 // static |
| 65 bool MediaCodecUtil::IsMediaCodecAvailable() { | 88 bool MediaCodecUtil::IsMediaCodecAvailable() { |
| 66 // MediaCodec is only available on JB and greater. | 89 // MediaCodec is only available on JB and greater. |
| 67 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16) | 90 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16) |
| 68 return false; | 91 return false; |
| 69 // Blacklist some devices on Jellybean as for MediaCodec support is buggy. | 92 // Blacklist some devices on Jellybean as for MediaCodec support is buggy. |
| 70 // http://crbug.com/365494. | 93 // http://crbug.com/365494. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 | 132 |
| 110 JNIEnv* env = AttachCurrentThread(); | 133 JNIEnv* env = AttachCurrentThread(); |
| 111 std::string mime = CodecTypeToAndroidMimeType(codec); | 134 std::string mime = CodecTypeToAndroidMimeType(codec); |
| 112 if (mime.empty()) | 135 if (mime.empty()) |
| 113 return false; | 136 return false; |
| 114 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime); | 137 ScopedJavaLocalRef<jstring> j_mime = ConvertUTF8ToJavaString(env, mime); |
| 115 return Java_MediaCodecUtil_canDecode(env, j_mime.obj(), is_secure); | 138 return Java_MediaCodecUtil_canDecode(env, j_mime.obj(), is_secure); |
| 116 } | 139 } |
| 117 | 140 |
| 118 // static | 141 // static |
| 119 bool MediaCodecUtil::IsKnownUnaccelerated(const std::string& mime_type, | 142 bool MediaCodecUtil::IsKnownUnaccelerated(const std::string& android_mime_type, |
| 120 MediaCodecDirection direction) { | 143 MediaCodecDirection direction) { |
| 144 DCHECK(IsMimeTypeSupported(android_mime_type)); | |
| 121 if (!IsMediaCodecAvailable()) | 145 if (!IsMediaCodecAvailable()) |
| 122 return true; | 146 return true; |
| 123 | 147 |
| 124 std::string codec_name = GetDefaultCodecName(mime_type, direction); | 148 std::string codec_name = GetDefaultCodecName(android_mime_type, direction); |
| 125 DVLOG(1) << __FUNCTION__ << "Default codec for " << mime_type << " : " | 149 DVLOG(1) << __FUNCTION__ << "Default codec for " << android_mime_type << " : " |
| 126 << codec_name << ", direction: " << direction; | 150 << codec_name << ", direction: " << direction; |
| 127 if (!codec_name.size()) | 151 if (!codec_name.size()) |
| 128 return true; | 152 return true; |
| 129 | 153 |
| 130 // MediaTek hardware vp8 is known slower than the software implementation. | 154 // MediaTek hardware vp8 is known slower than the software implementation. |
| 131 // MediaTek hardware vp9 is known crashy, see http://crbug.com/446974 and | 155 // MediaTek hardware vp9 is known crashy, see http://crbug.com/446974 and |
| 132 // http://crbug.com/597836. | 156 // http://crbug.com/597836. |
| 133 if (base::StartsWith(codec_name, "OMX.MTK.", base::CompareCase::SENSITIVE)) { | 157 if (base::StartsWith(codec_name, "OMX.MTK.", base::CompareCase::SENSITIVE)) { |
| 134 if (mime_type == "video/x-vnd.on2.vp8") | 158 if (android_mime_type == kVp8MimeType) |
| 135 return true; | 159 return true; |
| 136 | 160 |
| 137 if (mime_type == "video/x-vnd.on2.vp9") | 161 if (android_mime_type == kVp9MimeType) |
| 138 return base::android::BuildInfo::GetInstance()->sdk_int() < 21; | 162 return base::android::BuildInfo::GetInstance()->sdk_int() < 21; |
| 139 | 163 |
| 140 return false; | 164 return false; |
| 141 } | 165 } |
| 142 | 166 |
| 143 // It would be nice if MediaCodecInfo externalized some notion of | 167 // It would be nice if MediaCodecInfo externalized some notion of |
| 144 // HW-acceleration but it doesn't. Android Media guidance is that the | 168 // HW-acceleration but it doesn't. Android Media guidance is that the |
| 145 // "OMX.google" prefix is always used for SW decoders, so that's what we | 169 // "OMX.google" prefix is always used for SW decoders, so that's what we |
| 146 // use. "OMX.SEC.*" codec is Samsung software implementation - report it | 170 // use. "OMX.SEC.*" codec is Samsung software implementation - report it |
| 147 // as unaccelerated as well. | 171 // as unaccelerated as well. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 171 return (spec.find("m3u8") != std::string::npos); | 195 return (spec.find("m3u8") != std::string::npos); |
| 172 } | 196 } |
| 173 | 197 |
| 174 // static | 198 // static |
| 175 bool MediaCodecUtil::RegisterMediaCodecUtil(JNIEnv* env) { | 199 bool MediaCodecUtil::RegisterMediaCodecUtil(JNIEnv* env) { |
| 176 return RegisterNativesImpl(env); | 200 return RegisterNativesImpl(env); |
| 177 } | 201 } |
| 178 | 202 |
| 179 // static | 203 // static |
| 180 bool MediaCodecUtil::IsVp8DecoderAvailable() { | 204 bool MediaCodecUtil::IsVp8DecoderAvailable() { |
| 181 return IsMediaCodecAvailable() && IsDecoderSupportedByDevice("vp8"); | 205 return IsMediaCodecAvailable() && IsDecoderSupportedByDevice(kVp8MimeType); |
| 182 } | 206 } |
| 183 | 207 |
| 184 // static | 208 // static |
| 185 bool MediaCodecUtil::IsVp8EncoderAvailable() { | 209 bool MediaCodecUtil::IsVp8EncoderAvailable() { |
| 186 // Currently the vp8 encoder and decoder blacklists cover the same devices, | 210 // Currently the vp8 encoder and decoder blacklists cover the same devices, |
| 187 // but we have a second method for clarity in future issues. | 211 // but we have a second method for clarity in future issues. |
| 188 return IsVp8DecoderAvailable(); | 212 return IsVp8DecoderAvailable(); |
| 189 } | 213 } |
| 190 | 214 |
| 191 // static | 215 // static |
| 192 bool MediaCodecUtil::IsVp9DecoderAvailable() { | 216 bool MediaCodecUtil::IsVp9DecoderAvailable() { |
| 193 return IsMediaCodecAvailable() && IsDecoderSupportedByDevice("vp9"); | 217 return IsMediaCodecAvailable() && IsDecoderSupportedByDevice(kVp9MimeType); |
| 194 } | 218 } |
| 195 | 219 |
| 196 // static | 220 // static |
| 197 bool MediaCodecUtil::IsSurfaceViewOutputSupported() { | 221 bool MediaCodecUtil::IsSurfaceViewOutputSupported() { |
| 198 // Disable SurfaceView output for the Samsung Galaxy S3; it does not work | 222 // Disable SurfaceView output for the Samsung Galaxy S3; it does not work |
| 199 // well enough for even 360p24 H264 playback. http://crbug.com/602870. | 223 // well enough for even 360p24 H264 playback. http://crbug.com/602870. |
| 200 // | 224 // |
| 201 // Notably this is codec agnostic at present, so any devices added to | 225 // Notably this is codec agnostic at present, so any devices added to |
| 202 // the blacklist will avoid trying to play any codecs on SurfaceView. If | 226 // the blacklist will avoid trying to play any codecs on SurfaceView. If |
| 203 // needed in the future this can be expanded to be codec specific. | 227 // needed in the future this can be expanded to be codec specific. |
| 204 return !base::StartsWith(base::android::BuildInfo::GetInstance()->model(), | 228 return !base::StartsWith(base::android::BuildInfo::GetInstance()->model(), |
| 205 "GT-I9300", base::CompareCase::INSENSITIVE_ASCII); | 229 "GT-I9300", base::CompareCase::INSENSITIVE_ASCII); |
| 206 } | 230 } |
| 207 | 231 |
| 208 } // namespace media | 232 } // namespace media |
| OLD | NEW |