// We need a thread that fetches the HLS segments continuously as we stream the audio
// and inserts them into a cache protected by a mutex.
// And the SoundcloudMediaSource needs to pull the segments from the cache.
struct SegmentInfo {
url: String,
duration: Duration,
}
struct SegmentCache(Vec<Option<Vec<u8>>>);
struct SoundcloudFetcher {
duration: Duration,
segment_infos: Vec<SegmentInfo>,
segment_cache: Arc<Mutex<SegmentCache>>,
source_pos: Arc<Mutex<Duration>>,
}
impl SoundcloudFetcher {
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));
let fetcher = SoundcloudFetcher {
duration: hls.duration(),
segment_infos: hls
.segments
.values()
.map(|segment| SegmentInfo {
url: segment.uri().to_string(),
duration: segment.duration.duration(),
})
.collect(),
segment_cache: segment_cache.clone(),
source_pos: source_pos.clone(),
};
std::thread::spawn(move || {
fetcher.run();
});
(segment_cache, source_pos)
}