revid, alsa: reorder audio setup

This commit is contained in:
Trek H 2020-08-11 20:17:52 +09:30
parent 7fb61edb0d
commit adf5ddece1
3 changed files with 30 additions and 9 deletions

View File

@ -437,6 +437,10 @@ func (d *ALSA) formatBuffer() pcm.Buffer {
return formatted
}
func (d *ALSA) DataSize() int {
return pcm.DataSize(d.SampleRate, d.Channels, d.BitDepth, d.RecPeriod, d.Codec)
}
// nearestPowerOfTwo finds and returns the nearest power of two to the given integer.
// If the lower and higher power of two are the same distance, it returns the higher power.
// For negative values, 1 is returned.

View File

@ -29,13 +29,32 @@ import (
"strconv"
"bitbucket.org/ausocean/av/codec/codecutil"
"bitbucket.org/ausocean/av/codec/pcm"
"bitbucket.org/ausocean/av/container/mts"
"bitbucket.org/ausocean/av/device/alsa"
"bitbucket.org/ausocean/utils/logger"
)
func (r *Revid) setupAudio() error {
// Create new ALSA device.
d := alsa.New(r.cfg.Logger)
r.input = d
// Configure ALSA device.
r.cfg.Logger.Log(logger.Debug, "configuring input device")
err := r.input.Set(r.cfg)
if err != nil {
r.cfg.Logger.Log(logger.Warning, "errors from configuring input device", "errors", err)
}
r.cfg.Logger.Log(logger.Info, "input device configured")
// Set revid's lexer.
l, err := codecutil.NewByteLexer(d.DataSize())
if err != nil {
return err
}
r.lexTo = l.Lex
// Add metadata.
mts.Meta.Add("sampleRate", strconv.Itoa(int(r.cfg.SampleRate)))
mts.Meta.Add("channels", strconv.Itoa(int(r.cfg.Channels)))
mts.Meta.Add("period", fmt.Sprintf("%.6f", r.cfg.RecPeriod))
@ -50,13 +69,5 @@ func (r *Revid) setupAudio() error {
r.cfg.Logger.Log(logger.Fatal, "no audio codec set in config")
}
r.input = alsa.New(r.cfg.Logger)
l, err := codecutil.NewByteLexer(pcm.DataSize(r.cfg.SampleRate, r.cfg.Channels, r.cfg.BitDepth, r.cfg.RecPeriod, r.cfg.InputCodec))
if err != nil {
return err
}
r.lexTo = l.Lex
return nil
}

View File

@ -392,6 +392,12 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
}
}
// input.Set does not need to be called for InputAudio since it is called
// in setupAudio above, so we can return here.
if r.cfg.Input == config.InputAudio {
return nil
}
// Configure the input device. We know that defaults are set, so no need to
// return error, but we should log.
r.cfg.Logger.Log(logger.Debug, "configuring input device")