Fix USB audio channels and remove redundant synchronization

USB audio configuration:
- Set playback to mono (microphone input from remote PC)
- Set capture to stereo (audio output to remote PC)
- Fixes audio input initialization failures and stereo output

Audio management optimizations:
- Remove redundant mutex in stopInputAudio (C layer provides synchronization)
- Remove unnecessary 100ms delay when switching audio sources
- Simplify error handling (Disconnect is idempotent)
- Remove time import (no longer needed)
This commit is contained in:
Alex P 2025-11-18 14:25:33 +02:00
parent 0022599b03
commit 478302144f
2 changed files with 4 additions and 12 deletions

View File

@ -4,7 +4,6 @@ import (
"io"
"sync"
"sync/atomic"
"time"
"github.com/jetkvm/kvm/internal/audio"
"github.com/jetkvm/kvm/internal/logging"
@ -49,7 +48,6 @@ func initAudio() {
}
func getAudioConfig() audio.AudioConfig {
// config is already loaded
cfg := audio.DefaultAudioConfig()
if config.AudioBitrate >= 64 && config.AudioBitrate <= 256 {
cfg.Bitrate = uint16(config.AudioBitrate)
@ -160,9 +158,7 @@ func stopInputAudio() {
inRelay.Stop()
}
if inSource != nil {
inputSourceMutex.Lock()
(*inSource).Disconnect()
inputSourceMutex.Unlock()
}
}
@ -252,8 +248,6 @@ func SetAudioOutputSource(source string) error {
stopOutputAudio()
config.AudioOutputSource = source
time.Sleep(100 * time.Millisecond)
if err := startAudio(); err != nil {
audioLogger.Error().Err(err).Str("source", source).Msg("Failed to start audio output after source change")
}
@ -328,10 +322,8 @@ func handleInputTrackForSession(track *webrtc.TrackRemote) {
}
if err := (*source).WriteMessage(0, opusData); err != nil {
if inputSource.Load() == source {
audioLogger.Warn().Err(err).Msg("failed to write audio message")
(*source).Disconnect()
}
audioLogger.Warn().Err(err).Msg("failed to write audio message")
(*source).Disconnect()
}
inputSourceMutex.Unlock()

View File

@ -66,11 +66,11 @@ var defaultGadgetConfig = map[string]gadgetConfigItem{
path: []string{"functions", "uac1.usb0"},
configPath: []string{"uac1.usb0"},
attrs: gadgetAttributes{
"p_chmask": "3", // Playback: stereo (2 channels)
"p_chmask": "1", // Playback: mono (1 channel for microphone)
"p_srate": "48000", // Playback: 48kHz sample rate
"p_ssize": "2", // Playback: 16-bit (2 bytes)
"p_volume_present": "1", // Playback: enable volume control
"c_chmask": "1", // Capture: mono (1 channel)
"c_chmask": "3", // Capture: stereo (2 channels for HDMI audio)
"c_srate": "48000", // Capture: 48kHz sample rate
"c_ssize": "2", // Capture: 16-bit (2 bytes)
"c_volume_present": "0", // Capture: no volume control