|
| 1 | +import SwiftUI |
| 2 | +import VisionCamera |
| 3 | + |
| 4 | +@MainActor |
| 5 | +final class CameraModel: ObservableObject { |
| 6 | + // Outputs belong to this StateObject, so rendering the screen doesn't recreate them. |
| 7 | + let photoOutput = PhotoOutput() |
| 8 | + |
| 9 | + @Published private(set) var device: CameraDevice? |
| 10 | + @Published private(set) var videoOutput: (any VideoOutput)? |
| 11 | + @Published private(set) var recordingMode: RecordingMode = .standard |
| 12 | + @Published private(set) var status = "Preparing camera..." |
| 13 | + @Published private(set) var thumbnail: UIImage? |
| 14 | + @Published private(set) var isActive = false |
| 15 | + @Published private(set) var isConfigured = false |
| 16 | + @Published private(set) var isRunning = false |
| 17 | + @Published private(set) var isInterrupted = false |
| 18 | + @Published private(set) var isBusy = false |
| 19 | + @Published private(set) var isRecording = false |
| 20 | + @Published private(set) var isFinalizing = false |
| 21 | + @Published var zoom = 1.0 |
| 22 | + |
| 23 | + private var microphoneGranted = false |
| 24 | + private var isPrepared = false |
| 25 | + private var isPreparing = false |
| 26 | + private var position: TargetCameraPosition = .back |
| 27 | + private var recorder: (any Recorder)? |
| 28 | + private var recordingID: UUID? |
| 29 | + private var isStartingRecording = false |
| 30 | + private var requestedActive = false |
| 31 | + private var sceneIsActive = false |
| 32 | + |
| 33 | + var controlsEnabled: Bool { |
| 34 | + return requestedActive && isActive && isConfigured && isRunning && !isInterrupted && !isBusy && !isFinalizing |
| 35 | + } |
| 36 | + |
| 37 | + var canSwitchCamera: Bool { |
| 38 | + return controlsEnabled && (!isRecording || recordingMode == .persistent) |
| 39 | + } |
| 40 | + |
| 41 | + func prepare() async { |
| 42 | + guard !isPrepared, !isPreparing else { return } |
| 43 | + isPreparing = true |
| 44 | + defer { isPreparing = false } |
| 45 | + do { |
| 46 | + guard try await VisionCamera.requestCameraPermission() else { |
| 47 | + status = "Camera access is denied. Enable it in Settings to use the example." |
| 48 | + return |
| 49 | + } |
| 50 | + microphoneGranted = try await VisionCamera.requestMicrophonePermission() |
| 51 | + guard let device = VisionCamera.defaultCamera(for: position) else { |
| 52 | + status = "No camera is available. Run this example on an iPhone or iPad." |
| 53 | + return |
| 54 | + } |
| 55 | + self.device = device |
| 56 | + setVideoOutput() |
| 57 | + isPrepared = true |
| 58 | + // Permission prompts can change the scene phase. Apply its latest value after preparation. |
| 59 | + await updateActive() |
| 60 | + } catch { |
| 61 | + report(error) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + func setActive(_ active: Bool) async { |
| 66 | + sceneIsActive = active |
| 67 | + await updateActive() |
| 68 | + } |
| 69 | + |
| 70 | + private func updateActive() async { |
| 71 | + // This example pauses capture outside the active scene and ends both recording backends. |
| 72 | + // Camera owns session start/stop; the app owns the recording policy. |
| 73 | + requestedActive = sceneIsActive && isPrepared |
| 74 | + if requestedActive { |
| 75 | + isActive = true |
| 76 | + return |
| 77 | + } |
| 78 | + // If start is still pending, toggleRecording submits the stop once it starts. |
| 79 | + guard !isStartingRecording else { return } |
| 80 | + await finishRecording() |
| 81 | + // A new scene activation may have arrived while the stop request was being submitted. |
| 82 | + if !requestedActive { isActive = false } |
| 83 | + } |
| 84 | + |
| 85 | + func didConfigure() { |
| 86 | + isConfigured = true |
| 87 | + showReadyStatus() |
| 88 | + } |
| 89 | + |
| 90 | + func didStart() { |
| 91 | + isRunning = true |
| 92 | + showReadyStatus() |
| 93 | + } |
| 94 | + |
| 95 | + func didStop() { |
| 96 | + isRunning = false |
| 97 | + } |
| 98 | + |
| 99 | + func didInterrupt(_ reason: InterruptionReason) { |
| 100 | + isInterrupted = true |
| 101 | + status = "Camera interrupted: \(reason)" |
| 102 | + } |
| 103 | + |
| 104 | + func didEndInterruption() { |
| 105 | + isInterrupted = false |
| 106 | + showReadyStatus() |
| 107 | + } |
| 108 | + |
| 109 | + func report(_ error: Error) { |
| 110 | + status = error.localizedDescription |
| 111 | + } |
| 112 | + |
| 113 | + func setRecordingMode(_ mode: RecordingMode) { |
| 114 | + guard controlsEnabled, !isRecording, recordingMode != mode else { return } |
| 115 | + recordingMode = mode |
| 116 | + setVideoOutput() |
| 117 | + } |
| 118 | + |
| 119 | + func switchCamera() { |
| 120 | + guard canSwitchCamera else { return } |
| 121 | + let nextPosition: TargetCameraPosition = position == .back ? .front : .back |
| 122 | + guard let nextDevice = VisionCamera.defaultCamera(for: nextPosition) else { |
| 123 | + status = "The other camera is unavailable." |
| 124 | + return |
| 125 | + } |
| 126 | + isConfigured = false |
| 127 | + position = nextPosition |
| 128 | + device = nextDevice |
| 129 | + zoom = 1 |
| 130 | + } |
| 131 | + |
| 132 | + func capturePhoto() async { |
| 133 | + guard controlsEnabled else { return } |
| 134 | + isBusy = true |
| 135 | + defer { isBusy = false } |
| 136 | + do { |
| 137 | + let photo = try await photoOutput.capturePhoto() |
| 138 | + thumbnail = try await photo.image() |
| 139 | + status = "Photo captured" |
| 140 | + } catch { |
| 141 | + report(error) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + func toggleRecording() async { |
| 146 | + guard controlsEnabled else { return } |
| 147 | + if isRecording { |
| 148 | + await finishRecording() |
| 149 | + return |
| 150 | + } |
| 151 | + guard let output = videoOutput else { return } |
| 152 | + isBusy = true |
| 153 | + isStartingRecording = true |
| 154 | + defer { |
| 155 | + isBusy = false |
| 156 | + isStartingRecording = false |
| 157 | + if !requestedActive { isActive = false } |
| 158 | + } |
| 159 | + do { |
| 160 | + let recorder = try await output.makeRecorder() |
| 161 | + // The scene may have become inactive while the output prepared the recorder. |
| 162 | + guard requestedActive else { return } |
| 163 | + let id = UUID() |
| 164 | + self.recorder = recorder |
| 165 | + recordingID = id |
| 166 | + isRecording = true |
| 167 | + try await recorder.start( |
| 168 | + // Keep the model and recorder alive through finalization, even if the screen disappears. |
| 169 | + // recordingFinished releases the recorder and breaks this ownership relationship. |
| 170 | + onFinished: { result in |
| 171 | + DispatchQueue.main.async { |
| 172 | + self.recordingFinished(id: id, message: "Video saved: \(result.fileURL.lastPathComponent)") |
| 173 | + } |
| 174 | + }, |
| 175 | + onError: { error in |
| 176 | + DispatchQueue.main.async { |
| 177 | + self.recordingFinished(id: id, message: error.localizedDescription) |
| 178 | + } |
| 179 | + }, |
| 180 | + onPaused: nil, |
| 181 | + onResumed: nil |
| 182 | + ) |
| 183 | + isStartingRecording = false |
| 184 | + guard recordingID == id else { return } |
| 185 | + if !requestedActive { |
| 186 | + await finishRecording() |
| 187 | + } else if !isFinalizing { |
| 188 | + status = "Recording" |
| 189 | + } |
| 190 | + } catch { |
| 191 | + if let id = recordingID { |
| 192 | + recordingFinished(id: id, message: error.localizedDescription) |
| 193 | + } else { |
| 194 | + report(error) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private func setVideoOutput() { |
| 200 | + isConfigured = false |
| 201 | + let options = VideoOutputOptions(isAudioEnabled: microphoneGranted) |
| 202 | + switch recordingMode { |
| 203 | + case .standard: |
| 204 | + videoOutput = MovieFileVideoOutput(options: options) |
| 205 | + case .persistent: |
| 206 | + videoOutput = VideoFrameOutput(options: options) |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private func finishRecording() async { |
| 211 | + guard let recorder, !isFinalizing, !isStartingRecording else { return } |
| 212 | + isFinalizing = true |
| 213 | + status = "Finishing video..." |
| 214 | + await recorder.stop() |
| 215 | + // Retain the recorder until its callback confirms that the file is finalized. |
| 216 | + } |
| 217 | + |
| 218 | + private func recordingFinished(id: UUID, message: String) { |
| 219 | + guard recordingID == id else { return } |
| 220 | + recorder = nil |
| 221 | + recordingID = nil |
| 222 | + isRecording = false |
| 223 | + isFinalizing = false |
| 224 | + status = message |
| 225 | + } |
| 226 | + |
| 227 | + private func showReadyStatus() { |
| 228 | + guard isConfigured, isRunning, !isRecording else { return } |
| 229 | + status = microphoneGranted ? "Ready" : "Ready. Microphone denied; video will be silent." |
| 230 | + } |
| 231 | +} |
0 commit comments