From adf5ddece165764abd76b03dafae337664de6b08 Mon Sep 17 00:00:00 2001 From: Trek H Date: Tue, 11 Aug 2020 20:17:52 +0930 Subject: [PATCH] revid, alsa: reorder audio setup --- device/alsa/alsa.go | 4 ++++ revid/audio_linux.go | 29 ++++++++++++++++++++--------- revid/revid.go | 6 ++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index 3a6e7b14..e4db1c31 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -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. diff --git a/revid/audio_linux.go b/revid/audio_linux.go index d7c78b28..4c128922 100644 --- a/revid/audio_linux.go +++ b/revid/audio_linux.go @@ -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 } diff --git a/revid/revid.go b/revid/revid.go index d0d981b4..75d06e76 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -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")