mirror of https://github.com/jetkvm/kvm.git
Extract alsaDevice configuration to helper
This commit is contained in:
parent
7f930e01b3
commit
9b2500b2df
37
audio.go
37
audio.go
|
|
@ -28,6 +28,14 @@ var (
|
||||||
audioInputEnabled atomic.Bool
|
audioInputEnabled atomic.Bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getAlsaDevice(source string) string {
|
||||||
|
if source == "hdmi" {
|
||||||
|
return "hw:0,0"
|
||||||
|
} else {
|
||||||
|
return "hw:1,0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func initAudio() {
|
func initAudio() {
|
||||||
audioLogger = logging.GetDefaultLogger().With().Str("component", "audio-manager").Logger()
|
audioLogger = logging.GetDefaultLogger().With().Str("component", "audio-manager").Logger()
|
||||||
|
|
||||||
|
|
@ -40,7 +48,7 @@ func initAudio() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAudioConfig() audio.AudioConfig {
|
func getAudioConfig() audio.AudioConfig {
|
||||||
ensureConfigLoaded()
|
// config is already loaded
|
||||||
cfg := audio.DefaultAudioConfig()
|
cfg := audio.DefaultAudioConfig()
|
||||||
if config.AudioBitrate >= 64 && config.AudioBitrate <= 256 {
|
if config.AudioBitrate >= 64 && config.AudioBitrate <= 256 {
|
||||||
cfg.Bitrate = uint16(config.AudioBitrate)
|
cfg.Bitrate = uint16(config.AudioBitrate)
|
||||||
|
|
@ -73,11 +81,7 @@ func startAudio() error {
|
||||||
|
|
||||||
if outputSource == nil && audioOutputEnabled.Load() && currentAudioTrack != nil {
|
if outputSource == nil && audioOutputEnabled.Load() && currentAudioTrack != nil {
|
||||||
ensureConfigLoaded()
|
ensureConfigLoaded()
|
||||||
alsaDevice := "hw:1,0"
|
alsaDevice := getAlsaDevice(config.AudioOutputSource)
|
||||||
if config.AudioOutputSource == "hdmi" {
|
|
||||||
alsaDevice = "hw:0,0"
|
|
||||||
}
|
|
||||||
|
|
||||||
source := audio.NewCgoOutputSource(alsaDevice)
|
source := audio.NewCgoOutputSource(alsaDevice)
|
||||||
source.SetConfig(getAudioConfig())
|
source.SetConfig(getAudioConfig())
|
||||||
outputSource = source
|
outputSource = source
|
||||||
|
|
@ -89,8 +93,7 @@ func startAudio() error {
|
||||||
|
|
||||||
ensureConfigLoaded()
|
ensureConfigLoaded()
|
||||||
if inputSource.Load() == nil && audioInputEnabled.Load() && config.UsbDevices != nil && config.UsbDevices.Audio {
|
if inputSource.Load() == nil && audioInputEnabled.Load() && config.UsbDevices != nil && config.UsbDevices.Audio {
|
||||||
alsaPlaybackDevice := "hw:1,0"
|
alsaPlaybackDevice := getAlsaDevice("usb")
|
||||||
|
|
||||||
source := audio.NewCgoInputSource(alsaPlaybackDevice)
|
source := audio.NewCgoInputSource(alsaPlaybackDevice)
|
||||||
source.SetConfig(getAudioConfig())
|
source.SetConfig(getAudioConfig())
|
||||||
var audioSource audio.AudioSource = source
|
var audioSource audio.AudioSource = source
|
||||||
|
|
@ -175,11 +178,8 @@ func setAudioTrack(audioTrack *webrtc.TrackLocalStaticSample) {
|
||||||
var newSource audio.AudioSource
|
var newSource audio.AudioSource
|
||||||
if currentAudioTrack != nil && audioOutputEnabled.Load() {
|
if currentAudioTrack != nil && audioOutputEnabled.Load() {
|
||||||
ensureConfigLoaded()
|
ensureConfigLoaded()
|
||||||
alsaDevice := "hw:1,0"
|
alsaDevice := getAlsaDevice(config.AudioOutputSource)
|
||||||
if config.AudioOutputSource == "hdmi" {
|
newSource := audio.NewCgoOutputSource(alsaDevice)
|
||||||
alsaDevice = "hw:0,0"
|
|
||||||
}
|
|
||||||
newSource = audio.NewCgoOutputSource(alsaDevice)
|
|
||||||
newSource.SetConfig(getAudioConfig())
|
newSource.SetConfig(getAudioConfig())
|
||||||
newRelay = audio.NewOutputRelay(newSource, currentAudioTrack)
|
newRelay = audio.NewOutputRelay(newSource, currentAudioTrack)
|
||||||
outputSource = newSource
|
outputSource = newSource
|
||||||
|
|
@ -255,11 +255,7 @@ func SetAudioOutputSource(source string) error {
|
||||||
stopOutputAudio()
|
stopOutputAudio()
|
||||||
|
|
||||||
if audioOutputEnabled.Load() && activeConnections.Load() > 0 && currentAudioTrack != nil {
|
if audioOutputEnabled.Load() && activeConnections.Load() > 0 && currentAudioTrack != nil {
|
||||||
alsaDevice := "hw:1,0"
|
alsaDevice := getAlsaDevice(source)
|
||||||
if source == "hdmi" {
|
|
||||||
alsaDevice = "hw:0,0"
|
|
||||||
}
|
|
||||||
|
|
||||||
newSource := audio.NewCgoOutputSource(alsaDevice)
|
newSource := audio.NewCgoOutputSource(alsaDevice)
|
||||||
newSource.SetConfig(getAudioConfig())
|
newSource.SetConfig(getAudioConfig())
|
||||||
newRelay := audio.NewOutputRelay(newSource, currentAudioTrack)
|
newRelay := audio.NewOutputRelay(newSource, currentAudioTrack)
|
||||||
|
|
@ -291,10 +287,7 @@ func RestartAudioOutput() {
|
||||||
stopOutputAudio()
|
stopOutputAudio()
|
||||||
|
|
||||||
ensureConfigLoaded()
|
ensureConfigLoaded()
|
||||||
alsaDevice := "hw:1,0"
|
alsaDevice := getAlsaDevice(config.AudioOutputSource)
|
||||||
if config.AudioOutputSource == "hdmi" {
|
|
||||||
alsaDevice = "hw:0,0"
|
|
||||||
}
|
|
||||||
|
|
||||||
newSource := audio.NewCgoOutputSource(alsaDevice)
|
newSource := audio.NewCgoOutputSource(alsaDevice)
|
||||||
newSource.SetConfig(getAudioConfig())
|
newSource.SetConfig(getAudioConfig())
|
||||||
|
|
|
||||||
|
|
@ -926,7 +926,8 @@ func updateUsbRelatedConfig(wasAudioEnabled bool) error {
|
||||||
config.AudioOutputSource = "hdmi"
|
config.AudioOutputSource = "hdmi"
|
||||||
stopOutputAudio()
|
stopOutputAudio()
|
||||||
if audioOutputEnabled.Load() && activeConnections.Load() > 0 && currentAudioTrack != nil {
|
if audioOutputEnabled.Load() && activeConnections.Load() > 0 && currentAudioTrack != nil {
|
||||||
newSource := audio.NewCgoOutputSource("hw:0,0")
|
alsaDevice := getAlsaDevice("hdmi")
|
||||||
|
newSource := audio.NewCgoOutputSource(alsaDevice)
|
||||||
newSource.SetConfig(getAudioConfig())
|
newSource.SetConfig(getAudioConfig())
|
||||||
newRelay := audio.NewOutputRelay(newSource, currentAudioTrack)
|
newRelay := audio.NewOutputRelay(newSource, currentAudioTrack)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue