mirror of https://github.com/jetkvm/kvm.git
[WIP] Improvements: Improve audio resume mechanism after Hardware Settings deactivation & reactivation
This commit is contained in:
parent
f2ad918dfd
commit
dec0b9d3db
|
@ -36,9 +36,6 @@ export function useMicrophone() {
|
||||||
// Track microphone state when USB audio gets disabled, so we can restore it when re-enabled
|
// Track microphone state when USB audio gets disabled, so we can restore it when re-enabled
|
||||||
const [microphoneWasActiveBeforeUsbDisable, setMicrophoneWasActiveBeforeUsbDisable] = useState<boolean>(false);
|
const [microphoneWasActiveBeforeUsbDisable, setMicrophoneWasActiveBeforeUsbDisable] = useState<boolean>(false);
|
||||||
|
|
||||||
// Track previous USB audio state to detect changes
|
|
||||||
const prevUsbAudioEnabled = useRef<boolean | null>(null);
|
|
||||||
|
|
||||||
// RPC helper functions to replace HTTP API calls
|
// RPC helper functions to replace HTTP API calls
|
||||||
const rpcMicrophoneStart = useCallback((): Promise<void> => {
|
const rpcMicrophoneStart = useCallback((): Promise<void> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -597,17 +594,13 @@ export function useMicrophone() {
|
||||||
return () => clearTimeout(timer);
|
return () => clearTimeout(timer);
|
||||||
}, [syncMicrophoneState, microphoneWasEnabled, isMicrophoneActive, peerConnection, startMicrophone, rpcDataChannel?.readyState]);
|
}, [syncMicrophoneState, microphoneWasEnabled, isMicrophoneActive, peerConnection, startMicrophone, rpcDataChannel?.readyState]);
|
||||||
|
|
||||||
// Handle USB audio enable/disable changes
|
// Handle audio device changes (USB audio enable/disable) via WebSocket events
|
||||||
useEffect(() => {
|
const handleAudioDeviceChanged = useCallback((data: AudioDeviceChangedData) => {
|
||||||
// If this is the first run, just store the current state
|
devInfo("Audio device changed:", data);
|
||||||
if (prevUsbAudioEnabled.current === null) {
|
|
||||||
prevUsbAudioEnabled.current = isUsbAudioEnabled;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// USB audio was just disabled
|
// USB audio was just disabled
|
||||||
if (prevUsbAudioEnabled.current && !isUsbAudioEnabled) {
|
if (!data.enabled && data.reason === "usb_reconfiguration") {
|
||||||
devInfo("USB audio disabled - storing microphone state");
|
devInfo("USB audio disabled via device change event - storing microphone state");
|
||||||
setMicrophoneWasActiveBeforeUsbDisable(isMicrophoneActive);
|
setMicrophoneWasActiveBeforeUsbDisable(isMicrophoneActive);
|
||||||
// Clear the enabled flag to prevent auto-restore attempts
|
// Clear the enabled flag to prevent auto-restore attempts
|
||||||
if (isMicrophoneActive) {
|
if (isMicrophoneActive) {
|
||||||
|
@ -616,8 +609,8 @@ export function useMicrophone() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// USB audio was just re-enabled
|
// USB audio was just re-enabled
|
||||||
else if (!prevUsbAudioEnabled.current && isUsbAudioEnabled) {
|
else if (data.enabled && data.reason === "usb_reconfiguration") {
|
||||||
devInfo("USB audio re-enabled - checking if microphone should be restored");
|
devInfo("USB audio re-enabled via device change event - checking if microphone should be restored");
|
||||||
|
|
||||||
// If microphone was active before USB was disabled, restore it
|
// If microphone was active before USB was disabled, restore it
|
||||||
if (microphoneWasActiveBeforeUsbDisable && !isMicrophoneActive && rpcDataChannel?.readyState === "open") {
|
if (microphoneWasActiveBeforeUsbDisable && !isMicrophoneActive && rpcDataChannel?.readyState === "open") {
|
||||||
|
@ -640,9 +633,10 @@ export function useMicrophone() {
|
||||||
// Clear the stored state
|
// Clear the stored state
|
||||||
setMicrophoneWasActiveBeforeUsbDisable(false);
|
setMicrophoneWasActiveBeforeUsbDisable(false);
|
||||||
}
|
}
|
||||||
|
}, [isMicrophoneActive, microphoneWasActiveBeforeUsbDisable, startMicrophone, setMicrophoneWasEnabled, rpcDataChannel?.readyState]);
|
||||||
|
|
||||||
prevUsbAudioEnabled.current = isUsbAudioEnabled;
|
// Subscribe to audio device change events
|
||||||
}, [isUsbAudioEnabled, isMicrophoneActive, microphoneWasActiveBeforeUsbDisable, startMicrophone, setMicrophoneWasEnabled, rpcDataChannel?.readyState]);
|
useAudioEvents(handleAudioDeviceChanged);
|
||||||
|
|
||||||
// Cleanup on unmount - use ref to avoid dependency on stopMicrophoneStream
|
// Cleanup on unmount - use ref to avoid dependency on stopMicrophoneStream
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
Loading…
Reference in New Issue