ferrosonic 0.8.2

A terminal-based Subsonic music client with bit-perfect audio playback
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! MPRIS2 D-Bus server implementation

use mpris_server::{
    zbus::{fdo, Result},
    LoopStatus, Metadata, PlaybackRate, PlaybackStatus, PlayerInterface, Property, RootInterface,
    Server, Time, TrackId, Volume,
};
use tokio::sync::mpsc;
use tracing::info;
use url::Url;

use crate::app::actions::AudioAction;
use crate::app::state::{NowPlaying, PlaybackState, SharedState};
use crate::config::Config;
use crate::subsonic::auth::generate_auth_params;
use crate::subsonic::models::Child;

/// API version for Subsonic
const API_VERSION: &str = "1.16.1";
/// Client name for Subsonic
const CLIENT_NAME: &str = "ferrosonic";

/// Build a cover art URL from config and cover art ID
fn build_cover_art_url(config: &Config, cover_art_id: &str) -> Option<String> {
    if config.base_url.is_empty() || cover_art_id.is_empty() {
        return None;
    }

    let (salt, token) = generate_auth_params(&config.password);
    let mut url = Url::parse(&format!("{}/rest/getCoverArt", config.base_url)).ok()?;

    url.query_pairs_mut()
        .append_pair("id", cover_art_id)
        .append_pair("u", &config.username)
        .append_pair("t", &token)
        .append_pair("s", &salt)
        .append_pair("v", API_VERSION)
        .append_pair("c", CLIENT_NAME);

    Some(url.to_string())
}

fn mpris_path_segment(value: &str) -> String {
    value
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || ch == '_' {
                ch
            } else {
                '_'
            }
        })
        .collect()
}

fn build_metadata(
    now_playing: &NowPlaying,
    current_song: Option<Child>,
    config: &Config,
) -> Metadata {
    let mut metadata = Metadata::new();

    if let Some(song) = current_song {
        metadata.set_trackid(
            Some(TrackId::try_from(format!("/org/mpris/MediaPlayer2/Track/{}", song.id)).ok())
                .flatten(),
        );
        metadata.set_title(Some(song.title));
        metadata.set_artist(song.artist.map(|a| vec![a]));
        metadata.set_album(song.album);

        if let Some(duration) = song.duration {
            metadata.set_length(Some(Time::from_micros(duration as i64 * 1_000_000)));
        }

        if let Some(track) = song.track {
            metadata.set_track_number(Some(track));
        }

        if let Some(disc) = song.disc_number {
            metadata.set_disc_number(Some(disc));
        }

        if let Some(ref cover_art_id) = song.cover_art {
            if let Some(cover_url) = build_cover_art_url(config, cover_art_id) {
                metadata.set_art_url(Some(cover_url));
            }
        }
    } else if let Some(station) = now_playing.radio_station.as_ref() {
        metadata.set_trackid(
            Some(
                TrackId::try_from(format!(
                    "/org/mpris/MediaPlayer2/Radio/{}",
                    mpris_path_segment(&station.id)
                ))
                .ok(),
            )
            .flatten(),
        );
        metadata.set_title(Some(
            now_playing
                .radio_title
                .clone()
                .unwrap_or_else(|| station.name.clone()),
        ));
        metadata.set_artist(Some(vec![now_playing
            .radio_artist
            .clone()
            .unwrap_or_else(|| "Internet Radio".to_string())]));
        metadata.set_album(
            station
                .home_page_url
                .clone()
                .or_else(|| Some(station.name.clone())),
        );

        if let Some(ref cover_art_id) = station.cover_art {
            if let Some(cover_url) = build_cover_art_url(config, cover_art_id) {
                metadata.set_art_url(Some(cover_url));
            }
        }
    }

    metadata
}

/// MPRIS server instance name
const PLAYER_NAME: &str = "ferrosonic";

/// MPRIS2 player implementation
pub struct MprisPlayer {
    state: SharedState,
    audio_tx: mpsc::Sender<AudioAction>,
}

impl MprisPlayer {
    pub fn new(state: SharedState, audio_tx: mpsc::Sender<AudioAction>) -> Self {
        Self { state, audio_tx }
    }

