@@ -31,6 +31,9 @@ of this software and associated documentation files (the "Software"), to deal
31
31
#include " platformcontext.h"
32
32
#import < AVFoundation/AVFoundation.h>
33
33
34
+ #include < chrono>
35
+ #include < thread>
36
+
34
37
// a platform factory function needed by
35
38
// libmain.cpp
36
39
Context* createPlatformContext ()
@@ -42,22 +45,31 @@ of this software and associated documentation files (the "Software"), to deal
42
45
Context()
43
46
{
44
47
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 ;
49
49
if ([AVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo] == AVAuthorizationStatusAuthorized) {
50
- enumerateDevices ();
50
+ NSLog (@" Already have camera permission" );
51
+ cameraPermissionReceived = 1 ;
51
52
}
52
53
else {
53
- NSLog (@" Bundle path for Info.plist: %@ " , [[NSBundle mainBundle ] bundlePath ]);
54
+ NSLog (@" Requesting permission, bundle path for Info.plist: %@ " , [[NSBundle mainBundle ] bundlePath ]);
54
55
[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
+ }
60
66
} ];
67
+ while (cameraPermissionReceived == 0 ) {
68
+ std::this_thread::sleep_for (std::chrono::milliseconds (200 ));
69
+ }
70
+ }
71
+ if (cameraPermissionReceived == 1 ) {
72
+ enumerateDevices ();
61
73
}
62
74
}
63
75
0 commit comments