char/live.umm.gay
git clone https://git.t4t.associates/char/live.umm.gay
e154831
main
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 { 16pub video_track : Arc < TrackLocalStaticRTP >, 17pub audio_track : Arc < TrackLocalStaticRTP >, 18pub viewer_count : Arc < AtomicU64 >, 19} 20 21static STREAMS : Lazy < Mutex < HashMap < String , OngoingStream >>> = 22Lazy :: new ( ||Mutex :: new ( HashMap :: new ())); 23 24pub async fn get_ongoing_stream ( channel : & str ) ->Result < OngoingStream > { 25let mut streams =STREAMS . lock (). expect ( "Mutex was poisoned" ); 26 27let stream =match streams. entry ( channel. into ()) { 28Entry :: Occupied ( o) => o. into_mut (), 29Entry :: Vacant ( v) =>{ 30let video_track =Arc :: new ( TrackLocalStaticRTP :: new ( 31RTCRtpCodecCapability { 32mime_type : "video/h264" . into (), 33 ..Default :: default () 34}, 35"video" . into (), 36"pion" . into (), 37)); 38 39let audio_track =Arc :: new ( TrackLocalStaticRTP :: new ( 40RTCRtpCodecCapability { 41mime_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, 51viewer_count : Arc :: new ( AtomicU64 :: new ( 0 )), 52}) 53} 54}; 55 56Ok ( stream. clone ()) 57}