Skip to content

Commit

Permalink
Merge pull request #1824 from srcejon/fix_1819
Browse files Browse the repository at this point in the history
Request authorization for access to camera and microphone on Mac
  • Loading branch information
f4exb authored Sep 13, 2023
2 parents af916d6 + 28fa137 commit e83d974
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ cmake_minimum_required(VERSION 3.13.0)

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

project(sdrangel)
if(APPLE)
project(sdrangel LANGUAGES CXX OBJCXX)
else()
project(sdrangel)
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)

Expand Down Expand Up @@ -911,14 +915,17 @@ target_link_libraries(sdrangelbench
sdrbench
logging
)

############ build sdrangel gui ################
if (BUILD_GUI)
set(sdrangel_SOURCES
app/main.cpp
sdrgui/resources/sdrangel.rc
settings/settings.qrc
)

if(APPLE)
set(sdrangel_SOURCES ${sdrangel_SOURCES} mac/auth.mm)
endif()
if(ANDROID AND NOT ENABLE_QT6)
add_library(${CMAKE_PROJECT_NAME} SHARED ${sdrangel_SOURCES})
elseif(ANDROID)
Expand Down Expand Up @@ -970,7 +977,10 @@ if (BUILD_GUI)
sdrbase
sdrgui
logging
)
)
endif()
if(APPLE)
target_link_libraries(${CMAKE_PROJECT_NAME} "-framework AVFoundation" objc)
endif()

if(WIN32)
Expand Down
6 changes: 6 additions & 0 deletions app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ int main(int argc, char* argv[])
sfc.setVersion(3, 3);
sfc.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(sfc);

// Request authorization for access to camera and microphone (mac/auth.mm)
extern int authCameraAndMic();
if (authCameraAndMic() < 0) {
qWarning("Failed to authorize access to camera and microphone. Enable access in System Settings > Privacy & Security");
}
#endif

#ifdef ANDROID
Expand Down
35 changes: 35 additions & 0 deletions mac/auth.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Request permission.authorization to use camera and microphone
// From: https://developer.apple.com/documentation/bundleresources/information_property_list/protected_resources/requesting_authorization_for_media_capture_on_macos?language=objc

#include <AVFoundation/AVFoundation.h>

// Returns:
// 1 - if permission granted,
// 0 - if pending,
// -1 - if not granted.
int authCameraAndMic()
{
// Request permission to access the camera and microphone.
switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo])
{
case AVAuthorizationStatusAuthorized:
// The user has previously granted access to the camera.
return 1;
case AVAuthorizationStatusNotDetermined:
{
// The app hasn't yet asked the user for camera access.
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
}
}];
return 0;
}
case AVAuthorizationStatusDenied:
// The user has previously denied access.
return -1;
case AVAuthorizationStatusRestricted:
// The user can't grant access due to restrictions.
return -1;
}
}

0 comments on commit e83d974

Please sign in to comment.