fix: improve microphone handling and cleanup

- Use mono audio instead of stereo for microphone input
- Add cleanup to stop audio track on component unmount
- Explicitly set AudioTrack to nil when creation fails
This commit is contained in:
Alex P 2025-11-09 13:39:38 +02:00
parent 41345b0527
commit d1824c5727
2 changed files with 9 additions and 1 deletions

View File

@ -620,7 +620,7 @@ export default function KvmIdRoute() {
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true,
channelCount: 2,
channelCount: 1,
}
}).then((stream) => {
microphoneRequestInProgress.current = false;
@ -652,6 +652,13 @@ export default function KvmIdRoute() {
audioTransceiver.sender.replaceTrack(null);
}
}
// Cleanup on unmount or when dependencies change
return () => {
if (audioTransceiver?.sender.track) {
audioTransceiver.sender.track.stop();
}
};
}, [microphoneEnabled, audioTransceiver, peerConnection, setMicrophoneEnabled]);
// Cleanup effect

View File

@ -332,6 +332,7 @@ func newSession(config SessionConfig) (*Session, error) {
)
if err != nil {
scopedLogger.Warn().Err(err).Msg("Failed to create AudioTrack (non-fatal)")
session.AudioTrack = nil
} else {
_, err = peerConnection.AddTransceiverFromTrack(session.AudioTrack, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendrecv,