use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::sync::broadcast;
const BROADCAST_CAPACITY: usize = 512;
pub struct MountPoint {
pub path: String,
pub sdp: String,
sender: broadcast::Sender<Arc<Vec<u8>>>,
}
impl MountPoint {
#[must_use]
pub fn new(path: String, sdp: String) -> (Self, broadcast::Receiver<Arc<Vec<u8>>>) {
let (sender, rx) = broadcast::channel(BROADCAST_CAPACITY);
let mp = Self { path, sdp, sender };
(mp, rx)
}
#[must_use]
pub fn subscribe(&self) -> broadcast::Receiver<Arc<Vec<u8>>> {
self.sender.subscribe()
}
pub fn publish(&self, rtp_bytes: Arc<Vec<u8>>) -> usize {
self.sender.send(rtp_bytes).unwrap_or(0)
}
}
#[derive(Default, Clone)]
pub struct MountPointRegistry {
inner: Arc<Mutex<HashMap<String, Arc<MountPoint>>>>,
}
impl MountPointRegistry {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn register(&self, point: MountPoint) -> Arc<MountPoint> {
let path = point.path.clone();
let shared = Arc::new(point);
let clone = Arc::clone(&shared);
self.inner
.lock()
.expect("registry mutex poisoned")
.insert(path, shared);
clone
}
#[must_use]
pub fn lookup(&self, path: &str) -> Option<Arc<MountPoint>> {
self.inner
.lock()
.expect("registry mutex poisoned")
.get(path)
.cloned()
}
pub fn unregister(&self, path: &str) -> bool {
self.inner
.lock()
.expect("registry mutex poisoned")
.remove(path)
.is_some()
}
#[must_use]
pub fn list_paths(&self) -> Vec<String> {
let mut paths: Vec<String> = self
.inner
.lock()
.expect("registry mutex poisoned")
.keys()
.cloned()
.collect();
paths.sort();
paths
}
}