Synchronize connection-owned stream access

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-10 11:25:08 -04:00
committed by Brandon McGinty
parent f0aa5ff35a
commit e9ba7f580a
6 changed files with 133 additions and 84 deletions
+52 -31
View File
@@ -129,7 +129,9 @@ func (b *Barnard) OnTimestampToggle(ui *uiterm.Ui, key uiterm.Key) {
func (b *Barnard) OnNoiseSuppressionToggle(ui *uiterm.Ui, key uiterm.Key) {
enabled := !b.UserConfig.GetNoiseSuppressionEnabled()
b.UserConfig.SetNoiseSuppressionEnabled(enabled)
if err := b.UserConfig.SetNoiseSuppressionEnabled(enabled); err != nil {
b.AddOutputLine("Noise suppression: could not save setting: " + err.Error())
}
b.NoiseSuppressor.SetEnabled(enabled)
if enabled {
@@ -198,7 +200,9 @@ func (b *Barnard) CommandMicDown(ui *uiterm.Ui, cmd string) {
func (b *Barnard) CommandNoiseSuppressionToggle(ui *uiterm.Ui, cmd string) {
enabled := !b.UserConfig.GetNoiseSuppressionEnabled()
b.UserConfig.SetNoiseSuppressionEnabled(enabled)
if err := b.UserConfig.SetNoiseSuppressionEnabled(enabled); err != nil {
b.AddOutputLine("Noise suppression: could not save setting: " + err.Error())
}
b.NoiseSuppressor.SetEnabled(enabled)
if enabled {
@@ -266,11 +270,18 @@ func (b *Barnard) CommandPlayFile(ui *uiterm.Ui, cmd string) {
// Enable stereo encoder for file playback
b.Client.EnableStereoEncoder()
// Auto-start transmission if not already transmitting
if !b.isTransmitting() && b.Stream != nil {
err := b.Stream.StartSource(b.UserConfig.GetInputDevice())
if err != nil {
b.AddOutputLine(fmt.Sprintf("Error starting transmission: %s", err.Error()))
// Auto-start transmission if not already transmitting. FileStreamMutex is
// held here, before withStream's connection mutex, matching cleanup.
if !b.isTransmitting() {
var startErr error
started := b.withStream(func(stream *gumbleopenal.Stream) {
startErr = stream.StartSource(b.UserConfig.GetInputDevice())
})
if !started {
startErr = errors.New("audio unavailable while reconnecting")
}
if startErr != nil {
b.AddOutputLine(fmt.Sprintf("Error starting transmission: %s", startErr))
b.FileStream.Stop()
b.Client.DisableStereoEncoder()
return
@@ -327,8 +338,8 @@ func (b *Barnard) setTransmit(ui *uiterm.Ui, val int) {
close(b.toneTestStop)
b.toneTestStop = nil
}
} else if b.Stream != nil {
b.Stream.StopSource()
} else {
b.withStream(func(stream *gumbleopenal.Stream) { _ = stream.StopSource() })
}
} else if !b.isConnected() {
b.Notify("error", "me", "no tx while disconnected")
@@ -347,20 +358,26 @@ func (b *Barnard) setTransmit(ui *uiterm.Ui, val int) {
b.Notify("micup", "me", "")
b.UpdateGeneralStatus(" Tx ", true)
} else {
err := b.Stream.StartSource(b.UserConfig.GetInputDevice())
if err != nil {
b.setTransmitting(false)
if fatalAudioOpenError(err) {
// A missing capture device cannot recover through normal
// transmission controls; exit so option 1 reports it on stderr.
b.exitWithError(fmt.Errorf("audio device initialization failed: %w", err))
started := b.withStream(func(stream *gumbleopenal.Stream) {
err := stream.StartSource(b.UserConfig.GetInputDevice())
if err != nil {
b.setTransmitting(false)
if fatalAudioOpenError(err) {
// A missing capture device cannot recover through normal
// transmission controls; exit so option 1 reports it on stderr.
b.exitWithError(fmt.Errorf("audio device initialization failed: %w", err))
return
}
b.Notify("error", "me", err.Error())
b.UpdateGeneralStatus(err.Error(), true)
return
}
b.Notify("error", "me", err.Error())
b.UpdateGeneralStatus(err.Error(), true)
} else {
b.Notify("micup", "me", "")
b.UpdateGeneralStatus(" Tx ", true)
})
if !started {
b.setTransmitting(false)
b.UpdateGeneralStatus("audio unavailable while reconnecting", true)
}
}
}
@@ -371,25 +388,29 @@ func fatalAudioOpenError(err error) bool {
}
func (b *Barnard) OnMicVolumeDown(ui *uiterm.Ui, key uiterm.Key) {
if b.ToneTest || b.Stream == nil {
if b.ToneTest {
return
}
b.Stream.SetMicVolume(-0.1, true)
b.UserConfig.SetMicVolume(b.Stream.GetMicVolume())
if err := b.UserConfig.SaveConfig(); err != nil {
b.AddOutputLine("Microphone: could not save volume: " + err.Error())
}
b.withStream(func(stream *gumbleopenal.Stream) {
stream.SetMicVolume(-0.1, true)
b.UserConfig.SetMicVolume(stream.GetMicVolume())
if err := b.UserConfig.SaveConfig(); err != nil {
b.AddOutputLine("Microphone: could not save volume: " + err.Error())
}
})
}
func (b *Barnard) OnMicVolumeUp(ui *uiterm.Ui, key uiterm.Key) {
if b.ToneTest || b.Stream == nil {
if b.ToneTest {
return
}
b.Stream.SetMicVolume(0.1, true)
b.UserConfig.SetMicVolume(b.Stream.GetMicVolume())
if err := b.UserConfig.SaveConfig(); err != nil {
b.AddOutputLine("Microphone: could not save volume: " + err.Error())
}
b.withStream(func(stream *gumbleopenal.Stream) {
stream.SetMicVolume(0.1, true)
b.UserConfig.SetMicVolume(stream.GetMicVolume())
if err := b.UserConfig.SaveConfig(); err != nil {
b.AddOutputLine("Microphone: could not save volume: " + err.Error())
}
})
}
func (b *Barnard) OnQuitPress(ui *uiterm.Ui, key uiterm.Key) {