summaryrefslogtreecommitdiff
path: root/chromium/media/base/mac/videotoolbox_glue.mm
blob: 010767c8ee140cb923bbdb00d26d5a9b99e091a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/mac/videotoolbox_glue.h"

#include <dlfcn.h>
#import <Foundation/Foundation.h>

#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"

// This class stores VideoToolbox library symbol pointers.
struct VideoToolboxGlue::Library {
  typedef OSStatus (*VTCompressionSessionCreateMethod)(
      CFAllocatorRef,
      int32_t,
      int32_t,
      CoreMediaGlue::CMVideoCodecType,
      CFDictionaryRef,
      CFDictionaryRef,
      CFAllocatorRef,
      VTCompressionOutputCallback,
      void*,
      VTCompressionSessionRef*);
  typedef OSStatus (*VTCompressionSessionEncodeFrameMethod)(
      VTCompressionSessionRef,
      CVImageBufferRef,
      CoreMediaGlue::CMTime,
      CoreMediaGlue::CMTime,
      CFDictionaryRef,
      void*,
      VTEncodeInfoFlags*);
  typedef CVPixelBufferPoolRef (*VTCompressionSessionGetPixelBufferPoolMethod)(
      VTCompressionSessionRef);
  typedef void (*VTCompressionSessionInvalidateMethod)(VTCompressionSessionRef);
  typedef OSStatus (*VTCompressionSessionCompleteFramesMethod)(
      VTCompressionSessionRef,
      CoreMediaGlue::CMTime);
  typedef OSStatus (*VTSessionSetPropertyMethod)(VTSessionRef,
                                                 CFStringRef,
                                                 CFTypeRef);

  VTCompressionSessionCreateMethod VTCompressionSessionCreate;
  VTCompressionSessionEncodeFrameMethod VTCompressionSessionEncodeFrame;
  VTCompressionSessionGetPixelBufferPoolMethod
      VTCompressionSessionGetPixelBufferPool;
  VTCompressionSessionInvalidateMethod VTCompressionSessionInvalidate;
  VTCompressionSessionCompleteFramesMethod VTCompressionSessionCompleteFrames;
  VTSessionSetPropertyMethod VTSessionSetProperty;

  CFStringRef* kVTCompressionPropertyKey_AllowFrameReordering;
  CFStringRef* kVTCompressionPropertyKey_AverageBitRate;
  CFStringRef* kVTCompressionPropertyKey_ColorPrimaries;
  CFStringRef* kVTCompressionPropertyKey_ExpectedFrameRate;
  CFStringRef* kVTCompressionPropertyKey_MaxFrameDelayCount;
  CFStringRef* kVTCompressionPropertyKey_MaxKeyFrameInterval;
  CFStringRef* kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration;
  CFStringRef* kVTCompressionPropertyKey_ProfileLevel;
  CFStringRef* kVTCompressionPropertyKey_RealTime;
  CFStringRef* kVTCompressionPropertyKey_TransferFunction;
  CFStringRef* kVTCompressionPropertyKey_YCbCrMatrix;
  CFStringRef* kVTEncodeFrameOptionKey_ForceKeyFrame;
  CFStringRef* kVTProfileLevel_H264_Baseline_AutoLevel;
  CFStringRef* kVTProfileLevel_H264_Main_AutoLevel;
  CFStringRef* kVTProfileLevel_H264_Extended_AutoLevel;
  CFStringRef* kVTProfileLevel_H264_High_AutoLevel;
  CFStringRef*
      kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
};

