Skip to content

Instantly share code, notes, and snippets.

@kingosticks
Last active May 30, 2022 12:33
Show Gist options
  • Save kingosticks/f51066fdcd26cd4e7f342d807ad6cff4 to your computer and use it in GitHub Desktop.
Save kingosticks/f51066fdcd26cd4e7f342d807ad6cff4 to your computer and use it in GitHub Desktop.
Using source-setup with spotifyaudiosrc
use gst::glib;
use gst::prelude::*;
use gst::gst_info;
use anyhow::{Error};
use std::env;
use once_cell::sync::Lazy;
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
gst::DebugCategory::new(
"spotifyplayer",
gst::DebugColorFlags::empty(),
Some("Spotify audio source"),
)
});
fn main() -> Result<(), Error> {
gst::init()?;
gstspotify::plugin_register_static()?;
let args: Vec<_> = env::args().collect();
let uri: &str = if args.len() == 2 {
args[1].as_ref()
} else {
println!("Usage: playbin uri");
std::process::exit(-1)
};
let playbin = gst::ElementFactory::make("playbin", None)
.expect("Failed to create GStreamer playbin element");
playbin.set_property("uri", uri);
playbin.connect_closure(
"source-setup",
false,
glib::closure!(|_playbin: &gst::Pipeline, source: &gst::Element| {
gst_info!(CAT, obj: source, "Hello from source-setup");
let username = std::env::var("SPOTIFY_USERNAME").unwrap_or_default();
source.set_property("username", username);
let password = std::env::var("SPOTIFY_PASSWORD").unwrap_or_default();
source.set_property("password", password );
gst_info!(CAT, obj: source, "Goodbye from source-setup");
}),
);
playbin
.set_state(gst::State::Playing)
.expect("Unable to set the pipeline to the `Playing` state");
let bus = playbin.bus().unwrap();
for msg in bus.iter_timed(gst::ClockTime::NONE) {
use gst::MessageView;
match msg.view() {
MessageView::Eos(..) => break,
MessageView::Error(err) => {
println!(
"Error from {:?}: {} ({:?})",
err.src().map(|s| s.path_string()),
err.error(),
err.debug()
);
break;
}
_ => (),
}
}
playbin
.set_state(gst::State::Null)
.expect("Unable to set the pipeline to the `Null` state");
Ok(())
}
@kingosticks
Copy link
Author

Using SPOTIFY_USERNAME=foo SPOTIFY_PASSWORD=bar GST_DEBUG=4,*spotify*:6 target/debug/examples/player spotify:track:5URJEznfi3XiVHQCyC3sII.

Log: https://dpaste.com/AKZ872S4M

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment