Open source CapCut alternative.
TypeScript
feat(ios): record in landscape
David Le4e89c1104ba4
diff --git a/apps/ios/Donkey/Controllers/CameraController.swift b/apps/ios/Donkey/Controllers/CameraController.swiftindex 11f80c7a..f03c7d05 100644--- a/apps/ios/Donkey/Controllers/CameraController.swift+++ b/apps/ios/Donkey/Controllers/CameraController.swift@@ -13,6 +13,14 @@ final class CameraController: CameraControlling {var onRecordingFinished: ((URL, TimeInterval, Data?) -> Void)?private let engine = CaptureEngine()+ /// Watches the device against the preview layer so the picture stays+ /// level however the phone is held.+ private var rotationCoordinator: AVCaptureDevice.RotationCoordinator?+ private var rotationObservations: [NSKeyValueObservation] = []+ /// The angle the preview needs right now. Held because the preview+ /// layer has no connection until the session has an input, so the angle+ /// is applied again the moment the session reports running.+ private var previewRotation: CGFloat = 90init() {session = engine.session@@ -31,21 +39,69 @@ final class CameraController: CameraControlling {model?.sessionFailed(reason: "Camera access is off.\nAllow camera access in Settings to record.")return}- engine.start(facing: model?.facing ?? .front, settings: model?.settings ?? CameraSettings())+ let facing = model?.facing ?? .front+ engine.start(facing: facing, settings: model?.settings ?? CameraSettings())+ trackRotation(facing: facing)}}- func deactivate() { engine.stop() }- func setFacing(_ facing: CameraFacing) { engine.set(facing: facing) }+ func deactivate() {+ rotationObservations = []+ rotationCoordinator = nil+ engine.stop()+ }++ func setFacing(_ facing: CameraFacing) {+ engine.set(facing: facing)+ trackRotation(facing: facing)+ }+func setZoom(display: Double) { engine.setZoom(display: display) }func setTorch(_ on: Bool) { engine.setTorch(on) }func apply(_ settings: CameraSettings) { engine.apply(settings) }func startRecording() { engine.startRecording() }func stopRecording() { engine.stopRecording() }+ /// Follows the phone. The coordinator reads the device against the+ /// preview layer: the preview takes the angle that keeps it upright on+ /// screen, and the movie output takes the device's own angle, so a take+ /// shot with the phone sideways writes a landscape file the way the+ /// system camera does.+ private func trackRotation(facing: CameraFacing) {+ guard let device = CaptureEngine.camera(for: facing) else { return }+ let coordinator = AVCaptureDevice.RotationCoordinator(+ device: device,+ previewLayer: previewView.previewLayer+ )+ rotationCoordinator = coordinator+ previewRotation = coordinator.videoRotationAngleForHorizonLevelPreview+ applyPreviewRotation()+ engine.setCaptureRotation(coordinator.videoRotationAngleForHorizonLevelCapture)+ rotationObservations = [+ coordinator.observe(\.videoRotationAngleForHorizonLevelPreview, options: .new) { [weak self] _, change in+ guard let angle = change.newValue else { return }+ Task { @MainActor in+ self?.previewRotation = angle+ self?.applyPreviewRotation()+ }+ },+ coordinator.observe(\.videoRotationAngleForHorizonLevelCapture, options: .new) { [weak self] _, change in+ guard let angle = change.newValue else { return }+ Task { @MainActor in self?.engine.setCaptureRotation(angle) }+ },+ ]+ }++ private func applyPreviewRotation() {+ guard let connection = previewView.previewLayer.connection,+ connection.isVideoRotationAngleSupported(previewRotation) else { return }+ connection.videoRotationAngle = previewRotation+ }+private func handle(_ event: CaptureEngine.Event) {switch event {case .running(let mapping, let hasTorch, let effective):+ applyPreviewRotation()model?.sessionDidStart(zoomMapping: mapping, hasTorch: hasTorch, effective: effective)case .failed(let reason):model?.sessionFailed(reason: reason)@@ -81,6 +137,11 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecoprivate var facing: CameraFacing = .frontprivate var settings = CameraSettings()private var zoomMapping = ZoomMapping(wideBase: 1, minDisplay: 1, maxDisplay: 1)+ /// How the movie output aims the frames it writes, in degrees. The+ /// controller keeps this current as the phone turns; a rolling take+ /// keeps the angle it started with, since a movie file output cannot be+ /// re-aimed once it is writing.+ private var captureRotation: CGFloat = 90private static let recordableDimensions: Set<String> = ["1280x720", "1920x1080", "2560x1440", "3840x2160",@@ -153,6 +214,13 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputReco}}+ func setCaptureRotation(_ angle: CGFloat) {+ queue.async {+ self.captureRotation = angle+ self.applyCaptureRotation()+ }+ }+func setTorch(_ on: Bool) {queue.async {guard let device = self.videoInput?.device, device.hasTorch else { return }@@ -175,6 +243,7 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecofunc startRecording() {queue.async {guard !self.movieOutput.isRecording else { return }+ self.applyCaptureRotation()let url = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString + ".mov")self.movieOutput.startRecording(to: url, recordingDelegate: self)@@ -214,7 +283,7 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecosetZoomImmediate(display: 1)}- private static func camera(for facing: CameraFacing) -> AVCaptureDevice? {+ static func camera(for facing: CameraFacing) -> AVCaptureDevice? {let position: AVCaptureDevice.Position = facing == .front ? .front : .backlet types: [AVCaptureDevice.DeviceType] = position == .back? [.builtInTripleCamera, .builtInDualWideCamera, .builtInDualCamera, .builtInWideAngleCamera]@@ -269,9 +338,20 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputRecomovieOutput.setOutputSettings([AVVideoCodecKey: AVVideoCodecType.hevc], for: connection)}}+ applyCaptureRotation()effectiveSettings = choice.effective}+ /// A take in flight keeps its own angle: AVFoundation gives no defined+ /// result for re-aiming a movie file output mid-recording, so the phone+ /// turning during a take leaves the file as it started.+ private func applyCaptureRotation() {+ guard !movieOutput.isRecording,+ let connection = movieOutput.connection(with: .video),+ connection.isVideoRotationAngleSupported(captureRotation) else { return }+ connection.videoRotationAngle = captureRotation+ }+private var effectiveSettings = CameraSettings()private func setZoomImmediate(display: Double) {diff --git a/apps/ios/Donkey/DonkeyApp.swift b/apps/ios/Donkey/DonkeyApp.swiftindex 0cc17dee..a38e57be 100644--- a/apps/ios/Donkey/DonkeyApp.swift+++ b/apps/ios/Donkey/DonkeyApp.swift@@ -2,9 +2,11 @@ import DonkeyKitModelsimport DonkeyKitUIimport GoogleSignInimport SwiftUI+import UIKit@mainstruct DonkeyApp: App {+ @UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate@State private var wiring = AppWiring()var body: some Scene {@@ -27,6 +29,18 @@ struct DonkeyApp: App {}}+/// The system asks the delegate which way the window may turn, and the+/// camera screen's lock answers: every orientation, except for the length of+/// a take.+final class AppDelegate: NSObject, UIApplicationDelegate {+ func application(+ _ application: UIApplication,+ supportedInterfaceOrientationsFor window: UIWindow?+ ) -> UIInterfaceOrientationMask {+ InterfaceOrientationLock.shared.mask+ }+}+/// Builds the models and controllers once and wires controller events back/// into model state. App entry stays this thin per docs/guides/swift.md.@Observablediff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swiftindex f7518b68..0da4da6d 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift@@ -35,6 +35,12 @@ struct CameraScreen<CameraPreview: View>: View {/// be clear. It stays until the next tap: a hand working the controls is/// never raced by a clock.@State private var showsChrome = false+ @Environment(\.verticalSizeClass) private var verticalSizeClass++ /// The phone held sideways. The shutter crosses to the trailing edge and+ /// the well drops under it, so both stay under the thumb of the hand+ /// gripping the short edge.+ private var isSideways: Bool { verticalSizeClass == .compact }private static var stageSpace: String { "cameraStage" }private static var wellSide: CGFloat { 54 }@@ -86,6 +92,17 @@ struct CameraScreen<CameraPreview: View>: View {// A run begins on a clear stage, and the controls are back the moment// it ends..onChange(of: stageIsClear) { _, _ in showsChrome = false }+ // A take keeps the orientation it started in, so the screen keeps it+ // too: the controls stay where the hand left them and the picture+ // matches the file being written.+ .onChange(of: camera.isRecording) { _, recording in+ if recording {+ InterfaceOrientationLock.shared.hold()+ } else {+ InterfaceOrientationLock.shared.release()+ }+ }+ .onDisappear { InterfaceOrientationLock.shared.release() }// The docked clip keeps itself to one appearance: watching it or// waiting out the timer both retire it..task(id: corner?.id) {@@ -159,8 +176,8 @@ struct CameraScreen<CameraPreview: View>: View {// without this the stack hugs its content and the rail floats mid-screen..frame(maxWidth: .infinity, maxHeight: .infinity).overlay(alignment: .leading) { rail }- .overlay(alignment: .bottom) { recordButton }- .overlay(alignment: .bottomLeading) { thumbnailWell }+ .overlay(alignment: isSideways ? .trailing : .bottom) { recordButton }+ .overlay(alignment: isSideways ? .bottomTrailing : .bottomLeading) { thumbnailWell }.animation(.spring(duration: 0.45), value: corner?.id).animation(.spring(duration: 0.45), value: camera.isRecording).padding(.top, 8)@@ -194,8 +211,8 @@ struct CameraScreen<CameraPreview: View>: View {wellButton(corner)}}- .padding(.leading, 20)- .padding(.bottom, 44)+ .padding(isSideways ? .trailing : .leading, 20)+ .padding(.bottom, isSideways ? 20 : 44)}private func wellButton(_ clip: CornerClip) -> some View {@@ -219,7 +236,7 @@ struct CameraScreen<CameraPreview: View>: View {}.buttonStyle(.plain).id(clip.id)- .transition(.scale(scale: 0.25, anchor: .bottomLeading).combined(with: .opacity))+ .transition(.scale(scale: 0.25, anchor: isSideways ? .bottomTrailing : .bottomLeading).combined(with: .opacity)).accessibilityLabel("Play last recording")}@@ -404,7 +421,7 @@ struct CameraScreen<CameraPreview: View>: View {.buttonStyle(.plain).disabled(camera.availability != .running).opacity(camera.availability == .running ? 1 : 0.5)- .padding(.bottom, 30)+ .padding(isSideways ? .trailing : .bottom, isSideways ? 24 : 30).accessibilityLabel(camera.isRecording ? "Stop recording" : "Record")}}diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/InterfaceOrientation.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/InterfaceOrientation.swiftnew file mode 100644index 00000000..89b98740--- /dev/null+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/InterfaceOrientation.swift@@ -0,0 +1,54 @@+#if os(iOS)+import SwiftUI+import UIKit++/// Holds the screen still while a take is rolling.+///+/// A movie file output writes the whole take at the angle it started with,+/// so a phone turned mid-take would leave a level preview over a sideways+/// file and rearrange the controls under the hand holding them. The camera+/// screen pins the interface for the length of a take and lets it go again+/// the moment the take ends.+///+/// The app delegate answers the system's orientation question with `mask`.+public final class InterfaceOrientationLock {+ public static let shared = InterfaceOrientationLock()++ /// Which way this window may turn right now.+ public private(set) var mask: UIInterfaceOrientationMask = .all++ private init() {}++ /// Pins the interface to the orientation it is already in.+ public func hold() {+ guard let scene = Self.scenes.first else { return }+ set(Self.mask(for: scene.interfaceOrientation))+ }++ public func release() {+ set(.all)+ }++ private func set(_ mask: UIInterfaceOrientationMask) {+ guard mask != self.mask else { return }+ self.mask = mask+ for scene in Self.scenes {+ scene.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()+ scene.requestGeometryUpdate(.iOS(interfaceOrientations: mask))+ }+ }++ private static var scenes: [UIWindowScene] {+ UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }+ }++ private static func mask(for orientation: UIInterfaceOrientation) -> UIInterfaceOrientationMask {+ switch orientation {+ case .landscapeLeft: .landscapeLeft+ case .landscapeRight: .landscapeRight+ case .portraitUpsideDown: .portraitUpsideDown+ default: .portrait+ }+ }+}+#endifdiff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.mdindex 60886353..a4f72ece 100644--- a/docs/guides/ios-app.md+++ b/docs/guides/ios-app.md@@ -128,6 +128,9 @@ the Library tab. The well holds one take and retires it once it has beenwatched or thirty seconds pass, so the viewfinder goes back to being aviewfinder.+The camera shoots whichever way the phone is held: turn it sideways and the+take is a landscape file, with the shutter across on the trailing edge.+## TeleprompterThe teleprompter paces raw notes by itself. Reading speed is words per