summaryrefslogtreecommitdiffstats
path: root/dom/media/eme/EMEUtils.cpp
blob: 19639388dab9be96df2d872e2a002b8d24fa0e09 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/EMEUtils.h"

#include "jsfriendapi.h"
#include "MediaData.h"
#include "KeySystemConfig.h"
#include "mozilla/StaticPrefs_media.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/KeySystemNames.h"
#include "mozilla/dom/UnionTypes.h"
#include "nsContentUtils.h"

#ifdef MOZ_WMF_CDM
#  include "mozilla/PMFCDM.h"
#endif

namespace mozilla {

LogModule* GetEMELog() {
  static LazyLogModule log("EME");
  return log;
}

LogModule* GetEMEVerboseLog() {
  static LazyLogModule log("EMEV");
  return log;
}

void CopyArrayBufferViewOrArrayBufferData(
    const dom::ArrayBufferViewOrArrayBuffer& aBufferOrView,
    nsTArray<uint8_t>& aOutData) {
  aOutData.Clear();
  Unused << dom::AppendTypedArrayDataTo(aBufferOrView, aOutData);
}

bool IsClearkeyKeySystem(const nsAString& aKeySystem) {
  if (StaticPrefs::media_clearkey_test_key_systems_enabled()) {
    return aKeySystem.EqualsLiteral(kClearKeyKeySystemName) ||
           aKeySystem.EqualsLiteral(kClearKeyWithProtectionQueryKeySystemName);
  }
  return aKeySystem.EqualsLiteral(kClearKeyKeySystemName);
}

bool IsWidevineKeySystem(const nsAString& aKeySystem) {
  return aKeySystem.EqualsLiteral(kWidevineKeySystemName);
}

#ifdef MOZ_WMF_CDM
bool IsPlayReadyEnabled() {
  // 1=enabled encrypted and clear, 2=enabled encrytped.
  return StaticPrefs::media_eme_playready_enabled() &&
         (StaticPrefs::media_wmf_media_engine_enabled() == 1 ||
          StaticPrefs::media_wmf_media_engine_enabled() == 2);
}

bool IsPlayReadyKeySystemAndSupported(const nsAString& aKeySystem) {
  if (!IsPlayReadyEnabled()) {
    return false;
  }
  return aKeySystem.EqualsLiteral(kPlayReadyKeySystemName) ||
         aKeySystem.EqualsLiteral(kPlayReadyKeySystemHardware) ||
         aKeySystem.EqualsLiteral(kPlayReadyHardwareClearLeadKeySystemName);
}

bool IsWidevineHardwareDecryptionEnabled() {
  // 1=enabled encrypted and clear, 2=enabled encrytped.
  return StaticPrefs::media_eme_widevine_experiment_enabled() &&
         (StaticPrefs::media_wmf_media_engine_enabled() == 1 ||
          StaticPrefs::media_wmf_media_engine_enabled() == 2);
}

bool IsWidevineExperimentKeySystemAndSupported(const nsAString& aKeySystem) {
  if (!IsWidevineHardwareDecryptionEnabled()) {
    return false;
  }
  return aKeySystem.EqualsLiteral(kWidevineExperimentKeySystemName) ||
         aKeySystem.EqualsLiteral(kWidevineExperiment2KeySystemName);
}

bool IsWMFClearKeySystemAndSupported(const nsAString& aKeySystem) {
  if (!StaticPrefs::media_eme_wmf_clearkey_enabled()) {
    return false;
  }
  // 1=enabled encrypted and clear, 2=enabled encrytped.
  if (StaticPrefs::media_wmf_media_engine_enabled() != 1 &&
      StaticPrefs::media_wmf_media_engine_enabled() != 2) {
    return false;
  }
  return aKeySystem.EqualsLiteral(kClearKeyKeySystemName);
}
#endif

nsString KeySystemToProxyName(const nsAString& aKeySystem) {
  if (IsClearkeyKeySystem(aKeySystem)) {
#ifdef MOZ_WMF_CDM
    if (StaticPrefs::media_eme_wmf_clearkey_enabled()) {
      return u"mfcdm-clearkey"_ns;
    }
#endif
    return u"gmp-clearkey"_ns;
  }
  if (IsWidevineKeySystem(aKeySystem)) {
    return u"gmp-widevinecdm"_ns;
  }
#ifdef MOZ_WMF_CDM
  if (IsPlayReadyKeySystemAndSupported(aKeySystem)) {
    return u"mfcdm-playready"_ns;
  }
  if (IsWidevineExperimentKeySystemAndSupported(aKeySystem)) {
    return u"mfcdm-widevine"_ns;
  }
#endif
  MOZ_ASSERT_UNREACHABLE("Not supported key system!");
  return u""_ns;
}

bool IsHardwareDecryptionSupported(
    const dom::MediaKeySystemConfiguration& aConfig) {
  for (const auto& capabilities : aConfig.mAudioCapabilities) {
    if (capabilities.mRobustness.EqualsLiteral("HW_SECURE_ALL")) {
      return true;
    }
  }
  for (const auto& capabilities : aConfig.mVideoCapabilities) {
    if (capabilities.mRobustness.EqualsLiteral("3000") ||
        capabilities.mRobustness.EqualsLiteral("HW_SECURE_ALL") ||
        capabilities.mRobustness.EqualsLiteral("HW_SECURE_DECODE")) {
      return true;
    }
  }
  return false;
}

bool IsHardwareDecryptionSupported(const KeySystemConfig& aConfig) {
  for (const auto& robustness : aConfig.mAudioRobustness) {
    if (robustness.EqualsLiteral("HW_SECURE_ALL")) {
      return true;
    }
  }
  for (const auto& robustness : aConfig.mVideoRobustness) {
    if (robustness.EqualsLiteral("3000") ||
        robustness.EqualsLiteral("HW_SECURE_ALL") ||
        robustness.EqualsLiteral("HW_SECURE_DECODE")) {
      return true;
    }
  }
  return false;
}

const char* EncryptionSchemeStr(const CryptoScheme& aScheme) {
  switch (aScheme) {
    case CryptoScheme::None:
      return "none";
    case CryptoScheme::Cenc:
      return "cenc";
    case CryptoScheme::Cbcs:
      return "cbcs";
    default:
      return "not-defined!";
  }
}

#ifdef MOZ_WMF_CDM
void MFCDMCapabilitiesIPDLToKeySystemConfig(
    const MFCDMCapabilitiesIPDL& aCDMConfig,
    KeySystemConfig& aKeySystemConfig) {
  aKeySystemConfig.mKeySystem = aCDMConfig.keySystem();

  for (const auto& type : aCDMConfig.initDataTypes()) {
    aKeySystemConfig.mInitDataTypes.AppendElement(type);
  }

  for (const auto& type : aCDMConfig.sessionTypes()) {
    aKeySystemConfig.mSessionTypes.AppendElement(type);
  }

  for (const auto& c : aCDMConfig.videoCapabilities()) {
    if (!c.robustness().IsEmpty() &&
        !aKeySystemConfig.mVideoRobustness.Contains(c.robustness())) {
      aKeySystemConfig.mVideoRobustness.AppendElement(c.robustness());
    }
    aKeySystemConfig.mMP4.SetCanDecryptAndDecode(
        NS_ConvertUTF16toUTF8(c.contentType()));
  }
  for (const auto& c : aCDMConfig.audioCapabilities()) {
    if (!c.robustness().IsEmpty() &&
        !aKeySystemConfig.mAudioRobustness.Contains(c.robustness())) {
      aKeySystemConfig.mAudioRobustness.AppendElement(c.robustness());
    }
    aKeySystemConfig.mMP4.SetCanDecryptAndDecode(
        NS_ConvertUTF16toUTF8(c.contentType()));
  }
  aKeySystemConfig.mPersistentState = aCDMConfig.persistentState();
  aKeySystemConfig.mDistinctiveIdentifier = aCDMConfig.distinctiveID();
  for (const auto& scheme : aCDMConfig.encryptionSchemes()) {
    aKeySystemConfig.mEncryptionSchemes.AppendElement(
        NS_ConvertUTF8toUTF16(EncryptionSchemeStr(scheme)));
  }
  aKeySystemConfig.mIsHDCP22Compatible = aCDMConfig.isHDCP22Compatible()
                                             ? *aCDMConfig.isHDCP22Compatible()
                                             : false;
  EME_LOG("New Capabilities=%s",
          NS_ConvertUTF16toUTF8(aKeySystemConfig.GetDebugInfo()).get());
}
#endif

bool DoesKeySystemSupportClearLead(const nsAString& aKeySystem) {
  // I believe that Widevine L3 supports clear-lead, but I couldn't find any
  // official documentation to prove that. The only one I can find is that Shaka
  // player mentions the clear lead feature. So we expect L3 should have that as
  // well. But for HWDRM, Widevine L1 and SL3000 needs to rely on special checks
  // to know whether clearlead is supported. That will be implemented by
  // querying for special key system names.
  // https://shaka-project.github.io/shaka-packager/html/documentation.html
#ifdef MOZ_WMF_CDM
  if (aKeySystem.EqualsLiteral(kWidevineExperiment2KeySystemName) ||
      aKeySystem.EqualsLiteral(kPlayReadyHardwareClearLeadKeySystemName)) {
    return true;
  }
#endif
  return aKeySystem.EqualsLiteral(kWidevineKeySystemName);
}

bool CheckIfHarewareDRMConfigExists(
    const nsTArray<dom::MediaKeySystemConfiguration>& aConfigs) {
  bool foundHWDRMconfig = false;
  for (const auto& config : aConfigs) {
    if (IsHardwareDecryptionSupported(config)) {
      foundHWDRMconfig = true;
      break;
    }
  }
  return foundHWDRMconfig;
}

bool DoesKeySystemSupportHardwareDecryption(const nsAString& aKeySystem) {
#ifdef MOZ_WMF_CDM
  if (aKeySystem.EqualsLiteral(kPlayReadyKeySystemHardware) ||
      aKeySystem.EqualsLiteral(kPlayReadyHardwareClearLeadKeySystemName) ||
      aKeySystem.EqualsLiteral(kWidevineExperimentKeySystemName) ||
      aKeySystem.EqualsLiteral(kWidevineExperiment2KeySystemName)) {
    return true;
  }
#endif
  return false;
}

void DeprecationWarningLog(const dom::Document* aDocument,
                           const char* aMsgName) {
  if (!aDocument || !aMsgName) {
    return;
  }
  EME_LOG("DeprecationWarning Logging deprecation warning '%s' to WebConsole.",
          aMsgName);
  nsTHashMap<nsCharPtrHashKey, bool> warnings;
  warnings.InsertOrUpdate(aMsgName, true);
  AutoTArray<nsString, 1> params;
  nsString& uri = *params.AppendElement();
  Unused << aDocument->GetDocumentURI(uri);
  nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, "Media"_ns,
                                  aDocument, nsContentUtils::eDOM_PROPERTIES,
                                  aMsgName, params);
}

}  // namespace mozilla