mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
add audio seeking support
This commit is contained in:
parent
7ffe996652
commit
4835d25370
7 changed files with 203 additions and 120 deletions
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)]
|
|
||||||
pub struct AudioFileRef(Arc<AudioFile>);
|
|
||||||
|
|
||||||
struct AudioFile {
|
#[derive(Clone)]
|
||||||
|
pub 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,39 +53,42 @@ impl AudioFileRef {
|
||||||
size.unwrap() as usize
|
size.unwrap() as usize
|
||||||
};
|
};
|
||||||
|
|
||||||
AudioFileRef(Arc::new(AudioFile {
|
let bufsize = size + (CHUNK_SIZE - size % CHUNK_SIZE);
|
||||||
file: file,
|
|
||||||
size: size,
|
|
||||||
|
|
||||||
data: Mutex::new(AudioFileData {
|
|
||||||
buffer: vec![0u8; size + (CHUNK_SIZE - size % CHUNK_SIZE)],
|
|
||||||
bitmap: BitSet::with_capacity(size / CHUNK_SIZE)
|
|
||||||
}),
|
|
||||||
cond: Condvar::new(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn fetch(&self, streams: mpsc::Sender<StreamRequest>) {
|
|
||||||
let &AudioFileRef(ref inner) = self;
|
|
||||||
|
|
||||||
let mut index : usize = 0;
|
|
||||||
|
|
||||||
while index * CHUNK_SIZE < inner.size {
|
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
let ret = AudioFile {
|
||||||
|
file: file,
|
||||||
|
size: size,
|
||||||
|
seek: tx,
|
||||||
|
|
||||||
|
data: Arc::new((Mutex::new(AudioFileData {
|
||||||
|
buffer: vec![0u8; bufsize],
|
||||||
|
bitmap: BitSet::with_capacity(bufsize / CHUNK_SIZE as usize)
|
||||||
|
}), Condvar::new())),
|
||||||
|
};
|
||||||
|
|
||||||
|
let f = ret.clone();
|
||||||
|
|
||||||
|
thread::spawn( move || { f.fetch(streams, rx); });
|
||||||
|
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fetch_chunk(&self, streams: &mpsc::Sender<StreamRequest>, index: usize) {
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
streams.send(StreamRequest {
|
streams.send(StreamRequest {
|
||||||
id: inner.file,
|
id: self.file,
|
||||||
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
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
|
|
||||||
let mut offset = 0;
|
let mut offset = 0usize;
|
||||||
for event in rx.iter() {
|
for event in rx.iter() {
|
||||||
match event {
|
match event {
|
||||||
StreamEvent::Header(_,_) => (),
|
StreamEvent::Header(_,_) => (),
|
||||||
StreamEvent::Data(data) => {
|
StreamEvent::Data(data) => {
|
||||||
let mut handle = inner.data.lock().unwrap();
|
let mut handle = self.data.0.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();
|
||||||
|
|
||||||
|
@ -96,25 +100,51 @@ impl AudioFileRef {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut handle = inner.data.lock().unwrap();
|
let mut handle = self.data.0.lock().unwrap();
|
||||||
handle.bitmap.insert(index);
|
handle.bitmap.insert(index as usize);
|
||||||
inner.cond.notify_all();
|
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 handle = self.data.0.lock().unwrap();
|
||||||
|
while handle.bitmap.contains(&index) && index * CHUNK_SIZE < self.size {
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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..]));
|
||||||
|
|
|
@ -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) => {
|
||||||
|
|
|
@ -92,6 +92,8 @@ 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 mut close = false;
|
||||||
|
{
|
||||||
let channel = match self.channels.get_mut(&id) {
|
let channel = match self.channels.get_mut(&id) {
|
||||||
Some(ch) => ch,
|
Some(ch) => ch,
|
||||||
None => { return; }
|
None => { return; }
|
||||||
|
@ -126,11 +128,16 @@ impl StreamManager {
|
||||||
channel.callback.send(StreamEvent::Data(
|
channel.callback.send(StreamEvent::Data(
|
||||||
data.clone().offset(packet.position() as usize))).unwrap();
|
data.clone().offset(packet.position() as usize))).unwrap();
|
||||||
} else {
|
} else {
|
||||||
// TODO: close the channel
|
close = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if close {
|
||||||
|
self.channels.remove(&id);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Subsystem for StreamManager {
|
impl Subsystem for StreamManager {
|
||||||
|
|
|
@ -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
40
src/util/subfile.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue