WMFCDMImpl.h (4906B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef DOM_MEDIA_EME_MEDIAFOUNDATION_WMFCDMIMPL_H_ 8 #define DOM_MEDIA_EME_MEDIAFOUNDATION_WMFCDMIMPL_H_ 9 10 #include "MediaData.h" 11 #include "mozilla/Assertions.h" 12 #include "mozilla/EMEUtils.h" 13 #include "mozilla/KeySystemConfig.h" 14 #include "mozilla/MFCDMChild.h" 15 #include "mozilla/media/MediaUtils.h" 16 #include "nsString.h" 17 #include "nsThreadUtils.h" 18 19 namespace mozilla { 20 21 class WMFCDMProxyCallback; 22 23 /** 24 * WMFCDMImpl is a helper class for MFCDM protocol clients. It creates, manages, 25 * and calls MFCDMChild object in the content process on behalf of the client, 26 * and performs conversion between EME and MFCDM types and constants. This class 27 * can be used in two ways (1) call Supports/GetCapabilities to know the 28 * information about given key system or config (2) do session-related 29 * operations. In this case, Init() MUST be called first. 30 */ 31 class WMFCDMImpl final { 32 public: 33 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WMFCDMImpl); 34 35 explicit WMFCDMImpl(const nsAString& aKeySystem) : mKeySystem(aKeySystem) {} 36 37 using InitPromise = GenericPromise; 38 struct InitParams { 39 nsString mOrigin; 40 CopyableTArray<nsString> mInitDataTypes; 41 bool mPersistentStateRequired; 42 bool mDistinctiveIdentifierRequired; 43 WMFCDMProxyCallback* mProxyCallback; 44 CopyableTArray<MFCDMMediaCapability> mAudioCapabilities; 45 CopyableTArray<MFCDMMediaCapability> mVideoCapabilities; 46 }; 47 48 RefPtr<InitPromise> Init(const InitParams& aParams); 49 50 // Following functions MUST be called after calling Init(). 51 RefPtr<MFCDMChild::SessionPromise> CreateSession( 52 uint32_t aPromiseId, const KeySystemConfig::SessionType aSessionType, 53 const nsAString& aInitDataType, const nsTArray<uint8_t>& aInitData) { 54 MOZ_DIAGNOSTIC_ASSERT(mCDM); 55 return mCDM->CreateSessionAndGenerateRequest(aPromiseId, aSessionType, 56 aInitDataType, aInitData); 57 } 58 59 RefPtr<GenericPromise> LoadSession( 60 uint32_t aPromiseId, const KeySystemConfig::SessionType aSessionType, 61 const nsAString& aSessionId) { 62 MOZ_DIAGNOSTIC_ASSERT(mCDM); 63 return mCDM->LoadSession(aPromiseId, aSessionType, aSessionId); 64 } 65 66 RefPtr<GenericPromise> UpdateSession(uint32_t aPromiseId, 67 const nsAString& aSessionId, 68 nsTArray<uint8_t>& aResponse) { 69 MOZ_DIAGNOSTIC_ASSERT(mCDM); 70 return mCDM->UpdateSession(aPromiseId, aSessionId, aResponse); 71 } 72 73 RefPtr<GenericPromise> CloseSession(uint32_t aPromiseId, 74 const nsAString& aSessionId) { 75 MOZ_DIAGNOSTIC_ASSERT(mCDM); 76 return mCDM->CloseSession(aPromiseId, aSessionId); 77 } 78 79 RefPtr<GenericPromise> RemoveSession(uint32_t aPromiseId, 80 const nsAString& aSessionId) { 81 MOZ_DIAGNOSTIC_ASSERT(mCDM); 82 return mCDM->RemoveSession(aPromiseId, aSessionId); 83 } 84 85 RefPtr<GenericPromise> SetServerCertificate(uint32_t aPromiseId, 86 nsTArray<uint8_t>& aCert) { 87 MOZ_DIAGNOSTIC_ASSERT(mCDM); 88 return mCDM->SetServerCertificate(aPromiseId, aCert); 89 } 90 91 RefPtr<GenericPromise> GetStatusForPolicy( 92 uint32_t aPromiseId, const dom::HDCPVersion& aMinHdcpVersion) { 93 MOZ_DIAGNOSTIC_ASSERT(mCDM); 94 return mCDM->GetStatusForPolicy(aPromiseId, aMinHdcpVersion); 95 } 96 97 uint64_t Id() { 98 MOZ_DIAGNOSTIC_ASSERT(mCDM, 99 "Should be called only after Init() is resolved"); 100 MOZ_DIAGNOSTIC_ASSERT(mCDM->Id() != 0, 101 "Should be called only after Init() is resolved"); 102 return mCDM->Id(); 103 } 104 105 private: 106 ~WMFCDMImpl() { 107 if (mCDM) { 108 mCDM->Shutdown(); 109 } 110 }; 111 112 const nsString mKeySystem; 113 RefPtr<MFCDMChild> mCDM; 114 115 MozPromiseHolder<InitPromise> mInitPromiseHolder; 116 }; 117 118 // A helper class to get multiple capabilities from different key systems. 119 class WMFCDMCapabilites final { 120 public: 121 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WMFCDMCapabilites); 122 WMFCDMCapabilites() = default; 123 124 using SupportedConfigsPromise = KeySystemConfig::SupportedConfigsPromise; 125 RefPtr<SupportedConfigsPromise> GetCapabilities( 126 const nsTArray<KeySystemConfigRequest>& aRequests); 127 128 private: 129 ~WMFCDMCapabilites(); 130 131 nsTArray<RefPtr<MFCDMChild>> mCDMs; 132 MozPromiseHolder<SupportedConfigsPromise> mCapabilitiesPromiseHolder; 133 MozPromiseRequestHolder< 134 MFCDMChild::CapabilitiesPromise::AllSettledPromiseType> 135 mCapabilitiesPromisesRequest; 136 }; 137 138 } // namespace mozilla 139 140 #endif // DOM_MEDIA_EME_MEDIAFOUNDATION_WMFCDMIMPL_H_