summaryrefslogtreecommitdiffstats
path: root/dom/media/eme/mediafoundation/WMFCDMImpl.cpp
blob: add978f755386c5aba21fd65ea983c0aa2bc473f (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
/* -*- 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 "WMFCDMImpl.h"

#include <unordered_map>

#include "mozilla/AppShutdown.h"
#include "mozilla/ClearOnShutdown.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) {
    mCDM = MakeRefPtr<MFCDMChild>(mKeySystem);
  }
  RefPtr<WMFCDMImpl> self = this;
  mCDM->Init(aParams.mOrigin, aParams.mInitDataTypes,
             aParams.mPersistentStateRequired
                 ? KeySystemConfig::Requirement::Required
                 : KeySystemConfig::Requirement::Optional,
             aParams.mDistinctiveIdentifierRequired
                 ? KeySystemConfig::Requirement::Required
                 : KeySystemConfig::Requirement::Optional,
             aParams.mAudioCapabilities, aParams.mVideoCapabilities,
             aParams.mProxyCallback)
      ->Then(
          mCDM->ManagerThread(), __func__,
          [self, this](const MFCDMInitIPDL& init) {
            mInitPromiseHolder.ResolveIfExists(true, __func__);
          },
          [self, this](const nsresult rv) {
            mInitPromiseHolder.RejectIfExists(rv, __func__);
          });
  return mInitPromiseHolder.Ensure(__func__);
}

}  // namespace mozilla