Compare commits

...

7 Commits

18 changed files with 1350 additions and 130 deletions

7
errors/Cargo.lock generated
View File

@ -1,7 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "errors"
version = "0.1.0"

View File

@ -1,18 +0,0 @@
use std::error::Error;
use std::result;
pub trait FatalError {}
pub type Result<A, FE, E> = result::Result<result::Result<A, E>, FE>;
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Result<A, FE, E> {
Ok(Ok(val))
}
pub fn error<A, FE: FatalError, E: Error>(err: E) -> Result<A, FE, E> {
Ok(Err(err))
}
pub fn fatal<A, FE: FatalError, E: Error>(err: FE) -> Result<A, FE, E> {
Err(err)
}

View File

@ -26,12 +26,19 @@
name = "ld-tools-devshell"; name = "ld-tools-devshell";
buildInputs = [ buildInputs = [
pkgs.clang pkgs.clang
pkgs.pipewire
pkgs.openssl
pkgs.pkg-config
pkgs.nodejs
pkgs.entr
pkgs.dbus pkgs.dbus
pkgs.entr
pkgs.glib
pkgs.gst_all_1.gst-plugins-bad
pkgs.gst_all_1.gst-plugins-base
pkgs.gst_all_1.gst-plugins-good
pkgs.gst_all_1.gst-plugins-ugly
pkgs.gst_all_1.gstreamer
pkgs.nodejs
pkgs.openssl
pkgs.pipewire
pkgs.pkg-config
pkgs.sqlite
rust rust
]; ];
LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib"; LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib";

65
flow/Cargo.lock generated Normal file
View File

@ -0,0 +1,65 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "flow"
version = "0.1.0"
dependencies = [
"thiserror",
]
[[package]]
name = "proc-macro2"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"

11
flow/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "flow"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[dev-dependencies]
thiserror = "1"

217
flow/src/lib.rs Normal file
View File

