Skip to main content

Overview

The Probe struct scans a MediaSourceStream for metadata and container formats, providing methods to automatically detect and instantiate appropriate format readers.

Probe

Definition

pub struct Probe {
    filter: BloomFilter,
    registered: Vec<Descriptor>,
}
The Probe uses a bloom filter for efficient format marker detection and maintains a list of registered format descriptors.

Methods

register_all

pub fn register_all<Q: QueryDescriptor>(&mut self)
Register all descriptors supported by the parameterized type.
Q
QueryDescriptor
Type implementing QueryDescriptor trait
Example:
use symphonia::core::probe::Probe;
use symphonia::format_ogg::OggReader;

let mut probe = Probe::default();
probe.register_all::<OggReader>();

register

pub fn register(&mut self, descriptor: &Descriptor)
Register a single format descriptor.
descriptor
&Descriptor
The descriptor to register

format

pub fn format(
    &self,
    hint: &Hint,
    mss: MediaSourceStream,
    format_opts: &FormatOptions,
    metadata_opts: &MetadataOptions,
) -> Result<ProbeResult>
Search the provided MediaSourceStream for a container format. Metadata read during the search is queued and attached to the format reader.
hint
&Hint
Optional hints about the format (file extension, MIME type)
mss
MediaSourceStream
The media source stream to probe
format_opts
&FormatOptions
Options for the format reader
metadata_opts
&MetadataOptions
Options for metadata parsing
ProbeResult
Result<ProbeResult>
Contains the instantiated FormatReader and any metadata discovered during probing
Example:
use symphonia::core::probe::Hint;
use symphonia::core::formats::FormatOptions;
use symphonia::core::meta::MetadataOptions;
use symphonia::core::io::MediaSourceStream;
use std::fs::File;

let file = File::open("audio.ogg")?;
let mss = MediaSourceStream::new(Box::new(file), Default::default());

let mut hint = Hint::new();
hint.with_extension("ogg");

let probe = symphonia::default::get_probe();
let probed = probe.format(
    &hint,
    mss,
    &FormatOptions::default(),
    &MetadataOptions::default()
)?;

let mut format = probed.format;
let metadata = probed.metadata;

next

pub fn next(&self, mss: &mut MediaSourceStream) -> Result<Instantiate>
Search the provided MediaSourceStream for the next metadata or container format marker.
mss
&mut MediaSourceStream
The media source stream to search
Instantiate
Result<Instantiate>
An instantiation function for either a FormatReader or MetadataReader

Hint

Definition

pub struct Hint {
    extension: Option<String>,
    mime_type: Option<String>,
}
Provides additional context when probing a media source stream, such as file extensions or MIME types that may not be available from the stream itself.

Methods

new

pub fn new() -> Self
Instantiate an empty Hint.
Hint
Hint
A new empty hint

with_extension

pub fn with_extension(&mut self, extension: &str) -> &mut Self
Add a file extension hint.
extension
&str
The file extension (without the dot)
&mut Hint
&mut Self
Self for chaining
Example:
let mut hint = Hint::new();
hint.with_extension("mp3");

mime_type

pub fn mime_type(&mut self, mime_type: &str) -> &mut Self
Add a MIME/Media-type hint.
mime_type
&str
The MIME type (e.g., “audio/mpeg”)
&mut Hint
&mut Self
Self for chaining
Example:
let mut hint = Hint::new();
hint.mime_type("audio/ogg");

ProbeResult

Definition

pub struct ProbeResult {
    pub format: Box<dyn FormatReader>,
    pub metadata: ProbedMetadata,
}
Contains the result of a format probe operation.
format
Box<dyn FormatReader>
An instance of a format reader for the probed format
metadata
ProbedMetadata
Metadata read during the probe operation before the format reader was instantiated. Container metadata is available through the format reader itself.

ProbedMetadata

Definition

pub struct ProbedMetadata {
    metadata: Option<MetadataLog>,
}
Metadata discovered during probing, separate from container metadata.

Methods

get

pub fn get(&mut self) -> Option<Metadata<'_>>
Returns the metadata found during probing.
Option<Metadata>
Option<Metadata<'_>>
Metadata if any was present outside the container, None otherwise

into_inner

pub fn into_inner(self) -> Option<MetadataLog>
Returns the inner metadata log.
Option<MetadataLog>
Option<MetadataLog>
The metadata log if present

Descriptor

Definition

pub struct Descriptor {
    pub short_name: &'static str,
    pub long_name: &'static str,
    pub extensions: &'static [&'static str],
    pub mime_types: &'static [&'static str],
    pub markers: &'static [&'static [u8]],
    pub score: fn(&[u8]) -> u8,
    pub inst: Instantiate,
}
Provides declarative information about container and metadata formats.
short_name
&'static str
A short ASCII-only string identifying the format
long_name
&'static str
A longer, more descriptive string identifying the format
extensions
&'static [&'static str]
List of case-insensitive file extensions generally used by the format
mime_types
&'static [&'static str]
List of case-insensitive MIME types generally used by the format
markers
&'static [&'static [u8]]
Byte-string start-of-stream markers searched for within the stream
score
fn(&[u8]) -> u8
Function to score a context buffer (0-255)
inst
Instantiate
Instantiation function for the format

Instantiate

Definition

pub enum Instantiate {
    Format(fn(MediaSourceStream, &FormatOptions) -> Result<Box<dyn FormatReader>>),
    Metadata(fn(&MetadataOptions) -> Box<dyn MetadataReader>),
}
Enumeration of instantiation functions used by descriptors.
Format
fn(MediaSourceStream, &FormatOptions) -> Result<Box<dyn FormatReader>>
Instantiation function for a FormatReader
Metadata
fn(&MetadataOptions) -> Box<dyn MetadataReader>
Instantiation function for a MetadataReader

QueryDescriptor

Definition

pub trait QueryDescriptor {
    fn query() -> &'static [Descriptor];
    fn score(context: &[u8]) -> u8;
}
Trait indicating that the implementer may be registered and is capable of probing.

query

fn query() -> &'static [Descriptor]
Returns a list of descriptors supported by this type.
&[Descriptor]
&'static [Descriptor]
List of format descriptors

score

fn score(context: &[u8]) -> u8
Calculate and return a confidence value (0-255) for decoding the source stream.
context
&[u8]
Context buffer to score
u8
u8
Confidence score from 0 (no confidence) to 255 (certain)

Complete Example

use symphonia::core::probe::{Hint, Probe};
use symphonia::core::formats::FormatOptions;
use symphonia::core::meta::MetadataOptions;
use symphonia::core::io::MediaSourceStream;
use std::fs::File;

fn probe_file(path: &str) -> Result<(), Box<dyn std::error::Error>> {
    // Open the file
    let file = File::open(path)?;
    let mss = MediaSourceStream::new(Box::new(file), Default::default());

    // Create hint from file path
    let mut hint = Hint::new();
    if let Some(ext) = std::path::Path::new(path).extension() {
        hint.with_extension(ext.to_str().unwrap());
    }

    // Get the default probe with all enabled formats
    let probe = symphonia::default::get_probe();

    // Probe the format
    let probed = probe.format(
        &hint,
        mss,
        &FormatOptions::default(),
        &MetadataOptions::default()
    )?;

    // Access the format reader
    let mut format = probed.format;
    
    // Print track information
    for track in format.tracks() {
        println!("Track {}: {:?}", track.id, track.codec_params.codec);
    }

    Ok(())
}

See Also

Build docs developers (and LLMs) love