Sample CodeiOS, iPadOS, Mac Catalyst, macOSReviewed 2026-07-21View on Apple Developer

Discovering a third-party media-streaming device

At a glance

Item Summary
Purpose Build an extension that streams media to a server app in iOS or macOS.
App architecture A Swift sample with the source-visible chain ServerAppServerViewVideoViewModelDemoClientSessionSwiftUI / Network APIs.
Main patterns Model-View-ViewModel, View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks, Coordinator, Publisher-backed observable state
Project style 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue(label:), DispatchQueue.main, RunLoop.main; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Published.
Key frameworks/packages os, SwiftUI, Foundation, Network, AVRouting; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── DeviceDiscoveryExtensionDemo/
    ├── CustomMediaDeviceDemo/
    │   ├── Server/
    │   │   ├── ServerApp.swift
    │   │   └── ServerView.swift
    │   ├── Client/
    │   │   ├── ClientApp.swift
    │   │   ├── ClientView.swift
    │   │   ├── DevicePickerView.swift
    │   │   ├── RemoteControlView.swift
    │   │   └── RouteController.swift
    │   └── Protocol/
    │       ├── DemoSession.swift
    │       └── DemoProtocol.swift
    ├── CustomMediaDeviceMacServer/
    │   ├── DemoMacServerApp.swift
    │   └── ContentView.swift
    └── CustomMediaDeviceExtension/
        └── Appex.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 13 project/configuration file(s) and 49 source declaration(s).

Overall architecture

Reference code

DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Server/ServerApp.swift:23 — architecture anchor

@main
struct DataAccessDemoServerApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ServerView()
        }
    }
}

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

Ownership and state

Ownership evidence

DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Protocol/DemoSession.swift:26 — stored dependency or nearest verified ownership anchor

    struct Volume {
        var muted = false
        var level: Float = 1.0
    }
Owner Object or state Relationship Mutation authority
Volume Float (level) owns value state App/module collaborators
Volume Volume (volume) creates and retains App/module collaborators
State Float (timeRemaining) owns value state App/module collaborators
State Int (sessionId) 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
Queue scheduling DispatchQueue(label:) The source constructs a dispatch queue; its label alone does not prove a thread. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift:39
Queue scheduling DispatchQueue.main The source addresses the main dispatch queue. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientView.swift:46
Run-loop scheduling RunLoop.main The source refers to the main thread’s run loop, a scheduling/liveness boundary rather than actor isolation. DeviceDiscoveryExtensionDemo/CustomMediaDeviceExtension/DeviceDiscovery.swift:176

@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

DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift:39 — representative execution boundary

class ClientDiscovery: NSObject, CBCentralManagerDelegate, ObservableObject {
    // ...
    let centralManagerQueue = DispatchQueue(label: "centralManager.concurrent.queue", attributes: .concurrent)
    // ...
}

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. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift:14
State propagation ObservableObject ObservableObject supplies an observation contract. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift:29
State propagation @Published A published property can emit owner-controlled changes. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift:33
Source import os The cited file imports this module; runtime use and architectural role are not inferred. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientApp.swift:9
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientApp.swift:8
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift:8
Source import Network The cited file imports this module; runtime use and architectural role are not inferred. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientView.swift:9
Source import AVRouting The cited file imports this module; runtime use and architectural role are not inferred. DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientView.swift:12

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

DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Protocol/BonjourConnection.swift:20 — representative type boundary

protocol DemoConnectionDelegate: AnyObject {
    // ...
    func didStart(connection: DemoConnection)
    // ...
}
Type Responsibility Depends on or conforms to
AppDelegate Receives callback-driven events NSObject, UIApplicationDelegate
DataAccessDemoServerApp Application entry and top-level composition App
DataAccessDemoClientApp Application entry and top-level composition App
DemoMacServerApp Application entry and top-level composition App
DemoConnectionDelegate Defines a capability or collaboration contract AnyObject
DemoSession Owns a session-scoped interaction Concrete collaborators/imported frameworks
DemoClientSession Owns a session-scoped interaction DemoSession<DemoClientMessage, DemoServerMessage>
DemoServerSession Owns a session-scoped interaction DemoSession<DemoServerMessage, DemoClientMessage>
VideoViewModel UI-facing state and feature coordination ObservableObject
ServerView User-interface presentation and input forwarding View

The source explicitly defines local protocol relationships: DemoUdpConnectionDemoConnection, DemoMessageBaseHasMessageTypeParam, DemoClientMessageDemoMessageBase, DemoServerMessageDemoMessageBase.

Access control

