perf(player): lazy init and release audio stream
This commit is contained in:
1 parent
3226dc251a
commit
bb44dd96cd
1 file changed
+33
-24
+33
-24
@@ -454,36 +454,13 @@ impl Player {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialize audio device with retry logic
|
// Initialize audio device lazily on first playback to avoid idle CPU usage.
|
||||||
// PipeWire/ALSA may need time to initialize at app startup
|
|
||||||
let mut current_device_name = device_name.clone();
|
let mut current_device_name = device_name.clone();
|
||||||
let mut stream_opt: Option<(OutputStream, rodio::OutputStreamHandle)> = None;
|
let mut stream_opt: Option<(OutputStream, rodio::OutputStreamHandle)> = None;
|
||||||
|
|
||||||
const MAX_INIT_RETRIES: u32 = 5;
|
const MAX_INIT_RETRIES: u32 = 5;
|
||||||
const RETRY_DELAY_MS: u64 = 500;
|
const RETRY_DELAY_MS: u64 = 500;
|
||||||
|
|
||||||
for attempt in 1..=MAX_INIT_RETRIES {
|
|
||||||
log::info!("Audio device initialization attempt {}/{}", attempt, MAX_INIT_RETRIES);
|
|
||||||
|
|
||||||
stream_opt = init_device(¤t_device_name, &thread_state);
|
|
||||||
|
|
||||||
if stream_opt.is_some() {
|
|
||||||
log::info!("Audio device initialized successfully on attempt {}", attempt);
|
|
||||||
thread_state.set_stream_error(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if attempt < MAX_INIT_RETRIES {
|
|
||||||
log::warn!("Audio init failed, retrying in {}ms...", RETRY_DELAY_MS);
|
|
||||||
std::thread::sleep(Duration::from_millis(RETRY_DELAY_MS));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if stream_opt.is_none() {
|
|
||||||
log::error!("Failed to initialize audio after {} attempts. Audio will not work until device is reinitialized.", MAX_INIT_RETRIES);
|
|
||||||
thread_state.set_stream_error(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut current_sink: Option<Sink> = None;
|
let mut current_sink: Option<Sink> = None;
|
||||||
// Store audio data for seeking (we need to re-decode from the beginning)
|
// Store audio data for seeking (we need to re-decode from the beginning)
|
||||||
let mut current_audio_data: Option<Vec<u8>> = None;
|
let mut current_audio_data: Option<Vec<u8>> = None;
|
||||||
@@ -503,6 +480,36 @@ impl Player {
|
|||||||
AudioCommand::Play { data, track_id, duration_secs } => {
|
AudioCommand::Play { data, track_id, duration_secs } => {
|
||||||
log::info!("Audio thread: playing track {}", track_id);
|
log::info!("Audio thread: playing track {}", track_id);
|
||||||
|
|
||||||
|
if stream_opt.is_none() {
|
||||||
|
for attempt in 1..=MAX_INIT_RETRIES {
|
||||||
|
log::info!(
|
||||||
|
"Audio device init on playback attempt {}/{}",
|
||||||
|
attempt,
|
||||||
|
MAX_INIT_RETRIES
|
||||||
|
);
|
||||||
|
*stream_opt = init_device(current_device_name, &thread_state);
|
||||||
|
if stream_opt.is_some() {
|
||||||
|
thread_state.set_stream_error(false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if attempt < MAX_INIT_RETRIES {
|
||||||
|
log::warn!(
|
||||||
|
"Audio init failed, retrying in {}ms...",
|
||||||
|
RETRY_DELAY_MS
|
||||||
|
);
|
||||||
|
std::thread::sleep(Duration::from_millis(RETRY_DELAY_MS));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if stream_opt.is_none() {
|
||||||
|
log::error!(
|
||||||
|
"Failed to initialize audio after {} attempts.",
|
||||||
|
MAX_INIT_RETRIES
|
||||||
|
);
|
||||||
|
thread_state.set_stream_error(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let Some(ref stream) = *stream_opt else {
|
let Some(ref stream) = *stream_opt else {
|
||||||
log::error!("Audio thread: no audio device available");
|
log::error!("Audio thread: no audio device available");
|
||||||
return;
|
return;
|
||||||
@@ -612,6 +619,8 @@ impl Player {
|
|||||||
thread_state.position.store(0, Ordering::SeqCst);
|
thread_state.position.store(0, Ordering::SeqCst);
|
||||||
thread_state.playback_start_millis.store(0, Ordering::SeqCst);
|
thread_state.playback_start_millis.store(0, Ordering::SeqCst);
|
||||||
thread_state.position_at_start.store(0, Ordering::SeqCst);
|
thread_state.position_at_start.store(0, Ordering::SeqCst);
|
||||||
|
// Drop the stream to release the device and stop background CPU use.
|
||||||
|
drop(stream_opt.take());
|
||||||
log::info!("Audio thread: stopped");
|
log::info!("Audio thread: stopped");
|
||||||
}
|
}
|
||||||
AudioCommand::SetVolume(volume) => {
|
AudioCommand::SetVolume(volume) => {
|
||||||
|
|||||||
Reference in new issue
Block a user