use std::sync::{Arc, Mutex};
use std::time::Duration;
use bytes::Bytes;
use hang::moq_net;
use crate::catalog::{self, Kind, MoqrosCatalog};
use crate::codec::{Codec, Decoder};
use crate::data::{DataMode, DataSubscriber};
use crate::track::{CatalogWatch, GroupReader, RouteWatch};
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, &ImageOptions::default()).await
}
pub async fn subscribe_images_with(&self, name: &str, options: &ImageOptions) -> Result<ImageSubscriber> {
let (broadcast, catalog, route) = self.catalog(name).await?;
images(name, broadcast, catalog, route, options).await
}
pub async fn subscribe_data(&self, name: &str) -> Result<DataSubscriber> {
let (broadcast, catalog, route) = self.catalog(name).await?;
data(name, broadcast, catalog, route).await
}
pub async fn subscribe(&self, name: &str) -> Result<Subscription> {
self.subscribe_with(name, &ImageOptions::default()).await
}
pub async fn subscribe_with(&self, name: &str, options: &ImageOptions) -> Result<Subscription> {
let (broadcast, catalog, route) = self.catalog(name).await?;
match Kind::of(catalog.catalog()) {
Kind::Images => Ok(Subscription::Images(
images(name, broadcast, catalog, route, options).await?,
)),
Kind::Data => Ok(Subscription::Data(data(name, broadcast, catalog, route).await?)),
Kind::None => Err(Error::NoTracks(name.to_string())),
}
}
pub fn announced(&self, prefix: &str) -> Announcements {
Announcements {
announced: self.origin.consume().announced(),
prefix: prefix.trim_matches('/').to_string(),
}
}
async fn catalog(&self, name: &str) -> Result<(moq_net::broadcast::Consumer, CatalogWatch, RouteWatch)> {
let consumer = self.origin.consume();
let mut announced = consumer.announced();
loop {
let update = announced
.next()
.await
.ok_or_else(|| Error::NotAnnounced(name.to_string()))?;
if update.kind.is_active() && update.prefix.as_str() == name {
break;
}
}
let broadcast = consumer.request_broadcast(name).await?;
let mut updates = MoqrosCatalog::subscribe(&broadcast).await?;
let first = updates.next().await?.ok_or_else(|| Error::NoTracks(name.to_string()))?;
let route = RouteWatch::new(announced, name);
Ok((broadcast, CatalogWatch::new(updates, first), route))
}
}
async fn images(
name: &str,
broadcast: moq_net::broadcast::Consumer,
catalog: CatalogWatch,
route: RouteWatch,
options: &ImageOptions,
) -> Result<ImageSubscriber> {
let (track, config, codec) = catalog::select_video(catalog.catalog(), name, options.max_height)?;
tracing::info!(
broadcast = name,
track,
codec = %config.codec,
width = ?config.coded_width,
height = ?config.coded_height,
"subscribing"
);
let video = GroupReader::subscribe(&broadcast, track, true, route).await?;
Ok(ImageSubscriber {
_broadcast: broadcast,
video,
catalog,
codec,
decoder: Arc::new(Mutex::new(Decoder::new(codec)?)),
})
}
async fn data(
name: &str,
broadcast: moq_net::broadcast::Consumer,
catalog: CatalogWatch,
route: RouteWatch,
) -> Result<DataSubscriber> {
let (track, settings) = catalog::find_data(catalog.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 = GroupReader::subscribe(&broadcast, &track, settings.mode == DataMode::Snapshot, route).await?;
Ok(DataSubscriber::new(broadcast, reader, settings, catalog))
}
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() && is_under(name, &self.prefix) {
return Some(name.to_string());
}
}
None
}
}
fn is_under(name: &str, prefix: &str) -> bool {
prefix.is_empty() || name == prefix || name.strip_prefix(prefix).is_some_and(|rest| rest.starts_with('/'))
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ImageOptions {
pub max_height: Option<u32>,
}
impl ImageOptions {
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)]
pub struct VideoFrame {
pub timestamp: Duration,
pub capture_time: Option<Duration>,
pub keyframe: bool,
pub data: Bytes,
}
pub struct ImageSubscriber {
_broadcast: moq_net::broadcast::Consumer,
video: GroupReader,
catalog: CatalogWatch,
codec: Codec,
decoder: Arc<Mutex<Decoder>>,
}
impl ImageSubscriber {
pub fn codec(&self) -> Codec {
self.codec
}
pub fn frame_id(&self) -> Option<&str> {
self.catalog.section().frame_id.as_deref()
}
pub fn has_capture_times(&self) -> bool {
self.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 frame = loop {
tokio::select! {
biased;
() = self.catalog.changed() => {}
frame = self.video.next_frame() => break frame?,
}
};
let Some(frame) = frame else {
return Ok(None);
};
let frame = hang::container::Frame::decode(frame.payload)?;
let timestamp = Duration::from_micros(u64::try_from(frame.timestamp.as_micros()).unwrap_or(u64::MAX));
Ok(Some(VideoFrame {
timestamp,
capture_time: self.catalog.capture_time(timestamp),
keyframe: self.codec.is_keyframe(&frame.payload),
data: frame.payload,
}))
}
pub async fn next_image(&mut self) -> Result<Option<Image>> {
while let Some(frame) = self.next_frame().await? {
let decoder = self.decoder.clone();
let timestamp = frame.capture_time.unwrap_or(frame.timestamp);
let decoded = tokio::task::spawn_blocking(move || lock(&decoder).decode(&frame.data, timestamp))
.await
.map_err(|err| Error::Codec(format!("decoder task failed: {err}")))??;
if let Some(image) = decoded {
return Ok(Some(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 consumer = origin.consume();
let announced = consumer.announced();
let broadcast = consumer.request_broadcast(name).await.unwrap();
let mut updates = MoqrosCatalog::subscribe(&broadcast).await.unwrap();
let first = updates.next().await.unwrap().unwrap();
images(
name,
broadcast,
CatalogWatch::new(updates, first),
RouteWatch::new(announced, name),
&ImageOptions::default(),
)
.await
.unwrap()
}
#[test]
fn matches_prefixes_by_segment() {
assert!(is_under("robots/a/camera", "robots"));
assert!(is_under("robots/a/camera", "robots/a"));
assert!(is_under("robots", "robots"));
assert!(is_under("anything", ""));
assert!(!is_under("robots2/camera", "robots"));
assert!(!is_under("robot", "robots"));
}
#[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());
let mut images = subscribe_images(&origin, "camera").await;
assert!(publisher.publish(&test_pattern(64, 48, 1)).unwrap());
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");
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());
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 in [100_000, 100_033] {
let image = images.next_image().await.unwrap().expect("a decoded image");
assert_eq!(image.timestamp, Duration::from_millis(millis));
}
publisher.set_frame_id("left").unwrap();
publisher.publish(&at(2, 5_000)).unwrap();
let image = images.next_image().await.unwrap().expect("a decoded image");
assert_eq!(image.timestamp, Duration::from_millis(5_000));
assert_eq!(images.frame_id(), Some("left"));
}
}