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

Using AVFoundation to play and persist HTTP live streams

At a glance

Item Summary
Purpose Play HTTP Live Streams and persist streams on disk for offline playback using AVFoundation.
App architecture A Swift sample with the source-visible chain AppDelegateAssetListTableViewControllerAssetListManagerAVFoundation APIs.
Main patterns View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks
Project style 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue.main.async, Task, await suspension point, @MainActor, Task closure isolated to MainActor; none alone proves a background thread.
State/event model Source-visible mechanisms: NotificationCenter.
Key frameworks/packages AVFoundation, Foundation, UIKit, os, AVKit; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── HLSCatalog/
│   ├── AppDelegate.swift
│   ├── Managers/
│   │   ├── AssetPlaybackManager.swift
│   │   ├── AssetListManager.swift
│   │   ├── AssetPersistenceManager.swift
│   │   └── StreamListManager.swift
│   ├── AssetListTableViewController.swift
│   ├── Model/
│   │   ├── Asset.swift
│   │   └── Stream.swift
│   ├── AssetListTableViewCell.swift
│   └── PerfMeasurements.swift
├── Configuration/
│   └── SampleCode.xcconfig
└── HLSCatalog.xcodeproj/
    └── .xcodesamplecode.plist

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 7 project/configuration file(s) and 14 source declaration(s).

Overall architecture

Reference code

HLSCatalog/AppDelegate.swift:12 — architecture anchor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        // Restore the state of the application and any running downloads.
        AssetPersistenceManager.sharedManager.restorePersistenceManager()

        return true
    }
}

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

Ownership evidence

HLSCatalog/AppDelegate.swift:15 — stored dependency or nearest verified ownership anchor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    // ...
    var window: UIWindow?
    // ...
}
Owner Object or state Relationship Mutation authority
AppDelegate UIWindow (window) stores or receives App/module collaborators
AssetPlaybackManager Logger (logger) creates and retains Initialized by the owner; the binding is immutable
AssetPlaybackManager AssetPlaybackManager (sharedManager) creates and retains Initialized by the owner; the binding is immutable
AssetPlaybackManager AssetPlaybackDelegate (delegate) holds a non-owning reference The referenced object’s lifecycle is owned elsewhere

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.main.async The source addresses the main dispatch queue. HLSCatalog/AssetListTableViewCell.swift:70
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. HLSCatalog/AssetListTableViewController.swift:97
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. HLSCatalog/AssetListTableViewController.swift:99
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. HLSCatalog/Managers/AssetPersistenceManager.swift:106
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. HLSCatalog/Managers/AssetPersistenceManager.swift:106

@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

HLSCatalog/AssetListTableViewCell.swift:70 — representative execution boundary

        DispatchQueue.main.async {
            // ...
            case .downloading:
            // ...
        }

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 NotificationCenter NotificationCenter distributes named process-local events. HLSCatalog/AssetListTableViewCell.swift:47
Source import AVFoundation The cited file imports this module; runtime use and architectural role are not inferred. HLSCatalog/AssetListTableViewController.swift:13
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. HLSCatalog/Managers/AssetListManager.swift:9
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. HLSCatalog/AppDelegate.swift:10
Source import os The cited file imports this module; runtime use and architectural role are not inferred. HLSCatalog/Managers/AssetPersistenceManager.swift:12
Source import AVKit The cited file imports this module; runtime use and architectural role are not inferred. HLSCatalog/AssetListTableViewController.swift:14
Source import OSLog The cited file imports this module; runtime use and architectural role are not inferred. HLSCatalog/AssetListTableViewController.swift:15

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

HLSCatalog/Managers/AssetPlaybackManager.swift:166 — representative type boundary

