Merge AudioFile and AudioFileReader

This commit is contained in:
Paul Lietar 2015-06-24 01:41:39 +01:00
parent 4835d25370
commit caf78992fb
2 changed files with 46 additions and 53 deletions

View file

@ -14,11 +14,16 @@ const CHUNK_SIZE : usize = 0x40000;
#[derive(Clone)] #[derive(Clone)]
pub struct AudioFile { pub struct AudioFile {
file: FileId, position: usize,
size: usize,
seek: mpsc::Sender<u64>, seek: mpsc::Sender<u64>,
shared: Arc<AudioFileShared>,
}
data: Arc<(Mutex<AudioFileData>, Condvar)>, struct AudioFileShared {
fileid: FileId,
size: usize,
data: Mutex<AudioFileData>,
cond: Condvar
} }
struct AudioFileData { struct AudioFileData {
@ -27,11 +32,11 @@ struct AudioFileData {
} }
impl AudioFile { impl AudioFile {
pub fn new(file: FileId, streams: mpsc::Sender<StreamRequest>) -> AudioFile { pub fn new(fileid: FileId, streams: mpsc::Sender<StreamRequest>) -> AudioFile {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
streams.send(StreamRequest { streams.send(StreamRequest {
id: file, id: fileid,
offset: 0, offset: 0,
size: 1, size: 1,
callback: tx callback: tx
@ -56,28 +61,31 @@ impl AudioFile {
let bufsize = size + (CHUNK_SIZE - size % CHUNK_SIZE); let bufsize = size + (CHUNK_SIZE - size % CHUNK_SIZE);
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let ret = AudioFile { let shared = Arc::new(AudioFileShared {
file: file, fileid: fileid,
size: size, size: size,
seek: tx, data: Mutex::new(AudioFileData {
data: Arc::new((Mutex::new(AudioFileData {
buffer: vec![0u8; bufsize], buffer: vec![0u8; bufsize],
bitmap: BitSet::with_capacity(bufsize / CHUNK_SIZE as usize) bitmap: BitSet::with_capacity(bufsize / CHUNK_SIZE as usize)
}), Condvar::new())), }),
cond: Condvar::new(),
});
let file = AudioFile {
position: 0,
seek: tx,
shared: shared.clone(),
}; };
let f = ret.clone(); thread::spawn( move || { AudioFile::fetch(shared, streams, rx); });
thread::spawn( move || { f.fetch(streams, rx); }); file
ret
} }
fn fetch_chunk(&self, streams: &mpsc::Sender<StreamRequest>, index: usize) { fn fetch_chunk(shared: &Arc<AudioFileShared>, streams: &mpsc::Sender<StreamRequest>, index: usize) {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
streams.send(StreamRequest { streams.send(StreamRequest {
id: self.file, id: shared.fileid,
offset: (index * CHUNK_SIZE / 4) as u32, offset: (index * CHUNK_SIZE / 4) as u32,
size: (CHUNK_SIZE / 4) as u32, size: (CHUNK_SIZE / 4) as u32,
callback: tx callback: tx
@ -88,7 +96,7 @@ impl AudioFile {
match event { match event {
StreamEvent::Header(_,_) => (), StreamEvent::Header(_,_) => (),
StreamEvent::Data(data) => { StreamEvent::Data(data) => {
let mut handle = self.data.0.lock().unwrap(); let mut handle = shared.data.lock().unwrap();
copy_memory(&data, &mut handle.buffer[index * CHUNK_SIZE + offset ..]); copy_memory(&data, &mut handle.buffer[index * CHUNK_SIZE + offset ..]);
offset += data.len(); offset += data.len();
@ -100,16 +108,16 @@ impl AudioFile {
} }
{ {
let mut handle = self.data.0.lock().unwrap(); let mut handle = shared.data.lock().unwrap();
handle.bitmap.insert(index as usize); handle.bitmap.insert(index as usize);
self.data.1.notify_all(); shared.cond.notify_all();
} }
} }
fn fetch(&self, streams: mpsc::Sender<StreamRequest>, seek: mpsc::Receiver<u64>) { fn fetch(shared: Arc<AudioFileShared>, streams: mpsc::Sender<StreamRequest>, seek: mpsc::Receiver<u64>) {
let mut index = 0; let mut index = 0;
loop { loop {
index = if index * CHUNK_SIZE < self.size { index = if index * CHUNK_SIZE < shared.size {
match seek.try_recv() { match seek.try_recv() {
Ok(position) => position as usize / CHUNK_SIZE, Ok(position) => position as usize / CHUNK_SIZE,
Err(TryRecvError::Empty) => index, Err(TryRecvError::Empty) => index,
@ -123,43 +131,29 @@ impl AudioFile {
}; };
{ {
let handle = self.data.0.lock().unwrap(); let handle = shared.data.lock().unwrap();
while handle.bitmap.contains(&index) && index * CHUNK_SIZE < self.size { while handle.bitmap.contains(&index) && index * CHUNK_SIZE < shared.size {
index += 1; index += 1;
} }
} }
if index * CHUNK_SIZE < self.size { if index * CHUNK_SIZE < shared.size {
self.fetch_chunk(&streams, index) AudioFile::fetch_chunk(&shared, &streams, index)
} }
} }
} }
} }
pub struct AudioFileReader { impl io::Read for AudioFile {
file: AudioFile,
position: usize,
}
impl AudioFileReader {
pub fn new(file: AudioFile) -> AudioFileReader {
AudioFileReader {
file: file,
position: 0
}
}
}
impl io::Read for AudioFileReader {
fn read(&mut self, output: &mut [u8]) -> io::Result<usize> { fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
let index = self.position / CHUNK_SIZE; let index = self.position / CHUNK_SIZE;
let offset = self.position % CHUNK_SIZE; let offset = self.position % CHUNK_SIZE;
let len = min(output.len(), CHUNK_SIZE-offset); let len = min(output.len(), CHUNK_SIZE-offset);
let mut handle = self.file.data.0.lock().unwrap(); let mut handle = self.shared.data.lock().unwrap();
while !handle.bitmap.contains(&index) { while !handle.bitmap.contains(&index) {
handle = self.file.data.1.wait(handle).unwrap(); handle = self.shared.cond.wait(handle).unwrap();
} }
copy_memory(&handle.buffer[self.position..self.position+len], output); copy_memory(&handle.buffer[self.position..self.position+len], output);
@ -169,16 +163,16 @@ impl io::Read for AudioFileReader {
} }
} }
impl io::Seek for AudioFileReader { impl io::Seek for AudioFile {
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> { fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
let newpos = match pos { let newpos = match pos {
SeekFrom::Start(offset) => offset as i64, SeekFrom::Start(offset) => offset as i64,
SeekFrom::End(offset) => self.file.size as i64 + offset, SeekFrom::End(offset) => self.shared.size as i64 + offset,
SeekFrom::Current(offset) => self.position as i64 + offset, SeekFrom::Current(offset) => self.position as i64 + offset,
}; };
self.position = min(newpos as usize, self.file.size); self.position = min(newpos as usize, self.shared.size);
self.file.seek.send(self.position as u64).unwrap(); self.seek.send(self.position as u64).unwrap();
Ok(self.position as u64) Ok(self.position as u64)
} }
} }

View file

@ -5,7 +5,7 @@ use vorbis;
use audio_key::{AudioKeyRequest, AudioKeyResponse}; use audio_key::{AudioKeyRequest, AudioKeyResponse};
use metadata::TrackRef; use metadata::TrackRef;
use session::Session; use session::Session;
use audio_file::{AudioFile, AudioFileReader}; use audio_file::AudioFile;
use audio_decrypt::AudioDecrypt; use audio_decrypt::AudioDecrypt;
use util::Subfile; use util::Subfile;
@ -32,8 +32,7 @@ impl Player {
vorbis::Decoder::new( vorbis::Decoder::new(
Subfile::new( Subfile::new(
AudioDecrypt::new(key, AudioDecrypt::new(key,
AudioFileReader::new( AudioFile::new(file_id, session.stream.clone())), 0xa7)).unwrap();
AudioFile::new(file_id, session.stream.clone()))), 0xa7)).unwrap();
//decoder.time_seek(60f64).unwrap(); //decoder.time_seek(60f64).unwrap();
portaudio::initialize().unwrap(); portaudio::initialize().unwrap();