spotify: move SetupThread to common

Will be used by the lyrics element.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1880>
This commit is contained in:
Guillaume Desmottes 2024-10-24 08:47:33 +02:00 committed by GStreamer Marge Bot
parent 997cd73a83
commit 3f7c5fbe1c
2 changed files with 32 additions and 30 deletions

View file

@ -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<std::thread::JoinHandle<Result<anyhow::Result<()>, 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;
}
}

View file

@ -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<gst::DebugCategory> = LazyLock::new(|| {
gst::DebugCategory::new(
@ -66,35 +67,6 @@ struct Settings {
bitrate: Bitrate,
}
#[derive(Default)]
enum SetupThread {
#[default]
None,
Pending {
thread_handle: Option<std::thread::JoinHandle<Result<anyhow::Result<()>, 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<SetupThread>,