diff --git a/cmake/compile_definitions/macos.cmake b/cmake/compile_definitions/macos.cmake index dbca9df9073..f2b6499efee 100644 --- a/cmake/compile_definitions/macos.cmake +++ b/cmake/compile_definitions/macos.cmake @@ -35,6 +35,7 @@ list(APPEND SUNSHINE_EXTERNAL_LIBRARIES ${CORE_MEDIA_LIBRARY} ${CORE_VIDEO_LIBRARY} ${FOUNDATION_LIBRARY} + ${SCREEN_CAPTURE_KIT_LIBRARY} ${VIDEO_TOOLBOX_LIBRARY}) set(APPLE_PLIST_TEMPLATE "${SUNSHINE_SOURCE_ASSETS_DIR}/macos/build/Info.plist.in") @@ -45,8 +46,6 @@ set(PLATFORM_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/platform/macos/av_audio.h" "${CMAKE_SOURCE_DIR}/src/platform/macos/av_audio.mm" "${CMAKE_SOURCE_DIR}/src/platform/macos/av_img_t.h" - "${CMAKE_SOURCE_DIR}/src/platform/macos/av_video.h" - "${CMAKE_SOURCE_DIR}/src/platform/macos/av_video.m" "${CMAKE_SOURCE_DIR}/src/platform/macos/display.mm" "${CMAKE_SOURCE_DIR}/src/platform/macos/input.cpp" "${CMAKE_SOURCE_DIR}/src/platform/macos/microphone.mm" @@ -55,6 +54,16 @@ set(PLATFORM_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/platform/macos/nv12_zero_device.cpp" "${CMAKE_SOURCE_DIR}/src/platform/macos/nv12_zero_device.h" "${CMAKE_SOURCE_DIR}/src/platform/macos/publish.cpp" + "${CMAKE_SOURCE_DIR}/src/platform/macos/sc_video.h" + "${CMAKE_SOURCE_DIR}/src/platform/macos/sc_video.m" "${CMAKE_SOURCE_DIR}/third-party/TPCircularBuffer/TPCircularBuffer.c" "${CMAKE_SOURCE_DIR}/third-party/TPCircularBuffer/TPCircularBuffer.h" ${APPLE_PLIST_FILE}) + +# sc_video.m is written against ARC for clarity (SCK APIs are async/ +# block-heavy and benefit from ARC). The rest of the macOS Obj-C +# sources remain MRC; objects flowing across the boundary follow the +# standard +1-retain alloc/init convention so both modes interoperate. +set_source_files_properties( + "${CMAKE_SOURCE_DIR}/src/platform/macos/sc_video.m" + PROPERTIES COMPILE_FLAGS "-fobjc-arc") diff --git a/cmake/dependencies/macos.cmake b/cmake/dependencies/macos.cmake index 5e225fdac21..846bf78d176 100644 --- a/cmake/dependencies/macos.cmake +++ b/cmake/dependencies/macos.cmake @@ -10,6 +10,14 @@ FIND_LIBRARY(CORE_MEDIA_LIBRARY CoreMedia) FIND_LIBRARY(CORE_VIDEO_LIBRARY CoreVideo) FIND_LIBRARY(FOUNDATION_LIBRARY Foundation) FIND_LIBRARY(VIDEO_TOOLBOX_LIBRARY VideoToolbox) +# ScreenCaptureKit is the modern (macOS 12.3+) replacement for the +# deprecated AVCaptureScreenInput-based capture path. Sunshine's +# sc_video.{h,m} is unconditionally compiled into the macOS target; +# fail configure with a clear message rather than failing the build +# later on header lookup when the SDK doesn't ship the framework +# (e.g., when building with an Xcode older than 13.3 / SDK older than +# 12.3, which dropped out of routine compatibility long ago). +FIND_LIBRARY(SCREEN_CAPTURE_KIT_LIBRARY ScreenCaptureKit REQUIRED) if(SUNSHINE_ENABLE_TRAY) FIND_LIBRARY(COCOA Cocoa REQUIRED) diff --git a/src/platform/macos/av_video.h b/src/platform/macos/av_video.h deleted file mode 100644 index b2fa5d4b255..00000000000 --- a/src/platform/macos/av_video.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * @file src/platform/macos/av_video.h - * @brief Declarations for video capture on macOS. - */ -#pragma once - -// platform includes -#import -#import - -struct CaptureSession { - AVCaptureVideoDataOutput *output; - NSCondition *captureStopped; -}; - -static const int kMaxDisplays = 32; - -@interface AVVideo: NSObject - -@property (nonatomic, assign) CGDirectDisplayID displayID; -@property (nonatomic, assign) CMTime minFrameDuration; -@property (nonatomic, assign) OSType pixelFormat; -@property (nonatomic, assign) int frameWidth; -@property (nonatomic, assign) int frameHeight; - -typedef bool (^FrameCallbackBlock)(CMSampleBufferRef); - -@property (nonatomic, assign) AVCaptureSession *session; -@property (nonatomic, assign) NSMapTable *videoOutputs; -@property (nonatomic, assign) NSMapTable *captureCallbacks; -@property (nonatomic, assign) NSMapTable *captureSignals; - -+ (NSArray *)displayNames; -+ (NSString *)getDisplayName:(CGDirectDisplayID)displayID; - -- (id)initWithDisplay:(CGDirectDisplayID)displayID frameRate:(int)frameRate; - -- (void)setFrameWidth:(int)frameWidth frameHeight:(int)frameHeight; -- (dispatch_semaphore_t)capture:(FrameCallbackBlock)frameCallback; - -@end diff --git a/src/platform/macos/av_video.m b/src/platform/macos/av_video.m deleted file mode 100644 index 630d7101598..00000000000 --- a/src/platform/macos/av_video.m +++ /dev/null @@ -1,146 +0,0 @@ -/** - * @file src/platform/macos/av_video.m - * @brief Definitions for video capture on macOS. - */ -// local includes -#import "av_video.h" - -@implementation AVVideo - -// XXX: Currently, this function only returns the screen IDs as names, -// which is not very helpful to the user. The API to retrieve names -// was deprecated with 10.9+. -// However, there is a solution with little external code that can be used: -// https://stackoverflow.com/questions/20025868/cgdisplayioserviceport-is-deprecated-in-os-x-10-9-how-to-replace -+ (NSArray *)displayNames { - CGDirectDisplayID displays[kMaxDisplays]; - uint32_t count; - if (CGGetActiveDisplayList(kMaxDisplays, displays, &count) != kCGErrorSuccess) { - return [NSArray array]; - } - - NSMutableArray *result = [NSMutableArray array]; - - for (uint32_t i = 0; i < count; i++) { - [result addObject:@{ - @"id": [NSNumber numberWithUnsignedInt:displays[i]], - @"name": [NSString stringWithFormat:@"%d", displays[i]], - @"displayName": [self getDisplayName:displays[i]], - }]; - } - - return [NSArray arrayWithArray:result]; -} - -+ (NSString *)getDisplayName:(CGDirectDisplayID)displayID { - for (NSScreen *screen in [NSScreen screens]) { - if ([screen.deviceDescription[@"NSScreenNumber"] isEqualToNumber:[NSNumber numberWithUnsignedInt:displayID]]) { - return screen.localizedName; - } - } - return nil; -} - -- (id)initWithDisplay:(CGDirectDisplayID)displayID frameRate:(int)frameRate { - self = [super init]; - - CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayID); - - self.displayID = displayID; - self.pixelFormat = kCVPixelFormatType_32BGRA; - self.frameWidth = (int) CGDisplayModeGetPixelWidth(mode); - self.frameHeight = (int) CGDisplayModeGetPixelHeight(mode); - self.minFrameDuration = CMTimeMake(1, frameRate); - self.session = [[AVCaptureSession alloc] init]; - self.videoOutputs = [[NSMapTable alloc] init]; - self.captureCallbacks = [[NSMapTable alloc] init]; - self.captureSignals = [[NSMapTable alloc] init]; - - CFRelease(mode); - - AVCaptureScreenInput *screenInput = [[AVCaptureScreenInput alloc] initWithDisplayID:self.displayID]; - [screenInput setMinFrameDuration:self.minFrameDuration]; - - if ([self.session canAddInput:screenInput]) { - [self.session addInput:screenInput]; - } else { - [screenInput release]; - return nil; - } - - [self.session startRunning]; - - return self; -} - -- (void)dealloc { - [self.videoOutputs release]; - [self.captureCallbacks release]; - [self.captureSignals release]; - [self.session stopRunning]; - [super dealloc]; -} - -- (void)setFrameWidth:(int)frameWidth frameHeight:(int)frameHeight { - self.frameWidth = frameWidth; - self.frameHeight = frameHeight; -} - -- (dispatch_semaphore_t)capture:(FrameCallbackBlock)frameCallback { - @synchronized(self) { - AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init]; - - [videoOutput setVideoSettings:@{ - (NSString *) kCVPixelBufferPixelFormatTypeKey: [NSNumber numberWithUnsignedInt:self.pixelFormat], - (NSString *) kCVPixelBufferWidthKey: [NSNumber numberWithInt:self.frameWidth], - (NSString *) kCVPixelBufferHeightKey: [NSNumber numberWithInt:self.frameHeight], - (NSString *) AVVideoScalingModeKey: AVVideoScalingModeResizeAspect, - }]; - - dispatch_queue_attr_t qos = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, DISPATCH_QUEUE_PRIORITY_HIGH); - dispatch_queue_t recordingQueue = dispatch_queue_create("videoCaptureQueue", qos); - [videoOutput setSampleBufferDelegate:self queue:recordingQueue]; - - [self.session stopRunning]; - - if ([self.session canAddOutput:videoOutput]) { - [self.session addOutput:videoOutput]; - } else { - [videoOutput release]; - return nil; - } - - AVCaptureConnection *videoConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo]; - dispatch_semaphore_t signal = dispatch_semaphore_create(0); - - [self.videoOutputs setObject:videoOutput forKey:videoConnection]; - [self.captureCallbacks setObject:frameCallback forKey:videoConnection]; - [self.captureSignals setObject:signal forKey:videoConnection]; - - [self.session startRunning]; - - return signal; - } -} - -- (void)captureOutput:(AVCaptureOutput *)captureOutput - didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer - fromConnection:(AVCaptureConnection *)connection { - FrameCallbackBlock callback = [self.captureCallbacks objectForKey:connection]; - - if (callback != nil) { - if (!callback(sampleBuffer)) { - @synchronized(self) { - [self.session stopRunning]; - [self.captureCallbacks removeObjectForKey:connection]; - [self.session removeOutput:[self.videoOutputs objectForKey:connection]]; - [self.videoOutputs removeObjectForKey:connection]; - dispatch_semaphore_signal([self.captureSignals objectForKey:connection]); - [self.captureSignals removeObjectForKey:connection]; - [self.session startRunning]; - } - } - } -} - -@end diff --git a/src/platform/macos/display.mm b/src/platform/macos/display.mm index be124b2d331..45bf97e46b2 100644 --- a/src/platform/macos/display.mm +++ b/src/platform/macos/display.mm @@ -7,9 +7,9 @@ #include "src/logging.h" #include "src/platform/common.h" #include "src/platform/macos/av_img_t.h" -#include "src/platform/macos/av_video.h" #include "src/platform/macos/misc.h" #include "src/platform/macos/nv12_zero_device.h" +#include "src/platform/macos/sc_video.h" // Avoid conflict between AVFoundation and libavutil both defining AVMediaType #define AVMediaType AVMediaType_FFmpeg @@ -22,7 +22,7 @@ using namespace std::literals; struct av_display_t: public display_t { - AVVideo *av_capture {}; + SCVideo *av_capture {}; CGDirectDisplayID display_id {}; ~av_display_t() override { @@ -86,7 +86,7 @@ capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const } else if (pix_fmt == pix_fmt_e::nv12 || pix_fmt == pix_fmt_e::p010) { auto device = std::make_unique(); - device->init(static_cast(av_capture), pix_fmt, setResolution, setPixelFormat); + device->init((void *) av_capture, pix_fmt, setResolution, setPixelFormat); return device; } else { @@ -143,11 +143,11 @@ int dummy_img(img_t *img) override { * height --> the intended capture height */ static void setResolution(void *display, int width, int height) { - [static_cast(display) setFrameWidth:width frameHeight:height]; + [(SCVideo *) display setFrameWidth:width frameHeight:height]; } static void setPixelFormat(void *display, OSType pixelFormat) { - static_cast(display).pixelFormat = pixelFormat; + ((SCVideo *) display).pixelFormat = pixelFormat; } }; @@ -163,7 +163,7 @@ static void setPixelFormat(void *display, OSType pixelFormat) { display->display_id = CGMainDisplayID(); // Print all displays available with it's name and id - auto display_array = [AVVideo displayNames]; + auto display_array = [SCVideo displayNames]; BOOST_LOG(info) << "Detecting displays"sv; for (NSDictionary *item in display_array) { NSNumber *display_id = item[@"id"]; @@ -177,7 +177,19 @@ static void setPixelFormat(void *display, OSType pixelFormat) { } BOOST_LOG(info) << "Configuring selected display ("sv << display->display_id << ") to stream"sv; - display->av_capture = [[AVVideo alloc] initWithDisplay:display->display_id frameRate:config.framerate]; + // ScreenCaptureKit is the only capture backend Sunshine ships on macOS; + // the deployment target (14.2) is well above SCK's minimum (12.3) so + // there is no @available branch and no legacy AVCaptureScreenInput + // fallback to maintain. + // + // hdrAllowed reflects the negotiated `enable_hdr` for this session + // (rtsp.cpp maps `x-nv-video[0].dynamicRangeMode` into config.dynamicRange). + // SCK uses this together with the chosen pixel format depth to decide + // whether to flip captureDynamicRange to HDRLocalDisplay; neither + // condition alone is sufficient. See sc_video.m::applyDynamicRangeForPixelFormat:. + const BOOL hdr_allowed = config.dynamicRange ? YES : NO; + BOOST_LOG(info) << "Using ScreenCaptureKit capture backend (HDR "sv << (hdr_allowed ? "allowed" : "blocked") << ")"sv; + display->av_capture = [[SCVideo alloc] initWithDisplay:display->display_id frameRate:config.framerate hdrAllowed:hdr_allowed]; if (!display->av_capture) { BOOST_LOG(error) << "Video setup failed."sv; @@ -196,7 +208,7 @@ static void setPixelFormat(void *display, OSType pixelFormat) { std::vector display_names(mem_type_e hwdevice_type) { __block std::vector display_names; - auto display_array = [AVVideo displayNames]; + auto display_array = [SCVideo displayNames]; display_names.reserve([display_array count]); [display_array enumerateObjectsUsingBlock:^(NSDictionary *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) { diff --git a/src/platform/macos/sc_video.h b/src/platform/macos/sc_video.h new file mode 100644 index 00000000000..37831570b93 --- /dev/null +++ b/src/platform/macos/sc_video.h @@ -0,0 +1,47 @@ +/** + * @file src/platform/macos/sc_video.h + * @brief Declarations for ScreenCaptureKit-based video capture on macOS. + * + * SCVideo is now Sunshine's only macOS capture backend. The deployment + * target (MACOSX_DEPLOYMENT_TARGET=14.2) is well above the macOS 12.3 + * minimum where ScreenCaptureKit became available, so the legacy + * AVCaptureScreenInput-based AVVideo path has been removed entirely. + */ +#pragma once + +#import +#import +#import + +// Block signature used to deliver captured sample buffers back to the +// platform-agnostic capture loop. Returning NO from the block stops +// further deliveries on this capture session. +typedef bool (^FrameCallbackBlock)(CMSampleBufferRef); + +@interface SCVideo : NSObject + +@property (nonatomic, assign) CGDirectDisplayID displayID; +@property (nonatomic, assign) CMTime minFrameDuration; +@property (nonatomic, assign) OSType pixelFormat; +@property (nonatomic, assign) int frameWidth; +@property (nonatomic, assign) int frameHeight; + +// YES iff the negotiated streaming session enabled HDR (Moonlight's +// hdrMode flag). Required (in combination with a 10-bit pixel format) +// before SCK is allowed to flip captureDynamicRange to HDRLocalDisplay +// on macOS 14+. Defaults to NO; the SDR capture path is always safe. +@property (nonatomic, assign) BOOL hdrAllowed; + +- (instancetype)initWithDisplay:(CGDirectDisplayID)displayID frameRate:(int)frameRate; +- (instancetype)initWithDisplay:(CGDirectDisplayID)displayID frameRate:(int)frameRate hdrAllowed:(BOOL)hdrAllowed; + +- (void)setFrameWidth:(int)frameWidth frameHeight:(int)frameHeight; +- (dispatch_semaphore_t)capture:(FrameCallbackBlock)frameCallback; + +// Enumerate the currently-active CGDisplays as an array of dictionaries +// with keys @"id" (NSNumber, the CGDirectDisplayID), @"name" (NSString, +// the numeric id as a string for legacy callers), and @"displayName" +// (NSString, the user-facing name from NSScreen.localizedName). ++ (NSArray *)displayNames; + +@end diff --git a/src/platform/macos/sc_video.m b/src/platform/macos/sc_video.m new file mode 100644 index 00000000000..eab24a328da --- /dev/null +++ b/src/platform/macos/sc_video.m @@ -0,0 +1,461 @@ +/** + * @file src/platform/macos/sc_video.m + * @brief ScreenCaptureKit-based video capture. Sole macOS capture + * backend; Sunshine's deployment target (14.2) is well above the SCK + * minimum (12.3) so the legacy AVCaptureScreenInput-based AVVideo + * implementation has been retired. + * + * Lifecycle: the underlying SCStream is started exactly once during + * -initWithDisplay:frameRate: and stopped exactly once during -dealloc. + * -capture: only swaps the active callback / signal; it never touches + * the stream lifecycle. This avoids the "addStreamOutput called twice" + * failure mode that SCK exhibits when an output is re-registered on a + * stream that already retains it across stop/start cycles. + * + * Compiled with ARC (-fobjc-arc) for clarity. The other macOS capture + * files remain MRC; objects flowing from this file to display.mm + * follow the standard alloc/init +1-retain convention so the boundary + * works regardless of compile mode on the other side. + */ +#import "sc_video.h" + +#import + +// Bounded wait for any SCK completion handler. SCK should always +// invoke these, but a misbehaving system service must not hang the +// whole startup path. +static const int64_t kSCVideoCompletionTimeoutSec = 5; + +API_AVAILABLE(macos(12.3)) +@interface SCVideo () + +@property (nonatomic, strong) SCStream *stream; +@property (nonatomic, strong) SCContentFilter *filter; +@property (nonatomic, strong) SCStreamConfiguration *streamConfig; +@property (nonatomic, strong) dispatch_queue_t sampleQueue; + +// All four of the following are mutated from multiple threads (the +// caller of -capture:, the SCK sample-handler queue, and the SCStream +// delegate's didStopWithError:) and so are only ever accessed under +// @synchronized(self). +@property (nonatomic, copy) FrameCallbackBlock currentCallback; +@property (nonatomic, strong) dispatch_semaphore_t currentSignal; +@property (nonatomic, assign) BOOL streamRunning; + +@end + +@implementation SCVideo + +- (instancetype)initWithDisplay:(CGDirectDisplayID)displayID frameRate:(int)frameRate { + return [self initWithDisplay:displayID frameRate:frameRate hdrAllowed:NO]; +} + +- (instancetype)initWithDisplay:(CGDirectDisplayID)displayID frameRate:(int)frameRate hdrAllowed:(BOOL)hdrAllowed { + self = [super init]; + if (!self) { + return nil; + } + + self.displayID = displayID; + self.minFrameDuration = CMTimeMake(1, frameRate); + self.pixelFormat = kCVPixelFormatType_32BGRA; + self.hdrAllowed = hdrAllowed; + + // Prefer the active display mode's pixel dimensions; fall back to + // CGDisplayBounds if no mode is currently set (e.g., during display + // reconfiguration). If both fail we still proceed — SCK will + // accept the requested SCContentFilter dimensions later. + CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayID); + if (mode) { + self.frameWidth = (int) CGDisplayModeGetPixelWidth(mode); + self.frameHeight = (int) CGDisplayModeGetPixelHeight(mode); + CGDisplayModeRelease(mode); + } else { + CGRect bounds = CGDisplayBounds(displayID); + self.frameWidth = (int) CGRectGetWidth(bounds); + self.frameHeight = (int) CGRectGetHeight(bounds); + } + + // dispatch_queue_attr_make_with_qos_class's third parameter is a + // relative priority (range -15..0), NOT one of the legacy global- + // queue DISPATCH_QUEUE_PRIORITY_* constants. Using 0 keeps the + // queue at the chosen QoS class's nominal priority. + dispatch_queue_attr_t qos = dispatch_queue_attr_make_with_qos_class( + DISPATCH_QUEUE_SERIAL, + QOS_CLASS_USER_INTERACTIVE, + 0 + ); + self.sampleQueue = dispatch_queue_create("dev.lizardbyte.sunshine.sckCapture", qos); + + // SCK content enumeration is async; block (with a bounded timeout) + // until we have the SCDisplay matching the requested CGDirectDisplayID + // so this initializer remains synchronous: callers are not yet block-aware. + __block SCDisplay *selectedDisplay = nil; + __block NSError *enumerationError = nil; + dispatch_semaphore_t ready = dispatch_semaphore_create(0); + + [SCShareableContent getShareableContentExcludingDesktopWindows:NO + onScreenWindowsOnly:NO + completionHandler:^(SCShareableContent *_Nullable content, NSError *_Nullable error) { + if (error || !content) { + enumerationError = error; + } else { + for (SCDisplay *d in content.displays) { + if (d.displayID == displayID) { + selectedDisplay = d; + break; + } + } + // If the requested display wasn't found (display reconfigured, + // unplugged, etc.) fall back to the first display SCK reports. + if (!selectedDisplay && content.displays.count > 0) { + selectedDisplay = content.displays.firstObject; + } + } + dispatch_semaphore_signal(ready); + }]; + if (dispatch_semaphore_wait(ready, dispatch_time(DISPATCH_TIME_NOW, kSCVideoCompletionTimeoutSec * NSEC_PER_SEC)) != 0) { + NSLog(@"SCVideo: getShareableContent timed out after %lld seconds", kSCVideoCompletionTimeoutSec); + return nil; + } + + if (!selectedDisplay) { + NSLog(@"SCVideo: failed to resolve SCDisplay for id %u: %@", displayID, enumerationError); + return nil; + } + + // Empty excluded-windows array: capture everything on the display. + self.filter = [[SCContentFilter alloc] initWithDisplay:selectedDisplay excludingWindows:@[]]; + + self.streamConfig = [[SCStreamConfiguration alloc] init]; + self.streamConfig.width = self.frameWidth; + self.streamConfig.height = self.frameHeight; + self.streamConfig.minimumFrameInterval = self.minFrameDuration; + self.streamConfig.pixelFormat = self.pixelFormat; + self.streamConfig.queueDepth = 6; // SCK docs recommend 3-8 + self.streamConfig.showsCursor = YES; + + // If the initial pixel format is already a 10-bit format, flip on EDR + // immediately so the very first sample buffer carries HDR metadata. + [self applyDynamicRangeForPixelFormat:self.pixelFormat]; + + self.stream = [[SCStream alloc] initWithFilter:self.filter + configuration:self.streamConfig + delegate:self]; + if (!self.stream) { + NSLog(@"SCVideo: SCStream allocation failed"); + return nil; + } + + // Register the SCStreamOutput exactly once, here. SCStream retains + // outputs across stop/start cycles, so re-registering on every + // -capture: call would fail (or worse, silently duplicate + // delivery). All subsequent state changes are callback swaps on + // -capture: rather than stream-lifecycle operations. + NSError *outputError = nil; + if (![self.stream addStreamOutput:self + type:SCStreamOutputTypeScreen + sampleHandlerQueue:self.sampleQueue + error:&outputError]) { + NSLog(@"SCVideo: addStreamOutput failed: %@", outputError); + return nil; + } + + // Start the stream once. Frames begin flowing immediately on the + // sampleQueue; sample-handler delivery is a no-op until the first + // -capture: installs a callback (see -stream:didOutputSampleBuffer:ofType:). + __block NSError *startError = nil; + dispatch_semaphore_t started = dispatch_semaphore_create(0); + [self.stream startCaptureWithCompletionHandler:^(NSError *_Nullable error) { + startError = error; + dispatch_semaphore_signal(started); + }]; + if (dispatch_semaphore_wait(started, dispatch_time(DISPATCH_TIME_NOW, kSCVideoCompletionTimeoutSec * NSEC_PER_SEC)) != 0) { + NSLog(@"SCVideo: startCapture timed out after %lld seconds", kSCVideoCompletionTimeoutSec); + return nil; + } + if (startError) { + NSLog(@"SCVideo: startCapture failed: %@", startError); + return nil; + } + @synchronized(self) { + self.streamRunning = YES; + } + + return self; +} + +/** + * @brief Whether a CVPixelBuffer OSType denotes a 10-bit (or wider) format. + * + * Returning YES is the signal that the capture surface is HDR-capable; we + * use it to drive SCStreamConfiguration.captureDynamicRange on macOS 14+ + * so SCK emits BT.2020 PQ-tagged buffers instead of 10-bit Rec.709. + */ ++ (BOOL)pixelFormatIsHighBitDepth:(OSType)pixelFormat { + switch (pixelFormat) { + case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange: + case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange: + case kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange: + case kCVPixelFormatType_422YpCbCr10BiPlanarFullRange: + case kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange: + case kCVPixelFormatType_444YpCbCr10BiPlanarFullRange: + case kCVPixelFormatType_ARGB2101010LEPacked: + case kCVPixelFormatType_64ARGB: + case kCVPixelFormatType_64RGBALE: + return YES; + default: + return NO; + } +} + +- (void)applyDynamicRangeForPixelFormat:(OSType)pixelFormat { + // captureDynamicRange / SCCaptureDynamicRange* are macOS 14 (Sonoma) + // SDK symbols. The compile-time guard ensures this block is preprocessed + // away entirely when building against an older SDK that lacks the + // declarations; the runtime @available guard prevents using the + // symbols at runtime on pre-14 systems even with a newer SDK. On + // 12.3-13.x SCK still honours a requested 10-bit pixel format, but + // the OS won't tag buffers with BT.2020 PQ metadata automatically; + // downstream code falls back to Sunshine's existing colorspace logic. + // + // Gating: EDR is only enabled when BOTH (a) the chosen pixel format + // is 10-bit, AND (b) the session was actually negotiated as HDR + // (`hdrAllowed`). The pixel format on its own is necessary but not + // sufficient — a 10-bit format may be selected for codec reasons + // (e.g., a ProRes profile) without the client ever requesting HDR + // ingest, and silently emitting BT.2020 PQ-tagged buffers into a + // stream the control plane describes as SDR causes the decoder to + // tone-map undefined content. Defaulting hdrAllowed to NO keeps the + // legacy/SDR semantics intact when callers don't opt in. +#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 140000 + if (@available(macOS 14.0, *)) { + if (self.hdrAllowed && [SCVideo pixelFormatIsHighBitDepth:pixelFormat]) { + // hdrLocalDisplay matches the host display's HDR characteristics, + // which is what we want for game-streaming: stream what the user + // would see locally, including the local panel's PQ peak luminance. + self.streamConfig.captureDynamicRange = SCCaptureDynamicRangeHDRLocalDisplay; + } else { + self.streamConfig.captureDynamicRange = SCCaptureDynamicRangeSDR; + } + } +#else + (void) pixelFormat; +#endif +} + +- (void)setFrameWidth:(int)frameWidth frameHeight:(int)frameHeight { + _frameWidth = frameWidth; + _frameHeight = frameHeight; + + if (self.streamConfig) { + self.streamConfig.width = frameWidth; + self.streamConfig.height = frameHeight; + [self applyConfigurationIfRunning]; + } +} + +- (void)setPixelFormat:(OSType)pixelFormat { + _pixelFormat = pixelFormat; + + if (self.streamConfig) { + self.streamConfig.pixelFormat = pixelFormat; + [self applyDynamicRangeForPixelFormat:pixelFormat]; + [self applyConfigurationIfRunning]; + } +} + +- (void)setMinFrameDuration:(CMTime)minFrameDuration { + _minFrameDuration = minFrameDuration; + + if (self.streamConfig) { + self.streamConfig.minimumFrameInterval = minFrameDuration; + [self applyConfigurationIfRunning]; + } +} + +- (void)applyConfigurationIfRunning { + BOOL running; + @synchronized(self) { + running = self.streamRunning; + } + if (!running || !self.stream) { + return; + } + [self.stream updateConfiguration:self.streamConfig + completionHandler:^(NSError *_Nullable error) { + if (error) { + NSLog(@"SCVideo: updateConfiguration failed: %@", error); + } + }]; +} + +- (dispatch_semaphore_t)capture:(FrameCallbackBlock)frameCallback { + // Swap in the new callback. The SCStream output and frame flow are + // already running from -init; this method is purely a callback + // installation, not a stream-lifecycle operation. That avoids the + // double-add failure mode and makes -capture: cheap enough to be + // called multiple times across the SCVideo's lifetime (e.g., the + // encoder probe path's dummy_img followed by the real capture). + dispatch_semaphore_t newSignal = dispatch_semaphore_create(0); + dispatch_semaphore_t previousSignal = nil; + + @synchronized(self) { + previousSignal = self.currentSignal; + self.currentCallback = frameCallback; + self.currentSignal = newSignal; + } + + // Unblock any prior caller still waiting on the old semaphore. + // They will observe their callback was cleared and return. + if (previousSignal) { + dispatch_semaphore_signal(previousSignal); + } + + return newSignal; +} + +- (void)dealloc { + BOOL running; + SCStream *stream; + dispatch_semaphore_t pendingSignal; + @synchronized(self) { + running = self.streamRunning; + stream = self.stream; + pendingSignal = self.currentSignal; + self.streamRunning = NO; + self.currentCallback = nil; + self.currentSignal = nil; + } + + // Unblock any caller still waiting on the semaphore that -capture: + // returned. Without this, if the stream stops without triggering + // -stream:didStopWithError: (or the delegate callback can't be + // delivered during teardown), the waiting thread would stall + // forever. Signalling after clearing currentCallback means the + // caller wakes up to observe their callback was cleared and exits. + if (pendingSignal) { + dispatch_semaphore_signal(pendingSignal); + } + + if (running && stream) { + // Best-effort synchronous stop with a bounded wait so a + // misbehaving SCK doesn't hang teardown. + dispatch_semaphore_t stopped = dispatch_semaphore_create(0); + [stream stopCaptureWithCompletionHandler:^(NSError *_Nullable error) { + (void) error; + dispatch_semaphore_signal(stopped); + }]; + dispatch_semaphore_wait(stopped, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC)); + } +} + +#pragma mark - SCStreamOutput + +- (void)stream:(SCStream *)stream + didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer + ofType:(SCStreamOutputType)type { + if (type != SCStreamOutputTypeScreen) { + return; + } + if (!CMSampleBufferIsValid(sampleBuffer)) { + return; + } + + // Drop frames whose status array says they aren't ready. SCK delivers + // a status attachment on every sample buffer indicating idle vs + // complete vs blank — we want only complete frames downstream. + CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO); + if (attachmentsArray && CFArrayGetCount(attachmentsArray) > 0) { + CFDictionaryRef attachments = CFArrayGetValueAtIndex(attachmentsArray, 0); + CFNumberRef statusNum = CFDictionaryGetValue(attachments, (__bridge CFStringRef) SCStreamFrameInfoStatus); + if (statusNum) { + int status = 0; + CFNumberGetValue(statusNum, kCFNumberSInt32Type, &status); + if (status != SCFrameStatusComplete) { + return; + } + } + } + + FrameCallbackBlock callback; + dispatch_semaphore_t signal; + @synchronized(self) { + callback = self.currentCallback; + signal = self.currentSignal; + } + + if (!callback) { + // No active consumer. Drop the frame; the stream keeps running + // so subsequent -capture: calls can pick up immediately. + return; + } + + if (!callback(sampleBuffer)) { + // Consumer signalled stop. Clear the callback and wake the + // caller; the underlying SCStream stays alive for any future + // -capture: caller (cheaper than tearing down and restarting). + @synchronized(self) { + if (self.currentCallback == callback) { + self.currentCallback = nil; + self.currentSignal = nil; + } + } + if (signal) { + dispatch_semaphore_signal(signal); + } + } +} + +#pragma mark - SCStreamDelegate + +- (void)stream:(SCStream *)stream didStopWithError:(NSError *)error { + if (error) { + NSLog(@"SCVideo: stream stopped with error: %@", error); + } + dispatch_semaphore_t signal; + @synchronized(self) { + self.streamRunning = NO; + signal = self.currentSignal; + self.currentCallback = nil; + self.currentSignal = nil; + } + if (signal) { + dispatch_semaphore_signal(signal); + } +} + +#pragma mark - Display enumeration + +// Active-display upper bound. We just need a buffer size that comfortably +// exceeds any plausible attached-display count. +static const int kMaxDisplays = 32; + ++ (NSString *)getDisplayName:(CGDirectDisplayID)displayID { + for (NSScreen *screen in [NSScreen screens]) { + if ([screen.deviceDescription[@"NSScreenNumber"] isEqualToNumber:[NSNumber numberWithUnsignedInt:displayID]]) { + return screen.localizedName; + } + } + return nil; +} + ++ (NSArray *)displayNames { + CGDirectDisplayID displays[kMaxDisplays]; + uint32_t count = 0; + if (CGGetActiveDisplayList(kMaxDisplays, displays, &count) != kCGErrorSuccess) { + return @[]; + } + + NSMutableArray *result = [NSMutableArray array]; + for (uint32_t i = 0; i < count; i++) { + [result addObject:@{ + @"id": [NSNumber numberWithUnsignedInt:displays[i]], + @"name": [NSString stringWithFormat:@"%u", displays[i]], + @"displayName": [SCVideo getDisplayName:displays[i]] ?: [NSString stringWithFormat:@"Display %u", displays[i]], + }]; + } + return [NSArray arrayWithArray:result]; +} + +@end