char/live.umm.gay

git clone https://git.t4t.associates/char/live.umm.gay

videogame hacker[wish-server-rs] Refactor :)e154831

main
1.6 KiB57 linesraw
1use std::{
2    collections::{hash_map::Entry, HashMap},
3    sync::{atomic::AtomicU64, Arc, Mutex},
4};
5
6use miette::Result;
7
8use once_cell::sync::Lazy;
9use webrtc::{
10    rtp_transceiver::rtp_codec::RTCRtpCodecCapability,
11    track::track_local::track_local_static_rtp::TrackLocalStaticRTP,
12};
13
14#[derive(Clone)]
15pub struct OngoingStream {
16    pub video_track: Arc<TrackLocalStaticRTP>,
17    pub audio_track: Arc<TrackLocalStaticRTP>,
18    pub viewer_count: Arc<AtomicU64>,
19}
20
21static STREAMS: Lazy<Mutex<HashMap<String, OngoingStream>>> =
22    Lazy::new(|| Mutex::new(HashMap::new()));
23
24pub async fn get_ongoing_stream(channel: &str) -> Result<OngoingStream> {
25    let mut streams = STREAMS.lock().expect("Mutex was poisoned");
26
27    let stream = match streams.entry(channel.into()) {
28        Entry::Occupied(o) => o.into_mut(),
29        Entry::Vacant(v) => {
30            let video_track = Arc::new(TrackLocalStaticRTP::new(
31                RTCRtpCodecCapability {
32                    mime_type: "video/h264".into(),
33                    ..Default::default()
34                },
35                "video".into(),
36                "pion".into(),
37            ));
38
39            let audio_track = Arc::new(TrackLocalStaticRTP::new(
40                RTCRtpCodecCapability {
41                    mime_type: "audio/opus".into(),
42                    ..Default::default()
43                },
44                "audio".into(),
45                "pion".into(),
46            ));
47
48            v.insert(OngoingStream {
49                video_track,
50                audio_track,
51                viewer_count: Arc::new(AtomicU64::new(0)),
52            })
53        }
54    };
55
56    Ok(stream.clone())
57}