From bea747085f92b49dfbd27ba4eb4692cc7cf6250e Mon Sep 17 00:00:00 2001 From: Trek H Date: Tue, 4 Jun 2019 12:28:40 +0930 Subject: [PATCH] revid: updated lex funcs in tests, changed audioDevice logger Since adding the extra bufSize arg to Lex functions, the test functions using them needed to be updated. NewAudioDevice was changed to accept a logger to log to instead of creating a new one. --- codec/h265/lex_test.go | 2 +- protocol/rtmp/rtmp_test.go | 4 ++-- revid/audio-input.go | 23 +++-------------------- revid/audio-input_test.go | 33 +++++++++++++++++++++++++++++---- revid/revid.go | 2 +- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/codec/h265/lex_test.go b/codec/h265/lex_test.go index 1a409e4c..02ed5f1f 100644 --- a/codec/h265/lex_test.go +++ b/codec/h265/lex_test.go @@ -246,7 +246,7 @@ func TestLex(t *testing.T) { for testNum, test := range tests { r := &rtpReader{packets: test.packets} d := &destination{} - err := NewLexer(test.donl).Lex(d, r, 0) + err := NewLexer(test.donl).Lex(d, r, 0, 0) if err != nil { t.Fatalf("error lexing: %v\n", err) } diff --git a/protocol/rtmp/rtmp_test.go b/protocol/rtmp/rtmp_test.go index e1e79796..cf5f505c 100644 --- a/protocol/rtmp/rtmp_test.go +++ b/protocol/rtmp/rtmp_test.go @@ -199,7 +199,7 @@ func TestFromFrame(t *testing.T) { if err != nil { t.Errorf("Failed to create flv encoder with error: %v", err) } - err = h264.Lex(flvEncoder, bytes.NewReader(videoData), time.Second/time.Duration(frameRate)) + err = h264.Lex(flvEncoder, bytes.NewReader(videoData), time.Second/time.Duration(frameRate), 0) if err != nil { t.Errorf("Lexing failed with error: %v", err) } @@ -251,7 +251,7 @@ func TestFromFile(t *testing.T) { if err != nil { t.Fatalf("failed to create encoder: %v", err) } - err = h264.Lex(flvEncoder, f, time.Second/time.Duration(25)) + err = h264.Lex(flvEncoder, f, time.Second/time.Duration(25), 0) if err != nil { t.Errorf("Lexing and encoding failed with error: %v", err) } diff --git a/revid/audio-input.go b/revid/audio-input.go index 0592a6d2..0491b64b 100644 --- a/revid/audio-input.go +++ b/revid/audio-input.go @@ -36,13 +36,11 @@ import ( "bitbucket.org/ausocean/av/codec/adpcm" "bitbucket.org/ausocean/av/codec/pcm" - "bitbucket.org/ausocean/iot/pi/smartlogger" "bitbucket.org/ausocean/utils/logger" "bitbucket.org/ausocean/utils/ring" ) const ( - logPath = "/var/log/netsender" rbTimeout = 100 * time.Millisecond rbNextTimeout = 100 * time.Millisecond rbLen = 200 @@ -58,8 +56,6 @@ const ( var Rates = [8]int{8000, 16000, 32000, 44100, 48000, 88200, 96000, 192000} // AudioDevice holds everything we need to know about the audio input stream. -// Note: At 44100 Hz sample rate, 2 channels and 16-bit samples, a period of 5 seconds -// results in PCM data chunks of 882000 bytes. A longer period exceeds datastore's 1MB blob limit. type AudioDevice struct { l Logger mu sync.Mutex @@ -87,24 +83,11 @@ type AudioConfig struct { Codec uint8 } -// NewAudioDevice initializes and returns an AudioDevice struct which can be started, read from, and stopped. -func NewAudioDevice(cfg *AudioConfig) (*AudioDevice, error) { +// NewAudioDevice initializes and returns an AudioDevice which can be started, read from, and stopped. +func NewAudioDevice(cfg *AudioConfig, l Logger) (*AudioDevice, error) { a := &AudioDevice{} a.AudioConfig = cfg - - // Initialize logger. - logLevel := int(logger.Debug) - validLogLevel := true - if logLevel < int(logger.Debug) || logLevel > int(logger.Fatal) { - logLevel = int(logger.Info) - validLogLevel = false - } - logSender := smartlogger.New(logPath) - a.l = logger.New(int8(logLevel), &logSender.LogRoller) - a.l.Log(logger.Info, "log-netsender: Logger Initialized") - if !validLogLevel { - a.l.Log(logger.Error, "Invalid log level was defaulted to Info") - } + a.l = l // Open the requested audio device. err := a.open() diff --git a/revid/audio-input_test.go b/revid/audio-input_test.go index 871cc92e..3b62cb21 100644 --- a/revid/audio-input_test.go +++ b/revid/audio-input_test.go @@ -3,6 +3,9 @@ package revid import ( "bytes" "errors" + "fmt" + "os" + "runtime" "testing" "time" @@ -86,7 +89,28 @@ func checkDevice(ac *AudioConfig) error { return nil } -func TestAudio(t *testing.T) { +// rTestLogger implements a revid.Logger. +type rTestLogger struct{} + +func (tl rTestLogger) SetLevel(level int8) {} + +func (tl rTestLogger) Log(level int8, msg string, params ...interface{}) { + logLevels := [...]string{"Debug", "Info", "Warn", "Error", "", "", "Fatal"} + if level < -1 || level > 5 { + panic("Invalid log level") + } + if !silent { + fmt.Printf("%s: %s\n", logLevels[level+1], msg) + } + if level == 5 { + buf := make([]byte, 1<<16) + size := runtime.Stack(buf, true) + fmt.Printf("%s\n", string(buf[:size])) + os.Exit(1) + } +} + +func TestAudioDevice(t *testing.T) { // We want to open a device with a standard configuration. ac := &AudioConfig{ SampleRate: 8000, @@ -100,11 +124,12 @@ func TestAudio(t *testing.T) { // Skip if there are no suitable devices to test with. err := checkDevice(ac) if err != nil { - t.Error(err) + t.Skip(err) } // Create a new audioDevice, start, read/lex, and then stop it. - ai, err := NewAudioDevice(ac) + var l rTestLogger + ai, err := NewAudioDevice(ac, l) if err != nil { t.Error(err) } @@ -114,6 +139,6 @@ func TestAudio(t *testing.T) { t.Error(err) } go codecutil.LexBytes(dst, ai, time.Duration(ac.RecPeriod*float64(time.Second)), ai.ChunkSize()) - time.Sleep(time.Millisecond * 1000 * time.Duration(n)) + time.Sleep(time.Second * time.Duration(ac.RecPeriod) * time.Duration(n)) ai.Stop() } diff --git a/revid/revid.go b/revid/revid.go index 0bc4b954..fa243bef 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -635,7 +635,7 @@ func (r *Revid) startAudioDevice() (func() error, error) { BitDepth: r.config.BitDepth, Codec: r.config.InputCodec, } - ai, err := NewAudioDevice(ac) + ai, err := NewAudioDevice(ac, r.config.Logger) if err != nil { r.config.Logger.Log(logger.Fatal, pkg+"failed to create audio device", "error", err.Error()) }