Symbol Access Verified effect Likely rationale
activateRouteManager (DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientView.swift:140) 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.
routesHaveSameNetworkEndpoint (DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/RouteController.swift:186) 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.
findControllerRoutes (DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/RouteController.swift:193) 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.
processBluetoothIdentifier (DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/RouteController.swift:201) 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.

Reference code

DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientView.swift:140 — representative boundary

    private func activateRouteManager() {
        // ...
            print(customRowTapped)
        // ...
    }

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 DataAccessDemoClientApp, DataAccessDemoServerApp, DemoMacServerApp The source’s App suffix makes this role explicit.
View lifecycle, callbacks, and feature coordination ClientSessionController, RouteController The source’s Controller suffix makes this role explicit.
Cross-object flow or session coordination Coordinator The source’s Coordinator suffix makes this role explicit.
Receives callback-driven events AppDelegate, DemoConnectionDelegate The source’s Delegate suffix makes this role explicit.
Long-lived feature or framework coordination BTPeripheralManager, RouteManager The source’s Manager suffix makes this role explicit.
Owns a session-scoped interaction DemoClientSession, DemoServerSession, DemoSession The source’s Session suffix makes this role explicit.
User-interface presentation and input forwarding ClientDiscoveryView, ClientView, ContentView, DevicePickerView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientView.swift:14 Role-named view models keep UI-facing state or coordination outside view declarations.
View-controller organization DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/RemoteControlView.swift:62 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Protocol-oriented abstraction DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Protocol/BonjourConnection.swift:33 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift:29 Callback protocols invert event delivery back into the sample’s owner.
Coordinator DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/DevicePickerView.swift:36 A role-named coordinator centralizes cross-object flow.
Publisher-backed observable state DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift:33 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: DataAccessDemoClientApp, DataAccessDemoServerApp, DemoMacServerApp; Controller: ClientSessionController, RouteController; Coordinator: Coordinator; Delegate: AppDelegate, DemoConnectionDelegate; Manager: BTPeripheralManager, RouteManager; Session: DemoClientSession, DemoServerSession, DemoSession; View: ClientDiscoveryView, ClientView, ContentView, DevicePickerView, RemoteControlView; ViewModel: VideoViewModel.
  • Protocols: DemoConnectionDelegate, HasMessageTypeParam, DemoMessageBase, DemoConnection.
  • Methods: application, toStr, appendInterpolation, didStart, didStop, didSend, didReceive, post.
  • Files: DeviceDiscoveryExtensionDemo/CustomMediaDeviceMacServer/DemoMacServerApp.swift, DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Protocol/DemoSession.swift, DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Server/ServerView.swift, DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientView.swift, DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/DevicePickerView.swift, DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/RemoteControlView.swift.

Architecture takeaways

  • ServerApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, Network, AVRouting, CoreBluetooth 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
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Server/ServerApp.swift Cited implementation, AppDelegate, DataAccessDemoServerApp
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Protocol/DemoSession.swift Cited implementation, DemoProtocolError, DemoServerStatus, Volume, State, DemoSessionState, DemoSession, DemoClientSession, DemoServerSession
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Protocol/BonjourConnection.swift DemoConnectionDelegate, Cited implementation, DemoConnection, DemoUdpConnection
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientView.swift Cited implementation, VideoViewModel, DispatchQueue.main, Network, AVRouting, ClientView, ConnectionTester, ClientView_Previews
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/RouteController.swift Cited implementation, RouteController
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/RemoteControlView.swift ClientSessionController, RemoteControlView
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientDiscovery.swift Cited implementation, DispatchQueue(label:), SwiftUI state property wrapper, ObservableObject, @Published, Foundation, ClientDiscoveryView, ClientDiscovery
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/DevicePickerView.swift Coordinator, DevicePickerView
DeviceDiscoveryExtensionDemo/CustomMediaDeviceExtension/DeviceDiscovery.swift RunLoop.main, DeviceDiscovery, DeviceState
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/ClientApp.swift os, SwiftUI, DataAccessDemoClientApp
DeviceDiscoveryExtensionDemo/CustomMediaDeviceMacServer/DemoMacServerApp.swift DemoMacServerApp
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Server/ServerView.swift RoomLocation, TargetProtocol, BroadcastType, VideoViewModel, ServerView, RoundedButtonStyle, ServerView_Previews
DeviceDiscoveryExtensionDemo/CustomMediaDeviceExtension/Appex.swift DataAccessDemoExtension
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Protocol/DemoProtocol.swift DemoMessageType, HasMessageTypeParam, DemoMessageBase, DemoClientMessage, Message, DemoServerMessage
DeviceDiscoveryExtensionDemo/CustomMediaDeviceMacServer/ContentView.swift ContentView, ContentView_Previews
DeviceDiscoveryExtensionDemo/CustomMediaDeviceDemo/Client/RouteManager.swift RouteManager