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-07-02 17:24:25 +00:00
|
|
|
use std::thread;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
use stream::StreamEvent;
|
2015-06-23 14:38:29 +00:00
|
|
|
use util::FileId;
|
2015-07-02 17:24:25 +00:00
|
|
|
use session::Session;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
const CHUNK_SIZE : usize = 0x10000;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
pub struct AudioFile<'s> {
|
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-07-02 17:24:25 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
thread: thread::JoinGuard<'s, ()>,
|
2015-06-24 00:41:39 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-06-24 00:41:39 +00:00
|
|
|
struct AudioFileShared {
|
2015-07-02 17:24:25 +00:00
|
|
|
file_id: FileId,
|
2015-06-24 00:41:39 +00:00
|
|
|
size: usize,
|
|
|
|
data: Mutex<AudioFileData>,
|
|
|
|
cond: Condvar
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AudioFileData {
|
|
|
|
buffer: Vec<u8>,
|
|
|
|
bitmap: BitSet,
|
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
impl <'s> AudioFile <'s> {
|
|
|
|
pub fn new(session: &Session, file_id: FileId) -> AudioFile {
|
|
|
|
let mut it = session.stream(file_id, 0, 1).into_iter()
|
|
|
|
.filter_map(|event| {
|
2015-06-23 14:38:29 +00:00
|
|
|
match event {
|
2015-07-02 17:24:25 +00:00
|
|
|
StreamEvent::Header(id, ref data) if id == 0x3 => {
|
|
|
|
Some(BigEndian::read_u32(data) as usize * 4)
|
|
|
|
}
|
|
|
|
_ => None
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
2015-07-02 17:24:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let size = it.next().unwrap();
|
2015-06-23 17:34:48 +00:00
|
|
|
|
|
|
|
let bufsize = size + (CHUNK_SIZE - size % CHUNK_SIZE);
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-06-24 00:41:39 +00:00
|
|
|
let shared = Arc::new(AudioFileShared {
|
2015-07-02 17:24:25 +00:00
|
|
|
file_id: file_id,
|
2015-06-24 00:41:39 +00:00
|
|
|
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(),
|
|
|
|
});
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
let shared_ = shared.clone();
|
|
|
|
let (seek_tx, seek_rx) = mpsc::channel();
|
|
|
|
|
2015-06-24 00:41:39 +00:00
|
|
|
let file = AudioFile {
|
2015-07-02 17:24:25 +00:00
|
|
|
thread: thread::scoped( move || { AudioFile::fetch(session, shared_, seek_rx); }),
|
2015-06-24 00:41:39 +00:00
|
|
|
position: 0,
|
2015-07-02 17:24:25 +00:00
|
|
|
seek: seek_tx,
|
|
|
|
shared: shared,
|
2015-06-23 17:34:48 +00:00
|
|
|
};
|
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
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
fn fetch_chunk(session: &Session, shared: &Arc<AudioFileShared>, index: usize) {
|
|
|
|
let rx = session.stream(shared.file_id,
|
|
|
|
(index * CHUNK_SIZE / 4) as u32,
|
|
|
|
(CHUNK_SIZE / 4) as u32);
|
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-07-02 17:24:25 +00:00
|
|
|
fn fetch(session: &Session, shared: Arc<AudioFileShared>, 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 {
|
2015-07-02 17:24:25 +00:00
|
|
|
AudioFile::fetch_chunk(session, &shared, index)
|
2015-06-23 17:34:48 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
impl <'s> io::Read for AudioFile <'s> {
|
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-07-02 17:24:25 +00:00
|
|
|
impl <'s> io::Seek for AudioFile <'s> {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|