// Lazy-instance responsible for loading VideoToolbox.
class VideoToolboxGlue::Loader {
 public:
  Loader() {
    NSBundle* bundle = [NSBundle
        bundleWithPath:@"/System/Library/Frameworks/VideoToolbox.framework"];
    const char* path = [[bundle executablePath] fileSystemRepresentation];
    if (!path)
      return;

    handle_ = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
    if (!handle_)
      return;

#define LOAD_SYMBOL(SYMBOL)                                             \
  if (!LoadSymbol(#SYMBOL, reinterpret_cast<void**>(&library_.SYMBOL))) \
    return;

    LOAD_SYMBOL(VTCompressionSessionCreate)
    LOAD_SYMBOL(VTCompressionSessionEncodeFrame)
    LOAD_SYMBOL(VTCompressionSessionGetPixelBufferPool)
    LOAD_SYMBOL(VTCompressionSessionInvalidate)
    LOAD_SYMBOL(VTCompressionSessionCompleteFrames)
    LOAD_SYMBOL(VTSessionSetProperty)

    LOAD_SYMBOL(kVTCompressionPropertyKey_AllowFrameReordering)
    LOAD_SYMBOL(kVTCompressionPropertyKey_AverageBitRate)
    LOAD_SYMBOL(kVTCompressionPropertyKey_ColorPrimaries)
    LOAD_SYMBOL(kVTCompressionPropertyKey_ExpectedFrameRate)
    LOAD_SYMBOL(kVTCompressionPropertyKey_MaxFrameDelayCount)
    LOAD_SYMBOL(kVTCompressionPropertyKey_MaxKeyFrameInterval)
    LOAD_SYMBOL(kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration)
    LOAD_SYMBOL(kVTCompressionPropertyKey_ProfileLevel)
    LOAD_SYMBOL(kVTCompressionPropertyKey_RealTime)
    LOAD_SYMBOL(kVTCompressionPropertyKey_TransferFunction)
    LOAD_SYMBOL(kVTCompressionPropertyKey_YCbCrMatrix)
    LOAD_SYMBOL(kVTEncodeFrameOptionKey_ForceKeyFrame);
    LOAD_SYMBOL(kVTProfileLevel_H264_Baseline_AutoLevel)
    LOAD_SYMBOL(kVTProfileLevel_H264_Main_AutoLevel)
    LOAD_SYMBOL(kVTProfileLevel_H264_Extended_AutoLevel)
    LOAD_SYMBOL(kVTProfileLevel_H264_High_AutoLevel)
    LOAD_SYMBOL(
        kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder)

#undef LOAD_SYMBOL

    glue_.library_ = &library_;
  }

  const VideoToolboxGlue* glue() const {
    return (glue_.library_) ? &glue_ : NULL;
  }

 private:
  bool LoadSymbol(const char* name, void** symbol_out) {
    *symbol_out = dlsym(handle_, name);
    return *symbol_out != NULL;
  }

  Library library_;
  VideoToolboxGlue glue_;
  void* handle_;

  DISALLOW_COPY_AND_ASSIGN(Loader);
};

static base::LazyInstance<VideoToolboxGlue::Loader> g_videotoolbox_loader =
    LAZY_INSTANCE_INITIALIZER;

// static
const VideoToolboxGlue* VideoToolboxGlue::Get() {
  return g_videotoolbox_loader.Get().glue();
}

VideoToolboxGlue::VideoToolboxGlue() : library_(NULL) {
}

OSStatus VideoToolboxGlue::VTCompressionSessionCreate(
    CFAllocatorRef allocator,
    int32_t width,
    int32_t height,
    CoreMediaGlue::CMVideoCodecType codecType,
    CFDictionaryRef encoderSpecification,
    CFDictionaryRef sourceImageBufferAttributes,
    CFAllocatorRef compressedDataAllocator,
    VTCompressionOutputCallback outputCallback,
    void* outputCallbackRefCon,
    VTCompressionSessionRef* compressionSessionOut) const {
  return library_->VTCompressionSessionCreate(allocator,
                                              width,
                                              height,
                                              codecType,
                                              encoderSpecification,
                                              sourceImageBufferAttributes,
                                              compressedDataAllocator,
                                              outputCallback,
                                              outputCallbackRefCon,
                                              compressionSessionOut);
}

OSStatus VideoToolboxGlue::VTCompressionSessionEncodeFrame(
    VTCompressionSessionRef session,
    CVImageBufferRef imageBuffer,
    CoreMediaGlue::CMTime presentationTimeStamp,
    CoreMediaGlue::CMTime duration,
    CFDictionaryRef frameProperties,
    void* sourceFrameRefCon,
    VTEncodeInfoFlags* infoFlagsOut) const {
  return library_->VTCompressionSessionEncodeFrame(session,
                                                   imageBuffer,
                                                   presentationTimeStamp,
                                                   duration,
                                                   frameProperties,
                                                   sourceFrameRefCon,
                                                   infoFlagsOut);
}

CVPixelBufferPoolRef VideoToolboxGlue::VTCompressionSessionGetPixelBufferPool(
    VTCompressionSessionRef session) const {
  return library_->VTCompressionSessionGetPixelBufferPool(session);
}

void VideoToolboxGlue::VTCompressionSessionInvalidate(
    VTCompressionSessionRef session) const {
  library_->VTCompressionSessionInvalidate(session);
}

OSStatus VideoToolboxGlue::VTCompressionSessionCompleteFrames(
    VTCompressionSessionRef session,
    CoreMediaGlue::CMTime completeUntilPresentationTimeStamp) const {
  return library_->VTCompressionSessionCompleteFrames(
      session, completeUntilPresentationTimeStamp);
}

OSStatus VideoToolboxGlue::VTSessionSetProperty(VTSessionRef session,
                                                CFStringRef propertyKey,
                                                CFTypeRef propertyValue) const {
  return library_->VTSessionSetProperty(session, propertyKey, propertyValue);
}

#define KEY_ACCESSOR(KEY) \
  CFStringRef VideoToolboxGlue::KEY() const { return *library_->KEY; }

KEY_ACCESSOR(kVTCompressionPropertyKey_AllowFrameReordering)
KEY_ACCESSOR(kVTCompressionPropertyKey_AverageBitRate)
KEY_ACCESSOR(kVTCompressionPropertyKey_ColorPrimaries)
KEY_ACCESSOR(kVTCompressionPropertyKey_ExpectedFrameRate)
KEY_ACCESSOR(kVTCompressionPropertyKey_MaxFrameDelayCount)
KEY_ACCESSOR(kVTCompressionPropertyKey_MaxKeyFrameInterval)
KEY_ACCESSOR(kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration)
KEY_ACCESSOR(kVTCompressionPropertyKey_ProfileLevel)
KEY_ACCESSOR(kVTCompressionPropertyKey_RealTime)
KEY_ACCESSOR(kVTCompressionPropertyKey_TransferFunction)
KEY_ACCESSOR(kVTCompressionPropertyKey_YCbCrMatrix)
KEY_ACCESSOR(kVTEncodeFrameOptionKey_ForceKeyFrame)
KEY_ACCESSOR(kVTProfileLevel_H264_Baseline_AutoLevel)
KEY_ACCESSOR(kVTProfileLevel_H264_Main_AutoLevel)
KEY_ACCESSOR(kVTProfileLevel_H264_Extended_AutoLevel)
KEY_ACCESSOR(kVTProfileLevel_H264_High_AutoLevel)
KEY_ACCESSOR(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder)

#undef KEY_ACCESSOR