2018-07-16 01:11:57 +00:00
|
|
|
// Nasty hack to avoid conflict between AVFoundation and
|
|
|
|
// libavutil both defining AVMediaType
|
|
|
|
#define AVMediaType AVMediaType_FFmpeg
|
|
|
|
#include "vt.h"
|
2018-08-16 05:02:15 +00:00
|
|
|
#include "pacer/pacer.h"
|
2018-07-16 01:11:57 +00:00
|
|
|
#undef AVMediaType
|
|
|
|
|
|
|
|
#include <SDL_syswm.h>
|
|
|
|
#include <Limelight.h>
|
2019-02-14 04:55:52 +00:00
|
|
|
#include <streaming/session.h>
|
2018-07-16 01:11:57 +00:00
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
#include <mach/mach_time.h>
|
2022-01-23 02:25:50 +00:00
|
|
|
#include <mach/machine.h>
|
|
|
|
#include <sys/sysctl.h>
|
2018-07-16 01:11:57 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#import <VideoToolbox/VideoToolbox.h>
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
2019-02-14 04:55:52 +00:00
|
|
|
#import <dispatch/dispatch.h>
|
2019-11-06 02:36:43 +00:00
|
|
|
#import <Metal/Metal.h>
|
2018-07-18 07:10:22 +00:00
|
|
|
|
2020-05-07 05:17:22 +00:00
|
|
|
@interface VTView : NSView
|
|
|
|
- (NSView *)hitTest:(NSPoint)point;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation VTView
|
|
|
|
|
|
|
|
- (NSView *)hitTest:(NSPoint)point {
|
|
|
|
Q_UNUSED(point);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2018-08-16 06:57:03 +00:00
|
|
|
class VTRenderer : public IFFmpegRenderer
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
VTRenderer()
|
|
|
|
: m_HwContext(nullptr),
|
|
|
|
m_DisplayLayer(nullptr),
|
|
|
|
m_FormatDesc(nullptr),
|
2019-05-12 01:33:12 +00:00
|
|
|
m_StreamView(nullptr),
|
2019-05-21 16:09:16 +00:00
|
|
|
m_DisplayLink(nullptr),
|
2021-11-19 06:28:15 +00:00
|
|
|
m_LastAvColorSpace(AVCOL_SPC_UNSPECIFIED),
|
2020-03-11 05:21:09 +00:00
|
|
|
m_ColorSpace(nullptr),
|
2019-05-21 16:09:16 +00:00
|
|
|
m_VsyncMutex(nullptr),
|
|
|
|
m_VsyncPassed(nullptr)
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
2019-03-19 03:56:53 +00:00
|
|
|
SDL_zero(m_OverlayTextFields);
|
2018-07-16 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 03:56:53 +00:00
|
|
|
virtual ~VTRenderer() override
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
2019-05-12 01:33:12 +00:00
|
|
|
if (m_DisplayLink != nullptr) {
|
|
|
|
CVDisplayLinkStop(m_DisplayLink);
|
|
|
|
CVDisplayLinkRelease(m_DisplayLink);
|
|
|
|
}
|
|
|
|
|
2019-05-19 16:52:59 +00:00
|
|
|
if (m_VsyncPassed != nullptr) {
|
|
|
|
SDL_DestroyCond(m_VsyncPassed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_VsyncMutex != nullptr) {
|
|
|
|
SDL_DestroyMutex(m_VsyncMutex);
|
|
|
|
}
|
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
if (m_HwContext != nullptr) {
|
|
|
|
av_buffer_unref(&m_HwContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_FormatDesc != nullptr) {
|
|
|
|
CFRelease(m_FormatDesc);
|
|
|
|
}
|
2018-07-18 07:10:22 +00:00
|
|
|
|
2020-03-11 05:21:09 +00:00
|
|
|
if (m_ColorSpace != nullptr) {
|
|
|
|
CGColorSpaceRelease(m_ColorSpace);
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:56:53 +00:00
|
|
|
for (int i = 0; i < Overlay::OverlayMax; i++) {
|
|
|
|
if (m_OverlayTextFields[i] != nullptr) {
|
|
|
|
[m_OverlayTextFields[i] removeFromSuperview];
|
|
|
|
}
|
2019-02-14 04:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_StreamView != nullptr) {
|
|
|
|
[m_StreamView removeFromSuperview];
|
2018-07-21 02:37:54 +00:00
|
|
|
}
|
2018-07-18 07:10:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 01:33:12 +00:00
|
|
|
static
|
|
|
|
CVReturn
|
|
|
|
displayLinkOutputCallback(
|
|
|
|
CVDisplayLinkRef displayLink,
|
|
|
|
const CVTimeStamp* /* now */,
|
|
|
|
const CVTimeStamp* /* vsyncTime */,
|
|
|
|
CVOptionFlags,
|
|
|
|
CVOptionFlags*,
|
|
|
|
void *displayLinkContext)
|
|
|
|
{
|
|
|
|
auto me = reinterpret_cast<VTRenderer*>(displayLinkContext);
|
|
|
|
|
|
|
|
SDL_assert(displayLink == me->m_DisplayLink);
|
|
|
|
|
|
|
|
SDL_LockMutex(me->m_VsyncMutex);
|
|
|
|
SDL_CondSignal(me->m_VsyncPassed);
|
|
|
|
SDL_UnlockMutex(me->m_VsyncMutex);
|
|
|
|
|
|
|
|
return kCVReturnSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool initializeVsyncCallback(SDL_SysWMinfo* info)
|
|
|
|
{
|
|
|
|
NSScreen* screen = [info->info.cocoa.window screen];
|
|
|
|
CVReturn status;
|
|
|
|
if (screen == nullptr) {
|
|
|
|
// Window not visible on any display, so use a
|
|
|
|
// CVDisplayLink that can work with all active displays.
|
|
|
|
// When we become visible, we'll recreate ourselves
|
|
|
|
// and associate with the new screen.
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"NSWindow is not visible on any display");
|
|
|
|
status = CVDisplayLinkCreateWithActiveCGDisplays(&m_DisplayLink);
|
|
|
|
}
|
|
|
|
else {
|
2019-05-19 16:52:59 +00:00
|
|
|
CGDirectDisplayID displayId = [[screen deviceDescription][@"NSScreenNumber"] unsignedIntValue];
|
2019-05-12 01:33:12 +00:00
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"NSWindow on display: %x",
|
|
|
|
displayId);
|
|
|
|
status = CVDisplayLinkCreateWithCGDisplay(displayId, &m_DisplayLink);
|
|
|
|
}
|
|
|
|
if (status != kCVReturnSuccess) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to create CVDisplayLink: %d",
|
|
|
|
status);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = CVDisplayLinkSetOutputCallback(m_DisplayLink, displayLinkOutputCallback, this);
|
|
|
|
if (status != kCVReturnSuccess) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"CVDisplayLinkSetOutputCallback() failed: %d",
|
|
|
|
status);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-21 16:09:16 +00:00
|
|
|
// The CVDisplayLink callback uses these, so we must initialize them before
|
|
|
|
// starting the callbacks.
|
|
|
|
m_VsyncMutex = SDL_CreateMutex();
|
|
|
|
m_VsyncPassed = SDL_CreateCond();
|
|
|
|
|
2019-05-12 01:33:12 +00:00
|
|
|
status = CVDisplayLinkStart(m_DisplayLink);
|
|
|
|
if (status != kCVReturnSuccess) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"CVDisplayLinkStart() failed: %d",
|
|
|
|
status);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-08 03:08:43 +00:00
|
|
|
virtual void waitToRender() override
|
|
|
|
{
|
|
|
|
if (m_DisplayLink != nullptr) {
|
|
|
|
// Vsync is enabled, so wait for a swap before returning
|
|
|
|
SDL_LockMutex(m_VsyncMutex);
|
|
|
|
if (SDL_CondWaitTimeout(m_VsyncPassed, m_VsyncMutex, 100) == SDL_MUTEX_TIMEDOUT) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"V-sync wait timed out after 100 ms");
|
|
|
|
}
|
|
|
|
SDL_UnlockMutex(m_VsyncMutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
// Caller frees frame after we return
|
2019-02-16 03:31:01 +00:00
|
|
|
virtual void renderFrame(AVFrame* frame) override
|
2018-07-18 07:10:22 +00:00
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
CVPixelBufferRef pixBuf = reinterpret_cast<CVPixelBufferRef>(frame->data[3]);
|
|
|
|
|
2018-08-16 06:57:03 +00:00
|
|
|
if (m_DisplayLayer.status == AVQueuedSampleBufferRenderingStatusFailed) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Resetting failed AVSampleBufferDisplay layer");
|
2018-08-17 06:09:40 +00:00
|
|
|
|
|
|
|
// Trigger the main thread to recreate the decoder
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = SDL_RENDER_TARGETS_RESET;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
return;
|
2018-08-16 06:57:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 05:09:43 +00:00
|
|
|
// FFmpeg 5.0+ sets the CVPixelBuffer attachments properly now, so we don't have to
|
|
|
|
// fix them up ourselves (except CGColorSpace and PAR attachments).
|
2020-03-11 05:21:09 +00:00
|
|
|
|
2022-01-22 05:09:43 +00:00
|
|
|
// The VideoToolbox decoder attaches pixel aspect ratio information to the CVPixelBuffer
|
|
|
|
// which will rescale the video stream in accordance with the host display resolution
|
|
|
|
// to preserve the original aspect ratio of the host desktop. This behavior currently
|
|
|
|
// differs from the behavior of all other Moonlight Qt renderers, so we will strip
|
|
|
|
// these attachments for consistent behavior.
|
|
|
|
CVBufferRemoveAttachment(pixBuf, kCVImageBufferPixelAspectRatioKey);
|
2020-03-11 05:21:09 +00:00
|
|
|
|
2021-11-19 06:28:15 +00:00
|
|
|
// Reset m_ColorSpace if the colorspace changes. This can happen when
|
|
|
|
// a game enters HDR mode (Rec 601 -> Rec 2020).
|
|
|
|
if (frame->colorspace != m_LastAvColorSpace) {
|
|
|
|
if (m_ColorSpace != nullptr) {
|
|
|
|
CGColorSpaceRelease(m_ColorSpace);
|
|
|
|
m_ColorSpace = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-01-22 05:09:43 +00:00
|
|
|
switch (frame->colorspace) {
|
|
|
|
case AVCOL_SPC_BT709:
|
2020-03-11 05:21:09 +00:00
|
|
|
m_ColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_709);
|
2022-01-22 05:09:43 +00:00
|
|
|
break;
|
|
|
|
case AVCOL_SPC_BT2020_NCL:
|
2020-03-11 05:21:09 +00:00
|
|
|
m_ColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
|
2022-01-22 05:09:43 +00:00
|
|
|
break;
|
|
|
|
case AVCOL_SPC_SMPTE170M:
|
2020-03-11 06:21:00 +00:00
|
|
|
m_ColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
2022-01-22 05:09:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2020-03-11 05:21:09 +00:00
|
|
|
}
|
2022-01-22 05:09:43 +00:00
|
|
|
|
|
|
|
m_LastAvColorSpace = frame->colorspace;
|
2020-03-11 05:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ColorSpace != nullptr) {
|
|
|
|
CVBufferSetAttachment(pixBuf, kCVImageBufferCGColorSpaceKey, m_ColorSpace, kCVAttachmentMode_ShouldPropagate);
|
|
|
|
}
|
|
|
|
|
2018-07-18 07:10:22 +00:00
|
|
|
// If the format has changed or doesn't exist yet, construct it with the
|
|
|
|
// pixel buffer data
|
|
|
|
if (!m_FormatDesc || !CMVideoFormatDescriptionMatchesImageBuffer(m_FormatDesc, pixBuf)) {
|
|
|
|
if (m_FormatDesc != nullptr) {
|
|
|
|
CFRelease(m_FormatDesc);
|
|
|
|
}
|
|
|
|
status = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault,
|
|
|
|
pixBuf, &m_FormatDesc);
|
|
|
|
if (status != noErr) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"CMVideoFormatDescriptionCreateForImageBuffer() failed: %d",
|
|
|
|
status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue this sample for the next v-sync
|
|
|
|
CMSampleTimingInfo timingInfo = {
|
|
|
|
.duration = kCMTimeInvalid,
|
2021-03-03 00:14:15 +00:00
|
|
|
.presentationTimeStamp = CMClockMakeHostTimeFromSystemUnits(mach_absolute_time()),
|
2018-07-18 07:10:22 +00:00
|
|
|
.decodeTimeStamp = kCMTimeInvalid,
|
|
|
|
};
|
|
|
|
|
|
|
|
CMSampleBufferRef sampleBuffer;
|
|
|
|
status = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault,
|
|
|
|
pixBuf,
|
|
|
|
m_FormatDesc,
|
|
|
|
&timingInfo,
|
|
|
|
&sampleBuffer);
|
|
|
|
if (status != noErr) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"CMSampleBufferCreateReadyWithImageBuffer() failed: %d",
|
|
|
|
status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[m_DisplayLayer enqueueSampleBuffer:sampleBuffer];
|
|
|
|
|
|
|
|
CFRelease(sampleBuffer);
|
|
|
|
}
|
2018-07-16 01:11:57 +00:00
|
|
|
|
2019-04-12 05:27:20 +00:00
|
|
|
virtual bool initialize(PDECODER_PARAMETERS params) override
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2019-04-12 05:27:20 +00:00
|
|
|
if (params->videoFormat & VIDEO_FORMAT_MASK_H264) {
|
2022-01-07 05:05:18 +00:00
|
|
|
if (!VTIsHardwareDecodeSupported(kCMVideoCodecType_H264)) {
|
2018-07-16 01:11:57 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-07 05:05:18 +00:00
|
|
|
"No HW accelerated H.264 decode via VT");
|
|
|
|
return false;
|
2018-07-16 01:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 05:27:20 +00:00
|
|
|
else if (params->videoFormat & VIDEO_FORMAT_MASK_H265) {
|
2022-01-07 05:05:18 +00:00
|
|
|
if (!VTIsHardwareDecodeSupported(kCMVideoCodecType_HEVC)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HW accelerated HEVC decode via VT");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HEVC Main10 requires more extensive checks because there's no
|
|
|
|
// simple API to check for Main10 hardware decoding, and if we don't
|
|
|
|
// have it, we'll silently get software decoding with horrible performance.
|
|
|
|
if (params->videoFormat == VIDEO_FORMAT_H265_MAIN10) {
|
|
|
|
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
|
|
|
|
if (device == nullptr) {
|
2018-07-16 01:11:57 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-07 05:05:18 +00:00
|
|
|
"Unable to get default Metal device");
|
2018-07-16 01:11:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 02:36:43 +00:00
|
|
|
|
2022-01-07 05:05:18 +00:00
|
|
|
// Exclude all GPUs earlier than macOSGPUFamily2
|
|
|
|
// https://developer.apple.com/documentation/metal/mtlfeatureset/mtlfeatureset_macos_gpufamily2_v1
|
|
|
|
if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily2_v1]) {
|
|
|
|
if ([device.name containsString:@"Intel"]) {
|
|
|
|
// 500-series Intel GPUs are Skylake and don't support Main10 hardware decoding
|
|
|
|
if ([device.name containsString:@" 5"]) {
|
2019-11-06 02:36:43 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-07 05:05:18 +00:00
|
|
|
"No HEVC Main10 support on Skylake iGPU");
|
|
|
|
[device release];
|
2019-11-06 02:36:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 05:05:18 +00:00
|
|
|
else if ([device.name containsString:@"AMD"]) {
|
|
|
|
// FirePro D, M200, and M300 series GPUs don't support Main10 hardware decoding
|
|
|
|
if ([device.name containsString:@"FirePro D"] ||
|
|
|
|
[device.name containsString:@" M2"] ||
|
|
|
|
[device.name containsString:@" M3"]) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HEVC Main10 support on AMD GPUs until Polaris");
|
|
|
|
[device release];
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 02:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-12 03:00:34 +00:00
|
|
|
else {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HEVC Main10 support on macOS GPUFamily1 GPUs");
|
|
|
|
[device release];
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-07 05:05:18 +00:00
|
|
|
|
|
|
|
[device release];
|
2018-07-16 01:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SysWMinfo info;
|
|
|
|
|
|
|
|
SDL_VERSION(&info.version);
|
|
|
|
|
2019-04-12 05:27:20 +00:00
|
|
|
if (!SDL_GetWindowWMInfo(params->window, &info)) {
|
2018-07-16 01:11:57 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_GetWindowWMInfo() failed: %s",
|
|
|
|
SDL_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_assert(info.subsystem == SDL_SYSWM_COCOA);
|
|
|
|
|
|
|
|
// SDL adds its own content view to listen for events.
|
|
|
|
// We need to add a subview for our display layer.
|
|
|
|
NSView* contentView = info.info.cocoa.window.contentView;
|
2020-05-07 05:17:22 +00:00
|
|
|
m_StreamView = [[VTView alloc] initWithFrame:contentView.bounds];
|
2018-09-23 01:34:15 +00:00
|
|
|
|
|
|
|
m_DisplayLayer = [[AVSampleBufferDisplayLayer alloc] init];
|
2019-02-14 04:55:52 +00:00
|
|
|
m_DisplayLayer.bounds = m_StreamView.bounds;
|
|
|
|
m_DisplayLayer.position = CGPointMake(CGRectGetMidX(m_StreamView.bounds), CGRectGetMidY(m_StreamView.bounds));
|
2018-09-23 01:34:15 +00:00
|
|
|
m_DisplayLayer.videoGravity = AVLayerVideoGravityResizeAspect;
|
2022-01-15 20:14:31 +00:00
|
|
|
m_DisplayLayer.opaque = YES;
|
|
|
|
|
2022-01-23 02:25:50 +00:00
|
|
|
// This workaround prevents the image from going through processing that causes some
|
|
|
|
// color artifacts in some cases. HDR seems to be okay without this, so we'll exclude
|
|
|
|
// it out of caution. The artifacts seem to be far more significant on M1 Macs and
|
|
|
|
// the workaround can cause performance regressions on Intel Macs, so only use this
|
|
|
|
// on Apple silicon.
|
|
|
|
//
|
|
|
|
// https://github.com/moonlight-stream/moonlight-qt/issues/493
|
|
|
|
// https://github.com/moonlight-stream/moonlight-qt/issues/722
|
2022-06-26 21:22:05 +00:00
|
|
|
if (!(params->videoFormat & VIDEO_FORMAT_MASK_10BIT)) {
|
2022-01-23 02:25:50 +00:00
|
|
|
int err;
|
|
|
|
uint32_t cpuType;
|
|
|
|
size_t size = sizeof(cpuType);
|
|
|
|
|
2022-01-23 19:01:20 +00:00
|
|
|
// Apple Silicon Macs have CPU_ARCH_ABI64 set, so we need to mask that off.
|
|
|
|
// For some reason, 64-bit Intel Macs don't seem to have CPU_ARCH_ABI64 set.
|
2022-01-23 02:25:50 +00:00
|
|
|
err = sysctlbyname("hw.cputype", &cpuType, &size, NULL, 0);
|
2022-01-23 19:01:20 +00:00
|
|
|
if (err == 0 && (cpuType & ~CPU_ARCH_MASK) == CPU_TYPE_ARM) {
|
2022-01-23 02:25:50 +00:00
|
|
|
if (info.info.cocoa.window.screen != nullptr) {
|
|
|
|
m_DisplayLayer.shouldRasterize = YES;
|
|
|
|
m_DisplayLayer.rasterizationScale = info.info.cocoa.window.screen.backingScaleFactor;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Unable to rasterize layer due to missing NSScreen");
|
|
|
|
SDL_assert(false);
|
|
|
|
}
|
2022-01-15 20:14:31 +00:00
|
|
|
}
|
2022-01-23 02:25:50 +00:00
|
|
|
else if (err != 0) {
|
2022-01-15 20:14:31 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-23 02:25:50 +00:00
|
|
|
"sysctlbyname(hw.cputype) failed: %d", err);
|
2022-01-15 20:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-23 01:34:15 +00:00
|
|
|
|
|
|
|
// Create a layer-hosted view by setting the layer before wantsLayer
|
|
|
|
// This avoids us having to add our AVSampleBufferDisplayLayer as a
|
|
|
|
// sublayer of a layer-backed view which leaves a useless layer in
|
|
|
|
// the middle.
|
2019-02-14 04:55:52 +00:00
|
|
|
m_StreamView.layer = m_DisplayLayer;
|
|
|
|
m_StreamView.wantsLayer = YES;
|
2018-07-16 01:11:57 +00:00
|
|
|
|
2019-02-14 04:55:52 +00:00
|
|
|
[contentView addSubview: m_StreamView];
|
2018-07-16 01:11:57 +00:00
|
|
|
|
|
|
|
err = av_hwdevice_ctx_create(&m_HwContext,
|
|
|
|
AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
if (err < 0) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"av_hwdevice_ctx_create() failed for VT decoder: %d",
|
|
|
|
err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-30 02:26:56 +00:00
|
|
|
if (params->enableFramePacing) {
|
2019-05-12 01:33:12 +00:00
|
|
|
if (!initializeVsyncCallback(&info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:56:53 +00:00
|
|
|
void updateOverlayOnMainThread(Overlay::OverlayType type)
|
2019-02-14 04:55:52 +00:00
|
|
|
{
|
|
|
|
// Lazy initialization for the overlay
|
2019-03-19 03:56:53 +00:00
|
|
|
if (m_OverlayTextFields[type] == nullptr) {
|
|
|
|
m_OverlayTextFields[type] = [[NSTextField alloc] initWithFrame:m_StreamView.bounds];
|
|
|
|
[m_OverlayTextFields[type] setBezeled:NO];
|
|
|
|
[m_OverlayTextFields[type] setDrawsBackground:NO];
|
|
|
|
[m_OverlayTextFields[type] setEditable:NO];
|
|
|
|
[m_OverlayTextFields[type] setSelectable:NO];
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Overlay::OverlayDebug:
|
2020-02-29 23:54:10 +00:00
|
|
|
[m_OverlayTextFields[type] setAlignment:NSTextAlignmentLeft];
|
2019-03-19 03:56:53 +00:00
|
|
|
break;
|
|
|
|
case Overlay::OverlayStatusUpdate:
|
2020-02-29 23:54:10 +00:00
|
|
|
[m_OverlayTextFields[type] setAlignment:NSTextAlignmentRight];
|
2019-03-19 03:56:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Color color = Session::get()->getOverlayManager().getOverlayColor(type);
|
|
|
|
[m_OverlayTextFields[type] setTextColor:[NSColor colorWithSRGBRed:color.r / 255.0 green:color.g / 255.0 blue:color.b / 255.0 alpha:color.a / 255.0]];
|
|
|
|
[m_OverlayTextFields[type] setFont:[NSFont messageFontOfSize:Session::get()->getOverlayManager().getOverlayFontSize(type)]];
|
|
|
|
|
|
|
|
[m_StreamView addSubview: m_OverlayTextFields[type]];
|
2019-02-14 04:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update text contents
|
2019-03-19 03:56:53 +00:00
|
|
|
[m_OverlayTextFields[type] setStringValue: [NSString stringWithUTF8String:Session::get()->getOverlayManager().getOverlayText(type)]];
|
2019-02-14 04:55:52 +00:00
|
|
|
|
|
|
|
// Unhide if it's enabled
|
2019-03-19 03:56:53 +00:00
|
|
|
[m_OverlayTextFields[type] setHidden: !Session::get()->getOverlayManager().isOverlayEnabled(type)];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void updateDebugOverlayOnMainThread(void* context)
|
|
|
|
{
|
|
|
|
VTRenderer* me = (VTRenderer*)context;
|
|
|
|
|
|
|
|
me->updateOverlayOnMainThread(Overlay::OverlayDebug);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void updateStatusOverlayOnMainThread(void* context)
|
|
|
|
{
|
|
|
|
VTRenderer* me = (VTRenderer*)context;
|
|
|
|
|
|
|
|
me->updateOverlayOnMainThread(Overlay::OverlayStatusUpdate);
|
2019-02-14 04:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void notifyOverlayUpdated(Overlay::OverlayType type) override
|
|
|
|
{
|
|
|
|
// We must do the actual UI updates on the main thread, so queue an
|
|
|
|
// async callback on the main thread via GCD to do the UI update.
|
2019-03-19 03:56:53 +00:00
|
|
|
switch (type) {
|
|
|
|
case Overlay::OverlayDebug:
|
2019-02-14 04:55:52 +00:00
|
|
|
dispatch_async_f(dispatch_get_main_queue(), this, updateDebugOverlayOnMainThread);
|
2019-03-19 03:56:53 +00:00
|
|
|
break;
|
|
|
|
case Overlay::OverlayStatusUpdate:
|
|
|
|
dispatch_async_f(dispatch_get_main_queue(), this, updateStatusOverlayOnMainThread);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-02-14 04:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 01:47:26 +00:00
|
|
|
virtual bool prepareDecoderContext(AVCodecContext* context, AVDictionary**) override
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
context->hw_device_ctx = av_buffer_ref(m_HwContext);
|
2018-07-28 23:06:26 +00:00
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Using VideoToolbox accelerated renderer");
|
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-19 07:59:04 +00:00
|
|
|
virtual bool needsTestFrame() override
|
|
|
|
{
|
2019-02-16 03:10:21 +00:00
|
|
|
// We used to trust VT to tell us whether decode will work, but
|
|
|
|
// there are cases where it can lie because the hardware technically
|
|
|
|
// can decode the format but VT is unserviceable for some other reason.
|
|
|
|
// Decoding the test frame will tell us for sure whether it will work.
|
|
|
|
return true;
|
2018-08-19 07:59:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 05:21:09 +00:00
|
|
|
int getDecoderColorspace() override
|
|
|
|
{
|
2020-03-11 06:21:00 +00:00
|
|
|
// macOS seems to handle Rec 601 best
|
|
|
|
return COLORSPACE_REC_601;
|
2020-03-11 05:21:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 06:59:04 +00:00
|
|
|
int getRendererAttributes() override
|
|
|
|
{
|
|
|
|
// AVSampleBufferDisplayLayer supports HDR output
|
|
|
|
return RENDERER_ATTRIBUTE_HDR_SUPPORT;
|
|
|
|
}
|
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
private:
|
|
|
|
AVBufferRef* m_HwContext;
|
|
|
|
AVSampleBufferDisplayLayer* m_DisplayLayer;
|
|
|
|
CMVideoFormatDescriptionRef m_FormatDesc;
|
2019-02-14 04:55:52 +00:00
|
|
|
NSView* m_StreamView;
|
2019-03-19 03:56:53 +00:00
|
|
|
NSTextField* m_OverlayTextFields[Overlay::OverlayMax];
|
2019-05-12 01:33:12 +00:00
|
|
|
CVDisplayLinkRef m_DisplayLink;
|
2021-11-19 06:28:15 +00:00
|
|
|
AVColorSpace m_LastAvColorSpace;
|
2020-03-11 05:21:09 +00:00
|
|
|
CGColorSpaceRef m_ColorSpace;
|
2019-05-12 01:33:12 +00:00
|
|
|
SDL_mutex* m_VsyncMutex;
|
|
|
|
SDL_cond* m_VsyncPassed;
|
2018-07-16 01:11:57 +00:00
|
|
|
};
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
IFFmpegRenderer* VTRendererFactory::createRenderer() {
|
2018-07-16 01:11:57 +00:00
|
|
|
return new VTRenderer();
|
|
|
|
}
|