moq_video/decode/
consumer.rs1use std::collections::VecDeque;
4
5use hang::catalog::VideoConfig;
6
7use super::decoder::{Config, Decoder};
8use crate::Error;
9use crate::Frame;
10
11pub struct Consumer {
16 decoder: Decoder,
17 track: moq_mux::container::Consumer<moq_mux::container::legacy::Wire>,
18 pending: VecDeque<Frame>,
22}
23
24impl Consumer {
25 pub async fn new(
28 broadcast: &moq_net::broadcast::Consumer,
29 catalog: &VideoConfig,
30 name: impl Into<String>,
31 config: Config,
32 ) -> Result<Self, Error> {
33 let decoder = Decoder::new(catalog, &config)?;
34
35 let name = name.into();
36 let track = broadcast
37 .track(&name)?
38 .subscribe(moq_net::track::Subscription::default().with_priority(hang::catalog::PRIORITY.video))
39 .await?;
40 let mut track = moq_mux::container::Consumer::new(track, moq_mux::container::legacy::Wire);
41 if let Some(latency) = config.latency_max {
42 track = track.with_latency(latency);
43 }
44
45 Ok(Self {
46 decoder,
47 track,
48 pending: VecDeque::new(),
49 })
50 }
51
52 pub fn name(&self) -> &str {
54 self.decoder.name()
55 }
56
57 pub async fn read(&mut self) -> Result<Option<Frame>, Error> {
59 loop {
60 if let Some(frame) = self.pending.pop_front() {
61 return Ok(Some(frame));
62 }
63
64 let Some(mux_frame) = self.track.read().await? else {
65 return Ok(None);
66 };
67
68 self.pending.extend(
69 self.decoder
70 .decode(&mux_frame.payload, mux_frame.timestamp, mux_frame.keyframe)?,
71 );
72 }
73 }
74}