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

#ifndef DOM_MEDIA_EME_MEDIAFOUNDATION_WMFCDMIMPL_H_
#define DOM_MEDIA_EME_MEDIAFOUNDATION_WMFCDMIMPL_H_

#include "MediaData.h"
#include "mozilla/Assertions.h"
#include "mozilla/EMEUtils.h"
#include "mozilla/KeySystemConfig.h"
#include "mozilla/media/MediaUtils.h"
#include "mozilla/MFCDMChild.h"
#include "nsString.h"
#include "nsThreadUtils.h"

namespace mozilla {

class WMFCDMProxyCallback;

/**
 * WMFCDMImpl is a helper class for MFCDM protocol clients. It creates, manages,
 * and calls MFCDMChild object in the content process on behalf of the client,
 * and performs conversion between EME and MFCDM types and constants. This class
 * can be used in two ways (1) call Supports/GetCapabilities to know the
 * information about given key system or config (2) do session-related
 * operations. In this case, Init() MUST be called first.
 */
class WMFCDMImpl final {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WMFCDMImpl);

  explicit WMFCDMImpl(const nsAString& aKeySystem) : mKeySystem(aKeySystem) {}

  using InitPromise = GenericPromise;
  struct InitParams {
    nsString mOrigin;
    CopyableTArray<nsString> mInitDataTypes;
    bool mPersistentStateRequired;
    bool mDistinctiveIdentifierRequired;
    WMFCDMProxyCallback* mProxyCallback;
    CopyableTArray<MFCDMMediaCapability> mAudioCapabilities;
    CopyableTArray<MFCDMMediaCapability> mVideoCapabilities;
  };

  RefPtr<InitPromise> Init(const InitParams& aParams);

  // Following functions MUST be called after calling Init().
  RefPtr<MFCDMChild::SessionPromise> CreateSession(
      uint32_t aPromiseId, const KeySystemConfig::SessionType aSessionType,
      const nsAString& aInitDataType, const nsTArray<uint8_t>& aInitData) {
    MOZ_DIAGNOSTIC_ASSERT(mCDM);
    return mCDM->CreateSessionAndGenerateRequest(aPromiseId, aSessionType,
                                                 aInitDataType, aInitData);
  }

  RefPtr<GenericPromise> LoadSession(
      uint32_t aPromiseId, const KeySystemConfig::SessionType aSessionType,
      const nsAString& aSessionId) {
    MOZ_DIAGNOSTIC_ASSERT(mCDM);
    return mCDM->LoadSession(aPromiseId, aSessionType, aSessionId);
  }

  RefPtr<GenericPromise> UpdateSession(uint32_t aPromiseId,
                                       const nsAString& aSessionId,
                                       nsTArray<uint8_t>& aResponse) {
    MOZ_DIAGNOSTIC_ASSERT(mCDM);
    return mCDM->UpdateSession(aPromiseId, aSessionId, aResponse);
  }

  RefPtr<GenericPromise> CloseSession(uint32_t aPromiseId,
                                      const nsAString& aSessionId) {
    MOZ_DIAGNOSTIC_ASSERT(mCDM);
    return mCDM->CloseSession(aPromiseId, aSessionId);
  }

  RefPtr<GenericPromise> RemoveSession(uint32_t aPromiseId,
                                       const nsAString& aSessionId) {
    MOZ_DIAGNOSTIC_ASSERT(mCDM);
    return mCDM->RemoveSession(aPromiseId, aSessionId);
  }

  RefPtr<GenericPromise> SetServerCertificate(uint32_t aPromiseId,
                                              nsTArray<uint8_t>& aCert) {
    MOZ_DIAGNOSTIC_ASSERT(mCDM);
    return mCDM->SetServerCertificate(aPromiseId, aCert);
  }

  RefPtr<GenericPromise> GetStatusForPolicy(
      uint32_t aPromiseId, const dom::HDCPVersion& aMinHdcpVersion) {
    MOZ_DIAGNOSTIC_ASSERT(mCDM);
    return mCDM->GetStatusForPolicy(aPromiseId, aMinHdcpVersion);
  }

  uint64_t Id() {
    MOZ_DIAGNOSTIC_ASSERT(mCDM,
                          "Should be called only after Init() is resolved");
    MOZ_DIAGNOSTIC_ASSERT(mCDM->Id() != 0,
                          "Should be called only after Init() is resolved");
    return mCDM->Id();
  }

 private:
  ~WMFCDMImpl() {
    if (mCDM) {
      mCDM->Shutdown();
    }
  };

  const nsString mKeySystem;
  RefPtr<MFCDMChild> mCDM;

  MozPromiseHolder<InitPromise> mInitPromiseHolder;
};

// A helper class to get multiple capabilities from different key systems.
class WMFCDMCapabilites final {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WMFCDMCapabilites);
  WMFCDMCapabilites() = default;

  using SupportedConfigsPromise = KeySystemConfig::SupportedConfigsPromise;
  RefPtr<SupportedConfigsPromise> GetCapabilities(
      const nsTArray<KeySystemConfigRequest>& aRequests);

 private:
  ~WMFCDMCapabilites();

  nsTArray<RefPtr<MFCDMChild>> mCDMs;
  MozPromiseHolder<SupportedConfigsPromise> mCapabilitiesPromiseHolder;
  MozPromiseRequestHolder<
      MFCDMChild::CapabilitiesPromise::AllSettledPromiseType>
      mCapabilitiesPromisesRequest;
};

}  // namespace mozilla

#endif  // DOM_MEDIA_EME_MEDIAFOUNDATION_WMFCDMIMPL_H_