@ -0,0 +1,217 @@
use std::error::Error;
pub trait FatalError: Error {}
pub enum Flow<A, FE, E> {
Ok(A),
Fatal(FE),
Err(E),
}
impl<A, FE, E> Flow<A, FE, E> {
pub fn map<B, O>(self, mapper: O) -> Flow<B, FE, E>
where
O: FnOnce(A) -> B,
{
match self {
Flow::Ok(val) => Flow::Ok(mapper(val)),
Flow::Fatal(err) => Flow::Fatal(err),
Flow::Err(err) => Flow::Err(err),
}
}
pub fn and_then<B, O>(self, handler: O) -> Flow<B, FE, E>
where
O: FnOnce(A) -> Flow<B, FE, E>,
{
match self {
Flow::Ok(val) => handler(val),
Flow::Fatal(err) => Flow::Fatal(err),
Flow::Err(err) => Flow::Err(err),
}
}
pub fn map_err<F, O>(self, mapper: O) -> Flow<A, FE, F>
where
O: FnOnce(E) -> F,
{
match self {
Flow::Ok(val) => Flow::Ok(val),
Flow::Fatal(err) => Flow::Fatal(err),
Flow::Err(err) => Flow::Err(mapper(err)),
}
}
pub fn or_else<O, F>(self, handler: O) -> Flow<A, FE, F>
where
O: FnOnce(E) -> Flow<A, FE, F>,
{
match self {
Flow::Ok(val) => Flow::Ok(val),
Flow::Fatal(err) => Flow::Fatal(err),
Flow::Err(err) => handler(err),
}
}
}
impl<A, FE, E> From<Result<A, E>> for Flow<A, FE, E> {
fn from(r: Result<A, E>) -> Self {
match r {
Ok(val) => Flow::Ok(val),
Err(err) => Flow::Err(err),
}
}
}
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> {
Flow::Ok(val)
}
pub fn error<A, FE: FatalError, E: Error>(err: E) -> Flow<A, FE, E> {
Flow::Err(err)
}
pub fn fatal<A, FE: FatalError, E: Error>(err: FE) -> Flow<A, FE, E> {
Flow::Fatal(err)
}
#[macro_export]
macro_rules! return_fatal {
($x:expr) => {
match $x {
Flow::Fatal(err) => return Flow::Fatal(err),
Flow::Err(err) => Err(err),
Flow::Ok(val) => Ok(val),
}
};
}
#[macro_export]
macro_rules! return_error {
($x:expr) => {
match $x {
Flow::Ok(val) => val,
Flow::Err(err) => return Flow::Err(err),
Flow::Fatal(err) => return Flow::Fatal(err),
}
};
}
#[cfg(test)]
mod test {
use super::*;
use thiserror::Error;
#[derive(Debug, Error)]
enum FatalError {
#[error("A fatal error occurred")]
FatalError,
}
impl super::FatalError for FatalError {}
impl PartialEq for FatalError {
fn eq(&self, rhs: &Self) -> bool {
true
}
}
#[derive(Debug, Error)]
enum Error {
#[error("an error occurred")]
Error,
}
impl PartialEq for Error {
fn eq(&self, rhs: &Self) -> bool {
true
}
}
impl PartialEq for Flow<i32, FatalError, Error> {
fn eq(&self, rhs: &Self) -> bool {
match (self, rhs) {
(Flow::Ok(val), Flow::Ok(rhs)) => val == rhs,
(Flow::Err(_), Flow::Err(_)) => true,
(Flow::Fatal(_), Flow::Fatal(_)) => true,
_ => false,
}
}
}
impl std::fmt::Debug for Flow<i32, FatalError, Error> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Flow::Ok(val) => f.write_fmt(format_args!("Flow::Ok {}", val)),
Flow::Err(err) => f.write_fmt(format_args!("Flow::Err {:?}", err)),
Flow::Fatal(err) => f.write_fmt(format_args!("Flow::Fatal {:?}", err)),
}
}
}
#[test]
fn it_can_map_things() {
let success = ok(15);
assert_eq!(ok(16), success.map(|v| v + 1));
}
#[test]
fn it_can_chain_success() {
let success = ok(15);
assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
}
#[test]
fn it_can_handle_an_error() {
let failure = error(Error::Error);
assert_eq!(ok(16), failure.or_else(|_| ok(16)));
}
#[test]
fn early_exit_on_fatal() {
fn ok_func() -> Flow<i32, FatalError, Error> {
let value = return_fatal!(ok::<i32, FatalError, Error>(15));
match value {
Ok(_) => ok(14),
Err(err) => error(err),
}
}
fn err_func() -> Flow<i32, FatalError, Error> {
let value = return_fatal!(error::<i32, FatalError, Error>(Error::Error));
match value {
Ok(_) => panic!("shouldn't have gotten here"),
Err(err) => ok(0),
}
}
fn fatal_func() -> Flow<i32, FatalError, Error> {
return_fatal!(fatal::<i32, FatalError, Error>(FatalError::FatalError));
panic!("failed to bail");
}
fatal_func();
assert_eq!(ok_func(), ok(14));
assert_eq!(err_func(), ok(0));
}
#[test]
fn it_can_early_exit_on_all_errors() {
fn ok_func() -> Flow<i32, FatalError, Error> {
let value = return_error!(ok::<i32, FatalError, Error>(15));
assert_eq!(value, 15);
ok(14)
}
fn err_func() -> Flow<i32, FatalError, Error> {
return_error!(error::<i32, FatalError, Error>(Error::Error));
panic!("failed to bail");
}
fn fatal_func() -> Flow<i32, FatalError, Error> {
return_error!(fatal::<i32, FatalError, Error>(FatalError::FatalError));
panic!("failed to bail");
}
fatal_func();
assert_eq!(ok_func(), ok(14));
err_func();
}
}

7
music-player/errors.rs Normal file
View File

@ -0,0 +1,7 @@
pub use flow::error;
pub enum FatalError {
UnexpectedError,
}
impl flow::FatalError for FatalError {}

View File

