use std::sync::Arc;
use crate::{
player::MixerId,
source::{
measured::{CpuLoad, SharedCpuLoadState},
metered::{AudioLevel, SharedAudioLevelState},
},
};
#[derive(Clone)]
pub struct MixerHandle {
mixer_id: MixerId,
measurement_state: Option<SharedCpuLoadState>,
metering_state: Option<SharedAudioLevelState>,
}
impl MixerHandle {
pub(crate) fn new(
mixer_id: MixerId,
measurement_state: Option<SharedCpuLoadState>,
metering_state: Option<SharedAudioLevelState>,
) -> Self {
Self {
mixer_id,
measurement_state,
metering_state,
}
}
pub fn id(&self) -> MixerId {
self.mixer_id
}
pub fn cpu_load(&self) -> Option<CpuLoad> {
self.measurement_state
.as_ref()
.and_then(|s| s.try_lock().ok())
.map(|state| state.cpu_load())
}
pub fn cpu_load_state(&self) -> Option<SharedCpuLoadState> {
self.measurement_state.as_ref().map(Arc::clone)
}
pub fn audio_level(&self) -> Option<AudioLevel> {
self.metering_state
.as_ref()
.and_then(|s| s.try_lock().ok())
.map(|state| state.audio_level().clone())
}
pub fn audio_level_state(&self) -> Option<SharedAudioLevelState> {
self.metering_state.as_ref().map(Arc::clone)
}
}