From adf5ddece165764abd76b03dafae337664de6b08 Mon Sep 17 00:00:00 2001 From: Trek H Date: Tue, 11 Aug 2020 20:17:52 +0930 Subject: [PATCH 1/5] 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") From c394dc9d895b5ea9d2f02a53d240aa328e751961 Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 13 Aug 2020 15:28:59 +0930 Subject: [PATCH 2/5] alsa, revid: reverted changes to revid and changed how ALSA implements Device --- device/alsa/alsa.go | 12 ++++++++++-- revid/audio_linux.go | 2 +- revid/revid.go | 6 ------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index 970a325f..eadf7bd3 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -116,11 +116,11 @@ func (d *ALSA) Name() string { return "ALSA" } -// Set will take a Config struct, check the validity of the relevant fields +// Setup will take a Config struct, check the validity of the relevant fields // and then performs any configuration necessary. If fields are not valid, // an error is added to the multiError and a default value is used. // It then initialises the ALSA device which can then be started, read from, and stopped. -func (d *ALSA) Set(c config.Config) error { +func (d *ALSA) Setup(c config.Config) error { var errs device.MultiError if c.SampleRate <= 0 { errs = append(errs, errInvalidSampleRate) @@ -185,6 +185,14 @@ func (d *ALSA) Set(c config.Config) error { return nil } +// Set exists to satisfy the implementation of the Device interface that revid uses. +// Everything that would usually be in Set is in the Setup function. +// This is because an ALSA device is different to other devices in that it +// outputs binary non-packetised data and it requires a different configuration procedure. +func (d *ALSA) Set(c config.Config) error { + return nil +} + // Start will start recording audio and writing to the ringbuffer. // Once an ALSA device has been stopped it cannot be started again. This is likely to change in future. func (d *ALSA) Start() error { diff --git a/revid/audio_linux.go b/revid/audio_linux.go index 4c128922..d3d08a58 100644 --- a/revid/audio_linux.go +++ b/revid/audio_linux.go @@ -41,7 +41,7 @@ func (r *Revid) setupAudio() error { // Configure ALSA device. r.cfg.Logger.Log(logger.Debug, "configuring input device") - err := r.input.Set(r.cfg) + err := d.Setup(r.cfg) if err != nil { r.cfg.Logger.Log(logger.Warning, "errors from configuring input device", "errors", err) } diff --git a/revid/revid.go b/revid/revid.go index 75d06e76..d0d981b4 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -392,12 +392,6 @@ 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") From ba08c58af40b0d2a87bbc0eed2ce95bedba4ef47 Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 13 Aug 2020 15:42:28 +0930 Subject: [PATCH 3/5] alsa: added comment for exported function DataSize --- device/alsa/alsa.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index eadf7bd3..72b643a1 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -466,6 +466,8 @@ func (d *ALSA) formatBuffer() pcm.Buffer { return formatted } +// DataSize returns the size in bytes of the data ALSA device d will +// output in the duration of a single recording period. func (d *ALSA) DataSize() int { return pcm.DataSize(d.SampleRate, d.Channels, d.BitDepth, d.RecPeriod, d.Codec) } From 456b711b2810d2b983bfc4e650be762973fa6dad Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 13 Aug 2020 15:51:24 +0930 Subject: [PATCH 4/5] alsa: test files updated to use Setup instead of Set --- device/alsa/alsa_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/device/alsa/alsa_test.go b/device/alsa/alsa_test.go index aeeb6ac9..918a022a 100644 --- a/device/alsa/alsa_test.go +++ b/device/alsa/alsa_test.go @@ -53,7 +53,7 @@ func TestDevice(t *testing.T) { // Create a new ALSA device, start, read/lex, and then stop it. l := logger.New(logger.Debug, os.Stderr, true) ai := New(l) - err := ai.Set(c) + err := ai.Setup(c) // If there was an error opening the device, skip this test. if _, ok := err.(OpenError); ok { t.Skip(err) @@ -119,7 +119,7 @@ func TestIsRunning(t *testing.T) { l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. d := New(l) - err := d.Set(config.Config{ + err := d.Setup(config.Config{ SampleRate: sampleRate, Channels: channels, BitDepth: bitDepth, From abbe35d7a89423059e0f33be98614d74fcbf8c27 Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 13 Aug 2020 16:23:19 +0930 Subject: [PATCH 5/5] alsa: improve documentation --- device/alsa/alsa.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index 72b643a1..0ebfa931 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -117,7 +117,7 @@ func (d *ALSA) Name() string { } // Setup will take a Config struct, check the validity of the relevant fields -// and then performs any configuration necessary. If fields are not valid, +// and then perform any configuration necessary. If fields are not valid, // an error is added to the multiError and a default value is used. // It then initialises the ALSA device which can then be started, read from, and stopped. func (d *ALSA) Setup(c config.Config) error {