Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Playing and editing Cinematic mode video

At a glance

Item Summary
Purpose Play and edit Cinematic mode video with an adjustable depth of field and focus points.
App architecture A Metal, Swift sample with the source-visible chain CinematicVideoPlaybackAndEditingAppContentViewCinematicPlayerCinematic APIs.
Main patterns Protocol-oriented abstraction, Binding-based state propagation
Project style 15 scanned source file(s) across Metal, Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: async declaration or closure, Task, @MainActor, Task closure isolated to MainActor, DispatchQueue.main; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, AnyCancellable.
Key frameworks/packages AVFoundation, SwiftUI, Cinematic, CoreMedia, Photos; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── CinematicVideoPlaybackAndEditing/
    ├── CinematicVideoPlaybackAndEditingApp.swift
    ├── ContentView.swift
    ├── AssetView.swift
    ├── CinematicPlayer.swift
    ├── PlayerLayerView.swift
    ├── SampleCustomCompositor.swift
    ├── CinematicAssetReader.swift
    ├── CinematicAsset.swift
    ├── CinematicAssetData.swift
    ├── CinematicEditHelper.swift
    ├── CinematicEditor.swift
    └── PlayerLayerContainer.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Metal, Swift.
  • The verified tree contains 4 project/configuration file(s) and 18 source declaration(s).

Overall architecture

Reference code

CinematicVideoPlaybackAndEditing/CinematicVideoPlaybackAndEditingApp.swift:10 — architecture anchor

@main
struct CinematicVideoPlaybackAndEditingApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

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 Cinematic.

Ownership and state

Ownership evidence

CinematicVideoPlaybackAndEditing/ContentView.swift:13 — stored dependency or nearest verified ownership anchor

struct ContentView: View {
    // ...
    @State private var selectedPickerItem: PhotosPickerItem?
    // ...
}
Owner Object or state Relationship Mutation authority
ContentView PhotosPickerItem (selectedPickerItem) owns wrapper-managed state Owning lexical scope
ContentView CinematicAsset (selectedAsset) owns wrapper-managed state Owning lexical scope
ContentView AsyncStream (continuation) owns wrapper-managed state Owning lexical scope
AssetView CinematicAsset (asset) borrows mutable state The upstream binding owner is authoritative

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
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. CinematicVideoPlaybackAndEditing/CinematicAsset.swift:37
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. CinematicVideoPlaybackAndEditing/CinematicEditor.swift:109
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. CinematicVideoPlaybackAndEditing/CinematicEditor.swift:271
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. CinematicVideoPlaybackAndEditing/CinematicEditor.swift:271
Queue scheduling DispatchQueue.main The source addresses the main dispatch queue. CinematicVideoPlaybackAndEditing/PlayerLayerContainer.swift:26

@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

CinematicVideoPlaybackAndEditing/CinematicAsset.swift:37 — representative execution boundary

    init?(localIdentifier: String) async {
        // ...
        self.localIdentifier = localIdentifier
        // ...
    }

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 SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. CinematicVideoPlaybackAndEditing/AssetView.swift:14
State propagation AnyCancellable A cancellable value records subscription lifetime management. CinematicVideoPlaybackAndEditing/PlayerLayerView.swift:13
Source import AVFoundation The cited file imports this module; runtime use and architectural role are not inferred. CinematicVideoPlaybackAndEditing/CinematicAsset.swift:9
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. CinematicVideoPlaybackAndEditing/AssetView.swift:8
Source import Cinematic The cited file imports this module; runtime use and architectural role are not inferred. CinematicVideoPlaybackAndEditing/CinematicAsset.swift:11
Source import CoreMedia The cited file imports this module; runtime use and architectural role are not inferred. CinematicVideoPlaybackAndEditing/CinematicAssetData.swift:8
Source import Photos The cited file imports this module; runtime use and architectural role are not inferred. CinematicVideoPlaybackAndEditing/AssetView.swift:9
Source import AVKit The cited file imports this module; runtime use and architectural role are not inferred. CinematicVideoPlaybackAndEditing/AssetView.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

CinematicVideoPlaybackAndEditing/ViewRepresentable.swift:21 — representative type boundary

public protocol ViewRepresentable: PlatformViewRepresentable {
    // ...
    associatedtype ViewType: PlatformView
    // ...
}
Type Responsibility Depends on or conforms to
CinematicVideoPlaybackAndEditingApp Application entry and top-level composition App
ContentView User-interface presentation and input forwarding View
AssetView User-interface presentation and input forwarding View
CinematicPlayer Owns media or timeline playback View
PlayerLayerView User-interface presentation and input forwarding PlatformView
ViewRepresentable Defines a capability or collaboration contract PlatformViewRepresentable
SampleCompositionInstruction Owns feature behavior and collaborator lifecycle NSObject
SampleCustomCompositor Owns feature behavior and collaborator lifecycle NSObject, AVVideoCompositing
SourceFrame Represents a feature value or composable behavior Concrete collaborators/imported frameworks
CinematicAssetSampleBuffer Represents a feature value or composable behavior Concrete collaborators/imported frameworks

The source explicitly defines local protocol relationships: PlayerLayerContainerViewRepresentable.

Access control

Symbol Access Verified effect Likely rationale
texture (CinematicVideoPlaybackAndEditing/CinematicEditHelper.swift:54) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
imageFetch (CinematicVideoPlaybackAndEditing/CinematicEditor.swift:17) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
focusChangeInProgress (CinematicVideoPlaybackAndEditing/CinematicEditor.swift:19) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
objectTrackingInProgress (CinematicVideoPlaybackAndEditing/CinematicEditor.swift:21) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.

Reference code

CinematicVideoPlaybackAndEditing/CinematicEditHelper.swift:54 — representative boundary

    private func texture(from outputBuffer: CVPixelBuffer) -> MTLTexture? {
        var image: CVMetalTexture?
        // ...
    }

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 CinematicVideoPlaybackAndEditingApp The source’s App suffix makes this role explicit.
Owns media or timeline playback CinematicPlayer The source’s Player suffix makes this role explicit.
User-interface presentation and input forwarding AssetView, ContentView, PlayerLayerView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Protocol-oriented abstraction CinematicVideoPlaybackAndEditing/PlayerLayerContainer.swift:11 A local protocol and concrete conformance create an explicit capability boundary.
Binding-based state propagation CinematicVideoPlaybackAndEditing/AssetView.swift:14 A binding exposes controlled read/write access while the upstream owner remains authoritative.

Main application flow

Reference code

CinematicVideoPlaybackAndEditing/CinematicAsset.swift:112loadData()

    static private func loadData(from asset: AVAsset, pathComponent: String) async throws -> CinematicAssetData {
        // ...
        let cinematicAssetInfo = try await CNAssetInfo(asset: asset)
        let renderingSessionAttributes = try await CNRenderingSession.Attributes(asset: asset)
        guard let renderingCommandQueue = MTLCreateSystemDefaultDevice()?.makeCommandQueue() else {
            fatalError("Couldn't create command queue")
        }
        let renderingSession = CNRenderingSession(commandQueue: renderingCommandQueue,
                                                  sessionAttributes: renderingSessionAttributes,
                                                  preferredTransform: cinematicAssetInfo.preferredTransform,
                                                  quality: CNRenderingQuality.export)
        let cinematicScript = try await CNScript(asset: asset)
        let url = try FileManager.default.url(for: .applicationSupportDirectory,
                                              in: .userDomainMask, appropriateFor: nil,
                                              create: false)
        let fileURL = url.appendingPathComponent(pathComponent, conformingTo: .archive)
        if FileManager.default.fileExists(atPath: fileURL.path) {
            let scriptFileData = try Data(contentsOf: fileURL)
            let cinematicScriptChanges = CNScript.Changes(dataRepresentation: scriptFileData)
            cinematicScript.reload(changes: cinematicScriptChanges)
        }
        let nominalFrameRate = try await cinematicAssetInfo.frameTimingTrack.load(.nominalFrameRate)
        let naturalTimeScale = try await cinematicAssetInfo.frameTimingTrack.load(.naturalTimeScale)
        let cinematicAssetData = CinematicAssetData(avAsset: asset,
                                                    assetInfo: cinematicAssetInfo,
                                                    renderingSessionAttributes: renderingSessionAttributes,
                                                    renderingSession: renderingSession,
                                                    commandQueue: renderingCommandQueue,
                                                    script: cinematicScript,
                                                    nominalFrameRate: nominalFrameRate,
                                                    naturalTimeScale: naturalTimeScale)
        return cinematicAssetData
    }

Naming conventions

  • Types: App: CinematicVideoPlaybackAndEditingApp; Player: CinematicPlayer; View: AssetView, ContentView, PlayerLayerView.
  • Protocols: ViewRepresentable.
  • Methods: refreshPlayer, setVideoComposition, refreshFrame, renderContextChanged, startRequest, drawFocusDetections, setupForReading, cancelReading.
  • Files: CinematicVideoPlaybackAndEditing/CinematicVideoPlaybackAndEditingApp.swift, CinematicVideoPlaybackAndEditing/ContentView.swift, CinematicVideoPlaybackAndEditing/AssetView.swift, CinematicVideoPlaybackAndEditing/CinematicPlayer.swift, CinematicVideoPlaybackAndEditing/PlayerLayerView.swift, CinematicVideoPlaybackAndEditing/SampleCustomCompositor.swift.

Architecture takeaways

  • CinematicVideoPlaybackAndEditingApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches AVFoundation, SwiftUI, Cinematic, CoreMedia 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
CinematicVideoPlaybackAndEditing/CinematicVideoPlaybackAndEditingApp.swift Cited implementation, CinematicVideoPlaybackAndEditingApp
CinematicVideoPlaybackAndEditing/ContentView.swift Cited implementation, ContentView, ContentView_Previews
CinematicVideoPlaybackAndEditing/ViewRepresentable.swift ViewRepresentable
CinematicVideoPlaybackAndEditing/CinematicEditHelper.swift Cited implementation, CinematicEditHelper
CinematicVideoPlaybackAndEditing/CinematicEditor.swift Cited implementation, Task, @MainActor, Task closure isolated to MainActor, CinematicEditor
CinematicVideoPlaybackAndEditing/PlayerLayerContainer.swift Cited implementation, DispatchQueue.main, PlayerLayerContainer
CinematicVideoPlaybackAndEditing/AssetView.swift Cited implementation, SwiftUI state property wrapper, SwiftUI, Photos, AVKit, AssetView
CinematicVideoPlaybackAndEditing/CinematicAsset.swift async declaration or closure, AVFoundation, Cinematic, CinematicAsset
CinematicVideoPlaybackAndEditing/PlayerLayerView.swift AnyCancellable, PlayerLayerView
CinematicVideoPlaybackAndEditing/CinematicAssetData.swift CoreMedia, CinematicAssetData
CinematicVideoPlaybackAndEditing/CinematicPlayer.swift CinematicPlayer
CinematicVideoPlaybackAndEditing/SampleCustomCompositor.swift SampleCompositionInstruction, SampleCustomCompositor, SourceFrame
CinematicVideoPlaybackAndEditing/CinematicAssetReader.swift CinematicAssetSampleBuffer, CinematicAssetReader
CinematicVideoPlaybackAndEditing/SampleObjectTracking.swift SampleObjectTracking
CinematicVideoPlaybackAndEditing/SampleShaders.metal RasterizerData