librespot/src/audio_file.rs

181 lines
5.1 KiB
Rust
Raw Normal View History

2015-06-23 14:38:29 +00:00
use byteorder::{ByteOrder, BigEndian};
use std::cmp::min;
use std::collections::BitSet;
2015-06-23 17:34:48 +00:00
use std::io::{self, SeekFrom};
2015-06-23 14:38:29 +00:00
use std::slice::bytes::copy_memory;
2015-06-23 17:34:48 +00:00
use std::sync::{Arc, Condvar, Mutex};
use std::sync::mpsc::{self, TryRecvError};
2015-06-23 14:38:29 +00:00
use stream::{StreamRequest, StreamEvent};
use util::FileId;
2015-06-23 17:34:48 +00:00
use std::thread;
2015-06-23 14:38:29 +00:00
2015-06-23 17:34:48 +00:00
const CHUNK_SIZE : usize = 0x40000;
2015-06-23 14:38:29 +00:00
2015-06-23 17:34:48 +00:00
#[derive(Clone)]
pub struct AudioFile {
2015-06-24 00:41:39 +00:00
position: usize,
2015-06-23 17:34:48 +00:00
seek: mpsc::Sender<u64>,
2015-06-24 00:41:39 +00:00
shared: Arc<AudioFileShared>,
}
2015-06-23 14:38:29 +00:00
2015-06-24 00:41:39 +00:00
struct AudioFileShared {
fileid: FileId,
size: usize,
data: Mutex<AudioFileData>,
cond: Condvar
2015-06-23 14:38:29 +00:00
}
struct AudioFileData {
buffer: Vec<u8>,
bitmap: BitSet,
}
2015-06-23 17:34:48 +00:00
impl AudioFile {
2015-06-24 00:41:39 +00:00
pub fn new(fileid: FileId, streams: mpsc::Sender<StreamRequest>) -> AudioFile {
2015-06-23 14:38:29 +00:00
let (tx, rx) = mpsc::channel();
streams.send(StreamRequest {
2015-06-24 00:41:39 +00:00
id: fileid,
2015-06-23 14:38:29 +00:00
offset: 0,
size: 1,
callback: tx
}).unwrap();
let size = {
let mut size = None;
for event in rx.iter() {
match event {
StreamEvent::Header(id, data) => {
if id == 0x3 {
size = Some(BigEndian::read_u32(&data) * 4);
break;
}
},
StreamEvent::Data(_) => break
}
}
size.unwrap() as usize
};
2015-06-23 17:34:48 +00:00
let bufsize = size + (CHUNK_SIZE - size % CHUNK_SIZE);
let (tx, rx) = mpsc::channel();
2015-06-23 14:38:29 +00:00
2015-06-24 00:41:39 +00:00
let shared = Arc::new(AudioFileShared {
fileid: fileid,
size: size,
data: Mutex::new(AudioFileData {
2015-06-23 17:34:48 +00:00
buffer: vec![0u8; bufsize],
bitmap: BitSet::with_capacity(bufsize / CHUNK_SIZE as usize)
2015-06-24 00:41:39 +00:00
}),
cond: Condvar::new(),
});
let file = AudioFile {
position: 0,
seek: tx,
shared: shared.clone(),
2015-06-23 17:34:48 +00:00
};
2015-06-23 14:38:29 +00:00
2015-06-24 00:41:39 +00:00
thread::spawn( move || { AudioFile::fetch(shared, streams, rx); });
2015-06-23 14:38:29 +00:00
2015-06-24 00:41:39 +00:00
file
2015-06-23 17:34:48 +00:00
}
2015-06-24 00:41:39 +00:00
fn fetch_chunk(shared: &Arc<AudioFileShared>, streams: &mpsc::Sender<StreamRequest>, index: usize) {
2015-06-23 17:34:48 +00:00
let (tx, rx) = mpsc::channel();
streams.send(StreamRequest {
2015-06-24 00:41:39 +00:00
id: shared.fileid,
2015-06-23 17:34:48 +00:00
offset: (index * CHUNK_SIZE / 4) as u32,
size: (CHUNK_SIZE / 4) as u32,
callback: tx
}).unwrap();
2015-06-23 14:38:29 +00:00
2015-06-23 17:34:48 +00:00
let mut offset = 0usize;
for event in rx.iter() {
match event {
StreamEvent::Header(_,_) => (),
StreamEvent::Data(data) => {
2015-06-24 00:41:39 +00:00
let mut handle = shared.data.lock().unwrap();
2015-06-23 17:34:48 +00:00
copy_memory(&data, &mut handle.buffer[index * CHUNK_SIZE + offset ..]);
offset += data.len();
if offset >= CHUNK_SIZE {
break
2015-06-23 14:38:29 +00:00
}
}
}
2015-06-23 17:34:48 +00:00
}
2015-06-24 00:41:39 +00:00
2015-06-23 17:34:48 +00:00
{
2015-06-24 00:41:39 +00:00
let mut handle = shared.data.lock().unwrap();
2015-06-23 17:34:48 +00:00
handle.bitmap.insert(index as usize);
2015-06-24 00:41:39 +00:00
shared.cond.notify_all();
2015-06-23 17:34:48 +00:00
}
}
2015-06-24 00:41:39 +00:00
fn fetch(shared: Arc<AudioFileShared>, streams: mpsc::Sender<StreamRequest>, seek: mpsc::Receiver<u64>) {
2015-06-23 17:34:48 +00:00
let mut index = 0;
loop {
2015-06-24 00:41:39 +00:00
index = if index * CHUNK_SIZE < shared.size {
2015-06-23 17:34:48 +00:00
match seek.try_recv() {
Ok(position) => position as usize / CHUNK_SIZE,
Err(TryRecvError::Empty) => index,
Err(TryRecvError::Disconnected) => break
}
} else {
match seek.recv() {
Ok(position) => position as usize / CHUNK_SIZE,
Err(_) => break
}
};
2015-06-23 14:38:29 +00:00
{
2015-06-24 00:41:39 +00:00
let handle = shared.data.lock().unwrap();
while handle.bitmap.contains(&index) && index * CHUNK_SIZE < shared.size {
2015-06-23 17:34:48 +00:00
index += 1;
}
2015-06-23 14:38:29 +00:00
}
2015-06-24 00:41:39 +00:00
if index * CHUNK_SIZE < shared.size {
AudioFile::fetch_chunk(&shared, &streams, index)
2015-06-23 17:34:48 +00:00
}
2015-06-23 14:38:29 +00:00
}
}
}
2015-06-24 00:41:39 +00:00
impl io::Read for AudioFile {
2015-06-23 14:38:29 +00:00
fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
let index = self.position / CHUNK_SIZE;
let offset = self.position % CHUNK_SIZE;
let len = min(output.len(), CHUNK_SIZE-offset);
2015-06-24 00:41:39 +00:00
let mut handle = self.shared.data.lock().unwrap();
2015-06-23 14:38:29 +00:00
while !handle.bitmap.contains(&index) {
2015-06-24 00:41:39 +00:00
handle = self.shared.cond.wait(handle).unwrap();
2015-06-23 14:38:29 +00:00
}
copy_memory(&handle.buffer[self.position..self.position+len], output);
self.position += len;
Ok(len)
}
}
2015-06-24 00:41:39 +00:00
impl io::Seek for AudioFile {
2015-06-23 17:34:48 +00:00
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
let newpos = match pos {
SeekFrom::Start(offset) => offset as i64,
2015-06-24 00:41:39 +00:00
SeekFrom::End(offset) => self.shared.size as i64 + offset,
2015-06-23 17:34:48 +00:00
SeekFrom::Current(offset) => self.position as i64 + offset,
};
2015-06-24 00:41:39 +00:00
self.position = min(newpos as usize, self.shared.size);
self.seek.send(self.position as u64).unwrap();
2015-06-23 17:34:48 +00:00
Ok(self.position as u64)
2015-06-23 14:38:29 +00:00
}
}