Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1526)

Unified Diff: content/renderer/media/crypto/key_systems.cc

Issue 557723003: Implement Chromium side of MediaKeys.isTypeSupported(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Requested changes. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/media/crypto/key_systems.cc
diff --git a/content/renderer/media/crypto/key_systems.cc b/content/renderer/media/crypto/key_systems.cc
index a15e0b3578172be5e3ddcbaa4dc6e41a307ebf21..74e9224c26329b8880a9ba598a3ac4b7a8b9a299 100644
--- a/content/renderer/media/crypto/key_systems.cc
+++ b/content/renderer/media/crypto/key_systems.cc
@@ -13,7 +13,7 @@
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "content/public/common/content_client.h"
-#include "content/public/common/eme_codec.h"
+#include "content/public/common/eme_constants.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/public/renderer/key_system_info.h"
#include "content/renderer/media/crypto/key_systems_support_uma.h"
@@ -30,15 +30,26 @@ const char kClearKeyKeySystem[] = "org.w3.clearkey";
const char kPrefixedClearKeyKeySystem[] = "webkit-org.w3.clearkey";
const char kUnsupportedClearKeyKeySystem[] = "unsupported-org.w3.clearkey";
-struct CodecMask {
- const char* type;
- EmeCodec mask;
+struct NamedInitDataType {
+ const char* name;
+ EmeInitDataType type;
};
-// Mapping between container types and the masks of associated codecs.
+// Mapping between initialization data types names and enum values.
ddorwin 2014/09/23 22:48:16 Add a comment to update IsSaneInitDataTypeWithCont
sandersd (OOO until July 31) 2014/09/24 22:22:34 Done.
+static NamedInitDataType kInitDataTypes[] = {
+ {"cenc", EME_INIT_DATA_TYPE_CENC},
ddorwin 2014/09/23 22:48:16 ifdef - this will fail to compile. Also, remove th
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
+ {"webm", EME_INIT_DATA_TYPE_WEBM},
ddorwin 2014/09/23 22:48:17 ditto on swapping order
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
+};
+
+struct NamedCodec {
+ const char* name;
+ EmeCodec type;
+};
+
+// Mapping between containers and their codecs.
// Only audio codec can belong to a "audio/*" container. Both audio and video
// codecs can belong to a "video/*" container.
-CodecMask kContainerCodecMasks[] = {
+static NamedCodec kContainerCodecs[] = {
{"audio/webm", EME_CODEC_WEBM_AUDIO_ALL},
{"video/webm", EME_CODEC_WEBM_ALL},
#if defined(USE_PROPRIETARY_CODECS)
@@ -47,8 +58,8 @@ CodecMask kContainerCodecMasks[] = {
#endif // defined(USE_PROPRIETARY_CODECS)
};
-// Mapping between codec types and their masks.
-CodecMask kCodecMasks[] = {
+// Mapping between codec names and enum values.
+static NamedCodec kCodecs[] = {
{"vorbis", EME_CODEC_WEBM_VORBIS},
{"vp8", EME_CODEC_WEBM_VP8},
{"vp8.0", EME_CODEC_WEBM_VP8},
@@ -68,6 +79,7 @@ static void AddClearKey(std::vector<KeySystemInfo>* concrete_key_systems) {
// http://developer.android.com/guide/appendix/media-formats.html
// VP9 support is device dependent.
+ info.supported_init_data_types = EME_INIT_DATA_TYPE_WEBM;
info.supported_codecs = EME_CODEC_WEBM_ALL;
#if defined(OS_ANDROID)
@@ -77,6 +89,7 @@ static void AddClearKey(std::vector<KeySystemInfo>* concrete_key_systems) {
#endif // defined(OS_ANDROID)
#if defined(USE_PROPRIETARY_CODECS)
+ info.supported_init_data_types |= EME_INIT_DATA_TYPE_CENC;
info.supported_codecs |= EME_CODEC_MP4_ALL;
#endif // defined(USE_PROPRIETARY_CODECS)
@@ -93,6 +106,12 @@ class KeySystems {
bool IsConcreteSupportedKeySystem(const std::string& key_system);
+ bool IsSupportedKeySystem(const std::string& key_system);
+
+ bool IsSupportedKeySystemWithInitDataType(
+ const std::string& key_system,
+ const std::string& init_data_type);
+
bool IsSupportedKeySystemWithMediaMimeType(
const std::string& mime_type,
const std::vector<std::string>& codecs,
@@ -119,6 +138,7 @@ class KeySystems {
#if defined(ENABLE_PEPPER_CDMS)
const std::string& pepper_type,
#endif
+ SupportedInitDataTypes supported_init_data_types,
SupportedCodecs supported_codecs,
const std::string& parent_key_system);
@@ -132,17 +152,19 @@ class KeySystems {
#if defined(ENABLE_PEPPER_CDMS)
std::string pepper_type;
#endif
+ SupportedInitDataTypes supported_init_data_types;
SupportedCodecs supported_codecs;
};
ddorwin 2014/09/23 22:48:16 Is there a reason you removed the typedefs? They w
sandersd (OOO until July 31) 2014/09/24 22:22:34 Only one of them was used more than once in the co
- typedef base::hash_map<std::string, KeySystemProperties>
- KeySystemPropertiesMap;
- typedef base::hash_map<std::string, std::string> ParentKeySystemMap;
- typedef base::hash_map<std::string, EmeCodec> CodecMaskMap;
-
KeySystems();
~KeySystems() {}
+ EmeInitDataType LookupInitDataType(const std::string& init_data_type) const;
ddorwin 2014/09/23 22:48:16 nit: GetInitDataTypeForString()?
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
+ SupportedCodecs LookupContainer(const std::string& container) const;
ddorwin 2014/09/23 22:48:16 In the future, it'd be nice to have a container en
ddorwin 2014/09/23 22:48:17 Is this more GetCodecsMaskForContainer()?
sandersd (OOO until July 31) 2014/09/24 22:22:34 Done.
sandersd (OOO until July 31) 2014/09/24 22:22:34 Done.
+ EmeCodec LookupCodec(const std::string& codec) const;
ddorwin 2014/09/23 22:48:17 nit: GetCodecForString() or ConvertStringToCodec()
sandersd (OOO until July 31) 2014/09/24 22:22:34 Done.
+
+ const std::string& GetConcreteKeySystem(const std::string& key_system) const;
ddorwin 2014/09/23 22:48:17 nit: ...Name
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
+
// Returns whether a |container| type is supported by checking
// |key_system_supported_codecs|.
// TODO(xhwang): Update this to actually check initDataType support.
@@ -157,16 +179,17 @@ class KeySystems {
SupportedCodecs key_system_supported_codecs) const;
// Map from key system string to capabilities.
- KeySystemPropertiesMap concrete_key_system_map_;
+ base::hash_map<std::string, KeySystemProperties> concrete_key_system_map_;
// Map from parent key system to the concrete key system that should be used
// to represent its capabilities.
- ParentKeySystemMap parent_key_system_map_;
+ base::hash_map<std::string, std::string> parent_key_system_map_;
KeySystemsSupportUMA key_systems_support_uma_;
- CodecMaskMap container_codec_masks_;
- CodecMaskMap codec_masks_;
+ base::hash_map<std::string, SupportedCodecs> container_codecs_;
ddorwin 2014/09/23 22:48:16 container_to_codecs_map_?
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
+ base::hash_map<std::string, EmeCodec> codecs_;
ddorwin 2014/09/23 22:48:17 codec_string_map_?
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
+ base::hash_map<std::string, EmeInitDataType> init_data_types_;
ddorwin 2014/09/23 22:48:16 ...map_
ddorwin 2014/09/23 22:48:17 make this one first.
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
bool needs_update_;
base::Time last_update_time_;
@@ -188,18 +211,20 @@ KeySystems& KeySystems::GetInstance() {
// Because we use a LazyInstance, the key systems info must be populated when
// the instance is lazily initiated.
KeySystems::KeySystems() : needs_update_(true) {
- // Build container and codec masks for quick look up.
- for (size_t i = 0; i < arraysize(kContainerCodecMasks); ++i) {
- const CodecMask& container_codec_mask = kContainerCodecMasks[i];
- DCHECK(container_codec_masks_.find(container_codec_mask.type) ==
- container_codec_masks_.end());
- container_codec_masks_[container_codec_mask.type] =
- container_codec_mask.mask;
+ for (size_t i = 0; i < arraysize(kContainerCodecs); ++i) {
ddorwin 2014/09/23 22:48:17 You might be able to use a template function. Howe
sandersd (OOO until July 31) 2014/09/24 22:22:33 Sounds good, I've mentioned that in the bug for re
+ const std::string& name = kContainerCodecs[i].name;
+ DCHECK(container_codecs_.find(name) == container_codecs_.end());
+ container_codecs_[name] = kContainerCodecs[i].type;
+ }
+ for (size_t i = 0; i < arraysize(kCodecs); ++i) {
+ const std::string& name = kCodecs[i].name;
+ DCHECK(codecs_.find(name) == codecs_.end());
+ codecs_[name] = kCodecs[i].type;
}
- for (size_t i = 0; i < arraysize(kCodecMasks); ++i) {
- const CodecMask& codec_mask = kCodecMasks[i];
- DCHECK(codec_masks_.find(codec_mask.type) == codec_masks_.end());
- codec_masks_[codec_mask.type] = codec_mask.mask;
+ for (size_t i = 0; i < arraysize(kInitDataTypes); ++i) {
ddorwin 2014/09/23 22:48:16 move to line 214
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
+ const std::string& name = kInitDataTypes[i].name;
+ DCHECK(init_data_types_.find(name) == init_data_types_.end());
+ init_data_types_[name] = kInitDataTypes[i].type;
}
UpdateSupportedKeySystems();
@@ -209,6 +234,41 @@ KeySystems::KeySystems() : needs_update_(true) {
#endif // defined(WIDEVINE_CDM_AVAILABLE)
}
+EmeInitDataType KeySystems::LookupInitDataType(
+ const std::string& init_data_type) const {
+ base::hash_map<std::string, EmeInitDataType>::const_iterator iter =
+ init_data_types_.find(init_data_type);
+ if (iter != init_data_types_.end())
+ return iter->second;
+ return EME_INIT_DATA_TYPE_NONE;
+}
+
+SupportedCodecs KeySystems::LookupContainer(
+ const std::string& container) const {
+ base::hash_map<std::string, SupportedCodecs>::const_iterator iter =
+ container_codecs_.find(container);
+ if (iter != container_codecs_.end())
+ return iter->second;
+ return EME_CODEC_NONE;
+}
+
+EmeCodec KeySystems::LookupCodec(const std::string& codec) const {
+ base::hash_map<std::string, EmeCodec>::const_iterator iter =
+ codecs_.find(codec);
+ if (iter != codecs_.end())
+ return iter->second;
+ return EME_CODEC_NONE;
+}
+
+const std::string& KeySystems::GetConcreteKeySystem(
+ const std::string& key_system) const {
+ base::hash_map<std::string, std::string>::const_iterator iter =
+ parent_key_system_map_.find(key_system);
+ if (iter != parent_key_system_map_.end())
+ return iter->second;
+ return key_system;
+}
+
void KeySystems::UpdateIfNeeded() {
#if defined(WIDEVINE_CDM_AVAILABLE)
DCHECK(thread_checker_.CalledOnValidThread());
@@ -264,6 +324,7 @@ void KeySystems::AddConcreteSupportedKeySystems(
key_system_info.pepper_type,
#endif
key_system_info.supported_codecs,
+ key_system_info.supported_init_data_types,
key_system_info.parent_key_system);
}
}
@@ -275,6 +336,7 @@ void KeySystems::AddConcreteSupportedKeySystem(
const std::string& pepper_type,
#endif
SupportedCodecs supported_codecs,
+ SupportedInitDataTypes supported_init_data_types,
ddorwin 2014/09/23 22:48:16 move up
sandersd (OOO until July 31) 2014/09/24 22:22:34 Alright, now I'm sad that the compiler didn't warn
ddorwin 2014/09/27 00:41:24 The danger of using masks!
const std::string& parent_key_system) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!IsConcreteSupportedKeySystem(concrete_key_system))
@@ -291,6 +353,7 @@ void KeySystems::AddConcreteSupportedKeySystem(
#endif
properties.supported_codecs = supported_codecs;
+ properties.supported_init_data_types = supported_init_data_types;
ddorwin 2014/09/23 22:48:17 ditto
sandersd (OOO until July 31) 2014/09/24 22:22:34 Done.
concrete_key_system_map_[concrete_key_system] = properties;
@@ -306,8 +369,7 @@ void KeySystems::AddConcreteSupportedKeySystem(
bool KeySystems::IsConcreteSupportedKeySystem(const std::string& key_system) {
DCHECK(thread_checker_.CalledOnValidThread());
- return concrete_key_system_map_.find(key_system) !=
- concrete_key_system_map_.end();
+ return concrete_key_system_map_.count(key_system);
}
bool KeySystems::IsSupportedContainer(
@@ -324,16 +386,10 @@ bool KeySystems::IsSupportedContainer(
if (container.find("audio/") == 0)
ddorwin 2014/09/23 22:48:16 Note: If we also use enums for container types in
sandersd (OOO until July 31) 2014/09/24 22:22:33 Yes, we'll need both to be able to keep doing the
canonical_container.replace(0, 6, "video/");
- CodecMaskMap::const_iterator container_iter =
- container_codec_masks_.find(canonical_container);
- // Unrecognized container.
- if (container_iter == container_codec_masks_.end())
- return false;
-
- EmeCodec container_codec_mask = container_iter->second;
// A container is supported iif at least one codec in that container is
// supported.
- return (container_codec_mask & key_system_supported_codecs) != 0;
+ SupportedCodecs supported_codecs = LookupContainer(canonical_container);
+ return (supported_codecs & key_system_supported_codecs) != 0;
}
bool KeySystems::IsSupportedContainerAndCodecs(
@@ -345,53 +401,67 @@ bool KeySystems::IsSupportedContainerAndCodecs(
DCHECK(!codecs.empty());
DCHECK(IsSupportedContainer(container, key_system_supported_codecs));
- CodecMaskMap::const_iterator container_iter =
- container_codec_masks_.find(container);
- EmeCodec container_codec_mask = container_iter->second;
+ SupportedCodecs container_supported_codecs = LookupContainer(container);
for (size_t i = 0; i < codecs.size(); ++i) {
- const std::string& codec = codecs[i];
- if (codec.empty())
- continue;
- CodecMaskMap::const_iterator codec_iter = codec_masks_.find(codec);
- if (codec_iter == codec_masks_.end()) // Unrecognized codec.
- return false;
+ EmeCodec codec = LookupCodec(codecs[i]);
ddorwin 2014/09/23 22:48:16 The "codec.empty()" logic was for a specific reaso
xhwang 2014/09/29 21:27:17 Yeah, see the CL description of https://codereview
sandersd (OOO until July 31) 2014/09/29 21:38:48 Acknowledged. (There is a bug reference comment no
- EmeCodec codec_mask = codec_iter->second;
- if (!(codec_mask & key_system_supported_codecs)) // Unsupported codec.
+ // Unsupported codec.
+ if (!(codec & key_system_supported_codecs))
return false;
// Unsupported codec/container combination, e.g. "video/webm" and "avc1".
- if (!(codec_mask & container_codec_mask))
+ if (!(codec & container_supported_codecs))
return false;
}
return true;
}
+bool KeySystems::IsSupportedKeySystem(const std::string& key_system) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return (concrete_key_system_map_.count(key_system) ||
+ parent_key_system_map_.count(key_system));
+}
+
+bool KeySystems::IsSupportedKeySystemWithInitDataType(
+ const std::string& key_system,
+ const std::string& init_data_type) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ // If |key_system| is a parent key system, use its concrete child.
+ const std::string& concrete_key_system = GetConcreteKeySystem(key_system);
+
+ // Locate |concrete_key_system|.
+ base::hash_map<std::string, KeySystemProperties>::const_iterator
+ key_system_iter = concrete_key_system_map_.find(concrete_key_system);
+ if (key_system_iter == concrete_key_system_map_.end())
+ return false;
+
+ // Check |init_data_type| and |key_system| x |init_data_type|.
+ const KeySystemProperties& properties = key_system_iter->second;
+ EmeInitDataType eme_init_data_type = LookupInitDataType(init_data_type);
+ return properties.supported_init_data_types & eme_init_data_type;
+}
+
+// TODO(sandersd): Remove if possible, otherwise reorganize in the same order as
ddorwin 2014/09/23 22:48:16 See comments below. canPlayType() calls this direc
sandersd (OOO until July 31) 2014/09/24 22:22:33 Done.
+// IsKeySystemSupportedWithInitDataType().
bool KeySystems::IsSupportedKeySystemWithMediaMimeType(
const std::string& mime_type,
const std::vector<std::string>& codecs,
const std::string& key_system) {
DCHECK(thread_checker_.CalledOnValidThread());
- // If |key_system| is a parent key_system, use its concrete child.
- // Otherwise, use |key_system|.
- std::string concrete_key_system;
- ParentKeySystemMap::iterator parent_key_system_iter =
- parent_key_system_map_.find(key_system);
- if (parent_key_system_iter != parent_key_system_map_.end())
- concrete_key_system = parent_key_system_iter->second;
- else
- concrete_key_system = key_system;
+ // If |key_system| is a parent key system, use its concrete child.
+ const std::string& concrete_key_system = GetConcreteKeySystem(key_system);
bool has_type = !mime_type.empty();
key_systems_support_uma_.ReportKeySystemQuery(key_system, has_type);
ddorwin 2014/09/23 22:48:17 We now have some paths, including the function abo
xhwang 2014/09/29 21:27:17 I checked the current UMA stats (under Counts) and
sandersd (OOO until July 31) 2014/09/29 21:38:48 Acknowledged.
// Check key system support.
- KeySystemPropertiesMap::const_iterator key_system_iter =
- concrete_key_system_map_.find(concrete_key_system);
+ base::hash_map<std::string, KeySystemProperties>::const_iterator
+ key_system_iter = concrete_key_system_map_.find(concrete_key_system);
if (key_system_iter == concrete_key_system_map_.end())
return false;
@@ -421,8 +491,8 @@ bool KeySystems::IsSupportedKeySystemWithMediaMimeType(
bool KeySystems::UseAesDecryptor(const std::string& concrete_key_system) {
DCHECK(thread_checker_.CalledOnValidThread());
- KeySystemPropertiesMap::iterator key_system_iter =
- concrete_key_system_map_.find(concrete_key_system);
+ base::hash_map<std::string, KeySystemProperties>::const_iterator
+ key_system_iter = concrete_key_system_map_.find(concrete_key_system);
if (key_system_iter == concrete_key_system_map_.end()) {
DLOG(FATAL) << concrete_key_system << " is not a known concrete system";
return false;
@@ -435,8 +505,8 @@ bool KeySystems::UseAesDecryptor(const std::string& concrete_key_system) {
std::string KeySystems::GetPepperType(const std::string& concrete_key_system) {
DCHECK(thread_checker_.CalledOnValidThread());
- KeySystemPropertiesMap::iterator key_system_iter =
- concrete_key_system_map_.find(concrete_key_system);
+ base::hash_map<std::string, KeySystemProperties>::const_iterator
+ key_system_iter = concrete_key_system_map_.find(concrete_key_system);
if (key_system_iter == concrete_key_system_map_.end()) {
DLOG(FATAL) << concrete_key_system << " is not a known concrete system";
return std::string();
@@ -450,17 +520,16 @@ std::string KeySystems::GetPepperType(const std::string& concrete_key_system) {
void KeySystems::AddContainerMask(const std::string& container, uint32 mask) {
DCHECK(thread_checker_.CalledOnValidThread());
- DCHECK(container_codec_masks_.find(container) ==
- container_codec_masks_.end());
+ DCHECK(container_codecs_.find(container) == container_codecs_.end());
- container_codec_masks_[container] = static_cast<EmeCodec>(mask);
+ container_codecs_[container] = static_cast<EmeCodec>(mask);
}
void KeySystems::AddCodecMask(const std::string& codec, uint32 mask) {
DCHECK(thread_checker_.CalledOnValidThread());
- DCHECK(codec_masks_.find(codec) == codec_masks_.end());
+ DCHECK(codecs_.find(codec) == codecs_.end());
- codec_masks_[codec] = static_cast<EmeCodec>(mask);
+ codecs_[codec] = static_cast<EmeCodec>(mask);
}
//------------------------------------------------------------------------------
@@ -484,10 +553,33 @@ std::string GetPrefixedKeySystemName(const std::string& key_system) {
return key_system;
}
+bool IsSaneInitDataTypeWithContainer(
+ const std::string& init_data_type,
+ const std::string& container) {
+ if (init_data_type == "cenc") {
+ return container != "audio/webm" && container != "video/webm";
ddorwin 2014/09/23 22:48:17 Why not positive checks? This is actually wrong if
sandersd (OOO until July 31) 2014/09/24 22:22:34 I was assuming the goal was to exclude bad combina
+ } else if (init_data_type == "webm") {
+ return container != "audio/mp4" && container != "video/mp4";
+ } else {
+ return true;
ddorwin 2014/09/23 22:48:17 Related to enums, it would be nice to be able to h
sandersd (OOO until July 31) 2014/09/24 22:22:33 Sounds reasonable, I've just added the NOTREACHED
+ }
+}
+
bool IsConcreteSupportedKeySystem(const std::string& key_system) {
return KeySystems::GetInstance().IsConcreteSupportedKeySystem(key_system);
}
+bool IsSupportedKeySystem(const std::string& key_system) {
ddorwin 2014/09/23 22:48:16 Related to UMAs, if someone refactored the code su
sandersd (OOO until July 31) 2014/09/24 22:22:34 I added a comment to the implementation.
+ return KeySystems::GetInstance().IsSupportedKeySystem(key_system);
+}
+
+bool IsSupportedKeySystemWithInitDataType(
+ const std::string& key_system,
+ const std::string& init_data_type) {
+ return KeySystems::GetInstance().IsSupportedKeySystemWithInitDataType(
+ key_system, init_data_type);
+}
+
bool IsSupportedKeySystemWithMediaMimeType(
const std::string& mime_type,
const std::vector<std::string>& codecs,

Powered by Google App Engine
This is Rietveld 408576698