@ -2,6 +2,17 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "ahash"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
dependencies = [
"getrandom",
"once_cell",
"version_check",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -155,8 +166,16 @@ dependencies = [
] ]
[[package]] [[package]]
name = "errors" name = "fallible-iterator"
version = "0.1.0" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
@ -167,6 +186,10 @@ dependencies = [
"instant", "instant",
] ]
[[package]]
name = "flow"
version = "0.1.0"
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -290,6 +313,18 @@ name = "hashbrown"
version = "0.12.3" version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [
"ahash",
]
[[package]]
name = "hashlink"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69fe1fcf8b4278d860ad0548329f892a3631fb63f82574df68275f34cdbe0ffa"
dependencies = [
"hashbrown",
]
[[package]] [[package]]
name = "headers" name = "headers"
@ -448,6 +483,16 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "libsqlite3-sys"
version = "0.25.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29f835d03d717946d28b1d1ed632eb6f0e24a299388ee623d0c23118d3e8a7fa"
dependencies = [
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.9" version = "0.4.9"
@ -537,11 +582,14 @@ name = "music-player"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dbus", "dbus",
"errors", "flow",
"mpris", "mpris",
"rusqlite",
"serde", "serde",
"thiserror", "thiserror",
"tokio", "tokio",
"url",
"uuid",
"warp", "warp",
] ]
@ -712,6 +760,20 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rusqlite"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01e213bc3ecb39ac32e81e51ebe31fd888a940515173e3a18a35f8c6e896422a"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"smallvec",
]
[[package]] [[package]]
name = "rustls-pemfile" name = "rustls-pemfile"
version = "0.2.1" version = "0.2.1"
@ -1124,6 +1186,21 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "uuid"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79"
dependencies = [
"getrandom",
]
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View File

