From 741b64ba6989addaa6b22d496a5e62d2bd3c6ae7 Mon Sep 17 00:00:00 2001 From: lars-berger Date: Sat, 30 Nov 2024 20:35:13 +0800 Subject: [PATCH] wip --- .../src/providers/audio/audio_provider.rs | 20 ++++++++++++++++--- .../desktop/src/providers/provider_manager.rs | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/desktop/src/providers/audio/audio_provider.rs b/packages/desktop/src/providers/audio/audio_provider.rs index 5d72c4a8..037c9c87 100644 --- a/packages/desktop/src/providers/audio/audio_provider.rs +++ b/packages/desktop/src/providers/audio/audio_provider.rs @@ -1,4 +1,7 @@ -use std::collections::HashMap; +use std::{ + collections::HashMap, + time::{Duration, Instant}, +}; use anyhow::Context; use crossbeam::channel; @@ -95,7 +98,6 @@ struct DeviceState { device_id: String, device_type: DeviceType, volume: u32, - com_device: IMMDevice, com_volume: IAudioEndpointVolume, com_volume_callback: IAudioEndpointVolumeCallback, } @@ -103,6 +105,8 @@ struct DeviceState { pub struct AudioProvider { common: CommonProviderState, com_enumerator: Option, + last_emit: Instant, + pending_emission: bool, default_playback_id: Option, default_recording_id: Option, device_states: HashMap, @@ -120,6 +124,8 @@ impl AudioProvider { Self { common, com_enumerator: None, + last_emit: Instant::now(), + pending_emission: false, default_playback_id: None, default_recording_id: None, device_states: HashMap::new(), @@ -191,6 +197,13 @@ impl AudioProvider { _ => {} } } + default(Duration::from_millis(20)) => { + // Batch emissions to reduce overhead. + if self.pending_emission { + self.emit_output(); + self.pending_emission = false; + } + } } } @@ -296,6 +309,8 @@ impl AudioProvider { } self.common.emitter.emit_output(Ok(output)); + self.last_emit = Instant::now(); + self.pending_emission = true; } /// Gets the default device ID for the given device type. @@ -358,7 +373,6 @@ impl AudioProvider { device_id: device_id.clone(), device_type: device_type.clone(), volume: (volume * 100.0).round() as u32, - com_device, com_volume, com_volume_callback, }; diff --git a/packages/desktop/src/providers/provider_manager.rs b/packages/desktop/src/providers/provider_manager.rs index 3f547ebd..186a5a4c 100644 --- a/packages/desktop/src/providers/provider_manager.rs +++ b/packages/desktop/src/providers/provider_manager.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, sync::Arc}; -use anyhow::{bail, Context}; +use anyhow::Context; use serde::{ser::SerializeStruct, Serialize}; use tauri::{AppHandle, Emitter}; use tokio::{