Skip to content

Commit

Permalink
Follow autoplay client setting
Browse files Browse the repository at this point in the history
  • Loading branch information
roderickvd committed Jan 1, 2022
1 parent 0fdff0d commit 2d699e2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
35 changes: 21 additions & 14 deletions connect/src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ type BoxedStream<T> = Pin<Box<dyn FusedStream<Item = T> + Send>>;
struct SpircTask {
player: Player,
mixer: Box<dyn Mixer>,
config: SpircTaskConfig,

sequence: SeqGenerator<u32>,

Expand Down Expand Up @@ -123,10 +122,6 @@ pub enum SpircCommand {
Shuffle,
}

struct SpircTaskConfig {
autoplay: bool,
}

const CONTEXT_TRACKS_HISTORY: usize = 10;
const CONTEXT_FETCH_THRESHOLD: u32 = 5;

Expand Down Expand Up @@ -337,9 +332,6 @@ impl Spirc {
let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();

let initial_volume = config.initial_volume;
let task_config = SpircTaskConfig {
autoplay: config.autoplay,
};

let device = initial_device_state(config);

Expand All @@ -348,7 +340,6 @@ impl Spirc {
let mut task = SpircTask {
player,
mixer,
config: task_config,

sequence: SeqGenerator::new(1),

Expand Down Expand Up @@ -1098,8 +1089,19 @@ impl SpircTask {
self.context_fut = self.resolve_station(&context_uri);
self.update_tracks_from_context();
}

if new_index >= tracks_len {
if self.config.autoplay {
let autoplay = self
.session
.get_user_attribute("autoplay")
.unwrap_or_else(|| {
warn!(
"Unable to get autoplay user attribute. Continuing with autoplay disabled."
);
"0".into()
});

if autoplay == "1" {
// Extend the playlist
debug!("Extending playlist <{}>", context_uri);
self.update_tracks_from_context();
Expand Down Expand Up @@ -1262,18 +1264,23 @@ impl SpircTask {

fn update_tracks(&mut self, frame: &protocol::spirc::Frame) {
trace!("State: {:#?}", frame.get_state());

let index = frame.get_state().get_playing_track_index();
let context_uri = frame.get_state().get_context_uri().to_owned();
let tracks = frame.get_state().get_track();

trace!("Frame has {:?} tracks", tracks.len());

if context_uri.starts_with("spotify:station:")
|| context_uri.starts_with("spotify:dailymix:")
{
self.context_fut = self.resolve_station(&context_uri);
} else if self.config.autoplay {
info!("Fetching autoplay context uri");
// Get autoplay_station_uri for regular playlists
self.autoplay_fut = self.resolve_autoplay_uri(&context_uri);
} else if let Some(autoplay) = self.session.get_user_attribute("autoplay") {
if &autoplay == "1" {
info!("Fetching autoplay context uri");
// Get autoplay_station_uri for regular playlists
self.autoplay_fut = self.resolve_autoplay_uri(&context_uri);
}
}

self.player
Expand Down
2 changes: 0 additions & 2 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ pub struct ConnectConfig {
pub device_type: DeviceType,
pub initial_volume: Option<u16>,
pub has_volume_ctrl: bool,
pub autoplay: bool,
}

impl Default for ConnectConfig {
Expand All @@ -133,7 +132,6 @@ impl Default for ConnectConfig {
device_type: DeviceType::default(),
initial_volume: Some(50),
has_volume_ctrl: true,
autoplay: false,
}
}
}
9 changes: 0 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ fn get_setup() -> Setup {
const VALID_NORMALISATION_RELEASE_RANGE: RangeInclusive<u64> = 1..=1000;

const AP_PORT: &str = "ap-port";
const AUTOPLAY: &str = "autoplay";
const BACKEND: &str = "backend";
const BITRATE: &str = "bitrate";
const CACHE: &str = "cache";
Expand Down Expand Up @@ -245,7 +244,6 @@ fn get_setup() -> Setup {
const ZEROCONF_PORT: &str = "zeroconf-port";

// Mostly arbitrary.
const AUTOPLAY_SHORT: &str = "A";
const AP_PORT_SHORT: &str = "a";
const BACKEND_SHORT: &str = "B";
const BITRATE_SHORT: &str = "b";
Expand Down Expand Up @@ -376,11 +374,6 @@ fn get_setup() -> Setup {
EMIT_SINK_EVENTS,
"Run PROGRAM set by `--onevent` before the sink is opened and after it is closed.",
)
.optflag(
AUTOPLAY_SHORT,
AUTOPLAY,
"Automatically play similar songs when your music ends.",
)
.optflag(
PASSTHROUGH_SHORT,
PASSTHROUGH,
Expand Down Expand Up @@ -1245,14 +1238,12 @@ fn get_setup() -> Setup {
.unwrap_or_default();

let has_volume_ctrl = !matches!(mixer_config.volume_ctrl, VolumeCtrl::Fixed);
let autoplay = opt_present(AUTOPLAY);

ConnectConfig {
name,
device_type,
initial_volume,
has_volume_ctrl,
autoplay,
}
};

Expand Down

0 comments on commit 2d699e2

Please sign in to comment.