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

Side by Side 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: Work around performance warning. Created 6 years, 2 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "content/renderer/media/crypto/key_systems.h" 5 #include "content/renderer/media/crypto/key_systems.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/containers/hash_tables.h" 9 #include "base/containers/hash_tables.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/threading/thread_checker.h" 13 #include "base/threading/thread_checker.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "content/public/common/content_client.h" 15 #include "content/public/common/content_client.h"
16 #include "content/public/common/eme_codec.h" 16 #include "content/public/common/eme_constants.h"
17 #include "content/public/renderer/content_renderer_client.h" 17 #include "content/public/renderer/content_renderer_client.h"
18 #include "content/public/renderer/key_system_info.h" 18 #include "content/public/renderer/key_system_info.h"
19 #include "content/renderer/media/crypto/key_systems_support_uma.h" 19 #include "content/renderer/media/crypto/key_systems_support_uma.h"
20 20
21 #if defined(OS_ANDROID) 21 #if defined(OS_ANDROID)
22 #include "media/base/android/media_codec_bridge.h" 22 #include "media/base/android/media_codec_bridge.h"
23 #endif 23 #endif
24 24
25 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. 25 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
26 26
27 namespace content { 27 namespace content {
28 28
29 const char kClearKeyKeySystem[] = "org.w3.clearkey"; 29 const char kClearKeyKeySystem[] = "org.w3.clearkey";
30 const char kPrefixedClearKeyKeySystem[] = "webkit-org.w3.clearkey"; 30 const char kPrefixedClearKeyKeySystem[] = "webkit-org.w3.clearkey";
31 const char kUnsupportedClearKeyKeySystem[] = "unsupported-org.w3.clearkey"; 31 const char kUnsupportedClearKeyKeySystem[] = "unsupported-org.w3.clearkey";
32 32
33 struct CodecMask { 33 struct NamedInitDataType {
34 const char* type; 34 const char* name;
35 EmeCodec mask; 35 EmeInitDataType type;
36 }; 36 };
37 37
38 // Mapping between container types and the masks of associated codecs. 38 // Mapping between initialization data types names and enum values. When adding
39 // entries, make sure to update IsSaneInitDataTypeWithContainer().
40 static NamedInitDataType kInitDataTypeNames[] = {
41 {"webm", EME_INIT_DATA_TYPE_WEBM},
42 #if defined(USE_PROPRIETARY_CODECS)
43 {"cenc", EME_INIT_DATA_TYPE_CENC}
44 #endif // defined(USE_PROPRIETARY_CODECS)
45 };
46
47 struct NamedCodec {
48 const char* name;
49 EmeCodec type;
50 };
51
52 // Mapping between containers and their codecs.
39 // Only audio codec can belong to a "audio/*" container. Both audio and video 53 // Only audio codec can belong to a "audio/*" container. Both audio and video
40 // codecs can belong to a "video/*" container. 54 // codecs can belong to a "video/*" container.
41 CodecMask kContainerCodecMasks[] = { 55 static NamedCodec kContainerToCodecMasks[] = {
42 {"audio/webm", EME_CODEC_WEBM_AUDIO_ALL}, 56 {"audio/webm", EME_CODEC_WEBM_AUDIO_ALL},
43 {"video/webm", EME_CODEC_WEBM_ALL}, 57 {"video/webm", EME_CODEC_WEBM_ALL},
44 #if defined(USE_PROPRIETARY_CODECS) 58 #if defined(USE_PROPRIETARY_CODECS)
45 {"audio/mp4", EME_CODEC_MP4_AUDIO_ALL}, 59 {"audio/mp4", EME_CODEC_MP4_AUDIO_ALL},
46 {"video/mp4", EME_CODEC_MP4_ALL} 60 {"video/mp4", EME_CODEC_MP4_ALL}
47 #endif // defined(USE_PROPRIETARY_CODECS) 61 #endif // defined(USE_PROPRIETARY_CODECS)
48 }; 62 };
49 63
50 // Mapping between codec types and their masks. 64 // Mapping between codec names and enum values.
51 CodecMask kCodecMasks[] = { 65 static NamedCodec kCodecStrings[] = {
52 {"vorbis", EME_CODEC_WEBM_VORBIS}, 66 {"vorbis", EME_CODEC_WEBM_VORBIS},
53 {"vp8", EME_CODEC_WEBM_VP8}, 67 {"vp8", EME_CODEC_WEBM_VP8},
54 {"vp8.0", EME_CODEC_WEBM_VP8}, 68 {"vp8.0", EME_CODEC_WEBM_VP8},
55 {"vp9", EME_CODEC_WEBM_VP9}, 69 {"vp9", EME_CODEC_WEBM_VP9},
56 {"vp9.0", EME_CODEC_WEBM_VP9}, 70 {"vp9.0", EME_CODEC_WEBM_VP9},
57 #if defined(USE_PROPRIETARY_CODECS) 71 #if defined(USE_PROPRIETARY_CODECS)
58 {"mp4a", EME_CODEC_MP4_AAC}, 72 {"mp4a", EME_CODEC_MP4_AAC},
59 {"avc1", EME_CODEC_MP4_AVC1}, 73 {"avc1", EME_CODEC_MP4_AVC1},
60 {"avc3", EME_CODEC_MP4_AVC1} 74 {"avc3", EME_CODEC_MP4_AVC1}
61 #endif // defined(USE_PROPRIETARY_CODECS) 75 #endif // defined(USE_PROPRIETARY_CODECS)
62 }; 76 };
63 77
64 static void AddClearKey(std::vector<KeySystemInfo>* concrete_key_systems) { 78 static void AddClearKey(std::vector<KeySystemInfo>* concrete_key_systems) {
65 KeySystemInfo info(kClearKeyKeySystem); 79 KeySystemInfo info(kClearKeyKeySystem);
66 80
67 // On Android, Vorbis, VP8, AAC and AVC1 are supported in MediaCodec: 81 // On Android, Vorbis, VP8, AAC and AVC1 are supported in MediaCodec:
68 // http://developer.android.com/guide/appendix/media-formats.html 82 // http://developer.android.com/guide/appendix/media-formats.html
69 // VP9 support is device dependent. 83 // VP9 support is device dependent.
70 84
85 info.supported_init_data_types = EME_INIT_DATA_TYPE_WEBM;
71 info.supported_codecs = EME_CODEC_WEBM_ALL; 86 info.supported_codecs = EME_CODEC_WEBM_ALL;
72 87
73 #if defined(OS_ANDROID) 88 #if defined(OS_ANDROID)
74 // Temporarily disable VP9 support for Android. 89 // Temporarily disable VP9 support for Android.
75 // TODO(xhwang): Use mime_util.h to query VP9 support on Android. 90 // TODO(xhwang): Use mime_util.h to query VP9 support on Android.
76 info.supported_codecs &= ~EME_CODEC_WEBM_VP9; 91 info.supported_codecs &= ~EME_CODEC_WEBM_VP9;
77 #endif // defined(OS_ANDROID) 92 #endif // defined(OS_ANDROID)
78 93
79 #if defined(USE_PROPRIETARY_CODECS) 94 #if defined(USE_PROPRIETARY_CODECS)
95 info.supported_init_data_types |= EME_INIT_DATA_TYPE_CENC;
80 info.supported_codecs |= EME_CODEC_MP4_ALL; 96 info.supported_codecs |= EME_CODEC_MP4_ALL;
81 #endif // defined(USE_PROPRIETARY_CODECS) 97 #endif // defined(USE_PROPRIETARY_CODECS)
82 98
83 info.use_aes_decryptor = true; 99 info.use_aes_decryptor = true;
84 100
85 concrete_key_systems->push_back(info); 101 concrete_key_systems->push_back(info);
86 } 102 }
87 103
88 class KeySystems { 104 class KeySystems {
89 public: 105 public:
90 static KeySystems& GetInstance(); 106 static KeySystems& GetInstance();
91 107
92 void UpdateIfNeeded(); 108 void UpdateIfNeeded();
93 109
94 bool IsConcreteSupportedKeySystem(const std::string& key_system); 110 bool IsConcreteSupportedKeySystem(const std::string& key_system);
95 111
112 bool IsSupportedKeySystem(const std::string& key_system);
113
114 bool IsSupportedKeySystemWithInitDataType(
115 const std::string& key_system,
116 const std::string& init_data_type);
117
96 bool IsSupportedKeySystemWithMediaMimeType( 118 bool IsSupportedKeySystemWithMediaMimeType(
97 const std::string& mime_type, 119 const std::string& mime_type,
98 const std::vector<std::string>& codecs, 120 const std::vector<std::string>& codecs,
99 const std::string& key_system); 121 const std::string& key_system);
100 122
101 bool UseAesDecryptor(const std::string& concrete_key_system); 123 bool UseAesDecryptor(const std::string& concrete_key_system);
102 124
103 #if defined(ENABLE_PEPPER_CDMS) 125 #if defined(ENABLE_PEPPER_CDMS)
104 std::string GetPepperType(const std::string& concrete_key_system); 126 std::string GetPepperType(const std::string& concrete_key_system);
105 #endif 127 #endif
106 128
107 void AddContainerMask(const std::string& container, uint32 mask); 129 void AddContainerMask(const std::string& container, uint32 mask);
108 void AddCodecMask(const std::string& codec, uint32 mask); 130 void AddCodecMask(const std::string& codec, uint32 mask);
109 131
110 private: 132 private:
111 void UpdateSupportedKeySystems(); 133 void UpdateSupportedKeySystems();
112 134
113 void AddConcreteSupportedKeySystems( 135 void AddConcreteSupportedKeySystems(
114 const std::vector<KeySystemInfo>& concrete_key_systems); 136 const std::vector<KeySystemInfo>& concrete_key_systems);
115 137
116 void AddConcreteSupportedKeySystem( 138 void AddConcreteSupportedKeySystem(
117 const std::string& key_system, 139 const std::string& key_system,
118 bool use_aes_decryptor, 140 bool use_aes_decryptor,
119 #if defined(ENABLE_PEPPER_CDMS) 141 #if defined(ENABLE_PEPPER_CDMS)
120 const std::string& pepper_type, 142 const std::string& pepper_type,
121 #endif 143 #endif
144 SupportedInitDataTypes supported_init_data_types,
122 SupportedCodecs supported_codecs, 145 SupportedCodecs supported_codecs,
123 const std::string& parent_key_system); 146 const std::string& parent_key_system);
124 147
125 friend struct base::DefaultLazyInstanceTraits<KeySystems>; 148 friend struct base::DefaultLazyInstanceTraits<KeySystems>;
126 149
127 struct KeySystemProperties { 150 struct KeySystemProperties {
128 KeySystemProperties() 151 KeySystemProperties()
129 : use_aes_decryptor(false), supported_codecs(EME_CODEC_NONE) {} 152 : use_aes_decryptor(false), supported_codecs(EME_CODEC_NONE) {}
130 153
131 bool use_aes_decryptor; 154 bool use_aes_decryptor;
132 #if defined(ENABLE_PEPPER_CDMS) 155 #if defined(ENABLE_PEPPER_CDMS)
133 std::string pepper_type; 156 std::string pepper_type;
134 #endif 157 #endif
158 SupportedInitDataTypes supported_init_data_types;
135 SupportedCodecs supported_codecs; 159 SupportedCodecs supported_codecs;
136 }; 160 };
137 161
138 typedef base::hash_map<std::string, KeySystemProperties> 162 typedef base::hash_map<std::string, KeySystemProperties>
139 KeySystemPropertiesMap; 163 KeySystemPropertiesMap;
140 typedef base::hash_map<std::string, std::string> ParentKeySystemMap; 164 typedef base::hash_map<std::string, std::string> ParentKeySystemMap;
141 typedef base::hash_map<std::string, EmeCodec> CodecMaskMap; 165 typedef base::hash_map<std::string, SupportedCodecs> ContainerCodecsMap;
166 typedef base::hash_map<std::string, EmeCodec> CodecsMap;
167 typedef base::hash_map<std::string, EmeInitDataType> InitDataTypesMap;
142 168
143 KeySystems(); 169 KeySystems();
144 ~KeySystems() {} 170 ~KeySystems() {}
145 171
172 EmeInitDataType GetInitDataTypeForName(
173 const std::string& init_data_type) const;
174 // TODO(sandersd): Separate container enum from codec mask value.
175 // http://crbug.com/417440
176 SupportedCodecs GetCodecMaskForContainer(
177 const std::string& container) const;
178 EmeCodec GetCodecForString(const std::string& codec) const;
179
180 const std::string& GetConcreteKeySystemName(
181 const std::string& key_system) const;
182
146 // Returns whether a |container| type is supported by checking 183 // Returns whether a |container| type is supported by checking
147 // |key_system_supported_codecs|. 184 // |key_system_supported_codecs|.
148 // TODO(xhwang): Update this to actually check initDataType support. 185 // TODO(xhwang): Update this to actually check initDataType support.
149 bool IsSupportedContainer(const std::string& container, 186 bool IsSupportedContainer(const std::string& container,
150 SupportedCodecs key_system_supported_codecs) const; 187 SupportedCodecs key_system_supported_codecs) const;
151 188
152 // Returns true if all |codecs| are supported in |container| by checking 189 // Returns true if all |codecs| are supported in |container| by checking
153 // |key_system_supported_codecs|. 190 // |key_system_supported_codecs|.
154 bool IsSupportedContainerAndCodecs( 191 bool IsSupportedContainerAndCodecs(
155 const std::string& container, 192 const std::string& container,
156 const std::vector<std::string>& codecs, 193 const std::vector<std::string>& codecs,
157 SupportedCodecs key_system_supported_codecs) const; 194 SupportedCodecs key_system_supported_codecs) const;
158 195
159 // Map from key system string to capabilities. 196 // Map from key system string to capabilities.
160 KeySystemPropertiesMap concrete_key_system_map_; 197 KeySystemPropertiesMap concrete_key_system_map_;
161 198
162 // Map from parent key system to the concrete key system that should be used 199 // Map from parent key system to the concrete key system that should be used
163 // to represent its capabilities. 200 // to represent its capabilities.
164 ParentKeySystemMap parent_key_system_map_; 201 ParentKeySystemMap parent_key_system_map_;
165 202
166 KeySystemsSupportUMA key_systems_support_uma_; 203 KeySystemsSupportUMA key_systems_support_uma_;
167 204
168 CodecMaskMap container_codec_masks_; 205 InitDataTypesMap init_data_type_name_map_;
169 CodecMaskMap codec_masks_; 206 ContainerCodecsMap container_to_codec_mask_map_;
207 CodecsMap codec_string_map_;
170 208
171 bool needs_update_; 209 bool needs_update_;
172 base::Time last_update_time_; 210 base::Time last_update_time_;
173 211
174 // Makes sure all methods are called from the same thread. 212 // Makes sure all methods are called from the same thread.
175 base::ThreadChecker thread_checker_; 213 base::ThreadChecker thread_checker_;
176 214
177 DISALLOW_COPY_AND_ASSIGN(KeySystems); 215 DISALLOW_COPY_AND_ASSIGN(KeySystems);
178 }; 216 };
179 217
180 static base::LazyInstance<KeySystems> g_key_systems = LAZY_INSTANCE_INITIALIZER; 218 static base::LazyInstance<KeySystems> g_key_systems = LAZY_INSTANCE_INITIALIZER;
181 219
182 KeySystems& KeySystems::GetInstance() { 220 KeySystems& KeySystems::GetInstance() {
183 KeySystems& key_systems = g_key_systems.Get(); 221 KeySystems& key_systems = g_key_systems.Get();
184 key_systems.UpdateIfNeeded(); 222 key_systems.UpdateIfNeeded();
185 return key_systems; 223 return key_systems;
186 } 224 }
187 225
188 // Because we use a LazyInstance, the key systems info must be populated when 226 // Because we use a LazyInstance, the key systems info must be populated when
189 // the instance is lazily initiated. 227 // the instance is lazily initiated.
190 KeySystems::KeySystems() : needs_update_(true) { 228 KeySystems::KeySystems() : needs_update_(true) {
191 // Build container and codec masks for quick look up. 229 for (size_t i = 0; i < arraysize(kInitDataTypeNames); ++i) {
192 for (size_t i = 0; i < arraysize(kContainerCodecMasks); ++i) { 230 const std::string& name = kInitDataTypeNames[i].name;
193 const CodecMask& container_codec_mask = kContainerCodecMasks[i]; 231 DCHECK(!init_data_type_name_map_.count(name));
194 DCHECK(container_codec_masks_.find(container_codec_mask.type) == 232 init_data_type_name_map_[name] = kInitDataTypeNames[i].type;
195 container_codec_masks_.end());
196 container_codec_masks_[container_codec_mask.type] =
197 container_codec_mask.mask;
198 } 233 }
199 for (size_t i = 0; i < arraysize(kCodecMasks); ++i) { 234 for (size_t i = 0; i < arraysize(kContainerToCodecMasks); ++i) {
200 const CodecMask& codec_mask = kCodecMasks[i]; 235 const std::string& name = kContainerToCodecMasks[i].name;
201 DCHECK(codec_masks_.find(codec_mask.type) == codec_masks_.end()); 236 DCHECK(!container_to_codec_mask_map_.count(name));
202 codec_masks_[codec_mask.type] = codec_mask.mask; 237 container_to_codec_mask_map_[name] = kContainerToCodecMasks[i].type;
238 }
239 for (size_t i = 0; i < arraysize(kCodecStrings); ++i) {
240 const std::string& name = kCodecStrings[i].name;
241 DCHECK(!codec_string_map_.count(name));
242 codec_string_map_[name] = kCodecStrings[i].type;
203 } 243 }
204 244
205 UpdateSupportedKeySystems(); 245 UpdateSupportedKeySystems();
206 246
207 #if defined(WIDEVINE_CDM_AVAILABLE) 247 #if defined(WIDEVINE_CDM_AVAILABLE)
208 key_systems_support_uma_.AddKeySystemToReport(kWidevineKeySystem); 248 key_systems_support_uma_.AddKeySystemToReport(kWidevineKeySystem);
209 #endif // defined(WIDEVINE_CDM_AVAILABLE) 249 #endif // defined(WIDEVINE_CDM_AVAILABLE)
210 } 250 }
211 251
252 EmeInitDataType KeySystems::GetInitDataTypeForName(
253 const std::string& init_data_type) const {
254 InitDataTypesMap::const_iterator iter =
255 init_data_type_name_map_.find(init_data_type);
256 if (iter != init_data_type_name_map_.end())
257 return iter->second;
258 return EME_INIT_DATA_TYPE_NONE;
259 }
260
261 SupportedCodecs KeySystems::GetCodecMaskForContainer(
262 const std::string& container) const {
263 ContainerCodecsMap::const_iterator iter =
264 container_to_codec_mask_map_.find(container);
265 if (iter != container_to_codec_mask_map_.end())
266 return iter->second;
267 return EME_CODEC_NONE;
268 }
269
270 EmeCodec KeySystems::GetCodecForString(const std::string& codec) const {
271 CodecsMap::const_iterator iter = codec_string_map_.find(codec);
272 if (iter != codec_string_map_.end())
273 return iter->second;
274 return EME_CODEC_NONE;
275 }
276
277 const std::string& KeySystems::GetConcreteKeySystemName(
278 const std::string& key_system) const {
279 ParentKeySystemMap::const_iterator iter =
280 parent_key_system_map_.find(key_system);
281 if (iter != parent_key_system_map_.end())
282 return iter->second;
283 return key_system;
284 }
285
212 void KeySystems::UpdateIfNeeded() { 286 void KeySystems::UpdateIfNeeded() {
213 #if defined(WIDEVINE_CDM_AVAILABLE) 287 #if defined(WIDEVINE_CDM_AVAILABLE)
214 DCHECK(thread_checker_.CalledOnValidThread()); 288 DCHECK(thread_checker_.CalledOnValidThread());
215 if (!needs_update_) 289 if (!needs_update_)
216 return; 290 return;
217 291
218 // The update could involve a sync IPC to the browser process. Use a minimum 292 // The update could involve a sync IPC to the browser process. Use a minimum
219 // update interval to avoid unnecessary frequent IPC to the browser. 293 // update interval to avoid unnecessary frequent IPC to the browser.
220 static const int kMinUpdateIntervalInSeconds = 1; 294 static const int kMinUpdateIntervalInSeconds = 1;
221 base::Time now = base::Time::Now(); 295 base::Time now = base::Time::Now();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 DCHECK(concrete_key_system_map_.empty()); 330 DCHECK(concrete_key_system_map_.empty());
257 DCHECK(parent_key_system_map_.empty()); 331 DCHECK(parent_key_system_map_.empty());
258 332
259 for (size_t i = 0; i < concrete_key_systems.size(); ++i) { 333 for (size_t i = 0; i < concrete_key_systems.size(); ++i) {
260 const KeySystemInfo& key_system_info = concrete_key_systems[i]; 334 const KeySystemInfo& key_system_info = concrete_key_systems[i];
261 AddConcreteSupportedKeySystem(key_system_info.key_system, 335 AddConcreteSupportedKeySystem(key_system_info.key_system,
262 key_system_info.use_aes_decryptor, 336 key_system_info.use_aes_decryptor,
263 #if defined(ENABLE_PEPPER_CDMS) 337 #if defined(ENABLE_PEPPER_CDMS)
264 key_system_info.pepper_type, 338 key_system_info.pepper_type,
265 #endif 339 #endif
340 key_system_info.supported_init_data_types,
266 key_system_info.supported_codecs, 341 key_system_info.supported_codecs,
267 key_system_info.parent_key_system); 342 key_system_info.parent_key_system);
268 } 343 }
269 } 344 }
270 345
271 void KeySystems::AddConcreteSupportedKeySystem( 346 void KeySystems::AddConcreteSupportedKeySystem(
272 const std::string& concrete_key_system, 347 const std::string& concrete_key_system,
273 bool use_aes_decryptor, 348 bool use_aes_decryptor,
274 #if defined(ENABLE_PEPPER_CDMS) 349 #if defined(ENABLE_PEPPER_CDMS)
275 const std::string& pepper_type, 350 const std::string& pepper_type,
276 #endif 351 #endif
352 SupportedInitDataTypes supported_init_data_types,
277 SupportedCodecs supported_codecs, 353 SupportedCodecs supported_codecs,
278 const std::string& parent_key_system) { 354 const std::string& parent_key_system) {
279 DCHECK(thread_checker_.CalledOnValidThread()); 355 DCHECK(thread_checker_.CalledOnValidThread());
280 DCHECK(!IsConcreteSupportedKeySystem(concrete_key_system)) 356 DCHECK(!IsConcreteSupportedKeySystem(concrete_key_system))
281 << "Key system '" << concrete_key_system << "' already registered"; 357 << "Key system '" << concrete_key_system << "' already registered";
282 DCHECK(parent_key_system_map_.find(concrete_key_system) == 358 DCHECK(!parent_key_system_map_.count(concrete_key_system))
283 parent_key_system_map_.end())
284 << "'" << concrete_key_system << " is already registered as a parent"; 359 << "'" << concrete_key_system << " is already registered as a parent";
285 360
286 KeySystemProperties properties; 361 KeySystemProperties properties;
287 properties.use_aes_decryptor = use_aes_decryptor; 362 properties.use_aes_decryptor = use_aes_decryptor;
288 #if defined(ENABLE_PEPPER_CDMS) 363 #if defined(ENABLE_PEPPER_CDMS)
289 DCHECK_EQ(use_aes_decryptor, pepper_type.empty()); 364 DCHECK_EQ(use_aes_decryptor, pepper_type.empty());
290 properties.pepper_type = pepper_type; 365 properties.pepper_type = pepper_type;
291 #endif 366 #endif
292 367
368 properties.supported_init_data_types = supported_init_data_types;
293 properties.supported_codecs = supported_codecs; 369 properties.supported_codecs = supported_codecs;
294 370
295 concrete_key_system_map_[concrete_key_system] = properties; 371 concrete_key_system_map_[concrete_key_system] = properties;
296 372
297 if (!parent_key_system.empty()) { 373 if (!parent_key_system.empty()) {
298 DCHECK(!IsConcreteSupportedKeySystem(parent_key_system)) 374 DCHECK(!IsConcreteSupportedKeySystem(parent_key_system))
299 << "Parent '" << parent_key_system << "' already registered concrete"; 375 << "Parent '" << parent_key_system << "' already registered concrete";
300 DCHECK(parent_key_system_map_.find(parent_key_system) == 376 DCHECK(!parent_key_system_map_.count(parent_key_system))
301 parent_key_system_map_.end())
302 << "Parent '" << parent_key_system << "' already registered"; 377 << "Parent '" << parent_key_system << "' already registered";
303 parent_key_system_map_[parent_key_system] = concrete_key_system; 378 parent_key_system_map_[parent_key_system] = concrete_key_system;
304 } 379 }
305 } 380 }
306 381
307 bool KeySystems::IsConcreteSupportedKeySystem(const std::string& key_system) { 382 bool KeySystems::IsConcreteSupportedKeySystem(const std::string& key_system) {
308 DCHECK(thread_checker_.CalledOnValidThread()); 383 DCHECK(thread_checker_.CalledOnValidThread());
309 return concrete_key_system_map_.find(key_system) != 384 return concrete_key_system_map_.count(key_system) != 0;
310 concrete_key_system_map_.end();
311 } 385 }
312 386
313 bool KeySystems::IsSupportedContainer( 387 bool KeySystems::IsSupportedContainer(
314 const std::string& container, 388 const std::string& container,
315 SupportedCodecs key_system_supported_codecs) const { 389 SupportedCodecs key_system_supported_codecs) const {
316 DCHECK(thread_checker_.CalledOnValidThread()); 390 DCHECK(thread_checker_.CalledOnValidThread());
317 DCHECK(!container.empty()); 391 DCHECK(!container.empty());
318 392
319 // When checking container support for EME, "audio/foo" should be treated the 393 // When checking container support for EME, "audio/foo" should be treated the
320 // same as "video/foo". Convert the |container| to achieve this. 394 // same as "video/foo". Convert the |container| to achieve this.
321 // TODO(xhwang): Replace this with real checks against supported initDataTypes 395 // TODO(xhwang): Replace this with real checks against supported initDataTypes
322 // combined with supported demuxers. 396 // combined with supported demuxers.
323 std::string canonical_container = container; 397 std::string canonical_container = container;
324 if (container.find("audio/") == 0) 398 if (container.find("audio/") == 0)
325 canonical_container.replace(0, 6, "video/"); 399 canonical_container.replace(0, 6, "video/");
326 400
327 CodecMaskMap::const_iterator container_iter =
328 container_codec_masks_.find(canonical_container);
329 // Unrecognized container.
330 if (container_iter == container_codec_masks_.end())
331 return false;
332
333 EmeCodec container_codec_mask = container_iter->second;
334 // A container is supported iif at least one codec in that container is 401 // A container is supported iif at least one codec in that container is
335 // supported. 402 // supported.
336 return (container_codec_mask & key_system_supported_codecs) != 0; 403 SupportedCodecs supported_codecs =
404 GetCodecMaskForContainer(canonical_container);
405 return (supported_codecs & key_system_supported_codecs) != 0;
337 } 406 }
338 407
339 bool KeySystems::IsSupportedContainerAndCodecs( 408 bool KeySystems::IsSupportedContainerAndCodecs(
340 const std::string& container, 409 const std::string& container,
341 const std::vector<std::string>& codecs, 410 const std::vector<std::string>& codecs,
342 SupportedCodecs key_system_supported_codecs) const { 411 SupportedCodecs key_system_supported_codecs) const {
343 DCHECK(thread_checker_.CalledOnValidThread()); 412 DCHECK(thread_checker_.CalledOnValidThread());
344 DCHECK(!container.empty()); 413 DCHECK(!container.empty());
345 DCHECK(!codecs.empty()); 414 DCHECK(!codecs.empty());
346 DCHECK(IsSupportedContainer(container, key_system_supported_codecs)); 415 DCHECK(IsSupportedContainer(container, key_system_supported_codecs));
347 416
348 CodecMaskMap::const_iterator container_iter = 417 SupportedCodecs container_supported_codecs =
349 container_codec_masks_.find(container); 418 GetCodecMaskForContainer(container);
350 EmeCodec container_codec_mask = container_iter->second;
351 419
352 for (size_t i = 0; i < codecs.size(); ++i) { 420 for (size_t i = 0; i < codecs.size(); ++i) {
353 const std::string& codec = codecs[i]; 421 // TODO(sandersd): This should fail for isTypeSupported().
354 if (codec.empty()) 422 // http://crbug.com/417461
423 if (codecs[i].empty())
355 continue; 424 continue;
356 CodecMaskMap::const_iterator codec_iter = codec_masks_.find(codec);
357 if (codec_iter == codec_masks_.end()) // Unrecognized codec.
358 return false;
359 425
360 EmeCodec codec_mask = codec_iter->second; 426 EmeCodec codec = GetCodecForString(codecs[i]);
361 if (!(codec_mask & key_system_supported_codecs)) // Unsupported codec. 427
428 // Unsupported codec.
429 if (!(codec & key_system_supported_codecs))
362 return false; 430 return false;
363 431
364 // Unsupported codec/container combination, e.g. "video/webm" and "avc1". 432 // Unsupported codec/container combination, e.g. "video/webm" and "avc1".
365 if (!(codec_mask & container_codec_mask)) 433 if (!(codec & container_supported_codecs))
366 return false; 434 return false;
367 } 435 }
368 436
369 return true; 437 return true;
370 } 438 }
371 439
440 bool KeySystems::IsSupportedKeySystem(const std::string& key_system) {
441 DCHECK(thread_checker_.CalledOnValidThread());
442 return (concrete_key_system_map_.count(key_system) ||
443 parent_key_system_map_.count(key_system));
444 }
445
446 bool KeySystems::IsSupportedKeySystemWithInitDataType(
447 const std::string& key_system,
448 const std::string& init_data_type) {
449 DCHECK(thread_checker_.CalledOnValidThread());
450
451 // If |key_system| is a parent key system, use its concrete child.
452 const std::string& concrete_key_system = GetConcreteKeySystemName(key_system);
453
454 // Locate |concrete_key_system|.
455 KeySystemPropertiesMap::const_iterator key_system_iter =
456 concrete_key_system_map_.find(concrete_key_system);
457 if (key_system_iter == concrete_key_system_map_.end())
458 return false;
459
460 // Check |init_data_type| and |key_system| x |init_data_type|.
461 const KeySystemProperties& properties = key_system_iter->second;
462 EmeInitDataType eme_init_data_type = GetInitDataTypeForName(init_data_type);
463 return (properties.supported_init_data_types & eme_init_data_type) != 0;
464 }
465
466 // TODO(sandersd): Reorganize to be more similar to
467 // IsKeySystemSupportedWithInitDataType(). Note that a fork may still be
468 // required; http://crbug.com/417461.
372 bool KeySystems::IsSupportedKeySystemWithMediaMimeType( 469 bool KeySystems::IsSupportedKeySystemWithMediaMimeType(
373 const std::string& mime_type, 470 const std::string& mime_type,
374 const std::vector<std::string>& codecs, 471 const std::vector<std::string>& codecs,
375 const std::string& key_system) { 472 const std::string& key_system) {
376 DCHECK(thread_checker_.CalledOnValidThread()); 473 DCHECK(thread_checker_.CalledOnValidThread());
377 474
378 // If |key_system| is a parent key_system, use its concrete child. 475 // If |key_system| is a parent key system, use its concrete child.
379 // Otherwise, use |key_system|. 476 const std::string& concrete_key_system = GetConcreteKeySystemName(key_system);
380 std::string concrete_key_system;
381 ParentKeySystemMap::iterator parent_key_system_iter =
382 parent_key_system_map_.find(key_system);
383 if (parent_key_system_iter != parent_key_system_map_.end())
384 concrete_key_system = parent_key_system_iter->second;
385 else
386 concrete_key_system = key_system;
387 477
388 bool has_type = !mime_type.empty(); 478 bool has_type = !mime_type.empty();
389 479
390 key_systems_support_uma_.ReportKeySystemQuery(key_system, has_type); 480 key_systems_support_uma_.ReportKeySystemQuery(key_system, has_type);
391 481
392 // Check key system support. 482 // Check key system support.
393 KeySystemPropertiesMap::const_iterator key_system_iter = 483 KeySystemPropertiesMap::const_iterator key_system_iter =
394 concrete_key_system_map_.find(concrete_key_system); 484 concrete_key_system_map_.find(concrete_key_system);
395 if (key_system_iter == concrete_key_system_map_.end()) 485 if (key_system_iter == concrete_key_system_map_.end())
396 return false; 486 return false;
(...skipping 17 matching lines...) Expand all
414 return false; 504 return false;
415 } 505 }
416 506
417 key_systems_support_uma_.ReportKeySystemSupport(key_system, true); 507 key_systems_support_uma_.ReportKeySystemSupport(key_system, true);
418 return true; 508 return true;
419 } 509 }
420 510
421 bool KeySystems::UseAesDecryptor(const std::string& concrete_key_system) { 511 bool KeySystems::UseAesDecryptor(const std::string& concrete_key_system) {
422 DCHECK(thread_checker_.CalledOnValidThread()); 512 DCHECK(thread_checker_.CalledOnValidThread());
423 513
424 KeySystemPropertiesMap::iterator key_system_iter = 514 KeySystemPropertiesMap::const_iterator key_system_iter =
425 concrete_key_system_map_.find(concrete_key_system); 515 concrete_key_system_map_.find(concrete_key_system);
426 if (key_system_iter == concrete_key_system_map_.end()) { 516 if (key_system_iter == concrete_key_system_map_.end()) {
427 DLOG(FATAL) << concrete_key_system << " is not a known concrete system"; 517 DLOG(FATAL) << concrete_key_system << " is not a known concrete system";
428 return false; 518 return false;
429 } 519 }
430 520
431 return key_system_iter->second.use_aes_decryptor; 521 return key_system_iter->second.use_aes_decryptor;
432 } 522 }
433 523
434 #if defined(ENABLE_PEPPER_CDMS) 524 #if defined(ENABLE_PEPPER_CDMS)
435 std::string KeySystems::GetPepperType(const std::string& concrete_key_system) { 525 std::string KeySystems::GetPepperType(const std::string& concrete_key_system) {
436 DCHECK(thread_checker_.CalledOnValidThread()); 526 DCHECK(thread_checker_.CalledOnValidThread());
437 527
438 KeySystemPropertiesMap::iterator key_system_iter = 528 KeySystemPropertiesMap::const_iterator key_system_iter =
439 concrete_key_system_map_.find(concrete_key_system); 529 concrete_key_system_map_.find(concrete_key_system);
440 if (key_system_iter == concrete_key_system_map_.end()) { 530 if (key_system_iter == concrete_key_system_map_.end()) {
441 DLOG(FATAL) << concrete_key_system << " is not a known concrete system"; 531 DLOG(FATAL) << concrete_key_system << " is not a known concrete system";
442 return std::string(); 532 return std::string();
443 } 533 }
444 534
445 const std::string& type = key_system_iter->second.pepper_type; 535 const std::string& type = key_system_iter->second.pepper_type;
446 DLOG_IF(FATAL, type.empty()) << concrete_key_system << " is not Pepper-based"; 536 DLOG_IF(FATAL, type.empty()) << concrete_key_system << " is not Pepper-based";
447 return type; 537 return type;
448 } 538 }
449 #endif 539 #endif
450 540
451 void KeySystems::AddContainerMask(const std::string& container, uint32 mask) { 541 void KeySystems::AddContainerMask(const std::string& container, uint32 mask) {
452 DCHECK(thread_checker_.CalledOnValidThread()); 542 DCHECK(thread_checker_.CalledOnValidThread());
453 DCHECK(container_codec_masks_.find(container) == 543 DCHECK(!container_to_codec_mask_map_.count(container));
454 container_codec_masks_.end()); 544 container_to_codec_mask_map_[container] = static_cast<EmeCodec>(mask);
455
456 container_codec_masks_[container] = static_cast<EmeCodec>(mask);
457 } 545 }
458 546
459 void KeySystems::AddCodecMask(const std::string& codec, uint32 mask) { 547 void KeySystems::AddCodecMask(const std::string& codec, uint32 mask) {
460 DCHECK(thread_checker_.CalledOnValidThread()); 548 DCHECK(thread_checker_.CalledOnValidThread());
461 DCHECK(codec_masks_.find(codec) == codec_masks_.end()); 549 DCHECK(!codec_string_map_.count(codec));
462 550 codec_string_map_[codec] = static_cast<EmeCodec>(mask);
463 codec_masks_[codec] = static_cast<EmeCodec>(mask);
464 } 551 }
465 552
466 //------------------------------------------------------------------------------ 553 //------------------------------------------------------------------------------
467 554
468 std::string GetUnprefixedKeySystemName(const std::string& key_system) { 555 std::string GetUnprefixedKeySystemName(const std::string& key_system) {
469 if (key_system == kClearKeyKeySystem) 556 if (key_system == kClearKeyKeySystem)
470 return kUnsupportedClearKeyKeySystem; 557 return kUnsupportedClearKeyKeySystem;
471 558
472 if (key_system == kPrefixedClearKeyKeySystem) 559 if (key_system == kPrefixedClearKeyKeySystem)
473 return kClearKeyKeySystem; 560 return kClearKeyKeySystem;
474 561
475 return key_system; 562 return key_system;
476 } 563 }
477 564
478 std::string GetPrefixedKeySystemName(const std::string& key_system) { 565 std::string GetPrefixedKeySystemName(const std::string& key_system) {
479 DCHECK_NE(key_system, kPrefixedClearKeyKeySystem); 566 DCHECK_NE(key_system, kPrefixedClearKeyKeySystem);
480 567
481 if (key_system == kClearKeyKeySystem) 568 if (key_system == kClearKeyKeySystem)
482 return kPrefixedClearKeyKeySystem; 569 return kPrefixedClearKeyKeySystem;
483 570
484 return key_system; 571 return key_system;
485 } 572 }
486 573
574 bool IsSaneInitDataTypeWithContainer(
575 const std::string& init_data_type,
576 const std::string& container) {
577 if (init_data_type == "cenc") {
578 return container == "audio/mp4" || container == "video/mp4";
579 } else if (init_data_type == "webm") {
580 return container == "audio/webm" || container == "video/webm";
581 } else {
582 return true;
583 }
584 }
585
487 bool IsConcreteSupportedKeySystem(const std::string& key_system) { 586 bool IsConcreteSupportedKeySystem(const std::string& key_system) {
488 return KeySystems::GetInstance().IsConcreteSupportedKeySystem(key_system); 587 return KeySystems::GetInstance().IsConcreteSupportedKeySystem(key_system);
489 } 588 }
490 589
590 bool IsSupportedKeySystem(const std::string& key_system) {
591 return KeySystems::GetInstance().IsSupportedKeySystem(key_system);
592 }
593
594 bool IsSupportedKeySystemWithInitDataType(
595 const std::string& key_system,
596 const std::string& init_data_type) {
597 return KeySystems::GetInstance().IsSupportedKeySystemWithInitDataType(
598 key_system, init_data_type);
599 }
600
491 bool IsSupportedKeySystemWithMediaMimeType( 601 bool IsSupportedKeySystemWithMediaMimeType(
492 const std::string& mime_type, 602 const std::string& mime_type,
493 const std::vector<std::string>& codecs, 603 const std::vector<std::string>& codecs,
494 const std::string& key_system) { 604 const std::string& key_system) {
495 return KeySystems::GetInstance().IsSupportedKeySystemWithMediaMimeType( 605 return KeySystems::GetInstance().IsSupportedKeySystemWithMediaMimeType(
496 mime_type, codecs, key_system); 606 mime_type, codecs, key_system);
497 } 607 }
498 608
499 std::string KeySystemNameForUMA(const std::string& key_system) { 609 std::string KeySystemNameForUMA(const std::string& key_system) {
500 if (key_system == kClearKeyKeySystem) 610 if (key_system == kClearKeyKeySystem)
(...skipping 24 matching lines...) Expand all
525 CONTENT_EXPORT void AddContainerMask(const std::string& container, 635 CONTENT_EXPORT void AddContainerMask(const std::string& container,
526 uint32 mask) { 636 uint32 mask) {
527 KeySystems::GetInstance().AddContainerMask(container, mask); 637 KeySystems::GetInstance().AddContainerMask(container, mask);
528 } 638 }
529 639
530 CONTENT_EXPORT void AddCodecMask(const std::string& codec, uint32 mask) { 640 CONTENT_EXPORT void AddCodecMask(const std::string& codec, uint32 mask) {
531 KeySystems::GetInstance().AddCodecMask(codec, mask); 641 KeySystems::GetInstance().AddCodecMask(codec, mask);
532 } 642 }
533 643
534 } // namespace content 644 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/crypto/key_systems.h ('k') | content/renderer/media/crypto/key_systems_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698