Skip to content

Commit c8bfa81

Browse files
authored
Merge pull request #37 from bakwc/fixMacosCamPermission
Fixed macOS Catalina camera permissions request
2 parents 71239ab + f49d9b5 commit c8bfa81

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ common/version.h
1010
QtCaptureTest/build
1111
.DS_Store
1212
.vscode
13+
.idea/
14+
cmake-build-*/

mac/platformcontext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class PlatformContext : public Context
5555
information into the m_devices array
5656
*/
5757
virtual bool enumerateDevices();
58-
58+
private:
59+
int cameraPermissionReceived; // 0 = waiting, 1 = success, -1 = error
5960
};
6061

6162
#endif

mac/platformcontext.mm

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ of this software and associated documentation files (the "Software"), to deal
3131
#include "platformcontext.h"
3232
#import <AVFoundation/AVFoundation.h>
3333

34+
#include <chrono>
35+
#include <thread>
36+
3437
// a platform factory function needed by
3538
// libmain.cpp
3639
Context* createPlatformContext()
@@ -42,22 +45,31 @@ of this software and associated documentation files (the "Software"), to deal
4245
Context()
4346
{
4447
LOG(LOG_INFO, "Platform context created\n");
45-
/**
46-
If we are not yet authorized to use the camera, request auth. If we're already authed we can go
47-
ahead and enumerate.
48-
*/
48+
cameraPermissionReceived = 0;
4949
if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusAuthorized) {
50-
enumerateDevices();
50+
NSLog(@"Already have camera permission");
51+
cameraPermissionReceived = 1;
5152
}
5253
else {
53-
NSLog(@"Bundle path for Info.plist: %@", [[NSBundle mainBundle] bundlePath]);
54+
NSLog(@"Requesting permission, bundle path for Info.plist: %@", [[NSBundle mainBundle] bundlePath]);
5455
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
55-
/**
56-
TODO Would like to call enumerateDevices(); here so that once the user has authorized they
57-
can list the cameras, but this crashes. Probbaly we need to do it on the same thread
58-
as this was originally called from.
59-
*/
56+
if (granted) {
57+
cameraPermissionReceived = 1;
58+
} else {
59+
cameraPermissionReceived = -1;
60+
}
61+
if (granted) {
62+
NSLog(@"Permission granted");
63+
} else {
64+
NSLog(@"Failed to get permission");
65+
}
6066
} ];
67+
while (cameraPermissionReceived == 0) {
68+
std::this_thread::sleep_for(std::chrono::milliseconds(200));
69+
}
70+
}
71+
if (cameraPermissionReceived == 1) {
72+
enumerateDevices();
6173
}
6274
}
6375

0 commit comments

Comments
 (0)