diff --git a/Casks/simulatorcamera.rb b/Casks/simulatorcamera.rb index a0b3737..82b0012 100644 --- a/Casks/simulatorcamera.rb +++ b/Casks/simulatorcamera.rb @@ -18,8 +18,6 @@ app "SimulatorCamera.app" - binary "#{appdir}/SimulatorCamera.app/Contents/MacOS/simcamctl" - uninstall delete: [ "/Applications/SimulatorCamera.app", ] diff --git a/README.md b/README.md index 0c83e40..d5a6ea4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/SimulatorCamera.xcodeproj/project.pbxproj b/SimulatorCamera.xcodeproj/project.pbxproj index 0e5cda4..e51e6f2 100644 --- a/SimulatorCamera.xcodeproj/project.pbxproj +++ b/SimulatorCamera.xcodeproj/project.pbxproj @@ -72,6 +72,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 0158D6EC2042D196BF05FD65 /* simcamctl.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = simcamctl.entitlements; sourceTree = ""; }; 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 = ""; }; 0C92C6072F6DEFA308054810 /* FrameSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FrameSource.swift; sourceTree = ""; }; @@ -115,6 +116,7 @@ 6104DACFC118A2330797BEC2 /* FeedCommand.swift */, 34DBFC0A082BB7CE216717D6 /* main.swift */, 17E2492228A96A265315275C /* SimCamCLIClient.swift */, + 0158D6EC2042D196BF05FD65 /* simcamctl.entitlements */, ); path = simcamctl; sourceTree = ""; @@ -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", @@ -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", diff --git a/project.yml b/project.yml index f7fc4a6..e466d38 100644 --- a/project.yml +++ b/project.yml @@ -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 diff --git a/simcamctl/FeedCommand.swift b/simcamctl/FeedCommand.swift index c126362..169fe64 100644 --- a/simcamctl/FeedCommand.swift +++ b/simcamctl/FeedCommand.swift @@ -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) diff --git a/simcamctl/simcamctl.entitlements b/simcamctl/simcamctl.entitlements new file mode 100644 index 0000000..26518d1 --- /dev/null +++ b/simcamctl/simcamctl.entitlements @@ -0,0 +1,10 @@ + + + + + + com.apple.security.device.camera + + +