Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macos): prevent indefinite hanging if screen capture is not granted #3360

Merged
Merged
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
8 changes: 8 additions & 0 deletions src/platform/macos/display.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "src/platform/common.h"
#include "src/platform/macos/av_img_t.h"
#include "src/platform/macos/av_video.h"
#include "src/platform/macos/misc.h"
#include "src/platform/macos/nv12_zero_device.h"

#include "src/config.h"
Expand Down Expand Up @@ -100,6 +101,13 @@

int
dummy_img(img_t *img) override {
if (!platf::is_screen_capture_allowed()) {
// If we don't have the screen capture permission, this function will hang
// indefinitely without doing anything useful. Exit instead to avoid this.
// A non-zero return value indicates failure to the calling function.
return 1;

Check warning on line 108 in src/platform/macos/display.mm

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/display.mm#L108

Added line #L108 was not covered by tests
}

auto signal = [av_capture capture:^(CMSampleBufferRef sampleBuffer) {
auto new_sample_buffer = std::make_shared<av_sample_buf_t>(sampleBuffer);
auto new_pixel_buffer = std::make_shared<av_pixel_buf_t>(new_sample_buffer->buf);
Expand Down
5 changes: 5 additions & 0 deletions src/platform/macos/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

#include <CoreGraphics/CoreGraphics.h>

namespace platf {
bool
is_screen_capture_allowed();
}

namespace dyn {
typedef void (*apiproc)();

Expand Down
12 changes: 12 additions & 0 deletions src/platform/macos/misc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
CGRequestScreenCaptureAccess(void) __attribute__((weak_import));
#endif

namespace {
auto screen_capture_allowed = std::atomic<bool> { false };
} // namespace

// Return whether screen capture is allowed for this process.
bool
is_screen_capture_allowed() {
return screen_capture_allowed;
}

std::unique_ptr<deinit_t>
init() {
// This will generate a warning about CGPreflightScreenCaptureAccess and
Expand All @@ -68,6 +78,8 @@
return nullptr;
}
#pragma clang diagnostic pop
// Record that we determined that we have the screen capture permission.
screen_capture_allowed = true;
return std::make_unique<deinit_t>();
}

Expand Down
Loading