1 // Copyright 2013 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.
5 #include "content/renderer/media/crypto/key_systems.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/time/time.h"
15 #include "content/public/common/content_client.h"
16 #include "content/public/common/eme_codec.h"
17 #include "content/public/renderer/content_renderer_client.h"
18 #include "content/public/renderer/key_system_info.h"
19 #include "content/renderer/media/crypto/key_systems_support_uma.h"
21 #if defined(OS_ANDROID)
22 #include "media/base/android/media_codec_bridge.h"
25 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
29 const char kClearKeyKeySystem[] = "org.w3.clearkey";
30 const char kPrefixedClearKeyKeySystem[] = "webkit-org.w3.clearkey";
31 const char kUnsupportedClearKeyKeySystem[] = "unsupported-org.w3.clearkey";
38 // Mapping between container types and the masks of associated codecs.
39 // Only audio codec can belong to a "audio/*" container. Both audio and video
40 // codecs can belong to a "video/*" container.
41 CodecMask kContainerCodecMasks[] = {
42 {"audio/webm", EME_CODEC_WEBM_AUDIO_ALL},
43 {"video/webm", EME_CODEC_WEBM_ALL},
44 #if defined(USE_PROPRIETARY_CODECS)
45 {"audio/mp4", EME_CODEC_MP4_AUDIO_ALL},
46 {"video/mp4", EME_CODEC_MP4_ALL}
47 #endif // defined(USE_PROPRIETARY_CODECS)
50 // Mapping between codec types and their masks.
51 CodecMask kCodecMasks[] = {
52 {"vorbis", EME_CODEC_WEBM_VORBIS},
53 {"vp8", EME_CODEC_WEBM_VP8},
54 {"vp8.0", EME_CODEC_WEBM_VP8},
55 {"vp9", EME_CODEC_WEBM_VP9},
56 {"vp9.0", EME_CODEC_WEBM_VP9},
57 #if defined(USE_PROPRIETARY_CODECS)
58 {"mp4a", EME_CODEC_MP4_AAC},
59 {"avc1", EME_CODEC_MP4_AVC1},
60 {"avc3", EME_CODEC_MP4_AVC1}
61 #endif // defined(USE_PROPRIETARY_CODECS)
64 static void AddClearKey(std::vector<KeySystemInfo>* concrete_key_systems) {
65 KeySystemInfo info(kClearKeyKeySystem);
67 // On Android, Vorbis, VP8, AAC and AVC1 are supported in MediaCodec:
68 // http://developer.android.com/guide/appendix/media-formats.html
69 // VP9 support is device dependent.
71 info.supported_codecs = EME_CODEC_WEBM_ALL;
73 #if defined(OS_ANDROID)
74 // Temporarily disable VP9 support for Android.
75 // TODO(xhwang): Use mime_util.h to query VP9 support on Android.
76 info.supported_codecs &= ~EME_CODEC_WEBM_VP9;
77 #endif // defined(OS_ANDROID)
79 #if defined(USE_PROPRIETARY_CODECS)
80 info.supported_codecs |= EME_CODEC_MP4_ALL;
81 #endif // defined(USE_PROPRIETARY_CODECS)
83 info.use_aes_decryptor = true;
85 concrete_key_systems->push_back(info);
90 static KeySystems& GetInstance();
92 void UpdateIfNeeded();
94 bool IsConcreteSupportedKeySystem(const std::string& key_system);
96 bool IsSupportedKeySystemWithMediaMimeType(
97 const std::string& mime_type,
98 const std::vector<std::string>& codecs,
99 const std::string& key_system);
101 bool UseAesDecryptor(const std::string& concrete_key_system);
103 #if defined(ENABLE_PEPPER_CDMS)
104 std::string GetPepperType(const std::string& concrete_key_system);
107 void AddContainerMask(const std::string& container, uint32 mask);
108 void AddCodecMask(const std::string& codec, uint32 mask);
111 void UpdateSupportedKeySystems();
113 void AddConcreteSupportedKeySystems(
114 const std::vector<KeySystemInfo>& concrete_key_systems);
116 void AddConcreteSupportedKeySystem(
117 const std::string& key_system,
118 bool use_aes_decryptor,
119 #if defined(ENABLE_PEPPER_CDMS)
120 const std::string& pepper_type,
122 SupportedCodecs supported_codecs,
123 const std::string& parent_key_system);
125 friend struct base::DefaultLazyInstanceTraits<KeySystems>;
127 struct KeySystemProperties {
128 KeySystemProperties() : use_aes_decryptor(false) {}
130 bool use_aes_decryptor;
131 #if defined(ENABLE_PEPPER_CDMS)
132 std::string pepper_type;
134 SupportedCodecs supported_codecs;
137 typedef base::hash_map<std::string, KeySystemProperties>
138 KeySystemPropertiesMap;
139 typedef base::hash_map<std::string, std::string> ParentKeySystemMap;
140 typedef base::hash_map<std::string, EmeCodec> CodecMaskMap;
145 // Returns whether a |container| type is supported by checking
146 // |key_system_supported_codecs|.
147 // TODO(xhwang): Update this to actually check initDataType support.
148 bool IsSupportedContainer(const std::string& container,
149 SupportedCodecs key_system_supported_codecs) const;
151 // Returns true if all |codecs| are supported in |container| by checking
152 // |key_system_supported_codecs|.
153 bool IsSupportedContainerAndCodecs(
154 const std::string& container,
155 const std::vector<std::string>& codecs,
156 SupportedCodecs key_system_supported_codecs) const;
158 // Map from key system string to capabilities.
159 KeySystemPropertiesMap concrete_key_system_map_;
161 // Map from parent key system to the concrete key system that should be used
162 // to represent its capabilities.
163 ParentKeySystemMap parent_key_system_map_;
165 KeySystemsSupportUMA key_systems_support_uma_;
167 CodecMaskMap container_codec_masks_;
168 CodecMaskMap codec_masks_;
171 base::Time last_update_time_;
173 // Makes sure all methods are called from the same thread.
174 base::ThreadChecker thread_checker_;
176 DISALLOW_COPY_AND_ASSIGN(KeySystems);
179 static base::LazyInstance<KeySystems> g_key_systems = LAZY_INSTANCE_INITIALIZER;
181 KeySystems& KeySystems::GetInstance() {
182 KeySystems& key_systems = g_key_systems.Get();
183 key_systems.UpdateIfNeeded();
187 // Because we use a LazyInstance, the key systems info must be populated when
188 // the instance is lazily initiated.
189 KeySystems::KeySystems() : needs_update_(true) {
190 // Build container and codec masks for quick look up.
191 for (size_t i = 0; i < arraysize(kContainerCodecMasks); ++i) {
192 const CodecMask& container_codec_mask = kContainerCodecMasks[i];
193 DCHECK(container_codec_masks_.find(container_codec_mask.type) ==
194 container_codec_masks_.end());
195 container_codec_masks_[container_codec_mask.type] =
196 container_codec_mask.mask;
198 for (size_t i = 0; i < arraysize(kCodecMasks); ++i) {
199 const CodecMask& codec_mask = kCodecMasks[i];
200 DCHECK(codec_masks_.find(codec_mask.type) == codec_masks_.end());
201 codec_masks_[codec_mask.type] = codec_mask.mask;
204 UpdateSupportedKeySystems();
206 #if defined(WIDEVINE_CDM_AVAILABLE)
207 key_systems_support_uma_.AddKeySystemToReport(kWidevineKeySystem);
208 #endif // defined(WIDEVINE_CDM_AVAILABLE)
211 void KeySystems::UpdateIfNeeded() {
212 #if defined(WIDEVINE_CDM_AVAILABLE)
213 DCHECK(thread_checker_.CalledOnValidThread());
217 // The update could involve a sync IPC to the browser process. Use a minimum
218 // update interval to avoid unnecessary frequent IPC to the browser.
219 static const int kMinUpdateIntervalInSeconds = 1;
220 base::Time now = base::Time::Now();
221 if (now - last_update_time_ <
222 base::TimeDelta::FromSeconds(kMinUpdateIntervalInSeconds)) {
226 UpdateSupportedKeySystems();
230 void KeySystems::UpdateSupportedKeySystems() {
231 DCHECK(thread_checker_.CalledOnValidThread());
232 DCHECK(needs_update_);
233 concrete_key_system_map_.clear();
234 parent_key_system_map_.clear();
236 // Build KeySystemInfo.
237 std::vector<KeySystemInfo> key_systems_info;
238 GetContentClient()->renderer()->AddKeySystems(&key_systems_info);
239 // Clear Key is always supported.
240 AddClearKey(&key_systems_info);
242 AddConcreteSupportedKeySystems(key_systems_info);
244 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
245 if (IsConcreteSupportedKeySystem(kWidevineKeySystem))
246 needs_update_ = false;
249 last_update_time_ = base::Time::Now();
252 void KeySystems::AddConcreteSupportedKeySystems(
253 const std::vector<KeySystemInfo>& concrete_key_systems) {
254 DCHECK(thread_checker_.CalledOnValidThread());
255 DCHECK(concrete_key_system_map_.empty());
256 DCHECK(parent_key_system_map_.empty());
258 for (size_t i = 0; i < concrete_key_systems.size(); ++i) {
259 const KeySystemInfo& key_system_info = concrete_key_systems[i];
260 AddConcreteSupportedKeySystem(key_system_info.key_system,
261 key_system_info.use_aes_decryptor,
262 #if defined(ENABLE_PEPPER_CDMS)
263 key_system_info.pepper_type,
265 key_system_info.supported_codecs,
266 key_system_info.parent_key_system);
270 void KeySystems::AddConcreteSupportedKeySystem(
271 const std::string& concrete_key_system,
272 bool use_aes_decryptor,
273 #if defined(ENABLE_PEPPER_CDMS)
274 const std::string& pepper_type,
276 SupportedCodecs supported_codecs,
277 const std::string& parent_key_system) {
278 DCHECK(thread_checker_.CalledOnValidThread());
279 DCHECK(!IsConcreteSupportedKeySystem(concrete_key_system))
280 << "Key system '" << concrete_key_system << "' already registered";
281 DCHECK(parent_key_system_map_.find(concrete_key_system) ==
282 parent_key_system_map_.end())
283 << "'" << concrete_key_system << " is already registered as a parent";
285 KeySystemProperties properties;
286 properties.use_aes_decryptor = use_aes_decryptor;
287 #if defined(ENABLE_PEPPER_CDMS)
288 DCHECK_EQ(use_aes_decryptor, pepper_type.empty());
289 properties.pepper_type = pepper_type;
292 properties.supported_codecs = supported_codecs;
294 concrete_key_system_map_[concrete_key_system] = properties;
296 if (!parent_key_system.empty()) {
297 DCHECK(!IsConcreteSupportedKeySystem(parent_key_system))
298 << "Parent '" << parent_key_system << "' already registered concrete";
299 DCHECK(parent_key_system_map_.find(parent_key_system) ==
300 parent_key_system_map_.end())
301 << "Parent '" << parent_key_system << "' already registered";
302 parent_key_system_map_[parent_key_system] = concrete_key_system;
306 bool KeySystems::IsConcreteSupportedKeySystem(const std::string& key_system) {
307 DCHECK(thread_checker_.CalledOnValidThread());
308 return concrete_key_system_map_.find(key_system) !=
309 concrete_key_system_map_.end();
312 bool KeySystems::IsSupportedContainer(
313 const std::string& container,
314 SupportedCodecs key_system_supported_codecs) const {
315 DCHECK(thread_checker_.CalledOnValidThread());
316 DCHECK(!container.empty());
318 // When checking container support for EME, "audio/foo" should be treated the
319 // same as "video/foo". Convert the |container| to achieve this.
320 // TODO(xhwang): Replace this with real checks against supported initDataTypes
321 // combined with supported demuxers.
322 std::string canonical_container = container;
323 if (container.find("audio/") == 0)
324 canonical_container.replace(0, 6, "video/");
326 CodecMaskMap::const_iterator container_iter =
327 container_codec_masks_.find(canonical_container);
328 // Unrecognized container.
329 if (container_iter == container_codec_masks_.end())
332 EmeCodec container_codec_mask = container_iter->second;
333 // A container is supported iif at least one codec in that container is
335 return (container_codec_mask & key_system_supported_codecs) != 0;
338 bool KeySystems::IsSupportedContainerAndCodecs(
339 const std::string& container,
340 const std::vector<std::string>& codecs,
341 SupportedCodecs key_system_supported_codecs) const {
342 DCHECK(thread_checker_.CalledOnValidThread());
343 DCHECK(!container.empty());
344 DCHECK(!codecs.empty());
345 DCHECK(IsSupportedContainer(container, key_system_supported_codecs));
347 CodecMaskMap::const_iterator container_iter =
348 container_codec_masks_.find(container);
349 EmeCodec container_codec_mask = container_iter->second;
351 for (size_t i = 0; i < codecs.size(); ++i) {
352 const std::string& codec = codecs[i];
355 CodecMaskMap::const_iterator codec_iter = codec_masks_.find(codec);
356 if (codec_iter == codec_masks_.end()) // Unrecognized codec.
359 EmeCodec codec_mask = codec_iter->second;
360 if (!(codec_mask & key_system_supported_codecs)) // Unsupported codec.
363 // Unsupported codec/container combination, e.g. "video/webm" and "avc1".
364 if (!(codec_mask & container_codec_mask))
371 bool KeySystems::IsSupportedKeySystemWithMediaMimeType(
372 const std::string& mime_type,
373 const std::vector<std::string>& codecs,
374 const std::string& key_system) {
375 DCHECK(thread_checker_.CalledOnValidThread());
377 // If |key_system| is a parent key_system, use its concrete child.
378 // Otherwise, use |key_system|.
379 std::string concrete_key_system;
380 ParentKeySystemMap::iterator parent_key_system_iter =
381 parent_key_system_map_.find(key_system);
382 if (parent_key_system_iter != parent_key_system_map_.end())
383 concrete_key_system = parent_key_system_iter->second;
385 concrete_key_system = key_system;
387 bool has_type = !mime_type.empty();
389 key_systems_support_uma_.ReportKeySystemQuery(key_system, has_type);
391 // Check key system support.
392 KeySystemPropertiesMap::const_iterator key_system_iter =
393 concrete_key_system_map_.find(concrete_key_system);
394 if (key_system_iter == concrete_key_system_map_.end())
397 key_systems_support_uma_.ReportKeySystemSupport(key_system, false);
400 DCHECK(codecs.empty());
404 SupportedCodecs key_system_supported_codecs =
405 key_system_iter->second.supported_codecs;
407 if (!IsSupportedContainer(mime_type, key_system_supported_codecs))
410 if (!codecs.empty() &&
411 !IsSupportedContainerAndCodecs(
412 mime_type, codecs, key_system_supported_codecs)) {
416 key_systems_support_uma_.ReportKeySystemSupport(key_system, true);
420 bool KeySystems::UseAesDecryptor(const std::string& concrete_key_system) {
421 DCHECK(thread_checker_.CalledOnValidThread());
423 KeySystemPropertiesMap::iterator key_system_iter =
424 concrete_key_system_map_.find(concrete_key_system);
425 if (key_system_iter == concrete_key_system_map_.end()) {
426 DLOG(FATAL) << concrete_key_system << " is not a known concrete system";
430 return key_system_iter->second.use_aes_decryptor;
433 #if defined(ENABLE_PEPPER_CDMS)
434 std::string KeySystems::GetPepperType(const std::string& concrete_key_system) {
435 DCHECK(thread_checker_.CalledOnValidThread());
437 KeySystemPropertiesMap::iterator key_system_iter =
438 concrete_key_system_map_.find(concrete_key_system);
439 if (key_system_iter == concrete_key_system_map_.end()) {
440 DLOG(FATAL) << concrete_key_system << " is not a known concrete system";
441 return std::string();
444 const std::string& type = key_system_iter->second.pepper_type;
445 DLOG_IF(FATAL, type.empty()) << concrete_key_system << " is not Pepper-based";
450 void KeySystems::AddContainerMask(const std::string& container, uint32 mask) {
451 DCHECK(thread_checker_.CalledOnValidThread());
452 DCHECK(container_codec_masks_.find(container) ==
453 container_codec_masks_.end());
455 container_codec_masks_[container] = static_cast<EmeCodec>(mask);
458 void KeySystems::AddCodecMask(const std::string& codec, uint32 mask) {
459 DCHECK(thread_checker_.CalledOnValidThread());
460 DCHECK(codec_masks_.find(codec) == codec_masks_.end());
462 codec_masks_[codec] = static_cast<EmeCodec>(mask);
465 //------------------------------------------------------------------------------
467 std::string GetUnprefixedKeySystemName(const std::string& key_system) {
468 if (key_system == kClearKeyKeySystem)
469 return kUnsupportedClearKeyKeySystem;
471 if (key_system == kPrefixedClearKeyKeySystem)
472 return kClearKeyKeySystem;
477 std::string GetPrefixedKeySystemName(const std::string& key_system) {
478 DCHECK_NE(key_system, kPrefixedClearKeyKeySystem);
480 if (key_system == kClearKeyKeySystem)
481 return kPrefixedClearKeyKeySystem;
486 bool IsConcreteSupportedKeySystem(const std::string& key_system) {
487 return KeySystems::GetInstance().IsConcreteSupportedKeySystem(key_system);
490 bool IsSupportedKeySystemWithMediaMimeType(
491 const std::string& mime_type,
492 const std::vector<std::string>& codecs,
493 const std::string& key_system) {
494 return KeySystems::GetInstance().IsSupportedKeySystemWithMediaMimeType(
495 mime_type, codecs, key_system);
498 std::string KeySystemNameForUMA(const std::string& key_system) {
499 if (key_system == kClearKeyKeySystem)
501 #if defined(WIDEVINE_CDM_AVAILABLE)
502 if (key_system == kWidevineKeySystem)
504 #endif // WIDEVINE_CDM_AVAILABLE
508 bool CanUseAesDecryptor(const std::string& concrete_key_system) {
509 return KeySystems::GetInstance().UseAesDecryptor(concrete_key_system);
512 #if defined(ENABLE_PEPPER_CDMS)
513 std::string GetPepperType(const std::string& concrete_key_system) {
514 return KeySystems::GetInstance().GetPepperType(concrete_key_system);
518 // These two functions are for testing purpose only. The declaration in the
519 // header file is guarded by "#if defined(UNIT_TEST)" so that they can be used
520 // by tests but not non-test code. However, this .cc file is compiled as part of
521 // "content" where "UNIT_TEST" is not defined. So we need to specify
522 // "CONTENT_EXPORT" here again so that they are visible to tests.
524 CONTENT_EXPORT void AddContainerMask(const std::string& container,
526 KeySystems::GetInstance().AddContainerMask(container, mask);
529 CONTENT_EXPORT void AddCodecMask(const std::string& codec, uint32 mask) {
530 KeySystems::GetInstance().AddCodecMask(codec, mask);
533 } // namespace content