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>
impl<S: ByteSource> TrackSampleLoader<S>
Sourcepub fn new(track: Track, source: S, budget_bytes: u64) -> Result<Self>
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.
Sourcepub fn with_cache(
track: Track,
source: S,
cache: &SampleCache,
base: usize,
) -> Result<Self>
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.
Sourcepub fn cache(&self) -> SampleCache
pub fn cache(&self) -> SampleCache
The cache this loader loads into, to share with another.
Sourcepub fn append(&mut self, samples: &[TrackSample]) -> Result<()>
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.
pub fn track(&self) -> &Track
pub fn source(&self) -> &S
Sourcepub fn sample_provider(&self) -> Result<PrefetchedSampleProvider>
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.
Sourcepub fn audio_packet_provider(
&self,
decoded_ranges: Vec<SampleRange>,
) -> Result<PrefetchedAudioPacketProvider>
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.
Sourcepub fn opus_packet_provider_with_ranges(
&self,
decoded_ranges: Vec<SampleRange>,
) -> Result<PrefetchedAudioPacketProvider>
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.
Sourcepub async fn opus_packet_provider(
&self,
) -> Result<PrefetchedAudioPacketProvider>
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.
Sourcepub fn aac_packet_provider(&self) -> Result<PrefetchedAudioPacketProvider>
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.
Sourcepub fn vorbis_packet_provider(&self) -> Result<PrefetchedAudioPacketProvider>
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.
Sourcepub fn audio_budget_floor(
&self,
packets: &PrefetchedAudioPacketProvider,
preroll_packets: usize,
) -> u64
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).
Sourcepub fn check_audio_budget(
&self,
packets: &PrefetchedAudioPacketProvider,
preroll_packets: usize,
) -> Result<()>
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.
Sourcepub async fn load(&self, required: Range<usize>, readahead: usize) -> Result<()>
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.
Sourcepub async fn load_missing(&self) -> Result<usize>
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.
Sourcepub fn clear_missing(&self)
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.
pub fn is_loaded(&self, decode_index: usize) -> bool
Sourcepub fn resident_bytes(&self) -> u64
pub fn resident_bytes(&self) -> u64
The compressed bytes currently held, which never exceeds
Self::budget_bytes: by every loader sharing the cache.