mirror of https://bitbucket.org/ausocean/av.git
Merged in issue-399-software-channels (pull request #502)
Software Channels Approved-by: Trek Hopton
This commit is contained in:
commit
47b2b1576f
|
@ -35,7 +35,7 @@ import (
|
|||
"io"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"sync"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bitbucket.org/ausocean/av/container/mts"
|
||||
|
@ -78,16 +78,17 @@ const (
|
|||
i2cPort = 1
|
||||
)
|
||||
|
||||
// Treatment modes.
|
||||
// Channel modes.
|
||||
const (
|
||||
modePaused = "Paused"
|
||||
modeTreatment = "Play"
|
||||
modeCheck = "Check"
|
||||
modeStereo = "Stereo"
|
||||
modeLeft = "LeftMono"
|
||||
modeRight = "RightMono"
|
||||
modeMute = "Mute"
|
||||
)
|
||||
|
||||
// Variable map to send to netreceiver/vidgrind.
|
||||
var varMap = map[string]string{
|
||||
"mode": "enum:Paused,Play,Check",
|
||||
"SpeakerMode": "enum:"+strings.Join([]string{modeStereo, modeLeft, modeRight, modeMute}, ","),
|
||||
"AudioFilePath": "string",
|
||||
}
|
||||
|
||||
|
@ -131,6 +132,10 @@ func main() {
|
|||
log.Fatal("could not initialise revid", "error", err)
|
||||
}
|
||||
|
||||
// Play the audio (audio will play even while muted).
|
||||
log.Debug("Playing the audio")
|
||||
go playAudio(filePtr, log)
|
||||
|
||||
// Start the control loop.
|
||||
log.Debug("starting control loop")
|
||||
run(rv, ns, filePtr, log, netLog)
|
||||
|
@ -139,12 +144,7 @@ func main() {
|
|||
// 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)
|
||||
func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger, nl *netlogger.Logger) {
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
audioQuit chan struct{}
|
||||
treating bool
|
||||
vs int
|
||||
)
|
||||
var vs int
|
||||
|
||||
for {
|
||||
l.Debug("running netsender")
|
||||
|
@ -207,50 +207,80 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger,
|
|||
}
|
||||
|
||||
l.Debug("checking mode")
|
||||
switch ns.Mode() {
|
||||
case modePaused:
|
||||
stopAudio(&wg, &treating, audioQuit)
|
||||
l.Info("mode is Paused, stopping revid")
|
||||
rv.Stop()
|
||||
case modeTreatment:
|
||||
l.Debug("checking audio file path")
|
||||
f := vars["AudioFilePath"]
|
||||
if f != "" && *file != f {
|
||||
file = &f
|
||||
l.Info("updated audio file path, stopping audio", "AudioFilePath", f)
|
||||
stopAudio(&wg, &treating, audioQuit)
|
||||
}
|
||||
if !treating {
|
||||
l.Info("starting audio treatment")
|
||||
rv.Stop()
|
||||
audioQuit = make(chan struct{})
|
||||
treating = true
|
||||
wg.Add(1)
|
||||
go playAudio(file, audioQuit, &wg, l)
|
||||
}
|
||||
case modeCheck:
|
||||
stopAudio(&wg, &treating, audioQuit)
|
||||
l.Info("sound checking")
|
||||
err = rv.Start()
|
||||
if err != nil {
|
||||
l.Error("could not start revid", "error", err)
|
||||
ns.SetMode(modePaused, &vs)
|
||||
sleep(ns, l)
|
||||
continue
|
||||
}
|
||||
default:
|
||||
l.Warning("mode is not valid", "mode", ns.Mode())
|
||||
|
||||
}
|
||||
l.Info("revid updated with new mode")
|
||||
_ = setChannels(vars["SpeakerMode"], l)
|
||||
sleep(ns, l)
|
||||
}
|
||||
}
|
||||
|
||||
// playAudio is intended to be run as a routine. It will repeatedly play an audio file until
|
||||
// a signal is received to return. The entire audio file is played before the termination
|
||||
// signal chan is checked.
|
||||
func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l logging.Logger) {
|
||||
// setChannels handles the muting of one, both, or neither of the channels. It takes in SpeakerMode
|
||||
// and sets the relevant volumes.
|
||||
func setChannels(mode string, l logging.Logger) error {
|
||||
l.Info("mode is", "mode", mode)
|
||||
|
||||
// Set the volume of each channel.
|
||||
vols := map[string]string{
|
||||
modeStereo: "100%,100%",
|
||||
modeLeft: "0%,100%",
|
||||
modeRight: "100%,0%",
|
||||
modeMute: "0%,0%",
|
||||
}[mode]
|
||||
if vols == "" {
|
||||
l.Warning("invalid SpeakeMode", "SpeakerMode", mode)
|
||||
return fmt.Errorf("invalid SpeakerMode: %s", mode)
|
||||
}
|
||||
|
||||
// Create the command to change the channel volumes.
|
||||
cmd := exec.Command("amixer", "sset", "Speaker", vols)
|
||||
|
||||
// Pipe the output to stdout and stderr.
|
||||
outPipe, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
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()
|
||||
if err != nil {
|
||||
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.
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
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.
|
||||
var outBuff bytes.Buffer
|
||||
go func() {
|
||||
_, err = io.Copy(&outBuff, outPipe)
|
||||
if err != nil {
|
||||
l.Error("failed to copy out pipe", "error", err)
|
||||
}
|
||||
l.Info("command run", "stdout", outBuff)
|
||||
}()
|
||||
|
||||
// Copy any std error to a buffer for logging.
|
||||
var errBuff bytes.Buffer
|
||||
go func() {
|
||||
_, err = io.Copy(&errBuff, errPipe)
|
||||
if err != nil {
|
||||
l.Error("failed to copy error pipe", "error", err)
|
||||
}
|
||||
l.Error("command failed", "stderr", errBuff)
|
||||
}()
|
||||
if errBuff.String() != "" {
|
||||
return fmt.Errorf("channel set command failed: %s", &errBuff)
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
var numPlays int
|
||||
for {
|
||||
cmd := exec.Command(audioCmd, *file)
|
||||
|
@ -303,28 +333,7 @@ func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l logging.L
|
|||
if errBuff.Len() != 0 {
|
||||
l.Error("errors from stderr", "stderr", string(errBuff.Bytes()))
|
||||
}
|
||||
|
||||
// Check for audio signal halt.
|
||||
// TODO: work out better way to do this. Doing it this way means we have to wait for
|
||||
// the audio file to finish playing.
|
||||
select {
|
||||
case <-quit:
|
||||
wg.Done()
|
||||
return
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// stopAudio signals to the playAudio routine to terminate and then waits for it to
|
||||
// do so.
|
||||
func stopAudio(wg *sync.WaitGroup, treating *bool, signal chan struct{}) {
|
||||
if !*treating {
|
||||
return
|
||||
}
|
||||
close(signal)
|
||||
wg.Wait()
|
||||
*treating = false
|
||||
}
|
||||
|
||||
// sleep uses a delay to halt the program based on the monitoring period
|
||||
|
|
Loading…
Reference in New Issue