From a4955b4d9a0ef84ffe1aa262a38f77740f53fa4d Mon Sep 17 00:00:00 2001 From: kthelgason Date: Thu, 24 Aug 2017 04:22:58 -0700 Subject: [PATCH] Enable the HW VideoToolbox encoder on mac. This CL was originally submitted by an external contributor in https://chromium-review.googlesource.com/c/external/webrtc/+/582051. I have rebased it to the new ObjC encoder class. BUG=webrtc:8022 Review-Url: https://codereview.webrtc.org/3003653002 Cr-Commit-Position: refs/heads/master@{#19486} --- .../VideoToolbox/RTCVideoEncoderH264.mm | 47 +++++++++++++++---- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/webrtc/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm b/webrtc/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm index 7ac8c18237..44983beb6a 100644 --- a/webrtc/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm +++ b/webrtc/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm @@ -509,24 +509,51 @@ CFStringRef ExtractProfile(const cricket::VideoCodec &codec) { CFRelease(pixelFormat); pixelFormat = nullptr; } - OSStatus status = VTCompressionSessionCreate(nullptr, // use default allocator - _width, - _height, - kCMVideoCodecType_H264, - nullptr, // use default encoder - sourceAttributes, - nullptr, // use default compressed data allocator - compressionOutputCallback, - nullptr, - &_compressionSession); + CFMutableDictionaryRef encoder_specs = nullptr; +#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) + // Currently hw accl is supported above 360p on mac, below 360p + // the compression session will be created with hw accl disabled. + encoder_specs = CFDictionaryCreateMutable( + nullptr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + CFDictionarySetValue(encoder_specs, + kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, + kCFBooleanTrue); +#endif + OSStatus status = + VTCompressionSessionCreate(nullptr, // use default allocator + _width, + _height, + kCMVideoCodecType_H264, + encoder_specs, // use hardware accelerated encoder if available + sourceAttributes, + nullptr, // use default compressed data allocator + compressionOutputCallback, + nullptr, + &_compressionSession); if (sourceAttributes) { CFRelease(sourceAttributes); sourceAttributes = nullptr; } + if (encoder_specs) { + CFRelease(encoder_specs); + encoder_specs = nullptr; + } if (status != noErr) { LOG(LS_ERROR) << "Failed to create compression session: " << status; return WEBRTC_VIDEO_CODEC_ERROR; } +#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) + CFBooleanRef hwaccl_enabled = nullptr; + status = VTSessionCopyProperty(_compressionSession, + kVTCompressionPropertyKey_UsingHardwareAcceleratedVideoEncoder, + nullptr, + &hwaccl_enabled); + if (status == noErr && (CFBooleanGetValue(hwaccl_enabled))) { + LOG(LS_INFO) << "Compression session created with hw accl enabled"; + } else { + LOG(LS_INFO) << "Compression session created with hw accl disabled"; + } +#endif [self configureCompressionSession]; return WEBRTC_VIDEO_CODEC_OK; }