add audio seeking support

This commit is contained in:
Paul Lietar 2015-06-23 18:34:48 +01:00
parent 7ffe996652
commit 4835d25370
7 changed files with 203 additions and 120 deletions

View file

@ -1,7 +1,9 @@
use crypto::aes; use crypto::aes;
use crypto::symmetriccipher::SynchronousStreamCipher; use crypto::symmetriccipher::SynchronousStreamCipher;
use readall::ReadAllExt;
use std::io; use std::io;
use std::ops::Add;
use num::FromPrimitive;
use gmp::Mpz;
use audio_key::AudioKey; use audio_key::AudioKey;
@ -16,16 +18,10 @@ pub struct AudioDecrypt<T : io::Read> {
} }
impl <T : io::Read> AudioDecrypt<T> { impl <T : io::Read> AudioDecrypt<T> {
pub fn new(key: AudioKey, mut reader: T) -> AudioDecrypt<T> { pub fn new(key: AudioKey, reader: T) -> AudioDecrypt<T> {
let mut cipher = aes::ctr(aes::KeySize::KeySize128, let cipher = aes::ctr(aes::KeySize::KeySize128,
&key, &key,
AUDIO_AESIV); AUDIO_AESIV);
let mut buf = [0; 0xa7];
let mut buf2 = [0; 0xa7];
reader.read_all(&mut buf).unwrap();
cipher.process(&buf, &mut buf2);
AudioDecrypt { AudioDecrypt {
cipher: cipher, cipher: cipher,
key: key, key: key,
@ -45,9 +41,23 @@ impl <T : io::Read> io::Read for AudioDecrypt<T> {
} }
} }
impl <T : io::Read> io::Seek for AudioDecrypt<T> { impl <T : io::Read + io::Seek> io::Seek for AudioDecrypt<T> {
fn seek(&mut self, _pos: io::SeekFrom) -> io::Result<u64> { fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
Err(io::Error::new(io::ErrorKind::Other, "Cannot seek")) let newpos = try!(self.reader.seek(pos));
let skip = newpos % 16;
let iv = Mpz::from_bytes_be(AUDIO_AESIV)
.add(Mpz::from_u64(newpos / 16).unwrap())
.to_bytes_be();
self.cipher = aes::ctr(aes::KeySize::KeySize128,
&self.key,
&iv);
let buf = vec![0u8; skip as usize];
let mut buf2 = vec![0u8; skip as usize];
self.cipher.process(&buf, &mut buf2);
Ok(newpos as u64)
} }
} }

View file

@ -1,23 +1,24 @@
use byteorder::{ByteOrder, BigEndian}; use byteorder::{ByteOrder, BigEndian};
use std::cmp::min; use std::cmp::min;
use std::collections::BitSet; use std::collections::BitSet;
use std::io; use std::io::{self, SeekFrom};
use std::slice::bytes::copy_memory; use std::slice::bytes::copy_memory;
use std::sync::{mpsc, Arc, Condvar, Mutex}; use std::sync::{Arc, Condvar, Mutex};
use std::sync::mpsc::{self, TryRecvError};
use stream::{StreamRequest, StreamEvent}; use stream::{StreamRequest, StreamEvent};
use util::FileId; use util::FileId;
use std::thread;
const CHUNK_SIZE : usize = 0x40000;
const CHUNK_SIZE: usize = 0x40000;
#[derive(Clone)] #[derive(Clone)]
pub struct AudioFileRef(Arc<AudioFile>); pub struct AudioFile {
struct AudioFile {
file: FileId, file: FileId,
size: usize, size: usize,
seek: mpsc::Sender<u64>,
data: Mutex<AudioFileData>, data: Arc<(Mutex<AudioFileData>, Condvar)>,
cond: Condvar
} }
struct AudioFileData { struct AudioFileData {
@ -25,8 +26,8 @@ struct AudioFileData {
bitmap: BitSet, bitmap: BitSet,
} }
impl AudioFileRef { impl AudioFile {
pub fn new(file: FileId, streams: mpsc::Sender<StreamRequest>) -> AudioFileRef { pub fn new(file: FileId, streams: mpsc::Sender<StreamRequest>) -> AudioFile {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
streams.send(StreamRequest { streams.send(StreamRequest {
@ -52,69 +53,98 @@ impl AudioFileRef {
size.unwrap() as usize size.unwrap() as usize
}; };
AudioFileRef(Arc::new(AudioFile { let bufsize = size + (CHUNK_SIZE - size % CHUNK_SIZE);
let (tx, rx) = mpsc::channel();
let ret = AudioFile {
file: file, file: file,
size: size, size: size,
seek: tx,
data: Mutex::new(AudioFileData { data: Arc::new((Mutex::new(AudioFileData {
buffer: vec![0u8; size + (CHUNK_SIZE - size % CHUNK_SIZE)], buffer: vec![0u8; bufsize],
bitmap: BitSet::with_capacity(size / CHUNK_SIZE) bitmap: BitSet::with_capacity(bufsize / CHUNK_SIZE as usize)
}), }), Condvar::new())),
cond: Condvar::new(), };
}))
let f = ret.clone();
thread::spawn( move || { f.fetch(streams, rx); });
ret
} }
pub fn fetch(&self, streams: mpsc::Sender<StreamRequest>) { fn fetch_chunk(&self, streams: &mpsc::Sender<StreamRequest>, index: usize) {
let &AudioFileRef(ref inner) = self; 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();
let mut index : usize = 0; 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();
while index * CHUNK_SIZE < inner.size { if offset >= CHUNK_SIZE {
let (tx, rx) = mpsc::channel(); break
streams.send(StreamRequest {
id: inner.file,
offset: (index * CHUNK_SIZE / 4) as u32,
size: (CHUNK_SIZE / 4) as u32,
callback: tx
}).unwrap();
let mut offset = 0;
for event in rx.iter() {
match event {
StreamEvent::Header(_,_) => (),
StreamEvent::Data(data) => {
let mut handle = inner.data.lock().unwrap();
copy_memory(&data, &mut handle.buffer[index * CHUNK_SIZE + offset..]);
offset += data.len();
if offset >= CHUNK_SIZE {
break
}
} }
} }
} }
}
{
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
}
};
{ {
let mut handle = inner.data.lock().unwrap(); let handle = self.data.0.lock().unwrap();
handle.bitmap.insert(index); while handle.bitmap.contains(&index) && index * CHUNK_SIZE < self.size {
inner.cond.notify_all(); index += 1;
}
} }
index += 1; if index * CHUNK_SIZE < self.size {
self.fetch_chunk(&streams, index)
}
} }
} }
} }
pub struct AudioFileReader { pub struct AudioFileReader {
file: AudioFileRef, file: AudioFile,
position: usize position: usize,
} }
impl AudioFileReader { impl AudioFileReader {
pub fn new(file: &AudioFileRef) -> AudioFileReader { pub fn new(file: AudioFile) -> AudioFileReader {
AudioFileReader { AudioFileReader {
file: file.clone(), file: file,
position: 0 position: 0
} }
} }
@ -126,11 +156,10 @@ impl io::Read for AudioFileReader {
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 &AudioFileRef(ref inner) = &self.file; let mut handle = self.file.data.0.lock().unwrap();
let mut handle = inner.data.lock().unwrap();
while !handle.bitmap.contains(&index) { while !handle.bitmap.contains(&index) {
handle = inner.cond.wait(handle).unwrap(); handle = self.file.data.1.wait(handle).unwrap();
} }
copy_memory(&handle.buffer[self.position..self.position+len], output); copy_memory(&handle.buffer[self.position..self.position+len], output);
@ -141,8 +170,16 @@ impl io::Read for AudioFileReader {
} }
impl io::Seek for AudioFileReader { impl io::Seek for AudioFileReader {
fn seek(&mut self, _pos: io::SeekFrom) -> io::Result<u64> { fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
Err(io::Error::new(io::ErrorKind::Other, "Cannot seek")) 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)
} }
} }

View file

@ -9,7 +9,6 @@ use std::result;
use std::sync::mpsc; use std::sync::mpsc;
use keys::SharedKeys; use keys::SharedKeys;
use util;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -69,7 +68,7 @@ impl PlainConnection {
pub fn recv_packet(&mut self) -> Result<Vec<u8>> { pub fn recv_packet(&mut self) -> Result<Vec<u8>> {
let size = try!(self.stream.read_u32::<BigEndian>()) as usize; let size = try!(self.stream.read_u32::<BigEndian>()) as usize;
let mut buffer = util::alloc_buffer(size); let mut buffer = vec![0u8; size];
BigEndian::write_u32(&mut buffer, size as u32); BigEndian::write_u32(&mut buffer, size as u32);
try!(self.stream.read_all(&mut buffer[4..])); try!(self.stream.read_all(&mut buffer[4..]));

View file

@ -1,13 +1,13 @@
use portaudio; use portaudio;
use std::sync::mpsc; use std::sync::mpsc;
use std::thread;
use vorbis; 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::{AudioFileRef, AudioFileReader}; use audio_file::{AudioFile, AudioFileReader};
use audio_decrypt::AudioDecrypt; use audio_decrypt::AudioDecrypt;
use util::Subfile;
pub struct Player; pub struct Player;
@ -28,14 +28,13 @@ impl Player {
key key
}; };
let reader = { let mut decoder =
let file = AudioFileRef::new(file_id, session.stream.clone()); vorbis::Decoder::new(
let f = file.clone(); Subfile::new(
let s = session.stream.clone(); AudioDecrypt::new(key,
thread::spawn( move || { f.fetch(s) }); AudioFileReader::new(
AudioDecrypt::new(key, AudioFileReader::new(&file)) AudioFile::new(file_id, session.stream.clone()))), 0xa7)).unwrap();
}; //decoder.time_seek(60f64).unwrap();
portaudio::initialize().unwrap(); portaudio::initialize().unwrap();
@ -48,8 +47,6 @@ impl Player {
).unwrap(); ).unwrap();
stream.start().unwrap(); stream.start().unwrap();
let mut decoder = vorbis::Decoder::new(reader).unwrap();
for pkt in decoder.packets() { for pkt in decoder.packets() {
match pkt { match pkt {
Ok(packet) => { Ok(packet) => {

View file

@ -92,43 +92,50 @@ impl StreamManager {
let mut packet = Cursor::new(&data as &[u8]); let mut packet = Cursor::new(&data as &[u8]);
let id : ChannelId = packet.read_u16::<BigEndian>().unwrap(); let id : ChannelId = packet.read_u16::<BigEndian>().unwrap();
let channel = match self.channels.get_mut(&id) { let mut close = false;
Some(ch) => ch, {
None => { return; } let channel = match self.channels.get_mut(&id) {
}; Some(ch) => ch,
None => { return; }
};
match channel.mode { match channel.mode {
ChannelMode::Header => { ChannelMode::Header => {
let mut length = 0; let mut length = 0;
while packet.position() < data.len() as u64 { while packet.position() < data.len() as u64 {
length = packet.read_u16::<BigEndian>().unwrap(); length = packet.read_u16::<BigEndian>().unwrap();
if length > 0 { if length > 0 {
let header_id = packet.read_u8().unwrap(); let header_id = packet.read_u8().unwrap();
channel.callback.send(StreamEvent::Header( channel.callback.send(StreamEvent::Header(
header_id, header_id,
data.clone() data.clone()
.offset(packet.position() as usize) .offset(packet.position() as usize)
.limit(length as usize - 1) .limit(length as usize - 1)
)).unwrap(); )).unwrap();
packet.seek(SeekFrom::Current(length as i64 - 1)).unwrap(); packet.seek(SeekFrom::Current(length as i64 - 1)).unwrap();
}
}
if length == 0 {
channel.mode = ChannelMode::Data;
} }
} }
if length == 0 { ChannelMode::Data => {
channel.mode = ChannelMode::Data; if packet.position() < data.len() as u64 {
channel.callback.send(StreamEvent::Data(
data.clone().offset(packet.position() as usize))).unwrap();
} else {
close = true;
}
} }
} }
}
ChannelMode::Data => { if close {
if packet.position() < data.len() as u64 { self.channels.remove(&id);
channel.callback.send(StreamEvent::Data(
data.clone().offset(packet.position() as usize))).unwrap();
} else {
// TODO: close the channel
}
}
} }
} }
} }

View file

@ -3,10 +3,12 @@ use rand::{Rng,Rand};
mod int128; mod int128;
mod spotify_id; mod spotify_id;
mod arcvec; mod arcvec;
mod subfile;
pub use util::int128::u128; pub use util::int128::u128;
pub use util::spotify_id::{SpotifyId, FileId}; pub use util::spotify_id::{SpotifyId, FileId};
pub use util::arcvec::ArcVec; pub use util::arcvec::ArcVec;
pub use util::subfile::Subfile;
#[macro_export] #[macro_export]
macro_rules! eprintln( macro_rules! eprintln(
@ -37,15 +39,6 @@ pub fn rand_vec<G: Rng, R: Rand>(rng: &mut G, size: usize) -> Vec<R> {
return vec return vec
} }
pub fn alloc_buffer(size: usize) -> Vec<u8> {
let mut vec = Vec::with_capacity(size);
unsafe {
vec.set_len(size);
}
vec
}
pub mod version { pub mod version {
include!(concat!(env!("OUT_DIR"), "/version.rs")); include!(concat!(env!("OUT_DIR"), "/version.rs"));

40
src/util/subfile.rs Normal file
View file

@ -0,0 +1,40 @@
use std::io::{Read, Seek, SeekFrom, Result};
pub struct Subfile<T: Read + Seek> {
stream: T,
offset: u64
}
impl <T: Read + Seek> Subfile<T> {
pub fn new(mut stream: T, offset: u64) -> Subfile<T> {
stream.seek(SeekFrom::Start(offset)).unwrap();
Subfile {
stream: stream,
offset: offset
}
}
}
impl <T: Read + Seek> Read for Subfile<T> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.stream.read(buf)
}
}
impl <T: Read + Seek> Seek for Subfile<T> {
fn seek(&mut self, mut pos: SeekFrom) -> Result<u64> {
pos = match pos {
SeekFrom::Start(offset) => SeekFrom::Start(offset + self.offset),
x => x
};
let newpos = try!(self.stream.seek(pos));
if newpos > self.offset {
return Ok(newpos - self.offset)
} else {
return Ok(0)
}
}
}