2019-04-22 08:44:08 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
revid_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Alan Noble <alan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This is Copyright (C) 2019 the Australian Ocean Lab (AusOcean).
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-03-08 10:15:38 +03:00
|
|
|
package revid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-03-16 08:16:33 +03:00
|
|
|
"io"
|
2019-03-08 10:15:38 +03:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
2019-03-08 13:13:01 +03:00
|
|
|
|
2019-03-08 10:15:38 +03:00
|
|
|
"bitbucket.org/ausocean/iot/pi/netsender"
|
|
|
|
)
|
|
|
|
|
2019-03-08 13:13:01 +03:00
|
|
|
const raspividPath = "/usr/local/bin/raspivid"
|
|
|
|
|
|
|
|
// Suppress all test logging, except for t.Errorf output.
|
2019-06-18 12:33:38 +03:00
|
|
|
var silent = true
|
2019-03-08 10:15:38 +03:00
|
|
|
|
2019-03-08 23:27:55 +03:00
|
|
|
// TestRaspivid tests that raspivid starts correctly.
|
|
|
|
// It is intended to be run on a Raspberry Pi.
|
2019-03-08 10:15:38 +03:00
|
|
|
func TestRaspivid(t *testing.T) {
|
2019-03-08 13:13:01 +03:00
|
|
|
if _, err := os.Stat(raspividPath); os.IsNotExist(err) {
|
|
|
|
t.Skip("Skipping TestRaspivid since no raspivid found.")
|
|
|
|
}
|
|
|
|
|
2019-03-08 10:15:38 +03:00
|
|
|
var logger testLogger
|
|
|
|
ns, err := netsender.New(&logger, nil, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("netsender.New failed with error %v", err)
|
|
|
|
}
|
2019-03-08 13:13:01 +03:00
|
|
|
|
2019-03-08 10:15:38 +03:00
|
|
|
var c Config
|
|
|
|
c.Logger = &logger
|
|
|
|
c.Input = Raspivid
|
|
|
|
|
|
|
|
rv, err := New(c, ns)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("revid.New failed with error %v", err)
|
|
|
|
}
|
2019-03-08 13:13:01 +03:00
|
|
|
|
2019-03-08 10:15:38 +03:00
|
|
|
err = rv.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("revid.Start failed with error %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testLogger implements a netsender.Logger.
|
2019-03-08 13:13:01 +03:00
|
|
|
type testLogger struct{}
|
2019-03-08 10:15:38 +03:00
|
|
|
|
2019-04-15 02:12:56 +03:00
|
|
|
func (tl *testLogger) SetLevel(level int8) {}
|
2019-03-08 10:15:38 +03:00
|
|
|
|
|
|
|
func (tl *testLogger) 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)
|
|
|
|
}
|
2019-03-08 23:27:55 +03:00
|
|
|
if level == 5 {
|
2019-03-08 10:15:38 +03:00
|
|
|
buf := make([]byte, 1<<16)
|
|
|
|
size := runtime.Stack(buf, true)
|
|
|
|
fmt.Printf("%s\n", string(buf[:size]))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 10:44:00 +03:00
|
|
|
|
2019-03-17 02:04:36 +03:00
|
|
|
// tstMtsEncoder emulates the mts.Encoder to the extent of the dst field.
|
|
|
|
// This will allow access to the dst to check that it has been set corrctly.
|
2019-04-23 06:48:41 +03:00
|
|
|
type tstMtsEncoder struct {
|
2019-04-24 06:31:29 +03:00
|
|
|
// dst is here solely to detect the type stored in the encoder.
|
2019-04-23 06:48:41 +03:00
|
|
|
// No data is written to dst.
|
|
|
|
dst io.WriteCloser
|
|
|
|
}
|
2019-03-17 02:04:36 +03:00
|
|
|
|
2019-04-15 02:12:56 +03:00
|
|
|
func (e *tstMtsEncoder) Write(d []byte) (int, error) { return len(d), nil }
|
|
|
|
func (e *tstMtsEncoder) Close() error { return nil }
|
2019-03-17 02:04:36 +03:00
|
|
|
|
|
|
|
// tstFlvEncoder emulates the flv.Encoder to the extent of the dst field.
|
|
|
|
// This will allow access to the dst to check that it has been set corrctly.
|
2019-04-23 06:48:41 +03:00
|
|
|
type tstFlvEncoder struct {
|
2019-04-24 06:31:29 +03:00
|
|
|
// dst is here solely to detect the type stored in the encoder.
|
2019-04-23 06:48:41 +03:00
|
|
|
// No data is written to dst.
|
|
|
|
dst io.WriteCloser
|
|
|
|
}
|
2019-03-17 02:04:36 +03:00
|
|
|
|
2019-04-03 04:01:21 +03:00
|
|
|
func (e *tstFlvEncoder) Write(d []byte) (int, error) { return len(d), nil }
|
2019-04-15 02:12:56 +03:00
|
|
|
func (e *tstFlvEncoder) Close() error { return nil }
|
2019-03-17 02:04:36 +03:00
|
|
|
|
2019-04-02 05:51:46 +03:00
|
|
|
// dummyMultiWriter emulates the MultiWriter provided by std lib, so that we
|
|
|
|
// can access the destinations.
|
2019-04-23 06:48:41 +03:00
|
|
|
type dummyMultiWriter struct {
|
2019-04-24 06:31:29 +03:00
|
|
|
// dst is here solely to detect the types stored in the multiWriter.
|
2019-04-23 06:48:41 +03:00
|
|
|
// No data is written to dst.
|
|
|
|
dst []io.WriteCloser
|
|
|
|
}
|
2019-04-02 05:51:46 +03:00
|
|
|
|
2019-04-03 04:08:37 +03:00
|
|
|
func (w *dummyMultiWriter) Write(d []byte) (int, error) { return len(d), nil }
|
2019-04-15 02:12:56 +03:00
|
|
|
func (w *dummyMultiWriter) Close() error { return nil }
|
2019-04-10 05:46:51 +03:00
|
|
|
|
2019-03-13 10:44:00 +03:00
|
|
|
// TestResetEncoderSenderSetup checks that revid.reset() correctly sets up the
|
|
|
|
// revid.encoder slice and the senders the encoders write to.
|
|
|
|
func TestResetEncoderSenderSetup(t *testing.T) {
|
|
|
|
// We will use these to indicate types after assertion.
|
|
|
|
const (
|
2019-03-29 08:47:11 +03:00
|
|
|
mtsSenderStr = "*revid.mtsSender"
|
|
|
|
rtpSenderStr = "*revid.rtpSender"
|
|
|
|
rtmpSenderStr = "*revid.rtmpSender"
|
|
|
|
mtsEncoderStr = "*revid.tstMtsEncoder"
|
|
|
|
flvEncoderStr = "*revid.tstFlvEncoder"
|
2019-03-13 10:44:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Struct that will be used to format test cases nicely below.
|
|
|
|
type encoder struct {
|
|
|
|
encoderType string
|
|
|
|
destinations []string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
outputs []uint8
|
|
|
|
encoders []encoder
|
|
|
|
}{
|
|
|
|
{
|
2019-05-13 09:48:41 +03:00
|
|
|
outputs: []uint8{HTTP},
|
2019-03-13 10:44:00 +03:00
|
|
|
encoders: []encoder{
|
|
|
|
{
|
|
|
|
encoderType: mtsEncoderStr,
|
|
|
|
destinations: []string{mtsSenderStr},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-13 09:48:41 +03:00
|
|
|
outputs: []uint8{RTMP},
|
2019-03-13 10:44:00 +03:00
|
|
|
encoders: []encoder{
|
|
|
|
{
|
|
|
|
encoderType: flvEncoderStr,
|
|
|
|
destinations: []string{rtmpSenderStr},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-13 09:48:41 +03:00
|
|
|
outputs: []uint8{RTP},
|
2019-03-13 10:44:00 +03:00
|
|
|
encoders: []encoder{
|
|
|
|
{
|
|
|
|
encoderType: mtsEncoderStr,
|
|
|
|
destinations: []string{rtpSenderStr},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-13 09:48:41 +03:00
|
|
|
outputs: []uint8{HTTP, RTMP},
|
2019-03-13 10:44:00 +03:00
|
|
|
encoders: []encoder{
|
|
|
|
{
|
|
|
|
encoderType: mtsEncoderStr,
|
|
|
|
destinations: []string{mtsSenderStr},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
encoderType: flvEncoderStr,
|
|
|
|
destinations: []string{rtmpSenderStr},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-13 09:48:41 +03:00
|
|
|
outputs: []uint8{HTTP, RTP, RTMP},
|
2019-03-13 10:44:00 +03:00
|
|
|
encoders: []encoder{
|
|
|
|
{
|
|
|
|
encoderType: mtsEncoderStr,
|
|
|
|
destinations: []string{mtsSenderStr, rtpSenderStr},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
encoderType: flvEncoderStr,
|
|
|
|
destinations: []string{rtmpSenderStr},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-13 09:48:41 +03:00
|
|
|
outputs: []uint8{RTP, RTMP},
|
2019-03-13 10:44:00 +03:00
|
|
|
encoders: []encoder{
|
|
|
|
{
|
|
|
|
encoderType: mtsEncoderStr,
|
|
|
|
destinations: []string{rtpSenderStr},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
encoderType: flvEncoderStr,
|
|
|
|
destinations: []string{rtmpSenderStr},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rv, err := New(Config{Logger: &testLogger{}}, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through our test cases.
|
|
|
|
for testNum, test := range tests {
|
|
|
|
// Create a new config and reset revid with it.
|
2019-03-17 02:04:36 +03:00
|
|
|
const dummyURL = "rtmp://dummy"
|
2019-05-13 09:53:38 +03:00
|
|
|
c := Config{Logger: &testLogger{}, Outputs: test.outputs, RTMPURL: dummyURL}
|
2019-03-17 02:04:36 +03:00
|
|
|
err := rv.setConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v for test %v", err, testNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This logic is what we want to check.
|
2019-04-15 02:12:56 +03:00
|
|
|
err = rv.setupPipeline(
|
2019-06-03 12:05:28 +03:00
|
|
|
func(dst io.WriteCloser, rate float64) (io.WriteCloser, error) {
|
2019-04-15 02:12:56 +03:00
|
|
|
return &tstMtsEncoder{dst: dst}, nil
|
|
|
|
},
|
|
|
|
func(dst io.WriteCloser, rate int) (io.WriteCloser, error) {
|
|
|
|
return &tstFlvEncoder{dst: dst}, nil
|
|
|
|
},
|
|
|
|
func(writers ...io.WriteCloser) io.WriteCloser {
|
|
|
|
return &dummyMultiWriter{dst: writers}
|
|
|
|
},
|
|
|
|
)
|
2019-03-13 10:44:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v for test %v", err, testNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First check that we have the correct number of encoders.
|
2019-04-18 10:25:48 +03:00
|
|
|
got := len(rv.encoders.(*dummyMultiWriter).dst)
|
2019-03-13 10:44:00 +03:00
|
|
|
want := len(test.encoders)
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("incorrect number of encoders in revid for test: %v. \nGot: %v\nWant: %v\n", testNum, got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check the correctness of encoders and their destinations.
|
2019-04-18 10:25:48 +03:00
|
|
|
for _, e := range rv.encoders.(*dummyMultiWriter).dst {
|
2019-03-13 10:44:00 +03:00
|
|
|
// Get e's type.
|
2019-03-29 08:47:11 +03:00
|
|
|
encoderType := fmt.Sprintf("%T", e)
|
2019-03-13 10:44:00 +03:00
|
|
|
|
|
|
|
// Check that we expect this encoder to be here.
|
|
|
|
idx := -1
|
|
|
|
for i, expect := range test.encoders {
|
|
|
|
if expect.encoderType == encoderType {
|
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx == -1 {
|
2019-03-17 02:04:36 +03:00
|
|
|
t.Fatalf("encoder %v isn't expected in test %v", encoderType, testNum)
|
2019-03-13 10:44:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now check that this encoder has correct number of destinations (senders).
|
2019-04-23 06:48:41 +03:00
|
|
|
var ms io.WriteCloser
|
2019-03-17 02:04:36 +03:00
|
|
|
switch encoderType {
|
|
|
|
case mtsEncoderStr:
|
|
|
|
ms = e.(*tstMtsEncoder).dst
|
|
|
|
case flvEncoderStr:
|
|
|
|
ms = e.(*tstFlvEncoder).dst
|
|
|
|
}
|
2019-03-24 12:31:25 +03:00
|
|
|
|
2019-04-02 05:51:46 +03:00
|
|
|
senders := ms.(*dummyMultiWriter).dst
|
2019-03-13 10:44:00 +03:00
|
|
|
got = len(senders)
|
|
|
|
want = len(test.encoders[idx].destinations)
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("did not get expected number of senders in test %v. \nGot: %v\nWant: %v\n", testNum, got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that destinations are as expected.
|
|
|
|
for _, expectDst := range test.encoders[idx].destinations {
|
|
|
|
ok := false
|
|
|
|
for _, dst := range senders {
|
|
|
|
// Get type of sender.
|
2019-03-30 02:38:27 +03:00
|
|
|
senderType := fmt.Sprintf("%T", dst)
|
2019-03-13 10:44:00 +03:00
|
|
|
|
|
|
|
// If it's one we want, indicate.
|
|
|
|
if senderType == expectDst {
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 00:56:34 +03:00
|
|
|
// If not okay then we couldn't find expected sender.
|
2019-03-13 10:44:00 +03:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("could not find expected destination %v, for test %v", expectDst, testNum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|