Capturing Cinematic video
At a glance
| Item | Summary |
|---|---|
| Purpose | Capture video with an adjustable depth of field and focus points. |
| App architecture | A Swift sample with the source-visible chain CinematicCaptureSampleApp → CameraView → CinematicMetadataManager → CaptureService → AVFoundation APIs. |
| Main patterns | Protocol-oriented abstraction, Delegate or data-source callbacks, Service object, Publisher-backed observable state, Actor isolation |
| Project style | 25 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, Task, await suspension point, actor, Sendable or @Sendable; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, SwiftUI state property wrapper, @Published. |
| Key frameworks/packages | SwiftUI, AVFoundation, Combine, Foundation, CoreMedia; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── CinematicCaptureSample/
├── CinematicCaptureSampleApp.swift
├── Model/
│ └── DataTypes.swift
├── CaptureService.swift
├── Views/
│ ├── FocusOverlayView.swift
│ ├── Controls/
│ │ └── SimulatedApertureView.swift
│ ├── Overlays/
│ │ ├── RecordingTimeView.swift
│ │ └── StatusOverlayView.swift
│ └── Toolbars/
│ └── MainToolbar/
│ └── CaptureButton.swift
├── CameraModel.swift
├── CameraView.swift
├── Capture/
│ └── SPCObserver.swift
└── Preview Content/
└── PreviewCameraModel.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 3 project/configuration file(s) and 36 source declaration(s).
Overall architecture
flowchart LR
N1["CinematicCaptureSampleApp"]
N2["CameraView"]
N3["CinematicMetadataManager"]
N4["CaptureService"]
N5["AVFoundation APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
CinematicCaptureSample/CinematicCaptureSampleApp.swift:10 — architecture anchor
@main
struct CinematicCaptureSampleApp: App {
@State private var camera = CameraModel()
var body: some Scene {
WindowGroup {
CameraView(camera: camera)
.statusBarHidden(true)
.task {
// Start the capture pipeline.
await camera.start()
}
}
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into AVFoundation.
Ownership and state
classDiagram
CinematicCaptureSampleApp *-- CameraModel : camera
CinematicCaptureSampleApp *-- Logger : logger
Movie *-- URL : url
CinematicMetadataManager *-- Array : cinematicFocusMetadata
Ownership evidence
CinematicCaptureSample/CinematicCaptureSampleApp.swift:13 — stored dependency or nearest verified ownership anchor
@main
struct CinematicCaptureSampleApp: App {
// ...
@State private var camera = CameraModel()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
CinematicCaptureSampleApp |
CameraModel (camera) |
owns wrapper-managed state | Owning lexical scope |
CinematicCaptureSampleApp |
Logger (logger) |
creates and retains | Initialized by the owner; the binding is immutable |
Movie |
URL (url) |
owns value state | Initialized by the owner; the binding is immutable |
CinematicMetadataManager |
Array (cinematicFocusMetadata) |
owns value state | App/module collaborators |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | CinematicCaptureSample/CameraModel.swift:21 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | CinematicCaptureSample/CameraModel.swift:50 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | CinematicCaptureSample/CameraModel.swift:51 |
| Actor isolation | actor |
The cited type is actor-isolated; this does not select a background thread. | CinematicCaptureSample/CaptureService.swift:20 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | CinematicCaptureSample/Model/DataTypes.swift:63 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
CinematicCaptureSample/CameraModel.swift:21 — representative execution boundary
@MainActor
@Observable
final class CameraModel: Camera {
// ...
let preview: CALayer = AVSampleBufferDisplayLayer()
// ...
}State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | @Observable |
Observation macro publishes source-visible changes. | CinematicCaptureSample/CameraModel.swift:22 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | CinematicCaptureSample/CameraView.swift:16 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | CinematicCaptureSample/Capture/MovieCapture.swift:15 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | CinematicCaptureSample/CameraModel.swift:8 |
| Source import | AVFoundation |
The cited file imports this module; runtime use and architectural role are not inferred. | CinematicCaptureSample/CameraModel.swift:11 |
| Source import | Combine |
The cited file imports this module; runtime use and architectural role are not inferred. | CinematicCaptureSample/CameraModel.swift:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | CinematicCaptureSample/CaptureService.swift:8 |
| Source import | CoreMedia |
The cited file imports this module; runtime use and architectural role are not inferred. | CinematicCaptureSample/CameraModel.swift:10 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
CinematicCaptureSample/Model/DataTypes.swift:77 — representative type boundary
protocol OutputService {
associatedtype Output: AVCaptureOutput
var output: Output { get }
var captureActivity: CaptureActivity { get }
func updateConfiguration(for device: AVCaptureDevice)
func setVideoRotationAngle(_ angle: CGFloat)
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
CinematicCaptureSampleApp |
Application entry and top-level composition | App |
OutputService |
Defines a capability or collaboration contract | Concrete collaborators/imported frameworks |
CinematicMetadataManager |
Long-lived feature or framework coordination | Concrete collaborators/imported frameworks |
CaptureService |
Framework-facing operations | NSObject |
FocusOverlayView |
User-interface presentation and input forwarding | View |
CameraModel |
Feature data or observable state | Camera |
CameraView |
User-interface presentation and input forwarding | Concrete collaborators/imported frameworks |
SystemPreferredCameraObserver |
Observes and relays feature changes | NSObject |
PreviewCameraModel |
Feature data or observable state | Camera |
SimulatedApertureView |
User-interface presentation and input forwarding | Concrete collaborators/imported frameworks |
The source explicitly defines local protocol relationships: CameraModel → Camera, MovieCapture → OutputService, PreviewCameraModel → Camera.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
status (CinematicCaptureSample/CameraModel.swift:33) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
captureActivity (CinematicCaptureSample/CameraModel.swift:36) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
isSwitchingVideoDevices (CinematicCaptureSample/CameraModel.swift:39) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
thumbnail (CinematicCaptureSample/CameraModel.swift:42) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
Reference code
CinematicCaptureSample/CameraModel.swift:33 — representative boundary
@MainActor
@Observable
final class CameraModel: Camera {
// ...
private(set) var status = CameraStatus.unknown
// ...
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | CinematicCaptureSampleApp |
The source’s App suffix makes this role explicit. |
| Receives callback-driven events | MovieCaptureDelegate |
The source’s Delegate suffix makes this role explicit. |
| Long-lived feature or framework coordination | CinematicMetadataManager |
The source’s Manager suffix makes this role explicit. |
| Feature data or observable state | CameraModel, PreviewCameraModel |
The source’s Model suffix makes this role explicit. |
| Observes and relays feature changes | SystemPreferredCameraObserver |
The source’s Observer suffix makes this role explicit. |
| Framework-facing operations | CaptureService, OutputService |
The source’s Service suffix makes this role explicit. |
| User-interface presentation and input forwarding | CameraView, FocusOverlayView, PreviewView, RecordingTimeView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Protocol-oriented abstraction | CinematicCaptureSample/CameraModel.swift:23 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | CinematicCaptureSample/Capture/MovieCapture.swift:68 |
Callback protocols invert event delivery back into the sample’s owner. |
| Service object | CinematicCaptureSample/CaptureService.swift:20 |
A role-named service contains framework-facing operations. |
| Publisher-backed observable state | CinematicCaptureSample/Capture/MovieCapture.swift:15 |
Published properties notify observers while mutation remains with the state object. |
| Actor isolation | CinematicCaptureSample/CaptureService.swift:20 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
Main application flow
sequenceDiagram
participant Output as Metadata output
participant Service as CaptureService
participant Store as CinematicMetadataManager
participant Overlay as FocusOverlayView
actor User
Output->>Service: metadataOutput(objects)
Service->>Store: replace normalized focus metadata
Store-->>Overlay: observable update
User->>Overlay: tap or long press
Overlay->>Service: focus command through CameraModel
Service->>Service: configure device focus
Reference code
CinematicCaptureSample/CaptureService.swift:498 — metadataOutput
nonisolated func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
assumeIsolated { isolatedSelf in
isolatedSelf.metadataObjectsCache = metadataObjects
var cinematicFocusMetadataObjects = [CinematicFocusMetadata]()
for metadataObject in metadataObjects {
// Don't draw bodies in the preview as otherwise the preview gets too busy.
if metadataObject.type == .catBody || metadataObject.type == .dogBody || metadataObject.type == .humanBody {
continue
}
let boundsInOutputCoordinates = isolatedSelf.videoOutput.outputRectConverted(fromMetadataOutputRect: metadataObject.bounds)
let fullFrameInOutputCoordinates = isolatedSelf.videoOutput.outputRectConverted(fromMetadataOutputRect: CGRect(x: 0, y: 0, width: 1, height: 1))
let layerBoundsNormalized = CGRect(x: boundsInOutputCoordinates.origin.x / fullFrameInOutputCoordinates.width,
y: boundsInOutputCoordinates.origin.y / fullFrameInOutputCoordinates.height,
width: boundsInOutputCoordinates.size.width / fullFrameInOutputCoordinates.width,
height: boundsInOutputCoordinates.size.height / fullFrameInOutputCoordinates.size.height)
let cinematicFocusMetadata = CinematicFocusMetadata(metadataObject: metadataObject, layerBoundsNormalized: layerBoundsNormalized)
cinematicFocusMetadataObjects.append(cinematicFocusMetadata)
}
isolatedSelf.metadataManager.cinematicFocusMetadata = cinematicFocusMetadataObjects
}
}Naming conventions
- Types: App: CinematicCaptureSampleApp; Delegate: MovieCaptureDelegate; Manager: CinematicMetadataManager; Model: CameraModel, PreviewCameraModel; Observer: SystemPreferredCameraObserver; Service: CaptureService, OutputService; View: CameraView, FocusOverlayView, PreviewView, RecordingTimeView, SimulatedApertureView.
- Protocols:
OutputService,Camera. - Methods:
updateConfiguration,setVideoRotationAngle,start,setUpSession,metadataVDOSetup,updateVDORotation,addInput,addOutput. - Files:
CinematicCaptureSample/CinematicCaptureSampleApp.swift,CinematicCaptureSample/CaptureService.swift,CinematicCaptureSample/Views/FocusOverlayView.swift,CinematicCaptureSample/CameraModel.swift,CinematicCaptureSample/CameraView.swift,CinematicCaptureSample/Preview Content/PreviewCameraModel.swift.
Architecture takeaways
CinematicCaptureSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, AVFoundation, CoreMedia, PhotosUI through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
CinematicCaptureSample/CinematicCaptureSampleApp.swift |
Cited implementation, CinematicCaptureSampleApp |
CinematicCaptureSample/Model/DataTypes.swift |
OutputService, Sendable or @Sendable, CameraStatus, CaptureActivity, CaptureMode, Movie, CameraError |
CinematicCaptureSample/CameraModel.swift |
Cited implementation, @MainActor, Task, await suspension point, @Observable, SwiftUI, AVFoundation, Combine, CoreMedia, CameraModel |
CinematicCaptureSample/Capture/MovieCapture.swift |
Cited implementation, @Published, MovieCapture, MovieCaptureDelegate |
CinematicCaptureSample/CaptureService.swift |
CaptureService, actor, Foundation, CinematicMetadataManager |
CinematicCaptureSample/CameraView.swift |
SwiftUI state property wrapper, CameraView |
CinematicCaptureSample/Views/FocusOverlayView.swift |
FocusOverlayView, PreviewType |
CinematicCaptureSample/Capture/SPCObserver.swift |
SystemPreferredCameraObserver |
CinematicCaptureSample/Preview Content/PreviewCameraModel.swift |
PreviewCameraModel |
CinematicCaptureSample/Views/Controls/SimulatedApertureView.swift |
SimulatedApertureView |
CinematicCaptureSample/Views/Overlays/RecordingTimeView.swift |
RecordingTimeView |
CinematicCaptureSample/Views/Overlays/StatusOverlayView.swift |
StatusOverlayView |
CinematicCaptureSample/Views/Toolbars/MainToolbar/CaptureButton.swift |
CaptureButton, MovieCaptureButton, NoFadeButtonStyle |
CinematicCaptureSample/Model/Camera.swift |
Camera, CinematicFocusMetadata |
CinematicCaptureSample/Model/MediaLibrary.swift |
MediaLibrary, Error |
CinematicCaptureSample/Views/CameraPreview.swift |
CameraPreview, PreviewView |