Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / renderer / media / chrome_key_systems.cc
blob5454c813bf58de41988eb7ce0c69109397d2806b
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 "chrome/renderer/media/chrome_key_systems.h"
7 #include <string>
9 #include "base/logging.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/common/render_messages.h"
14 #include "content/public/renderer/render_thread.h"
16 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
18 // The following must be after widevine_cdm_version.h.
20 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_MIN_GLIBC_VERSION)
21 #include <gnu/libc-version.h>
22 #include "base/version.h"
23 #endif
25 #if defined(OS_ANDROID)
26 #include "chrome/common/encrypted_media_messages_android.h"
27 #endif
29 using content::KeySystemInfo;
31 const char kAudioWebM[] = "audio/webm";
32 const char kVideoWebM[] = "video/webm";
33 const char kVorbis[] = "vorbis";
34 const char kVorbisVP8[] = "vorbis,vp8,vp8.0";
36 #if defined(USE_PROPRIETARY_CODECS)
37 const char kAudioMp4[] = "audio/mp4";
38 const char kVideoMp4[] = "video/mp4";
39 const char kMp4a[] = "mp4a";
40 #if defined(WIDEVINE_CDM_AVAILABLE)
41 const char kAvc1Avc3[] = "avc1,avc3";
42 #endif // WIDEVINE_CDM_AVAILABLE
43 const char kMp4aAvc1Avc3[] = "mp4a,avc1,avc3";
44 #endif // defined(USE_PROPRIETARY_CODECS)
46 #if defined(ENABLE_PEPPER_CDMS)
47 static bool IsPepperCdmRegistered(
48 const std::string& pepper_type,
49 std::vector<base::string16>* additional_param_names,
50 std::vector<base::string16>* additional_param_values) {
51 bool is_registered = false;
52 content::RenderThread::Get()->Send(
53 new ChromeViewHostMsg_IsInternalPluginRegisteredForMimeType(
54 pepper_type,
55 &is_registered,
56 additional_param_names,
57 additional_param_values));
59 return is_registered;
62 // External Clear Key (used for testing).
63 static void AddExternalClearKey(
64 std::vector<KeySystemInfo>* concrete_key_systems) {
65 static const char kExternalClearKeyKeySystem[] =
66 "org.chromium.externalclearkey";
67 static const char kExternalClearKeyDecryptOnlyKeySystem[] =
68 "org.chromium.externalclearkey.decryptonly";
69 static const char kExternalClearKeyFileIOTestKeySystem[] =
70 "org.chromium.externalclearkey.fileiotest";
71 static const char kExternalClearKeyInitializeFailKeySystem[] =
72 "org.chromium.externalclearkey.initializefail";
73 static const char kExternalClearKeyCrashKeySystem[] =
74 "org.chromium.externalclearkey.crash";
75 static const char kExternalClearKeyPepperType[] =
76 "application/x-ppapi-clearkey-cdm";
78 std::vector<base::string16> additional_param_names;
79 std::vector<base::string16> additional_param_values;
80 if (!IsPepperCdmRegistered(kExternalClearKeyPepperType,
81 &additional_param_names,
82 &additional_param_values)) {
83 return;
86 KeySystemInfo info(kExternalClearKeyKeySystem);
88 info.supported_types.push_back(std::make_pair(kAudioWebM, kVorbis));
89 info.supported_types.push_back(std::make_pair(kVideoWebM, kVorbisVP8));
90 #if defined(USE_PROPRIETARY_CODECS)
91 info.supported_types.push_back(std::make_pair(kAudioMp4, kMp4a));
92 info.supported_types.push_back(std::make_pair(kVideoMp4, kMp4aAvc1Avc3));
93 #endif // defined(USE_PROPRIETARY_CODECS)
94 info.pepper_type = kExternalClearKeyPepperType;
96 concrete_key_systems->push_back(info);
98 // Add support of decrypt-only mode in ClearKeyCdm.
99 info.key_system = kExternalClearKeyDecryptOnlyKeySystem;
100 concrete_key_systems->push_back(info);
102 // A key system that triggers FileIO test in ClearKeyCdm.
103 info.key_system = kExternalClearKeyFileIOTestKeySystem;
104 concrete_key_systems->push_back(info);
106 // A key system that Chrome thinks is supported by ClearKeyCdm, but actually
107 // will be refused by ClearKeyCdm. This is to test the CDM initialization
108 // failure case.
109 info.key_system = kExternalClearKeyInitializeFailKeySystem;
110 concrete_key_systems->push_back(info);
112 // A key system that triggers a crash in ClearKeyCdm.
113 info.key_system = kExternalClearKeyCrashKeySystem;
114 concrete_key_systems->push_back(info);
116 #endif // defined(ENABLE_PEPPER_CDMS)
119 #if defined(WIDEVINE_CDM_AVAILABLE)
120 enum WidevineCdmType {
121 WIDEVINE,
122 WIDEVINE_HR,
123 #if defined(OS_ANDROID)
124 WIDEVINE_HR_NON_COMPOSITING,
125 #endif
128 // Defines bitmask values used to specify supported codecs.
129 // Each value represents a codec within a specific container.
130 // The mask values are stored in a SupportedCodecs.
131 typedef uint32 SupportedCodecs;
132 enum SupportedCodecMasks {
133 NO_CODECS = 0,
134 WEBM_VP8_AND_VORBIS = 1 << 0,
135 #if defined(USE_PROPRIETARY_CODECS)
136 MP4_AAC = 1 << 1,
137 MP4_AVC1 = 1 << 2,
138 MP4_CODECS = (MP4_AAC | MP4_AVC1),
139 #endif // defined(USE_PROPRIETARY_CODECS)
142 #if defined(OS_ANDROID)
143 #define COMPILE_ASSERT_MATCHING_ENUM(name) \
144 COMPILE_ASSERT(static_cast<int>(name) == \
145 static_cast<int>(android::name), \
146 mismatching_enums)
147 COMPILE_ASSERT_MATCHING_ENUM(WEBM_VP8_AND_VORBIS);
148 COMPILE_ASSERT_MATCHING_ENUM(MP4_AAC);
149 COMPILE_ASSERT_MATCHING_ENUM(MP4_AVC1);
150 #undef COMPILE_ASSERT_MATCHING_ENUM
152 static const uint8 kWidevineUuid[16] = {
153 0xED, 0xEF, 0x8B, 0xA9, 0x79, 0xD6, 0x4A, 0xCE,
154 0xA3, 0xC8, 0x27, 0xDC, 0xD5, 0x1D, 0x21, 0xED };
155 #else
156 static bool IsWidevineHrSupported() {
157 // TODO(jrummell): Need to call CheckPlatformState() but it is
158 // asynchronous, and needs to be done in the browser.
159 return false;
161 #endif
163 // Return |name|'s parent key system.
164 static std::string GetDirectParentName(std::string name) {
165 int last_period = name.find_last_of('.');
166 DCHECK_GT(last_period, 0);
167 return name.substr(0, last_period);
170 static void AddWidevineWithCodecs(
171 WidevineCdmType widevine_cdm_type,
172 SupportedCodecs supported_codecs,
173 std::vector<KeySystemInfo>* concrete_key_systems) {
175 KeySystemInfo info(kWidevineKeySystem);
177 switch (widevine_cdm_type) {
178 case WIDEVINE:
179 // For standard Widevine, add parent name.
180 info.parent_key_system = GetDirectParentName(kWidevineKeySystem);
181 break;
182 case WIDEVINE_HR:
183 info.key_system.append(".hr");
184 break;
185 #if defined(OS_ANDROID)
186 case WIDEVINE_HR_NON_COMPOSITING:
187 info.key_system.append(".hrnoncompositing");
188 break;
189 #endif
190 default:
191 NOTREACHED();
194 if (supported_codecs & WEBM_VP8_AND_VORBIS) {
195 info.supported_types.push_back(std::make_pair(kAudioWebM, kVorbis));
196 info.supported_types.push_back(std::make_pair(kVideoWebM, kVorbisVP8));
199 #if defined(USE_PROPRIETARY_CODECS)
200 if (supported_codecs & MP4_CODECS) {
201 // MP4 container is supported for audio and video if any codec is supported.
202 bool is_aac_supported = (supported_codecs & MP4_AAC) != NO_CODECS;
203 bool is_avc1_supported = (supported_codecs & MP4_AVC1) != NO_CODECS;
204 const char* video_codecs = is_avc1_supported ?
205 (is_aac_supported ? kMp4aAvc1Avc3 : kAvc1Avc3) :
207 const char* audio_codecs = is_aac_supported ? kMp4a : "";
209 info.supported_types.push_back(std::make_pair(kAudioMp4, audio_codecs));
210 info.supported_types.push_back(std::make_pair(kVideoMp4, video_codecs));
212 #endif // defined(USE_PROPRIETARY_CODECS)
214 #if defined(ENABLE_PEPPER_CDMS)
215 info.pepper_type = kWidevineCdmPluginMimeType;
216 #elif defined(OS_ANDROID)
217 info.uuid.assign(kWidevineUuid, kWidevineUuid + arraysize(kWidevineUuid));
218 #endif // defined(ENABLE_PEPPER_CDMS)
220 concrete_key_systems->push_back(info);
223 #if defined(ENABLE_PEPPER_CDMS)
224 // When the adapter is registered, a name-value pair is inserted in
225 // additional_param_* that lists the supported codecs. The name is "codecs" and
226 // the value is a comma-delimited list of codecs.
227 // This function finds "codecs" and parses the value into the vector |codecs|.
228 // Converts the codec strings to UTF-8 since we only expect ASCII strings and
229 // this simplifies the rest of the code in this file.
230 void GetSupportedCodecs(
231 const std::vector<base::string16>& additional_param_names,
232 const std::vector<base::string16>& additional_param_values,
233 std::vector<std::string>* codecs) {
234 DCHECK(codecs->empty());
235 DCHECK_EQ(additional_param_names.size(), additional_param_values.size());
236 for (size_t i = 0; i < additional_param_names.size(); ++i) {
237 if (additional_param_names[i] ==
238 base::ASCIIToUTF16(kCdmSupportedCodecsParamName)) {
239 const base::string16& codecs_string16 = additional_param_values[i];
240 std::string codecs_string;
241 if (!base::UTF16ToUTF8(codecs_string16.c_str(),
242 codecs_string16.length(),
243 &codecs_string)) {
244 DLOG(WARNING) << "Non-UTF-8 codecs string.";
245 // Continue using the best effort conversion.
247 base::SplitString(codecs_string,
248 kCdmSupportedCodecsValueDelimiter,
249 codecs);
250 break;
255 static void AddPepperBasedWidevine(
256 std::vector<KeySystemInfo>* concrete_key_systems) {
257 #if defined(WIDEVINE_CDM_MIN_GLIBC_VERSION)
258 Version glibc_version(gnu_get_libc_version());
259 DCHECK(glibc_version.IsValid());
260 if (glibc_version.IsOlderThan(WIDEVINE_CDM_MIN_GLIBC_VERSION))
261 return;
262 #endif // defined(WIDEVINE_CDM_MIN_GLIBC_VERSION)
264 std::vector<base::string16> additional_param_names;
265 std::vector<base::string16> additional_param_values;
266 if (!IsPepperCdmRegistered(kWidevineCdmPluginMimeType,
267 &additional_param_names,
268 &additional_param_values)) {
269 DVLOG(1) << "Widevine CDM is not currently available.";
270 return;
273 std::vector<std::string> codecs;
274 GetSupportedCodecs(additional_param_names, additional_param_values, &codecs);
276 SupportedCodecs supported_codecs = NO_CODECS;
277 for (size_t i = 0; i < codecs.size(); ++i) {
278 // TODO(ddorwin): Break up VP8 and Vorbis. For now, "vp8" implies both.
279 if (codecs[i] == kCdmSupportedCodecVp8)
280 supported_codecs |= WEBM_VP8_AND_VORBIS;
281 #if defined(USE_PROPRIETARY_CODECS)
282 if (codecs[i] == kCdmSupportedCodecAac)
283 supported_codecs |= MP4_AAC;
284 if (codecs[i] == kCdmSupportedCodecAvc1)
285 supported_codecs |= MP4_AVC1;
286 #endif // defined(USE_PROPRIETARY_CODECS)
289 AddWidevineWithCodecs(WIDEVINE, supported_codecs, concrete_key_systems);
291 if (IsWidevineHrSupported())
292 AddWidevineWithCodecs(WIDEVINE_HR, supported_codecs, concrete_key_systems);
294 #elif defined(OS_ANDROID)
295 static void AddAndroidWidevine(
296 std::vector<KeySystemInfo>* concrete_key_systems) {
297 SupportedKeySystemRequest request;
298 SupportedKeySystemResponse response;
300 request.uuid.insert(request.uuid.begin(), kWidevineUuid,
301 kWidevineUuid + arraysize(kWidevineUuid));
302 #if defined(USE_PROPRIETARY_CODECS)
303 request.codecs = static_cast<android::SupportedCodecs>(
304 android::MP4_AAC | android::MP4_AVC1);
305 #endif // defined(USE_PROPRIETARY_CODECS)
306 content::RenderThread::Get()->Send(
307 new ChromeViewHostMsg_GetSupportedKeySystems(request, &response));
308 DCHECK_EQ(response.compositing_codecs >> 3, 0) << "unrecognized codec";
309 DCHECK_EQ(response.non_compositing_codecs >> 3, 0) << "unrecognized codec";
310 if (response.compositing_codecs != android::NO_SUPPORTED_CODECS) {
311 AddWidevineWithCodecs(
312 WIDEVINE,
313 static_cast<SupportedCodecs>(response.compositing_codecs),
314 concrete_key_systems);
317 if (response.non_compositing_codecs != android::NO_SUPPORTED_CODECS) {
318 AddWidevineWithCodecs(
319 WIDEVINE_HR_NON_COMPOSITING,
320 static_cast<SupportedCodecs>(response.non_compositing_codecs),
321 concrete_key_systems);
324 #endif // defined(ENABLE_PEPPER_CDMS)
325 #endif // defined(WIDEVINE_CDM_AVAILABLE)
327 void AddChromeKeySystems(std::vector<KeySystemInfo>* key_systems_info) {
328 #if defined(ENABLE_PEPPER_CDMS)
329 AddExternalClearKey(key_systems_info);
330 #endif
332 #if defined(WIDEVINE_CDM_AVAILABLE)
333 #if defined(ENABLE_PEPPER_CDMS)
334 AddPepperBasedWidevine(key_systems_info);
335 #elif defined(OS_ANDROID)
336 AddAndroidWidevine(key_systems_info);
337 #endif
338 #endif