1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "KeySystemConfig.h"
#include "EMEUtils.h"
#include "GMPUtils.h"
#include "KeySystemNames.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/StaticPrefs_media.h"
#include "nsPrintfCString.h"
#ifdef XP_WIN
# include "WMFDecoderModule.h"
#endif
#ifdef MOZ_WIDGET_ANDROID
# include "AndroidDecoderModule.h"
# include "mozilla/java/MediaDrmProxyWrappers.h"
# include "nsMimeTypes.h"
#endif
#ifdef MOZ_WMF_CDM
# include "mediafoundation/WMFCDMImpl.h"
#endif
namespace mozilla {
/* static */
bool KeySystemConfig::Supports(const nsAString& aKeySystem) {
#ifdef MOZ_WIDGET_ANDROID
// No GMP on Android, check if we can use MediaDrm for this keysystem.
if (mozilla::java::MediaDrmProxy::IsSchemeSupported(
NS_ConvertUTF16toUTF8(aKeySystem))) {
return true;
}
#else
# ifdef MOZ_WMF_CDM
// Test only, pretend we have already installed CDMs.
if (StaticPrefs::media_eme_wmf_use_mock_cdm_for_external_cdms()) {
return true;
}
# endif
// Check if Widevine L3 or Clearkey has been downloaded via GMP downloader.
if (IsWidevineKeySystem(aKeySystem) || IsClearkeyKeySystem(aKeySystem)) {
return HaveGMPFor(nsCString(CHROMIUM_CDM_API),
{NS_ConvertUTF16toUTF8(aKeySystem)});
}
#endif
#if MOZ_WMF_CDM
// Check if Widevine L1 has been downloaded via GMP downloader.
if (IsWidevineExperimentKeySystemAndSupported(aKeySystem)) {
return HaveGMPFor(nsCString(kWidevineExperimentAPIName),
{nsCString(kWidevineExperimentKeySystemName)});
}
// PlayReady and WMF-based ClearKey are always installed, we don't need to
// download them.
if (IsPlayReadyKeySystemAndSupported(aKeySystem) ||
IsWMFClearKeySystemAndSupported(aKeySystem)) {
return true;
}
#endif
return false;
}
/* static */
bool KeySystemConfig::CreateKeySystemConfigs(
const nsAString& aKeySystem, const DecryptionInfo aDecryption,
nsTArray<KeySystemConfig>& aOutConfigs) {
if (!Supports(aKeySystem)) {
return false;
}
if (IsClearkeyKeySystem(aKeySystem)) {
KeySystemConfig* config = aOutConfigs.AppendElement();
config->mKeySystem = aKeySystem;
config->mInitDataTypes.AppendElement(u"cenc"_ns);
config->mInitDataTypes.AppendElement(u"keyids"_ns);
config->mInitDataTypes.AppendElement(u"webm"_ns);
config->mPersistentState = Requirement::Optional;
config->mDistinctiveIdentifier = Requirement::NotAllowed;
config->mSessionTypes.AppendElement(SessionType::Temporary);
config->mEncryptionSchemes.AppendElement(u"cenc"_ns);
config->mEncryptionSchemes.AppendElement(u"cbcs"_ns);
config->mEncryptionSchemes.AppendElement(u"cbcs-1-9"_ns);
if (StaticPrefs::media_clearkey_persistent_license_enabled()) {
config->mSessionTypes.AppendElement(SessionType::PersistentLicense);
}
#if defined(XP_WIN)
// Clearkey CDM uses WMF's H.264 decoder on Windows.
if (WMFDecoderModule::CanCreateMFTDecoder(WMFStreamType::H264)) {
config->mMP4.SetCanDecryptAndDecode(EME_CODEC_H264);
} else {
config->mMP4.SetCanDecrypt(EME_CODEC_H264);
}
#else
config->mMP4.SetCanDecrypt(EME_CODEC_H264);
#endif
config->mMP4.SetCanDecrypt(EME_CODEC_AAC);
config->mMP4.SetCanDecrypt(EME_CODEC_FLAC);
config->mMP4.SetCanDecrypt(EME_CODEC_OPUS);
config->mMP4.SetCanDecrypt(EME_CODEC_VP9);
#ifdef MOZ_AV1
config->mMP4.SetCanDecrypt(EME_CODEC_AV1);
#endif
config->mWebM.SetCanDecrypt(EME_CODEC_VORBIS);
config->mWebM.SetCanDecrypt(EME_CODEC_OPUS);
config->mWebM.SetCanDecrypt(EME_CODEC_VP8);
config->mWebM.SetCanDecrypt(EME_CODEC_VP9);
#ifdef MOZ_AV1
config->mWebM.SetCanDecrypt(EME_CODEC_AV1);
#endif
if (StaticPrefs::media_clearkey_test_key_systems_enabled()) {
// Add testing key systems. These offer the same capabilities as the
// base clearkey system, so just clone clearkey and change the name.
KeySystemConfig clearkeyWithProtectionQuery{*config};
clearkeyWithProtectionQuery.mKeySystem.AssignLiteral(
kClearKeyWithProtectionQueryKeySystemName);
aOutConfigs.AppendElement(std::move(clearkeyWithProtectionQuery));
}
return true;
}
if (IsWidevineKeySystem(aKeySystem)) {
KeySystemConfig* config = aOutConfigs.AppendElement();
config->mKeySystem = aKeySystem;
config->mInitDataTypes.AppendElement(u"cenc"_ns);
config->mInitDataTypes.AppendElement(u"keyids"_ns);
config->mInitDataTypes.AppendElement(u"webm"_ns);
config->mPersistentState = Requirement::Optional;
config->mDistinctiveIdentifier = Requirement::NotAllowed;
config->mSessionTypes.AppendElement(SessionType::Temporary);
#ifdef MOZ_WIDGET_ANDROID
config->mSessionTypes.AppendElement(SessionType::PersistentLicense);
#endif
config->mAudioRobustness.AppendElement(u"SW_SECURE_CRYPTO"_ns);
config->mVideoRobustness.AppendElement(u"SW_SECURE_CRYPTO"_ns);
config->mVideoRobustness.AppendElement(u"SW_SECURE_DECODE"_ns);
config->mEncryptionSchemes.AppendElement(u"cenc"_ns);
config->mEncryptionSchemes.AppendElement(u"cbcs"_ns);
config->mEncryptionSchemes.AppendElement(u"cbcs-1-9"_ns);
#if defined(MOZ_WIDGET_ANDROID)
// MediaDrm.isCryptoSchemeSupported only allows passing
// "video/mp4" or "video/webm" for mimetype string.
// See
// https://developer.android.com/reference/android/media/MediaDrm.html#isCryptoSchemeSupported(java.util.UUID,
// java.lang.String) for more detail.
typedef struct {
const nsCString& mMimeType;
const nsCString& mEMECodecType;
const char16_t* mCodecType;
KeySystemConfig::ContainerSupport* mSupportType;
} DataForValidation;
DataForValidation validationList[] = {
{nsCString(VIDEO_MP4), EME_CODEC_H264, java::MediaDrmProxy::AVC,
&config->mMP4},
{nsCString(VIDEO_MP4), EME_CODEC_VP9, java::MediaDrmProxy::AVC,
&config->mMP4},
# ifdef MOZ_AV1
{nsCString(VIDEO_MP4), EME_CODEC_AV1, java::MediaDrmProxy::AV1,
&config->mMP4},
# endif
{nsCString(AUDIO_MP4), EME_CODEC_AAC, java::MediaDrmProxy::AAC,
&config->mMP4},
{nsCString(AUDIO_MP4), EME_CODEC_FLAC, java::MediaDrmProxy::FLAC,
&config->mMP4},
{nsCString(AUDIO_MP4), EME_CODEC_OPUS, java::MediaDrmProxy::OPUS,
&config->mMP4},
{nsCString(VIDEO_WEBM), EME_CODEC_VP8, java::MediaDrmProxy::VP8,
&config->mWebM},
{nsCString(VIDEO_WEBM), EME_CODEC_VP9, java::MediaDrmProxy::VP9,
&config->mWebM},
# ifdef MOZ_AV1
{nsCString(VIDEO_WEBM), EME_CODEC_AV1, java::MediaDrmProxy::AV1,
&config->mWebM},
# endif
{nsCString(AUDIO_WEBM), EME_CODEC_VORBIS, java::MediaDrmProxy::VORBIS,
&config->mWebM},
{nsCString(AUDIO_WEBM), EME_CODEC_OPUS, java::MediaDrmProxy::OPUS,
&config->mWebM},
};
for (const auto& data : validationList) {
if (java::MediaDrmProxy::IsCryptoSchemeSupported(kWidevineKeySystemName,
data.mMimeType)) {
if (!AndroidDecoderModule::SupportsMimeType(data.mMimeType).isEmpty()) {
data.mSupportType->SetCanDecryptAndDecode(data.mEMECodecType);
} else {
data.mSupportType->SetCanDecrypt(data.mEMECodecType);
}
}
}
#else
# if defined(XP_WIN)
// Widevine CDM doesn't include an AAC decoder. So if WMF can't
// decode AAC, and a codec wasn't specified, be conservative
// and reject the MediaKeys request, since we assume Widevine
// will be used with AAC.
if (WMFDecoderModule::CanCreateMFTDecoder(WMFStreamType::AAC)) {
config->mMP4.SetCanDecrypt(EME_CODEC_AAC);
}
# else
config->mMP4.SetCanDecrypt(EME_CODEC_AAC);
# endif
config->mMP4.SetCanDecrypt(EME_CODEC_FLAC);
config->mMP4.SetCanDecrypt(EME_CODEC_OPUS);
config->mMP4.SetCanDecryptAndDecode(EME_CODEC_H264);
config->mMP4.SetCanDecryptAndDecode(EME_CODEC_VP9);
# ifdef MOZ_AV1
config->mMP4.SetCanDecryptAndDecode(EME_CODEC_AV1);
# endif
config->mWebM.SetCanDecrypt(EME_CODEC_VORBIS);
config->mWebM.SetCanDecrypt(EME_CODEC_OPUS);
config->mWebM.SetCanDecryptAndDecode(EME_CODEC_VP8);
config->mWebM.SetCanDecryptAndDecode(EME_CODEC_VP9);
# ifdef MOZ_AV1
config->mWebM.SetCanDecryptAndDecode(EME_CODEC_AV1);
# endif
#endif
return true;
}
#ifdef MOZ_WMF_CDM
if (IsPlayReadyKeySystemAndSupported(aKeySystem) ||
IsWidevineExperimentKeySystemAndSupported(aKeySystem)) {
RefPtr<WMFCDMImpl> cdm = MakeRefPtr<WMFCDMImpl>(aKeySystem);
return cdm->GetCapabilities(aDecryption == DecryptionInfo::Hardware,
aOutConfigs);
}
#endif
return false;
}
bool KeySystemConfig::IsSameKeySystem(const nsAString& aKeySystem) const {
#ifdef MOZ_WMF_CDM
// We want to map Widevine experiment key system to normal Widevine key system
// as well.
if (IsWidevineExperimentKeySystemAndSupported(mKeySystem)) {
return mKeySystem.Equals(aKeySystem) ||
aKeySystem.EqualsLiteral(kWidevineKeySystemName);
}
#endif
return mKeySystem.Equals(aKeySystem);
}
/* static */
void KeySystemConfig::GetGMPKeySystemConfigs(dom::Promise* aPromise) {
MOZ_ASSERT(aPromise);
nsTArray<KeySystemConfig> keySystemConfigs;
const nsTArray<nsString> keySystemNames{
NS_ConvertUTF8toUTF16(kClearKeyKeySystemName),
NS_ConvertUTF8toUTF16(kWidevineKeySystemName),
};
FallibleTArray<dom::CDMInformation> cdmInfo;
for (const auto& name : keySystemNames) {
#ifdef MOZ_WMF_CDM
if (IsWMFClearKeySystemAndSupported(name)) {
// Using wmf clearkey, not gmp clearkey.
continue;
}
#endif
if (KeySystemConfig::CreateKeySystemConfigs(
name, KeySystemConfig::DecryptionInfo::Software,
keySystemConfigs)) {
auto* info = cdmInfo.AppendElement(fallible);
if (!info) {
aPromise->MaybeReject(NS_ERROR_OUT_OF_MEMORY);
return;
}
MOZ_ASSERT(keySystemConfigs.Length() == cdmInfo.Length());
info->mKeySystemName = name;
info->mCapabilities = keySystemConfigs.LastElement().GetDebugInfo();
info->mClearlead = DoesKeySystemSupportClearLead(name);
// TODO : ask real CDM
info->mIsHDCP22Compatible = false;
}
}
aPromise->MaybeResolve(cdmInfo);
}
nsString KeySystemConfig::GetDebugInfo() const {
nsString debugInfo;
debugInfo.AppendLiteral(" key-system=");
debugInfo.Append(mKeySystem);
debugInfo.AppendLiteral(" init-data-type=[");
for (size_t idx = 0; idx < mInitDataTypes.Length(); idx++) {
debugInfo.Append(mInitDataTypes[idx]);
if (idx + 1 < mInitDataTypes.Length()) {
debugInfo.AppendLiteral(",");
}
}
debugInfo.AppendLiteral("]");
debugInfo.AppendASCII(
nsPrintfCString(" persistent=%s", RequirementToStr(mPersistentState))
.get());
debugInfo.AppendASCII(
nsPrintfCString(" distinctive=%s",
RequirementToStr(mDistinctiveIdentifier))
.get());
debugInfo.AppendLiteral(" sessionType=[");
for (size_t idx = 0; idx < mSessionTypes.Length(); idx++) {
debugInfo.AppendASCII(
nsPrintfCString("%s", SessionTypeToStr(mSessionTypes[idx])).get());
if (idx + 1 < mSessionTypes.Length()) {
debugInfo.AppendLiteral(",");
}
}
debugInfo.AppendLiteral("]");
debugInfo.AppendLiteral(" video-robustness=");
for (size_t idx = 0; idx < mVideoRobustness.Length(); idx++) {
debugInfo.Append(mVideoRobustness[idx]);
if (idx + 1 < mVideoRobustness.Length()) {
debugInfo.AppendLiteral(",");
}
}
debugInfo.AppendLiteral(" audio-robustness=");
for (size_t idx = 0; idx < mAudioRobustness.Length(); idx++) {
debugInfo.Append(mAudioRobustness[idx]);
if (idx + 1 < mAudioRobustness.Length()) {
debugInfo.AppendLiteral(",");
}
}
debugInfo.AppendLiteral(" scheme=[");
for (size_t idx = 0; idx < mEncryptionSchemes.Length(); idx++) {
debugInfo.Append(mEncryptionSchemes[idx]);
if (idx + 1 < mEncryptionSchemes.Length()) {
debugInfo.AppendLiteral(",");
}
}
debugInfo.AppendLiteral("]");
debugInfo.AppendLiteral(" MP4={");
debugInfo.Append(NS_ConvertUTF8toUTF16(mMP4.GetDebugInfo()));
debugInfo.AppendLiteral("}");
debugInfo.AppendLiteral(" WEBM={");
debugInfo.Append(NS_ConvertUTF8toUTF16(mWebM.GetDebugInfo()));
debugInfo.AppendLiteral("}");
debugInfo.AppendASCII(
nsPrintfCString(" isHDCP22Compatible=%d", mIsHDCP22Compatible));
return debugInfo;
}
KeySystemConfig::SessionType ConvertToKeySystemConfigSessionType(
dom::MediaKeySessionType aType) {
switch (aType) {
case dom::MediaKeySessionType::Temporary:
return KeySystemConfig::SessionType::Temporary;
case dom::MediaKeySessionType::Persistent_license:
return KeySystemConfig::SessionType::PersistentLicense;
default:
MOZ_ASSERT_UNREACHABLE("Invalid session type");
return KeySystemConfig::SessionType::Temporary;
}
}
const char* SessionTypeToStr(KeySystemConfig::SessionType aType) {
switch (aType) {
case KeySystemConfig::SessionType::Temporary:
return "Temporary";
case KeySystemConfig::SessionType::PersistentLicense:
return "PersistentLicense";
default:
MOZ_ASSERT_UNREACHABLE("Invalid session type");
return "Invalid";
}
}
const char* RequirementToStr(KeySystemConfig::Requirement aRequirement) {
switch (aRequirement) {
case KeySystemConfig::Requirement::Required:
return "required";
case KeySystemConfig::Requirement::Optional:
return "optional";
default:
return "not-allowed";
}
}
} // namespace mozilla
|