Chromium Code Reviews| 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 381616ffe7b8053488aca28a917ffbe1f6613f50..a042005d299ba44168d9ffa95bba06df367911c4 100644 |
| --- a/content/renderer/media/crypto/key_systems.cc |
| +++ b/content/renderer/media/crypto/key_systems.cc |
| @@ -4,13 +4,14 @@ |
| #include "content/renderer/media/crypto/key_systems.h" |
| -#include <map> |
| #include <string> |
| +#include "base/containers/hash_tables.h" |
| #include "base/lazy_instance.h" |
| #include "base/logging.h" |
| #include "base/strings/string_util.h" |
| #include "content/public/common/content_client.h" |
| +#include "content/public/common/encrypted_media_codecs.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" |
| @@ -27,19 +28,34 @@ const char kClearKeyKeySystem[] = "org.w3.clearkey"; |
| const char kPrefixedClearKeyKeySystem[] = "webkit-org.w3.clearkey"; |
| const char kUnsupportedClearKeyKeySystem[] = "unsupported-org.w3.clearkey"; |
| -const char kAudioWebM[] = "audio/webm"; |
| -const char kVideoWebM[] = "video/webm"; |
| -const char kVorbis[] = "vorbis"; |
| -const char kVP8[] = "vp8"; |
| -const char kVP80[] = "vp8.0"; |
| +struct CodecMaskMap { |
| + const char* type; |
| + SupportedCodecMask mask; |
| +}; |
| + |
| +// Mapping between container types and the masks of associated codecs. |
| +// Only audio codec can belong to a "audio/*" container. Both audio and video |
| +// codecs can belong to a "video/*" container. |
| +CodecMaskMap kContainerMasks[] = { |
| + {"audio/webm", WEBM_AUDIO_CODECS}, |
| + {"video/webm", WEBM_ALL_CODECS}, |
| +#if defined(USE_PROPRIETARY_CODECS) |
| + {"audio/mp4", MP4_AUDIO_CODECS}, |
| + {"video/mp4", MP4_ALL_CODECS} |
| +#endif // defined(USE_PROPRIETARY_CODECS) |
| +}; |
| +// Mapping between codec types and their masks. |
| +CodecMaskMap kCodecMasks[] = { |
| + {"vorbis", WEBM_VORBIS}, |
| + {"vp8", WEBM_VP8}, |
| + {"vp8.0", WEBM_VP8}, |
| #if defined(USE_PROPRIETARY_CODECS) |
| -const char kAudioMp4[] = "audio/mp4"; |
| -const char kVideoMp4[] = "video/mp4"; |
| -const char kMp4a[] = "mp4a"; |
| -const char kAvc1[] = "avc1"; |
| -const char kAvc3[] = "avc3"; |
| + {"mp4a", MP4_AAC}, |
| + {"avc1", MP4_AVC1}, |
| + {"avc3", MP4_AVC1} |
| #endif // defined(USE_PROPRIETARY_CODECS) |
| +}; |
| static void AddClearKey(std::vector<KeySystemInfo>* concrete_key_systems) { |
| KeySystemInfo info(kClearKeyKeySystem); |
| @@ -52,15 +68,9 @@ static void AddClearKey(std::vector<KeySystemInfo>* concrete_key_systems) { |
| DCHECK(media::MediaCodecBridge::IsAvailable()); |
| #endif |
| - info.supported_types[kAudioWebM].insert(kVorbis); |
| - info.supported_types[kVideoWebM] = info.supported_types[kAudioWebM]; |
| - info.supported_types[kVideoWebM].insert(kVP8); |
| - info.supported_types[kVideoWebM].insert(kVP80); |
| + info.supported_codecs = WEBM_ALL_CODECS; |
| #if defined(USE_PROPRIETARY_CODECS) |
| - info.supported_types[kAudioMp4].insert(kMp4a); |
| - info.supported_types[kVideoMp4] = info.supported_types[kAudioMp4]; |
| - info.supported_types[kVideoMp4].insert(kAvc1); |
| - info.supported_types[kVideoMp4].insert(kAvc3); |
| + info.supported_codecs |= MP4_ALL_CODECS; |
| #endif // defined(USE_PROPRIETARY_CODECS) |
| info.use_aes_decryptor = true; |
| @@ -85,10 +95,10 @@ class KeySystems { |
| std::string GetPepperType(const std::string& concrete_key_system); |
| #endif |
| - private: |
| - typedef KeySystemInfo::CodecSet CodecSet; |
| - typedef KeySystemInfo::ContainerCodecsMap ContainerCodecsMap; |
| + void AddContainerMask(const std::string& container, uint32 mask); |
| + void AddCodecMask(const std::string& codec, uint32 mask); |
| + private: |
| void AddConcreteSupportedKeySystems( |
| const std::vector<KeySystemInfo>& concrete_key_systems); |
| @@ -98,7 +108,7 @@ class KeySystems { |
| #if defined(ENABLE_PEPPER_CDMS) |
| const std::string& pepper_type, |
| #endif |
| - const ContainerCodecsMap& supported_types, |
| + SupportedCodecs supported_codecs, |
| const std::string& parent_key_system); |
| friend struct base::DefaultLazyInstanceTraits<KeySystems>; |
| @@ -110,19 +120,28 @@ class KeySystems { |
| #if defined(ENABLE_PEPPER_CDMS) |
| std::string pepper_type; |
| #endif |
| - ContainerCodecsMap supported_types; |
| + SupportedCodecs supported_codecs; |
| }; |
| - typedef std::map<std::string, KeySystemProperties> KeySystemPropertiesMap; |
| - |
| - typedef std::map<std::string, std::string> ParentKeySystemMap; |
| + typedef base::hash_map<std::string, KeySystemProperties> |
| + KeySystemPropertiesMap; |
| + typedef base::hash_map<std::string, std::string> ParentKeySystemMap; |
| + typedef base::hash_map<std::string, SupportedCodecMask> MaskMap; |
| KeySystems(); |
| ~KeySystems() {} |
| - bool IsSupportedKeySystemWithContainerAndCodec(const std::string& mime_type, |
| - const std::string& codec, |
| - const std::string& key_system); |
| + // Returns whether a |container| type is supported by checking |
| + // |supported_codecs|. |
| + // TODO(xhwang): Update this to actually check initDataType support. |
| + bool IsSupportedContainer(const std::string& container, |
| + SupportedCodecs supported_codecs) const; |
| + |
| + // Returns true if all |codecs| are supported in |container| by checking |
| + // |supported_codecs|. |
| + bool IsSupportedContainerAndCodecs(const std::string& container, |
| + const std::vector<std::string>& codecs, |
| + SupportedCodecs supported_codecs) const; |
| // Map from key system string to capabilities. |
| KeySystemPropertiesMap concrete_key_system_map_; |
| @@ -133,6 +152,9 @@ class KeySystems { |
| KeySystemsSupportUMA key_systems_support_uma_; |
| + MaskMap container_masks_; |
| + MaskMap codec_masks_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(KeySystems); |
| }; |
| @@ -145,6 +167,18 @@ KeySystems& KeySystems::GetInstance() { |
| // Because we use a LazyInstance, the key systems info must be populated when |
| // the instance is lazily initiated. |
| KeySystems::KeySystems() { |
| + // Build container and codec masks for quick look up. |
| + for (size_t i = 0; i < arraysize(kContainerMasks); ++i) { |
| + const CodecMaskMap& codec_mask = kContainerMasks[i]; |
| + DCHECK(container_masks_.find(codec_mask.type) == container_masks_.end()); |
| + container_masks_[codec_mask.type] = codec_mask.mask; |
| + } |
| + for (size_t i = 0; i < arraysize(kCodecMasks); ++i) { |
| + const CodecMaskMap& codec_mask = kCodecMasks[i]; |
| + DCHECK(codec_masks_.find(codec_mask.type) == codec_masks_.end()); |
| + codec_masks_[codec_mask.type] = codec_mask.mask; |
| + } |
| + |
| std::vector<KeySystemInfo> key_systems_info; |
| GetContentClient()->renderer()->AddKeySystems(&key_systems_info); |
| // Clear Key is always supported. |
| @@ -164,7 +198,7 @@ void KeySystems::AddConcreteSupportedKeySystems( |
| #if defined(ENABLE_PEPPER_CDMS) |
| key_system_info.pepper_type, |
| #endif |
| - key_system_info.supported_types, |
| + key_system_info.supported_codecs, |
| key_system_info.parent_key_system); |
| } |
| } |
| @@ -175,7 +209,7 @@ void KeySystems::AddConcreteSupportedKeySystem( |
| #if defined(ENABLE_PEPPER_CDMS) |
| const std::string& pepper_type, |
| #endif |
| - const ContainerCodecsMap& supported_types, |
| + SupportedCodecs supported_codecs, |
| const std::string& parent_key_system) { |
| DCHECK(!IsConcreteSupportedKeySystem(concrete_key_system)) |
| << "Key system '" << concrete_key_system << "' already registered"; |
| @@ -190,7 +224,7 @@ void KeySystems::AddConcreteSupportedKeySystem( |
| properties.pepper_type = pepper_type; |
| #endif |
| - properties.supported_types = supported_types; |
| + properties.supported_codecs = supported_codecs; |
| concrete_key_system_map_[concrete_key_system] = properties; |
| @@ -209,41 +243,54 @@ bool KeySystems::IsConcreteSupportedKeySystem(const std::string& key_system) { |
| concrete_key_system_map_.end(); |
| } |
| -bool KeySystems::IsSupportedKeySystemWithContainerAndCodec( |
| - const std::string& mime_type, |
| - const std::string& codec, |
| - const std::string& key_system) { |
| - bool has_type = !mime_type.empty(); |
| - DCHECK(has_type || codec.empty()); |
| +bool KeySystems::IsSupportedContainer(const std::string& container, |
| + SupportedCodecs supported_codecs) const { |
| + DCHECK(!container.empty()); |
| - key_systems_support_uma_.ReportKeySystemQuery(key_system, has_type); |
| + // When checking container support for EME, "audio/foo" should be treated the |
| + // same as "video/foo". Convert the |container| to achieve this. |
|
ddorwin
2014/04/23 21:07:20
Should we have a TODO to replace this with real ch
xhwang
2014/04/23 23:30:37
Done.
|
| + std::string canonical_container = container; |
| + if (container.find("audio/") == 0) |
|
ddorwin
2014/04/23 21:07:20
This would break down for audio/mpeg, but we don't
xhwang
2014/04/23 23:30:37
yeah, this is a hack :)
|
| + canonical_container.replace(0, 6, "video/"); |
| - KeySystemPropertiesMap::const_iterator key_system_iter = |
| - concrete_key_system_map_.find(key_system); |
| - if (key_system_iter == concrete_key_system_map_.end()) |
| + MaskMap::const_iterator container_iter = |
| + container_masks_.find(canonical_container); |
| + if (container_iter == container_masks_.end()) // Unrecognized container. |
| return false; |
| - key_systems_support_uma_.ReportKeySystemSupport(key_system, false); |
| + SupportedCodecMask container_mask = container_iter->second; |
| + // A container is supported iif at least one codec in that container is |
| + // supported. |
| + return (container_mask & supported_codecs); |
| +} |
| - if (mime_type.empty()) |
| - return true; |
| +bool KeySystems::IsSupportedContainerAndCodecs( |
| + const std::string& container, |
| + const std::vector<std::string>& codecs, |
| + SupportedCodecs supported_codecs) const { |
|
ddorwin
2014/04/23 21:07:20
nit: This might be easier to follow if this was ke
xhwang
2014/04/23 23:30:37
Done.
|
| + DCHECK(!container.empty()); |
| + DCHECK(!codecs.empty()); |
| + DCHECK(IsSupportedContainer(container, supported_codecs)); |
| - const ContainerCodecsMap& mime_types_map = |
| - key_system_iter->second.supported_types; |
| - ContainerCodecsMap::const_iterator mime_iter = mime_types_map.find(mime_type); |
| - if (mime_iter == mime_types_map.end()) |
| - return false; |
| + MaskMap::const_iterator container_iter = container_masks_.find(container); |
| + SupportedCodecMask container_mask = container_iter->second; |
|
ddorwin
2014/04/23 21:07:20
container_CODEC_mask?
xhwang
2014/04/23 23:30:37
Done.
|
| - if (codec.empty()) { |
| - key_systems_support_uma_.ReportKeySystemSupport(key_system, true); |
| - return true; |
| - } |
| + for (size_t i = 0; i < codecs.size(); ++i) { |
| + const std::string& codec = codecs[i]; |
| + DCHECK(!codec.empty()); |
| + MaskMap::const_iterator codec_iter = codec_masks_.find(codec); |
| + if (codec_iter == codec_masks_.end()) // Unrecognized codec. |
| + return false; |
| - const CodecSet& codecs = mime_iter->second; |
| - if (codecs.find(codec) == codecs.end()) |
| - return false; |
| + SupportedCodecMask codec_mask = codec_iter->second; |
| + if (!(codec_mask & supported_codecs)) // Unsupported codec. |
| + return false; |
| + |
| + // Unsupported codec/container combination, e.g. "video/webm" and "avc1". |
| + if (!(codec_mask & container_mask)) |
| + return false; |
| + } |
| - key_systems_support_uma_.ReportKeySystemSupport(key_system, true); |
| return true; |
| } |
| @@ -261,18 +308,34 @@ bool KeySystems::IsSupportedKeySystemWithMediaMimeType( |
| else |
| concrete_key_system = key_system; |
| - if (codecs.empty()) { |
| - return IsSupportedKeySystemWithContainerAndCodec( |
| - mime_type, std::string(), concrete_key_system); |
| + bool has_type = !mime_type.empty(); |
| + |
| + key_systems_support_uma_.ReportKeySystemQuery(key_system, has_type); |
| + |
| + // Check key system support. |
| + KeySystemPropertiesMap::const_iterator key_system_iter = |
| + concrete_key_system_map_.find(concrete_key_system); |
| + if (key_system_iter == concrete_key_system_map_.end()) |
| + return false; |
| + |
| + key_systems_support_uma_.ReportKeySystemSupport(key_system, false); |
| + |
| + if (!has_type) { |
| + DCHECK(codecs.empty()); |
| + return true; |
| } |
| - for (size_t i = 0; i < codecs.size(); ++i) { |
| - if (!IsSupportedKeySystemWithContainerAndCodec( |
| - mime_type, codecs[i], concrete_key_system)) { |
| - return false; |
| - } |
| + SupportedCodecs supported_codecs = key_system_iter->second.supported_codecs; |
| + |
| + if (!IsSupportedContainer(mime_type, supported_codecs)) |
| + return false; |
| + |
| + if (!codecs.empty() && |
| + !IsSupportedContainerAndCodecs(mime_type, codecs, supported_codecs)) { |
| + return false; |
| } |
| + key_systems_support_uma_.ReportKeySystemSupport(key_system, true); |
| return true; |
| } |
| @@ -302,6 +365,16 @@ std::string KeySystems::GetPepperType(const std::string& concrete_key_system) { |
| } |
| #endif |
| +void KeySystems::AddContainerMask(const std::string& container, uint32 mask) { |
| + DCHECK(container_masks_.find(container) == container_masks_.end()); |
| + container_masks_[container] = static_cast<SupportedCodecMask>(mask); |
| +} |
| + |
| +void KeySystems::AddCodecMask(const std::string& codec, uint32 mask) { |
| + DCHECK(codec_masks_.find(codec) == codec_masks_.end()); |
| + codec_masks_[codec] = static_cast<SupportedCodecMask>(mask); |
| +} |
| + |
| //------------------------------------------------------------------------------ |
| std::string GetUnprefixedKeySystemName(const std::string& key_system) { |
| @@ -355,4 +428,19 @@ std::string GetPepperType(const std::string& concrete_key_system) { |
| } |
| #endif |
| +// These two functions are for testing purpose only. The declaration in the |
| +// header file is guarded by "#if defined(UNIT_TEST)" so that they can be used |
| +// by tests but not non-test code. However, this .cc file is compiled as part of |
| +// "content" where "UNIT_TEST" is not defined. So we need to specify |
| +// "CONTENT_EXPORT" here again so that they are visible to tests. |
| + |
| +CONTENT_EXPORT void AddContainerMask(const std::string& container, |
| + uint32 mask) { |
| + KeySystems::GetInstance().AddContainerMask(container, mask); |
| +} |
| + |
| +CONTENT_EXPORT void AddCodecMask(const std::string& codec, uint32 mask) { |
| + KeySystems::GetInstance().AddCodecMask(codec, mask); |
| +} |
| + |
| } // namespace content |