protocol AssetPlaybackDelegate: AnyObject {
    // ...
    func streamPlaybackManager(_ streamPlaybackManager: AssetPlaybackManager, playerReadyToPlay player: AVPlayer)
    // ...
}
Type Responsibility Depends on or conforms to
AppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
AssetPlaybackDelegate Defines a capability or collaboration contract AnyObject
AssetListTableViewCellDelegate Defines a capability or collaboration contract AnyObject
AssetPlaybackManager Long-lived feature or framework coordination NSObject
AssetListTableViewController View lifecycle, callbacks, and feature coordination UITableViewController
AssetListManager Long-lived feature or framework coordination NSObject
AssetPersistenceManager Long-lived feature or framework coordination NSObject
StreamListManager Long-lived feature or framework coordination Concrete collaborators/imported frameworks
Asset Owns feature behavior and collaborator lifecycle Concrete collaborators/imported frameworks
DownloadState Represents mutable feature state String

The source explicitly defines local protocol relationships: AssetListTableViewControllerAssetListTableViewCellDelegate, AssetListTableViewControllerAssetPlaybackDelegate.

Access control

Symbol Access Verified effect Likely rationale
logger (HLSCatalog/AssetListTableViewController.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.
playerViewController (HLSCatalog/AssetListTableViewController.swift:25) fileprivate Use is restricted to this source file. Inference: share with same-file helpers or extensions without exposing the symbol module-wide.
assets (HLSCatalog/Managers/AssetListManager.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.
logger (HLSCatalog/Managers/AssetPersistenceManager.swift:14) 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

HLSCatalog/AssetListTableViewController.swift:17 — representative boundary

private let logger = Logger(subsystem: "com.example.apple-samplecode.HLSCatalog", category: "AssetListTableViewController")

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
View lifecycle, callbacks, and feature coordination AssetListTableViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AppDelegate, AssetListTableViewCellDelegate, AssetPlaybackDelegate The source’s Delegate suffix makes this role explicit.
Long-lived feature or framework coordination AssetListManager, AssetPersistenceManager, AssetPlaybackManager, StreamListManager The source’s Manager suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization HLSCatalog/AssetListTableViewController.swift:20 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Protocol-oriented abstraction HLSCatalog/AssetListTableViewController.swift:166 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks HLSCatalog/AppDelegate.swift:13 Callback protocols invert event delivery back into the sample’s owner.

Naming conventions

  • Types: Controller: AssetListTableViewController; Delegate: AppDelegate, AssetListTableViewCellDelegate, AssetPlaybackDelegate; Manager: AssetListManager, AssetPersistenceManager, AssetPlaybackManager, StreamListManager.
  • Protocols: AssetPlaybackDelegate, AssetListTableViewCellDelegate.
  • Methods: application, resetPlayer, setAssetForPlayback, handleTimebaseRateChanged, handlePlaybackStalled, streamPlaybackManager, viewDidLoad, viewWillAppear.
  • Files: HLSCatalog/AppDelegate.swift, HLSCatalog/Managers/AssetPlaybackManager.swift, HLSCatalog/AssetListTableViewController.swift, HLSCatalog/Managers/AssetListManager.swift, HLSCatalog/Managers/AssetPersistenceManager.swift, HLSCatalog/Managers/StreamListManager.swift.

Architecture takeaways

  • AppDelegate is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches AVFoundation, UIKit, AVKit 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
HLSCatalog/AppDelegate.swift Cited implementation, UIKit, AppDelegate
HLSCatalog/Managers/AssetPlaybackManager.swift AssetPlaybackDelegate, AssetPlaybackManager
HLSCatalog/AssetListTableViewController.swift Cited implementation, AssetListTableViewController, Task, await suspension point, AVFoundation, AVKit, OSLog
HLSCatalog/Managers/AssetListManager.swift Cited implementation, Foundation, AssetListManager
HLSCatalog/Managers/AssetPersistenceManager.swift Cited implementation, @MainActor, Task closure isolated to MainActor, os, AssetPersistenceManager
HLSCatalog/AssetListTableViewCell.swift DispatchQueue.main.async, NotificationCenter, AssetListTableViewCell, AssetListTableViewCellDelegate
HLSCatalog/Managers/StreamListManager.swift StreamListManager
HLSCatalog/Model/Asset.swift Asset, DownloadState, Keys
HLSCatalog/Model/Stream.swift Stream, CodingKeys
HLSCatalog/PerfMeasurements.swift PerfMeasurements