Skip to main content

TrackSampleLoader

Struct TrackSampleLoader 

Source
pub struct TrackSampleLoader<S> { /* private fields */ }
Expand description

Loads a track’s compressed samples from a ByteSource asynchronously, ahead of the synchronous readers that decode them.

The loader holds the track’s index, the source and a cache of loaded samples bounded by budget_bytes, evicting the least recently used samples once it is full. Self::sample_provider and Self::audio_packet_provider build providers that share the cache, for ExactFrameReader::from_provider and AudioSampleReader::from_provider. Those providers never touch the source themselves, so S need not be Send and its reads may suspend for as long as they like: a browser fetch is the case this exists for.

Implementations§

Source§

impl<S: ByteSource> TrackSampleLoader<S>

Source

pub fn new(track: Track, source: S, budget_bytes: u64) -> Result<Self>

Fails if budget_bytes cannot hold the track’s largest sample, which could then never be loaded. Reads no sample data.

Source

pub fn with_cache( track: Track, source: S, cache: &SampleCache, base: usize, ) -> Result<Self>

A loader for track that loads into cache, keying its samples from base. Loaders sharing a cache key their samples from bases far enough apart that their keys never meet: one loader’s base plus its sample count at most the next one’s base.

Fails if the cache’s budget cannot hold the track’s largest sample. Reads no sample data.

Source

pub fn cache(&self) -> SampleCache

The cache this loader loads into, to share with another.

Source

pub fn append(&mut self, samples: &[TrackSample]) -> Result<()>

