Skip to content

Commit

Permalink
Bug 1885350 - part4 : let WMFCDMImpl only query the specific decrypti…
Browse files Browse the repository at this point in the history
…on status. r=jolin

Before we always queried the capabilities from both software and
hardware decryption for every key system which WMFCDMImpl supports, now
it should only query the exact decryption state that we need for the
efficiency.

Differential Revision: https://phabricator.services.mozilla.com/D204713
  • Loading branch information
alastor0325 committed Mar 15, 2024
1 parent 20d1ae9 commit 85f2dc6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 54 deletions.
21 changes: 20 additions & 1 deletion dom/media/eme/EMEUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

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

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

namespace mozilla {
Expand Down Expand Up @@ -161,6 +161,25 @@ bool IsHardwareDecryptionSupported(
return supportHardwareDecryption;
}

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

const char* EncryptionSchemeStr(const CryptoScheme& aScheme) {
switch (aScheme) {
case CryptoScheme::None:
Expand Down
1 change: 1 addition & 0 deletions dom/media/eme/EMEUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const char* ToMediaKeyStatusStr(dom::MediaKeyStatus aStatus);
// Return true if given config supports hardware decryption (SL3000 or L1).
bool IsHardwareDecryptionSupported(
const dom::MediaKeySystemConfiguration& aConfig);
bool IsHardwareDecryptionSupported(const KeySystemConfig& aConfig);

const char* EncryptionSchemeStr(const CryptoScheme& aScheme);

Expand Down
10 changes: 7 additions & 3 deletions dom/media/eme/KeySystemConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ bool KeySystemConfig::Supports(const nsAString& aKeySystem) {

/* static */
bool KeySystemConfig::CreateKeySystemConfigs(
const nsAString& aKeySystem, nsTArray<KeySystemConfig>& aOutConfigs) {
const nsAString& aKeySystem, const DecryptionInfo aDecryption,
nsTArray<KeySystemConfig>& aOutConfigs) {
if (!Supports(aKeySystem)) {
return false;
}
Expand Down Expand Up @@ -230,7 +231,8 @@ bool KeySystemConfig::CreateKeySystemConfigs(
if (IsPlayReadyKeySystemAndSupported(aKeySystem) ||
IsWidevineExperimentKeySystemAndSupported(aKeySystem)) {
RefPtr<WMFCDMImpl> cdm = MakeRefPtr<WMFCDMImpl>(aKeySystem);
return cdm->GetCapabilities(aOutConfigs);
return cdm->GetCapabilities(aDecryption == DecryptionInfo::Hardware,
aOutConfigs);
}
#endif
return false;
Expand Down Expand Up @@ -264,7 +266,9 @@ void KeySystemConfig::GetGMPKeySystemConfigs(dom::Promise* aPromise) {
continue;
}
#endif
if (KeySystemConfig::CreateKeySystemConfigs(name, keySystemConfigs)) {
if (KeySystemConfig::CreateKeySystemConfigs(
name, KeySystemConfig::DecryptionInfo::Software,
keySystemConfigs)) {
auto* info = cdmInfo.AppendElement(fallible);
if (!info) {
aPromise->MaybeReject(NS_ERROR_OUT_OF_MEMORY);
Expand Down
6 changes: 6 additions & 0 deletions dom/media/eme/KeySystemConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ struct KeySystemConfig {

// Return true if given key system is supported on the current device.
static bool Supports(const nsAString& aKeySystem);

enum class DecryptionInfo : uint8_t {
Software,
Hardware,
};
static bool CreateKeySystemConfigs(const nsAString& aKeySystem,
const DecryptionInfo aDecryption,
nsTArray<KeySystemConfig>& aOutConfigs);
static void GetGMPKeySystemConfigs(dom::Promise* aPromise);

Expand Down
20 changes: 13 additions & 7 deletions dom/media/eme/MediaKeySystemAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,27 @@ static KeySystemConfig::EMECodecString ToEMEAPICodecString(

static nsTArray<KeySystemConfig> GetSupportedKeySystems(
const nsAString& aKeySystem, bool aIsHardwareDecryption) {
using DecryptionInfo = KeySystemConfig::DecryptionInfo;
nsTArray<KeySystemConfig> keySystemConfigs;
if (IsWidevineKeySystem(aKeySystem) || IsClearkeyKeySystem(aKeySystem)) {
Unused << KeySystemConfig::CreateKeySystemConfigs(aKeySystem,
keySystemConfigs);
Unused << KeySystemConfig::CreateKeySystemConfigs(
aKeySystem, DecryptionInfo::Software, keySystemConfigs);
}
#ifdef MOZ_WMF_CDM
if (IsPlayReadyKeySystem(aKeySystem)) {
Unused << KeySystemConfig::CreateKeySystemConfigs(
NS_ConvertUTF8toUTF16(kPlayReadyKeySystemName), keySystemConfigs);
NS_ConvertUTF8toUTF16(kPlayReadyKeySystemName),
DecryptionInfo::Software, keySystemConfigs);
if (aIsHardwareDecryption) {
Unused << KeySystemConfig::CreateKeySystemConfigs(
NS_ConvertUTF8toUTF16(kPlayReadyKeySystemHardware), keySystemConfigs);
NS_ConvertUTF8toUTF16(kPlayReadyKeySystemName),
DecryptionInfo::Hardware, keySystemConfigs);
Unused << KeySystemConfig::CreateKeySystemConfigs(
NS_ConvertUTF8toUTF16(kPlayReadyKeySystemHardware),
DecryptionInfo::Hardware, keySystemConfigs);
Unused << KeySystemConfig::CreateKeySystemConfigs(
NS_ConvertUTF8toUTF16(kPlayReadyHardwareClearLeadKeySystemName),
keySystemConfigs);
DecryptionInfo::Hardware, keySystemConfigs);
}
}
// If key system is kWidevineKeySystemName but with hardware decryption
Expand All @@ -257,10 +263,10 @@ static nsTArray<KeySystemConfig> GetSupportedKeySystems(
(IsWidevineKeySystem(aKeySystem) && aIsHardwareDecryption)) {
Unused << KeySystemConfig::CreateKeySystemConfigs(
NS_ConvertUTF8toUTF16(kWidevineExperimentKeySystemName),
keySystemConfigs);
DecryptionInfo::Hardware, keySystemConfigs);
Unused << KeySystemConfig::CreateKeySystemConfigs(
NS_ConvertUTF8toUTF16(kWidevineExperiment2KeySystemName),
keySystemConfigs);
DecryptionInfo::Hardware, keySystemConfigs);
}
#endif
return keySystemConfigs;
Expand Down
77 changes: 35 additions & 42 deletions dom/media/eme/mediafoundation/WMFCDMImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

namespace mozilla {

bool WMFCDMImpl::GetCapabilities(nsTArray<KeySystemConfig>& aOutConfigs) {
bool WMFCDMImpl::GetCapabilities(bool aIsHardwareDecryption,
nsTArray<KeySystemConfig>& aOutConfigs) {
MOZ_ASSERT(NS_IsMainThread());
if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
return false;
Expand All @@ -37,13 +38,14 @@ bool WMFCDMImpl::GetCapabilities(nsTArray<KeySystemConfig>& aOutConfigs) {
auto keySystem = std::string{NS_ConvertUTF16toUTF8(mKeySystem).get()};
if (auto rv = sKeySystemConfigs.find(keySystem);
rv != sKeySystemConfigs.end()) {
EME_LOG("Return cached capabilities for %s", keySystem.c_str());
for (const auto& config : rv->second) {
aOutConfigs.AppendElement(config);
EME_LOG("-- capabilities (%s)",
NS_ConvertUTF16toUTF8(config.GetDebugInfo()).get());
if (IsHardwareDecryptionSupported(config) == aIsHardwareDecryption) {
EME_LOG("Return cached capabilities for %s (%s)", keySystem.c_str(),
NS_ConvertUTF16toUTF8(config.GetDebugInfo()).get());
aOutConfigs.AppendElement(config);
return true;
}
}
return true;
}

// Not cached result, ask the remote process.
Expand All @@ -53,42 +55,33 @@ bool WMFCDMImpl::GetCapabilities(nsTArray<KeySystemConfig>& aOutConfigs) {
mCDM = MakeRefPtr<MFCDMChild>(mKeySystem);
}
bool ok = false;
static const bool sIsHwSecure[2] = {false, true};
for (const auto& isHWSecure : sIsHwSecure) {
media::Await(
do_AddRef(backgroundTaskQueue), mCDM->GetCapabilities(isHWSecure),
[&ok, &aOutConfigs, keySystem,
isHWSecure](const MFCDMCapabilitiesIPDL& capabilities) {
EME_LOG("capabilities: keySystem=%s (hw-secure=%d)",
keySystem.c_str(), isHWSecure);
for (const auto& v : capabilities.videoCapabilities()) {
EME_LOG("capabilities: video=%s",
NS_ConvertUTF16toUTF8(v.contentType()).get());
}
for (const auto& a : capabilities.audioCapabilities()) {
EME_LOG("capabilities: audio=%s",
NS_ConvertUTF16toUTF8(a.contentType()).get());
}
for (const auto& v : capabilities.encryptionSchemes()) {
EME_LOG("capabilities: encryptionScheme=%s",
EncryptionSchemeStr(v));
}
KeySystemConfig* config = aOutConfigs.AppendElement();
MFCDMCapabilitiesIPDLToKeySystemConfig(capabilities, *config);
sKeySystemConfigs[keySystem].AppendElement(*config);
// This is equal to "com.microsoft.playready.recommendation.3000", so
// we can store it directly without asking the remote process again.
if (keySystem.compare(kPlayReadyKeySystemName) == 0 && isHWSecure) {
config->mKeySystem.AssignLiteral(kPlayReadyKeySystemHardware);
sKeySystemConfigs["com.microsoft.playready.recommendation.3000"]
.AppendElement(*config);
}
ok = true;
},
[](nsresult rv) {
EME_LOG("Fail to get key system capabilities. rv=%x", uint32_t(rv));
});
}
media::Await(
do_AddRef(backgroundTaskQueue),
mCDM->GetCapabilities(aIsHardwareDecryption),
[&ok, &aOutConfigs, keySystem,
aIsHardwareDecryption](const MFCDMCapabilitiesIPDL& capabilities) {
EME_LOG("capabilities: keySystem=%s (hw-secure=%d)", keySystem.c_str(),
aIsHardwareDecryption);
for (const auto& v : capabilities.videoCapabilities()) {
EME_LOG("capabilities: video=%s",
NS_ConvertUTF16toUTF8(v.contentType()).get());
}
for (const auto& a : capabilities.audioCapabilities()) {
EME_LOG("capabilities: audio=%s",
NS_ConvertUTF16toUTF8(a.contentType()).get());
}
for (const auto& v : capabilities.encryptionSchemes()) {
EME_LOG("capabilities: encryptionScheme=%s", EncryptionSchemeStr(v));
}
KeySystemConfig* config = aOutConfigs.AppendElement();
MFCDMCapabilitiesIPDLToKeySystemConfig(capabilities, *config);
sKeySystemConfigs[keySystem].AppendElement(*config);
ok = true;
},
[](nsresult rv) {
EME_LOG("Fail to get key system capabilities. rv=%x", uint32_t(rv));
});

return ok;
}

Expand Down
3 changes: 2 additions & 1 deletion dom/media/eme/mediafoundation/WMFCDMImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class WMFCDMImpl final {
explicit WMFCDMImpl(const nsAString& aKeySystem) : mKeySystem(aKeySystem) {}

// TODO: make this async?
bool GetCapabilities(nsTArray<KeySystemConfig>& aOutConfigs);
bool GetCapabilities(bool aIsHardwareDecryption,
nsTArray<KeySystemConfig>& aOutConfigs);

using InitPromise = GenericPromise;
struct InitParams {
Expand Down

0 comments on commit 85f2dc6

Please sign in to comment.