Start setting up the scanning loop
This commit is contained in:
parent
8de85afa15
commit
eb95fa7d4a
|
@ -16,3 +16,14 @@ pub fn error<A, FE: FatalError, E: Error>(err: E) -> Result<A, FE, E> {
|
|||
pub fn fatal<A, FE: FatalError, E: Error>(err: FE) -> Result<A, FE, E> {
|
||||
Err(err)
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! result {
|
||||
($x:expr) => {
|
||||
match $x {
|
||||
Ok(Ok(val)) => val,
|
||||
Ok(Err(err)) => return Ok(Err(err.into())),
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
15
flake.nix
15
flake.nix
|
@ -26,18 +26,19 @@
|
|||
name = "ld-tools-devshell";
|
||||
buildInputs = [
|
||||
pkgs.clang
|
||||
pkgs.dbus
|
||||
pkgs.entr
|
||||
pkgs.glib
|
||||
pkgs.gst_all_1.gstreamer
|
||||
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-bad
|
||||
pkgs.gst_all_1.gst-plugins-ugly
|
||||
pkgs.pipewire
|
||||
pkgs.openssl
|
||||
pkgs.pkg-config
|
||||
pkgs.gst_all_1.gstreamer
|
||||
pkgs.nodejs
|
||||
pkgs.entr
|
||||
pkgs.dbus
|
||||
pkgs.openssl
|
||||
pkgs.pipewire
|
||||
pkgs.pkg-config
|
||||
pkgs.sqlite
|
||||
rust
|
||||
];
|
||||
LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib";
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
pub use error::{error, fatal, ok, Result};
|
||||
|
||||
pub enum FatalError {
|
||||
UnexpectedError,
|
||||
}
|
||||
|
||||
impl error::FatalError for FatalError {}
|
|
@ -2,6 +2,17 @@
|
|||
# It is not intended for manual editing.
|
||||
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]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
|
@ -158,6 +169,18 @@ dependencies = [
|
|||
name = "errors"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "fallible-iterator"
|
||||
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]]
|
||||
name = "fastrand"
|
||||
version = "1.8.0"
|
||||
|
@ -290,6 +313,18 @@ name = "hashbrown"
|
|||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "headers"
|
||||
|
@ -448,6 +483,16 @@ dependencies = [
|
|||
"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]]
|
||||
name = "lock_api"
|
||||
version = "0.4.9"
|
||||
|
@ -539,9 +584,11 @@ dependencies = [
|
|||
"dbus",
|
||||
"errors",
|
||||
"mpris",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"url",
|
||||
"warp",
|
||||
]
|
||||
|
||||
|
@ -712,6 +759,20 @@ dependencies = [
|
|||
"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]]
|
||||
name = "rustls-pemfile"
|
||||
version = "0.2.1"
|
||||
|
@ -1124,6 +1185,12 @@ version = "0.7.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
|
|
|
@ -9,9 +9,11 @@ edition = "2021"
|
|||
dbus = { version = "0.9.7" }
|
||||
errors = { path = "../../errors" }
|
||||
mpris = { version = "2.0" }
|
||||
rusqlite = { version = "0.28" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
thiserror = { version = "1.0" }
|
||||
tokio = { version = "1.24", features = ["full"] }
|
||||
url = "2.3.1"
|
||||
warp = { version = "0.3" }
|
||||
|
||||
[lib]
|
||||
|
|
|
@ -69,6 +69,9 @@ pub enum AudioError {
|
|||
|
||||
#[error("Unknown problem with mpris")]
|
||||
MprisError(mpris::DBusError),
|
||||
|
||||
#[error("url parse error {0}")]
|
||||
UrlError(url::ParseError),
|
||||
}
|
||||
|
||||
impl From<dbus::Error> for AudioError {
|
||||
|
@ -83,9 +86,16 @@ impl From<mpris::DBusError> for AudioError {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<url::ParseError> for AudioError {
|
||||
fn from(err: url::ParseError) -> Self {
|
||||
Self::UrlError(err)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Track {
|
||||
pub id: String,
|
||||
pub track_number: Option<i32>,
|
||||
pub name: Option<String>,
|
||||
pub album: Option<String>,
|
||||
|
@ -95,6 +105,7 @@ pub struct Track {
|
|||
impl From<&mpris::Metadata> for Track {
|
||||
fn from(data: &mpris::Metadata) -> Self {
|
||||
Self {
|
||||
id: data.track_id().unwrap().to_string(),
|
||||
track_number: data.track_number(),
|
||||
name: data.title().map(|s| s.to_owned()),
|
||||
album: data.album_name().map(|s| s.to_owned()),
|
||||
|
@ -134,9 +145,33 @@ pub struct Capabilities {
|
|||
pub trait AudioPlayer {
|
||||
fn capabilities(&self) -> Result<Capabilities, AudioError>;
|
||||
fn state(&self) -> Result<State, AudioError>;
|
||||
fn play(&self, trackid: url::Url) -> 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 {
|
||||
device_id: String,
|
||||
player: Player,
|
||||
|
@ -153,7 +188,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 device_id = self.device_id.clone();
|
||||
|
@ -226,7 +261,17 @@ impl AudioPlayer for MprisDevice {
|
|||
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> {
|
||||
unimplemented!("Audioplayer play/pause command")
|
||||
self.player.play_pause()?;
|
||||
Ok(State::Stopped)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
use dbus::ffidisp::Connection;
|
||||
use serde::Serialize;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::path::PathBuf;
|
||||
use warp::Filter;
|
||||
use std::{io::stdin, path::PathBuf, thread, time::Duration};
|
||||
// use warp::Filter;
|
||||
|
||||
pub mod audio;
|
||||
use music_player::core::Core;
|
||||
|
||||
/*
|
||||
fn tracks() -> Vec<Track> {
|
||||
|
@ -52,6 +49,16 @@ fn tracks() -> Vec<Track> {
|
|||
|
||||
#[tokio::main]
|
||||
pub async fn main() {
|
||||
match Core::new(PathBuf::from(":memory:")) {
|
||||
Ok(Ok(core)) => {
|
||||
let mut buf = String::new();
|
||||
let _ = stdin().read_line(&mut buf).unwrap();
|
||||
core.exit();
|
||||
}
|
||||
Ok(Err(err)) => println!("non-fatal error: {:?}", err),
|
||||
Err(err) => println!("fatal error: {:?}", err),
|
||||
}
|
||||
|
||||
/*
|
||||
let connection = Connection::new_session().expect("to connect to dbus");
|
||||
|
||||
|
|
|
@ -1,45 +1,110 @@
|
|||
use dbus::ffidisp::Connection;
|
||||
use mpris::{Player, PlayerFinder};
|
||||
use crate::{database::Database, Error, FatalError};
|
||||
use errors::{ok, result, Result};
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender},
|
||||
thread,
|
||||
thread::JoinHandle,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Device {
|
||||
dbus_name: String,
|
||||
name: String,
|
||||
pub enum ControlMsg {
|
||||
Exit,
|
||||
}
|
||||
|
||||
pub enum TrackMsg {
|
||||
DbUpdate,
|
||||
}
|
||||
|
||||
pub enum PlaybackMsg {
|
||||
PositionUpdate,
|
||||
Playing,
|
||||
Pausing,
|
||||
Stopping,
|
||||
}
|
||||
|
||||
pub struct Core {
|
||||
conn: Connection,
|
||||
player: Player,
|
||||
db: Database,
|
||||
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: Database,
|
||||
control_rx: Receiver<ControlMsg>,
|
||||
tracker_tx: Sender<TrackMsg>,
|
||||
next_scan: Instant,
|
||||
}
|
||||
|
||||
impl FileScanner {
|
||||
fn new(db: Database, control_rx: Receiver<ControlMsg>, tracker_tx: Sender<TrackMsg>) -> Self {
|
||||
Self {
|
||||
db,
|
||||
control_rx,
|
||||
tracker_tx,
|
||||
next_scan: Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
println!("scan");
|
||||
self.next_scan = Instant::now() + scan_frequency();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Core {
|
||||
fn new(&self) -> Result<Core, Error> {
|
||||
let conn = Connection::new_session()?;
|
||||
Ok(Core {
|
||||
conn,
|
||||
player: mpris::Player::new(conn, ":1.6".to_owned(), 1000)?,
|
||||
pub fn new(db_path: PathBuf) -> Result<Core, FatalError, Error> {
|
||||
let db = result!(Database::new(db_path));
|
||||
|
||||
let (control_tx, control_rx) = channel::<ControlMsg>();
|
||||
|
||||
let (track_handle, track_rx) = {
|
||||
let (track_tx, track_rx) = channel();
|
||||
let db = db.clone();
|
||||
let track_handle = thread::spawn(move || {
|
||||
FileScanner::new(db, 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> {
|
||||
mpris::PlayerFinder::for_connection(conn)
|
||||
.find_all()?
|
||||
.into_iter()
|
||||
.map(|player| Device {
|
||||
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()
|
||||
pub fn exit(&self) {
|
||||
let _ = self.control_tx.send(ControlMsg::Exit);
|
||||
/*
|
||||
self.track_handle.join();
|
||||
self.playback_handle.join();
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
use crate::FatalError;
|
||||
use errors::{error, ok, Result};
|
||||
use rusqlite::Connection;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DatabaseError {
|
||||
#[error("database is unreadable")]
|
||||
DatabaseUnreadable,
|
||||
#[error("unhandled database problem: {0}")]
|
||||
UnhandledError(rusqlite::Error),
|
||||
}
|
||||
|
||||
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) -> Result<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);
|
||||
}
|
||||
}
|
|
@ -1 +1,24 @@
|
|||
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)]
|
||||
pub enum FatalError {
|
||||
UnexpectedError,
|
||||
}
|
||||
|
||||
impl errors::FatalError for FatalError {}
|
||||
|
|
Loading…
Reference in New Issue