live.umm.gay/wish-server-rs/src/streams.rs

58 lines
1.6 KiB
Rust

use std::{
collections::{hash_map::Entry, HashMap},
sync::{atomic::AtomicU64, Arc, Mutex},
};
use miette::Result;
use once_cell::sync::Lazy;
use webrtc::{
rtp_transceiver::rtp_codec::RTCRtpCodecCapability,
track::track_local::track_local_static_rtp::TrackLocalStaticRTP,
};
#[derive(Clone)]
pub struct OngoingStream {
pub video_track: Arc<TrackLocalStaticRTP>,
pub audio_track: Arc<TrackLocalStaticRTP>,
pub viewer_count: Arc<AtomicU64>,
}
static STREAMS: Lazy<Mutex<HashMap<String, OngoingStream>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
pub async fn get_ongoing_stream(channel: &str) -> Result<OngoingStream> {
let mut streams = STREAMS.lock().expect("Mutex was poisoned");
let stream = match streams.entry(channel.into()) {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => {
let video_track = Arc::new(TrackLocalStaticRTP::new(
RTCRtpCodecCapability {
mime_type: "video/h264".into(),
..Default::default()
},
"video".into(),
"pion".into(),
));
let audio_track = Arc::new(TrackLocalStaticRTP::new(
RTCRtpCodecCapability {
mime_type: "audio/opus".into(),
..Default::default()
},
"audio".into(),
"pion".into(),
));
v.insert(OngoingStream {
video_track,
audio_track,
viewer_count: Arc::new(AtomicU64::new(0)),
})
}
};
Ok(stream.clone())
}