Skip to content

Commit 4cc843b

Browse files
committed
feat: Integrating update play now API
1 parent dda4399 commit 4cc843b

File tree

12 files changed

+163
-111
lines changed

12 files changed

+163
-111
lines changed

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# various Rust configurations.
55

66
[workspace]
7-
members = ["./native/*", "./cli", "lyric", "scrobbling"]
7+
members = ["./native/*", "./cli"]
88
resolver = "2"
99

1010
[patch.crates-io]

native/hub/src/player.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use std::time::{SystemTime, UNIX_EPOCH};
55
use anyhow::{bail, Error};
66
use anyhow::{Context, Result};
77
use log::{debug, error, info};
8-
use sea_orm::DatabaseConnection;
8+
use sea_orm::{DatabaseConnection, TransactionTrait};
99
use tokio::sync::Mutex;
1010
use tokio::task;
1111

12+
use database::actions::logging::insert_log;
1213
use database::actions::playback_queue::replace_playback_queue;
1314
use database::actions::stats::increase_played_through;
1415
use database::connection::MainDbConnection;
@@ -29,12 +30,12 @@ use scrobbling::ScrobblingTrack;
2930
use crate::{CrashResponse, PlaybackStatus, PlaylistItem, PlaylistUpdate, RealtimeFft};
3031

