diff --git a/audio/spotify/src/common.rs b/audio/spotify/src/common.rs index a5764ef8..821ab295 100644 --- a/audio/spotify/src/common.rs +++ b/audio/spotify/src/common.rs @@ -11,6 +11,7 @@ use anyhow::bail; use gst::glib; use gst::prelude::*; +use futures::future::{AbortHandle, Aborted}; use librespot_core::{ authentication::Credentials, cache::Cache, config::SessionConfig, session::Session, spotify_id::SpotifyId, @@ -167,3 +168,32 @@ impl Settings { Ok(track) } } + +#[derive(Default)] +pub enum SetupThread { + #[default] + None, + Pending { + thread_handle: Option, Aborted>>>, + abort_handle: AbortHandle, + }, + Cancelled, + Done, +} + +impl SetupThread { + pub fn abort(&mut self) { + // Cancel setup thread if it is pending and not done yet + if matches!(self, SetupThread::None | SetupThread::Done) { + return; + } + + if let SetupThread::Pending { + ref abort_handle, .. + } = *self + { + abort_handle.abort(); + } + *self = SetupThread::Cancelled; + } +} diff --git a/audio/spotify/src/spotifyaudiosrc/imp.rs b/audio/spotify/src/spotifyaudiosrc/imp.rs index 2159e154..4ab04a60 100644 --- a/audio/spotify/src/spotifyaudiosrc/imp.rs +++ b/audio/spotify/src/spotifyaudiosrc/imp.rs @@ -8,7 +8,7 @@ use std::sync::{mpsc, Arc, Mutex}; -use futures::future::{AbortHandle, Abortable, Aborted}; +use futures::future::{AbortHandle, Abortable}; use std::sync::LazyLock; use tokio::{runtime, task::JoinHandle}; @@ -27,6 +27,7 @@ use librespot_playback::{ }; use super::Bitrate; +use crate::common::SetupThread; static CAT: LazyLock = LazyLock::new(|| { gst::DebugCategory::new( @@ -66,35 +67,6 @@ struct Settings { bitrate: Bitrate, } -#[derive(Default)] -enum SetupThread { - #[default] - None, - Pending { - thread_handle: Option, Aborted>>>, - abort_handle: AbortHandle, - }, - Cancelled, - Done, -} - -impl SetupThread { - fn abort(&mut self) { - // Cancel setup thread if it is pending and not done yet - if matches!(self, SetupThread::None | SetupThread::Done) { - return; - } - - if let SetupThread::Pending { - ref abort_handle, .. - } = *self - { - abort_handle.abort(); - } - *self = SetupThread::Cancelled; - } -} - #[derive(Default)] pub struct SpotifyAudioSrc { setup_thread: Mutex,