Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Encoding video for live streaming

At a glance

Item Summary
Purpose Configure a compression session to encode video for live streaming.
App architecture A C/Objective-C header, Objective-C, Swift sample bundle with entry-bearing project variants Objective-C, Swift, each leading to VideoToolbox APIs.
Main patterns Actor-isolated state
Project style 15 scanned source file(s) across C/Objective-C header, Objective-C, Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── Swift/
│   ├── VTEncoderForStreaming/
│   │   ├── CommandOptions.swift
│   │   └── VTEncoderForStreaming.swift
│   └── VTEncoderUtils/
│       ├── VideoSource.swift
│       ├── RealTimeVideoSource.swift
│       ├── VTEncoderUtil.swift
│       └── VideoSink.swift
└── Objective-C/
    ├── VTEncoderForStreaming/
    │   ├── main.m
    │   └── VTEncoderForStreaming.h
    └── VTEncoderUtils/
        ├── VideoSource.h
        ├── VideoSource.m
        ├── VideoSink.h
        └── VideoSink.m

Structure observations

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

Overall architecture

Reference code

Swift/VTEncoderForStreaming/CommandOptions.swift:13 — architecture anchor

@main
struct CommandOptions: AsyncParsableCommand {
    // ...
}

Interpretation

The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.

Ownership and state

Ownership evidence

Swift/VTEncoderForStreaming/CommandOptions.swift:16 — stored dependency or nearest verified ownership anchor

    static var configuration = CommandConfiguration(
        usage: """
            VTEncoderForStreaming <source-movie> [<options>]
            """)

    /// The source movie file from which to read video frames.
    @Argument(help: "The source movie file from which to read video frames.")
Owner Object or state Relationship Mutation authority
CommandOptions CommandConfiguration (configuration) creates and retains App/module collaborators
CommandOptions String (sourceMovie) owns value state App/module collaborators
CommandOptions Int (bitrate) owns value state App/module collaborators
CommandOptions CodecType (codecType) stores or receives 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.

Class and protocol design

Swift/VTEncoderForStreaming/CommandOptions.swift:14 — representative type boundary

struct CommandOptions: AsyncParsableCommand {
    // ...
}
Type Responsibility Depends on or conforms to
CommandOptions Carries feature or framework configuration AsyncParsableCommand
CodecType Defines a closed set of feature states or choices String, CaseIterable, ExpressibleByArgument
PixelFormatType Defines a closed set of feature states or choices String, CaseIterable, ExpressibleByArgument
PresetType Defines a closed set of feature states or choices String, CaseIterable, ExpressibleByArgument
ProfileType Defines a closed set of feature states or choices String, CaseIterable, ExpressibleByArgument
QPModulationLevel Defines a closed set of feature states or choices String, CaseIterable, ExpressibleByArgument
VideoSource Represents a feature value or composable behavior Concrete collaborators/imported frameworks
SourceInfo Represents feature data Sendable
VideoSourceFrames Represents a feature value or composable behavior AsyncSequence
SampleBufferQueue Owns feature behavior and collaborator lifecycle @unchecked Sendable

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
frameRate (Objective-C/VTEncoderUtils/VideoSource.h:23) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
width (Objective-C/VTEncoderUtils/VideoSource.h:26) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
height (Objective-C/VTEncoderUtils/VideoSource.h:29) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
sourceMoviePath (Swift/VTEncoderForStreaming/VTEncoderForStreaming.swift:25) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.

Reference code

Objective-C/VTEncoderUtils/VideoSource.h:23 — representative boundary

@interface VideoSource : NSObject
// ...
@property(nonatomic, readonly) float frameRate;
// ...
@end

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
Feature and framework orchestration CommandOptions The sample keeps the demonstrated path in its primary concrete type.

Design patterns

Pattern Source evidence Purpose or tradeoff
Actor-isolated state Swift/VTEncoderUtils/VideoSource.swift:238 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: feature-specific names rather than reusable layer suffixes.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: validate, run, frames, enqueue, dequeue, wait, signal, next.
  • Files: Swift/VTEncoderForStreaming/CommandOptions.swift, Swift/VTEncoderUtils/VideoSource.swift, Swift/VTEncoderUtils/RealTimeVideoSource.swift, Objective-C/VTEncoderUtils/VideoSource.h, Objective-C/VTEncoderUtils/VideoSource.m, Objective-C/VTEncoderUtils/VideoSink.h.

Architecture takeaways

  • CommandOptions is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches AVFoundation, VideoToolbox, ArgumentParser 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.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
Swift/VTEncoderForStreaming/CommandOptions.swift CommandOptions, CodecType, PixelFormatType, PresetType, ProfileType, QPModulationLevel
Objective-C/VTEncoderForStreaming/main.m Feature implementation
Swift/VTEncoderUtils/VideoSource.swift VideoSource, SourceInfo, VideoSourceFrames, SampleBufferQueue, Semaphore, AsyncIterator
Swift/VTEncoderUtils/RealTimeVideoSource.swift RealTimeVideoSource, RealTimeVideoSourceFrames, LazyState, AsyncIterator
Swift/VTEncoderForStreaming/VTEncoderForStreaming.swift EncodePreset, CodecProfile, Options
Objective-C/VTEncoderUtils/VideoSource.h VideoSource, RealTimeVideoSource
Objective-C/VTEncoderUtils/VideoSource.m VideoSource, RealTimeVideoSource
Objective-C/VTEncoderUtils/VideoSink.h VideoSink
Objective-C/VTEncoderUtils/VideoSink.m VideoSink
Swift/VTEncoderUtils/VTEncoderUtil.swift RuntimeError