Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions .github/workflows/linux-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions bindings/rust/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 16 additions & 5 deletions src/ccap_imp_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -1254,4 +1265,4 @@ - (void)captureOutput:(AVCaptureOutput*)output

} // namespace ccap

#endif
#endif
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions tests/test_apple_permission.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/** Regression coverage for the real Apple camera authorization path. */
#include "ccap.h"
#include <gtest/gtest.h>

#import <AVFoundation/AVFoundation.h>
#import <objc/runtime.h>

#include <atomic>
#include <chrono>
#include <cstdlib>
#include <thread>

namespace {
std::atomic<bool> requested{false};
std::atomic<int> 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<IMP>(mockAuthorizationStatus));
method_setImplementation(request, reinterpret_cast<IMP>(mockRequestAccess));
method_setImplementation(device, reinterpret_cast<IMP>(mockDefaultDevice));
method_setImplementation(begin, reinterpret_cast<IMP>(mockBeginConfiguration));
method_setImplementation(commit, reinterpret_cast<IMP>(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;
}
Loading