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.
5 #include "media/base/mac/videotoolbox_glue.h"
8 #import <Foundation/Foundation.h>
10 #include "base/lazy_instance.h"
11 #include "base/memory/scoped_ptr.h"
13 // This class stores VideoToolbox library symbol pointers.
14 struct VideoToolboxGlue::Library {
15 typedef OSStatus (*VTCompressionSessionCreateMethod)(
19 CoreMediaGlue::CMVideoCodecType,
23 VTCompressionOutputCallback,
25 VTCompressionSessionRef*);
26 typedef OSStatus (*VTCompressionSessionEncodeFrameMethod)(
27 VTCompressionSessionRef,
29 CoreMediaGlue::CMTime,
30 CoreMediaGlue::CMTime,
34 typedef CVPixelBufferPoolRef (*VTCompressionSessionGetPixelBufferPoolMethod)(
35 VTCompressionSessionRef);
36 typedef void (*VTCompressionSessionInvalidateMethod)(VTCompressionSessionRef);
37 typedef OSStatus (*VTSessionSetPropertyMethod)(VTSessionRef,
41 VTCompressionSessionCreateMethod VTCompressionSessionCreate;
42 VTCompressionSessionEncodeFrameMethod VTCompressionSessionEncodeFrame;
43 VTCompressionSessionGetPixelBufferPoolMethod
44 VTCompressionSessionGetPixelBufferPool;
45 VTCompressionSessionInvalidateMethod VTCompressionSessionInvalidate;
46 VTSessionSetPropertyMethod VTSessionSetProperty;
48 CFStringRef* kVTCompressionPropertyKey_AllowFrameReordering;
49 CFStringRef* kVTCompressionPropertyKey_AverageBitRate;
50 CFStringRef* kVTCompressionPropertyKey_ColorPrimaries;
51 CFStringRef* kVTCompressionPropertyKey_ExpectedFrameRate;
52 CFStringRef* kVTCompressionPropertyKey_MaxKeyFrameInterval;
53 CFStringRef* kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration;
54 CFStringRef* kVTCompressionPropertyKey_ProfileLevel;
55 CFStringRef* kVTCompressionPropertyKey_RealTime;
56 CFStringRef* kVTCompressionPropertyKey_TransferFunction;
57 CFStringRef* kVTCompressionPropertyKey_YCbCrMatrix;
58 CFStringRef* kVTEncodeFrameOptionKey_ForceKeyFrame;
59 CFStringRef* kVTProfileLevel_H264_Baseline_AutoLevel;
60 CFStringRef* kVTProfileLevel_H264_Main_AutoLevel;
61 CFStringRef* kVTProfileLevel_H264_Extended_AutoLevel;
62 CFStringRef* kVTProfileLevel_H264_High_AutoLevel;
64 kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
67 // Lazy-instance responsible for loading VideoToolbox.
68 class VideoToolboxGlue::Loader {
71 NSBundle* bundle = [NSBundle
72 bundleWithPath:@"/System/Library/Frameworks/VideoToolbox.framework"];
73 const char* path = [[bundle executablePath] fileSystemRepresentation];
77 handle_ = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
81 #define LOAD_SYMBOL(SYMBOL) \
82 if (!LoadSymbol(#SYMBOL, reinterpret_cast<void**>(&library_.SYMBOL))) \
85 LOAD_SYMBOL(VTCompressionSessionCreate)
86 LOAD_SYMBOL(VTCompressionSessionEncodeFrame)
87 LOAD_SYMBOL(VTCompressionSessionGetPixelBufferPool)
88 LOAD_SYMBOL(VTCompressionSessionInvalidate)
89 LOAD_SYMBOL(VTSessionSetProperty)
91 LOAD_SYMBOL(kVTCompressionPropertyKey_AllowFrameReordering)
92 LOAD_SYMBOL(kVTCompressionPropertyKey_AverageBitRate)
93 LOAD_SYMBOL(kVTCompressionPropertyKey_ColorPrimaries)
94 LOAD_SYMBOL(kVTCompressionPropertyKey_ExpectedFrameRate)
95 LOAD_SYMBOL(kVTCompressionPropertyKey_MaxKeyFrameInterval)
96 LOAD_SYMBOL(kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration)
97 LOAD_SYMBOL(kVTCompressionPropertyKey_ProfileLevel)
98 LOAD_SYMBOL(kVTCompressionPropertyKey_RealTime)
99 LOAD_SYMBOL(kVTCompressionPropertyKey_TransferFunction)
100 LOAD_SYMBOL(kVTCompressionPropertyKey_YCbCrMatrix)
101 LOAD_SYMBOL(kVTEncodeFrameOptionKey_ForceKeyFrame);
102 LOAD_SYMBOL(kVTProfileLevel_H264_Baseline_AutoLevel)
103 LOAD_SYMBOL(kVTProfileLevel_H264_Main_AutoLevel)
104 LOAD_SYMBOL(kVTProfileLevel_H264_Extended_AutoLevel)
105 LOAD_SYMBOL(kVTProfileLevel_H264_High_AutoLevel)
107 kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder)
111 glue_.library_ = &library_;
114 const VideoToolboxGlue* glue() const {
115 return (glue_.library_) ? &glue_ : NULL;
119 bool LoadSymbol(const char* name, void** symbol_out) {
120 *symbol_out = dlsym(handle_, name);
121 return *symbol_out != NULL;
125 VideoToolboxGlue glue_;
128 DISALLOW_COPY_AND_ASSIGN(Loader);
131 static base::LazyInstance<VideoToolboxGlue::Loader> g_videotoolbox_loader =
132 LAZY_INSTANCE_INITIALIZER;
135 const VideoToolboxGlue* VideoToolboxGlue::Get() {
136 return g_videotoolbox_loader.Get().glue();
139 VideoToolboxGlue::VideoToolboxGlue() : library_(NULL) {
142 OSStatus VideoToolboxGlue::VTCompressionSessionCreate(
143 CFAllocatorRef allocator,
146 CoreMediaGlue::CMVideoCodecType codecType,
147 CFDictionaryRef encoderSpecification,
148 CFDictionaryRef sourceImageBufferAttributes,
149 CFAllocatorRef compressedDataAllocator,
150 VTCompressionOutputCallback outputCallback,
151 void* outputCallbackRefCon,
152 VTCompressionSessionRef* compressionSessionOut) const {
153 return library_->VTCompressionSessionCreate(allocator,
157 encoderSpecification,
158 sourceImageBufferAttributes,
159 compressedDataAllocator,
161 outputCallbackRefCon,
162 compressionSessionOut);
165 OSStatus VideoToolboxGlue::VTCompressionSessionEncodeFrame(
166 VTCompressionSessionRef session,
167 CVImageBufferRef imageBuffer,
168 CoreMediaGlue::CMTime presentationTimeStamp,
169 CoreMediaGlue::CMTime duration,
170 CFDictionaryRef frameProperties,
171 void* sourceFrameRefCon,
172 VTEncodeInfoFlags* infoFlagsOut) const {
173 return library_->VTCompressionSessionEncodeFrame(session,
175 presentationTimeStamp,
182 CVPixelBufferPoolRef VideoToolboxGlue::VTCompressionSessionGetPixelBufferPool(
183 VTCompressionSessionRef session) const {
184 return library_->VTCompressionSessionGetPixelBufferPool(session);
187 void VideoToolboxGlue::VTCompressionSessionInvalidate(
188 VTCompressionSessionRef session) const {
189 library_->VTCompressionSessionInvalidate(session);
192 OSStatus VideoToolboxGlue::VTSessionSetProperty(VTSessionRef session,
193 CFStringRef propertyKey,
194 CFTypeRef propertyValue) const {
195 return library_->VTSessionSetProperty(session, propertyKey, propertyValue);
198 #define KEY_ACCESSOR(KEY) \
199 CFStringRef VideoToolboxGlue::KEY() const { return *library_->KEY; }
201 KEY_ACCESSOR(kVTCompressionPropertyKey_AllowFrameReordering)
202 KEY_ACCESSOR(kVTCompressionPropertyKey_AverageBitRate)
203 KEY_ACCESSOR(kVTCompressionPropertyKey_ColorPrimaries)
204 KEY_ACCESSOR(kVTCompressionPropertyKey_ExpectedFrameRate)
205 KEY_ACCESSOR(kVTCompressionPropertyKey_MaxKeyFrameInterval)
206 KEY_ACCESSOR(kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration)
207 KEY_ACCESSOR(kVTCompressionPropertyKey_ProfileLevel)
208 KEY_ACCESSOR(kVTCompressionPropertyKey_RealTime)
209 KEY_ACCESSOR(kVTCompressionPropertyKey_TransferFunction)
210 KEY_ACCESSOR(kVTCompressionPropertyKey_YCbCrMatrix)
211 KEY_ACCESSOR(kVTEncodeFrameOptionKey_ForceKeyFrame)
212 KEY_ACCESSOR(kVTProfileLevel_H264_Baseline_AutoLevel)
213 KEY_ACCESSOR(kVTProfileLevel_H264_Main_AutoLevel)
214 KEY_ACCESSOR(kVTProfileLevel_H264_Extended_AutoLevel)
215 KEY_ACCESSOR(kVTProfileLevel_H264_High_AutoLevel)
216 KEY_ACCESSOR(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder)