Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Displaying video from connected devices

At a glance

Item Summary
Purpose Show video from devices connected with the Developer Strap in your visionOS app.
App architecture DisplayConnectedVideoApp presents ContentView; DeviceManager separates device discovery/connection from AVCaptureSession ownership and preview adaptation.
Main patterns SwiftUI scene composition, Observable state owner, Async update consumer, Platform-view adapter, Capture-to-preview adapter
Project style Code-rich sample with 6 scanned Swift file(s) and 385 Swift line(s); resources and generated assets are excluded from those counts.

Project structure

Source bundle/
└── DisplayConnectedVideo/
    ├── CaptureManager.swift  # CaptureManager
    ├── DeviceManager.swift  # DeviceManager
    ├── DevicePreview.swift  # DevicePreview, SampleBufferPreview
    ├── ConnectionManager.swift  # Device, ConnectionManager
    ├── DisplayConnectedVideoApp.swift  # DisplayConnectedVideoApp
    └── ContentView.swift  # ContentView

Structure observations

  • The runtime boundary is WindowGroup; the pruned tree lists only files that explain lifecycle, state, or framework integration.
  • App code is split into role-named views, models, managers, providers, components, or systems.

Overall architecture

Reference code

DisplayConnectedVideo/DisplayConnectedVideoApp.swift:11 — the app or executable entry declares the outer scene lifecycle.

struct DisplayConnectedVideoApp: App {
    var body: some Scene {
        
        WindowGroup {
            ContentView()
        }
     }
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It keeps scene ownership, shared state, RealityKit content, and framework-provider work at separate levels.

Ownership and state

Ownership evidence

DisplayConnectedVideo/ContentView.swift:12 — representative stored state or the nearest verified lifecycle anchor.

    @State private var deviceManager = DeviceManager()
Owner Object or state Relationship Mutation authority
ContentView DeviceManager as deviceManager creates and retains Only the declaring scope writes
ConnectionManager DiscoverySession as discoverySession stores and coordinates Only the declaring scope writes
CaptureManager AVCaptureSession as captureSession creates and retains Only the declaring scope writes
DeviceManager CaptureManager as captureManager stores and coordinates Only the declaring scope writes

Ownership here is deliberately narrow: @Environment and weak references are shared links, initialized @State or stored services are lifecycle ownership, and a RealityView content closure owns additions to its entity graph without making the SwiftUI view a reference-type owner.

Class and protocol design

Type Responsibility Depends on or conforms to
DisplayConnectedVideoApp (DisplayConnectedVideo/DisplayConnectedVideoApp.swift:11) Declares app scenes and top-level dependency lifetime. App
ContentView (DisplayConnectedVideo/ContentView.swift:11) Presents UI and forwards gestures or lifecycle events. View
DeviceManager (DisplayConnectedVideo/DeviceManager.swift:12) Coordinates long-lived framework or cross-view work. Concrete framework collaborators
CaptureManager (DisplayConnectedVideo/CaptureManager.swift:10) Coordinates long-lived framework or cross-view work. NSObject
ConnectionManager (DisplayConnectedVideo/ConnectionManager.swift:27) Coordinates long-lived framework or cross-view work. Concrete framework collaborators

The source defines no local substitution protocol in the reviewed boundary. Its protocol use is framework-facing (App, View, RealityKit/ARKit protocols, or platform adapters), so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
private(set) var devices: [Device] = [] (DisplayConnectedVideo/DeviceManager.swift:23) private(set) The getter keeps its wider visibility, while mutation stays inside the declaring type and its permitted same-file extensions. Inference: Protect a state invariant while allowing observation.
private let captureSession = AVCaptureSession() (DisplayConnectedVideo/CaptureManager.swift:11) private Use is restricted to the declaration and same-file extensions permitted by Swift. Inference: Hide implementation details and lifecycle-sensitive state.

No reviewed declaration uses fileprivate, public, open; unmodified Swift declarations are internal.

Reference code

DisplayConnectedVideo/DeviceManager.swift:23 — representative visibility boundary.

    private(set) var devices: [Device] = []
    
    /// The authorization status for camera access when the app launched.
    /// This is set twice when the app is launched for the first time. Initially to `AVCaptureDevice.notDetermined`
    /// then to a value that reflects the choice a person made when prompted for authorization.
    private(set) var initialAuthorizationStatus: AVAuthorizationStatus

Logic ownership and placement

Logic Owning type or file Placement rationale
Scene declaration and dependency lifetime DisplayConnectedVideoApp The App/entry boundary determines window, volume, and immersive-space lifetime.
Presentation, attachments, and gestures ContentView SwiftUI view code forwards user intent and RealityView lifecycle events.
Shared feature state and commands DeviceManager A role-named owner prevents sibling views from duplicating transitions.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition DisplayConnectedVideo/DisplayConnectedVideoApp.swift:11 Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary.
Observable state owner DisplayConnectedVideo/DeviceManager.swift:12 Shares feature state across multiple views without moving framework resources into view values.
Async update consumer DisplayConnectedVideo/CaptureManager.swift:99 Consumes provider or event streams in cancellable structured tasks.
Platform-view adapter DisplayConnectedVideo/DevicePreview.swift:10 Wraps an imperative platform view/controller behind a SwiftUI value.
Capture-to-preview adapter DisplayConnectedVideo/CaptureManager.swift:11 Keeps capture-session threading and sample-buffer delivery behind the observable device model and SwiftUI preview wrapper.

Naming conventions

  • Role suffixes are evidence, not decoration: App: DisplayConnectedVideoApp; Manager: CaptureManager, ConnectionManager, DeviceManager; View: ContentView.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: setUpSession, select, start, observeFlushToResumeDecoding, captureOutput, makeUIView, updateUIView, layoutSubviews.
  • Files generally match their primary type; Views, Models, Managers, Providers, Components, Systems, and Packages folders describe architectural roles where present.

Architecture takeaways

  • Treat DisplayConnectedVideoApp as the owner of scene declarations, not as the owner of every RealityKit entity created later.
  • Keep view-local interaction in SwiftUI, but move provider sessions, playback resources, shared game state, or transport state into a stable owner when their lifetime exceeds one render pass.

Source map

Source file Relevant symbols
DisplayConnectedVideo/CaptureManager.swift:10 CaptureManager
DisplayConnectedVideo/DeviceManager.swift:12 DeviceManager
DisplayConnectedVideo/DevicePreview.swift:10 DevicePreview, SampleBufferPreview
DisplayConnectedVideo/ConnectionManager.swift:15 Device, ConnectionManager
DisplayConnectedVideo/DisplayConnectedVideoApp.swift:11 DisplayConnectedVideoApp
DisplayConnectedVideo/ContentView.swift:11 ContentView