@ -7,11 +7,14 @@ edition = "2021"
[dependencies] [dependencies]
dbus = { version = "0.9.7" } dbus = { version = "0.9.7" }
errors = { path = "../../errors" } flow = { path = "../../flow" }
mpris = { version = "2.0" } mpris = { version = "2.0" }
rusqlite = { version = "0.28" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
thiserror = { version = "1.0" } thiserror = { version = "1.0" }
tokio = { version = "1.24", features = ["full"] } tokio = { version = "1.24", features = ["full"] }
url = { version = "2.3" }
uuid = { version = "1", features = ["v4"] }
warp = { version = "0.3" } warp = { version = "0.3" }
[lib] [lib]

View File

@ -69,6 +69,9 @@ pub enum AudioError {
#[error("Unknown problem with mpris")] #[error("Unknown problem with mpris")]
MprisError(mpris::DBusError), MprisError(mpris::DBusError),
#[error("url parse error {0}")]
UrlError(url::ParseError),
} }
impl From<dbus::Error> for AudioError { impl From<dbus::Error> for AudioError {
@ -83,18 +86,56 @@ impl From<mpris::DBusError> for AudioError {
} }
} }
#[derive(Clone, Debug, Serialize)] impl From<url::ParseError> for AudioError {
#[serde(rename_all = "camelCase")] fn from(err: url::ParseError) -> Self {
pub struct Track { Self::UrlError(err)
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize)]
pub struct TrackId(String);
impl Default for TrackId {
fn default() -> Self {
Self(uuid::Uuid::new_v4().as_hyphenated().to_string())
}
}
impl From<String> for TrackId {
fn from(id: String) -> Self {
Self(id)
}
}
impl AsRef<String> for TrackId {
fn as_ref<'a>(&'a self) -> &'a String {
&self.0
}
}
#[derive(Clone, Debug)]
pub struct TrackInfo {
pub track_number: Option<i32>, pub track_number: Option<i32>,
pub name: Option<String>, pub name: Option<String>,
pub album: Option<String>, pub album: Option<String>,
pub artist: Option<String>, pub artist: Option<String>,
} }
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Track {
pub id: TrackId,
pub track_number: Option<i32>,
pub name: Option<String>,
pub album: Option<String>,
pub artist: Option<String>,
}
/*
impl From<&mpris::Metadata> for Track { impl From<&mpris::Metadata> for Track {
fn from(data: &mpris::Metadata) -> Self { fn from(data: &mpris::Metadata) -> Self {
Self { Self {
id: data.track_id().unwrap(),
track_number: data.track_number(), track_number: data.track_number(),
name: data.title().map(|s| s.to_owned()), name: data.title().map(|s| s.to_owned()),
album: data.album_name().map(|s| s.to_owned()), album: data.album_name().map(|s| s.to_owned()),
@ -108,6 +149,7 @@ impl From<mpris::Metadata> for Track {
Self::from(&data) Self::from(&data)
} }
} }
*/
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -134,9 +176,33 @@ pub struct Capabilities {
pub trait AudioPlayer { pub trait AudioPlayer {
fn capabilities(&self) -> Result<Capabilities, AudioError>; fn capabilities(&self) -> Result<Capabilities, AudioError>;
fn state(&self) -> Result<State, AudioError>; fn state(&self) -> Result<State, AudioError>;
fn play(&self, trackid: url::Url) -> Result<State, AudioError>;
fn play_pause(&self) -> Result<State, AudioError>; fn play_pause(&self) -> Result<State, AudioError>;
} }
pub struct GStreamerPlayer {
url: url::Url,
}
impl AudioPlayer for GStreamerPlayer {
fn capabilities(&self) -> Result<Capabilities, AudioError> {
unimplemented!()
}
fn state(&self) -> Result<State, AudioError> {
unimplemented!()
}
fn play(&self, trackid: url::Url) -> Result<State, AudioError> {
unimplemented!()
}
fn play_pause(&self) -> Result<State, AudioError> {
unimplemented!()
}
}
/*
pub struct MprisDevice { pub struct MprisDevice {
device_id: String, device_id: String,
player: Player, player: Player,
@ -153,7 +219,7 @@ impl MprisDevice {
}) })
} }
pub fn run(&mut self, control_channel: Receiver<Message>) { pub fn monitor(&mut self, control_channel: Receiver<Message>) {
let (tx, rx) = channel(); let (tx, rx) = channel();
{ {
let device_id = self.device_id.clone(); let device_id = self.device_id.clone();
@ -226,7 +292,17 @@ impl AudioPlayer for MprisDevice {
unimplemented!("AudioPlayer state") unimplemented!("AudioPlayer state")
} }
fn play(&self, track_id: String) -> Result<State, AudioError> {
println!("playing: {}", track_id);
self.player
.go_to(&mpris::TrackID::from(dbus::Path::from(track_id)))?;
self.player.play();
Ok(State::Stopped)
}
fn play_pause(&self) -> Result<State, AudioError> { fn play_pause(&self) -> Result<State, AudioError> {
unimplemented!("Audioplayer play/pause command") self.player.play_pause()?;
Ok(State::Stopped)
} }
} }
*/

View File

@ -1,52 +0,0 @@
/*
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of the Luminescent Dreams Tools.
Luminescent Dreams Tools is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Luminescent Dreams Tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
*/
use dbus::ffidisp::Connection;
use music_player::audio::{list_devices, Message, MprisDevice};
use std::{io, thread};
fn main() {
for device in list_devices(Connection::new_session().expect("connection to dbus session"))
.expect("to list devices")
{
println!("Device: [{}] {}", device.address, device.name);
}
/*
let progress_thread = thread::spawn(|| MprisDevice::monitor_progress(":1.1611".to_owned()));
let event_thread = thread::spawn(|| MprisDevice::scan_event_stream(":1.1611".to_owned()));
*/
let (tx, rx) = std::sync::mpsc::channel();
let app_thread = thread::spawn(move || {
let mut device = MprisDevice::new(":1.1611".to_owned()).expect("to get a device");
device.run(rx);
});
let stdin = io::stdin();
let mut input = String::new();
let _ = stdin.read_line(&mut input);
let _ = tx.send(Message::Quit);
let _ = app_thread.join();
/*
println!(
"{:?}",
device.capabilities().expect("can retrieve capabilities")
);
// println!("{:?}", device.state().expect("play-pause to succeed"));
let _ = progress_thread.join();
let _ = event_thread.join();
*/
}

View File

@ -1,10 +1,8 @@
use dbus::ffidisp::Connection; use flow::Flow;
use serde::Serialize; use std::{io::stdin, path::PathBuf, sync::Arc, thread, time::Duration};
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; // use warp::Filter;
use std::path::PathBuf;
use warp::Filter;
pub mod audio; use music_player::{core::Core, database::MemoryIndex};
/* /*
fn tracks() -> Vec<Track> { fn tracks() -> Vec<Track> {
@ -52,6 +50,16 @@ fn tracks() -> Vec<Track> {
#[tokio::main] #[tokio::main]
pub async fn main() { pub async fn main() {
match Core::new(Arc::new(MemoryIndex::new())) {
Flow::Ok(core) => {
let mut buf = String::new();
let _ = stdin().read_line(&mut buf).unwrap();
core.exit();
}
Flow::Err(err) => println!("non-fatal error: {:?}", err),
Flow::Fatal(err) => println!("fatal error: {:?}", err),
}
/* /*
let connection = Connection::new_session().expect("to connect to dbus"); let connection = Connection::new_session().expect("to connect to dbus");

View File

@ -1,45 +1,179 @@
use dbus::ffidisp::Connection; use crate::{
use mpris::{Player, PlayerFinder}; database::{Database, MemoryIndex, MusicIndex},
Error, FatalError,
};
use flow::{error, fatal, ok, return_error, return_fatal, Flow};
use std::{
path::{Path, PathBuf},
sync::{
mpsc::{channel, Receiver, RecvTimeoutError, Sender},
Arc,
},
thread,
thread::JoinHandle,
time::{Duration, Instant},
};
#[derive(Clone, Debug, Serialize)] #[derive(Debug, Error)]
#[serde(rename_all = "camelCase")] pub enum ScannerError {
struct Device { #[error("Cannot scan {0}")]
dbus_name: String, CannotScan(PathBuf),
name: String, #[error("IO error {0}")]
IO(std::io::Error),
}
impl From<std::io::Error> for ScannerError {
fn from(err: std::io::Error) -> Self {
Self::IO(err)
}
}
pub enum ControlMsg {
Exit,
}
pub enum TrackMsg {
DbUpdate,
}
pub enum PlaybackMsg {
PositionUpdate,
Playing,
Pausing,
Stopping,
} }
pub struct Core { pub struct Core {
conn: Connection, db: Arc<dyn MusicIndex>,
player: Player, track_handle: JoinHandle<()>,
track_rx: Receiver<TrackMsg>,
playback_handle: JoinHandle<()>,
playback_rx: Receiver<PlaybackMsg>,
control_tx: Sender<ControlMsg>,
}
fn scan_frequency() -> Duration {
Duration::from_secs(60)
}
pub struct FileScanner {
db: Arc<dyn MusicIndex>,
control_rx: Receiver<ControlMsg>,
tracker_tx: Sender<TrackMsg>,
next_scan: Instant,
music_directories: Vec<PathBuf>,
}
impl FileScanner {
fn new(
db: Arc<dyn MusicIndex>,
roots: Vec<PathBuf>,
control_rx: Receiver<ControlMsg>,
tracker_tx: Sender<TrackMsg>,
) -> Self {
Self {
db,
control_rx,
tracker_tx,
next_scan: Instant::now(),
music_directories: roots,
}
}
fn scan(&mut self) {
loop {
match self.control_rx.recv_timeout(Duration::from_millis(100)) {
Ok(ControlMsg::Exit) => return,
Err(RecvTimeoutError::Timeout) => (),
Err(RecvTimeoutError::Disconnected) => return,
}
if Instant::now() >= self.next_scan {
for root in self.music_directories.iter() {
self.scan_dir(vec![root.clone()]);
}
self.next_scan = Instant::now() + scan_frequency();
}
}
}
fn scan_dir(&self, mut paths: Vec<PathBuf>) -> Flow<(), FatalError, ScannerError> {
while let Some(dir) = paths.pop() {
println!("scanning {:?}", dir);
return_error!(self.scan_dir_(&mut paths, dir));
}
ok(())
}
fn scan_dir_(
&self,
paths: &mut Vec<PathBuf>,
dir: PathBuf,
) -> Flow<(), FatalError, ScannerError> {
let dir_iter = return_error!(Flow::from(dir.read_dir().map_err(ScannerError::from)));
for entry in dir_iter {
match entry {
Ok(entry) if entry.path().is_dir() => paths.push(entry.path()),
Ok(entry) => {
let _ = return_fatal!(self.scan_file(entry.path()).or_else(|err| {
println!("scan_file failed: {:?}", err);
ok::<(), FatalError, ScannerError>(())
}));
()
}
Err(err) => {
println!("scan_dir could not read path: ({:?})", err);
}
}
}
ok(())
}
fn scan_file(&self, path: PathBuf) -> Flow<(), FatalError, ScannerError> {
ok(())
}
} }
impl Core { impl Core {
fn new(&self) -> Result<Core, Error> { pub fn new(db: Arc<dyn MusicIndex>) -> Flow<Core, FatalError, Error> {
let conn = Connection::new_session()?; let (control_tx, control_rx) = channel::<ControlMsg>();
Ok(Core {
conn, let (track_handle, track_rx) = {
player: mpris::Player::new(conn, ":1.6".to_owned(), 1000)?, let (track_tx, track_rx) = channel();
let db = db.clone();
let track_handle = thread::spawn(move || {
FileScanner::new(
db,
vec![PathBuf::from("/home/savanni/Music/")],
control_rx,
track_tx,
)
.scan();
});
(track_handle, track_rx)
};
let (playback_handle, playback_rx) = {
let (playback_tx, playback_rx) = channel();
let playback_handle = thread::spawn(move || {});
(playback_handle, playback_rx)
};
ok(Core {
db,
track_handle,
track_rx,
playback_handle,
playback_rx,
control_tx,
}) })
} }
fn list_devices(&self) -> Result<Vec<Device>, Error> { pub fn exit(&self) {
mpris::PlayerFinder::for_connection(conn) let _ = self.control_tx.send(ControlMsg::Exit);
.find_all()? /*
.into_iter() self.track_handle.join();
.map(|player| Device { self.playback_handle.join();
dbus_name: player.unique_name().to_owned(), */
name: player.identity().to_owned(),
})
.collect()
}
fn list_tracks(&self) -> Result<Vec<String>, Error> {
self.player
.get_track_list()?
.ids()
.into_iter()
.map(|id| id.as_str().to_owned())
.collect()
} }
} }

