MDSWQ3KZTRAW6QPPKQTKS6BDEX4T6C42VGCZOA6UGT2AOJKERTEQC
pub fn spawn(hls: &MediaPlaylist) -> (Arc<Mutex<SegmentCache>>, Arc<Mutex<Duration>>) {
let segment_cache = Arc::new(Mutex::new(SegmentCache(vec![
None;
hls.segments.iter().len()
])));
let source_pos = Arc::new(Mutex::new(Duration::ZERO));
pub fn spawn(hls: &MediaPlaylist) -> Arc<Mutex<SegmentCache>> {
let segment_cache = Arc::new(Mutex::new(SegmentCache {
source_position: (0, 0),
segments: vec![None; hls.segments.iter().len()],
}));
fn time_at_position(&self, segment_idx: usize, byte_idx: usize) -> Duration {
let mut t = Duration::ZERO;
for (idx, segment) in self.segment_infos.iter().enumerate() {
if segment_idx == idx {
t += Duration::from_secs_f64(byte_idx as f64 / 128_000.0);
break;
}
t += segment.duration;
}
return t;
}
'outer: loop {
let curr_time = *self.source_pos.lock();
loop {
// We look where the source is located and make sure the following 10 seconds are loaded in cache
let source_position = self.segment_cache.lock().source_position;
let curr_time = self.time_at_position(source_position.0, source_position.1);