    async fn get_state(&self) -> (NowPlaying, Option<Child>, Config) {
        let state = self.state.read().await;
        let now_playing = state.now_playing.clone();
        let current_song = state.current_song().cloned();
        let config = state.config.clone();
        (now_playing, current_song, config)
    }
}

impl RootInterface for MprisPlayer {
    async fn raise(&self) -> fdo::Result<()> {
        // We're a terminal app, can't raise
        Ok(())
    }

    async fn quit(&self) -> fdo::Result<()> {
        let mut state = self.state.write().await;
        state.should_quit = true;
        Ok(())
    }

    async fn can_quit(&self) -> fdo::Result<bool> {
        Ok(true)
    }

    async fn fullscreen(&self) -> fdo::Result<bool> {
        Ok(false)
    }

    async fn set_fullscreen(&self, _fullscreen: bool) -> Result<()> {
        Ok(())
    }

    async fn can_set_fullscreen(&self) -> fdo::Result<bool> {
        Ok(false)
    }

    async fn can_raise(&self) -> fdo::Result<bool> {
        Ok(false)
    }

    async fn has_track_list(&self) -> fdo::Result<bool> {
        Ok(false)
    }

    async fn identity(&self) -> fdo::Result<String> {
        Ok("Ferrosonic".to_string())
    }

    async fn desktop_entry(&self) -> fdo::Result<String> {
        Ok("ferrosonic".to_string())
    }

    async fn supported_uri_schemes(&self) -> fdo::Result<Vec<String>> {
        Ok(vec!["http".to_string(), "https".to_string()])
    }

    async fn supported_mime_types(&self) -> fdo::Result<Vec<String>> {
        Ok(vec![
            "audio/mpeg".to_string(),
            "audio/flac".to_string(),
            "audio/ogg".to_string(),
            "audio/wav".to_string(),
            "audio/x-wav".to_string(),
        ])
    }
}

impl PlayerInterface for MprisPlayer {
    async fn next(&self) -> fdo::Result<()> {
        let _ = self.audio_tx.send(AudioAction::Next).await;
        Ok(())
    }

    async fn previous(&self) -> fdo::Result<()> {
        let _ = self.audio_tx.send(AudioAction::Previous).await;
        Ok(())
    }

    async fn pause(&self) -> fdo::Result<()> {
        let _ = self.audio_tx.send(AudioAction::Pause).await;
        Ok(())
    }

    async fn play_pause(&self) -> fdo::Result<()> {
        let _ = self.audio_tx.send(AudioAction::TogglePause).await;
        Ok(())
    }

    async fn stop(&self) -> fdo::Result<()> {
        let _ = self.audio_tx.send(AudioAction::Stop).await;
        Ok(())
    }

    async fn play(&self) -> fdo::Result<()> {
        let _ = self.audio_tx.send(AudioAction::Resume).await;
        Ok(())
    }

    async fn seek(&self, offset: Time) -> fdo::Result<()> {
        let offset_secs = offset.as_micros() as f64 / 1_000_000.0;
        let _ = self
            .audio_tx
            .send(AudioAction::SeekRelative(offset_secs))
            .await;
        Ok(())
    }

    async fn set_position(&self, _track_id: TrackId, position: Time) -> fdo::Result<()> {
        let position_secs = position.as_micros() as f64 / 1_000_000.0;
        let _ = self.audio_tx.send(AudioAction::Seek(position_secs)).await;
        Ok(())
    }

    async fn open_uri(&self, _uri: String) -> fdo::Result<()> {
        // Not supported for now
        Ok(())
    }

    async fn playback_status(&self) -> fdo::Result<PlaybackStatus> {
        let (now_playing, _, _) = self.get_state().await;
        Ok(match now_playing.state {
            PlaybackState::Playing => PlaybackStatus::Playing,
            PlaybackState::Paused => PlaybackStatus::Paused,
            PlaybackState::Stopped => PlaybackStatus::Stopped,
        })
    }

    async fn loop_status(&self) -> fdo::Result<LoopStatus> {
        Ok(LoopStatus::None)
    }

    async fn set_loop_status(&self, _loop_status: LoopStatus) -> Result<()> {
        Ok(())
    }

    async fn rate(&self) -> fdo::Result<PlaybackRate> {
        Ok(1.0)
    }

    async fn set_rate(&self, _rate: PlaybackRate) -> Result<()> {
        Ok(())
    }

