From 27b49bb6b3950edf74dc9b24de59e6eadfbd927b Mon Sep 17 00:00:00 2001 From: Brian Kendall Date: Sun, 19 Feb 2023 22:58:18 -0500 Subject: [PATCH 1/2] Support compiling for earlier releases of macOS Allows compiling for macOS 10.12 and later --- CMakeLists.txt | 4 ++- src/platform/macos/av_audio.m | 32 ++++++++++++++++++++---- src/platform/macos/{misc.cpp => misc.mm} | 30 +++++++++++++++++++++- 3 files changed, 59 insertions(+), 7 deletions(-) rename src/platform/macos/{misc.cpp => misc.mm} (75%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 730775ea0e7..53acbcd4c2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -233,7 +233,7 @@ elseif(APPLE) src/platform/macos/display.mm src/platform/macos/input.cpp src/platform/macos/microphone.mm - src/platform/macos/misc.cpp + src/platform/macos/misc.mm src/platform/macos/misc.h src/platform/macos/nv12_zero_device.cpp src/platform/macos/nv12_zero_device.h @@ -620,6 +620,8 @@ endif() if(APPLE) target_link_options(sunshine PRIVATE LINKER:-sectcreate,__TEXT,__info_plist,${APPLE_PLIST_FILE}) + # Tell linker to dynamically load these symbols at runtime, in case they're unavailable: + target_link_options(sunshine PRIVATE -Wl,-U,_CGPreflightScreenCaptureAccess -Wl,-U,_CGRequestScreenCaptureAccess) endif() foreach(flag IN LISTS SUNSHINE_COMPILE_OPTIONS) diff --git a/src/platform/macos/av_audio.m b/src/platform/macos/av_audio.m index c5cd899ef55..945fcf9f154 100644 --- a/src/platform/macos/av_audio.m +++ b/src/platform/macos/av_audio.m @@ -3,11 +3,33 @@ @implementation AVAudio + (NSArray *)microphones { - AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInMicrophone, - AVCaptureDeviceTypeExternalUnknown] - mediaType:AVMediaTypeAudio - position:AVCaptureDevicePositionUnspecified]; - return discoverySession.devices; + + if([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:((NSOperatingSystemVersion) { 10, 15, 0 })]) { + // This will generate a warning about AVCaptureDeviceDiscoverySession being + // unavailable before macOS 10.15, but we have a guard to prevent it from + // being called on those earlier systems. + // Unfortunately the supported way to silence this warning, using @available, + // produces linker errors for __isPlatformVersionAtLeast, so we have to use + // a different method. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunguarded-availability-new" + AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInMicrophone, + AVCaptureDeviceTypeExternalUnknown] + mediaType:AVMediaTypeAudio + position:AVCaptureDevicePositionUnspecified]; + return discoverySession.devices; +#pragma clang diagnostic pop + } + else { + // We're intentionally using a deprecated API here specifically for versions + // of macOS where it's not deprecated, so we can ignore any deprecation + // warnings: +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + return [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio]; +#pragma clang diagnostic pop + } + } + (NSArray *)microphoneNames { diff --git a/src/platform/macos/misc.cpp b/src/platform/macos/misc.mm similarity index 75% rename from src/platform/macos/misc.cpp rename to src/platform/macos/misc.mm index cb9c0c528bf..f9767aef8dc 100644 --- a/src/platform/macos/misc.cpp +++ b/src/platform/macos/misc.mm @@ -1,3 +1,4 @@ +#include #include #include #include @@ -17,13 +18,40 @@ namespace bp = boost::process; namespace platf { +// Even though the following two functions are available starting in macOS 10.15, they weren't +// actually in the Mac SDK until Xcode 12.2, the first to include the SDK for macOS 11 +#if __MAC_OS_X_VERSION_MAX_ALLOWED < 110000 // __MAC_11_0 +// If they're not in the SDK then we can use our own function definitions. +// Need to use weak import so that this will link in macOS 10.14 and earlier +extern "C" bool CGPreflightScreenCaptureAccess(void) __attribute__((weak_import)); +extern "C" bool CGRequestScreenCaptureAccess(void) __attribute__((weak_import)); +#endif + std::unique_ptr init() { - if(!CGPreflightScreenCaptureAccess()) { + // This will generate a warning about CGPreflightScreenCaptureAccess and + // CGRequestScreenCaptureAccess being unavailable before macOS 10.15, but + // we have a guard to prevent it from being called on those earlier systems. + // Unfortunately the supported way to silence this warning, using @available, + // produces linker errors for __isPlatformVersionAtLeast, so we have to use + // a different method. + // We also ignore "tautological-pointer-compare" because when compiling with + // Xcode 12.2 and later, these functions are not weakly linked and will never + // be null, and therefore generate this warning. Since we are weakly linking + // when compiling with earlier Xcode versions, the check for null is + // necessary and so we ignore the warning. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunguarded-availability-new" +#pragma clang diagnostic ignored "-Wtautological-pointer-compare" + if([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:((NSOperatingSystemVersion) { 10, 15, 0 })] && + // Double check that these weakly-linked symbols have been loaded: + CGPreflightScreenCaptureAccess != nullptr && CGRequestScreenCaptureAccess != nullptr && + !CGPreflightScreenCaptureAccess()) { BOOST_LOG(error) << "No screen capture permission!"sv; BOOST_LOG(error) << "Please activate it in 'System Preferences' -> 'Privacy' -> 'Screen Recording'"sv; CGRequestScreenCaptureAccess(); return nullptr; } +#pragma clang diagnostic pop return std::make_unique(); } From 808eb245f0a2de3598dee84a162254fd4b90ff8f Mon Sep 17 00:00:00 2001 From: Brian Kendall Date: Wed, 8 Mar 2023 11:29:30 -0500 Subject: [PATCH 2/2] Fix lint error --- src/platform/macos/av_audio.m | 1 - 1 file changed, 1 deletion(-) diff --git a/src/platform/macos/av_audio.m b/src/platform/macos/av_audio.m index 945fcf9f154..43f726e43d0 100644 --- a/src/platform/macos/av_audio.m +++ b/src/platform/macos/av_audio.m @@ -29,7 +29,6 @@ @implementation AVAudio return [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio]; #pragma clang diagnostic pop } - } + (NSArray *)microphoneNames {