View File

@ -0,0 +1,106 @@
use crate::{
audio::{Track, TrackId, TrackInfo},
FatalError,
};
use flow::{error, ok, Flow};
use rusqlite::Connection;
use std::collections::HashMap;
use std::{
path::PathBuf,
sync::{Arc, Mutex, RwLock},
};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum DatabaseError {
#[error("database is unreadable")]
DatabaseUnreadable,
#[error("unhandled database problem: {0}")]
UnhandledError(rusqlite::Error),
}
pub trait MusicIndex: Sync + Send {
fn add_track(&mut self, track: &TrackInfo) -> Flow<Track, FatalError, DatabaseError>;
fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>;
fn get_track_info(&self, id: &TrackId) -> Flow<Option<Track>, FatalError, DatabaseError>;
}
pub struct MemoryIndex {
tracks: RwLock<HashMap<TrackId, Track>>,
}
impl MemoryIndex {
pub fn new() -> MemoryIndex {
MemoryIndex {
tracks: RwLock::new(HashMap::default()),
}
}
}
impl MusicIndex for MemoryIndex {
fn add_track(&mut self, info: &TrackInfo) -> Flow<Track, FatalError, DatabaseError> {
let id = TrackId::default();
let track = Track {
id: id.clone(),
track_number: info.track_number,
name: info.name.clone(),
album: info.album.clone(),
artist: info.artist.clone(),
};
let mut tracks = self.tracks.write().unwrap();
tracks.insert(id, track.clone());
ok(track)
}
fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> {
let mut tracks = self.tracks.write().unwrap();
tracks.remove(&id);
ok(())
}
fn get_track_info<'a>(
&'a self,
id: &TrackId,
) -> Flow<Option<Track>, FatalError, DatabaseError> {
let track = {
let tracks = self.tracks.read().unwrap();
tracks.get(&id).cloned()
};
ok(track)
}
}
pub struct ManagedConnection<'a> {
pool: &'a Database,
conn: Option<Connection>,
}
impl<'a> Drop for ManagedConnection<'a> {
fn drop(&mut self) {
self.pool.r(self.conn.take().unwrap());
}
}
#[derive(Clone)]
pub struct Database {
path: PathBuf,
pool: Arc<Mutex<Vec<Connection>>>,
}
impl Database {
pub fn new(path: PathBuf) -> Flow<Database, FatalError, DatabaseError> {
let connection = match Connection::open(path.clone()) {
Ok(connection) => connection,
Err(err) => return error(DatabaseError::UnhandledError(err)),
};
ok(Database {
path,
pool: Arc::new(Mutex::new(vec![connection])),
})
}
pub fn r(&self, conn: Connection) {
let mut pool = self.pool.lock().unwrap();
pool.push(conn);
}
}