    async fn shuffle(&self) -> fdo::Result<bool> {
        Ok(false)
    }

    async fn set_shuffle(&self, _shuffle: bool) -> Result<()> {
        Ok(())
    }

    async fn metadata(&self) -> fdo::Result<Metadata> {
        let (now_playing, current_song, config) = self.get_state().await;
        Ok(build_metadata(&now_playing, current_song, &config))
    }

    async fn volume(&self) -> fdo::Result<Volume> {
        Ok(1.0)
    }

    async fn set_volume(&self, volume: Volume) -> Result<()> {
        let volume_int = (volume * 100.0) as i32;
        let _ = self.audio_tx.send(AudioAction::SetVolume(volume_int)).await;
        Ok(())
    }

    async fn position(&self) -> fdo::Result<Time> {
        let (now_playing, _, _) = self.get_state().await;
        Ok(Time::from_micros(
            (now_playing.position * 1_000_000.0) as i64,
        ))
    }

    async fn minimum_rate(&self) -> fdo::Result<PlaybackRate> {
        Ok(1.0)
    }

    async fn maximum_rate(&self) -> fdo::Result<PlaybackRate> {
        Ok(1.0)
    }

    async fn can_go_next(&self) -> fdo::Result<bool> {
        let state = self.state.read().await;
        Ok(state
            .queue_position
            .map(|p| p + 1 < state.queue.len())
            .unwrap_or(false))
    }

    async fn can_go_previous(&self) -> fdo::Result<bool> {
        let state = self.state.read().await;
        Ok(state.queue_position.map(|p| p > 0).unwrap_or(false))
    }

    async fn can_play(&self) -> fdo::Result<bool> {
        let state = self.state.read().await;
        Ok(!state.queue.is_empty() || state.now_playing.radio_station.is_some())
    }

    async fn can_pause(&self) -> fdo::Result<bool> {
        Ok(true)
    }

    async fn can_seek(&self) -> fdo::Result<bool> {
        let state = self.state.read().await;
        Ok(state.now_playing.radio_station.is_none())
    }

    async fn can_control(&self) -> fdo::Result<bool> {
        Ok(true)
    }
}

/// Start the MPRIS server
pub async fn start_mpris_server(
    state: SharedState,
    audio_tx: mpsc::Sender<AudioAction>,
) -> Result<Server<MprisPlayer>> {
    info!("Starting MPRIS2 server");

    let player = MprisPlayer::new(state, audio_tx);
    let server = Server::new(PLAYER_NAME, player).await?;

    info!(
        "MPRIS2 server started as org.mpris.MediaPlayer2.{}",
        PLAYER_NAME
    );
    Ok(server)
}

/// Update MPRIS properties when state changes
pub async fn update_mpris_properties(
    server: &Server<MprisPlayer>,
    state: &SharedState,
) -> Result<()> {
    let state = state.read().await;

    // Emit property changes
    server
        .properties_changed([
            Property::PlaybackStatus(match state.now_playing.state {
                PlaybackState::Playing => PlaybackStatus::Playing,
                PlaybackState::Paused => PlaybackStatus::Paused,
                PlaybackState::Stopped => PlaybackStatus::Stopped,
            }),
            Property::CanGoNext(
                state
                    .queue_position
                    .map(|p| p + 1 < state.queue.len())
                    .unwrap_or(false),
            ),
            Property::CanGoPrevious(state.queue_position.map(|p| p > 0).unwrap_or(false)),
            Property::CanPlay(!state.queue.is_empty() || state.now_playing.radio_station.is_some()),
            Property::CanSeek(state.now_playing.radio_station.is_none()),
        ])
        .await?;

    // Update metadata if we have a current song or radio station
    if state.current_song().is_some() || state.now_playing.radio_station.is_some() {
        let metadata = build_metadata(
            &state.now_playing,
            state.current_song().cloned(),
            &state.config,
        );
        server
            .properties_changed([Property::Metadata(metadata)])
            .await?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::state::new_shared_state;

    #[tokio::test]
    async fn identity_reports_ferrosonic_name() {
        let (audio_tx, _audio_rx) = mpsc::channel(1);
        let player = MprisPlayer::new(new_shared_state(Config::new()), audio_tx);

        assert_eq!(
            player
                .identity()
                .await
                .expect("identity should be available"),
            "Ferrosonic"
        );
    }
}