From aaad238c6ef6a1af5ec5e08b8df96b7d218c1888 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Fri, 13 Sep 2019 13:11:35 +0300 Subject: [PATCH 1/2] AVFoundation: fix authorization request not being shown --- modules/videoio/src/cap_avfoundation_mac.mm | 26 +++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/modules/videoio/src/cap_avfoundation_mac.mm b/modules/videoio/src/cap_avfoundation_mac.mm index 124462c6c2..e3c69786fd 100644 --- a/modules/videoio/src/cap_avfoundation_mac.mm +++ b/modules/videoio/src/cap_avfoundation_mac.mm @@ -328,10 +328,28 @@ int CvCaptureCAM::startCaptureDevice(int cameraNum) { } else if (status != AVAuthorizationStatusAuthorized) { - fprintf(stderr, "OpenCV: not authorized to capture video (status %ld), requesting...\n", status); - // TODO: doesn't work via ssh - [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL) { /* we don't care */}]; - // we do not wait for completion + if (!cv::utils::getConfigurationParameterBool("OPENCV_AVFOUNDATION_SKIP_AUTH", false)) + { + fprintf(stderr, "OpenCV: not authorized to capture video (status %ld), requesting...\n", status); + [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 + { + 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; } From f10fce9ab4e4811e9579bfa024dc6c2876478d47 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Fri, 13 Sep 2019 13:20:29 +0300 Subject: [PATCH 2/2] AVFoundation: backported runtime authorization check from master --- modules/videoio/src/cap_avfoundation_mac.mm | 59 +++++++++++---------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/modules/videoio/src/cap_avfoundation_mac.mm b/modules/videoio/src/cap_avfoundation_mac.mm index e3c69786fd..75af8a0090 100644 --- a/modules/videoio/src/cap_avfoundation_mac.mm +++ b/modules/videoio/src/cap_avfoundation_mac.mm @@ -316,42 +316,45 @@ int CvCaptureCAM::startCaptureDevice(int cameraNum) { NSAutoreleasePool *localpool = [[NSAutoreleasePool alloc] init]; #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400 - AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; - if (status == AVAuthorizationStatusDenied) + if (@available(macOS 10.14, *)) { - fprintf(stderr, "OpenCV: camera access has been denied. Either run 'tccutil reset Camera' " - "command in same terminal to reset application authorization status, " - "either modify 'System Preferences -> Security & Privacy -> Camera' " - "settings for your application.\n"); - [localpool drain]; - return 0; - } - else if (status != AVAuthorizationStatusAuthorized) - { - if (!cv::utils::getConfigurationParameterBool("OPENCV_AVFOUNDATION_SKIP_AUTH", false)) + AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; + if (status == AVAuthorizationStatusDenied) { - fprintf(stderr, "OpenCV: not authorized to capture video (status %ld), requesting...\n", status); - [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL) { /* we don't care */}]; - if ([NSThread isMainThread]) + fprintf(stderr, "OpenCV: camera access has been denied. Either run 'tccutil reset Camera' " + "command in same terminal to reset application authorization status, " + "either modify 'System Preferences -> Security & Privacy -> Camera' " + "settings for your application.\n"); + [localpool drain]; + return 0; + } + else if (status != AVAuthorizationStatusAuthorized) + { + if (!cv::utils::getConfigurationParameterBool("OPENCV_AVFOUNDATION_SKIP_AUTH", false)) { - // we run the main loop for 0.1 sec to show the message - [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; + fprintf(stderr, "OpenCV: not authorized to capture video (status %ld), requesting...\n", status); + [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 { - 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"); + 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; } - 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