mirror of https://bitbucket.org/ausocean/av.git
give container/mts NewEncoder(...) log parameter and update code around repo accordingly
This commit is contained in:
parent
72063b1ac2
commit
18ffefd7cb
|
@ -142,6 +142,14 @@ var (
|
|||
pmtTable []byte
|
||||
)
|
||||
|
||||
type logger interface {
|
||||
Debug(string, ...interface{})
|
||||
Info(string, ...interface{})
|
||||
Warning(string, ...interface{})
|
||||
Error(string, ...interface{})
|
||||
Fatal(string, ...interface{})
|
||||
}
|
||||
|
||||
// Encoder encapsulates properties of an MPEG-TS generator.
|
||||
type Encoder struct {
|
||||
dst io.WriteCloser
|
||||
|
@ -163,11 +171,14 @@ type Encoder struct {
|
|||
startTime time.Time
|
||||
mediaPid uint16
|
||||
streamID byte
|
||||
|
||||
// log is a function that will be used through the encoder code for logging.
|
||||
log logger
|
||||
}
|
||||
|
||||
// NewEncoder returns an Encoder with the specified media type and rate eg. if a video stream
|
||||
// calls write for every frame, the rate will be the frame rate of the video.
|
||||
func NewEncoder(dst io.WriteCloser, rate float64, mediaType int, options ...func(*Encoder) error) (*Encoder, error) {
|
||||
func NewEncoder(dst io.WriteCloser, rate float64, mediaType int, log logger, options ...func(*Encoder) error) (*Encoder, error) {
|
||||
var mPID uint16
|
||||
var sID byte
|
||||
psiM := timeBased
|
||||
|
|
|
@ -54,6 +54,32 @@ func (d *destination) Write(p []byte) (int, error) {
|
|||
return len(p), nil
|
||||
}
|
||||
|
||||
// testLogger will allow logging to be done by the testing pkg.
|
||||
type testLogger testing.T
|
||||
|
||||
func (tl *testLogger) Debug(msg string, args ...interface{}) { tl.log("debug", msg, args) }
|
||||
func (tl *testLogger) Info(msg string, args ...interface{}) { tl.log("info", msg, args) }
|
||||
func (tl *testLogger) Warning(msg string, args ...interface{}) { tl.log("warning", msg, args) }
|
||||
func (tl *testLogger) Error(msg string, args ...interface{}) { tl.log("error", msg, args) }
|
||||
func (tl *testLogger) Fatal(msg string, args ...interface{}) { tl.log("fatal", msg, args) }
|
||||
|
||||
func (tl *testLogger) log(lvl string, msg string, args ...interface{}) {
|
||||
switch lvl {
|
||||
case "debug", "info", "warning", "error", "fatal":
|
||||
default:
|
||||
panic("invalid log level")
|
||||
}
|
||||
msg = lvl + ": " + msg
|
||||
for i := 0; i < len(args); i++ {
|
||||
msg += " %v"
|
||||
}
|
||||
if len(args) == 0 {
|
||||
tl.Log(msg + "\n")
|
||||
return
|
||||
}
|
||||
tl.Logf(msg+"\n", args)
|
||||
}
|
||||
|
||||
// TestEncodeVideo checks that we can correctly encode some dummy data into a
|
||||
// valid MPEG-TS stream. This checks for correct MPEG-TS headers and also that the
|
||||
// original data is stored correctly and is retreivable.
|
||||
|
@ -101,7 +127,7 @@ func TestEncodeVideo(t *testing.T) {
|
|||
|
||||
// Create the dst and write the test data to encoder.
|
||||
dst := &destination{}
|
||||
e, err := NewEncoder(nopCloser{dst}, 25, EncodeH264, PacketBasedPSI(psiSendCount))
|
||||
e, err := NewEncoder(nopCloser{dst}, 25, EncodeH264, (*testLogger)(t), PacketBasedPSI(psiSendCount))
|
||||
if err != nil {
|
||||
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
|
||||
}
|
||||
|
@ -165,7 +191,7 @@ func TestEncodePcm(t *testing.T) {
|
|||
sampleSize := 2
|
||||
blockSize := 16000
|
||||
writeFreq := float64(sampleRate*sampleSize) / float64(blockSize)
|
||||
e, err := NewEncoder(nopCloser{&buf}, writeFreq, EncodeAudio)
|
||||
e, err := NewEncoder(nopCloser{&buf}, writeFreq, EncodeAudio, (*testLogger)(t))
|
||||
if err != nil {
|
||||
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
|
||||
}
|
||||
|
@ -269,7 +295,7 @@ const fps = 25
|
|||
func TestMetaEncode1(t *testing.T) {
|
||||
Meta = meta.New()
|
||||
var buf bytes.Buffer
|
||||
e, err := NewEncoder(nopCloser{&buf}, fps, EncodeH264)
|
||||
e, err := NewEncoder(nopCloser{&buf}, fps, EncodeH264, (*testLogger)(t))
|
||||
if err != nil {
|
||||
t.Fatalf("could not create encoder, failed with error: %v", err)
|
||||
}
|
||||
|
@ -301,7 +327,7 @@ func TestMetaEncode1(t *testing.T) {
|
|||
func TestMetaEncode2(t *testing.T) {
|
||||
Meta = meta.New()
|
||||
var buf bytes.Buffer
|
||||
e, err := NewEncoder(nopCloser{&buf}, fps, EncodeH264)
|
||||
e, err := NewEncoder(nopCloser{&buf}, fps, EncodeH264, (*testLogger)(t))
|
||||
if err != nil {
|
||||
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
|
||||
}
|
||||
|
@ -333,7 +359,7 @@ func TestMetaEncode2(t *testing.T) {
|
|||
func TestExtractMeta(t *testing.T) {
|
||||
Meta = meta.New()
|
||||
var buf bytes.Buffer
|
||||
e, err := NewEncoder(nopCloser{&buf}, fps, EncodeH264)
|
||||
e, err := NewEncoder(nopCloser{&buf}, fps, EncodeH264, (*testLogger)(t))
|
||||
if err != nil {
|
||||
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
|
||||
}
|
||||
|
|
|
@ -74,6 +74,14 @@ type Logger interface {
|
|||
Log(level int8, message string, params ...interface{})
|
||||
}
|
||||
|
||||
type encLog struct{ Logger }
|
||||
|
||||
func (el *encLog) Debug(msg string, args ...interface{}) { el.Log(logger.Debug, msg, args) }
|
||||
func (el *encLog) Info(msg string, args ...interface{}) { el.Log(logger.Info, msg, args) }
|
||||
func (el *encLog) Warning(msg string, args ...interface{}) { el.Log(logger.Warning, msg, args) }
|
||||
func (el *encLog) Error(msg string, args ...interface{}) { el.Log(logger.Error, msg, args) }
|
||||
func (el *encLog) Fatal(msg string, args ...interface{}) { el.Log(logger.Fatal, msg, args) }
|
||||
|
||||
// Revid provides methods to control a revid session; providing methods
|
||||
// to start, stop and change the state of an instance using the Config struct.
|
||||
type Revid struct {
|
||||
|
@ -117,7 +125,7 @@ type Revid struct {
|
|||
// bitrate is used for bitrate calculations.
|
||||
bitrate bitrate.Calculator
|
||||
|
||||
// stop used used to signal stopping when looping an input.
|
||||
// stop is used to signal stopping when looping an input.
|
||||
stop chan struct{}
|
||||
}
|
||||
|
||||
|
@ -214,7 +222,7 @@ func (r *Revid) reset(c config.Config) error {
|
|||
panic("unknown input type")
|
||||
}
|
||||
|
||||
return mts.NewEncoder(dst, float64(fps), st, encOptions...)
|
||||
return mts.NewEncoder(dst, float64(fps), st, &encLog{r.cfg.Logger}, encOptions...)
|
||||
},
|
||||
func(dst io.WriteCloser, fps int) (io.WriteCloser, error) {
|
||||
return flv.NewEncoder(dst, true, true, fps)
|
||||
|
|
|
@ -51,7 +51,7 @@ func TestRaspivid(t *testing.T) {
|
|||
t.Skip("Skipping TestRaspivid since no raspivid found.")
|
||||
}
|
||||
|
||||
var logger testLogger
|
||||
var logger simpleLogger
|
||||
ns, err := netsender.New(&logger, nil, nil, nil)
|
||||
if err != nil {
|
||||
t.Errorf("netsender.New failed with error %v", err)
|
||||
|
@ -72,12 +72,12 @@ func TestRaspivid(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// testLogger implements a netsender.Logger.
|
||||
type testLogger struct{}
|
||||
// simpleLogger implements a netsender.Logger.
|
||||
type simpleLogger struct{}
|
||||
|
||||
func (tl *testLogger) SetLevel(level int8) {}
|
||||
func (tl *simpleLogger) SetLevel(level int8) {}
|
||||
|
||||
func (tl *testLogger) Log(level int8, msg string, params ...interface{}) {
|
||||
func (tl *simpleLogger) Log(level int8, msg string, params ...interface{}) {
|
||||
logLevels := [...]string{"Debug", "Info", "Warn", "Error", "", "", "Fatal"}
|
||||
if level < -1 || level > 5 {
|
||||
panic("Invalid log level")
|
||||
|
@ -216,7 +216,7 @@ func TestResetEncoderSenderSetup(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
rv, err := New(config.Config{Logger: &testLogger{}}, nil)
|
||||
rv, err := New(config.Config{Logger: &simpleLogger{}}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
|
@ -225,7 +225,7 @@ func TestResetEncoderSenderSetup(t *testing.T) {
|
|||
for testNum, test := range tests {
|
||||
// Create a new config and reset revid with it.
|
||||
const dummyURL = "rtmp://dummy"
|
||||
c := config.Config{Logger: &testLogger{}, Outputs: test.outputs, RTMPURL: dummyURL}
|
||||
c := config.Config{Logger: &simpleLogger{}, Outputs: test.outputs, RTMPURL: dummyURL}
|
||||
err := rv.setConfig(c)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v for test %v", err, testNum)
|
||||
|
|
|
@ -98,10 +98,16 @@ func (ts *destination) Write(d []byte) (int, error) {
|
|||
|
||||
func (ts *destination) Close() error { return nil }
|
||||
|
||||
// dummyLogger will allow logging to be done by the testing pkg.
|
||||
type dummyLogger testing.T
|
||||
// testLogger will allow logging to be done by the testing pkg.
|
||||
type testLogger testing.T
|
||||
|
||||
func (dl *dummyLogger) log(lvl int8, msg string, args ...interface{}) {
|
||||
func (tl *testLogger) Debug(msg string, args ...interface{}) { tl.log(logger.Debug, msg, args) }
|
||||
func (tl *testLogger) Info(msg string, args ...interface{}) { tl.log(logger.Info, msg, args) }
|
||||
func (tl *testLogger) Warning(msg string, args ...interface{}) { tl.log(logger.Warning, msg, args) }
|
||||
func (tl *testLogger) Error(msg string, args ...interface{}) { tl.log(logger.Error, msg, args) }
|
||||
func (tl *testLogger) Fatal(msg string, args ...interface{}) { tl.log(logger.Fatal, msg, args) }
|
||||
|
||||
func (dl *testLogger) log(lvl int8, msg string, args ...interface{}) {
|
||||
var l string
|
||||
switch lvl {
|
||||
case logger.Warning:
|
||||
|
@ -136,10 +142,10 @@ func TestMTSSenderSegment(t *testing.T) {
|
|||
dst := &destination{t: t, done: make(chan struct{}), doneAt: numberOfClips}
|
||||
const testRBCapacity = 50000000
|
||||
nElements := testRBCapacity / rbStartingElementSize
|
||||
sender := newMTSSender(dst, (*dummyLogger)(t).log, ring.NewBuffer(nElements, rbStartingElementSize, 0), 0)
|
||||
sender := newMTSSender(dst, (*testLogger)(t).log, ring.NewBuffer(nElements, rbStartingElementSize, 0), 0)
|
||||
|
||||
const psiSendCount = 10
|
||||
encoder, err := mts.NewEncoder(sender, 25, mts.EncodeH264, mts.PacketBasedPSI(psiSendCount))
|
||||
encoder, err := mts.NewEncoder(sender, 25, mts.EncodeH264, (*testLogger)(t), mts.PacketBasedPSI(psiSendCount))
|
||||
if err != nil {
|
||||
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
|
||||
}
|
||||
|
@ -217,10 +223,10 @@ func TestMtsSenderFailedSend(t *testing.T) {
|
|||
dst := &destination{t: t, testFails: true, failAt: clipToFailAt, done: make(chan struct{})}
|
||||
const testRBCapacity = 50000000 // 50MB
|
||||
nElements := testRBCapacity / rbStartingElementSize
|
||||
sender := newMTSSender(dst, (*dummyLogger)(t).log, ring.NewBuffer(nElements, rbStartingElementSize, 0), 0)
|
||||
sender := newMTSSender(dst, (*testLogger)(t).log, ring.NewBuffer(nElements, rbStartingElementSize, 0), 0)
|
||||
|
||||
const psiSendCount = 10
|
||||
encoder, err := mts.NewEncoder(sender, 25, mts.EncodeH264, mts.PacketBasedPSI(psiSendCount))
|
||||
encoder, err := mts.NewEncoder(sender, 25, mts.EncodeH264, (*testLogger)(t), mts.PacketBasedPSI(psiSendCount))
|
||||
if err != nil {
|
||||
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
|
||||
}
|
||||
|
@ -298,10 +304,10 @@ func TestMtsSenderDiscontinuity(t *testing.T) {
|
|||
// Create destination, the mtsSender and the mtsEncoder.
|
||||
const clipToDelay = 3
|
||||
dst := &destination{t: t, sendDelay: 10 * time.Millisecond, delayAt: clipToDelay, done: make(chan struct{})}
|
||||
sender := newMTSSender(dst, (*dummyLogger)(t).log, ring.NewBuffer(1, rbStartingElementSize, 0), 0)
|
||||
sender := newMTSSender(dst, (*testLogger)(t).log, ring.NewBuffer(1, rbStartingElementSize, 0), 0)
|
||||
|
||||
const psiSendCount = 10
|
||||
encoder, err := mts.NewEncoder(sender, 25, mts.EncodeH264, mts.PacketBasedPSI(psiSendCount))
|
||||
encoder, err := mts.NewEncoder(sender, 25, mts.EncodeH264, (*testLogger)(t), mts.PacketBasedPSI(psiSendCount))
|
||||
if err != nil {
|
||||
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue