spotifyaudiosrc: implement URI handler

Fix #204
This commit is contained in:
Guillaume Desmottes 2022-10-17 17:09:16 +02:00 committed by Guillaume Desmottes
parent e17688a2da
commit a5ebefd736
5 changed files with 54 additions and 1 deletions

View file

@ -16,6 +16,7 @@ librespot = { version = "0.4", default-features = false }
tokio = "1.0"
futures = "0.3"
anyhow = "1.0"
url = "2.3"
[lib]
name = "gstspotify"

View file

@ -21,4 +21,10 @@ The `spotifyaudiosrc` element can be used to play a song from Spotify using its
```
gst-launch-1.0 spotifyaudiosrc username=$USERNAME password=$PASSWORD track=spotify:track:3i3P1mGpV9eRlfKccjDjwi ! oggdemux ! vorbisdec ! audioconvert ! autoaudiosink
```
The element also implements an URI handler which accepts credentials and cache settings as URI parameters:
```console
gst-launch-1.0 playbin3 uri=spotify:track:3i3P1mGpV9eRlfKccjDjwi?username=$USERNAME\&password=$PASSWORD\&cache-credentials=cache\&cache-files=cache
```

View file

@ -83,6 +83,7 @@ impl ObjectSubclass for SpotifyAudioSrc {
const NAME: &'static str = "GstSpotifyAudioSrc";
type Type = super::SpotifyAudioSrc;
type ParentType = gst_base::BaseSrc;
type Interfaces = (gst::URIHandler,);
}
impl ObjectImpl for SpotifyAudioSrc {
@ -413,3 +414,45 @@ impl Sink for BufferSink {
Ok(())
}
}
impl URIHandlerImpl for SpotifyAudioSrc {
const URI_TYPE: gst::URIType = gst::URIType::Src;
fn protocols() -> &'static [&'static str] {
&["spotify"]
}
fn uri(&self) -> Option<String> {
let settings = self.settings.lock().unwrap();
if settings.track.is_empty() {
None
} else {
Some(settings.track.clone())
}
}
fn set_uri(&self, uri: &str) -> Result<(), glib::Error> {
gst::debug!(CAT, imp: self, "set URI: {}", uri);
let url = url::Url::parse(uri)
.map_err(|e| glib::Error::new(gst::URIError::BadUri, &format!("{:?}", e)))?;
// allow to configure auth and cache settings from the URI
for (key, value) in url.query_pairs() {
match key.as_ref() {
"username" | "password" | "cache-credentials" | "cache-files" => {
self.instance().set_property(&key, value.as_ref());
}
_ => {
gst::warning!(CAT, imp: self, "unsupported query: {}={}", key, value);
}
}
}
self.instance()
.set_property("track", format!("{}:{}", url.scheme(), url.path()));
Ok(())
}
}

View file

@ -12,7 +12,7 @@ use gst::prelude::*;
mod imp;
glib::wrapper! {
pub struct SpotifyAudioSrc(ObjectSubclass<imp::SpotifyAudioSrc>) @extends gst_base::BaseSrc, gst::Element, gst::Object;
pub struct SpotifyAudioSrc(ObjectSubclass<imp::SpotifyAudioSrc>) @extends gst_base::BaseSrc, gst::Element, gst::Object, @implements gst::URIHandler;
}
pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {

View file

@ -5565,6 +5565,9 @@
"GInitiallyUnowned",
"GObject"
],
"interfaces": [
"GstURIHandler"
],
"klass": "Source/Audio",
"long-name": "Spotify source",
"pad-templates": {