mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-03 09:49:31 +02:00
Spirc: Replace Mecury with Dealer (#1356)
This was a huge effort by photovoltex@gmail.com with help from the community. Over 140 commits were squashed. Below, their commit messages are kept unchanged. --- * dealer wrapper for ease of use * improve sending protobuf requests * replace connect config with connect_state config * start integrating dealer into spirc * payload handling, gzip support * put connect state consistent * formatting * request payload handling, gzip support * expose dealer::protocol, move request in own file * integrate handle of connect-state commands * spirc: remove ident field * transfer playing state better * spirc: remove remote_update stream * spirc: replace command sender with connect state update * spirc: remove device state and remaining unused methods * spirc: remove mercury sender * add repeat track state * ConnectState: add methods to replace state in spirc * spirc: move context into connect_state, update load and next * spirc: remove state, adjust remaining methods * spirc: handle more dealer request commands * revert rustfmt.toml * spirc: impl shuffle - impl shuffle again - extracted fill up of next tracks in own method - moved queue revision update into next track fill up - removed unused method `set_playing_track_index` - added option to specify index when resetting the playback context - reshuffle after repeat context * spirc: handle device became inactive * dealer: adjust payload handling * spirc: better set volume handling * dealer: box PlayCommand (clippy warning) * dealer: always respect queued tracks * spirc: update duration of track * ConnectState: update more restrictions * cleanup * spirc: handle queue requests * spirc: skip next with track * proto: exclude spirc.proto - move "deserialize_with" functions into own file - replace TrackRef with ProvidedTrack * spirc: stabilize transfer/context handling * core: cleanup some remains * connect: improvements to code structure and performance - use VecDeque for next and prev tracks * connect: delayed volume update * connect: move context resolve into own function * connect: load context asynchronous * connect: handle reconnect - might currently steal the active devices playback * connect: some fixes and adjustments - fix wrong offset when transferring playback - fix missing displayed context in web-player - remove access_token from log - send correct state reason when updating volume - queue track correctly - fix wrong assumption for skip_to * connect: replace error case with option * connect: use own context state * connect: more stabilising - handle SkipTo having no Index - handle no transferred restrictions - handle no transferred index - update state before shutdown, for smoother reacquiring * connect: working autoplay * connect: handle repeat context/track * connect: some quick fixes - found self-named uid in collection after reconnecting * connect: handle add_to_queue via set_queue * fix clippy warnings * fix check errors, fix/update example * fix 1.75 specific error * connect: position update improvements * connect: handle unavailable * connect: fix incorrect status handling for desktop and mobile * core: fix dealer reconnect - actually acquire new token - use login5 token retrieval * connect: split state into multiple files * connect: encapsulate provider logic * connect: remove public access to next and prev tracks * connect: remove public access to player * connect: move state only commands into own file * connect: improve logging * connect: handle transferred queue again * connect: fix all-features specific error * connect: extract transfer handling into own file * connect: remove old context model * connect: handle more transfer cases correctly * connect: do auth_token pre-acquiring earlier * connect: handle play with skip_to by uid * connect: simplified cluster update log * core/connect: add remaining set value commands * connect: position update workaround/fix * connect: some queue cleanups * connect: add uid to queue * connect: duration as volume delay const * connect: some adjustments and todo cleanups - send volume update before general update - simplify queue revision to use the track uri - argument why copying the prev/next tracks is fine * connect: handle shuffle from set_options * connect: handle context update * connect: move other structs into model.rs * connect: reduce SpircCommand visibility * connect: fix visibility of model * connect: fix: shuffle on startup isn't applied * connect: prevent loading a context with no tracks * connect: use the first page of a context * connect: improve context resolving - support multiple pages - support page_url of context - handle single track * connect: prevent integer underflow * connect: rename method for better clarity * connect: handle mutate and update messages * connect: fix 1.75 problems * connect: fill, instead of replace next page * connect: reduce context update to single method * connect: remove unused SpircError, handle local files * connect: reduce nesting, adjust initial transfer handling * connect: don't update volume initially * core: disable trace logging of handled mercury responses * core/connect: prevent takeover from other clients, handle session-update * connect: add queue-uid for set_queue command * connect: adjust fields for PlayCommand * connect: preserve context position after update_context * connect: unify metadata modification - only handle `is_queued` `true` items for queue * connect: polish request command handling - reply to all request endpoints - adjust some naming - add some docs * connect: add uid to tracks without * connect: simpler update of current index * core/connect: update log msg, fix wrong behavior - handle became inactive separately - remove duplicate stop - adjust docs for websocket request * core: add option to request without metrics and salt * core/context: adjust context requests and update - search should now return the expected context - removed workaround for single track playback - move local playback check into update_context - check track uri for invalid characters - early return with `?` * connect: handle possible search context uri * connect: remove logout support - handle logout command - disable support for logout - add todos for logout * connect: adjust detailed tracks/context handling - always allow next - handle no prev track available - separate active and fill up context * connect: adjust context resolve handling, again * connect: add autoplay metadata to tracks - transfer into autoplay again * core/connect: cleanup session after spirc stops * update CHANGELOG.md * playback: fix clippy warnings * connect: adjust metadata - unify naming - move more metadata infos into metadata.rs * connect: add delimiter between context and autoplay playback * connect: stop and resume correctly * connect: adjust context resolving - improved certain logging parts - preload autoplay when autoplay attribute mutates - fix transfer context uri - fix typo - handle empty strings for resolve uri - fix unexpected stop of playback * connect: ignore failure during stop * connect: revert resolve_uri changes * connect: correct context reset * connect: reduce boiler code * connect: fix some incorrect states - uid getting replaced by empty value - shuffle/repeat clearing autoplay context - fill_up updating and using incorrect index * core: adjust incorrect separator * connect: move `add_to_queue` and `mark_unavailable` into tracks.rs * connect: refactor - directly modify PutStateRequest - replace `next_tracks`, `prev_tracks`, `player` and `device` with `request` - provide helper methods for the removed fields * connect: adjust handling of context metadata/restrictions * connect: fix incorrect context states * connect: become inactive when no cluster is reported * update CHANGELOG.md * core/playback: preemptively fix clippy warnings * connect: minor adjustment to session changed * connect: change return type changing active context * connect: handle unavailable contexts * connect: fix previous restrictions blocking load with shuffle * connect: update comments and logging * core/connect: reduce some more duplicate code * more docs around the dealer
This commit is contained in:
parent
f646ef2b5a
commit
5839b36192
43 changed files with 4229 additions and 1283 deletions
415
connect/src/state/context.rs
Normal file
415
connect/src/state/context.rs
Normal file
|
@ -0,0 +1,415 @@
|
|||
use crate::state::{metadata::Metadata, provider::Provider, ConnectState, StateError};
|
||||
use librespot_core::{Error, SpotifyId};
|
||||
use librespot_protocol::player::{
|
||||
Context, ContextIndex, ContextPage, ContextTrack, ProvidedTrack, Restrictions,
|
||||
};
|
||||
use protobuf::MessageField;
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
const LOCAL_FILES_IDENTIFIER: &str = "spotify:local-files";
|
||||
const SEARCH_IDENTIFIER: &str = "spotify:search";
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StateContext {
|
||||
pub tracks: Vec<ProvidedTrack>,
|
||||
pub metadata: HashMap<String, String>,
|
||||
pub restrictions: Option<Restrictions>,
|
||||
/// is used to keep track which tracks are already loaded into the next_tracks
|
||||
pub index: ContextIndex,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Copy, Clone)]
|
||||
pub enum ContextType {
|
||||
#[default]
|
||||
Default,
|
||||
Shuffle,
|
||||
Autoplay,
|
||||
}
|
||||
|
||||
pub enum LoadNext {
|
||||
Done,
|
||||
PageUrl(String),
|
||||
Empty,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum UpdateContext {
|
||||
Default,
|
||||
Autoplay,
|
||||
}
|
||||
|
||||
pub enum ResetContext<'s> {
|
||||
Completely,
|
||||
DefaultIndex,
|
||||
WhenDifferent(&'s str),
|
||||
}
|
||||
|
||||
impl ConnectState {
|
||||
pub fn find_index_in_context<F: Fn(&ProvidedTrack) -> bool>(
|
||||
context: Option<&StateContext>,
|
||||
f: F,
|
||||
) -> Result<usize, StateError> {
|
||||
let ctx = context
|
||||
.as_ref()
|
||||
.ok_or(StateError::NoContext(ContextType::Default))?;
|
||||
|
||||
ctx.tracks
|
||||
.iter()
|
||||
.position(f)
|
||||
.ok_or(StateError::CanNotFindTrackInContext(None, ctx.tracks.len()))
|
||||
}
|
||||
|
||||
pub(super) fn get_context(&self, ty: &ContextType) -> Result<&StateContext, StateError> {
|
||||
match ty {
|
||||
ContextType::Default => self.context.as_ref(),
|
||||
ContextType::Shuffle => self.shuffle_context.as_ref(),
|
||||
ContextType::Autoplay => self.autoplay_context.as_ref(),
|
||||
}
|
||||
.ok_or(StateError::NoContext(*ty))
|
||||
}
|
||||
|
||||
pub fn context_uri(&self) -> &String {
|
||||
&self.player().context_uri
|
||||
}
|
||||
|
||||
pub fn reset_context(&mut self, mut reset_as: ResetContext) {
|
||||
self.set_active_context(ContextType::Default);
|
||||
self.fill_up_context = ContextType::Default;
|
||||
|
||||
if matches!(reset_as, ResetContext::WhenDifferent(ctx) if self.context_uri() != ctx) {
|
||||
reset_as = ResetContext::Completely
|
||||
}
|
||||
self.shuffle_context = None;
|
||||
|
||||
match reset_as {
|
||||
ResetContext::Completely => {
|
||||
self.context = None;
|
||||
self.autoplay_context = None;
|
||||
self.next_contexts.clear();
|
||||
}
|
||||
ResetContext::WhenDifferent(_) => debug!("context didn't change, no reset"),
|
||||
ResetContext::DefaultIndex => {
|
||||
for ctx in [self.context.as_mut(), self.autoplay_context.as_mut()]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
ctx.index.track = 0;
|
||||
ctx.index.page = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.update_restrictions()
|
||||
}
|
||||
|
||||
pub fn get_context_uri_from_context(context: &Context) -> Option<&String> {
|
||||
if !context.uri.starts_with(SEARCH_IDENTIFIER) {
|
||||
return Some(&context.uri);
|
||||
}
|
||||
|
||||
context
|
||||
.pages
|
||||
.first()
|
||||
.and_then(|p| p.tracks.first().map(|t| &t.uri))
|
||||
}
|
||||
|
||||
pub fn set_active_context(&mut self, new_context: ContextType) {
|
||||
self.active_context = new_context;
|
||||
|
||||
let ctx = match self.get_context(&new_context) {
|
||||
Err(why) => {
|
||||
debug!("couldn't load context info because: {why}");
|
||||
return;
|
||||
}
|
||||
Ok(ctx) => ctx,
|
||||
};
|
||||
|
||||
let mut restrictions = ctx.restrictions.clone();
|
||||
let metadata = ctx.metadata.clone();
|
||||
|
||||
let player = self.player_mut();
|
||||
|
||||
player.context_metadata.clear();
|
||||
player.restrictions.clear();
|
||||
|
||||
if let Some(restrictions) = restrictions.take() {
|
||||
player.restrictions = MessageField::some(restrictions);
|
||||
}
|
||||
|
||||
for (key, value) in metadata {
|
||||
player.context_metadata.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_context(&mut self, mut context: Context, ty: UpdateContext) -> Result<(), Error> {
|
||||
if context.pages.iter().all(|p| p.tracks.is_empty()) {
|
||||
error!("context didn't have any tracks: {context:#?}");
|
||||
return Err(StateError::ContextHasNoTracks.into());
|
||||
} else if context.uri.starts_with(LOCAL_FILES_IDENTIFIER) {
|
||||
return Err(StateError::UnsupportedLocalPlayBack.into());
|
||||
}
|
||||
|
||||
if matches!(ty, UpdateContext::Default) {
|
||||
self.next_contexts.clear();
|
||||
}
|
||||
|
||||
let mut first_page = None;
|
||||
for page in context.pages {
|
||||
if first_page.is_none() && !page.tracks.is_empty() {
|
||||
first_page = Some(page);
|
||||
} else {
|
||||
self.next_contexts.push(page)
|
||||
}
|
||||
}
|
||||
|
||||
let page = match first_page {
|
||||
None => Err(StateError::ContextHasNoTracks)?,
|
||||
Some(p) => p,
|
||||
};
|
||||
|
||||
let prev_context = match ty {
|
||||
UpdateContext::Default => self.context.as_ref(),
|
||||
UpdateContext::Autoplay => self.autoplay_context.as_ref(),
|
||||
};
|
||||
|
||||
debug!(
|
||||
"updated context {ty:?} from <{}> ({} tracks) to <{}> ({} tracks)",
|
||||
self.context_uri(),
|
||||
prev_context
|
||||
.map(|c| c.tracks.len().to_string())
|
||||
.unwrap_or_else(|| "-".to_string()),
|
||||
context.uri,
|
||||
page.tracks.len()
|
||||
);
|
||||
|
||||
match ty {
|
||||
UpdateContext::Default => {
|
||||
let mut new_context = self.state_context_from_page(
|
||||
page,
|
||||
context.restrictions.take(),
|
||||
Some(&context.uri),
|
||||
None,
|
||||
);
|
||||
|
||||
// when we update the same context, we should try to preserve the previous position
|
||||
// otherwise we might load the entire context twice
|
||||
if !self.context_uri().contains(SEARCH_IDENTIFIER)
|
||||
&& self.context_uri() == &context.uri
|
||||
{
|
||||
match Self::find_index_in_context(Some(&new_context), |t| {
|
||||
self.current_track(|t| &t.uri) == &t.uri
|
||||
}) {
|
||||
Ok(new_pos) => {
|
||||
debug!("found new index of current track, updating new_context index to {new_pos}");
|
||||
new_context.index.track = (new_pos + 1) as u32;
|
||||
}
|
||||
// the track isn't anymore in the context
|
||||
Err(_) if matches!(self.active_context, ContextType::Default) => {
|
||||
warn!("current track was removed, setting pos to last known index");
|
||||
new_context.index.track = self.player().index.track
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
// enforce reloading the context
|
||||
self.clear_next_tracks(true);
|
||||
}
|
||||
|
||||
self.context = Some(new_context);
|
||||
|
||||
if !context.url.contains(SEARCH_IDENTIFIER) {
|
||||
self.player_mut().context_url = context.url;
|
||||
} else {
|
||||
self.player_mut().context_url.clear()
|
||||
}
|
||||
self.player_mut().context_uri = context.uri;
|
||||
}
|
||||
UpdateContext::Autoplay => {
|
||||
self.autoplay_context = Some(self.state_context_from_page(
|
||||
page,
|
||||
context.restrictions.take(),
|
||||
Some(&context.uri),
|
||||
Some(Provider::Autoplay),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn state_context_from_page(
|
||||
&mut self,
|
||||
page: ContextPage,
|
||||
restrictions: Option<Restrictions>,
|
||||
new_context_uri: Option<&str>,
|
||||
provider: Option<Provider>,
|
||||
) -> StateContext {
|
||||
let new_context_uri = new_context_uri.unwrap_or(self.context_uri());
|
||||
|
||||
let tracks = page
|
||||
.tracks
|
||||
.iter()
|
||||
.flat_map(|track| {
|
||||
match self.context_to_provided_track(track, Some(new_context_uri), provider.clone())
|
||||
{
|
||||
Ok(t) => Some(t),
|
||||
Err(why) => {
|
||||
error!("couldn't convert {track:#?} into ProvidedTrack: {why}");
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
StateContext {
|
||||
tracks,
|
||||
restrictions,
|
||||
metadata: page.metadata,
|
||||
index: ContextIndex::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn merge_context(&mut self, context: Option<Context>) -> Option<()> {
|
||||
let mut context = context?;
|
||||
if self.context_uri() != &context.uri {
|
||||
return None;
|
||||
}
|
||||
|
||||
let current_context = self.context.as_mut()?;
|
||||
let new_page = context.pages.pop()?;
|
||||
|
||||
for new_track in new_page.tracks {
|
||||
if new_track.uri.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Ok(position) =
|
||||
Self::find_index_in_context(Some(current_context), |t| t.uri == new_track.uri)
|
||||
{
|
||||
let context_track = current_context.tracks.get_mut(position)?;
|
||||
|
||||
for (key, value) in new_track.metadata {
|
||||
warn!("merging metadata {key} {value}");
|
||||
context_track.metadata.insert(key, value);
|
||||
}
|
||||
|
||||
// the uid provided from another context might be actual uid of an item
|
||||
if !new_track.uid.is_empty() {
|
||||
context_track.uid = new_track.uid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(())
|
||||
}
|
||||
|
||||
pub(super) fn update_context_index(
|
||||
&mut self,
|
||||
ty: ContextType,
|
||||
new_index: usize,
|
||||
) -> Result<(), StateError> {
|
||||
let context = match ty {
|
||||
ContextType::Default => self.context.as_mut(),
|
||||
ContextType::Shuffle => self.shuffle_context.as_mut(),
|
||||
ContextType::Autoplay => self.autoplay_context.as_mut(),
|
||||
}
|
||||
.ok_or(StateError::NoContext(ty))?;
|
||||
|
||||
context.index.track = new_index as u32;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn context_to_provided_track(
|
||||
&self,
|
||||
ctx_track: &ContextTrack,
|
||||
context_uri: Option<&str>,
|
||||
provider: Option<Provider>,
|
||||
) -> Result<ProvidedTrack, Error> {
|
||||
let id = if !ctx_track.uri.is_empty() {
|
||||
if ctx_track.uri.contains(['?', '%']) {
|
||||
Err(StateError::InvalidTrackUri(ctx_track.uri.clone()))?
|
||||
}
|
||||
|
||||
SpotifyId::from_uri(&ctx_track.uri)?
|
||||
} else if !ctx_track.gid.is_empty() {
|
||||
SpotifyId::from_raw(&ctx_track.gid)?
|
||||
} else {
|
||||
Err(StateError::InvalidTrackUri(String::new()))?
|
||||
};
|
||||
|
||||
let provider = if self.unavailable_uri.contains(&ctx_track.uri) {
|
||||
Provider::Unavailable
|
||||
} else {
|
||||
provider.unwrap_or(Provider::Context)
|
||||
};
|
||||
|
||||
// assumption: the uid is used as unique-id of any item
|
||||
// - queue resorting is done by each client and orients itself by the given uid
|
||||
// - if no uid is present, resorting doesn't work or behaves not as intended
|
||||
let uid = if ctx_track.uid.is_empty() {
|
||||
// so setting providing a unique id should allow to resort the queue
|
||||
Uuid::new_v4().as_simple().to_string()
|
||||
} else {
|
||||
ctx_track.uid.to_string()
|
||||
};
|
||||
|
||||
let mut metadata = HashMap::new();
|
||||
for (k, v) in &ctx_track.metadata {
|
||||
metadata.insert(k.to_string(), v.to_string());
|
||||
}
|
||||
|
||||
let mut track = ProvidedTrack {
|
||||
uri: id.to_uri()?.replace("unknown", "track"),
|
||||
uid,
|
||||
metadata,
|
||||
provider: provider.to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(context_uri) = context_uri {
|
||||
track.set_context_uri(context_uri.to_string());
|
||||
track.set_entity_uri(context_uri.to_string());
|
||||
}
|
||||
|
||||
if matches!(provider, Provider::Autoplay) {
|
||||
track.set_autoplay(true)
|
||||
}
|
||||
|
||||
Ok(track)
|
||||
}
|
||||
|
||||
pub fn fill_context_from_page(&mut self, page: ContextPage) -> Result<(), Error> {
|
||||
let context = self.state_context_from_page(page, None, None, None);
|
||||
let ctx = self
|
||||
.context
|
||||
.as_mut()
|
||||
.ok_or(StateError::NoContext(ContextType::Default))?;
|
||||
|
||||
for t in context.tracks {
|
||||
ctx.tracks.push(t)
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn try_load_next_context(&mut self) -> Result<LoadNext, Error> {
|
||||
let next = match self.next_contexts.first() {
|
||||
None => return Ok(LoadNext::Empty),
|
||||
Some(_) => self.next_contexts.remove(0),
|
||||
};
|
||||
|
||||
if next.tracks.is_empty() {
|
||||
if next.page_url.is_empty() {
|
||||
Err(StateError::NoContext(ContextType::Default))?
|
||||
}
|
||||
|
||||
self.update_current_index(|i| i.page += 1);
|
||||
return Ok(LoadNext::PageUrl(next.page_url));
|
||||
}
|
||||
|
||||
self.fill_context_from_page(next)?;
|
||||
self.fill_up_next_tracks()?;
|
||||
|
||||
Ok(LoadNext::Done)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue