MediaDrmCDMProxy.h (6579B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 MediaDrmCDMProxy_h_ 8 #define MediaDrmCDMProxy_h_ 9 10 #include <jni.h> 11 12 #include "MediaCodec.h" 13 #include "mozilla/CDMCaps.h" 14 #include "mozilla/CDMProxy.h" 15 #include "mozilla/MediaDrmProxySupport.h" 16 #include "mozilla/UniquePtr.h" 17 #include "mozilla/dom/MediaKeySession.h" 18 #include "mozilla/dom/MediaKeys.h" 19 #include "mozilla/jni/Types.h" 20 #include "nsString.h" 21 22 namespace mozilla { 23 24 class MediaDrmCDMCallbackProxy; 25 class MediaDrmCDMProxy final : public CDMProxy { 26 public: 27 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDrmCDMProxy, override) 28 29 MediaDrmCDMProxy(dom::MediaKeys* aKeys, const nsAString& aKeySystem, 30 bool aDistinctiveIdentifierRequired, 31 bool aPersistentStateRequired); 32 33 void Init(PromiseId aPromiseId, const nsAString& aOrigin, 34 const nsAString& aTopLevelOrigin, 35 const nsAString& aGMPName) override; 36 37 void CreateSession(uint32_t aCreateSessionToken, 38 dom::MediaKeySessionType aSessionType, 39 PromiseId aPromiseId, const nsAString& aInitDataType, 40 nsTArray<uint8_t>& aInitData) override; 41 42 void LoadSession(PromiseId aPromiseId, dom::MediaKeySessionType aSessionType, 43 const nsAString& aSessionId) override; 44 45 void SetServerCertificate(PromiseId aPromiseId, 46 nsTArray<uint8_t>& aCert) override; 47 48 void UpdateSession(const nsAString& aSessionId, PromiseId aPromiseId, 49 nsTArray<uint8_t>& aResponse) override; 50 51 void CloseSession(const nsAString& aSessionId, PromiseId aPromiseId) override; 52 53 void RemoveSession(const nsAString& aSessionId, 54 PromiseId aPromiseId) override; 55 56 void QueryOutputProtectionStatus() override; 57 58 void NotifyOutputProtectionStatus( 59 OutputProtectionCheckStatus aCheckStatus, 60 OutputProtectionCaptureStatus aCaptureStatus) override; 61 62 void Shutdown() override; 63 64 void Terminated() override; 65 66 void OnSetSessionId(uint32_t aCreateSessionToken, 67 const nsAString& aSessionId) override; 68 69 void OnResolveLoadSessionPromise(uint32_t aPromiseId, bool aSuccess) override; 70 71 void OnSessionMessage(const nsAString& aSessionId, 72 dom::MediaKeyMessageType aMessageType, 73 const nsTArray<uint8_t>& aMessage) override; 74 75 void OnExpirationChange(const nsAString& aSessionId, 76 UnixTime aExpiryTime) override; 77 78 void OnSessionClosed(const nsAString& aSessionId, 79 dom::MediaKeySessionClosedReason aReason) override; 80 81 void OnSessionError(const nsAString& aSessionId, nsresult aException, 82 uint32_t aSystemCode, const nsAString& aMsg) override; 83 84 void OnRejectPromise(uint32_t aPromiseId, ErrorResult&& aException, 85 const nsCString& aMsg) override; 86 87 RefPtr<DecryptPromise> Decrypt(MediaRawData* aSample) override; 88 void OnDecrypted(uint32_t aId, DecryptStatus aResult, 89 const nsTArray<uint8_t>& aDecryptedData) override; 90 91 void RejectPromise(PromiseId aId, ErrorResult&& aException, 92 const nsCString& aReason) override; 93 // Reject promise with an InvalidStateError and the given message. 94 void RejectPromiseWithStateError(PromiseId aId, const nsCString& aReason); 95 96 // Resolves promise with "undefined". 97 // Can be called from any thread. 98 void ResolvePromise(PromiseId aId) override; 99 100 void OnKeyStatusesChange(const nsAString& aSessionId) override; 101 102 void GetStatusForPolicy(PromiseId aPromiseId, 103 const dom::HDCPVersion& aMinHdcpVersion) override; 104 105 #ifdef DEBUG 106 bool IsOnOwnerThread() override; 107 #endif 108 109 const nsString& GetMediaDrmStubId() const; 110 111 private: 112 virtual ~MediaDrmCDMProxy(); 113 114 void OnCDMCreated(uint32_t aPromiseId); 115 116 template <typename T> 117 void ResolvePromiseWithResult(PromiseId aId, const T& aResult); 118 119 struct CreateSessionData { 120 dom::MediaKeySessionType mSessionType; 121 uint32_t mCreateSessionToken; 122 PromiseId mPromiseId; 123 nsCString mInitDataType; 124 nsTArray<uint8_t> mInitData; 125 }; 126 127 struct UpdateSessionData { 128 PromiseId mPromiseId; 129 nsCString mSessionId; 130 nsTArray<uint8_t> mResponse; 131 }; 132 133 struct SessionOpData { 134 PromiseId mPromiseId; 135 nsCString mSessionId; 136 }; 137 138 class RejectPromiseTask : public Runnable { 139 public: 140 RejectPromiseTask(MediaDrmCDMProxy* aProxy, PromiseId aId, 141 ErrorResult&& aException, const nsCString& aReason) 142 : Runnable("RejectPromiseTask"), 143 mProxy(aProxy), 144 mId(aId), 145 mException(std::move(aException)), 146 mReason(aReason) {} 147 NS_IMETHOD Run() override { 148 // Moving into or out of a non-copyable ErrorResult will assert that both 149 // ErorResults are from our current thread. Avoid the assertion by moving 150 // into a current-thread CopyableErrorResult first. Note that this is 151 // safe, because CopyableErrorResult never holds state that can't move 152 // across threads. 153 CopyableErrorResult rv(std::move(mException)); 154 mProxy->RejectPromise(mId, std::move(rv), mReason); 155 return NS_OK; 156 } 157 158 private: 159 RefPtr<MediaDrmCDMProxy> mProxy; 160 PromiseId mId; 161 // We use a CopyableErrorResult here, because we're going to dispatch to a 162 // different thread and normal ErrorResult doesn't support that. 163 // CopyableErrorResult ensures that it only stores values that are safe to 164 // move across threads. 165 CopyableErrorResult mException; 166 nsCString mReason; 167 }; 168 169 nsCString mNodeId; 170 UniquePtr<MediaDrmProxySupport> mCDM; 171 bool mShutdownCalled; 172 173 // ===================================================================== 174 // For MediaDrmProxySupport 175 void md_Init(uint32_t aPromiseId); 176 void md_CreateSession(UniquePtr<CreateSessionData>&& aData); 177 void md_SetServerCertificate(PromiseId aPromiseId, 178 const nsTArray<uint8_t>& aCert); 179 void md_UpdateSession(UniquePtr<UpdateSessionData>&& aData); 180 void md_CloseSession(UniquePtr<SessionOpData>&& aData); 181 void md_Shutdown(); 182 // ===================================================================== 183 }; 184 185 } // namespace mozilla 186 187 #endif // MediaDrmCDMProxy_h_