Core Design Principles
Symphonia’s architecture follows these key principles:- Separation of demuxing and decoding - Container formats and codecs are completely independent
- Trait-based abstractions - Core functionality is defined through Rust traits
- Modular crate structure - Features can be selectively enabled via crate features
- Type safety - 100% safe Rust with strong type guarantees
- Zero-cost abstractions - Generic programming with no runtime overhead
Architectural Overview
The Demuxing Layer
FormatReader Trait
TheFormatReader trait (symphonia-core/src/formats.rs:168) is the core abstraction for container format demuxers. All container formats (MP4, MKV, OGG, WAV, etc.) implement this trait.
Container Format Flow
- Initialization:
FormatReader::try_new()probes the container header and extracts track information - Demuxing:
next_packet()returns the next encoded packet from any track - Track Selection: Caller filters packets by
track_idto process specific tracks - Seeking:
seek()moves to a different position in the stream
The
FormatReader returns packets from all tracks in the order they appear in the container. Your application must filter packets by track_id to process specific tracks.The Decoding Layer
Decoder Trait
TheDecoder trait (symphonia-core/src/codecs.rs:460) abstracts audio codec decoders. Each codec (MP3, FLAC, Vorbis, AAC, etc.) implements this trait.
Decoder Flow
- Instantiation: Create decoder using
CodecParametersfrom the track - Decoding: Feed
Packets todecode(), receiveAudioBufferRefs - Reset: Call
reset()after seeking to clear decoder state - Export: Convert
AudioBufferReftoSampleBufferfor output
Format and Codec Discovery
The Probe
TheProbe (symphonia-core/src/probe.rs:194) automatically detects container formats by scanning the byte stream for format markers.
The CodecRegistry
TheCodecRegistry (symphonia-core/src/codecs.rs:524) maps codec types to decoder implementations.
Modular Crate Structure
Symphonia is organized into multiple crates to allow selective compilation:- Core
- Format Crates
- Codec Crates
- Bundles
- Umbrella
symphonia-core - The foundation library containing:- Trait definitions (
FormatReader,Decoder) - Audio primitives (
AudioBuffer,SampleBuffer) - I/O abstractions (
MediaSourceStream) - Codec and format registries (
Probe,CodecRegistry) - No actual format or codec implementations
Feature Flags
Enable specific formats and codecs via Cargo features:Complete Decode Pipeline
Here’s how all the components work together:Key Architectural Benefits
Format/Codec Independence
Format/Codec Independence
Any codec bitstream can be stored in any compatible container. For example:
- Vorbis audio in OGG, MKV, or WebM
- AAC audio in MP4, ADTS, or MKV
Type Safety & Performance
Type Safety & Performance
Rust’s trait system provides:
- Compile-time polymorphism via generics (zero overhead)
- Runtime polymorphism via trait objects when needed
- Memory safety without garbage collection
- Thread safety via
Send + Syncbounds
Selective Compilation
Selective Compilation
The modular crate structure allows:
- Reduced binary size by excluding unused formats/codecs
- Faster compile times for development
- License compliance (exclude non-free codecs)
- Platform-specific builds (WASM, embedded)
Extensibility
Extensibility
Adding new functionality is straightforward:
- Implement
FormatReaderfor a new container format - Implement
Decoderfor a new audio codec - Implement
MediaSourcefor custom I/O sources - All existing code continues to work unchanged
Next Steps
Formats and Codecs
Learn about container formats vs codecs and how they work together
Audio Primitives
Understand AudioBuffer, sample formats, and audio data handling
Media Sources
Deep dive into MediaSource, streaming, and buffering
Getting Started
See the architecture in action with a complete example