macosencodingh.264video-toolbox

How to debug why Mac OS is not using Hardware H264 encoder


I'm trying to encode some video only stream using H264, and I'm willing to use the hardware encoder in order to compare both quality and resource consumption between hardware and CPU encoding. The thing is that I'm not being able to force the OS to use the hardware encoder.

This is the code I'm using to create the VTCompressionSession:

var status: OSStatus

let encoderSpecifications: CFDictionary? = [
    kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder as String: true,
    kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder as String: true,
    kVTVideoEncoderSpecification_EncoderID as String: "com.apple.videotoolbox.videoencoder.24rgb" // Tried without this paramenter so the system can decide what encoder ID should be using but doesn't work anyway.
]

let pixelBufferOptions: CFDictionary? = [
    kCVPixelBufferWidthKey as String: Int(width),
    kCVPixelBufferHeightKey as String: Int(height),
    kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_24RGB) // Tried commenting this in case that there was a pixelformat constraint but didn't change anything
];

status = VTCompressionSessionCreate(kCFAllocatorDefault, width, height, CMVideoCodecType(kCMVideoCodecType_H264), encoderSpecifications, pixelBufferOptions, nil, { (outputCallbackRefCon: UnsafeMutablePointer<Void>, sourceFrameRefCon: UnsafeMutablePointer<Void>, status: OSStatus, infoFlags: VTEncodeInfoFlags, sampleBuffer: CMSampleBuffer?) -> Void in
    ...
}, unsafeBitCast(self, UnsafeMutablePointer<Void>.self), &compressionSession)

I opened the Console and this the only relevant message I'm getting when I try to create the session:

10/28/15 22:06:27.711 Dupla-Mac[87762]: <<<< VTVideoEncoderSelection >>>> VTSelectAndCreateVideoEncoderInstanceInternal: no video encoder found for 'avc1'

This is the status code I get when I use the EncoderID:

2015-10-28 22:17:13.480 Dupla-Mac[87895:5578917] Couldn't create compression session :( -12908

And this is the one I get when I don't use the EncoderID:

2015-10-28 22:18:16.695 Dupla-Mac[87996:5581914] Couldn't create compression session :( -12915

Both relate to the lack of availability of the resource, but couldn't find any difference. I've checked that the most known functionality that may use the hardware encoder are turned off, but I don't know how to check this for sure. AirPlay is off, QuickTime is off, there's not any app accessing the camera, and so.

TL;DR: is there any way to force or to know what's the strategy the OS is using to enable the Hardware Encoder, and eventually know why it is not available at any moment?

Thanks in advance!


Solution

  • I guess you've already resolved the problem but for others - the only HW-accelerated encoder available on macOS (10.8-10.12 for all macs 2012+) / iOS(8-10) is com.apple.videotoolbox.videoencoder.h264.gva and here is the full list: https://gist.github.com/vade/06ace2e33561a79cc240