Add metadata display and optimize CPU usage
All checks were successful
Build and Release / build-and-release (push) Successful in 58s
All checks were successful
Build and Release / build-and-release (push) Successful in 58s
Metadata features: - Display artist, album, title in bottom status bar (left-aligned) - Display bitrate, codec, sample rate in bottom status bar (right-aligned) - Fetch metadata from MPV using metadata/by-key properties - Support both lowercase and uppercase metadata tags Performance optimizations: - Split property updates: fast updates (position/duration) vs slow (metadata) - Update metadata only every 2 seconds instead of every 100ms - Skip MPV property updates entirely when stopped - Conditional UI redraws - only when state actually changes - Variable poll rate: 200ms when stopped, 100ms when playing - Position update throttling (0.5s minimum change) - Reduces CPU usage from constant 10 FPS to ~0.1 FPS when idle
This commit is contained in:
84
src/main.rs
84
src/main.rs
@@ -79,39 +79,85 @@ async fn run_app<B: ratatui::backend::Backend>(
|
||||
state: &mut AppState,
|
||||
player: &mut player::Player,
|
||||
) -> Result<()> {
|
||||
let mut metadata_update_counter = 0u32;
|
||||
let mut last_position = 0.0f64;
|
||||
let mut needs_redraw = true;
|
||||
|
||||
loop {
|
||||
let mut state_changed = false;
|
||||
|
||||
// Check if mpv process died (e.g., user closed video window)
|
||||
if !player.is_process_alive() && state.player_state != PlayerState::Stopped {
|
||||
state.player_state = PlayerState::Stopped;
|
||||
state.current_position = 0.0;
|
||||
state.current_duration = 0.0;
|
||||
state_changed = true;
|
||||
}
|
||||
|
||||
// Update player properties from MPV
|
||||
player.update_properties();
|
||||
// Only update properties when playing or paused (not when stopped)
|
||||
if state.player_state != PlayerState::Stopped {
|
||||
player.update_properties();
|
||||
|
||||
// Update position and duration from player
|
||||
state.current_position = player.get_position().unwrap_or(0.0);
|
||||
state.current_duration = player.get_duration().unwrap_or(0.0);
|
||||
// Update metadata only every 20 iterations (~2 seconds) to reduce IPC calls
|
||||
metadata_update_counter += 1;
|
||||
if metadata_update_counter >= 20 {
|
||||
player.update_metadata();
|
||||
metadata_update_counter = 0;
|
||||
state_changed = true;
|
||||
}
|
||||
|
||||
// Check if track ended and play next (but only if track was actually loaded)
|
||||
if player.is_idle() && state.player_state == PlayerState::Playing && state.current_duration > 0.0 {
|
||||
if state.playlist_index + 1 < state.playlist.len() {
|
||||
state.play_next();
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
// Update position and duration from player
|
||||
let new_position = player.get_position().unwrap_or(0.0);
|
||||
let new_duration = player.get_duration().unwrap_or(0.0);
|
||||
|
||||
// Only mark as changed if position moved by at least 0.5 seconds
|
||||
if (new_position - last_position).abs() >= 0.5 {
|
||||
state.current_position = new_position;
|
||||
last_position = new_position;
|
||||
state_changed = true;
|
||||
}
|
||||
|
||||
if state.current_duration != new_duration {
|
||||
state.current_duration = new_duration;
|
||||
state_changed = true;
|
||||
}
|
||||
|
||||
// Check if track ended and play next (but only if track was actually loaded)
|
||||
if player.is_idle() && state.player_state == PlayerState::Playing && state.current_duration > 0.0 {
|
||||
if state.playlist_index + 1 < state.playlist.len() {
|
||||
state.play_next();
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
}
|
||||
// Update metadata immediately when track changes
|
||||
player.update_metadata();
|
||||
metadata_update_counter = 0;
|
||||
state_changed = true;
|
||||
} else {
|
||||
state.player_state = PlayerState::Stopped;
|
||||
state_changed = true;
|
||||
}
|
||||
} else {
|
||||
state.player_state = PlayerState::Stopped;
|
||||
}
|
||||
}
|
||||
|
||||
terminal.draw(|f| ui::render(f, state))?;
|
||||
// Only redraw if something changed or forced
|
||||
if needs_redraw || state_changed {
|
||||
terminal.draw(|f| ui::render(f, state, player))?;
|
||||
needs_redraw = false;
|
||||
}
|
||||
|
||||
if event::poll(std::time::Duration::from_millis(100))? {
|
||||
// Poll for events - use longer timeout when stopped to reduce CPU
|
||||
let poll_duration = if state.player_state == PlayerState::Stopped {
|
||||
std::time::Duration::from_millis(200) // 5 FPS when stopped
|
||||
} else {
|
||||
std::time::Duration::from_millis(100) // 10 FPS when playing/paused
|
||||
};
|
||||
|
||||
if event::poll(poll_duration)? {
|
||||
if let Event::Key(key) = event::read()? {
|
||||
if key.kind == KeyEventKind::Press {
|
||||
handle_key_event(terminal, state, player, key).await?;
|
||||
needs_redraw = true; // Force redraw after key event
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -193,6 +239,7 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
// Keep playing
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata(); // Update metadata immediately
|
||||
tracing::info!("Next track: {:?}", path);
|
||||
}
|
||||
}
|
||||
@@ -200,6 +247,7 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
// Load but stay paused
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata(); // Update metadata immediately
|
||||
player.pause()?;
|
||||
tracing::info!("Next track (paused): {:?}", path);
|
||||
}
|
||||
@@ -222,6 +270,7 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
// Keep playing
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata(); // Update metadata immediately
|
||||
tracing::info!("Previous track: {:?}", path);
|
||||
}
|
||||
}
|
||||
@@ -229,6 +278,7 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
// Load but stay paused
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata(); // Update metadata immediately
|
||||
player.pause()?;
|
||||
tracing::info!("Previous track (paused): {:?}", path);
|
||||
}
|
||||
@@ -275,6 +325,7 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
state.play_selection();
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata(); // Update metadata immediately when playing new file
|
||||
tracing::info!("Playing: {:?} (playlist: {} tracks)", path, state.playlist.len());
|
||||
}
|
||||
if state.visual_mode {
|
||||
@@ -309,6 +360,7 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
state.player_state = PlayerState::Playing;
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata(); // Update metadata immediately
|
||||
tracing::info!("Restarting playback: {:?}", path);
|
||||
}
|
||||
}
|
||||
@@ -341,7 +393,7 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
}
|
||||
(KeyCode::Char('r'), _) => {
|
||||
state.is_refreshing = true;
|
||||
terminal.draw(|f| ui::render(f, state))?; // Show "Refreshing library..." immediately
|
||||
terminal.draw(|f| ui::render(f, state, player))?; // Show "Refreshing library..." immediately
|
||||
tracing::info!("Rescanning...");
|
||||
let cache_dir = cache::get_cache_dir()?;
|
||||
let new_cache = scanner::scan_paths(&state.config.scan_paths.paths)?;
|
||||
|
||||
Reference in New Issue
Block a user