Adds samples to the end of the track, as indexing more of it finds them (issue #692). A provider built before keeps the samples it had; one built after has these too. Fails, adding none, if the budget cannot hold the largest of them.

Source

pub fn track(&self) -> &Track

Source

pub fn source(&self) -> &S

Source

pub fn sample_provider(&self) -> Result<PrefetchedSampleProvider>

A SampleProvider for a video track, answering from this loader’s cache. Fails if the track is not a video track.

Source

pub fn audio_packet_provider( &self, decoded_ranges: Vec<SampleRange>, ) -> Result<PrefetchedAudioPacketProvider>

An AudioPacketProvider for an audio track, answering from this loader’s cache, with decoded_ranges giving each packet’s decoded interval in decode order.

The intervals are the caller’s because how they are known without reading every packet depends on the codec, as AudioPacketProvider explains. An AAC track should use Self::aac_packet_provider, an Opus track Self::opus_packet_provider and a Vorbis track Self::vorbis_packet_provider, which derive them for the codec. Fails if the track is not an audio track or decoded_ranges does not have one interval per sample.

Source

pub fn opus_packet_provider_with_ranges( &self, decoded_ranges: Vec<SampleRange>, ) -> Result<PrefetchedAudioPacketProvider>

An AudioPacketProvider for an Opus track whose packets’ decoded intervals the caller gives, one per sample in decode order, as Self::opus_packet_provider would derive them from the whole track’s sample table: for a track only part of whose table is known yet (issue #692). Each packet is checked against its interval as playback reaches it, exactly as there. Reads no packet data. Fails if the track is not an Opus audio track or decoded_ranges does not have one interval per sample.

Source

pub async fn opus_packet_provider( &self, ) -> Result<PrefetchedAudioPacketProvider>

An AudioPacketProvider for an Opus track, answering from this loader’s cache, without reading every packet to learn its interval.

Each packet’s decoded interval is its sample-table duration, except the last packet’s, which comes from its own table of contents: a muxer trims the stream’s end by shortening that duration. Only that packet’s first two bytes are read here. The table is then checked a packet at a time, as playback reaches each one: a packet whose table of contents disagrees with its duration is read as ErrorKind::MalformedMedia, so a track whose intervals would differ from the ones Track::to_encoded_audio_samples gives is refused rather than played out of time. Fails if the track is not an Opus audio track.

Source

pub fn aac_packet_provider(&self) -> Result<PrefetchedAudioPacketProvider>

An AudioPacketProvider for an AAC track, answering from this loader’s cache, with each packet’s decoded interval taken from the track’s sample durations.

The intervals are the ones TrackAudioPacketProvider and Track::to_encoded_audio_samples give the same track, and building them reads no packet data. Fails if the track is not an AAC audio track: Opus tracks use Self::opus_packet_provider, and Vorbis tracks Self::vorbis_packet_provider.

Source

pub fn vorbis_packet_provider(&self) -> Result<PrefetchedAudioPacketProvider>

An AudioPacketProvider for a Vorbis track, answering from this loader’s cache, without reading any packet to learn its interval.

A Vorbis packet decodes to a quarter of its block size plus a quarter of the previous packet’s, and its block size is named by the mode number in its first byte. crate::WebmDemuxer reads that byte while scanning each block’s header and records it as Track::vorbis_packet_heads, so the intervals - the ones Track::to_encoded_audio_samples gives - come from the index alone, and a seek needs no earlier packet. Fails if the track is not a Vorbis audio track, or does not record its packets’ first bytes.

Source

pub fn audio_budget_floor( &self, packets: &PrefetchedAudioPacketProvider, preroll_packets: usize, ) -> u64

The smallest budget an AudioSampleReader over packets, decoding preroll_packets packets ahead of the first one a request needs, can make progress through: the largest run of a packet that decodes samples together with its preroll packets.

The reader keeps nothing it decodes as preroll, nor a packet that decodes no samples, so a request that resets the decoder must find its first packet and every preroll packet before it loaded at once. With less than this, the loader loads the same prefix of such a run on every Self::load and the reader reports ErrorKind::WouldBlock for the rest of it each time (issue #694).

Source

pub fn check_audio_budget( &self, packets: &PrefetchedAudioPacketProvider, preroll_packets: usize, ) -> Result<()>

Fails with ErrorKind::ResourceLimit if the budget is below Self::audio_budget_floor, so a reader over packets could never load the packets one of its requests decodes. Reads no packet data.

Source

pub async fn load(&self, required: Range<usize>, readahead: usize) -> Result<()>

Loads every sample in required that is not already loaded, then up to readahead samples after it, as far as the budget allows.

required is clamped to the track. Adjacent samples stored together, or separated only by a few kilobytes such as a WebM’s block headers, are read together. The samples loaded here are the most recently used, so making room for them evicts other samples first.

A required run larger than the whole budget - the walk to a frame deep inside a long group of pictures - is loaded from its start for as far as it fits, with no readahead. The reader decodes that much, reports ErrorKind::WouldBlock for the next sample, and resumes from there, so the run streams through the budget over several loads rather than having to fit in it at once.

Source

pub async fn load_missing(&self) -> Result<usize>

Loads every sample a provider was asked for and did not have since the last call, returning how many that was.

This is what a caller awaits after a reader reports ErrorKind::WouldBlock, before asking the reader again.

Source

pub fn clear_missing(&self)

Forgets the samples providers have reported missing, as a seek does: what the old position needed is no longer worth loading.

Source

pub fn is_loaded(&self, decode_index: usize) -> bool

Source

pub fn resident_bytes(&self) -> u64

The compressed bytes currently held, which never exceeds Self::budget_bytes: by every loader sharing the cache.

Source

pub fn budget_bytes(&self) -> u64

Auto Trait Implementations§

§

impl<S> Forget for TrackSampleLoader<S>

§

impl<S> Freeze for TrackSampleLoader<S>
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze, PhantomData<EditMapping>: Freeze, PhantomData<TrackSample>: Freeze, PhantomData<usize>: Freeze, S: Freeze, *const ArcInner<Mutex<CacheState>> is !null: Freeze, PhantomData<ArcInner<Mutex<CacheState>>>: Freeze,

§

impl<S> Move for TrackSampleLoader<S>

§

impl<S> RefUnwindSafe for TrackSampleLoader<S>

§

impl<S> Send for TrackSampleLoader<S>
where S: Send, RawVecInner: Send, Arc<Mutex<CacheState>>: Send,

§

impl<S> Sync for TrackSampleLoader<S>
where S: Sync, RawVecInner: Sync, Arc<Mutex<CacheState>>: Sync,

§

impl<S> Unpin for TrackSampleLoader<S>
where S: Unpin, *const u8 is !null: Unpin, Arc<Mutex<CacheState>>: Unpin,

§

impl<S> UnsafeUnpin for TrackSampleLoader<S>

§

impl<S> UnwindSafe for TrackSampleLoader<S>
where RawVecInner: UnwindSafe, S: UnwindSafe, Arc<Mutex<CacheState>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.