use std::sync::{Arc, Mutex};
use std::time::Duration;
use bytes::Bytes;
use hang::moq_net;
use crate::catalog::{self, Kind};
use crate::codec::{Codec, Decoder};
use crate::data::{DataMode, DataSubscriber};
use crate::error::Wrap;
use crate::track::{OpenBroadcast, TrackReader};
use crate::{ClientConfig, Error, Image, PixelFormat, Result, Session};
#[derive(Clone)]
pub struct Subscriber {
origin: moq_net::origin::Producer,
session: Session,
}
impl Subscriber {
pub fn connect(config: &ClientConfig) -> Result<Self> {
let origin = moq_tokio::origin::spawn();
let connection = config
.client()?
.with_subscriber(origin.clone())
.connect(config.url.clone());
Ok(Self {
origin,
session: Session::new(connection),
})
}
pub(crate) fn from_parts(origin: moq_net::origin::Producer, session: Session) -> Self {
Self { origin, session }
}
pub fn session(&self) -> &Session {
&self.session
}
pub async fn established(&self) -> Result<()> {
self.session.established().await
}
pub async fn closed(&self) -> Result<()> {
self.session.closed().await
}
pub async fn close(&self) -> Result<()> {
self.session.close().await
}
pub async fn subscribe_images(&self, name: &str) -> Result<ImageSubscriber> {
self.subscribe_images_with(name, &SubscribeOptions::default()).await
}
pub async fn subscribe_images_with(&self, name: &str, options: &SubscribeOptions) -> Result<ImageSubscriber> {
images(name, self.open(name).await?, options).await
}
pub async fn subscribe_data(&self, name: &str) -> Result<DataSubscriber> {
data(name, self.open(name).await?).await
}
pub async fn subscribe(&self, name: &str) -> Result<Subscription> {
self.subscribe_with(name, &SubscribeOptions::default()).await
}
pub async fn subscribe_with(&self, name: &str, options: &SubscribeOptions) -> Result<Subscription> {
let opened = self.open(name).await?;
match Kind::of(opened.catalog()) {
Kind::Images => Ok(Subscription::Images(images(name, opened, options).await?)),
Kind::Data => Ok(Subscription::Data(data(name, opened).await?)),
Kind::None => Err(Error::NoTracks(name.to_string())),
}
}
pub fn announced(&self, prefix: &str) -> Announcements {
Announcements {
announced: self.origin.consume().announced(),
prefix: crate::normalize_broadcast_name(prefix).to_string(),
}
}
async fn open(&self, name: &str) -> Result<OpenBroadcast> {
OpenBroadcast::open(&self.origin, name).await
}
}
async fn images(name: &str, opened: OpenBroadcast, options: &SubscribeOptions) -> Result<ImageSubscriber> {
let (track, config, codec) = catalog::select_video(opened.catalog(), name, options.max_height)?;
tracing::info!(
broadcast = name,
track,
codec = %config.codec,
width = ?config.coded_width,
height = ?config.coded_height,
"subscribing"
);
let track = track.to_string();
Ok(ImageSubscriber {
video: opened.subscribe(&track, true).await?,
codec,
decoder: Arc::new(Mutex::new(Decoder::new(codec)?)),
})
}
async fn data(name: &str, opened: OpenBroadcast) -> Result<DataSubscriber> {
let (track, settings) = catalog::find_data(opened.catalog()).ok_or_else(|| Error::NoData(name.to_string()))?;
tracing::info!(
broadcast = name,
track,
format = %settings.format,
mode = %settings.mode,
ros_type = settings.ros_type.as_deref().unwrap_or_default(),
"subscribing"
);
let reader = opened.subscribe(&track, settings.mode == DataMode::Snapshot).await?;
Ok(DataSubscriber::new(reader, settings))
}
pub struct Announcements {
announced: moq_net::announce::Consumer,
prefix: String,
}
impl Announcements {
pub async fn next(&mut self) -> Option<String> {
while let Some(update) = self.announced.next().await {
let name = update.prefix.as_str();
if update.kind.is_active() && crate::broadcast_is_under(name, &self.prefix) {
return Some(name.to_string());
}
}
None
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct SubscribeOptions {
pub max_height: Option<u32>,
}
impl SubscribeOptions {
pub fn with_max_height(mut self, max_height: u32) -> Self {
self.max_height = Some(max_height);
self
}
}
#[non_exhaustive]
pub enum Subscription {
Images(ImageSubscriber),
Data(DataSubscriber),
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct VideoFrame {
pub timestamp: Duration,
pub capture_time: Option<Duration>,
pub keyframe: bool,
pub data: Bytes,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DecodedImage {
pub timestamp: Duration,
pub capture_time: Option<Duration>,
pub image: Image,
}
pub struct ImageSubscriber {
video: TrackReader,
codec: Codec,
decoder: Arc<Mutex<Decoder>>,
}
impl ImageSubscriber {
pub fn codec(&self) -> Codec {
self.codec
}
pub fn frame_id(&self) -> Option<&str> {
self.video.catalog().section().frame_id.as_deref()
}
pub fn has_capture_times(&self) -> bool {
self.video.catalog().has_capture_times()
}
pub fn set_max_pixels(&mut self, max_pixels: u64) {
lock(&self.decoder).set_max_pixels(max_pixels);
}
pub fn set_output_format(&mut self, format: PixelFormat) -> Result<()> {
lock(&self.decoder).set_output_format(format)
}
pub async fn next_frame(&mut self) -> Result<Option<VideoFrame>> {
let Some(frame) = self.video.next_frame().await? else {
return Ok(None);
};
let frame = hang::container::Frame::decode(frame.payload).hang()?;
let timestamp = Duration::from_micros(u64::try_from(frame.timestamp.as_micros()).unwrap_or(u64::MAX));
Ok(Some(VideoFrame {
timestamp,
capture_time: self.video.catalog().capture_time(timestamp),
keyframe: self.codec.is_keyframe(&frame.payload),
data: frame.payload,
}))
}
pub async fn next_image(&mut self) -> Result<Option<DecodedImage>> {
while let Some(frame) = self.next_frame().await? {
let decoder = self.decoder.clone();
let (timestamp, capture_time) = (frame.timestamp, frame.capture_time);
let stamp = capture_time.unwrap_or(timestamp);
let decoded = tokio::task::spawn_blocking(move || lock(&decoder).decode(&frame.data, stamp))
.await
.map_err(|err| Error::Codec(format!("decoder task failed: {err}")))??;
if let Some(image) = decoded {
return Ok(Some(DecodedImage {
timestamp,
capture_time,
image,
}));
}
}
Ok(None)
}
}
fn lock(decoder: &Mutex<Decoder>) -> std::sync::MutexGuard<'_, Decoder> {
decoder.lock().unwrap_or_else(|err| err.into_inner())
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicBool, Ordering};
use super::*;
use crate::codec::tests::test_pattern;
use crate::{EncoderSettings, ImagePublisher};
async fn subscribe_images(origin: &moq_net::origin::Producer, name: &str) -> ImageSubscriber {
let opened = OpenBroadcast::open(origin, name).await.unwrap();
images(name, opened, &SubscribeOptions::default()).await.unwrap()
}
#[tokio::test]
async fn follows_announcements_under_a_prefix() {
let origin = moq_tokio::origin::spawn();
let first = origin.create_broadcast("robots/a/camera").unwrap();
first.announce(Default::default()).unwrap();
let mut announced = Announcements {
announced: origin.consume().announced(),
prefix: "robots".into(),
};
let other = origin.create_broadcast("robots2/camera").unwrap();
other.announce(Default::default()).unwrap();
let second = origin.create_broadcast("robots/b/camera").unwrap();
second.announce(Default::default()).unwrap();
assert_eq!(announced.next().await.as_deref(), Some("robots/a/camera"));
assert_eq!(announced.next().await.as_deref(), Some("robots/b/camera"));
}
#[tokio::test(flavor = "current_thread")]
async fn decoding_yields_to_the_runtime() {
if Codec::available().is_empty() {
return;
}
let origin = moq_tokio::origin::spawn();
let mut publisher = ImagePublisher::new(&origin, "camera", EncoderSettings::default()).unwrap();
assert!(publisher.publish(&test_pattern(64, 48, 0)).unwrap().is_sent());
let mut images = subscribe_images(&origin, "camera").await;
assert!(publisher.publish(&test_pattern(64, 48, 1)).unwrap().is_sent());
let ran = Arc::new(AtomicBool::new(false));
tokio::spawn({
let ran = ran.clone();
async move { ran.store(true, Ordering::Relaxed) }
});
let image = images.next_image().await.unwrap().expect("a decoded image").image;
assert_eq!((image.width, image.height), (64, 48));
assert!(ran.load(Ordering::Relaxed), "the other task never ran while decoding");
}
#[tokio::test]
async fn images_carry_capture_time_and_frame_id() {
if Codec::available().is_empty() {
return;
}
let origin = moq_tokio::origin::spawn();
let mut publisher = ImagePublisher::new(&origin, "camera", EncoderSettings::default()).unwrap();
publisher.set_frame_id("camera_optical").unwrap();
let at = |t: u32, millis: u64| {
let mut image = test_pattern(64, 48, t);
image.timestamp = Duration::from_millis(millis);
image
};
assert!(publisher.publish(&at(0, 100_000)).unwrap().is_sent());
let mut images = subscribe_images(&origin, "camera").await;
assert_eq!(images.frame_id(), Some("camera_optical"));
assert!(images.has_capture_times());
publisher.publish(&at(1, 100_033)).unwrap();
for (millis, pts) in [(100_000, 0), (100_033, 33)] {
let decoded = images.next_image().await.unwrap().expect("a decoded image");
assert_eq!(decoded.capture_time, Some(Duration::from_millis(millis)));
assert_eq!(decoded.image.timestamp, Duration::from_millis(millis));
assert_eq!(decoded.timestamp, Duration::from_millis(pts));
}
publisher.set_frame_id("left").unwrap();
publisher.publish(&at(2, 5_000)).unwrap();
let decoded = images.next_image().await.unwrap().expect("a decoded image");
assert_eq!(decoded.capture_time, Some(Duration::from_millis(5_000)));
assert!(decoded.timestamp > Duration::from_millis(33));
assert_eq!(images.frame_id(), Some("left"));
}
}