librespot/src/audio_file.rs

187 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-23 14:38:29 +00:00
file: FileId,
size: usize,
2015-06-23 17:34:48 +00:00
seek: mpsc::Sender<u64>,
2015-06-23 14:38:29 +00:00
2015-06-23 17:34:48 +00:00
data: Arc<(Mutex<AudioFileData>, 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 {
pub fn new(file: FileId, streams: mpsc::Sender<StreamRequest>) -> AudioFile {
2015-06-23 14:38:29 +00:00
let (tx, rx) = mpsc::channel();
streams.send(StreamRequest {
id: file,
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-23 17:34:48 +00:00
let ret = AudioFile {
2015-06-23 14:38:29 +00:00
file: file,
size: size,
2015-06-23 17:34:48 +00:00
seek: tx,
2015-06-23 14:38:29 +00:00
2015-06-23 17:34:48 +00:00
data: Arc::new((Mutex::new(AudioFileData {
buffer: vec![0u8; bufsize],
bitmap: BitSet::with_capacity(bufsize / CHUNK_SIZE as usize)
}), Condvar::new())),
};
2015-06-23 14:38:29 +00:00
2015-06-23 17:34:48 +00:00
let f = ret.clone();
2015-06-23 14:38:29 +00:00
2015-06-23 17:34:48 +00:00
thread::spawn( move || { f.fetch(streams, rx); });
2015-06-23 14:38:29 +00:00
2015-06-23 17:34:48 +00:00
ret
}
fn fetch_chunk(&self, streams: &mpsc::Sender<StreamRequest>, index: usize) {
let (tx, rx) = mpsc::channel();
streams.send(StreamRequest {
id: self.file,
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) => {
let mut handle = self.data.0.lock().unwrap();
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
}
{
let mut handle = self.data.0.lock().unwrap();
handle.bitmap.insert(index as usize);
self.data.1.notify_all();
}
}
fn fetch(&self, streams: mpsc::Sender<StreamRequest>, seek: mpsc::Receiver<u64>) {
let mut index = 0;
loop {
index = if index * CHUNK_SIZE < self.size {
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-23 17:34:48 +00:00
let handle = self.data.0.lock().unwrap();
while handle.bitmap.contains(&index) && index * CHUNK_SIZE < self.size {
index += 1;
}
2015-06-23 14:38:29 +00:00
}
2015-06-23 17:34:48 +00:00
if index * CHUNK_SIZE < self.size {
self.fetch_chunk(&streams, index)
}
2015-06-23 14:38:29 +00:00
}
}
}
pub struct AudioFileReader {
2015-06-23 17:34:48 +00:00
file: AudioFile,
position: usize,
2015-06-23 14:38:29 +00:00
}
impl AudioFileReader {
2015-06-23 17:34:48 +00:00
pub fn new(file: AudioFile) -> AudioFileReader {
2015-06-23 14:38:29 +00:00
AudioFileReader {
2015-06-23 17:34:48 +00:00
file: file,
2015-06-23 14:38:29 +00:00
position: 0
}
}
}
impl io::Read for AudioFileReader {
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-23 17:34:48 +00:00
let mut handle = self.file.data.0.lock().unwrap();
2015-06-23 14:38:29 +00:00
while !handle.bitmap.contains(&index) {
2015-06-23 17:34:48 +00:00
handle = self.file.data.1.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)
}
}
impl io::Seek for AudioFileReader {
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,
SeekFrom::End(offset) => self.file.size as i64 + offset,
SeekFrom::Current(offset) => self.position as i64 + offset,
};
self.position = min(newpos as usize, self.file.size);
self.file.seek.send(self.position as u64).unwrap();
Ok(self.position as u64)
2015-06-23 14:38:29 +00:00
}
}