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.
This commit is contained in:
Trek H 2019-06-04 12:28:40 +09:30
parent 409dcabe0a
commit bea747085f
5 changed files with 36 additions and 28 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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()

View File

@ -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()
}

View File

@ -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())
}