Monitor MPD FIFO output status for audio sources

This commit is contained in:
maze committed 2026-10-29 17:38:07 +01:00
1 parent 1b2285fc91
commit f2c9aeb3a8
9 files changed
+159 -1

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
package mpd
import "github.com/BeatGlow/beatglow/config"
// SetAudioSource configures FIFO output monitoring before Events is called.
func (p *Player) SetAudioSource(source config.Audio) {
p.audioSource = source
}
func (p *Player) checkFIFOOutput(conn *clientConn) {
lines, err := p.sendDataCommand(conn, "outputs")
if err == nil {
err = fifoOutputState(parseOutputs(lines), p.audioSource.Output)
}
state := "enabled"
if err != nil {
state = err.Error()
}
if state == p.fifoState {
return
}
p.fifoState = state
log := conn.log().WithField("fifo", p.audioSource.Path).WithField("output", p.audioSource.Output)
if err != nil {
log.WithError(err).Warn("FIFO audio source may not receive samples")
} else {
log.Info("MPD FIFO output is enabled")
}
}
+61
View File
@@ -0,0 +1,61 @@
package mpd
import (
"fmt"
"strings"
)
type output struct {
name, plugin string
enabled bool
}
func parseOutputs(lines []string) []output {
var outputs []output
for _, line := range lines {
key, value, ok := strings.Cut(line, ": ")
if !ok {
continue
}
if key == "outputid" {
outputs = append(outputs, output{})
}
if len(outputs) == 0 {
continue
}
o := &outputs[len(outputs)-1]
switch key {
case "outputname":
o.name = value
case "plugin":
o.plugin = value
case "outputenabled":
o.enabled = value == "1"
}
}
return outputs
}
func fifoOutputState(outputs []output, name string) error {
var matches []output
for _, o := range outputs {
if name != "" {
if o.name == name && (o.plugin == "fifo" || o.plugin == "") {
matches = append(matches, o)
}
} else if o.plugin == "fifo" {
matches = append(matches, o)
}
}
switch len(matches) {
case 0:
return fmt.Errorf("no matching MPD FIFO output; check MPD configuration and audio.output")
case 1:
if !matches[0].enabled {
return fmt.Errorf("MPD FIFO output %q is disabled", matches[0].name)
}
return nil
default:
return fmt.Errorf("multiple matching MPD FIFO outputs; set audio.output to the output name")
}
}
+33
View File
@@ -0,0 +1,33 @@
package mpd
import (
"strings"
"testing"
)
func TestFIFOOutputState(t *testing.T) {
for _, tt := range []struct {
name, response, output, want string
}{
{"enabled", "outputid: 0\noutputname: DAC\nplugin: alsa\noutputenabled: 0\noutputid: 1\noutputname: Visualizer\nplugin: fifo\noutputenabled: 1", "", ""},
{"disabled", "outputid: 2\noutputname: Visualizer\nplugin: fifo\noutputenabled: 0", "", "disabled"},
{"missing", "outputid: 0\noutputname: DAC\nplugin: alsa\noutputenabled: 1", "", "no matching"},
{"ambiguous", "outputid: 1\noutputname: One\nplugin: fifo\noutputenabled: 1\noutputid: 2\noutputname: Two\nplugin: fifo\noutputenabled: 0", "", "multiple"},
{"select named disabled", "outputid: 1\noutputname: One\nplugin: fifo\noutputenabled: 1\noutputid: 2\noutputname: Two\nplugin: fifo\noutputenabled: 0", "Two", "disabled"},
{"legacy named", "outputid: 1\noutputname: Visualizer\noutputenabled: 1", "Visualizer", ""},
{"legacy unidentified", "outputid: 1\noutputname: Visualizer\noutputenabled: 1", "", "no matching"},
{"wrong plugin", "outputid: 1\noutputname: Visualizer\nplugin: alsa\noutputenabled: 1", "Visualizer", "no matching"},
{"empty", "", "", "no matching"},
} {
t.Run(tt.name, func(t *testing.T) {
err := fifoOutputState(parseOutputs(strings.Split(tt.response, "\n")), tt.output)
if tt.want == "" {
if err != nil {
t.Fatal(err)
}
} else if err == nil || !strings.Contains(err.Error(), tt.want) {
t.Fatalf("got %v, want error containing %q", err, tt.want)
}
})
}
}
+11
View File
@@ -33,6 +33,8 @@ type Player struct {
Version Version
name string
audioSource config.Audio
fifoState string
addr string
password string
idleSubsystems []string
@@ -226,6 +228,10 @@ func (p *Player) sendDataCommand(conn *clientConn, format string, args ...interf
if line == "OK" {
break
}
if strings.HasPrefix(line, "ACK ") {
err = textproto.ProtocolError(line)
break
}
lines = append(lines, line)
}
@@ -443,6 +449,7 @@ func (p *Player) mainLoop(conn *clientConn, events chan player.Event) {
defer ticker.Stop()
var err error
var outputCheck time.Time
loop:
for {
select {
@@ -469,6 +476,10 @@ loop:
}
case <-ticker.C:
if p.audioSource.Type == "fifo" && time.Since(outputCheck) >= 15*time.Second {
outputCheck = time.Now()
p.checkFIFOOutput(conn)
}
var status map[string]string
if status, err = p.sendMapCommand(conn, "status"); err != nil {
log.WithError(err).Error("error retrieving status")