diff options
Diffstat (limited to 'dom/media/eme/mediafoundation/WMFCDMImpl.cpp')
-rw-r--r-- | dom/media/eme/mediafoundation/WMFCDMImpl.cpp | 155 |
1 files changed, 85 insertions, 70 deletions
diff --git a/dom/media/eme/mediafoundation/WMFCDMImpl.cpp b/dom/media/eme/mediafoundation/WMFCDMImpl.cpp index add978f755..983a7c00f2 100644 --- a/dom/media/eme/mediafoundation/WMFCDMImpl.cpp +++ b/dom/media/eme/mediafoundation/WMFCDMImpl.cpp @@ -10,81 +10,12 @@ #include "mozilla/AppShutdown.h" #include "mozilla/ClearOnShutdown.h" +#include "mozilla/ScopeExit.h" #include "mozilla/dom/MediaKeySession.h" #include "mozilla/dom/KeySystemNames.h" namespace mozilla { -bool WMFCDMImpl::GetCapabilities(bool aIsHardwareDecryption, - nsTArray<KeySystemConfig>& aOutConfigs) { - MOZ_ASSERT(NS_IsMainThread()); - if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) { - return false; - } - - static std::unordered_map<std::string, nsTArray<KeySystemConfig>> - sKeySystemConfigs{}; - static bool sSetRunOnShutdown = false; - if (!sSetRunOnShutdown) { - GetMainThreadSerialEventTarget()->Dispatch( - NS_NewRunnableFunction("WMFCDMImpl::GetCapabilities", [&] { - RunOnShutdown([&] { sKeySystemConfigs.clear(); }, - ShutdownPhase::XPCOMShutdown); - })); - sSetRunOnShutdown = true; - } - - // Retrieve result from our cached key system - auto keySystem = std::string{NS_ConvertUTF16toUTF8(mKeySystem).get()}; - if (auto rv = sKeySystemConfigs.find(keySystem); - rv != sKeySystemConfigs.end()) { - for (const auto& config : rv->second) { - 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; - } - } - } - - // Not cached result, ask the remote process. - nsCOMPtr<nsISerialEventTarget> backgroundTaskQueue; - NS_CreateBackgroundTaskQueue(__func__, getter_AddRefs(backgroundTaskQueue)); - if (!mCDM) { - mCDM = MakeRefPtr<MFCDMChild>(mKeySystem); - } - bool ok = false; - 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; -} - RefPtr<WMFCDMImpl::InitPromise> WMFCDMImpl::Init( const WMFCDMImpl::InitParams& aParams) { if (!mCDM) { @@ -111,4 +42,88 @@ RefPtr<WMFCDMImpl::InitPromise> WMFCDMImpl::Init( return mInitPromiseHolder.Ensure(__func__); } +RefPtr<KeySystemConfig::SupportedConfigsPromise> +WMFCDMCapabilites::GetCapabilities( + const nsTArray<KeySystemConfigRequest>& aRequests) { + MOZ_ASSERT(NS_IsMainThread()); + if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) { + return SupportedConfigsPromise::CreateAndReject(false, __func__); + } + + if (!mCapabilitiesPromiseHolder.IsEmpty()) { + return mCapabilitiesPromiseHolder.Ensure(__func__); + } + + using CapabilitiesPromise = MFCDMChild::CapabilitiesPromise; + nsTArray<RefPtr<CapabilitiesPromise>> promises; + for (const auto& request : aRequests) { + RefPtr<MFCDMChild> cdm = new MFCDMChild(request.mKeySystem); + promises.AppendElement(cdm->GetCapabilities(MFCDMCapabilitiesRequest{ + nsString{request.mKeySystem}, + request.mDecryption == KeySystemConfig::DecryptionInfo::Hardware})); + mCDMs.AppendElement(std::move(cdm)); + } + + CapabilitiesPromise::AllSettled(GetCurrentSerialEventTarget(), promises) + ->Then( + GetMainThreadSerialEventTarget(), __func__, + [self = RefPtr<WMFCDMCapabilites>(this), this]( + CapabilitiesPromise::AllSettledPromiseType::ResolveOrRejectValue&& + aResult) { + mCapabilitiesPromisesRequest.Complete(); + + // Reset cdm + auto exit = MakeScopeExit([&] { + for (auto& cdm : mCDMs) { + cdm->Shutdown(); + } + mCDMs.Clear(); + }); + + nsTArray<KeySystemConfig> outConfigs; + for (const auto& promiseRv : aResult.ResolveValue()) { + if (promiseRv.IsReject()) { + continue; + } + const MFCDMCapabilitiesIPDL& capabilities = + promiseRv.ResolveValue(); + EME_LOG("capabilities: keySystem=%s (hw-secure=%d)", + NS_ConvertUTF16toUTF8(capabilities.keySystem()).get(), + capabilities.isHardwareDecryption()); + 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 = outConfigs.AppendElement(); + MFCDMCapabilitiesIPDLToKeySystemConfig(capabilities, *config); + } + if (outConfigs.IsEmpty()) { + EME_LOG( + "Failed on getting capabilities from all settled promise"); + mCapabilitiesPromiseHolder.Reject(false, __func__); + return; + } + mCapabilitiesPromiseHolder.Resolve(std::move(outConfigs), __func__); + }) + ->Track(mCapabilitiesPromisesRequest); + + return mCapabilitiesPromiseHolder.Ensure(__func__); +} + +WMFCDMCapabilites::~WMFCDMCapabilites() { + mCapabilitiesPromisesRequest.DisconnectIfExists(); + mCapabilitiesPromiseHolder.RejectIfExists(false, __func__); + for (auto& cdm : mCDMs) { + cdm->Shutdown(); + } +} + } // namespace mozilla |