Open source CapCut alternative.
TypeScript
feat(ios): a watch app that records, shows the clock and streams the picture
David Lea558e8cc847b
diff --git a/apps/ios/Donkey.xcodeproj/project.pbxproj b/apps/ios/Donkey.xcodeproj/project.pbxprojindex 2849517f..9f3f1327 100644--- a/apps/ios/Donkey.xcodeproj/project.pbxproj+++ b/apps/ios/Donkey.xcodeproj/project.pbxproj@@ -10,11 +10,38 @@A100000100000000000000B1 /* DonkeyKitModels in Frameworks */ = {isa = PBXBuildFile; productRef = A100001100000000000000B1 /* DonkeyKitModels */; };A100000200000000000000B1 /* DonkeyKitUI in Frameworks */ = {isa = PBXBuildFile; productRef = A100001200000000000000B1 /* DonkeyKitUI */; };A100000300000000000000B1 /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = A100001300000000000000B1 /* GoogleSignIn */; };+ A200000100000000000000B1 /* DonkeyWatch.app in Embed Watch Content */ = {isa = PBXBuildFile; fileRef = A200002100000000000000B1 /* DonkeyWatch.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };+ A200000200000000000000B1 /* DonkeyKitModels in Frameworks */ = {isa = PBXBuildFile; productRef = A200001100000000000000B1 /* DonkeyKitModels */; };/* End PBXBuildFile section */+/* Begin PBXContainerItemProxy section */+ A200003300000000000000B1 /* PBXContainerItemProxy */ = {+ isa = PBXContainerItemProxy;+ containerPortal = A100008100000000000000B1 /* Project object */;+ proxyType = 1;+ remoteGlobalIDString = A200004100000000000000B1;+ remoteInfo = DonkeyWatch;+ };+/* End PBXContainerItemProxy section */++/* Begin PBXCopyFilesBuildPhase section */+ A200005400000000000000B1 /* Embed Watch Content */ = {+ isa = PBXCopyFilesBuildPhase;+ buildActionMask = 2147483647;+ dstPath = "$(CONTENTS_FOLDER_PATH)/Watch";+ dstSubfolderSpec = 16;+ files = (+ A200000100000000000000B1 /* DonkeyWatch.app in Embed Watch Content */,+ );+ name = "Embed Watch Content";+ runOnlyForDeploymentPostprocessing = 0;+ };+/* End PBXCopyFilesBuildPhase section */+/* Begin PBXFileReference section */A100002100000000000000B1 /* Donkey.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Donkey.app; sourceTree = BUILT_PRODUCTS_DIR; };A100002200000000000000B1 /* Donkey.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Donkey.xcconfig; path = Config/Donkey.xcconfig; sourceTree = "<group>"; };+ A200002100000000000000B1 /* DonkeyWatch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DonkeyWatch.app; sourceTree = BUILT_PRODUCTS_DIR; };/* End PBXFileReference section *//* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */@@ -36,6 +63,11 @@path = Donkey;sourceTree = "<group>";};+ A200003200000000000000B1 /* DonkeyWatch */ = {+ isa = PBXFileSystemSynchronizedRootGroup;+ path = DonkeyWatch;+ sourceTree = "<group>";+ };/* End PBXFileSystemSynchronizedRootGroup section *//* Begin PBXFrameworksBuildPhase section */@@ -49,6 +81,14 @@);runOnlyForDeploymentPostprocessing = 0;};+ A200005100000000000000B1 /* Frameworks */ = {+ isa = PBXFrameworksBuildPhase;+ buildActionMask = 2147483647;+ files = (+ A200000200000000000000B1 /* DonkeyKitModels in Frameworks */,+ );+ runOnlyForDeploymentPostprocessing = 0;+ };/* End PBXFrameworksBuildPhase section *//* Begin PBXGroup section */@@ -56,6 +96,7 @@isa = PBXGroup;children = (A100003200000000000000B1 /* Donkey */,+ A200003200000000000000B1 /* DonkeyWatch */,A100002200000000000000B1 /* Donkey.xcconfig */,A100006200000000000000B1 /* Products */,);@@ -65,6 +106,7 @@isa = PBXGroup;children = (A100002100000000000000B1 /* Donkey.app */,+ A200002100000000000000B1 /* DonkeyWatch.app */,);name = Products;sourceTree = "<group>";@@ -79,10 +121,12 @@A100005200000000000000B1 /* Sources */,A100005100000000000000B1 /* Frameworks */,A100005300000000000000B1 /* Resources */,+ A200005400000000000000B1 /* Embed Watch Content */,);buildRules = ();dependencies = (+ A200003400000000000000B1 /* PBXTargetDependency */,);fileSystemSynchronizedGroups = (A100003200000000000000B1 /* Donkey */,@@ -97,6 +141,29 @@productReference = A100002100000000000000B1 /* Donkey.app */;productType = "com.apple.product-type.application";};+ A200004100000000000000B1 /* DonkeyWatch */ = {+ isa = PBXNativeTarget;+ buildConfigurationList = A200007100000000000000B1 /* Build configuration list for PBXNativeTarget "DonkeyWatch" */;+ buildPhases = (+ A200005200000000000000B1 /* Sources */,+ A200005100000000000000B1 /* Frameworks */,+ A200005300000000000000B1 /* Resources */,+ );+ buildRules = (+ );+ dependencies = (+ );+ fileSystemSynchronizedGroups = (+ A200003200000000000000B1 /* DonkeyWatch */,+ );+ name = DonkeyWatch;+ packageProductDependencies = (+ A200001100000000000000B1 /* DonkeyKitModels */,+ );+ productName = DonkeyWatch;+ productReference = A200002100000000000000B1 /* DonkeyWatch.app */;+ productType = "com.apple.product-type.application";+ };/* End PBXNativeTarget section *//* Begin PBXProject section */@@ -110,6 +177,9 @@A100004100000000000000B1 = {CreatedOnToolsVersion = 26.0;};+ A200004100000000000000B1 = {+ CreatedOnToolsVersion = 26.0;+ };};};buildConfigurationList = A100007200000000000000B1 /* Build configuration list for PBXProject "Donkey" */;@@ -131,6 +201,7 @@projectRoot = "";targets = (A100004100000000000000B1 /* Donkey */,+ A200004100000000000000B1 /* DonkeyWatch */,);};/* End PBXProject section */@@ -143,6 +214,13 @@);runOnlyForDeploymentPostprocessing = 0;};+ A200005300000000000000B1 /* Resources */ = {+ isa = PBXResourcesBuildPhase;+ buildActionMask = 2147483647;+ files = (+ );+ runOnlyForDeploymentPostprocessing = 0;+ };/* End PBXResourcesBuildPhase section *//* Begin PBXSourcesBuildPhase section */@@ -153,8 +231,23 @@);runOnlyForDeploymentPostprocessing = 0;};+ A200005200000000000000B1 /* Sources */ = {+ isa = PBXSourcesBuildPhase;+ buildActionMask = 2147483647;+ files = (+ );+ runOnlyForDeploymentPostprocessing = 0;+ };/* End PBXSourcesBuildPhase section */+/* Begin PBXTargetDependency section */+ A200003400000000000000B1 /* PBXTargetDependency */ = {+ isa = PBXTargetDependency;+ target = A200004100000000000000B1 /* DonkeyWatch */;+ targetProxy = A200003300000000000000B1 /* PBXContainerItemProxy */;+ };+/* End PBXTargetDependency section */+/* Begin XCBuildConfiguration section */A10000A100000000000000B1 /* Debug */ = {isa = XCBuildConfiguration;@@ -287,6 +380,64 @@};name = Release;};+ A20000A300000000000000B1 /* Debug */ = {+ isa = XCBuildConfiguration;+ buildSettings = {+ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;+ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;+ CODE_SIGN_STYLE = Automatic;+ CURRENT_PROJECT_VERSION = 1;+ DEVELOPMENT_TEAM = Q593LLEXEN;+ ENABLE_PREVIEWS = YES;+ GENERATE_INFOPLIST_FILE = YES;+ INFOPLIST_KEY_CFBundleDisplayName = "Donkey Cut";+ INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";+ INFOPLIST_KEY_WKCompanionAppBundleIdentifier = com.donkeycut.donkeycut;+ INFOPLIST_KEY_WKRunsIndependentlyOfCompanionApp = NO;+ LD_RUNPATH_SEARCH_PATHS = (+ "$(inherited)",+ "@executable_path/Frameworks",+ );+ MARKETING_VERSION = 0.1.0;+ PRODUCT_BUNDLE_IDENTIFIER = com.donkeycut.donkeycut.watchkitapp;+ PRODUCT_NAME = "$(TARGET_NAME)";+ SDKROOT = watchos;+ SKIP_INSTALL = YES;+ SWIFT_EMIT_LOC_STRINGS = YES;+ TARGETED_DEVICE_FAMILY = 4;+ WATCHOS_DEPLOYMENT_TARGET = 26.0;+ };+ name = Debug;+ };+ A20000A400000000000000B1 /* Release */ = {+ isa = XCBuildConfiguration;+ buildSettings = {+ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;+ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;+ CODE_SIGN_STYLE = Automatic;+ CURRENT_PROJECT_VERSION = 1;+ DEVELOPMENT_TEAM = Q593LLEXEN;+ ENABLE_PREVIEWS = YES;+ GENERATE_INFOPLIST_FILE = YES;+ INFOPLIST_KEY_CFBundleDisplayName = "Donkey Cut";+ INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";+ INFOPLIST_KEY_WKCompanionAppBundleIdentifier = com.donkeycut.donkeycut;+ INFOPLIST_KEY_WKRunsIndependentlyOfCompanionApp = NO;+ LD_RUNPATH_SEARCH_PATHS = (+ "$(inherited)",+ "@executable_path/Frameworks",+ );+ MARKETING_VERSION = 0.1.0;+ PRODUCT_BUNDLE_IDENTIFIER = com.donkeycut.donkeycut.watchkitapp;+ PRODUCT_NAME = "$(TARGET_NAME)";+ SDKROOT = watchos;+ SKIP_INSTALL = YES;+ SWIFT_EMIT_LOC_STRINGS = YES;+ TARGETED_DEVICE_FAMILY = 4;+ WATCHOS_DEPLOYMENT_TARGET = 26.0;+ };+ name = Release;+ };/* End XCBuildConfiguration section *//* Begin XCConfigurationList section */@@ -308,6 +459,15 @@defaultConfigurationIsVisible = 0;defaultConfigurationName = Release;};+ A200007100000000000000B1 /* Build configuration list for PBXNativeTarget "DonkeyWatch" */ = {+ isa = XCConfigurationList;+ buildConfigurations = (+ A20000A300000000000000B1 /* Debug */,+ A20000A400000000000000B1 /* Release */,+ );+ defaultConfigurationIsVisible = 0;+ defaultConfigurationName = Release;+ };/* End XCConfigurationList section *//* Begin XCLocalSwiftPackageReference section */@@ -342,6 +502,10 @@package = A100009200000000000000B1 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */;productName = GoogleSignIn;};+ A200001100000000000000B1 /* DonkeyKitModels */ = {+ isa = XCSwiftPackageProductDependency;+ productName = DonkeyKitModels;+ };/* End XCSwiftPackageProductDependency section */};rootObject = A100008100000000000000B1 /* Project object */;diff --git a/apps/ios/Donkey/Controllers/CameraController.swift b/apps/ios/Donkey/Controllers/CameraController.swiftindex f03c7d05..9df9b625 100644--- a/apps/ios/Donkey/Controllers/CameraController.swift+++ b/apps/ios/Donkey/Controllers/CameraController.swift@@ -1,5 +1,7 @@import AVFoundation+import CoreImageimport DonkeyKitModels+import ImageIOimport UIKit/// Owns the capture side of the camera feature: permission prompts, the@@ -61,6 +63,9 @@ final class CameraController: CameraControlling {func apply(_ settings: CameraSettings) { engine.apply(settings) }func startRecording() { engine.startRecording() }func stopRecording() { engine.stopRecording() }+ /// Hands small JPEG frames of the live picture to `sink` until it is+ /// set to nil. The watch is the one taker.+ func setPreviewSink(_ sink: (@Sendable (Data) -> Void)?) { engine.setPreviewSink(sink) }/// Follows the phone. The coordinator reads the device against the/// preview layer: the preview takes the angle that keeps it upright on@@ -119,7 +124,7 @@ final class CameraController: CameraControlling {/// All AVFoundation state, confined to one serial queue per Apple's capture/// guidance. `@unchecked Sendable` is the queue-confinement contract: nothing/// here is touched off `queue` after init.-private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecordingDelegate, @unchecked Sendable {+private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecordingDelegate, AVCaptureVideoDataOutputSampleBufferDelegate, @unchecked Sendable {enum Event: Sendable {case running(ZoomMapping, hasTorch: Bool, effective: CameraSettings)case failed(reason: String)@@ -133,6 +138,21 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecoprivate let queue = DispatchQueue(label: "com.donkeycut.donkeycut.capture")private let movieOutput = AVCaptureMovieFileOutput()+ /// The live picture as sample buffers, for the watch's preview. Its+ /// connection stays disabled until something asks for frames, so the+ /// output costs nothing while nobody is watching.+ private let previewOutput = AVCaptureVideoDataOutput()+ /// Frames arrive here; the sink, the throttle and the CI context are+ /// confined to it.+ private let previewQueue = DispatchQueue(label: "com.donkeycut.donkeycut.capture.preview")+ private var previewSink: (@Sendable (Data) -> Void)?+ private var previewSentAt: TimeInterval = 0+ private let previewContext = CIContext()+ /// Frames a second the preview offers. The link to the watch gates the+ /// rate below this on its own.+ private static let previewFrameRate: Double = 8+ /// The long side of a preview frame in pixels: a watch screen's worth.+ private static let previewLongSide: CGFloat = 272private var videoInput: AVCaptureDeviceInput?private var facing: CameraFacing = .frontprivate var settings = CameraSettings()@@ -176,6 +196,15 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecoif self.session.canAddOutput(self.movieOutput) {self.session.addOutput(self.movieOutput)}+ self.previewOutput.videoSettings = [+ kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,+ ]+ self.previewOutput.alwaysDiscardsLateVideoFrames = true+ self.previewOutput.setSampleBufferDelegate(self, queue: self.previewQueue)+ if self.session.canAddOutput(self.previewOutput) {+ self.session.addOutput(self.previewOutput)+ self.previewOutput.connection(with: .video)?.isEnabled = false+ }self.session.commitConfiguration()}self.installCamera(facing: facing)@@ -218,6 +247,7 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecoqueue.async {self.captureRotation = angleself.applyCaptureRotation()+ self.applyPreviewAim()}}@@ -257,6 +287,35 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputReco}}+ func setPreviewSink(_ sink: (@Sendable (Data) -> Void)?) {+ previewQueue.async { self.previewSink = sink }+ queue.async { self.previewOutput.connection(with: .video)?.isEnabled = sink != nil }+ }++ // MARK: AVCaptureVideoDataOutputSampleBufferDelegate++ func captureOutput(+ _ output: AVCaptureOutput,+ didOutput sampleBuffer: CMSampleBuffer,+ from connection: AVCaptureConnection+ ) {+ guard let previewSink else { return }+ let now = Date.now.timeIntervalSinceReferenceDate+ guard now - previewSentAt >= 1 / Self.previewFrameRate,+ let pixelBuffer = sampleBuffer.imageBuffer else { return }+ previewSentAt = now+ let image = CIImage(cvPixelBuffer: pixelBuffer)+ let scale = Self.previewLongSide / max(image.extent.width, image.extent.height)+ let small = image.transformed(by: CGAffineTransform(scaleX: scale, y: scale))+ guard let colorSpace = CGColorSpace(name: CGColorSpace.sRGB),+ let data = previewContext.jpegRepresentation(+ of: small,+ colorSpace: colorSpace,+ options: [CIImageRepresentationOption(rawValue: kCGImageDestinationLossyCompressionQuality as String): 0.5]+ ) else { return }+ previewSink(data)+ }+// MARK: Queue-confined configurationprivate func installCamera(facing: CameraFacing) {@@ -281,6 +340,21 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecosession.commitConfiguration()applyFormat()setZoomImmediate(display: 1)+ applyPreviewAim()+ }++ /// The preview frames leave upright and, from the front camera, mirrored+ /// the way the phone's own viewfinder is, so the watch shows the same+ /// picture the person sees on the phone.+ private func applyPreviewAim() {+ guard let connection = previewOutput.connection(with: .video) else { return }+ if connection.isVideoRotationAngleSupported(captureRotation) {+ connection.videoRotationAngle = captureRotation+ }+ if connection.isVideoMirroringSupported {+ connection.automaticallyAdjustsVideoMirroring = false+ connection.isVideoMirrored = facing == .front+ }}static func camera(for facing: CameraFacing) -> AVCaptureDevice? {diff --git a/apps/ios/Donkey/Controllers/WatchLinkController.swift b/apps/ios/Donkey/Controllers/WatchLinkController.swiftnew file mode 100644index 00000000..61bfcb8f--- /dev/null+++ b/apps/ios/Donkey/Controllers/WatchLinkController.swift@@ -0,0 +1,150 @@+import DonkeyKitModels+import Synchronization+import WatchConnectivity++/// The phone end of the watch app. It tells the watch what the camera is+/// doing, carries the watch's record button to the camera, and streams a+/// small picture while the watch is looking.+///+/// The camera runs only while its tab is on screen, so a record command that+/// arrives on another tab switches to the camera first and starts the take+/// once it is running.+final class WatchLinkController: NSObject, WCSessionDelegate {+ private let app: AppModel+ private let camera: CameraModel+ private let cameraController: CameraController+ private let session = WCSession.default+ /// A record command waiting on the camera to come up, and when it lapses:+ /// a take nobody is expecting any more must never start on its own.+ private var pendingRecordUntil: Date?+ /// Frames on the wire the watch has not acknowledged. The link carries+ /// one at a time, so a slow link drops frames at the source.+ private nonisolated let framesInFlight = Mutex(0)++ init(app: AppModel, camera: CameraModel, cameraController: CameraController) {+ self.app = app+ self.camera = camera+ self.cameraController = cameraController+ super.init()+ guard WCSession.isSupported() else { return }+ session.delegate = self+ session.activate()+ observeCamera()+ }++ private func observeCamera() {+ withObservationTracking {+ _ = camera.recordingStartedAt+ _ = camera.availability+ } onChange: {+ Task { @MainActor [weak self] in+ guard let self else { return }+ cameraDidChange()+ observeCamera()+ }+ }+ }++ private var state: CameraRemoteState {+ CameraRemoteState(+ isCameraOpen: camera.availability == .running,+ recordingElapsed: camera.recordingStartedAt.map { Date.now.timeIntervalSince($0) }+ )+ }++ private func cameraDidChange() {+ if let pendingRecordUntil, camera.availability == .running {+ self.pendingRecordUntil = nil+ if pendingRecordUntil > .now, !camera.isRecording {+ camera.toggleRecording()+ }+ }+ publishState()+ }++ private func publishState() {+ guard session.activationState == .activated, session.isReachable else { return }+ session.sendMessage(CameraRemoteMessage.encode(state), replyHandler: nil)+ }++ private func handle(_ command: CameraRemoteCommand) {+ switch command {+ case .hello:+ break+ case .toggleRecording:+ if camera.availability == .running {+ camera.toggleRecording()+ } else {+ pendingRecordUntil = .now.addingTimeInterval(5)+ app.selectedTab = .camera+ }+ case .preview(let wanted):+ setPreview(wanted)+ }+ }++ private func setPreview(_ wanted: Bool) {+ guard wanted else {+ cameraController.setPreviewSink(nil)+ return+ }+ cameraController.setPreviewSink { [weak self] data in self?.send(frame: data) }+ }++ private nonisolated func send(frame: Data) {+ let session = WCSession.default+ guard session.isReachable else { return }+ let claimed = framesInFlight.withLock { count in+ guard count < 1 else { return false }+ count += 1+ return true+ }+ guard claimed else { return }+ let release: @Sendable () -> Void = { [weak self] in+ self?.framesInFlight.withLock { $0 -= 1 }+ }+ session.sendMessageData(frame, replyHandler: { _ in release() }, errorHandler: { _ in release() })+ }++ // MARK: WCSessionDelegate++ nonisolated func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: (any Error)?) {+ Task { @MainActor in publishState() }+ }++ nonisolated func sessionDidBecomeInactive(_ session: WCSession) {}++ nonisolated func sessionDidDeactivate(_ session: WCSession) {+ session.activate()+ }++ nonisolated func sessionReachabilityDidChange(_ session: WCSession) {+ let reachable = session.isReachable+ Task { @MainActor in+ if reachable {+ publishState()+ } else {+ setPreview(false)+ }+ }+ }++ nonisolated func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {+ guard let command = CameraRemoteMessage.command(in: message) else { return }+ Task { @MainActor in handle(command) }+ }++ nonisolated func session(_ session: WCSession, didReceiveMessage message: [String: Any], replyHandler: @escaping ([String: Any]) -> Void) {+ guard let command = CameraRemoteMessage.command(in: message) else {+ replyHandler([:])+ return+ }+ // The reply is called once, from the main actor; WatchConnectivity+ // takes it on any thread.+ nonisolated(unsafe) let reply = replyHandler+ Task { @MainActor in+ handle(command)+ reply(CameraRemoteMessage.encode(state))+ }+ }+}diff --git a/apps/ios/Donkey/DonkeyApp.swift b/apps/ios/Donkey/DonkeyApp.swiftindex a38e57be..b6b5f61c 100644--- a/apps/ios/Donkey/DonkeyApp.swift+++ b/apps/ios/Donkey/DonkeyApp.swift@@ -53,6 +53,7 @@ final class AppWiring {let auth: AuthModellet analytics: AnalyticsModellet cameraController: CameraController+ let watchLink: WatchLinkControllerlet sync: SyncEngineprivate let networkMonitor: NetworkMonitor@@ -92,6 +93,7 @@ final class AppWiring {cameraController.model = cameracamera.controller = cameraController+ watchLink = WatchLinkController(app: app, camera: camera, cameraController: cameraController)cameraController.onRecordingFinished = { [media] url, duration, thumbnail inmedia.ingest(movieAt: url, duration: duration, thumbnail: thumbnail)}diff --git a/apps/ios/DonkeyKit/Package.swift b/apps/ios/DonkeyKit/Package.swiftindex 7df4eda7..e75b660c 100644--- a/apps/ios/DonkeyKit/Package.swift+++ b/apps/ios/DonkeyKit/Package.swift@@ -7,7 +7,7 @@ import PackageDescription// controllers. Both default to main-actor isolation per docs/guides/swift.md.let package = Package(name: "DonkeyKit",- platforms: [.iOS(.v26), .macOS(.v26)],+ platforms: [.iOS(.v26), .macOS(.v26), .watchOS(.v26)],products: [.library(name: "DonkeyKitModels", targets: ["DonkeyKitModels"]),.library(name: "DonkeyKitUI", targets: ["DonkeyKitUI"]),diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraRemote.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraRemote.swiftnew file mode 100644index 00000000..b1b6dd04--- /dev/null+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraRemote.swift@@ -0,0 +1,55 @@+import Foundation++/// The camera as the watch sees it. The phone sends one of these whenever+/// the camera's state changes and in reply to a hello.+///+/// A take's age travels as elapsed seconds, so the watch starts its own+/// clock on receipt and never depends on the two clocks agreeing.+nonisolated public struct CameraRemoteState: Codable, Equatable, Sendable {+ /// True while the phone's camera is on screen and running.+ public var isCameraOpen: Bool+ /// Seconds into the take, or nil when the camera is not recording.+ public var recordingElapsed: TimeInterval?++ public init(isCameraOpen: Bool = false, recordingElapsed: TimeInterval? = nil) {+ self.isCameraOpen = isCameraOpen+ self.recordingElapsed = recordingElapsed+ }++ public var isRecording: Bool { recordingElapsed != nil }+}++/// What the watch asks of the phone.+nonisolated public enum CameraRemoteCommand: Codable, Equatable, Sendable {+ /// The watch came on screen and wants the state back in the reply.+ case hello+ /// Start a take, or stop the one running.+ case toggleRecording+ /// Whether the watch is looking at the picture and wants frames.+ case preview(Bool)+}++/// The envelope both ends put a state or a command in: one key, one JSON+/// value, inside a WatchConnectivity message dictionary.+nonisolated public enum CameraRemoteMessage {+ public static let stateKey = "state"+ public static let commandKey = "command"++ public static func encode(_ state: CameraRemoteState) -> [String: Any] {+ [stateKey: (try? JSONEncoder().encode(state)) ?? Data()]+ }++ public static func encode(_ command: CameraRemoteCommand) -> [String: Any] {+ [commandKey: (try? JSONEncoder().encode(command)) ?? Data()]+ }++ public static func state(in message: [String: Any]) -> CameraRemoteState? {+ guard let data = message[stateKey] as? Data else { return nil }+ return try? JSONDecoder().decode(CameraRemoteState.self, from: data)+ }++ public static func command(in message: [String: Any]) -> CameraRemoteCommand? {+ guard let data = message[commandKey] as? Data else { return nil }+ return try? JSONDecoder().decode(CameraRemoteCommand.self, from: data)+ }+}diff --git a/apps/ios/DonkeyWatch/Assets.xcassets/AccentColor.colorset/Contents.json b/apps/ios/DonkeyWatch/Assets.xcassets/AccentColor.colorset/Contents.jsonnew file mode 100644index 00000000..ba142414--- /dev/null+++ b/apps/ios/DonkeyWatch/Assets.xcassets/AccentColor.colorset/Contents.json@@ -0,0 +1,12 @@+{+ "colors" : [+ {+ "color" : {+ "color-space" : "srgb",+ "components" : { "alpha" : "1.000", "blue" : "0xFF", "green" : "0x84", "red" : "0x0A" }+ },+ "idiom" : "universal"+ }+ ],+ "info" : { "author" : "xcode", "version" : 1 }+}diff --git a/apps/ios/DonkeyWatch/Assets.xcassets/AppIcon.appiconset/AppIcon.png b/apps/ios/DonkeyWatch/Assets.xcassets/AppIcon.appiconset/AppIcon.pngnew file mode 100644index 00000000..b3eed43eBinary files /dev/null and b/apps/ios/DonkeyWatch/Assets.xcassets/AppIcon.appiconset/AppIcon.png differdiff --git a/apps/ios/DonkeyWatch/Assets.xcassets/AppIcon.appiconset/Contents.json b/apps/ios/DonkeyWatch/Assets.xcassets/AppIcon.appiconset/Contents.jsonnew file mode 100644index 00000000..6232d0ca--- /dev/null+++ b/apps/ios/DonkeyWatch/Assets.xcassets/AppIcon.appiconset/Contents.json@@ -0,0 +1,11 @@+{+ "images" : [+ {+ "filename" : "AppIcon.png",+ "idiom" : "universal",+ "platform" : "watchos",+ "size" : "1024x1024"+ }+ ],+ "info" : { "author" : "xcode", "version" : 1 }+}diff --git a/apps/ios/DonkeyWatch/Assets.xcassets/Contents.json b/apps/ios/DonkeyWatch/Assets.xcassets/Contents.jsonnew file mode 100644index 00000000..d8b757af--- /dev/null+++ b/apps/ios/DonkeyWatch/Assets.xcassets/Contents.json@@ -0,0 +1,3 @@+{+ "info" : { "author" : "xcode", "version" : 1 }+}diff --git a/apps/ios/DonkeyWatch/DonkeyWatchApp.swift b/apps/ios/DonkeyWatch/DonkeyWatchApp.swiftnew file mode 100644index 00000000..88cd2321--- /dev/null+++ b/apps/ios/DonkeyWatch/DonkeyWatchApp.swift@@ -0,0 +1,20 @@+import SwiftUI++/// The watch is a remote for the phone's camera: a record button, a red+/// clock while a take runs, and the picture so the shot can be framed from+/// across the room.+@main+struct DonkeyWatchApp: App {+ @State private var link = WatchCameraLink()+ @Environment(\.scenePhase) private var scenePhase++ var body: some Scene {+ WindowGroup {+ WatchCameraView(link: link)+ }+ // Frames flow only while the watch is looking.+ .onChange(of: scenePhase, initial: true) { _, phase in+ link.setWatching(phase == .active)+ }+ }+}diff --git a/apps/ios/DonkeyWatch/WatchCameraLink.swift b/apps/ios/DonkeyWatch/WatchCameraLink.swiftnew file mode 100644index 00000000..8c71ce55--- /dev/null+++ b/apps/ios/DonkeyWatch/WatchCameraLink.swift@@ -0,0 +1,96 @@+import DonkeyKitModels+import UIKit+import WatchConnectivity+import WatchKit++/// The watch end of the camera remote: the phone's camera state, the latest+/// preview frame, and the commands the screen sends back.+@Observable+final class WatchCameraLink: NSObject, WCSessionDelegate {+ private(set) var state = CameraRemoteState()+ private(set) var frame: UIImage?+ private(set) var isPhoneReachable = false+ /// When the take began, on this watch's clock. Set once when a take+ /// starts so the timer never jumps as later state messages land.+ private(set) var recordingStartedAt: Date?++ private let session = WCSession.default+ private var isWatching = false++ override init() {+ super.init()+ guard WCSession.isSupported() else { return }+ session.delegate = self+ session.activate()+ }++ func toggleRecording() {+ guard session.isReachable else { return }+ session.sendMessage(CameraRemoteMessage.encode(.toggleRecording), replyHandler: nil)+ }++ /// Whether the screen is up. On, the phone is asked for its state and+ /// starts sending frames; off, the frames stop.+ func setWatching(_ watching: Bool) {+ isWatching = watching+ guard session.activationState == .activated, session.isReachable else { return }+ session.sendMessage(CameraRemoteMessage.encode(.preview(watching)), replyHandler: nil)+ if watching {+ session.sendMessage(CameraRemoteMessage.encode(.hello)) { [weak self] reply in+ guard let state = CameraRemoteMessage.state(in: reply) else { return }+ Task { @MainActor in self?.apply(state) }+ }+ }+ }++ private func apply(_ state: CameraRemoteState) {+ self.state = state+ switch (recordingStartedAt, state.recordingElapsed) {+ case (nil, let elapsed?):+ recordingStartedAt = .now.addingTimeInterval(-elapsed)+ WKInterfaceDevice.current().play(.start)+ case (.some, nil):+ recordingStartedAt = nil+ WKInterfaceDevice.current().play(.stop)+ default:+ break+ }+ if !state.isCameraOpen {+ frame = nil+ }+ }++ // MARK: WCSessionDelegate++ nonisolated func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: (any Error)?) {+ let reachable = session.isReachable+ Task { @MainActor in+ isPhoneReachable = reachable+ setWatching(isWatching)+ }+ }++ nonisolated func sessionReachabilityDidChange(_ session: WCSession) {+ let reachable = session.isReachable+ Task { @MainActor in+ isPhoneReachable = reachable+ if reachable {+ setWatching(isWatching)+ } else {+ apply(CameraRemoteState())+ }+ }+ }++ nonisolated func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {+ guard let state = CameraRemoteMessage.state(in: message) else { return }+ Task { @MainActor in apply(state) }+ }++ nonisolated func session(_ session: WCSession, didReceiveMessageData messageData: Data, replyHandler: @escaping (Data) -> Void) {+ // The reply is the phone's cue to send the next frame.+ replyHandler(Data())+ guard let image = UIImage(data: messageData) else { return }+ Task { @MainActor in frame = image }+ }+}diff --git a/apps/ios/DonkeyWatch/WatchCameraView.swift b/apps/ios/DonkeyWatch/WatchCameraView.swiftnew file mode 100644index 00000000..1972c8d3--- /dev/null+++ b/apps/ios/DonkeyWatch/WatchCameraView.swift@@ -0,0 +1,98 @@+import DonkeyKitModels+import SwiftUI++struct WatchCameraView: View {+ var link: WatchCameraLink++ var body: some View {+ ZStack {+ picture+ VStack(spacing: 0) {+ if let startedAt = link.recordingStartedAt {+ RecordingClock(startedAt: startedAt)+ .padding(.top, 4)+ }+ Spacer()+ recordButton+ .padding(.bottom, 6)+ }+ }+ .frame(maxWidth: .infinity, maxHeight: .infinity)+ .background(.black)+ .ignoresSafeArea()+ .animation(.spring(duration: 0.3), value: link.recordingStartedAt != nil)+ }++ @ViewBuilder private var picture: some View {+ if let frame = link.frame {+ Image(uiImage: frame)+ .resizable()+ .scaledToFill()+ .ignoresSafeArea()+ } else {+ VStack(spacing: 8) {+ Image(systemName: link.isPhoneReachable ? "iphone" : "iphone.slash")+ .font(.title2)+ Text(hint)+ .font(.footnote)+ .multilineTextAlignment(.center)+ }+ .foregroundStyle(.secondary)+ .padding(.horizontal, 12)+ .padding(.bottom, 40)+ }+ }++ private var hint: String {+ if !link.isPhoneReachable { return "Open Donkey Cut on your iPhone" }+ if !link.state.isCameraOpen { return "Open the camera on your iPhone" }+ return "Waiting for the picture"+ }++ private var recordButton: some View {+ Button {+ link.toggleRecording()+ } label: {+ ZStack {+ Circle()+ .strokeBorder(.white, lineWidth: 3)+ .frame(width: 56, height: 56)+ RoundedRectangle(cornerRadius: link.recordingStartedAt != nil ? 5 : 22)+ .fill(.red)+ .frame(+ width: link.recordingStartedAt != nil ? 22 : 44,+ height: link.recordingStartedAt != nil ? 22 : 44+ )+ }+ .animation(.snappy(duration: 0.2), value: link.recordingStartedAt != nil)+ }+ .buttonStyle(.plain)+ .disabled(!link.isPhoneReachable)+ .accessibilityLabel(link.recordingStartedAt != nil ? "Stop recording" : "Start recording")+ }+}++/// The red clock: how long the camera has been rolling, with a tally dot+/// that blinks on the second.+struct RecordingClock: View {+ let startedAt: Date++ var body: some View {+ TimelineView(.periodic(from: .now, by: 0.5)) { context in+ let elapsed = context.date.timeIntervalSince(startedAt)+ HStack(spacing: 5) {+ Circle()+ .fill(.white)+ .frame(width: 7, height: 7)+ .opacity(Int(elapsed * 2) % 2 == 0 ? 1 : 0.3)+ Text(formattedDuration(elapsed))+ .font(.footnote.weight(.bold))+ .monospacedDigit()+ }+ .foregroundStyle(.white)+ .padding(.horizontal, 10)+ .padding(.vertical, 4)+ .background(Color.red.opacity(0.9), in: Capsule())+ }+ }+}diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.mdindex 968744fb..1abffce7 100644--- a/docs/guides/ios-app.md+++ b/docs/guides/ios-app.md@@ -131,6 +131,17 @@ viewfinder.The camera shoots whichever way the phone is held: turn it sideways and thetake is a landscape file, with the shutter across on the trailing edge.+## The Watch++The watch app is a remote for the phone's camera: a record button, a red+clock while a take runs, and the picture itself, so the shot can be framed+from where the person is standing. It works while the camera tab is up on the+phone — a record command sent from another tab switches to the camera first —+and the phone streams small frames only while the watch screen is on. The+phone and the watch talk over Watch Connectivity: the phone sends the camera's+state on every change, and the watch sends its commands and acknowledges each+frame, which is how the stream paces itself to the link.+## TeleprompterThe teleprompter paces raw notes by itself. Reading speed is words per@@ -195,7 +206,9 @@ DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer swift test```The camera shows its fallback state in the simulator; capture, torch, zoom,-and HDR need a device.+and HDR need a device. The scheme embeds the watch app, so the watchOS+platform has to be installed in Xcode (Settings → Components, or+`xcodebuild -downloadPlatform watchOS`).### The analytics contract