PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/media/base/mac/videotoolbox_glue.mm

https://gitlab.com/jonnialva90/iridium-browser
Objective C++ | 233 lines | 198 code | 29 blank | 6 comment | 4 complexity | c4e2c08e6bde894062fc5330eded0b27 MD5 | raw file
  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. #include "media/base/mac/videotoolbox_glue.h"
  5. #include <dlfcn.h>
  6. #import <Foundation/Foundation.h>
  7. #include "base/lazy_instance.h"
  8. #include "base/memory/scoped_ptr.h"
  9. // This class stores VideoToolbox library symbol pointers.
  10. struct VideoToolboxGlue::Library {
  11. typedef OSStatus (*VTCompressionSessionCreateMethod)(
  12. CFAllocatorRef,
  13. int32_t,
  14. int32_t,
  15. CoreMediaGlue::CMVideoCodecType,
  16. CFDictionaryRef,
  17. CFDictionaryRef,
  18. CFAllocatorRef,
  19. VTCompressionOutputCallback,
  20. void*,
  21. VTCompressionSessionRef*);
  22. typedef OSStatus (*VTCompressionSessionEncodeFrameMethod)(
  23. VTCompressionSessionRef,
  24. CVImageBufferRef,
  25. CoreMediaGlue::CMTime,
  26. CoreMediaGlue::CMTime,
  27. CFDictionaryRef,
  28. void*,
  29. VTEncodeInfoFlags*);
  30. typedef CVPixelBufferPoolRef (*VTCompressionSessionGetPixelBufferPoolMethod)(
  31. VTCompressionSessionRef);
  32. typedef void (*VTCompressionSessionInvalidateMethod)(VTCompressionSessionRef);
  33. typedef OSStatus (*VTCompressionSessionCompleteFramesMethod)(
  34. VTCompressionSessionRef,
  35. CoreMediaGlue::CMTime);
  36. typedef OSStatus (*VTSessionSetPropertyMethod)(VTSessionRef,
  37. CFStringRef,
  38. CFTypeRef);
  39. VTCompressionSessionCreateMethod VTCompressionSessionCreate;
  40. VTCompressionSessionEncodeFrameMethod VTCompressionSessionEncodeFrame;
  41. VTCompressionSessionGetPixelBufferPoolMethod
  42. VTCompressionSessionGetPixelBufferPool;
  43. VTCompressionSessionInvalidateMethod VTCompressionSessionInvalidate;
  44. VTCompressionSessionCompleteFramesMethod VTCompressionSessionCompleteFrames;
  45. VTSessionSetPropertyMethod VTSessionSetProperty;
  46. CFStringRef* kVTCompressionPropertyKey_AllowFrameReordering;
  47. CFStringRef* kVTCompressionPropertyKey_AverageBitRate;
  48. CFStringRef* kVTCompressionPropertyKey_ColorPrimaries;
  49. CFStringRef* kVTCompressionPropertyKey_ExpectedFrameRate;
  50. CFStringRef* kVTCompressionPropertyKey_MaxFrameDelayCount;
  51. CFStringRef* kVTCompressionPropertyKey_MaxKeyFrameInterval;
  52. CFStringRef* kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration;
  53. CFStringRef* kVTCompressionPropertyKey_ProfileLevel;
  54. CFStringRef* kVTCompressionPropertyKey_RealTime;
  55. CFStringRef* kVTCompressionPropertyKey_TransferFunction;
  56. CFStringRef* kVTCompressionPropertyKey_YCbCrMatrix;
  57. CFStringRef* kVTEncodeFrameOptionKey_ForceKeyFrame;
  58. CFStringRef* kVTProfileLevel_H264_Baseline_AutoLevel;
  59. CFStringRef* kVTProfileLevel_H264_Main_AutoLevel;
  60. CFStringRef* kVTProfileLevel_H264_Extended_AutoLevel;
  61. CFStringRef* kVTProfileLevel_H264_High_AutoLevel;
  62. CFStringRef*
  63. kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
  64. };
  65. // Lazy-instance responsible for loading VideoToolbox.
  66. class VideoToolboxGlue::Loader {
  67. public:
  68. Loader() {
  69. NSBundle* bundle = [NSBundle
  70. bundleWithPath:@"/System/Library/Frameworks/VideoToolbox.framework"];
  71. const char* path = [[bundle executablePath] fileSystemRepresentation];
  72. if (!path)
  73. return;
  74. handle_ = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
  75. if (!handle_)
  76. return;
  77. #define LOAD_SYMBOL(SYMBOL) \
  78. if (!LoadSymbol(#SYMBOL, reinterpret_cast<void**>(&library_.SYMBOL))) \
  79. return;
  80. LOAD_SYMBOL(VTCompressionSessionCreate)
  81. LOAD_SYMBOL(VTCompressionSessionEncodeFrame)
  82. LOAD_SYMBOL(VTCompressionSessionGetPixelBufferPool)
  83. LOAD_SYMBOL(VTCompressionSessionInvalidate)
  84. LOAD_SYMBOL(VTCompressionSessionCompleteFrames)
  85. LOAD_SYMBOL(VTSessionSetProperty)
  86. LOAD_SYMBOL(kVTCompressionPropertyKey_AllowFrameReordering)
  87. LOAD_SYMBOL(kVTCompressionPropertyKey_AverageBitRate)
  88. LOAD_SYMBOL(kVTCompressionPropertyKey_ColorPrimaries)
  89. LOAD_SYMBOL(kVTCompressionPropertyKey_ExpectedFrameRate)
  90. LOAD_SYMBOL(kVTCompressionPropertyKey_MaxFrameDelayCount)
  91. LOAD_SYMBOL(kVTCompressionPropertyKey_MaxKeyFrameInterval)
  92. LOAD_SYMBOL(kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration)
  93. LOAD_SYMBOL(kVTCompressionPropertyKey_ProfileLevel)
  94. LOAD_SYMBOL(kVTCompressionPropertyKey_RealTime)
  95. LOAD_SYMBOL(kVTCompressionPropertyKey_TransferFunction)
  96. LOAD_SYMBOL(kVTCompressionPropertyKey_YCbCrMatrix)
  97. LOAD_SYMBOL(kVTEncodeFrameOptionKey_ForceKeyFrame);
  98. LOAD_SYMBOL(kVTProfileLevel_H264_Baseline_AutoLevel)
  99. LOAD_SYMBOL(kVTProfileLevel_H264_Main_AutoLevel)
  100. LOAD_SYMBOL(kVTProfileLevel_H264_Extended_AutoLevel)
  101. LOAD_SYMBOL(kVTProfileLevel_H264_High_AutoLevel)
  102. LOAD_SYMBOL(
  103. kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder)
  104. #undef LOAD_SYMBOL
  105. glue_.library_ = &library_;
  106. }
  107. const VideoToolboxGlue* glue() const {
  108. return (glue_.library_) ? &glue_ : NULL;
  109. }
  110. private:
  111. bool LoadSymbol(const char* name, void** symbol_out) {
  112. *symbol_out = dlsym(handle_, name);
  113. return *symbol_out != NULL;
  114. }
  115. Library library_;
  116. VideoToolboxGlue glue_;
  117. void* handle_;
  118. DISALLOW_COPY_AND_ASSIGN(Loader);
  119. };
  120. static base::LazyInstance<VideoToolboxGlue::Loader> g_videotoolbox_loader =
  121. LAZY_INSTANCE_INITIALIZER;
  122. // static
  123. const VideoToolboxGlue* VideoToolboxGlue::Get() {
  124. return g_videotoolbox_loader.Get().glue();
  125. }
  126. VideoToolboxGlue::VideoToolboxGlue() : library_(NULL) {
  127. }
  128. OSStatus VideoToolboxGlue::VTCompressionSessionCreate(
  129. CFAllocatorRef allocator,
  130. int32_t width,
  131. int32_t height,
  132. CoreMediaGlue::CMVideoCodecType codecType,
  133. CFDictionaryRef encoderSpecification,
  134. CFDictionaryRef sourceImageBufferAttributes,
  135. CFAllocatorRef compressedDataAllocator,
  136. VTCompressionOutputCallback outputCallback,
  137. void* outputCallbackRefCon,
  138. VTCompressionSessionRef* compressionSessionOut) const {
  139. return library_->VTCompressionSessionCreate(allocator,
  140. width,
  141. height,
  142. codecType,
  143. encoderSpecification,
  144. sourceImageBufferAttributes,
  145. compressedDataAllocator,
  146. outputCallback,
  147. outputCallbackRefCon,
  148. compressionSessionOut);
  149. }
  150. OSStatus VideoToolboxGlue::VTCompressionSessionEncodeFrame(
  151. VTCompressionSessionRef session,
  152. CVImageBufferRef imageBuffer,
  153. CoreMediaGlue::CMTime presentationTimeStamp,
  154. CoreMediaGlue::CMTime duration,
  155. CFDictionaryRef frameProperties,
  156. void* sourceFrameRefCon,
  157. VTEncodeInfoFlags* infoFlagsOut) const {
  158. return library_->VTCompressionSessionEncodeFrame(session,
  159. imageBuffer,
  160. presentationTimeStamp,
  161. duration,
  162. frameProperties,
  163. sourceFrameRefCon,
  164. infoFlagsOut);
  165. }
  166. CVPixelBufferPoolRef VideoToolboxGlue::VTCompressionSessionGetPixelBufferPool(
  167. VTCompressionSessionRef session) const {
  168. return library_->VTCompressionSessionGetPixelBufferPool(session);
  169. }
  170. void VideoToolboxGlue::VTCompressionSessionInvalidate(
  171. VTCompressionSessionRef session) const {
  172. library_->VTCompressionSessionInvalidate(session);
  173. }
  174. OSStatus VideoToolboxGlue::VTCompressionSessionCompleteFrames(
  175. VTCompressionSessionRef session,
  176. CoreMediaGlue::CMTime completeUntilPresentationTimeStamp) const {
  177. return library_->VTCompressionSessionCompleteFrames(
  178. session, completeUntilPresentationTimeStamp);
  179. }
  180. OSStatus VideoToolboxGlue::VTSessionSetProperty(VTSessionRef session,
  181. CFStringRef propertyKey,
  182. CFTypeRef propertyValue) const {
  183. return library_->VTSessionSetProperty(session, propertyKey, propertyValue);
  184. }
  185. #define KEY_ACCESSOR(KEY) \
  186. CFStringRef VideoToolboxGlue::KEY() const { return *library_->KEY; }
  187. KEY_ACCESSOR(kVTCompressionPropertyKey_AllowFrameReordering)
  188. KEY_ACCESSOR(kVTCompressionPropertyKey_AverageBitRate)
  189. KEY_ACCESSOR(kVTCompressionPropertyKey_ColorPrimaries)
  190. KEY_ACCESSOR(kVTCompressionPropertyKey_ExpectedFrameRate)
  191. KEY_ACCESSOR(kVTCompressionPropertyKey_MaxFrameDelayCount)
  192. KEY_ACCESSOR(kVTCompressionPropertyKey_MaxKeyFrameInterval)
  193. KEY_ACCESSOR(kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration)
  194. KEY_ACCESSOR(kVTCompressionPropertyKey_ProfileLevel)
  195. KEY_ACCESSOR(kVTCompressionPropertyKey_RealTime)
  196. KEY_ACCESSOR(kVTCompressionPropertyKey_TransferFunction)
  197. KEY_ACCESSOR(kVTCompressionPropertyKey_YCbCrMatrix)
  198. KEY_ACCESSOR(kVTEncodeFrameOptionKey_ForceKeyFrame)
  199. KEY_ACCESSOR(kVTProfileLevel_H264_Baseline_AutoLevel)
  200. KEY_ACCESSOR(kVTProfileLevel_H264_Main_AutoLevel)
  201. KEY_ACCESSOR(kVTProfileLevel_H264_Extended_AutoLevel)
  202. KEY_ACCESSOR(kVTProfileLevel_H264_High_AutoLevel)
  203. KEY_ACCESSOR(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder)
  204. #undef KEY_ACCESSOR