summaryrefslogtreecommitdiffstats
path: root/dom/media/eme/mediafoundation/WMFCDMImpl.cpp
blob: 1fe42aa8e279f2185d51df03eca6dc6cf52d8ada (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
/* -*- 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 {

/* static */
bool WMFCDMImpl::Supports(const nsAString& aKeySystem) {
  MOZ_ASSERT(NS_IsMainThread());
  if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
    return false;
  }

  static std::map<nsString, bool> sSupports;
  static bool sSetRunOnShutdown = false;
  if (!sSetRunOnShutdown) {
    GetMainThreadSerialEventTarget()->Dispatch(
        NS_NewRunnableFunction("WMFCDMImpl::Supports", [&] {
          RunOnShutdown([&] { sSupports.clear(); },
                        ShutdownPhase::XPCOMShutdown);
        }));
    sSetRunOnShutdown = true;
  }

  nsString key(aKeySystem);
  if (const auto& s = sSupports.find(key); s != sSupports.end()) {
    return s->second;
  }

  RefPtr<WMFCDMImpl> cdm = MakeRefPtr<WMFCDMImpl>(aKeySystem);
  nsTArray<KeySystemConfig> configs;
  bool s = cdm->GetCapabilities(configs);
  sSupports[key] = s;
  return s;
}

bool WMFCDMImpl::GetCapabilities(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()) {
    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());
    }
    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;
  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));
        });
  }
  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