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.
4
5 #include "content/renderer/media/crypto/key_systems.h"
6
7 #include <string>
8
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"
20
21 #if defined(OS_ANDROID)
22 #include "media/base/android/media_codec_bridge.h"
23 #endif
24
25 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
26
27 namespace content {
28
29 const char kClearKeyKeySystem[] = "org.w3.clearkey";
30 const char kPrefixedClearKeyKeySystem[] = "webkit-org.w3.clearkey";
31 const char kUnsupportedClearKeyKeySystem[] = "unsupported-org.w3.clearkey";
32
33 struct CodecMask {
34 const char* type;
35 EmeCodec mask;
36 };
37
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)
48 };
49
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)
62 };
63
AddClearKey(std::vector<KeySystemInfo> * concrete_key_systems)64 static void AddClearKey(std::vector<KeySystemInfo>* concrete_key_systems) {
65 KeySystemInfo info(kClearKeyKeySystem);
66
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.
70
71 info.supported_codecs = EME_CODEC_WEBM_ALL;
72
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)
78
79 #if defined(USE_PROPRIETARY_CODECS)
80 info.supported_codecs |= EME_CODEC_MP4_ALL;
81 #endif // defined(USE_PROPRIETARY_CODECS)
82
83 info.use_aes_decryptor = true;
84
85 concrete_key_systems->push_back(info);
86 }
87
88 class KeySystems {
89 public:
90 static KeySystems& GetInstance();
91
92 void UpdateIfNeeded();
93
94 bool IsConcreteSupportedKeySystem(const std::string& key_system);
95
96 bool IsSupportedKeySystemWithMediaMimeType(
97 const std::string& mime_type,
98 const std::vector<std::string>& codecs,
99 const std::string& key_system);
100
101 bool UseAesDecryptor(const std::string& concrete_key_system);
102
103 #if defined(ENABLE_PEPPER_CDMS)
104 std::string GetPepperType(const std::string& concrete_key_system);
105 #endif
106
107 void AddContainerMask(const std::string& container, uint32 mask);
108 void AddCodecMask(const std::string& codec, uint32 mask);
109
110 private:
111 void UpdateSupportedKeySystems();
112
113 void AddConcreteSupportedKeySystems(
114 const std::vector<KeySystemInfo>& concrete_key_systems);
115
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,
121 #endif
122 SupportedCodecs supported_codecs,
123 const std::string& parent_key_system);
124
125 friend struct base::DefaultLazyInstanceTraits<KeySystems>;
126
127 struct KeySystemProperties {
KeySystemPropertiescontent::KeySystems::KeySystemProperties128 KeySystemProperties() : use_aes_decryptor(false) {}
129
130 bool use_aes_decryptor;
131 #if defined(ENABLE_PEPPER_CDMS)
132 std::string pepper_type;
133 #endif
134 SupportedCodecs supported_codecs;
135 };
136
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;
141
142 KeySystems();
~KeySystems()143 ~KeySystems() {}
144
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;
150
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;
157
158 // Map from key system string to capabilities.
159 KeySystemPropertiesMap concrete_key_system_map_;
160
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_;
164
165 KeySystemsSupportUMA key_systems_support_uma_;
166
167 CodecMaskMap container_codec_masks_;
168 CodecMaskMap codec_masks_;
169
170 bool needs_update_;
171 base::Time last_update_time_;
172
173 // Makes sure all methods are called from the same thread.
174 base::ThreadChecker thread_checker_;
175
176 DISALLOW_COPY_AND_ASSIGN(KeySystems);
177 };
178
179 static base::LazyInstance<KeySystems> g_key_systems = LAZY_INSTANCE_INITIALIZER;
180
GetInstance()181 KeySystems& KeySystems::GetInstance() {
182 KeySystems& key_systems = g_key_systems.Get();
183 key_systems.UpdateIfNeeded();
184 return key_systems;
185 }
186
187 // Because we use a LazyInstance, the key systems info must be populated when
188 // the instance is lazily initiated.
KeySystems()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;
197 }
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;
202 }
203
204 UpdateSupportedKeySystems();
205
206 #if defined(WIDEVINE_CDM_AVAILABLE)
207 key_systems_support_uma_.AddKeySystemToReport(kWidevineKeySystem);
208 #endif // defined(WIDEVINE_CDM_AVAILABLE)
209 }
210
UpdateIfNeeded()211 void KeySystems::UpdateIfNeeded() {
212 #if defined(WIDEVINE_CDM_AVAILABLE)
213 DCHECK(thread_checker_.CalledOnValidThread());
214 if (!needs_update_)
215 return;
216
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)) {
223 return;
224 }
225
226 UpdateSupportedKeySystems();
227 #endif
228 }
229
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();
235
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);
241
242 AddConcreteSupportedKeySystems(key_systems_info);
243
244 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
245 if (IsConcreteSupportedKeySystem(kWidevineKeySystem))
246 needs_update_ = false;
247 #endif
248
249 last_update_time_ = base::Time::Now();
250 }
251
AddConcreteSupportedKeySystems(const std::vector<KeySystemInfo> & concrete_key_systems)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());
257
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,
264 #endif
265 key_system_info.supported_codecs,
266 key_system_info.parent_key_system);
267 }
268 }
269
AddConcreteSupportedKeySystem(const std::string & concrete_key_system,bool use_aes_decryptor,const std::string & pepper_type,SupportedCodecs supported_codecs,const std::string & 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,
275 #endif
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";
284
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;
290 #endif
291
292 properties.supported_codecs = supported_codecs;
293
294 concrete_key_system_map_[concrete_key_system] = properties;
295
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;
303 }
304 }
305
IsConcreteSupportedKeySystem(const std::string & 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();
310 }
311
IsSupportedContainer(const std::string & container,SupportedCodecs key_system_supported_codecs) const312 bool KeySystems::IsSupportedContainer(
313 const std::string& container,
314 SupportedCodecs key_system_supported_codecs) const {
315 DCHECK(thread_checker_.CalledOnValidThread());
316 DCHECK(!container.empty());
317
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/");
325
326 CodecMaskMap::const_iterator container_iter =
327 container_codec_masks_.find(canonical_container);
328 // Unrecognized container.
329 if (container_iter == container_codec_masks_.end())
330 return false;
331
332 EmeCodec container_codec_mask = container_iter->second;
333 // A container is supported iif at least one codec in that container is
334 // supported.
335 return (container_codec_mask & key_system_supported_codecs) != 0;
336 }
337
IsSupportedContainerAndCodecs(const std::string & container,const std::vector<std::string> & codecs,SupportedCodecs key_system_supported_codecs) const338 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));
346
347 CodecMaskMap::const_iterator container_iter =
348 container_codec_masks_.find(container);
349 EmeCodec container_codec_mask = container_iter->second;
350
351 for (size_t i = 0; i < codecs.size(); ++i) {
352 const std::string& codec = codecs[i];
353 if (codec.empty())
354 continue;
355 CodecMaskMap::const_iterator codec_iter = codec_masks_.find(codec);
356 if (codec_iter == codec_masks_.end()) // Unrecognized codec.
357 return false;
358
359 EmeCodec codec_mask = codec_iter->second;
360 if (!(codec_mask & key_system_supported_codecs)) // Unsupported codec.
361 return false;
362
363 // Unsupported codec/container combination, e.g. "video/webm" and "avc1".
364 if (!(codec_mask & container_codec_mask))
365 return false;
366 }
367
368 return true;
369 }
370
IsSupportedKeySystemWithMediaMimeType(const std::string & mime_type,const std::vector<std::string> & codecs,const std::string & key_system)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());
376
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;
384 else
385 concrete_key_system = key_system;
386
387 bool has_type = !mime_type.empty();
388
389 key_systems_support_uma_.ReportKeySystemQuery(key_system, has_type);
390
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())
395 return false;
396
397 key_systems_support_uma_.ReportKeySystemSupport(key_system, false);
398
399 if (!has_type) {
400 DCHECK(codecs.empty());
401 return true;
402 }
403
404 SupportedCodecs key_system_supported_codecs =
405 key_system_iter->second.supported_codecs;
406
407 if (!IsSupportedContainer(mime_type, key_system_supported_codecs))
408 return false;
409
410 if (!codecs.empty() &&
411 !IsSupportedContainerAndCodecs(
412 mime_type, codecs, key_system_supported_codecs)) {
413 return false;
414 }
415
416 key_systems_support_uma_.ReportKeySystemSupport(key_system, true);
417 return true;
418 }
419
UseAesDecryptor(const std::string & concrete_key_system)420 bool KeySystems::UseAesDecryptor(const std::string& concrete_key_system) {
421 DCHECK(thread_checker_.CalledOnValidThread());
422
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";
427 return false;
428 }
429
430 return key_system_iter->second.use_aes_decryptor;
431 }
432
433 #if defined(ENABLE_PEPPER_CDMS)
GetPepperType(const std::string & concrete_key_system)434 std::string KeySystems::GetPepperType(const std::string& concrete_key_system) {
435 DCHECK(thread_checker_.CalledOnValidThread());
436
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();
442 }
443
444 const std::string& type = key_system_iter->second.pepper_type;
445 DLOG_IF(FATAL, type.empty()) << concrete_key_system << " is not Pepper-based";
446 return type;
447 }
448 #endif
449
AddContainerMask(const std::string & container,uint32 mask)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());
454
455 container_codec_masks_[container] = static_cast<EmeCodec>(mask);
456 }
457
AddCodecMask(const std::string & codec,uint32 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());
461
462 codec_masks_[codec] = static_cast<EmeCodec>(mask);
463 }
464
465 //------------------------------------------------------------------------------
466
GetUnprefixedKeySystemName(const std::string & key_system)467 std::string GetUnprefixedKeySystemName(const std::string& key_system) {
468 if (key_system == kClearKeyKeySystem)
469 return kUnsupportedClearKeyKeySystem;
470
471 if (key_system == kPrefixedClearKeyKeySystem)
472 return kClearKeyKeySystem;
473
474 return key_system;
475 }
476
GetPrefixedKeySystemName(const std::string & key_system)477 std::string GetPrefixedKeySystemName(const std::string& key_system) {
478 DCHECK_NE(key_system, kPrefixedClearKeyKeySystem);
479
480 if (key_system == kClearKeyKeySystem)
481 return kPrefixedClearKeyKeySystem;
482
483 return key_system;
484 }
485
IsConcreteSupportedKeySystem(const std::string & key_system)486 bool IsConcreteSupportedKeySystem(const std::string& key_system) {
487 return KeySystems::GetInstance().IsConcreteSupportedKeySystem(key_system);
488 }
489
IsSupportedKeySystemWithMediaMimeType(const std::string & mime_type,const std::vector<std::string> & codecs,const std::string & 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);
496 }
497
KeySystemNameForUMA(const std::string & key_system)498 std::string KeySystemNameForUMA(const std::string& key_system) {
499 if (key_system == kClearKeyKeySystem)
500 return "ClearKey";
501 #if defined(WIDEVINE_CDM_AVAILABLE)
502 if (key_system == kWidevineKeySystem)
503 return "Widevine";
504 #endif // WIDEVINE_CDM_AVAILABLE
505 return "Unknown";
506 }
507
CanUseAesDecryptor(const std::string & concrete_key_system)508 bool CanUseAesDecryptor(const std::string& concrete_key_system) {
509 return KeySystems::GetInstance().UseAesDecryptor(concrete_key_system);
510 }
511
512 #if defined(ENABLE_PEPPER_CDMS)
GetPepperType(const std::string & concrete_key_system)513 std::string GetPepperType(const std::string& concrete_key_system) {
514 return KeySystems::GetInstance().GetPepperType(concrete_key_system);
515 }
516 #endif
517
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.
523
AddContainerMask(const std::string & container,uint32 mask)524 CONTENT_EXPORT void AddContainerMask(const std::string& container,
525 uint32 mask) {
526 KeySystems::GetInstance().AddContainerMask(container, mask);
527 }
528
AddCodecMask(const std::string & codec,uint32 mask)529 CONTENT_EXPORT void AddCodecMask(const std::string& codec, uint32 mask) {
530 KeySystems::GetInstance().AddCodecMask(codec, mask);
531 }
532
533 } // namespace content
534