use super::*;
pub struct MountRegistry {
mounts: RwLock<HashMap<MountId, Arc<Mount>>>,
max_mounts: usize,
global: Option<Arc<rtflow_core::metrics::MetricsRegistry>>,
}
pub const DEFAULT_MAX_QUEUED_BYTES_PER_SESSION: usize = 4 * 1024 * 1024;
pub const MAX_CONSUMER_QUEUE_CAPACITY: usize = 4096;
#[derive(Default, Clone)]
#[non_exhaustive]
pub struct MountOptions {
pub consumer_queue_capacity: Option<usize>,
pub max_queued_bytes_per_session: Option<usize>,
pub event_sink: Option<Arc<dyn EventSink>>,
pub startup_cache_config: Option<crate::continuity::StartupCacheConfig>,
pub timeline: Option<rtflow_rtsp::range::TimelineMode>,
pub cancel: Option<CancellationToken>,
}
impl std::fmt::Debug for MountOptions {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut debug = formatter.debug_struct("MountOptions");
debug
.field("consumer_queue_capacity", &self.consumer_queue_capacity)
.field(
"max_queued_bytes_per_session",
&self.max_queued_bytes_per_session,
)
.field("event_sink", &self.event_sink.as_ref().map(|_| "<sink>"))
.field("timeline", &self.timeline)
.field("cancel", &self.cancel.is_some());
debug.field("startup_cache_config", &self.startup_cache_config);
debug.finish()
}
}
pub struct MountOptionsBuilder {
options: MountOptions,
}
impl MountOptions {
pub fn builder() -> MountOptionsBuilder {
MountOptionsBuilder {
options: MountOptions::default(),
}
}
}
impl MountOptionsBuilder {
pub fn consumer_queue_capacity(mut self, value: usize) -> Self {
self.options.consumer_queue_capacity = Some(value);
self
}
pub fn max_queued_bytes_per_session(mut self, value: usize) -> Self {
self.options.max_queued_bytes_per_session = Some(value);
self
}
pub fn event_sink(mut self, value: Arc<dyn EventSink>) -> Self {
self.options.event_sink = Some(value);
self
}
pub fn startup_cache_config(mut self, value: crate::continuity::StartupCacheConfig) -> Self {
self.options.startup_cache_config = Some(value);
self
}
pub fn timeline(mut self, value: rtflow_rtsp::range::TimelineMode) -> Self {
self.options.timeline = Some(value);
self
}
pub fn cancel(mut self, value: CancellationToken) -> Self {
self.options.cancel = Some(value);
self
}
pub fn build(self) -> MountOptions {
self.options
}
}
impl MountRegistry {
pub fn new(max_mounts: usize) -> Self {
Self {
mounts: RwLock::new(HashMap::new()),
max_mounts,
global: None,
}
}
pub fn attach_global(&mut self, registry: Arc<rtflow_core::metrics::MetricsRegistry>) {
self.global = Some(registry);
}
#[doc(hidden)]
pub fn insert(&self, path: Arc<str>, mount: Arc<Mount>) -> Result<()> {
let id = MountId::from(path);
let mut mounts = self
.mounts
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if mounts.contains_key(&id) {
return Err(Error::Conflict(id.to_string()));
}
if mounts.len() >= self.max_mounts {
return Err(Error::Limit("mounts"));
}
mounts.insert(id, mount);
Ok(())
}
#[doc(hidden)]
pub fn remove(&self, path: &str) -> Option<Arc<Mount>> {
self.mounts
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(path)
}
#[doc(hidden)]
pub fn get(&self, path: &str) -> Option<Arc<Mount>> {
self.mounts
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.get(path)
.cloned()
}
pub fn len(&self) -> usize {
self.mounts
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn cancel_all(&self) {
let mut mounts = self
.mounts
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
for mount in mounts.values() {
mount.cancel.cancel();
}
mounts.clear();
}
pub fn paths(&self) -> Vec<MountId> {
let mut paths = self
.mounts
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.keys()
.cloned()
.collect::<Vec<_>>();
paths.sort_by(|left, right| left.as_str().cmp(right.as_str()));
paths
}
#[allow(dead_code)] #[doc(hidden)]
pub fn find_longest_prefix(&self, requested: &str) -> Option<(MountId, Arc<Mount>)> {
let mounts = self
.mounts
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some((path, mount)) = mounts.get_key_value(&MountId::new(requested)) {
return Some((path.clone(), mount.clone()));
}
mounts
.iter()
.filter(|(path, _)| {
requested
.strip_prefix(path.as_str())
.is_some_and(|suffix| suffix.starts_with('/'))
})
.max_by_key(|(path, _)| path.as_str().len())
.map(|(path, mount)| (path.clone(), mount.clone()))
}
pub fn register(&self, path: Arc<str>, config: MountConfig) -> Result<MountHandle> {
self.register_with(path, config, MountOptions::default())
}
pub fn register_with(
&self,
path: Arc<str>,
config: MountConfig,
options: MountOptions,
) -> Result<MountHandle> {
if !path.starts_with('/') {
return Err(Error::InvalidConfig(
"mount path must start with '/'".into(),
));
}
let description = description_for(&config)?;
let queue_capacity = options
.consumer_queue_capacity
.unwrap_or(256)
.clamp(1, MAX_CONSUMER_QUEUE_CAPACITY);
let max_bytes = options
.max_queued_bytes_per_session
.unwrap_or(DEFAULT_MAX_QUEUED_BYTES_PER_SESSION);
let frames = FrameBus::new(queue_capacity, max_bytes);
let (keyframe_requests, _) = broadcast::channel(16);
let (nack_requests, _) = broadcast::channel(64);
let startup_cache = KeyframeCache::new(
&description,
options.startup_cache_config.unwrap_or_default(),
)?;
let mount = Arc::new(Mount {
id: MountId::from(path.clone()),
config,
frames,
consumer_queue_capacity: queue_capacity,
keyframe_requests,
nack_requests,
timeline: options.timeline,
stats: {
#[cfg_attr(not(feature = "metrics"), allow(unused_mut))]
let mut counters = StreamCounters::default();
if let Some(global) = &self.global {
counters.attach_global(global.clone());
} else if let Some(sink) = &options.event_sink {
if let Some(registry) = rtflow_core::metrics::registry_from_sink(sink) {
counters.attach_global(registry);
}
}
counters
},
description: std::sync::RwLock::new(Arc::new(description.clone())),
startup_cache: Mutex::new(startup_cache),
startup_cache_config: options.startup_cache_config.unwrap_or_default(),
bootstrap: Mutex::new(std::array::from_fn(|_| None)),
codec_by_track: std::sync::RwLock::new(codec_lookup_table(&description)),
event_sink: options
.event_sink
.unwrap_or_else(|| Arc::new(rtflow_core::observe::NoopEventSink)),
cancel: options.cancel.unwrap_or_default(),
proxy_running: AtomicBool::new(false),
publishers: AtomicUsize::new(0),
publisher_claimed: AtomicBool::new(false),
active_sessions: AtomicUsize::new(0),
max_queued_bytes_per_session: max_bytes,
announce_fmtp: Mutex::new(HashMap::new()),
rtmp_seq_headers: Mutex::new(HashMap::new()),
derived_payloads: crate::derived::DerivedPayloadCache::new(2),
});
self.insert(path.clone(), mount.clone())?;
Ok(MountHandle {
path: MountId::from(path),
description: Arc::new(description),
inner: mount,
})
}
pub fn get_handle(&self, path: &str) -> Option<MountHandle> {
self.get(path).map(|mount| {
let description = mount.description_snapshot();
MountHandle {
path: mount.id.clone(),
description,
inner: mount,
}
})
}
pub fn unregister(&self, path: &str) -> Option<MountHandle> {
let removed = self.remove(path)?;
removed.cancel.cancel();
tracing::info!(path, "mount unregistered");
Some(MountHandle {
path: removed.id.clone(),
description: Arc::new(description_for(&removed.config).unwrap_or_else(|_| {
MediaDescription {
session_name: path.into(),
tracks: Vec::new(),
connection: None,
}
})),
inner: removed,
})
}
pub fn snapshots(&self) -> Vec<(MountId, MountSnapshot)> {
let mut snapshots = self
.mounts
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.iter()
.map(|(path, mount)| {
let handle = MountHandle {
path: path.clone(),
description: Arc::new(MediaDescription {
session_name: path.as_str().into(),
tracks: Vec::new(),
connection: None,
}),
inner: mount.clone(),
};
(path.clone(), handle.snapshot())
})
.collect::<Vec<_>>();
snapshots.sort_by(|(left, _), (right, _)| left.as_str().cmp(right.as_str()));
snapshots
}
}
#[cfg(test)]
mod tests {
use super::*;
fn local(path: &str) -> rtflow_media::MediaDescription {
rtflow_media::MediaDescription::new(
path,
vec![rtflow_media::TrackDescription::h264_video(
0,
&[],
&[],
640,
360,
30,
)],
)
}
#[test]
fn longest_prefix_respects_path_boundaries() {
let registry = MountRegistry::new(4);
registry
.register(
Arc::from("/cam"),
MountConfig::Local {
description: local("cam"),
},
)
.unwrap();
registry
.register(
Arc::from("/cam/main"),
MountConfig::Local {
description: local("main"),
},
)
.unwrap();
let (path, _) = registry
.find_longest_prefix("/cam/main/track0")
.expect("prefix");
assert_eq!(path.as_str(), "/cam/main");
assert!(registry.find_longest_prefix("/camera").is_none());
}
#[test]
fn paths_are_sorted_and_cancel_all_clears() {
let registry = MountRegistry::new(4);
for path in ["/b", "/a"] {
registry
.register(
Arc::from(path),
MountConfig::Local {
description: local(path),
},
)
.unwrap();
}
assert_eq!(
registry
.paths()
.iter()
.map(|id| id.as_str())
.collect::<Vec<_>>(),
vec!["/a", "/b"]
);
let handle = registry.get_handle("/a").unwrap();
registry.cancel_all();
assert!(handle.cancellation_token().is_cancelled());
assert!(registry.is_empty());
}
#[tokio::test]
async fn dropping_mount_closes_frame_subscribers() {
let registry = MountRegistry::new(1);
let handle = registry
.register(
Arc::from("/close"),
MountConfig::Local {
description: local("close"),
},
)
.unwrap();
let mut receiver = handle.subscribe();
drop(handle);
drop(registry);
assert!(matches!(
receiver.recv().await,
Err(crate::FrameRecvError::Closed)
));
}
#[test]
fn queue_capacity_is_clamped() {
let registry = MountRegistry::new(1);
let handle = registry
.register_with(
Arc::from("/capacity"),
MountConfig::Local {
description: local("capacity"),
},
MountOptions::builder()
.consumer_queue_capacity(usize::MAX)
.build(),
)
.unwrap();
assert_eq!(
handle.snapshot().consumer_queue_capacity,
MAX_CONSUMER_QUEUE_CAPACITY
);
}
#[test]
fn oversized_frame_is_dropped_before_broadcast() {
let registry = MountRegistry::new(1);
let handle = registry
.register_with(
Arc::from("/oversized"),
MountConfig::Local {
description: local("oversized"),
},
MountOptions::builder()
.max_queued_bytes_per_session(4)
.build(),
)
.unwrap();
let publisher = handle.publisher().unwrap();
let frame = rtflow_media::AccessUnit {
nal_offsets: None,
track_id: 0,
codec: rtflow_media::Codec::H264,
pts: std::time::Duration::ZERO,
dts: None,
rtp_timestamp: 0,
is_keyframe: true,
data: bytes::Bytes::from_static(b"12345"),
};
assert_eq!(publisher.try_push(frame).unwrap(), PushResult::Dropped);
assert_eq!(handle.snapshot().stats.dropped_frames, 1);
}
#[test]
fn mount_options_builder_sets_values() {
let options = MountOptions::builder()
.consumer_queue_capacity(32)
.max_queued_bytes_per_session(1024)
.timeline(rtflow_rtsp::range::TimelineMode::Absolute {
offset: std::time::Duration::from_secs(10),
})
.build();
assert_eq!(options.consumer_queue_capacity, Some(32));
assert_eq!(options.max_queued_bytes_per_session, Some(1024));
assert!(matches!(
options.timeline,
Some(rtflow_rtsp::range::TimelineMode::Absolute { .. })
));
}
}