1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! The access units that a decoder holds, which find the metadata of each decoded picture.
use std::{
collections::VecDeque,
time::{Duration, Instant},
};
// The most units kept. A decoder with low latency holds a few, so a unit older than this many
// never comes out.
const CAPACITY: usize = 64;
// An access unit that the decoder took and has not yet output.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct PendingUnit {
// The timestamp in microseconds, which the decoder returns with the picture
pub(super) pts_us: u64,
pub(super) timestamp: Duration,
pub(super) metadata: u64,
pub(super) submitted: Instant,
}
// The units in the order of their timestamps, oldest first.
pub(super) struct PendingUnits {
units: VecDeque<PendingUnit>,
}
impl PendingUnits {
pub(super) fn new() -> Self {
Self {
units: VecDeque::with_capacity(CAPACITY),
}
}
// Adds a unit whose timestamp rises above the units before. A full list drops its oldest
// unit.
pub(super) fn push(&mut self, unit: PendingUnit) {
if self.units.len() == CAPACITY {
self.units.pop_front();
}
self.units.push_back(unit);
}
// Takes the unit that the picture at `pts_us` came from. The units before it produced no
// picture, such as a unit of parameter sets, so they go too. A timestamp that no unit has
// takes nothing.
#[cfg(any(test, target_os = "android"))]
pub(super) fn take(&mut self, pts_us: u64) -> Option<PendingUnit> {
let index = self.units.iter().position(|unit| unit.pts_us == pts_us)?;
self.units.drain(..index);
self.units.pop_front()
}
// Takes the unit at `pts_us` and leaves the others, for a decoder that answers each unit
// alone and may answer out of order.
#[cfg(any(test, target_os = "macos", target_os = "ios"))]
pub(super) fn remove(&mut self, pts_us: u64) -> Option<PendingUnit> {
let index = self.units.iter().position(|unit| unit.pts_us == pts_us)?;
self.units.remove(index)
}
// The units that wait for their pictures.
#[cfg(any(test, target_os = "macos", target_os = "ios"))]
pub(super) fn len(&self) -> usize {
self.units.len()
}
}
#[cfg(test)]
#[path = "tests/pending_units_tests.rs"]
mod tests;