Better handle errors in setChannels

This commit is contained in:
David Sutton 2023-06-01 09:30:55 +09:30
parent 701b1f6c3a
commit bbbd6baecb
1 changed files with 30 additions and 27 deletions

View File

@ -35,6 +35,7 @@ import (
"io" "io"
"os/exec" "os/exec"
"strconv" "strconv"
"strings"
"time" "time"
"bitbucket.org/ausocean/av/container/mts" "bitbucket.org/ausocean/av/container/mts"
@ -87,7 +88,7 @@ const (
// Variable map to send to netreceiver/vidgrind. // Variable map to send to netreceiver/vidgrind.
var varMap = map[string]string{ var varMap = map[string]string{
"speakerMode": "enum:Stereo,LeftMono,RightMono,Mute", "SpeakerMode": "enum:"+strings.Join([]string{modeStereo, modeLeft, modeRight, modeMute}, ","),
"AudioFilePath": "string", "AudioFilePath": "string",
} }
@ -131,7 +132,7 @@ func main() {
log.Fatal("could not initialise revid", "error", err) log.Fatal("could not initialise revid", "error", err)
} }
// Play the audio (audio will play even whilst muted). // Play the audio (audio will play even while muted).
log.Debug("Playing the audio") log.Debug("Playing the audio")
go playAudio(filePtr, log) go playAudio(filePtr, log)
@ -143,9 +144,7 @@ func main() {
// run starts a control loop that runs netsender, sends logs, checks for var changes, and // run starts a control loop that runs netsender, sends logs, checks for var changes, and
// if var changes, changes current mode (paused,audio playback or soundcheck) // if var changes, changes current mode (paused,audio playback or soundcheck)
func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger, nl *netlogger.Logger) { func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger, nl *netlogger.Logger) {
var ( var vs int
vs int
)
for { for {
l.Debug("running netsender") l.Debug("running netsender")
@ -208,32 +207,26 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger,
} }
l.Debug("checking mode") l.Debug("checking mode")
setChannels(vars["speakerMode"], l) _ = setChannels(vars["SpeakerMode"], l)
// l.Info("revid updated with new mode")
sleep(ns, l) sleep(ns, l)
} }
} }
// setChannels handles the muting of one, both, or neither of the channels. It takes in speakerMode // setChannels handles the muting of one, both, or neither of the channels. It takes in SpeakerMode
// and sets the relevant volumes. // and sets the relevant volumes.
func setChannels(mode string, l logging.Logger) { func setChannels(mode string, l logging.Logger) error {
vols := ""
l.Info("mode is", "mode", mode) l.Info("mode is", "mode", mode)
// Set the volume of each channel. // Set the volume of each channel.
switch mode { vols := map[string]string{
case modeStereo: modeStereo: "100%,100%",
vols = "100%,100%" modeLeft: "0%,100%",
case modeLeft: modeRight: "100%,0%",
vols = "0%,100%" modeMute: "0%,0%",
case modeRight: }[mode]
vols = "100%,0%" if vols == "" {
case modeMute: l.Warning("invalid SpeakeMode", "SpeakerMode", mode)
vols = "0%,0%" return fmt.Errorf("invalid SpeakerMode: %s", mode)
default:
// If an invalid option has been chosen, keep the previous setting.
return
} }
// Create the command to change the channel volumes. // Create the command to change the channel volumes.
@ -242,17 +235,20 @@ func setChannels(mode string, l logging.Logger) {
// Pipe the output to stdout and stderr. // Pipe the output to stdout and stderr.
outPipe, err := cmd.StdoutPipe() outPipe, err := cmd.StdoutPipe()
if err != nil { if err != nil {
l.Error("failed to pipe stdout", "error", err) l.Error("unable to setup pipe to stdout", "error", err)
return fmt.Errorf("unable to setup pipe to stdout: %w", err)
} }
errPipe, err := cmd.StderrPipe() errPipe, err := cmd.StderrPipe()
if err != nil { if err != nil {
l.Error("failed to pipe stderr", "error", err) l.Error("unable to setup pipe to stderr", "error", err)
return fmt.Errorf("unable to setup pipe to stderr: %w", err)
} }
// Execute the channel setting command. // Execute the channel setting command.
err = cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
l.Error("channel setting failed", "error", err) l.Error("unable to set channel", "error", err)
return fmt.Errorf("unable to set channel: %w", err)
} }
// Copy any std out to a buffer for logging. // Copy any std out to a buffer for logging.
@ -262,6 +258,7 @@ func setChannels(mode string, l logging.Logger) {
if err != nil { if err != nil {
l.Error("failed to copy out pipe", "error", err) l.Error("failed to copy out pipe", "error", err)
} }
l.Info("command run", "stdout", outBuff)
}() }()
// Copy any std error to a buffer for logging. // Copy any std error to a buffer for logging.
@ -271,12 +268,18 @@ func setChannels(mode string, l logging.Logger) {
if err != nil { if err != nil {
l.Error("failed to copy error pipe", "error", err) l.Error("failed to copy error pipe", "error", err)
} }
l.Error("command failed", "stderr", errBuff)
}() }()
if errBuff.String() != "" {
l.Info("mode set to", "mode", mode) return fmt.Errorf("channel set command failed: %s", &errBuff)
} }
// playAudio is intended to be run as a routine. It will continuously run even whilst muted.
l.Info("mode set to", "mode", mode)
return nil
}
// playAudio is intended to be run as a routine. It will continuously run even while muted.
func playAudio(file *string, l logging.Logger) { func playAudio(file *string, l logging.Logger) {
var numPlays int var numPlays int
for { for {