Show what a watched stream delivers: size, frame rate, bitrate
A viewer could see the codec and the picture's size of a watched stream, but not how many pictures arrived or at what bitrate, so a choppy or starved stream looked like any other. DecodeStats now counts the video bytes received and gives the decoded frame rate and the received bitrate since the previous `stats()` call (at least half a second; the watch screen asks once a second, other readers share the same window). The watch screen shows "VP8 · 1280×720 · 30 fps · 0.4 Mbit/s" in small text in the player's control bar, where a spacer was, and in Stream Info. These are read from the decoding pipeline, which runs in the app, rather than sent as an engine event as planned: the engine never sees the decoded pictures, and the screen already refreshes from these stats every second. Tested: the local test pattern decodes at 20-40 fps and 0.1-5 Mbit/s (media::tests::local_preview_decodes_the_pattern); a headless screenshot of the watch screen with the sample stream showed the line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d14b0d5be
commit
0bc96437e5
5 files changed
+65
-7
No files matched your search
@@ -79,6 +79,8 @@ const OPUS_BITRATE: i32 = 128_000;
|
||||
pub const MAX_QUEUED: usize = 30;
|
||||
/// Keyframe requests while waiting for one are at least this far apart.
|
||||
const KEYFRAME_RETRY: Duration = Duration::from_millis(500);
|
||||
/// The decoding rates of [`DecodeStats`] cover at least this long.
|
||||
const RATE_WINDOW: Duration = Duration::from_millis(500);
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum MediaError {
|
||||
@@ -2401,6 +2403,23 @@ pub struct DecodeStats {
|
||||
pub height: u32,
|
||||
/// The last error (no decoder for the codec, decoding failed).
|
||||
pub error: Option<String>,
|
||||
/// Bytes of video received.
|
||||
pub bytes: u64,
|
||||
/// Pictures decoded per second and video received in bit/s, over the
|
||||
/// time since the previous [`VideoPipeline::stats`] (at least
|
||||
/// [`RATE_WINDOW`]; the watch screen asks once a second).
|
||||
pub fps: u32,
|
||||
pub bitrate: u64,
|
||||
}
|
||||
|
||||
/// Where the rates of [`DecodeStats`] were last measured from.
|
||||
#[derive(Default)]
|
||||
struct DecodeRates {
|
||||
since: Option<Instant>,
|
||||
decoded: u64,
|
||||
bytes: u64,
|
||||
fps: u32,
|
||||
bitrate: u64,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -2416,6 +2435,7 @@ struct DecodeQueue {
|
||||
state: Mutex<QueueState>,
|
||||
cond: Condvar,
|
||||
stats: Mutex<DecodeStats>,
|
||||
rates: Mutex<DecodeRates>,
|
||||
}
|
||||
|
||||
/// Feeds frames into a [`VideoPipeline`]; cheap to clone.
|
||||
@@ -2432,6 +2452,7 @@ impl FrameInput {
|
||||
if frame.kind != MediaKind::Video {
|
||||
return;
|
||||
}
|
||||
lock(&self.queue.stats).bytes += frame.data.len() as u64;
|
||||
let mut state = lock(&self.queue.state);
|
||||
if state.frames.len() >= MAX_QUEUED {
|
||||
state.frames.clear();
|
||||
@@ -2486,7 +2507,19 @@ impl VideoPipeline {
|
||||
}
|
||||
|
||||
pub fn stats(&self) -> DecodeStats {
|
||||
lock(&self.input.queue.stats).clone()
|
||||
let mut stats = lock(&self.input.queue.stats).clone();
|
||||
let mut rates = lock(&self.input.queue.rates);
|
||||
let now = Instant::now();
|
||||
let elapsed = rates.since.map(|since| now.saturating_duration_since(since));
|
||||
if elapsed.is_none_or(|e| e >= RATE_WINDOW) {
|
||||
if let Some(seconds) = elapsed.map(|e| e.as_secs_f64()) {
|
||||
rates.fps = ((stats.decoded - rates.decoded) as f64 / seconds).round() as u32;
|
||||
rates.bitrate = ((stats.bytes - rates.bytes) as f64 * 8.0 / seconds) as u64;
|
||||
}
|
||||
(rates.since, rates.decoded, rates.bytes) = (Some(now), stats.decoded, stats.bytes);
|
||||
}
|
||||
(stats.fps, stats.bitrate) = (rates.fps, rates.bitrate);
|
||||
stats
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2981,6 +3014,11 @@ mod tests {
|
||||
assert!(stats.decoded >= 10 && stats.error.is_none(), "{stats:?}");
|
||||
assert_eq!(stats.codec, Some(Codec::Vp8));
|
||||
assert_eq!(preview.streamer().backend(), "synthetic");
|
||||
// Rates over the second since: the pattern's rate, its bitrate.
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
let stats = preview.stats();
|
||||
assert!((20..=40).contains(&stats.fps), "{stats:?}");
|
||||
assert!((100_000..5_000_000).contains(&stats.bitrate), "{stats:?}");
|
||||
}
|
||||
|
||||
fn layer(id: LayerId, scale: f32, max_fps: Option<u32>, bitrate: u64) -> LayerSpec {
|
||||
|
||||
Reference in new issue
Block a user