Commit f10fce9a authored by Maksim Shabunin's avatar Maksim Shabunin

AVFoundation: backported runtime authorization check from master

parent aaad238c
...@@ -316,42 +316,45 @@ int CvCaptureCAM::startCaptureDevice(int cameraNum) { ...@@ -316,42 +316,45 @@ int CvCaptureCAM::startCaptureDevice(int cameraNum) {
NSAutoreleasePool *localpool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *localpool = [[NSAutoreleasePool alloc] init];
#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (@available(macOS 10.14, *))
if (status == AVAuthorizationStatusDenied)
{ {
fprintf(stderr, "OpenCV: camera access has been denied. Either run 'tccutil reset Camera' " AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
"command in same terminal to reset application authorization status, " if (status == AVAuthorizationStatusDenied)
"either modify 'System Preferences -> Security & Privacy -> Camera' " {
"settings for your application.\n"); fprintf(stderr, "OpenCV: camera access has been denied. Either run 'tccutil reset Camera' "
[localpool drain]; "command in same terminal to reset application authorization status, "
return 0; "either modify 'System Preferences -> Security & Privacy -> Camera' "
} "settings for your application.\n");
else if (status != AVAuthorizationStatusAuthorized) [localpool drain];
{ return 0;
if (!cv::utils::getConfigurationParameterBool("OPENCV_AVFOUNDATION_SKIP_AUTH", false)) }
else if (status != AVAuthorizationStatusAuthorized)
{ {
fprintf(stderr, "OpenCV: not authorized to capture video (status %ld), requesting...\n", status); if (!cv::utils::getConfigurationParameterBool("OPENCV_AVFOUNDATION_SKIP_AUTH", false))
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL) { /* we don't care */}];
if ([NSThread isMainThread])
{ {
// we run the main loop for 0.1 sec to show the message fprintf(stderr, "OpenCV: not authorized to capture video (status %ld), requesting...\n", status);
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL) { /* we don't care */}];
if ([NSThread isMainThread])
{
// we run the main loop for 0.1 sec to show the message
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
else
{
fprintf(stderr, "OpenCV: can not spin main run loop from other thread, set "
"OPENCV_AVFOUNDATION_SKIP_AUTH=1 to disable authorization request "
"and perform it in your application.\n");
}
} }
else else
{ {
fprintf(stderr, "OpenCV: can not spin main run loop from other thread, set " fprintf(stderr, "OpenCV: not authorized to capture video (status %ld), set "
"OPENCV_AVFOUNDATION_SKIP_AUTH=1 to disable authorization request " "OPENCV_AVFOUNDATION_SKIP_AUTH=0 to enable authorization request or "
"and perform it in your application.\n"); "perform it in your application.\n", status);
} }
[localpool drain];
return 0;
} }
else
{
fprintf(stderr, "OpenCV: not authorized to capture video (status %ld), set "
"OPENCV_AVFOUNDATION_SKIP_AUTH=0 to enable authorization request or "
"perform it in your application.\n", status);
}
[localpool drain];
return 0;
} }
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment