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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! The counts and times that a decoder reports.
use std::time::Duration;
/// What a decoder has done since it started.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct DecoderStats {
/// The access units that the decoder took.
pub submitted: u64,
/// The access units that [`submit`](super::VideoDecoder::submit) refused with
/// [`DecoderError::Full`](super::DecoderError::Full) after its wait. The decoder never saw
/// them.
pub refused: u64,
/// The pictures that the decoder output. A decoder of Apple's counts a picture after it
/// queued the picture for the app and woke the task that waits. Android counts a picture when
/// MediaCodec renders it, and its image reaches the app's queue up to one picture later.
pub decoded: u64,
/// The pictures that never reached the app after the decoder took their access units. The app
/// did not take some in time. The platform decoder lost the others while it decoded or
/// rendered them.
pub dropped: u64,
/// The pictures that reached the app's reader after the next picture began to decode. Only
/// Android counts them: a decoder of Apple's has no reader between it and the app.
pub late: u64,
/// The decode time of the latest picture.
pub last_decode: Duration,
/// The decode time of the pictures so far, as a moving average.
pub mean_decode: Duration,
/// The longest decode time.
pub max_decode: Duration,
}
#[cfg(any(target_os = "android", target_os = "macos", target_os = "ios"))]
pub(super) use counters::StatsCounters;
#[cfg(any(target_os = "android", target_os = "macos", target_os = "ios"))]
mod counters {
use std::{
sync::atomic::{AtomicU64, Ordering::Relaxed},
time::Duration,
};
use super::DecoderStats;
// The counters of a decoder. Its threads add to them without a lock.
#[derive(Default)]
pub(crate) struct StatsCounters {
submitted: AtomicU64,
refused: AtomicU64,
decoded: AtomicU64,
dropped: AtomicU64,
late: AtomicU64,
last_decode_ns: AtomicU64,
mean_decode_ns: AtomicU64,
max_decode_ns: AtomicU64,
}
impl StatsCounters {
pub(crate) fn submitted(&self) {
self.submitted.fetch_add(1, Relaxed);
}
pub(crate) fn refused(&self) {
self.refused.fetch_add(1, Relaxed);
}
pub(crate) fn dropped(&self) {
self.dropped.fetch_add(1, Relaxed);
}
#[cfg(target_os = "android")]
pub(crate) fn late(&self) {
self.late.fetch_add(1, Relaxed);
}
// Only the output thread decodes, so the average needs no compare and swap.
pub(crate) fn decoded(&self, decode_time: Duration) {
let ns = u64::try_from(decode_time.as_nanos()).unwrap_or(u64::MAX);
let count = self.decoded.fetch_add(1, Relaxed);
let mean = self.mean_decode_ns.load(Relaxed);
let mean = if count == 0 {
ns
} else {
mean.saturating_add(ns / 8).saturating_sub(mean / 8)
};
self.mean_decode_ns.store(mean, Relaxed);
self.last_decode_ns.store(ns, Relaxed);
self.max_decode_ns.fetch_max(ns, Relaxed);
}
pub(crate) fn snapshot(&self) -> DecoderStats {
let time = |ns: &AtomicU64| Duration::from_nanos(ns.load(Relaxed));
DecoderStats {
submitted: self.submitted.load(Relaxed),
refused: self.refused.load(Relaxed),
decoded: self.decoded.load(Relaxed),
dropped: self.dropped.load(Relaxed),
late: self.late.load(Relaxed),
last_decode: time(&self.last_decode_ns),
mean_decode: time(&self.mean_decode_ns),
max_decode: time(&self.max_decode_ns),
}
}
}
}