diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml index 5711d47..1de2d1a 100644 --- a/.github/workflows/linux-build.yml +++ b/.github/workflows/linux-build.yml @@ -410,19 +410,11 @@ jobs: run: | cmake --build build/arm64/${{ matrix.build_type }}-${{ matrix.library_type }} --config ${{ matrix.build_type }} --target ccap_convert_test --parallel $(nproc) - # Cache QEMU installation - - name: Cache QEMU - if: (github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main')) && matrix.build_type == 'Release' - uses: awalsh128/cache-apt-pkgs-action@latest - with: - packages: qemu-user qemu-user-static - version: 1.0 - - name: Install QEMU for ARM64 test emulation (PRs and main push, Release only) if: (github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main')) && matrix.build_type == 'Release' run: | sudo apt-get update - sudo apt-get install -y qemu-user qemu-user-static + sudo apt-get install -y qemu-user qemu-aarch64 --version - name: Run ARM64 Unit Tests (PRs and main push, Release only) diff --git a/bindings/rust/src/provider.rs b/bindings/rust/src/provider.rs index e03e8b2..b7a9ad8 100644 --- a/bindings/rust/src/provider.rs +++ b/bindings/rust/src/provider.rs @@ -176,8 +176,11 @@ impl Provider { } let mut devices = Vec::new(); - for i in 0..device_names_list.deviceCount { - let name_bytes = &device_names_list.deviceNames[i]; + for name_bytes in device_names_list + .deviceNames + .iter() + .take(device_names_list.deviceCount) + { let name = unsafe { let cstr = CStr::from_ptr(name_bytes.as_ptr()); cstr.to_string_lossy().to_string() diff --git a/src/ccap_imp_apple.mm b/src/ccap_imp_apple.mm index 60f5360..023fd94 100644 --- a/src/ccap_imp_apple.mm +++ b/src/ccap_imp_apple.mm @@ -323,6 +323,7 @@ - (BOOL)open { } if (![_device hasMediaType:AVMediaTypeVideo]) { /// No video device found + [_session commitConfiguration]; reportError(ErrorCode::NoDeviceFound, "No video device found"); return NO; } @@ -345,6 +346,7 @@ - (BOOL)open { // Create input device _videoInput = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error]; if (!_videoInput || error) { + [_session commitConfiguration]; reportError(ErrorCode::DeviceOpenFailed, "Open camera failed" + (error && error.localizedDescription ? (": " + std::string([error.localizedDescription UTF8String])) : "")); return NO; } @@ -355,6 +357,7 @@ - (BOOL)open { CCAP_NSLOG_V(@"ccap: Add input to session"); } else { + [_session commitConfiguration]; reportError(ErrorCode::DeviceOpenFailed, "Session cannot add input device"); return NO; } @@ -472,6 +475,7 @@ - (BOOL)open { [_session addOutput:_videoOutput]; CCAP_NSLOG_V(@"ccap: Add output to session"); } else { + [_session commitConfiguration]; reportError(ErrorCode::DeviceOpenFailed, "Session cannot add output device"); return NO; } @@ -844,12 +848,14 @@ - (void)destroy { [_session beginConfiguration]; - if (_videoInput) { + if (_videoInput && [_session.inputs containsObject:_videoInput]) { [_session removeInput:_videoInput]; + } + if (_videoOutput && [_session.outputs containsObject:_videoOutput]) { [_session removeOutput:_videoOutput]; - _videoInput = nil; - _videoOutput = nil; } + _videoInput = nil; + _videoOutput = nil; [_session commitConfiguration]; _session = nil; @@ -1082,7 +1088,12 @@ - (void)captureOutput:(AVCaptureOutput*)output [m_imp setCameraName:@(deviceName.data())]; } [m_imp setResolution:CGSizeMake(m_frameProp.width, m_frameProp.height)]; - return [m_imp open]; + if (![m_imp open]) { + [m_imp destroy]; + m_imp = nil; + return false; + } + return true; } } @@ -1254,4 +1265,4 @@ - (void)captureOutput:(AVCaptureOutput*)output } // namespace ccap -#endif \ No newline at end of file +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 98ad81a..07012d2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -205,6 +205,10 @@ add_executable( test_grab_timeout.cpp ) +if (APPLE AND NOT IOS) + target_sources(ccap_convert_test PRIVATE test_apple_permission.mm) +endif () + target_link_libraries( ccap_convert_test ccap_test_utils diff --git a/tests/test_apple_permission.mm b/tests/test_apple_permission.mm new file mode 100644 index 0000000..953ac41 --- /dev/null +++ b/tests/test_apple_permission.mm @@ -0,0 +1,78 @@ +/** Regression coverage for the real Apple camera authorization path. */ +#include "ccap.h" +#include + +#import +#import + +#include +#include +#include +#include + +namespace { +std::atomic requested{false}; +std::atomic requestCount{0}; +bool authorized = false; +int configurationDepth = 0; + +AVAuthorizationStatus mockAuthorizationStatus(id, SEL, AVMediaType) { + if (authorized) return AVAuthorizationStatusAuthorized; + return requested.load() ? AVAuthorizationStatusDenied : AVAuthorizationStatusNotDetermined; +} + +void mockRequestAccess(id, SEL, AVMediaType, void (^completion)(BOOL)) { + ++requestCount; + requested = true; + dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{ + completion(NO); + }); +} + +AVCaptureDevice* mockDefaultDevice(id, SEL, AVMediaType) { return nil; } + +void mockBeginConfiguration(id, SEL) { ++configurationDepth; } + +void mockCommitConfiguration(id, SEL) { --configurationDepth; } +} // namespace + +TEST(AppleCameraPermission, WorkerWithoutMainRunLoopCanRetryAfterFailedOpen) { + // Re-exec to avoid running Objective-C in a child of a multithreaded process. + // Isolate method replacements and bound the lifetime of a deadlocked child. + const auto previousStyle = ::testing::GTEST_FLAG(death_test_style); + ::testing::GTEST_FLAG(death_test_style) = "threadsafe"; + EXPECT_EXIT({ + @autoreleasepool { + Method status = class_getClassMethod([AVCaptureDevice class], @selector(authorizationStatusForMediaType:)); + Method request = class_getClassMethod([AVCaptureDevice class], @selector(requestAccessForMediaType:completionHandler:)); + Method device = class_getClassMethod([AVCaptureDevice class], @selector(defaultDeviceWithMediaType:)); + Class sessionClass = object_getClass([[AVCaptureSession alloc] init]); + Method begin = class_getInstanceMethod(sessionClass, @selector(beginConfiguration)); + Method commit = class_getInstanceMethod(sessionClass, @selector(commitConfiguration)); + if (!status || !request || !device || !begin || !commit) std::_Exit(2); + method_setImplementation(status, reinterpret_cast(mockAuthorizationStatus)); + method_setImplementation(request, reinterpret_cast(mockRequestAccess)); + method_setImplementation(device, reinterpret_cast(mockDefaultDevice)); + method_setImplementation(begin, reinterpret_cast(mockBeginConfiguration)); + method_setImplementation(commit, reinterpret_cast(mockCommitConfiguration)); + + std::thread([] { + std::this_thread::sleep_for(std::chrono::seconds(5)); + std::_Exit(3); + }).detach(); + std::thread worker([] { + ccap::Provider provider; + if (provider.open("", false)) std::_Exit(1); + requested = false; + if (provider.open("", false) || requestCount != 2) std::_Exit(1); + + authorized = true; + if (provider.open("", false) || configurationDepth != 0) std::_Exit(4); + if (provider.open("", false) || configurationDepth != 0) std::_Exit(4); + }); + worker.join(); + std::_Exit(0); + } + }, ::testing::ExitedWithCode(0), ""); + ::testing::GTEST_FLAG(death_test_style) = previousStyle; +}