View File

@ -1 +1,25 @@
pub mod audio; pub mod audio;
pub mod core;
pub mod database;
use database::DatabaseError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Database error: {0}")]
DatabaseError(DatabaseError),
}
impl From<DatabaseError> for Error {
fn from(err: DatabaseError) -> Self {
Self::DatabaseError(err)
}
}
#[derive(Debug, Error)]
pub enum FatalError {
#[error("Unexpected error")]
UnexpectedError,
}
impl flow::FatalError for FatalError {}

514
music-player/streamer/Cargo.lock generated Normal file
View File

@ -0,0 +1,514 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800"
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-expr"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0357a6402b295ca3a86bc148e84df46c02e41f41fef186bda662557ef6328aa"
dependencies = [
"smallvec",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "futures-channel"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e5317663a9089767a1ec00a487df42e0ca174b61b4483213ac24448e4664df5"
dependencies = [
"futures-core",
]
[[package]]
name = "futures-core"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec90ff4d0fe1f57d600049061dc6bb68ed03c7d2fbd697274c41805dcb3f8608"
[[package]]
name = "futures-executor"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8de0a35a6ab97ec8869e32a2473f4b1324459e14c29275d14b10cb1fd19b50e"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-macro"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95a73af87da33b5acf53acfebdc339fe592ecf5357ac7c0a7734ab9d8c876a70"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-task"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf79a1bf610b10f42aea489289c5a2c478a786509693b80cd39c44ccd936366"
[[package]]
name = "futures-util"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c1d6de3acfef38d2be4b1f543f553131788603495be83da675e180c8d6b7bd1"
dependencies = [
"futures-core",
"futures-macro",
"futures-task",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "gio-sys"
version = "0.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9b693b8e39d042a95547fc258a7b07349b1f0b48f4b2fa3108ba3c51c0b5229"
dependencies = [
"glib-sys",
"gobject-sys",
"libc",
"system-deps",
"winapi",
]
[[package]]
name = "glib"
version = "0.16.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddd4df61a866ed7259d6189b8bcb1464989a77f1d85d25d002279bbe9dd38b2f"
dependencies = [
"bitflags",
"futures-channel",
"futures-core",
"futures-executor",
"futures-task",
"futures-util",
"gio-sys",
"glib-macros",
"glib-sys",
"gobject-sys",
"libc",
"once_cell",
"smallvec",
"thiserror",
]
[[package]]
name = "glib-macros"
version = "0.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e084807350b01348b6d9dbabb724d1a0bb987f47a2c85de200e98e12e30733bf"
dependencies = [
"anyhow",
"heck",
"proc-macro-crate",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "glib-sys"
version = "0.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61a4f46316d06bfa33a7ac22df6f0524c8be58e3db2d9ca99ccb1f357b62a65"
dependencies = [
"libc",
"system-deps",
]
[[package]]
name = "gobject-sys"
version = "0.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3520bb9c07ae2a12c7f2fbb24d4efc11231c8146a86956413fb1a79bb760a0f1"
dependencies = [
"glib-sys",
"libc",
"system-deps",
]
[[package]]
name = "gstreamer"
version = "0.19.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61af131b56332ec386a8ea538d961d0c6937054fb2771a9a505395f8471b7b0e"
dependencies = [
"bitflags",
"cfg-if",
"futures-channel",
"futures-core",
"futures-util",
"glib",
"gstreamer-sys",
"libc",
"muldiv",
"num-integer",
"num-rational",
"once_cell",
"option-operations",
"paste",
"pretty-hex",
"thiserror",
]
[[package]]
name = "gstreamer-sys"
version = "0.19.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "545f52ad8a480732cc4290fd65dfe42952c8ae374fe581831ba15981fedf18a4"
dependencies = [
"glib-sys",
"gobject-sys",
"libc",
"system-deps",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "indexmap"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "muldiv"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "956787520e75e9bd233246045d19f42fb73242759cc57fba9611d940ae96d4b0"
[[package]]
name = "nom8"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae01545c9c7fc4486ab7debaf2aad7003ac19431791868fb2e8066df97fad2f8"
dependencies = [
"memchr",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66"
[[package]]
name = "option-operations"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c26d27bb1aeab65138e4bf7666045169d1717febcc9ff870166be8348b223d0"
dependencies = [
"paste",
]
[[package]]
name = "paste"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba"
[[package]]
name = "pin-project-lite"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]]
name = "pretty-hex"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6fa0831dd7cc608c38a5e323422a0077678fa5744aa2be4ad91c4ece8eec8d5"
[[package]]
name = "proc-macro-crate"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66618389e4ec1c7afe67d51a9bf34ff9236480f8d51e7489b7d5ab0303c13f34"
dependencies = [
"once_cell",
"toml_edit",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
[[package]]
name = "slab"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef"
dependencies = [
"autocfg",
]
[[package]]
name = "smallvec"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
[[package]]
name = "streamer"
version = "0.1.0"
dependencies = [
"gstreamer",
]
[[package]]
name = "syn"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "system-deps"
version = "6.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2955b1fe31e1fa2fbd1976b71cc69a606d7d4da16f6de3333d0c92d51419aeff"
dependencies = [
"cfg-expr",
"heck",
"pkg-config",
"toml",
"version-compare",
]
[[package]]
name = "thiserror"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "toml"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
dependencies = [
"serde",
]
[[package]]
name = "toml_datetime"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4553f467ac8e3d374bc9a177a26801e5d0f9b211aa1673fb137a403afd1c9cf5"
[[package]]
name = "toml_edit"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c59d8dd7d0dcbc6428bf7aa2f0e823e26e43b3c9aca15bbc9475d23e5fa12b"
dependencies = [
"indexmap",
"nom8",
"toml_datetime",
]
[[package]]
name = "unicode-ident"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "version-compare"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -1,8 +1,10 @@
[package] [package]
name = "errors" name = "streamer"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
gstreamer = "0.19.7"

View File

@ -0,0 +1,46 @@
use gstreamer::{format::ClockTime, prelude::*, MessageView};
use std::{thread, time, time::Duration};
fn main() {
gstreamer::init().unwrap();
let uri = "file:///home/savanni/Music/Lindsey%20Stirling/Artemis/01%20-%20Underground.mp3.mp3";
let pipeline = gstreamer::parse_launch(&format!("playbin uri={}", uri)).unwrap();
pipeline.set_state(gstreamer::State::Playing).unwrap();
let message_handler = {
let pipeline = pipeline.clone();
thread::spawn(move || {
let bus = pipeline.bus().unwrap();
for msg in bus.iter_timed(gstreamer::ClockTime::NONE) {
match msg.view() {
MessageView::Eos(_) => (),
MessageView::Error(err) => {
println!(
"Error from {:?}: {} ({:?})",
err.src().map(|s| s.path_string()),
err.error(),
err.debug()
);
}
msg => println!("{:?}", msg),
}
}
})
};
let query_handler = {
let pipeline = pipeline.clone();
thread::spawn(move || loop {
let position: Option<ClockTime> = pipeline.query_position();
let duration: Option<ClockTime> = pipeline.query_duration();
println!("{:?} {:?}", position, duration);
thread::sleep(Duration::from_millis(100));
})
};
message_handler.join();
query_handler.join();
pipeline.set_state(gstreamer::State::Null).unwrap();
}