| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/mac/videotoolbox_glue.h" | |
| 6 | |
| 7 #include <dlfcn.h> | |
| 8 #import <Foundation/Foundation.h> | |
| 9 | |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/macros.h" | |
| 12 | |
| 13 // This class stores VideoToolbox library symbol pointers. | |
| 14 struct VideoToolboxGlue::Library { | |
| 15 typedef OSStatus (*VTCompressionSessionCreateMethod)( | |
| 16 CFAllocatorRef, | |
| 17 int32_t, | |
| 18 int32_t, | |
| 19 CoreMediaGlue::CMVideoCodecType, | |
| 20 CFDictionaryRef, | |
| 21 CFDictionaryRef, | |
| 22 CFAllocatorRef, | |
| 23 VTCompressionOutputCallback, | |
| 24 void*, | |
| 25 VTCompressionSessionRef*); | |
| 26 typedef OSStatus (*VTCompressionSessionEncodeFrameMethod)( | |
| 27 VTCompressionSessionRef, | |
| 28 CVImageBufferRef, | |
| 29 CoreMediaGlue::CMTime, | |
| 30 CoreMediaGlue::CMTime, | |
| 31 CFDictionaryRef, | |
| 32 void*, | |
| 33 VTEncodeInfoFlags*); | |
| 34 typedef CVPixelBufferPoolRef (*VTCompressionSessionGetPixelBufferPoolMethod)( | |
| 35 VTCompressionSessionRef); | |
| 36 typedef void (*VTCompressionSessionInvalidateMethod)(VTCompressionSessionRef); | |
| 37 typedef OSStatus (*VTCompressionSessionCompleteFramesMethod)( | |
| 38 VTCompressionSessionRef, | |
| 39 CoreMediaGlue::CMTime); | |
| 40 typedef OSStatus (*VTSessionSetPropertyMethod)(VTSessionRef, | |
| 41 CFStringRef, | |
| 42 CFTypeRef); | |
| 43 | |
| 44 VTCompressionSessionCreateMethod VTCompressionSessionCreate; | |
| 45 VTCompressionSessionEncodeFrameMethod VTCompressionSessionEncodeFrame; | |
| 46 VTCompressionSessionGetPixelBufferPoolMethod | |
| 47 VTCompressionSessionGetPixelBufferPool; | |
| 48 VTCompressionSessionInvalidateMethod VTCompressionSessionInvalidate; | |
| 49 VTCompressionSessionCompleteFramesMethod VTCompressionSessionCompleteFrames; | |
| 50 VTSessionSetPropertyMethod VTSessionSetProperty; | |
| 51 | |
| 52 CFStringRef* kVTCompressionPropertyKey_AllowFrameReordering; | |
| 53 CFStringRef* kVTCompressionPropertyKey_AverageBitRate; | |
| 54 CFStringRef* kVTCompressionPropertyKey_ColorPrimaries; | |
| 55 CFStringRef* kVTCompressionPropertyKey_DataRateLimits; | |
| 56 CFStringRef* kVTCompressionPropertyKey_ExpectedFrameRate; | |
| 57 CFStringRef* kVTCompressionPropertyKey_MaxFrameDelayCount; | |
| 58 CFStringRef* kVTCompressionPropertyKey_MaxKeyFrameInterval; | |
| 59 CFStringRef* kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration; | |
| 60 CFStringRef* kVTCompressionPropertyKey_ProfileLevel; | |
| 61 CFStringRef* kVTCompressionPropertyKey_RealTime; | |
| 62 CFStringRef* kVTCompressionPropertyKey_TransferFunction; | |
| 63 CFStringRef* kVTCompressionPropertyKey_YCbCrMatrix; | |
| 64 CFStringRef* kVTEncodeFrameOptionKey_ForceKeyFrame; | |
| 65 CFStringRef* kVTProfileLevel_H264_Baseline_AutoLevel; | |
| 66 CFStringRef* kVTProfileLevel_H264_Main_AutoLevel; | |
| 67 CFStringRef* kVTProfileLevel_H264_Extended_AutoLevel; | |
| 68 CFStringRef* kVTProfileLevel_H264_High_AutoLevel; | |
| 69 CFStringRef* | |
| 70 kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder; | |
| 71 CFStringRef* | |
| 72 kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder; | |
| 73 }; | |
| 74 | |
| 75 // Lazy-instance responsible for loading VideoToolbox. | |
| 76 class VideoToolboxGlue::Loader { | |
| 77 public: | |
| 78 Loader() { | |
| 79 NSBundle* bundle = [NSBundle | |
| 80 bundleWithPath:@"/System/Library/Frameworks/VideoToolbox.framework"]; | |
| 81 const char* path = [[bundle executablePath] fileSystemRepresentation]; | |
| 82 if (!path) | |
| 83 return; | |
| 84 | |
| 85 handle_ = dlopen(path, RTLD_LAZY | RTLD_LOCAL); | |
| 86 if (!handle_) | |
| 87 return; | |
| 88 | |
| 89 #define LOAD_SYMBOL(SYMBOL) \ | |
| 90 if (!LoadSymbol(#SYMBOL, reinterpret_cast<void**>(&library_.SYMBOL))) \ | |
| 91 return; | |
| 92 | |
| 93 LOAD_SYMBOL(VTCompressionSessionCreate) | |
| 94 LOAD_SYMBOL(VTCompressionSessionEncodeFrame) | |
| 95 LOAD_SYMBOL(VTCompressionSessionGetPixelBufferPool) | |
| 96 LOAD_SYMBOL(VTCompressionSessionInvalidate) | |
| 97 LOAD_SYMBOL(VTCompressionSessionCompleteFrames) | |
| 98 LOAD_SYMBOL(VTSessionSetProperty) | |
| 99 | |
| 100 LOAD_SYMBOL(kVTCompressionPropertyKey_AllowFrameReordering) | |
| 101 LOAD_SYMBOL(kVTCompressionPropertyKey_AverageBitRate) | |
| 102 LOAD_SYMBOL(kVTCompressionPropertyKey_ColorPrimaries) | |
| 103 LOAD_SYMBOL(kVTCompressionPropertyKey_DataRateLimits) | |
| 104 LOAD_SYMBOL(kVTCompressionPropertyKey_ExpectedFrameRate) | |
| 105 LOAD_SYMBOL(kVTCompressionPropertyKey_MaxFrameDelayCount) | |
| 106 LOAD_SYMBOL(kVTCompressionPropertyKey_MaxKeyFrameInterval) | |
| 107 LOAD_SYMBOL(kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration) | |
| 108 LOAD_SYMBOL(kVTCompressionPropertyKey_ProfileLevel) | |
| 109 LOAD_SYMBOL(kVTCompressionPropertyKey_RealTime) | |
| 110 LOAD_SYMBOL(kVTCompressionPropertyKey_TransferFunction) | |
| 111 LOAD_SYMBOL(kVTCompressionPropertyKey_YCbCrMatrix) | |
| 112 LOAD_SYMBOL(kVTEncodeFrameOptionKey_ForceKeyFrame); | |
| 113 LOAD_SYMBOL(kVTProfileLevel_H264_Baseline_AutoLevel) | |
| 114 LOAD_SYMBOL(kVTProfileLevel_H264_Main_AutoLevel) | |
| 115 LOAD_SYMBOL(kVTProfileLevel_H264_Extended_AutoLevel) | |
| 116 LOAD_SYMBOL(kVTProfileLevel_H264_High_AutoLevel) | |
| 117 LOAD_SYMBOL( | |
| 118 kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder) | |
| 119 LOAD_SYMBOL( | |
| 120 kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder) | |
| 121 | |
| 122 #undef LOAD_SYMBOL | |
| 123 | |
| 124 glue_.library_ = &library_; | |
| 125 } | |
| 126 | |
| 127 const VideoToolboxGlue* glue() const { | |
| 128 return (glue_.library_) ? &glue_ : NULL; | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 bool LoadSymbol(const char* name, void** symbol_out) { | |
| 133 *symbol_out = dlsym(handle_, name); | |
| 134 return *symbol_out != NULL; | |
| 135 } | |
| 136 | |
| 137 Library library_; | |
| 138 VideoToolboxGlue glue_; | |
| 139 void* handle_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(Loader); | |
| 142 }; | |
| 143 | |
| 144 static base::LazyInstance<VideoToolboxGlue::Loader> g_videotoolbox_loader = | |
| 145 LAZY_INSTANCE_INITIALIZER; | |
| 146 | |
| 147 // static | |
| 148 const VideoToolboxGlue* VideoToolboxGlue::Get() { | |
| 149 return g_videotoolbox_loader.Get().glue(); | |
| 150 } | |
| 151 | |
| 152 VideoToolboxGlue::VideoToolboxGlue() : library_(NULL) { | |
| 153 } | |
| 154 | |
| 155 OSStatus VideoToolboxGlue::VTCompressionSessionCreate( | |
| 156 CFAllocatorRef allocator, | |
| 157 int32_t width, | |
| 158 int32_t height, | |
| 159 CoreMediaGlue::CMVideoCodecType codecType, | |
| 160 CFDictionaryRef encoderSpecification, | |
| 161 CFDictionaryRef sourceImageBufferAttributes, | |
| 162 CFAllocatorRef compressedDataAllocator, | |
| 163 VTCompressionOutputCallback outputCallback, | |
| 164 void* outputCallbackRefCon, | |
| 165 VTCompressionSessionRef* compressionSessionOut) const { | |
| 166 return library_->VTCompressionSessionCreate(allocator, | |
| 167 width, | |
| 168 height, | |
| 169 codecType, | |
| 170 encoderSpecification, | |
| 171 sourceImageBufferAttributes, | |
| 172 compressedDataAllocator, | |
| 173 outputCallback, | |
| 174 outputCallbackRefCon, | |
| 175 compressionSessionOut); | |
| 176 } | |
| 177 | |
| 178 OSStatus VideoToolboxGlue::VTCompressionSessionEncodeFrame( | |
| 179 VTCompressionSessionRef session, | |
| 180 CVImageBufferRef imageBuffer, | |
| 181 CoreMediaGlue::CMTime presentationTimeStamp, | |
| 182 CoreMediaGlue::CMTime duration, | |
| 183 CFDictionaryRef frameProperties, | |
| 184 void* sourceFrameRefCon, | |
| 185 VTEncodeInfoFlags* infoFlagsOut) const { | |
| 186 return library_->VTCompressionSessionEncodeFrame(session, | |
| 187 imageBuffer, | |
| 188 presentationTimeStamp, | |
| 189 duration, | |
| 190 frameProperties, | |
| 191 sourceFrameRefCon, | |
| 192 infoFlagsOut); | |
| 193 } | |
| 194 | |
| 195 CVPixelBufferPoolRef VideoToolboxGlue::VTCompressionSessionGetPixelBufferPool( | |
| 196 VTCompressionSessionRef session) const { | |
| 197 return library_->VTCompressionSessionGetPixelBufferPool(session); | |
| 198 } | |
| 199 | |
| 200 void VideoToolboxGlue::VTCompressionSessionInvalidate( | |
| 201 VTCompressionSessionRef session) const { | |
| 202 library_->VTCompressionSessionInvalidate(session); | |
| 203 } | |
| 204 | |
| 205 OSStatus VideoToolboxGlue::VTCompressionSessionCompleteFrames( | |
| 206 VTCompressionSessionRef session, | |
| 207 CoreMediaGlue::CMTime completeUntilPresentationTimeStamp) const { | |
| 208 return library_->VTCompressionSessionCompleteFrames( | |
| 209 session, completeUntilPresentationTimeStamp); | |
| 210 } | |
| 211 | |
| 212 OSStatus VideoToolboxGlue::VTSessionSetProperty(VTSessionRef session, | |
| 213 CFStringRef propertyKey, | |
| 214 CFTypeRef propertyValue) const { | |
| 215 return library_->VTSessionSetProperty(session, propertyKey, propertyValue); | |
| 216 } | |
| 217 | |
| 218 #define KEY_ACCESSOR(KEY) \ | |
| 219 CFStringRef VideoToolboxGlue::KEY() const { return *library_->KEY; } | |
| 220 | |
| 221 KEY_ACCESSOR(kVTCompressionPropertyKey_AllowFrameReordering) | |
| 222 KEY_ACCESSOR(kVTCompressionPropertyKey_AverageBitRate) | |
| 223 KEY_ACCESSOR(kVTCompressionPropertyKey_ColorPrimaries) | |
| 224 KEY_ACCESSOR(kVTCompressionPropertyKey_DataRateLimits) | |
| 225 KEY_ACCESSOR(kVTCompressionPropertyKey_ExpectedFrameRate) | |
| 226 KEY_ACCESSOR(kVTCompressionPropertyKey_MaxFrameDelayCount) | |
| 227 KEY_ACCESSOR(kVTCompressionPropertyKey_MaxKeyFrameInterval) | |
| 228 KEY_ACCESSOR(kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration) | |
| 229 KEY_ACCESSOR(kVTCompressionPropertyKey_ProfileLevel) | |
| 230 KEY_ACCESSOR(kVTCompressionPropertyKey_RealTime) | |
| 231 KEY_ACCESSOR(kVTCompressionPropertyKey_TransferFunction) | |
| 232 KEY_ACCESSOR(kVTCompressionPropertyKey_YCbCrMatrix) | |
| 233 KEY_ACCESSOR(kVTEncodeFrameOptionKey_ForceKeyFrame) | |
| 234 KEY_ACCESSOR(kVTProfileLevel_H264_Baseline_AutoLevel) | |
| 235 KEY_ACCESSOR(kVTProfileLevel_H264_Main_AutoLevel) | |
| 236 KEY_ACCESSOR(kVTProfileLevel_H264_Extended_AutoLevel) | |
| 237 KEY_ACCESSOR(kVTProfileLevel_H264_High_AutoLevel) | |
| 238 KEY_ACCESSOR(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder) | |
| 239 KEY_ACCESSOR( | |
| 240 kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder) | |
| 241 | |
| 242 #undef KEY_ACCESSOR | |
| OLD | NEW |