Support compiling for earlier releases of macOS (#960)

This commit is contained in:
Brian Kendall
2023-03-10 09:53:29 -05:00
committed by GitHub
parent 6d54356166
commit 80aa61b6e4
3 changed files with 58 additions and 7 deletions

View File

@@ -233,7 +233,7 @@ elseif(APPLE)
src/platform/macos/display.mm src/platform/macos/display.mm
src/platform/macos/input.cpp src/platform/macos/input.cpp
src/platform/macos/microphone.mm src/platform/macos/microphone.mm
src/platform/macos/misc.cpp src/platform/macos/misc.mm
src/platform/macos/misc.h src/platform/macos/misc.h
src/platform/macos/nv12_zero_device.cpp src/platform/macos/nv12_zero_device.cpp
src/platform/macos/nv12_zero_device.h src/platform/macos/nv12_zero_device.h
@@ -620,6 +620,8 @@ endif()
if(APPLE) if(APPLE)
target_link_options(sunshine PRIVATE LINKER:-sectcreate,__TEXT,__info_plist,${APPLE_PLIST_FILE}) 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() endif()
foreach(flag IN LISTS SUNSHINE_COMPILE_OPTIONS) foreach(flag IN LISTS SUNSHINE_COMPILE_OPTIONS)

View File

@@ -3,11 +3,32 @@
@implementation AVAudio @implementation AVAudio
+ (NSArray<AVCaptureDevice *> *)microphones { + (NSArray<AVCaptureDevice *> *)microphones {
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInMicrophone,
AVCaptureDeviceTypeExternalUnknown] if([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:((NSOperatingSystemVersion) { 10, 15, 0 })]) {
mediaType:AVMediaTypeAudio // This will generate a warning about AVCaptureDeviceDiscoverySession being
position:AVCaptureDevicePositionUnspecified]; // unavailable before macOS 10.15, but we have a guard to prevent it from
return discoverySession.devices; // 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<NSString *> *)microphoneNames { + (NSArray<NSString *> *)microphoneNames {

View File

@@ -1,3 +1,4 @@
#include <Foundation/Foundation.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <fcntl.h> #include <fcntl.h>
@@ -17,13 +18,40 @@ namespace bp = boost::process;
namespace platf { 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<deinit_t> init() { std::unique_ptr<deinit_t> 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) << "No screen capture permission!"sv;
BOOST_LOG(error) << "Please activate it in 'System Preferences' -> 'Privacy' -> 'Screen Recording'"sv; BOOST_LOG(error) << "Please activate it in 'System Preferences' -> 'Privacy' -> 'Screen Recording'"sv;
CGRequestScreenCaptureAccess(); CGRequestScreenCaptureAccess();
return nullptr; return nullptr;
} }
#pragma clang diagnostic pop
return std::make_unique<deinit_t>(); return std::make_unique<deinit_t>();
} }