AppleUtils.h (4463B)
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/. */ 4 5 // Utility functions to help with Apple API calls. 6 7 #ifndef mozilla_AppleUtils_h 8 #define mozilla_AppleUtils_h 9 10 #include <CoreFoundation/CFBase.h> // For CFRelease() 11 #include <CoreVideo/CVBuffer.h> // For CVBufferRelease() 12 #include <VideoToolbox/VideoToolbox.h> // For VTCompressionSessionRef 13 14 #include "mozilla/Assertions.h" 15 #include "mozilla/Attributes.h" 16 17 #if TARGET_OS_IPHONE 18 inline bool OSSupportsSVC() { 19 // TODO 20 return false; 21 } 22 #else 23 # include "nsCocoaFeatures.h" 24 inline bool OSSupportsSVC() { 25 return nsCocoaFeatures::IsAtLeastVersion(11, 3, 0); 26 } 27 #endif 28 29 namespace mozilla { 30 31 template <typename T> 32 struct AutoTypeRefTraits; 33 34 enum class AutoTypePolicy { Retain, NoRetain }; 35 template <typename T, typename Traits = AutoTypeRefTraits<T>> 36 class AutoTypeRef { 37 public: 38 explicit AutoTypeRef(T aObj = Traits::InvalidValue(), 39 AutoTypePolicy aPolicy = AutoTypePolicy::NoRetain) 40 : mObj(aObj) { 41 if (mObj != Traits::InvalidValue()) { 42 if (aPolicy == AutoTypePolicy::Retain) { 43 mObj = Traits::Retain(mObj); 44 } 45 } 46 } 47 48 ~AutoTypeRef() { ReleaseIfNeeded(); } 49 50 // Copy constructor 51 AutoTypeRef(const AutoTypeRef<T, Traits>& aOther) : mObj(aOther.mObj) { 52 if (mObj != Traits::InvalidValue()) { 53 mObj = Traits::Retain(mObj); 54 } 55 } 56 57 // Copy assignment 58 AutoTypeRef<T, Traits>& operator=(const AutoTypeRef<T, Traits>& aOther) { 59 ReleaseIfNeeded(); 60 mObj = aOther.mObj; 61 if (mObj != Traits::InvalidValue()) { 62 mObj = Traits::Retain(mObj); 63 } 64 return *this; 65 } 66 67 // Move constructor 68 AutoTypeRef(AutoTypeRef<T, Traits>&& aOther) : mObj(aOther.Take()) {} 69 70 // Move assignment 71 AutoTypeRef<T, Traits>& operator=(const AutoTypeRef<T, Traits>&& aOther) { 72 Reset(aOther.Take(), AutoTypePolicy::NoRetain); 73 return *this; 74 } 75 76 explicit operator bool() const { return mObj != Traits::InvalidValue(); } 77 78 operator T() { return mObj; } 79 80 T& Ref() { return mObj; } 81 82 T* Receive() { 83 MOZ_ASSERT(mObj == Traits::InvalidValue(), 84 "Receive() should only be called for uninitialized objects"); 85 return &mObj; 86 } 87 88 void Reset(T aObj = Traits::InvalidValue(), 89 AutoTypePolicy aPolicy = AutoTypePolicy::NoRetain) { 90 ReleaseIfNeeded(); 91 mObj = aObj; 92 if (mObj != Traits::InvalidValue()) { 93 if (aPolicy == AutoTypePolicy::Retain) { 94 mObj = Traits::Retain(mObj); 95 } else { 96 mObj = aObj; 97 } 98 } 99 } 100 101 private: 102 T Take() { 103 T obj = mObj; 104 mObj = Traits::InvalidValue(); 105 return obj; 106 } 107 108 void ReleaseIfNeeded() { 109 if (mObj != Traits::InvalidValue()) { 110 Traits::Release(mObj); 111 mObj = Traits::InvalidValue(); 112 } 113 } 114 T mObj; 115 }; 116 117 template <typename CFT> 118 struct CFTypeRefTraits { 119 static CFT InvalidValue() { return nullptr; } 120 static CFT Retain(CFT aObject) { 121 CFRetain(aObject); 122 return aObject; 123 } 124 static void Release(CFT aObject) { CFRelease(aObject); } 125 }; 126 127 template <typename CVB> 128 struct CVBufferRefTraits { 129 static CVB InvalidValue() { return nullptr; } 130 static CVB Retain(CVB aObject) { 131 CVBufferRetain(aObject); 132 return aObject; 133 } 134 static void Release(CVB aObject) { CVBufferRelease(aObject); } 135 }; 136 137 template <typename CFT> 138 using AutoCFTypeRef = AutoTypeRef<CFT, CFTypeRefTraits<CFT>>; 139 template <typename CVB> 140 using AutoCVBufferRef = AutoTypeRef<CVB, CVBufferRefTraits<CVB>>; 141 142 class MOZ_RAII SessionPropertyManager { 143 public: 144 explicit SessionPropertyManager( 145 const AutoCFTypeRef<VTCompressionSessionRef>& aSession); 146 explicit SessionPropertyManager(const VTCompressionSessionRef& aSession); 147 ~SessionPropertyManager() = default; 148 149 bool IsSupported(CFStringRef aKey); 150 151 OSStatus Set(CFStringRef aKey, int32_t aValue); 152 OSStatus Set(CFStringRef aKey, int64_t aValue); 153 OSStatus Set(CFStringRef aKey, float aValue); 154 OSStatus Set(CFStringRef aKey, bool aValue); 155 OSStatus Set(CFStringRef aKey, CFStringRef value); 156 OSStatus Copy(CFStringRef aKey, bool& aValue); 157 158 private: 159 template <typename V> 160 OSStatus Set(CFStringRef aKey, V aValue, CFNumberType aType); 161 162 AutoCFTypeRef<VTCompressionSessionRef> mSession; 163 AutoCFTypeRef<CFDictionaryRef> mSupportedKeys; 164 }; 165 166 } // namespace mozilla 167 168 #endif // mozilla_AppleUtils_h