From 982251bac9c8ca06031038746349f9481a3d40d5 Mon Sep 17 00:00:00 2001 From: Wang Yang Date: Thu, 19 Nov 2026 13:16:21 +0800 Subject: [PATCH 1/3] fix(macos): allow camera open retry after failure --- src/ccap_imp_apple.mm | 9 +++-- tests/CMakeLists.txt | 4 +++ tests/test_apple_permission.mm | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tests/test_apple_permission.mm diff --git a/src/ccap_imp_apple.mm b/src/ccap_imp_apple.mm index 60f5360..a8c9cad 100644 --- a/src/ccap_imp_apple.mm +++ b/src/ccap_imp_apple.mm @@ -1082,7 +1082,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 +1259,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..31144e0 --- /dev/null +++ b/tests/test_apple_permission.mm @@ -0,0 +1,60 @@ +/** 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}; + +AVAuthorizationStatus mockAuthorizationStatus(id, SEL, AVMediaType) { + 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); + }); +} +} // namespace + +TEST(AppleCameraPermission, WorkerWithoutMainRunLoopCanRetryAfterDenial) { + // 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:)); + if (!status || !request) std::_Exit(2); + method_setImplementation(status, reinterpret_cast(mockAuthorizationStatus)); + method_setImplementation(request, reinterpret_cast(mockRequestAccess)); + + std::thread([] { + std::this_thread::sleep_for(std::chrono::seconds(5)); + std::_Exit(3); + }).detach(); + bool first = true; + bool second = true; + std::thread worker([&] { + ccap::Provider provider; + first = provider.open("", false); + requested = false; + second = provider.open("", false); + }); + worker.join(); + std::_Exit(!first && !second && requestCount == 2 ? 0 : 1); + } + }, ::testing::ExitedWithCode(0), ""); + ::testing::GTEST_FLAG(death_test_style) = previousStyle; +} From a5799c45bc6ea95983627d8d3f706896b88d4c44 Mon Sep 17 00:00:00 2001 From: wy Date: Thu, 19 Nov 2026 13:21:30 +0800 Subject: [PATCH 2/3] ci: fix Rust lint and Linux ARM64 test setup --- .github/workflows/linux-build.yml | 10 +--------- bindings/rust/src/provider.rs | 7 +++++-- 2 files changed, 6 insertions(+), 11 deletions(-) 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() From 685363c270071b6e6f850225a6f48ad669cb6b7d Mon Sep 17 00:00:00 2001 From: wy Date: Thu, 19 Nov 2026 13:25:01 +0800 Subject: [PATCH 3/3] fix(macos): finish session configuration on camera open failure --- src/ccap_imp_apple.mm | 12 +++++++++--- tests/test_apple_permission.mm | 34 ++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/ccap_imp_apple.mm b/src/ccap_imp_apple.mm index a8c9cad..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; diff --git a/tests/test_apple_permission.mm b/tests/test_apple_permission.mm index 31144e0..953ac41 100644 --- a/tests/test_apple_permission.mm +++ b/tests/test_apple_permission.mm @@ -13,8 +13,11 @@ 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; } @@ -25,9 +28,15 @@ void mockRequestAccess(id, SEL, AVMediaType, void (^completion)(BOOL)) { completion(NO); }); } + +AVCaptureDevice* mockDefaultDevice(id, SEL, AVMediaType) { return nil; } + +void mockBeginConfiguration(id, SEL) { ++configurationDepth; } + +void mockCommitConfiguration(id, SEL) { --configurationDepth; } } // namespace -TEST(AppleCameraPermission, WorkerWithoutMainRunLoopCanRetryAfterDenial) { +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); @@ -36,24 +45,33 @@ void mockRequestAccess(id, SEL, AVMediaType, void (^completion)(BOOL)) { @autoreleasepool { Method status = class_getClassMethod([AVCaptureDevice class], @selector(authorizationStatusForMediaType:)); Method request = class_getClassMethod([AVCaptureDevice class], @selector(requestAccessForMediaType:completionHandler:)); - if (!status || !request) std::_Exit(2); + 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(); - bool first = true; - bool second = true; - std::thread worker([&] { + std::thread worker([] { ccap::Provider provider; - first = provider.open("", false); + if (provider.open("", false)) std::_Exit(1); requested = false; - second = provider.open("", 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(!first && !second && requestCount == 2 ? 0 : 1); + std::_Exit(0); } }, ::testing::ExitedWithCode(0), ""); ::testing::GTEST_FLAG(death_test_style) = previousStyle;