This repository has been archived by the owner on Sep 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
metadata-pipe.patch
335 lines (315 loc) · 12.4 KB
/
metadata-pipe.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
diff --git a/Cargo.lock b/Cargo.lock
index 9c9c2f6..e5019d0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1045,6 +1045,7 @@ dependencies = [
name = "librespot-metadata"
version = "0.1.3"
dependencies = [
+ "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
"librespot-core 0.1.3",
@@ -1059,6 +1060,7 @@ name = "librespot-playback"
version = "0.1.3"
dependencies = [
"alsa 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"cpal 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/metadata/Cargo.toml b/metadata/Cargo.toml
index a4ca257..2c79059 100644
--- a/metadata/Cargo.toml
+++ b/metadata/Cargo.toml
@@ -7,6 +7,7 @@ license="MIT"
edition = "2018"
[dependencies]
+base64 = "0.10"
byteorder = "1.3"
futures = "0.1"
linear-map = "1.2"
diff --git a/playback/Cargo.toml b/playback/Cargo.toml
index 699a3f5..d7eb2cd 100644
--- a/playback/Cargo.toml
+++ b/playback/Cargo.toml
@@ -17,6 +17,7 @@ path = "../metadata"
version = "0.1.3"
[dependencies]
+base64 = "0.10"
futures = "0.1"
log = "0.4"
byteorder = "1.3"
diff --git a/playback/src/lib.rs b/playback/src/lib.rs
index e2a2ac8..c089ddb 100644
--- a/playback/src/lib.rs
+++ b/playback/src/lib.rs
@@ -4,6 +4,7 @@ extern crate log;
extern crate byteorder;
extern crate futures;
extern crate shell_words;
+extern crate base64;
#[cfg(feature = "alsa-backend")]
extern crate alsa;
diff --git a/playback/src/mixer/mod.rs b/playback/src/mixer/mod.rs
index 325c1e1..3bad013 100644
--- a/playback/src/mixer/mod.rs
+++ b/playback/src/mixer/mod.rs
@@ -9,6 +9,7 @@ pub trait Mixer: Send {
fn get_audio_filter(&self) -> Option<Box<dyn AudioFilter + Send>> {
None
}
+ fn set_metadata_pipe(&mut self, _metadata_pipe: Option<String>) {}
}
pub trait AudioFilter {
@@ -42,6 +43,9 @@ impl Default for MixerConfig {
pub mod softmixer;
use self::softmixer::SoftMixer;
+pub mod pipemixer;
+use self::pipemixer::PipeMixer;
+
fn mk_sink<M: Mixer + 'static>(device: Option<MixerConfig>) -> Box<dyn Mixer> {
Box::new(M::open(device))
}
@@ -49,6 +53,7 @@ fn mk_sink<M: Mixer + 'static>(device: Option<MixerConfig>) -> Box<dyn Mixer> {
pub fn find<T: AsRef<str>>(name: Option<T>) -> Option<fn(Option<MixerConfig>) -> Box<dyn Mixer>> {
match name.as_ref().map(AsRef::as_ref) {
None | Some("softvol") => Some(mk_sink::<SoftMixer>),
+ Some("pipe") => Some(mk_sink::<PipeMixer>),
#[cfg(feature = "alsa-backend")]
Some("alsa") => Some(mk_sink::<AlsaMixer>),
_ => None,
diff --git a/playback/src/mixer/pipemixer.rs b/playback/src/mixer/pipemixer.rs
new file mode 100644
index 0000000..5d1bd72
--- /dev/null
+++ b/playback/src/mixer/pipemixer.rs
@@ -0,0 +1,56 @@
+use base64;
+use std::f32;
+use std::fs::File;
+use std::io::Write;
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::sync::Arc;
+
+use super::{Mixer, MixerConfig};
+
+#[derive(Clone)]
+pub struct PipeMixer {
+ volume: Arc<AtomicUsize>,
+ pipe: Option<String>,
+}
+
+impl Mixer for PipeMixer {
+ fn open(_: Option<MixerConfig>) -> PipeMixer {
+ PipeMixer {
+ volume: Arc::new(AtomicUsize::new(0xFFFF)),
+ pipe: None,
+ }
+ }
+ fn start(&self) {}
+ fn stop(&self) {}
+ fn volume(&self) -> u16 {
+ self.volume.load(Ordering::Relaxed) as u16
+ }
+ fn set_volume(&self, volume: u16) {
+ self.volume.store(volume as usize, Ordering::Relaxed);
+
+ if let Some(path) = self.pipe.as_ref() {
+ let vol = volume;
+ let metadata_vol = if vol == 0 {
+ -144.0f32
+ } else if vol == 1 {
+ -30.0f32
+ } else if vol == 0xFFFF {
+ 0.0f32
+ } else {
+ ((vol as f32) - (0xFFFF as f32)) * 30.0f32 / (0xFFFE as f32)
+ };
+
+ let vol_string = format!("{:.*},0.00,0.00,0.00", 2, metadata_vol);
+ let vol_string_len = vol_string.chars().count();
+ let metadata_vol_string = base64::encode(&vol_string);
+ let metadata_xml = format!("<item><type>73736e63</type><code>70766f6c</code><length>{}</length>\n<data encoding=\"base64\">\n{}</data></item>", vol_string_len, metadata_vol_string);
+
+ let mut f = File::create(path).expect("Unable to open pipe");
+ f.write_all(metadata_xml.as_bytes())
+ .expect("Unable to write data");
+ }
+ }
+ fn set_metadata_pipe(&mut self, metadata_pipe: Option<String>) {
+ self.pipe = metadata_pipe;
+ }
+}
diff --git a/playback/src/player.rs b/playback/src/player.rs
index 404493f..86a83e2 100644
--- a/playback/src/player.rs
+++ b/playback/src/player.rs
@@ -1,6 +1,7 @@
use byteorder::{LittleEndian, ReadBytesExt};
use futures;
use futures::{future, Async, Future, Poll, Stream};
+use base64;
use std;
use std::borrow::Cow;
use std::cmp::max;
@@ -8,6 +9,8 @@ use std::io::{Read, Result, Seek, SeekFrom};
use std::mem;
use std::thread;
use std::time::{Duration, Instant};
+use std::fs::File;
+use std::io::Write;
use crate::config::{Bitrate, PlayerConfig};
use librespot_core::session::Session;
@@ -22,7 +25,7 @@ use crate::audio::{
READ_AHEAD_DURING_PLAYBACK_ROUNDTRIPS, READ_AHEAD_DURING_PLAYBACK_SECONDS,
};
use crate::audio_backend::Sink;
-use crate::metadata::{AudioItem, FileFormat};
+use crate::metadata::{FileFormat, Metadata, Track, Album, Artist, AudioItem};
use crate::mixer::AudioFilter;
const PRELOAD_NEXT_TRACK_BEFORE_END_DURATION_MS: u32 = 30000;
@@ -54,6 +57,7 @@ struct PlayerInternal {
sink_event_callback: Option<SinkEventCallback>,
audio_filter: Option<Box<dyn AudioFilter + Send>>,
event_senders: Vec<futures::sync::mpsc::UnboundedSender<PlayerEvent>>,
+ metadata_pipe: Option<String>,
}
enum PlayerCommand {
@@ -232,6 +236,7 @@ impl Player {
config: PlayerConfig,
session: Session,
audio_filter: Option<Box<dyn AudioFilter + Send>>,
+ metadata_pipe: Option<String>,
sink_builder: F,
) -> (Player, PlayerEventChannel)
where
@@ -254,6 +259,7 @@ impl Player {
sink_status: SinkStatus::Closed,
sink_event_callback: None,
audio_filter: audio_filter,
+ metadata_pipe: metadata_pipe,
event_senders: [event_sender].to_vec(),
};
@@ -566,6 +572,7 @@ impl PlayerState {
struct PlayerTrackLoader {
session: Session,
config: PlayerConfig,
+ metadata_pipe: Option<String>,
}
impl PlayerTrackLoader {
@@ -618,6 +625,40 @@ impl PlayerTrackLoader {
info!("Loading <{}> with Spotify URI <{}>", audio.name, audio.uri);
+ if let Some(path) = self.metadata_pipe.as_ref() {
+ let track = Track::get(&self.session, spotify_id).wait().unwrap();
+ let mut f = File::create(path).expect("Unable to open pipe");
+
+ // title
+ let title = track.name.clone();
+ let title_len = title.chars().count();
+ let title_string = base64::encode(&title);
+ let title_xml = format!("<item><type>636f7265</type><code>6d696e6d</code><length>{}</length>\n<data encoding=\"base64\">\n{}</data></item>", title_len, title_string);
+ f.write_all(title_xml.as_bytes()).expect("Unable to write title");
+
+ // album
+ let album = Album::get(&self.session, track.album).wait().unwrap();
+ let album_name = album.name.clone();
+ let album_name_len = album_name.chars().count();
+ let album_name_string = base64::encode(&album_name);
+ let album_name_xml = format!("<item><type>636f7265</type><code>6173616c</code><length>{}</length>\n<data encoding=\"base64\">\n{}</data></item>", album_name_len, album_name_string);
+ f.write_all(album_name_xml.as_bytes()).expect("Unable to write album");
+
+ // artist
+ let mut artists = String::new();
+ for id in &track.artists {
+ if artists != "" {
+ artists.push_str(" & ");
+ }
+ let artist = Artist::get(&self.session, *id).wait().unwrap();
+ artists.push_str(&artist.name);
+ }
+ let artists_len = artists.chars().count();
+ let artists_string = base64::encode(&artists);
+ let artists_xml = format!("<item><type>636f7265</type><code>61736172</code><length>{}</length>\n<data encoding=\"base64\">\n{}</data></item>", artists_len, artists_string);
+ f.write_all(artists_xml.as_bytes()).expect("Unable to write artists");
+ }
+
let audio = match self.find_available_alternative(&audio) {
Some(audio) => audio,
None => {
@@ -1541,6 +1582,7 @@ impl PlayerInternal {
let loader = PlayerTrackLoader {
session: self.session.clone(),
config: self.config.clone(),
+ metadata_pipe: self.metadata_pipe.clone(),
};
let (result_tx, result_rx) = futures::sync::oneshot::channel();
diff --git a/src/main.rs b/src/main.rs
index 7803a9f..ed38a07 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -75,6 +75,7 @@ fn list_backends() {
struct Setup {
backend: fn(Option<String>) -> Box<dyn Sink>,
device: Option<String>,
+ metadata_pipe: Option<String>,
mixer: fn(Option<MixerConfig>) -> Box<dyn Mixer>,
@@ -144,6 +145,7 @@ fn setup(args: &[String]) -> Setup {
"Alsa mixer card, e.g \"hw:0\" or similar from `aplay -l`. Defaults to 'default' ",
"MIXER_CARD",
)
+ .optopt("", "metadata-pipe", "Pipe to write metadata", "METADATA_PIPE")
.optopt(
"",
"mixer-index",
@@ -234,6 +236,8 @@ fn setup(args: &[String]) -> Setup {
exit(0);
}
+ let metadata_pipe = matches.opt_str("metadata-pipe");
+
let mixer_name = matches.opt_str("mixer");
let mixer = mixer::find(mixer_name.as_ref()).expect("Invalid mixer");
@@ -369,6 +373,7 @@ fn setup(args: &[String]) -> Setup {
connect_config: connect_config,
credentials: credentials,
device: device,
+ metadata_pipe: metadata_pipe,
enable_discovery: enable_discovery,
zeroconf_port: zeroconf_port,
mixer: mixer,
@@ -385,6 +390,7 @@ struct Main {
connect_config: ConnectConfig,
backend: fn(Option<String>) -> Box<dyn Sink>,
device: Option<String>,
+ metadata_pipe: Option<String>,
mixer: fn(Option<MixerConfig>) -> Box<dyn Mixer>,
mixer_config: MixerConfig,
handle: Handle,
@@ -415,6 +421,7 @@ impl Main {
connect_config: setup.connect_config,
backend: setup.backend,
device: setup.device,
+ metadata_pipe: setup.metadata_pipe,
mixer: setup.mixer,
mixer_config: setup.mixer_config,
@@ -487,17 +494,17 @@ impl Future for Main {
Ok(Async::Ready(session)) => {
self.connect = Box::new(futures::future::empty());
let mixer_config = self.mixer_config.clone();
- let mixer = (self.mixer)(Some(mixer_config));
+ let mut mixer = (self.mixer)(Some(mixer_config));
let player_config = self.player_config.clone();
let connect_config = self.connect_config.clone();
+ let metadata_pipe = self.metadata_pipe.clone();
+
+ mixer.set_metadata_pipe(metadata_pipe.clone());
let audio_filter = mixer.get_audio_filter();
let backend = self.backend;
let device = self.device.clone();
- let (player, event_channel) =
- Player::new(player_config, session.clone(), audio_filter, move || {
- (backend)(device)
- });
+ let (player, event_channel) = Player::new(player_config, session.clone(), audio_filter, metadata_pipe, move || (backend)(device));
if self.emit_sink_events {
if let Some(player_event_program) = &self.player_event_program {