From 3c31c98d9536fe32cf56c7665cb75abdba1de252 Mon Sep 17 00:00:00 2001 From: Simon Himmelbauer Date: Thu, 2 Feb 2023 12:05:59 +0100 Subject: [PATCH] spotifyaudiosrc: Support configurable bitrate Part-of: --- audio/spotify/src/spotifyaudiosrc/imp.rs | 24 ++++++++++++++-- audio/spotify/src/spotifyaudiosrc/mod.rs | 31 +++++++++++++++++++++ docs/plugins/gst_plugins_cache.json | 35 +++++++++++++++++++++++- 3 files changed, 87 insertions(+), 3 deletions(-) diff --git a/audio/spotify/src/spotifyaudiosrc/imp.rs b/audio/spotify/src/spotifyaudiosrc/imp.rs index 58f30edd..05f34243 100644 --- a/audio/spotify/src/spotifyaudiosrc/imp.rs +++ b/audio/spotify/src/spotifyaudiosrc/imp.rs @@ -31,6 +31,8 @@ use librespot::playback::{ player::{Player, PlayerEvent}, }; +use super::Bitrate; + static CAT: Lazy = Lazy::new(|| { gst::DebugCategory::new( "spotifyaudiosrc", @@ -71,6 +73,7 @@ struct Settings { cache_files: String, cache_max_size: u64, track: String, + bitrate: Bitrate, } #[derive(Default)] @@ -132,6 +135,11 @@ impl ObjectImpl for SpotifyAudioSrc { .default_value(Some("")) .mutable_ready() .build(), + glib::ParamSpecEnum::builder::("bitrate") + .nick("Spotify bitrate") + .blurb("Spotify audio bitrate in kbit/s") + .mutable_ready() + .build() ] }); @@ -164,6 +172,10 @@ impl ObjectImpl for SpotifyAudioSrc { let mut settings = self.settings.lock().unwrap(); settings.track = value.get().expect("type checked upstream"); } + "bitrate" => { + let mut settings = self.settings.lock().unwrap(); + settings.bitrate = value.get().expect("type checked upstream"); + } _ => unimplemented!(), } } @@ -194,6 +206,10 @@ impl ObjectImpl for SpotifyAudioSrc { let settings = self.settings.lock().unwrap(); settings.track.to_value() } + "bitrate" => { + let settings = self.settings.lock().unwrap(); + settings.bitrate.to_value() + } _ => unimplemented!(), } } @@ -434,7 +450,7 @@ impl SpotifyAudioSrc { } } - let (credentials, cache, track) = { + let (credentials, cache, track, bitrate) = { let settings = self.settings.lock().unwrap(); let credentials_cache = if settings.cache_credentials.is_empty() { @@ -482,7 +498,10 @@ impl SpotifyAudioSrc { bail!("track is not set") } - (credentials, cache, settings.track.clone()) + let bitrate = settings.bitrate.into(); + gst::debug!(CAT, imp: self, "Requesting bitrate {:?}", bitrate); + + (credentials, cache, settings.track.clone(), bitrate) }; let (session, _credentials) = @@ -490,6 +509,7 @@ impl SpotifyAudioSrc { let player_config = PlayerConfig { passthrough: true, + bitrate, ..Default::default() }; diff --git a/audio/spotify/src/spotifyaudiosrc/mod.rs b/audio/spotify/src/spotifyaudiosrc/mod.rs index a33988f3..183110c7 100644 --- a/audio/spotify/src/spotifyaudiosrc/mod.rs +++ b/audio/spotify/src/spotifyaudiosrc/mod.rs @@ -11,11 +11,42 @@ use gst::prelude::*; mod imp; +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy, glib::Enum)] +#[repr(u32)] +#[enum_type(name = "GstRsSpotifyBitrate")] +enum Bitrate { + #[enum_value(name = "96 kbit/s", nick = "96")] + B96, + #[enum_value(name = "160 kbit/s", nick = "160")] + B160, + #[enum_value(name = "320 kbit/s", nick = "320")] + B320, +} + +impl Default for Bitrate { + fn default() -> Self { + Self::B160 + } +} + +impl From for librespot::playback::config::Bitrate { + fn from(value: Bitrate) -> Self { + match value { + Bitrate::B96 => Self::Bitrate96, + Bitrate::B160 => Self::Bitrate160, + Bitrate::B320 => Self::Bitrate320, + } + } +} + glib::wrapper! { pub struct SpotifyAudioSrc(ObjectSubclass) @extends gst_base::PushSrc, gst_base::BaseSrc, gst::Element, gst::Object, @implements gst::URIHandler; } pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> { + #[cfg(feature = "doc")] + Bitrate::static_type().mark_as_plugin_api(gst::PluginAPIFlags::empty()); + gst::Element::register( Some(plugin), "spotifyaudiosrc", diff --git a/docs/plugins/gst_plugins_cache.json b/docs/plugins/gst_plugins_cache.json index 1260714d..7127d9da 100644 --- a/docs/plugins/gst_plugins_cache.json +++ b/docs/plugins/gst_plugins_cache.json @@ -6311,6 +6311,18 @@ } }, "properties": { + "bitrate": { + "blurb": "Spotify audio bitrate in kbit/s", + "conditionally-available": false, + "construct": false, + "construct-only": false, + "controllable": false, + "default": "160 (1)", + "mutable": "ready", + "readable": true, + "type": "GstRsSpotifyBitrate", + "writable": true + }, "cache-credentials": { "blurb": "Directory where to cache Spotify credentials", "conditionally-available": false, @@ -6391,7 +6403,28 @@ }, "filename": "gstspotify", "license": "MPL", - "other-types": {}, + "other-types": { + "GstRsSpotifyBitrate": { + "kind": "enum", + "values": [ + { + "desc": "96 kbit/s", + "name": "96", + "value": "0" + }, + { + "desc": "160 kbit/s", + "name": "160", + "value": "1" + }, + { + "desc": "320 kbit/s", + "name": "320", + "value": "2" + } + ] + } + }, "package": "gst-plugin-spotify", "source": "gst-plugin-spotify", "tracers": {},