librespot/audio/src/fetch.rs

365 lines
10 KiB
Rust
Raw Normal View History

2015-09-01 11:20:37 +00:00
use bit_set::BitSet;
2017-01-19 22:45:24 +00:00
use byteorder::{ByteOrder, BigEndian, WriteBytesExt};
use futures::Stream;
use futures::sync::{oneshot, mpsc};
use futures::{Poll, Async, Future};
2015-06-23 14:38:29 +00:00
use std::cmp::min;
2015-07-07 21:40:31 +00:00
use std::fs;
use std::io::{self, Read, Write, Seek, SeekFrom};
2017-01-19 22:45:24 +00:00
use std::sync::{Arc, Condvar, Mutex};
2016-01-12 23:29:31 +00:00
use tempfile::NamedTempFile;
2015-06-23 14:38:29 +00:00
2017-08-03 18:58:44 +00:00
use core::channel::{Channel, ChannelData, ChannelError, ChannelHeaders};
use core::session::Session;
2018-02-12 20:02:27 +00:00
use core::spotify_id::FileId;
2015-06-23 14:38:29 +00:00
2016-01-02 15:19:39 +00:00
const CHUNK_SIZE: usize = 0x20000;
2015-06-23 14:38:29 +00:00
pub enum AudioFile {
Cached(fs::File),
Streaming(AudioFileStreaming),
}
2015-07-07 21:40:31 +00:00
pub enum AudioFileOpen {
2017-08-03 19:37:04 +00:00
Cached(Option<fs::File>),
Streaming(AudioFileOpenStreaming),
2015-06-24 00:41:39 +00:00
}
2015-06-23 14:38:29 +00:00
pub struct AudioFileOpenStreaming {
2017-01-19 22:45:24 +00:00
session: Session,
data_rx: Option<ChannelData>,
headers: ChannelHeaders,
file_id: FileId,
complete_tx: Option<oneshot::Sender<NamedTempFile>>,
}
pub struct AudioFileStreaming {
read_file: fs::File,
position: u64,
seek: mpsc::UnboundedSender<u64>,
shared: Arc<AudioFileShared>,
}
struct AudioFileShared {
2017-01-19 22:45:24 +00:00
file_id: FileId,
chunk_count: usize,
2015-07-07 21:40:31 +00:00
cond: Condvar,
bitmap: Mutex<BitSet>,
2015-06-23 14:38:29 +00:00
}
impl AudioFileOpenStreaming {
fn finish(&mut self, size: usize) -> AudioFileStreaming {
2017-01-19 22:45:24 +00:00
let chunk_count = (size + CHUNK_SIZE - 1) / CHUNK_SIZE;
2015-07-07 21:40:31 +00:00
let shared = Arc::new(AudioFileShared {
2017-01-19 22:45:24 +00:00
file_id: self.file_id,
chunk_count: chunk_count,
2015-07-07 21:40:31 +00:00
cond: Condvar::new(),
2017-01-19 22:45:24 +00:00
bitmap: Mutex::new(BitSet::with_capacity(chunk_count)),
2015-07-07 21:40:31 +00:00
});
2017-01-19 22:45:24 +00:00
let mut write_file = NamedTempFile::new().unwrap();
write_file.set_len(size as u64).unwrap();
write_file.seek(SeekFrom::Start(0)).unwrap();
2017-01-19 22:45:24 +00:00
let read_file = write_file.reopen().unwrap();
2015-09-01 11:20:37 +00:00
2017-01-19 22:45:24 +00:00
let data_rx = self.data_rx.take().unwrap();
let complete_tx = self.complete_tx.take().unwrap();
let (seek_tx, seek_rx) = mpsc::unbounded();
2015-07-07 21:40:31 +00:00
2017-01-19 22:45:24 +00:00
let fetcher = AudioFileFetch::new(
self.session.clone(), shared.clone(), data_rx, write_file, seek_rx, complete_tx
);
self.session.spawn(move |_| fetcher);
2015-06-23 14:38:29 +00:00
AudioFileStreaming {
2017-01-19 22:45:24 +00:00
read_file: read_file,
2017-01-19 22:45:24 +00:00
position: 0,
seek: seek_tx,
shared: shared,
}
2015-07-07 21:40:31 +00:00
}
}
2017-01-19 22:45:24 +00:00
impl Future for AudioFileOpen {
type Item = AudioFile;
type Error = ChannelError;
2017-01-19 22:45:24 +00:00
fn poll(&mut self) -> Poll<AudioFile, ChannelError> {
match *self {
AudioFileOpen::Streaming(ref mut open) => {
let file = try_ready!(open.poll());
Ok(Async::Ready(AudioFile::Streaming(file)))
}
2017-08-03 19:37:04 +00:00
AudioFileOpen::Cached(ref mut file) => {
let file = file.take().unwrap();
Ok(Async::Ready(AudioFile::Cached(file)))
}
}
}
}
impl Future for AudioFileOpenStreaming {
type Item = AudioFileStreaming;
type Error = ChannelError;
fn poll(&mut self) -> Poll<AudioFileStreaming, ChannelError> {
2017-01-19 22:45:24 +00:00
loop {
let (id, data) = try_ready!(self.headers.poll()).unwrap();
2015-07-07 21:40:31 +00:00
2017-01-19 22:45:24 +00:00
if id == 0x3 {
let size = BigEndian::read_u32(&data) as usize * 4;
let file = self.finish(size);
2018-02-12 20:02:27 +00:00
2017-01-19 22:45:24 +00:00
return Ok(Async::Ready(file));
2015-07-07 21:40:31 +00:00
}
}
}
2017-01-19 22:45:24 +00:00
}
impl AudioFile {
pub fn open(session: &Session, file_id: FileId) -> AudioFileOpen {
let cache = session.cache().cloned();
if let Some(file) = cache.as_ref().and_then(|cache| cache.file(file_id)) {
2017-01-29 17:54:32 +00:00
debug!("File {} already in cache", file_id);
2017-08-03 19:37:04 +00:00
return AudioFileOpen::Cached(Some(file));
}
2017-01-29 17:54:32 +00:00
debug!("Downloading file {}", file_id);
2017-01-19 22:45:24 +00:00
let (complete_tx, complete_rx) = oneshot::channel();
let (headers, data) = request_chunk(session, file_id, 0).split();
2015-07-07 21:40:31 +00:00
let open = AudioFileOpenStreaming {
session: session.clone(),
2017-01-19 22:45:24 +00:00
file_id: file_id,
headers: headers,
data_rx: Some(data),
2015-07-07 21:40:31 +00:00
2017-01-19 22:45:24 +00:00
complete_tx: Some(complete_tx),
};
let session_ = session.clone();
session.spawn(move |_| {
complete_rx.map(move |mut file| {
if let Some(cache) = session_.cache() {
cache.save_file(file_id, &mut file);
2017-01-29 17:54:32 +00:00
debug!("File {} complete, saving to cache", file_id);
} else {
debug!("File {} complete", file_id);
}
}).or_else(|oneshot::Canceled| Ok(()))
});
AudioFileOpen::Streaming(open)
2017-01-19 22:45:24 +00:00
}
}
fn request_chunk(session: &Session, file: FileId, index: usize) -> Channel {
trace!("requesting chunk {}", index);
2017-01-19 22:45:24 +00:00
let start = (index * CHUNK_SIZE / 4) as u32;
let end = ((index + 1) * CHUNK_SIZE / 4) as u32;
let (id, channel) = session.channel().allocate();
let mut data: Vec<u8> = Vec::new();
data.write_u16::<BigEndian>(id).unwrap();
data.write_u8(0).unwrap();
data.write_u8(1).unwrap();
data.write_u16::<BigEndian>(0x0000).unwrap();
data.write_u32::<BigEndian>(0x00000000).unwrap();
data.write_u32::<BigEndian>(0x00009C40).unwrap();
data.write_u32::<BigEndian>(0x00020000).unwrap();
data.write(&file.0).unwrap();
data.write_u32::<BigEndian>(start).unwrap();
data.write_u32::<BigEndian>(end).unwrap();
session.send_packet(0x8, data);
channel
}
struct AudioFileFetch {
session: Session,
shared: Arc<AudioFileShared>,
output: Option<NamedTempFile>,
index: usize,
data_rx: ChannelData,
seek_rx: mpsc::UnboundedReceiver<u64>,
complete_tx: Option<oneshot::Sender<NamedTempFile>>,
}
impl AudioFileFetch {
fn new(session: Session, shared: Arc<AudioFileShared>,
data_rx: ChannelData, output: NamedTempFile,
seek_rx: mpsc::UnboundedReceiver<u64>,
complete_tx: oneshot::Sender<NamedTempFile>) -> AudioFileFetch
{
AudioFileFetch {
session: session,
shared: shared,
output: Some(output),
index: 0,
data_rx: data_rx,
seek_rx: seek_rx,
complete_tx: Some(complete_tx),
2015-07-07 21:40:31 +00:00
}
2015-06-23 17:34:48 +00:00
}
2015-06-24 00:41:39 +00:00
2017-01-19 22:45:24 +00:00
fn download(&mut self, mut new_index: usize) {
assert!(new_index < self.shared.chunk_count);
2015-06-23 17:34:48 +00:00
2017-01-19 22:45:24 +00:00
{
let bitmap = self.shared.bitmap.lock().unwrap();
while bitmap.contains(new_index) {
new_index = (new_index + 1) % self.shared.chunk_count;
2015-06-23 14:38:29 +00:00
}
2017-01-19 22:45:24 +00:00
}
if self.index != new_index {
self.index = new_index;
2015-06-24 00:41:39 +00:00
2017-01-19 22:45:24 +00:00
let offset = self.index * CHUNK_SIZE;
self.output.as_mut().unwrap()
.seek(SeekFrom::Start(offset as u64)).unwrap();
let (_headers, data) = request_chunk(&self.session, self.shared.file_id, self.index).split();
self.data_rx = data;
}
}
2015-07-07 21:40:31 +00:00
2017-01-19 22:45:24 +00:00
fn finish(&mut self) {
let mut output = self.output.take().unwrap();
let complete_tx = self.complete_tx.take().unwrap();
output.seek(SeekFrom::Start(0)).unwrap();
2017-08-03 19:37:04 +00:00
let _ = complete_tx.send(output);
2017-01-19 22:45:24 +00:00
}
}
impl Future for AudioFileFetch {
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<(), ()> {
loop {
let mut progress = false;
match self.seek_rx.poll() {
Ok(Async::Ready(None)) => {
return Ok(Async::Ready(()));
}
Ok(Async::Ready(Some(offset))) => {
progress = true;
let index = offset as usize / CHUNK_SIZE;
self.download(index);
}
Ok(Async::NotReady) => (),
Err(()) => unreachable!(),
}
match self.data_rx.poll() {
Ok(Async::Ready(Some(data))) => {
progress = true;
self.output.as_mut().unwrap()
2017-01-29 17:54:32 +00:00
.write_all(data.as_ref()).unwrap();
2017-01-19 22:45:24 +00:00
}
Ok(Async::Ready(None)) => {
progress = true;
trace!("chunk {} / {} complete", self.index, self.shared.chunk_count);
2017-01-19 22:45:24 +00:00
let full = {
let mut bitmap = self.shared.bitmap.lock().unwrap();
bitmap.insert(self.index as usize);
self.shared.cond.notify_all();
bitmap.len() >= self.shared.chunk_count
};
if full {
self.finish();
return Ok(Async::Ready(()));
}
let new_index = (self.index + 1) % self.shared.chunk_count;
self.download(new_index);
}
Ok(Async::NotReady) => (),
Err(ChannelError) => {
warn!("error from channel");
return Ok(Async::Ready(()));
},
}
if !progress {
return Ok(Async::NotReady);
}
}
2015-06-23 17:34:48 +00:00
}
2015-06-23 14:38:29 +00:00
}
impl Read for AudioFileStreaming {
2015-06-23 14:38:29 +00:00
fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
2015-07-07 21:40:31 +00:00
let index = self.position as usize / CHUNK_SIZE;
let offset = self.position as usize % CHUNK_SIZE;
2016-01-02 15:19:39 +00:00
let len = min(output.len(), CHUNK_SIZE - offset);
2015-06-23 14:38:29 +00:00
2015-07-07 21:40:31 +00:00
let mut bitmap = self.shared.bitmap.lock().unwrap();
2016-04-24 14:40:05 +00:00
while !bitmap.contains(index) {
2015-07-07 21:40:31 +00:00
bitmap = self.shared.cond.wait(bitmap).unwrap();
2015-06-23 14:38:29 +00:00
}
2015-07-07 21:40:31 +00:00
drop(bitmap);
2015-09-01 11:20:37 +00:00
let read_len = try!(self.read_file.read(&mut output[..len]));
2015-06-23 14:38:29 +00:00
2015-09-01 11:20:37 +00:00
self.position += read_len as u64;
2015-06-23 14:38:29 +00:00
2015-09-01 11:20:37 +00:00
Ok(read_len)
2015-06-23 14:38:29 +00:00
}
}
impl Seek for AudioFileStreaming {
2015-07-07 21:40:31 +00:00
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.position = try!(self.read_file.seek(pos));
2016-01-02 15:19:39 +00:00
// Notify the fetch thread to get the correct block
// This can fail if fetch thread has completed, in which case the
// block is ready. Just ignore the error.
let _ = self.seek.unbounded_send(self.position);
2017-01-19 22:45:24 +00:00
Ok(self.position)
2015-06-23 14:38:29 +00:00
}
}
impl Read for AudioFile {
fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
match *self {
AudioFile::Cached(ref mut file) => file.read(output),
AudioFile::Streaming(ref mut file) => file.read(output),
}
}
}
impl Seek for AudioFile {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match *self {
AudioFile::Cached(ref mut file) => file.seek(pos),
AudioFile::Streaming(ref mut file) => file.seek(pos),
}
}
}