Skip to content
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
2 changes: 0 additions & 2 deletions Casks/simulatorcamera.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

app "SimulatorCamera.app"

binary "#{appdir}/SimulatorCamera.app/Contents/MacOS/simcamctl"

uninstall delete: [
"/Applications/SimulatorCamera.app",
]
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,13 @@ Open the SimulatorCamera Mac app:

## Driving from the command line

`simcamctl` is bundled with the app and can be symlinked into `$PATH`:
`simcamctl` is **not** bundled in the app or the release download. Build it
from source and put it on your `$PATH`:

```bash
ln -s /Applications/SimulatorCamera.app/Contents/MacOS/simcamctl /usr/local/bin/simcamctl
xcodegen generate
xcodebuild -project SimulatorCamera.xcodeproj -scheme simcamctl -configuration Release build
# copy the built `simcamctl` (see BUILT_PRODUCTS_DIR) to /usr/local/bin

simcamctl ping
# extension reachable: "SimulatorCamera Virtual" source=test pattern streaming=yes
Expand Down
10 changes: 10 additions & 0 deletions SimulatorCamera.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0158D6EC2042D196BF05FD65 /* simcamctl.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = simcamctl.entitlements; sourceTree = "<group>"; };
02097D9CA05CFC06EEBF4035 /* SimulatorCameraTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SimulatorCameraTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
06AC788CDB1F5546EF813A18 /* SimulatorCameraProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimulatorCameraProvider.swift; sourceTree = "<group>"; };
0C92C6072F6DEFA308054810 /* FrameSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FrameSource.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -115,6 +116,7 @@
6104DACFC118A2330797BEC2 /* FeedCommand.swift */,
34DBFC0A082BB7CE216717D6 /* main.swift */,
17E2492228A96A265315275C /* SimCamCLIClient.swift */,
0158D6EC2042D196BF05FD65 /* simcamctl.entitlements */,
);
path = simcamctl;
sourceTree = "<group>";
Expand Down Expand Up @@ -512,9 +514,13 @@
285468F9A97DC35EA929EB78 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_ENTITLEMENTS = simcamctl/simcamctl.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CREATE_INFOPLIST_SECTION_IN_BINARY = YES;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSCameraUsageDescription = "simcamctl feed --camera streams your Mac camera into the iOS Simulator.";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
Expand Down Expand Up @@ -602,9 +608,13 @@
49BC6C74360D1D21B0C20FD8 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_ENTITLEMENTS = simcamctl/simcamctl.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CREATE_INFOPLIST_SECTION_IN_BINARY = YES;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSCameraUsageDescription = "simcamctl feed --camera streams your Mac camera into the iOS Simulator.";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
Expand Down
4 changes: 4 additions & 0 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ targets:
PRODUCT_BUNDLE_IDENTIFIER: $(SIMCAM_BUNDLE_PREFIX).simcamctl
PRODUCT_NAME: simcamctl
CODE_SIGN_STYLE: Automatic
CODE_SIGN_ENTITLEMENTS: simcamctl/simcamctl.entitlements
ENABLE_HARDENED_RUNTIME: YES
GENERATE_INFOPLIST_FILE: YES
CREATE_INFOPLIST_SECTION_IN_BINARY: YES
INFOPLIST_KEY_NSCameraUsageDescription: simcamctl feed --camera streams your Mac camera into the iOS Simulator.
SKIP_INSTALL: NO

# Logic tests for the wire contract shared by all three binaries. Compiles
Expand Down
26 changes: 25 additions & 1 deletion simcamctl/FeedCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,33 @@ final class FeedCommand: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate,
return pb
}

/// Camera capture delivers no frames — and no error — without TCC consent,
/// so resolve the authorization up front and fail loudly instead.
private func requireCameraAccess() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized:
return
case .notDetermined:
let answered = DispatchSemaphore(value: 0)
nonisolated(unsafe) var granted = false
AVCaptureDevice.requestAccess(for: .video) { granted = $0; answered.signal() }
answered.wait()
if !granted { fail(Self.cameraDeniedMessage) }
default:
fail(Self.cameraDeniedMessage)
}
}

private static let cameraDeniedMessage = """
camera access denied. Allow your terminal app (Terminal, iTerm, Warp, …) in \
System Settings → Privacy & Security → Camera, then run this command again. \
Alternatively pick "Mac Camera" in the SimulatorCamera app.
"""

private func startCamera() {
requireCameraAccess()
guard let device = AVCaptureDevice.default(for: .video),
let input = try? AVCaptureDeviceInput(device: device) else { fail("no Mac camera available (or camera access denied)") }
let input = try? AVCaptureDeviceInput(device: device) else { fail("no Mac camera available") }
let s = AVCaptureSession()
s.sessionPreset = .hd1280x720
s.addInput(input)
Expand Down
10 changes: 10 additions & 0 deletions simcamctl/simcamctl.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- `simcamctl feed --camera` captures the Mac webcam; the hardened runtime
blocks that without this entitlement. -->
<key>com.apple.security.device.camera</key>
<true/>
</dict>
</plist>
Loading