Bug 1925181 - Properly set small alloc randomization on Android content processes...
[gecko.git] / media / wmf-clearkey / WMFClearKeyUtils.h
blobe834e6cb9ae25ed89a375b27fa28c6011bb488da
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef DOM_MEDIA_PLATFORM_WMF_CLEARKEY_WMFCLEARKEYCDMUTILS_H
6 #define DOM_MEDIA_PLATFORM_WMF_CLEARKEY_WMFCLEARKEYCDMUTILS_H
8 #include <initguid.h>
9 #include <memory>
10 #include <mfidl.h>
11 #include <mutex>
12 #include <thread>
13 #include <windows.h>
14 #include <wrl.h>
15 #include <sstream>
16 #include <stdio.h>
18 #include "MFCDMExtra.h"
19 #include "mozilla/Assertions.h"
20 #include "mozilla/Unused.h"
22 namespace mozilla {
24 inline constexpr WCHAR kCLEARKEY_SYSTEM_NAME[] = L"org.w3.clearkey";
26 #define WMF_CLEARKEY_DEBUG 0
28 #ifdef WMF_CLEARKEY_DEBUG
29 # define LOG(msg, ...) \
30 printf(("[Thread %lu]: D/WMFClearKey " msg "\n"), \
31 static_cast<unsigned long>( \
32 std::hash<std::thread::id>{}(std::this_thread::get_id())), \
33 ##__VA_ARGS__)
34 #else
35 # define LOG(msg, ...)
36 #endif
38 #define PRETTY_FUNC \
39 ([&]() -> std::string { \
40 std::string prettyFunction(__PRETTY_FUNCTION__); \
41 std::size_t pos1 = prettyFunction.find("::"); \
42 std::size_t pos2 = prettyFunction.find("(", pos1); \
43 return prettyFunction.substr(0, pos2); \
44 })() \
45 .c_str()
47 // We can't reuse the definition in the `MediaEngineUtils.h` due to the
48 // restriction of not being able to use XPCOM string in the external library.
49 #ifndef RETURN_IF_FAILED
50 # define RETURN_IF_FAILED(x) \
51 do { \
52 HRESULT rv = x; \
53 if (FAILED(rv)) { \
54 LOG("(" #x ") failed, rv=%lx", rv); \
55 return rv; \
56 } \
57 } while (false)
58 #endif
60 #ifndef NOT_IMPLEMENTED
61 # define NOT_IMPLEMENTED() \
62 do { \
63 LOG("WARNING : '%s' NOT IMPLEMENTED!", PRETTY_FUNC); \
64 } while (0)
65 #endif
67 #ifndef ENTRY_LOG
68 # define ENTRY_LOG(...) \
69 do { \
70 LOG("%s [%p]", PRETTY_FUNC, this); \
71 } while (0)
72 # define ENTRY_LOG_ARGS(fmt, ...) \
73 do { \
74 LOG("%s [%p]: " fmt, PRETTY_FUNC, this, ##__VA_ARGS__); \
75 (void)(0, ##__VA_ARGS__); \
76 } while (0)
77 #endif
79 // TODO : should we use Microsoft's definition or Chromium's defintion?
81 // This is defined in Microsoft's sample code
82 // https://github.com/microsoft/media-foundation/blob/dc81175a3e893c7c58fcbf1a943ac342e39f172c/samples/storecdm/clearkeyStoreCDM/clearkeydll/guids.h#L17-L18C14
83 // DEFINE_GUID(CLEARKEY_GUID_CLEARKEY_PROTECTION_SYSTEM_ID, 0x9b0ff3ca, 0x1378,
84 // 0x4f28, 0x96, 0x65, 0x18, 0x9e, 0x15, 0x30, 0x2a, 0x71);
86 // Media Foundation Clear Key protection system ID from Chromium
87 // {E4E94971-696A-447E-96E4-93FDF3A57A7A}
88 // https://source.chromium.org/chromium/chromium/src/+/main:media/cdm/win/test/media_foundation_clear_key_guids.h;l=16-29?q=CLEARKEY_GUID_CLEARKEY_PROTECTION_SYSTEM_ID&ss=chromium%2Fchromium%2Fsrc
89 DEFINE_GUID(CLEARKEY_GUID_CLEARKEY_PROTECTION_SYSTEM_ID, 0xe4e94971, 0x696a,
90 0x447e, 0x96, 0xe4, 0x93, 0xfd, 0xf3, 0xa5, 0x7a, 0x7a);
92 // Media Foundation Clear Key content enabler type
93 // {C262FD73-2F13-41C2-94E7-4CAF087AE1D1}
94 DEFINE_GUID(MEDIA_FOUNDATION_CLEARKEY_GUID_CONTENT_ENABLER_TYPE, 0xc262fd73,
95 0x2f13, 0x41c2, 0x94, 0xe7, 0x4c, 0xaf, 0x8, 0x7a, 0xe1, 0xd1);
97 // https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:media/cdm/win/test/media_foundation_clear_key_guids.h;l=46;drc=fcefb4563e8ea5b2036ecde882f4f228dd1e4338
98 #define PLAYREADY_GUID_MEDIA_PROTECTION_SYSTEM_ID_STRING \
99 L"{F4637010-03C3-42CD-B932-B48ADF3A6A54}"
101 // A PROPVARIANT that is automatically initialized and cleared upon respective
102 // construction and destruction of this class.
103 class AutoPropVar final {
104 public:
105 AutoPropVar() { PropVariantInit(&mVar); }
107 ~AutoPropVar() { Reset(); }
109 AutoPropVar(const AutoPropVar&) = delete;
110 AutoPropVar& operator=(const AutoPropVar&) = delete;
111 bool operator==(const AutoPropVar&) const = delete;
112 bool operator!=(const AutoPropVar&) const = delete;
114 // Returns a pointer to the underlying PROPVARIANT for use as an out param
115 // in a function call.
116 PROPVARIANT* Receive() {
117 MOZ_ASSERT(mVar.vt == VT_EMPTY);
118 return &mVar;
121 // Clears the instance to prepare it for re-use (e.g., via Receive).
122 void Reset() {
123 if (mVar.vt != VT_EMPTY) {
124 HRESULT hr = PropVariantClear(&mVar);
125 MOZ_ASSERT(SUCCEEDED(hr));
126 Unused << hr;
130 const PROPVARIANT& get() const { return mVar; }
131 const PROPVARIANT* ptr() const { return &mVar; }
133 private:
134 PROPVARIANT mVar;
137 template <typename T>
138 class ScopedCoMem final {
139 public:
140 ScopedCoMem() : mPtr(nullptr) {}
142 ~ScopedCoMem() { Reset(nullptr); }
144 ScopedCoMem(const ScopedCoMem&) = delete;
145 ScopedCoMem& operator=(const ScopedCoMem&) = delete;
147 T** operator&() { // NOLINT
148 MOZ_ASSERT(mPtr == nullptr); // To catch memory leaks.
149 return &mPtr;
152 operator T*() { return mPtr; }
154 T* operator->() {
155 MOZ_ASSERT(mPtr != nullptr);
156 return mPtr;
159 const T* operator->() const {
160 MOZ_ASSERT(mPtr != nullptr);
161 return mPtr;
164 explicit operator bool() const { return mPtr; }
166 friend bool operator==(const ScopedCoMem& lhs, std::nullptr_t) {
167 return lhs.Get() == nullptr;
170 friend bool operator==(std::nullptr_t, const ScopedCoMem& rhs) {
171 return rhs.Get() == nullptr;
174 friend bool operator!=(const ScopedCoMem& lhs, std::nullptr_t) {
175 return lhs.Get() != nullptr;
178 friend bool operator!=(std::nullptr_t, const ScopedCoMem& rhs) {
179 return rhs.Get() != nullptr;
182 void Reset(T* ptr) {
183 if (mPtr) {
184 CoTaskMemFree(mPtr);
186 mPtr = ptr;
189 T* Get() const { return mPtr; }
191 private:
192 T* mPtr;
195 template <typename T>
196 class DataMutex final {
197 public:
198 DataMutex() = default;
200 template <typename... Args>
201 explicit DataMutex(Args&&... args) : mData(std::forward<Args>(args)...) {}
203 class AutoLock {
204 public:
205 AutoLock(std::unique_lock<std::mutex>&& lock, DataMutex<T>* mutex)
206 : mLock(std::move(lock)), mMutex(mutex) {}
207 T& operator*() { return ref(); }
208 T* operator->() { return &ref(); }
209 T& ref() const& { return mMutex->mData; }
210 operator T*() const& { return &ref(); }
211 ~AutoLock() { mMutex = nullptr; }
213 private:
214 std::unique_lock<std::mutex> mLock;
215 DataMutex<T>* mMutex;
218 AutoLock Lock() {
219 return AutoLock(std::unique_lock<std::mutex>(mMutex), this);
222 private:
223 std::mutex mMutex;
224 T mData;
227 } // namespace mozilla
229 #endif // DOM_MEDIA_PLATFORM_WMF_CLEARKEY_WMFCLEARKEYCDMUTILS_H