librespot/src/audio_file.rs

302 lines
8.4 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-01-19 22:45:24 +00:00
use channel::{Channel, ChannelData, ChannelError, ChannelHeaders};
use session::Session;
2017-01-19 22:45:24 +00:00
use util::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
2017-01-19 22:45:24 +00:00
component! {
AudioFileManager : AudioFileManagerInner { }
}
pub struct AudioFile {
2016-01-12 23:29:31 +00:00
read_file: fs::File,
2015-07-07 21:40:31 +00:00
position: u64,
2017-01-19 22:45:24 +00:00
seek: mpsc::UnboundedSender<u64>,
2015-07-07 21:40:31 +00:00
2015-06-24 00:41:39 +00:00
shared: Arc<AudioFileShared>,
}
2015-06-23 14:38:29 +00:00
2017-01-19 22:45:24 +00:00
pub struct AudioFileOpen {
session: Session,
data_rx: Option<ChannelData>,
headers: ChannelHeaders,
file_id: FileId,
complete_tx: Option<oneshot::Sender<NamedTempFile>>,
}
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
}
2017-01-19 22:45:24 +00:00
impl AudioFileOpen {
fn finish(&mut self, size: usize) -> AudioFile {
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
2017-01-19 22:45:24 +00:00
AudioFile {
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> {
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);
return Ok(Async::Ready(file));
2015-07-07 21:40:31 +00:00
}
}
}
2017-01-19 22:45:24 +00:00
}
2017-01-19 22:45:24 +00:00
impl AudioFileManager {
pub fn open(&self, file_id: FileId) -> (AudioFileOpen, oneshot::Receiver<NamedTempFile>) {
let (complete_tx, complete_rx) = oneshot::channel();
let (headers, data) = request_chunk(&self.session(), file_id, 0).split();
2015-07-07 21:40:31 +00:00
2017-01-19 22:45:24 +00:00
let open = AudioFileOpen {
session: self.session(),
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),
};
2017-01-19 22:45:24 +00:00
(open, complete_rx)
}
}
fn request_chunk(session: &Session, file: FileId, index: usize) -> Channel {
debug!("requesting chunk {}", index);
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();
complete_tx.complete(output);
}
}
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()
.write_all(&data).unwrap();
}
Ok(Async::Ready(None)) => {
progress = true;
debug!("chunk {} / {} complete", self.index, self.shared.chunk_count);
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 AudioFile {
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 AudioFile {
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.
2017-01-19 22:45:24 +00:00
let _ = self.seek.send(self.position);
Ok(self.position)
2015-06-23 14:38:29 +00:00
}
}