3132
pub fn metadata_summary_to_scrobbling_track(
32-
metadata: PlayingItemMetadataSummary,
33+
metadata: &PlayingItemMetadataSummary,
3334
) -> ScrobblingTrack {
3435
ScrobblingTrack {
35-
artist: metadata.artist,
36-
album: Some(metadata.album),
37-
track: metadata.title,
36+
artist: metadata.artist.clone(),
37+
album: Some(metadata.album.clone()),
38+
track: metadata.title.clone(),
3839
duration: Some(metadata.duration.clamp(0.0, u32::MAX as f64) as u32),
3940
album_artist: None,
4041
timestamp: Some(
@@ -61,13 +62,17 @@ pub async fn initialize_player(
6162
let main_db_for_status = Arc::clone(&main_db);
6263
let main_db_for_played_throudh = Arc::clone(&main_db);
6364
let main_db_for_playlist = Arc::clone(&main_db);
65+
let main_db_for_error_reporter = Arc::clone(&main_db);
6466

6567
let manager = Arc::new(Mutex::new(MediaControlManager::new()?));
6668

6769
let os_controller_receiver = manager.lock().await.subscribe_controller_events();
6870
let dispatcher = Arc::new(Mutex::new(PlayingItemActionDispatcher::new()));
6971
let dispatcher_for_played_through = Arc::clone(&dispatcher);
7072

73+
let scrobber_for_played_through = Arc::clone(&scrobbler);
74+
let scrobber_for_error_reporter = Arc::clone(&scrobbler);
75+
7176
manager.lock().await.initialize()?;
7277

7378
info!("Initializing event listeners");
@@ -137,6 +142,9 @@ pub async fn initialize_player(
137142
);
138143
}
139144
};
145+
146+
let track = metadata_summary_to_scrobbling_track(metadata);
147+
scrobbler.lock().await.update_now_playing_all(track);
140148
}
141149
None => {
142150
error!("No metadata found for: {:?}", item_clone_for_status);
@@ -215,6 +223,7 @@ pub async fn initialize_player(
215223
task::spawn(async move {
216224
let main_db = Arc::clone(&main_db_for_played_throudh);
217225
let dispatcher = Arc::clone(&dispatcher_for_played_through);
226+
let scrobbler = Arc::clone(&scrobber_for_played_through);
218227

219228
while let Ok(item) = played_through_receiver.recv().await {
220229
match item {
@@ -242,7 +251,7 @@ pub async fn initialize_player(
242251
}
243252

244253
let metadata: PlayingItemMetadataSummary = metadata[0].clone();
245-
let track: ScrobblingTrack = metadata_summary_to_scrobbling_track(metadata);
254+
let track: ScrobblingTrack = metadata_summary_to_scrobbling_track(&metadata);
246255

247256
scrobbler.lock().await.scrobble_all(track);
248257
}
@@ -255,6 +264,41 @@ pub async fn initialize_player(
255264
}
256265
});
257266

267+
task::spawn(async move {
268+
let main_db = Arc::clone(&main_db_for_error_reporter);
269+
270+
let scrobbler = Arc::clone(&scrobber_for_error_reporter);
271+
let error_receiver = scrobbler.lock().await.subscribe_error();
272+
273+
while let Ok(error) = error_receiver.recv().await {
274+
error!(
275+
"Scrobbler received error: {:?}::{:?}: {:#?}",
276+
error.service, error.action, error.error
277+
);
278+
279+
match main_db.begin().await {
280+
Ok(txn) => {
281+
if let Err(e) = insert_log(
282+
&txn,
283+
database::actions::logging::LogLevel::Error,
284+
format!("scrobbler::{:?}::{:?}", error.action, error.service),
285+
format!("{:#?}", error),
286+
)
287+
.await
288+
{
289+
error!("Failed to log scrobbler error: {:#?}", e);
290+
}
291+
}
292+
Err(e) => {
293+
error!(
294+
"Failed to start txn while logging scrobbler error: {:#?}",
295+
e
296+
);
297+
}
298+
}
299+
}
300+
});
301+
258302
task::spawn(async move {
259303
let player = Arc::clone(&player);
260304

playback/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ windows = { version = "0.44.0", features = [
4141
"Win32_Graphics_Gdi",
4242
] }
4343
once_cell = "1.20.2"
44+
simple_channel = { path = "../simple-channel" }
4445

4546
[target.'cfg(not(target_os = "android"))'.dependencies]
4647
souvlaki = "0.7.3"
4748

4849
[target.'cfg(target_os = "android")'.dependencies]
4950
ndk-context = "0.1.1"
5051
jni = "0.21.1"
51-

playback/src/controller.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ use std::{
1111
use anyhow::{bail, Error, Result};
1212
use log::{debug, info};
1313
use once_cell::sync::OnceCell;
14+
use tokio::sync::Mutex;
1415

1516
#[cfg(target_os = "android")]
1617
use crate::dummy_souvlaki::{MediaControlEvent, MediaControls, PlatformConfig, SeekDirection};
17-
use crate::simple_channel::{SimpleChannel, SimpleReceiver, SimpleSender};
1818
#[cfg(not(target_os = "android"))]
1919
use souvlaki::{MediaControlEvent, MediaControls, PlatformConfig, SeekDirection};
20-
use tokio::sync::Mutex;
20+
21+
use simple_channel::{SimpleChannel, SimpleReceiver, SimpleSender};
2122

2223
use crate::player::{PlaybackState, Player};
2324

playback/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod internal;
22
mod realtime_fft;
33
mod sfx_internal;
44
mod shared_source;
5-
mod simple_channel;
65

76
pub mod buffered;
87
pub mod controller;

playback/src/player.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use std::time::Duration;
44
use std::{fmt, thread};
55

66
use log::{debug, error};
7+
use simple_channel::{SimpleChannel, SimpleReceiver, SimpleSender};
78
use tokio::sync::mpsc;
89
use tokio_util::sync::CancellationToken;
910

1011
use crate::internal::{PlaybackMode, PlayerCommand, PlayerEvent, PlayerInternal};
11-
use crate::simple_channel::{SimpleChannel, SimpleReceiver, SimpleSender};
1212
use crate::strategies::AddMode;
1313

1414
#[derive(Debug, Clone)]

playback/src/realtime_fft.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::thread;
33

44
use rustfft::{num_complex::Complex, FftPlanner};
55

6-
use crate::simple_channel::{SimpleChannel, SimpleReceiver, SimpleSender};
6+
use simple_channel::{SimpleChannel, SimpleReceiver, SimpleSender};
77

88
pub struct RealTimeFFT {
99
window_size: usize,

scrobbling/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ reqwest = { version = "0.12.9", features = ["blocking", "json", "socks"] }
1616
serde = "1.0.216"
1717
serde_json = "1.0.133"
1818
tokio = { version = "1.42.0" }
19+
simple_channel = { path = "../simple-channel" }
1920

2021
[dev-dependencies]
2122
clap = "4.5.9"

0 commit comments

Comments
 (0)