2019-02-15 18:10:35 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
mtsSender_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
mtsSender_test.go contains tests that validate the functionalilty of the
|
|
|
|
mtsSender under senders.go. Tests include checks that the mtsSender is
|
|
|
|
segmenting sends correctly, and also that it can correct discontinuities.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
mtsSender_test.go is Copyright (C) 2017-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
|
2019-03-01 02:32:43 +03:00
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2019-02-15 18:10:35 +03:00
|
|
|
*/
|
|
|
|
package revid
|
|
|
|
|
2019-02-16 09:03:39 +03:00
|
|
|
import (
|
2019-02-16 20:05:59 +03:00
|
|
|
"errors"
|
2019-02-16 09:03:39 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-03-01 02:33:39 +03:00
|
|
|
"github.com/Comcast/gots/packet"
|
|
|
|
"github.com/Comcast/gots/pes"
|
|
|
|
|
2019-03-25 04:21:03 +03:00
|
|
|
"bitbucket.org/ausocean/av/container/mts"
|
|
|
|
"bitbucket.org/ausocean/av/container/mts/meta"
|
2019-02-16 09:03:39 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ring buffer sizes and read/write timeouts.
|
|
|
|
const (
|
|
|
|
rbSize = 100
|
|
|
|
rbElementSize = 150000
|
|
|
|
wTimeout = 10 * time.Millisecond
|
|
|
|
rTimeout = 10 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2019-03-12 09:38:36 +03:00
|
|
|
var (
|
|
|
|
errSendFailed = errors.New("send failed")
|
|
|
|
)
|
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// destination simulates a destination for the mtsSender. It allows for the
|
|
|
|
// emulation of failed and delayed sends.
|
2019-04-01 04:32:15 +03:00
|
|
|
type destination struct {
|
2019-04-08 12:32:42 +03:00
|
|
|
buf [][]byte
|
|
|
|
testFails bool
|
|
|
|
failAt int
|
|
|
|
currentPkt int
|
|
|
|
t *testing.T
|
|
|
|
sendDelay time.Duration
|
|
|
|
delayAt int
|
2019-02-16 09:03:39 +03:00
|
|
|
}
|
|
|
|
|
2019-04-01 04:32:15 +03:00
|
|
|
func (ts *destination) Write(d []byte) (int, error) {
|
2019-04-08 12:32:42 +03:00
|
|
|
ts.t.Log("writing clip to destination")
|
|
|
|
if ts.delayAt != 0 && ts.currentPkt == ts.delayAt {
|
|
|
|
time.Sleep(ts.sendDelay)
|
|
|
|
}
|
|
|
|
if ts.testFails && ts.currentPkt == ts.failAt {
|
|
|
|
ts.t.Log("failed send")
|
2019-03-01 03:47:40 +03:00
|
|
|
ts.currentPkt++
|
2019-04-01 04:32:15 +03:00
|
|
|
return 0, errSendFailed
|
2019-02-16 17:01:30 +03:00
|
|
|
}
|
2019-02-16 20:05:59 +03:00
|
|
|
cpy := make([]byte, len(d))
|
|
|
|
copy(cpy, d)
|
2019-03-01 03:41:20 +03:00
|
|
|
ts.buf = append(ts.buf, cpy)
|
2019-03-01 03:47:40 +03:00
|
|
|
ts.currentPkt++
|
2019-04-01 04:32:15 +03:00
|
|
|
return len(d), nil
|
2019-02-16 09:03:39 +03:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
func (ts *destination) Close() error { return nil }
|
|
|
|
|
|
|
|
// dummyLogger will allow logging to be done by the testing pkg.
|
|
|
|
type dummyLogger testing.T
|
|
|
|
|
|
|
|
func (dl dummyLogger) log(lvl int8, msg string, args ...interface{}) {
|
2019-02-16 09:03:39 +03:00
|
|
|
var l string
|
|
|
|
switch lvl {
|
|
|
|
case logger.Warning:
|
|
|
|
l = "warning"
|
|
|
|
case logger.Debug:
|
|
|
|
l = "debug"
|
|
|
|
case logger.Info:
|
|
|
|
l = "info"
|
|
|
|
case logger.Error:
|
|
|
|
l = "error"
|
|
|
|
case logger.Fatal:
|
|
|
|
l = "fatal"
|
|
|
|
}
|
|
|
|
msg = l + ": " + msg
|
|
|
|
for i := 0; i < len(args); i++ {
|
|
|
|
msg += " %v"
|
|
|
|
}
|
2019-04-08 12:32:42 +03:00
|
|
|
if len(args) == 0 {
|
|
|
|
dl.Log(msg + "\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dl.Logf(msg+"\n", args)
|
2019-02-16 09:03:39 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// TestSegment ensures that the mtsSender correctly segments data into clips
|
|
|
|
// based on positioning of PSI in the mtsEncoder's output stream.
|
2019-03-12 08:52:11 +03:00
|
|
|
func TestMtsSenderSegment(t *testing.T) {
|
2019-02-16 16:22:40 +03:00
|
|
|
mts.Meta = meta.New()
|
2019-03-01 07:45:44 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Create ringBuffer, sender, sender and the MPEGTS encoder.
|
|
|
|
tstDst := &destination{t: t}
|
|
|
|
sender := newMtsSender(tstDst, dummyLogger(*t).log, ringBufferSize, ringBufferElementSize, writeTimeout)
|
|
|
|
encoder := mts.NewEncoder(sender, 25)
|
2019-02-16 09:03:39 +03:00
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// Turn time based PSI writing off for encoder.
|
2019-02-16 09:03:39 +03:00
|
|
|
const psiSendCount = 10
|
|
|
|
encoder.TimeBasedPsi(false, psiSendCount)
|
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Write the packets to the encoder, which will in turn write to the mtsSender.
|
|
|
|
// Payload will just be packet number.
|
|
|
|
t.Log("writing packets")
|
2019-02-16 09:03:39 +03:00
|
|
|
const noOfPacketsToWrite = 100
|
|
|
|
for i := 0; i < noOfPacketsToWrite; i++ {
|
2019-03-10 05:30:58 +03:00
|
|
|
encoder.Write([]byte{byte(i)})
|
2019-04-08 12:32:42 +03:00
|
|
|
}
|
2019-02-16 09:03:39 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Give the mtsSender some time to finish up and then Close it.
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
sender.Close()
|
2019-02-16 09:03:39 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Check the data.
|
|
|
|
result := tstDst.buf
|
|
|
|
expectData := 0
|
|
|
|
for clipNo, clip := range result {
|
|
|
|
t.Logf("Checking clip: %v\n", clipNo)
|
|
|
|
|
|
|
|
// Check that the clip is of expected length.
|
|
|
|
clipLen := len(clip)
|
|
|
|
if clipLen != psiSendCount*mts.PacketSize {
|
|
|
|
t.Fatalf("Clip %v is not correct length. Got: %v Want: %v\n Clip: %v\n", clipNo, clipLen, psiSendCount*mts.PacketSize, clip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also check that the first packet is a PAT.
|
|
|
|
firstPkt := clip[:mts.PacketSize]
|
|
|
|
var pkt packet.Packet
|
|
|
|
copy(pkt[:], firstPkt)
|
|
|
|
pid := pkt.PID()
|
|
|
|
if pid != mts.PatPid {
|
|
|
|
t.Fatalf("First packet of clip %v is not pat, but rather: %v\n", clipNo, pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the clip data is okay.
|
|
|
|
t.Log("checking clip data")
|
|
|
|
for i := 0; i < len(clip); i += mts.PacketSize {
|
|
|
|
copy(pkt[:], clip[i:i+mts.PacketSize])
|
|
|
|
if pkt.PID() == mts.VideoPid {
|
|
|
|
t.Log("got video PID")
|
|
|
|
payload, err := pkt.Payload()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse PES from the MTS payload.
|
|
|
|
pes, err := pes.NewPESHeader(payload)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the data from the PES packet and convert to an int.
|
|
|
|
data := int8(pes.Data()[0])
|
|
|
|
|
|
|
|
// Calc expected data in the PES and then check.
|
|
|
|
if data != int8(expectData) {
|
|
|
|
t.Errorf("Did not get expected pkt data. ClipNo: %v, pktNoInClip: %v, Got: %v, want: %v\n", clipNo, i/mts.PacketSize, data, expectData)
|
|
|
|
}
|
|
|
|
expectData++
|
2019-02-16 20:05:59 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-16 09:03:39 +03:00
|
|
|
}
|
2019-04-08 12:32:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestMtsSenderFailedSend checks that a failed send is correctly handled by
|
|
|
|
// the mtsSender. The mtsSender should try to send the same clip again.
|
|
|
|
func TestMtsSenderFailedSend(t *testing.T) {
|
|
|
|
mts.Meta = meta.New()
|
|
|
|
|
|
|
|
// Create destination, the mtsSender and the mtsEncoder
|
|
|
|
const clipToFailAt = 3
|
|
|
|
tstDst := &destination{t: t, testFails: true, failAt: clipToFailAt}
|
|
|
|
sender := newMtsSender(tstDst, dummyLogger(*t).log, ringBufferSize, ringBufferElementSize, writeTimeout)
|
|
|
|
encoder := mts.NewEncoder(sender, 25)
|
|
|
|
|
|
|
|
// Turn time based PSI writing off for encoder and send PSI every 10 packets.
|
|
|
|
const psiSendCount = 10
|
|
|
|
encoder.TimeBasedPsi(false, psiSendCount)
|
|
|
|
|
|
|
|
// Write the packets to the encoder, which will in turn write to the mtsSender.
|
|
|
|
// Payload will just be packet number.
|
|
|
|
t.Log("writing packets")
|
|
|
|
const noOfPacketsToWrite = 100
|
|
|
|
for i := 0; i < noOfPacketsToWrite; i++ {
|
|
|
|
encoder.Write([]byte{byte(i)})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give the mtsSender some time to finish up and then Close it.
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
sender.Close()
|
2019-02-16 09:03:39 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Check that we have data as expected.
|
2019-04-01 04:32:15 +03:00
|
|
|
result := tstDst.buf
|
2019-02-16 20:05:59 +03:00
|
|
|
expectData := 0
|
2019-02-16 09:03:39 +03:00
|
|
|
for clipNo, clip := range result {
|
2019-02-16 22:02:44 +03:00
|
|
|
t.Logf("Checking clip: %v\n", clipNo)
|
2019-03-01 07:45:44 +03:00
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// Check that the clip is of expected length.
|
2019-02-16 09:03:39 +03:00
|
|
|
clipLen := len(clip)
|
2019-02-16 20:05:59 +03:00
|
|
|
if clipLen != psiSendCount*mts.PacketSize {
|
2019-02-16 16:22:40 +03:00
|
|
|
t.Fatalf("Clip %v is not correct length. Got: %v Want: %v\n Clip: %v\n", clipNo, clipLen, psiSendCount*mts.PacketSize, clip)
|
2019-02-16 09:03:39 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// Also check that the first packet is a PAT.
|
2019-02-16 09:03:39 +03:00
|
|
|
firstPkt := clip[:mts.PacketSize]
|
2019-03-01 07:18:26 +03:00
|
|
|
var pkt packet.Packet
|
2019-02-16 09:03:39 +03:00
|
|
|
copy(pkt[:], firstPkt)
|
2019-03-01 07:18:26 +03:00
|
|
|
pid := pkt.PID()
|
2019-02-16 09:03:39 +03:00
|
|
|
if pid != mts.PatPid {
|
2019-02-16 17:01:30 +03:00
|
|
|
t.Fatalf("First packet of clip %v is not pat, but rather: %v\n", clipNo, pid)
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// Check that the clip data is okay.
|
2019-04-08 12:32:42 +03:00
|
|
|
t.Log("checking clip data")
|
2019-02-16 17:01:30 +03:00
|
|
|
for i := 0; i < len(clip); i += mts.PacketSize {
|
2019-03-01 07:58:40 +03:00
|
|
|
copy(pkt[:], clip[i:i+mts.PacketSize])
|
|
|
|
if pkt.PID() == mts.VideoPid {
|
2019-04-08 12:32:42 +03:00
|
|
|
t.Log("got video PID")
|
2019-03-01 07:18:26 +03:00
|
|
|
payload, err := pkt.Payload()
|
2019-02-16 17:01:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// Parse PES from the MTS payload.
|
2019-02-16 17:01:30 +03:00
|
|
|
pes, err := pes.NewPESHeader(payload)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// Get the data from the PES packet and convert to an int.
|
2019-02-16 20:05:59 +03:00
|
|
|
data := int8(pes.Data()[0])
|
2019-02-16 17:01:30 +03:00
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// Calc expected data in the PES and then check.
|
2019-02-16 20:05:59 +03:00
|
|
|
if data != int8(expectData) {
|
|
|
|
t.Errorf("Did not get expected pkt data. ClipNo: %v, pktNoInClip: %v, Got: %v, want: %v\n", clipNo, i/mts.PacketSize, data, expectData)
|
2019-02-16 17:01:30 +03:00
|
|
|
}
|
2019-02-16 20:05:59 +03:00
|
|
|
expectData++
|
2019-02-16 17:01:30 +03:00
|
|
|
}
|
2019-02-16 09:03:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 18:10:35 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// TestMtsSenderDiscontinuity checks that a discontinuity in a stream is
|
|
|
|
// correctly handled by the mtsSender. A discontinuity is caused by overflowing
|
|
|
|
// the mtsSender's ringBuffer. It is expected that the next clip seen has the
|
|
|
|
// disconinuity indicator applied.
|
2019-03-12 08:52:11 +03:00
|
|
|
func TestMtsSenderDiscontinuity(t *testing.T) {
|
2019-02-16 17:01:30 +03:00
|
|
|
mts.Meta = meta.New()
|
2019-03-01 07:45:44 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Create destination, the mtsSender and the mtsEncoder.
|
|
|
|
const clipToDelay = 3
|
|
|
|
tstDst := &destination{t: t, sendDelay: 10 * time.Millisecond, delayAt: clipToDelay}
|
|
|
|
sender := newMtsSender(tstDst, dummyLogger(*t).log, 1, ringBufferElementSize, writeTimeout)
|
|
|
|
encoder := mts.NewEncoder(sender, 25)
|
2019-02-16 17:01:30 +03:00
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// Turn time based PSI writing off for encoder.
|
2019-02-16 17:01:30 +03:00
|
|
|
const psiSendCount = 10
|
|
|
|
encoder.TimeBasedPsi(false, psiSendCount)
|
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Write the packets to the encoder, which will in turn write to the mtsSender.
|
|
|
|
// Payload will just be packet number.
|
2019-02-16 17:01:30 +03:00
|
|
|
const noOfPacketsToWrite = 100
|
|
|
|
for i := 0; i < noOfPacketsToWrite; i++ {
|
2019-03-10 05:30:58 +03:00
|
|
|
encoder.Write([]byte{byte(i)})
|
2019-02-16 17:01:30 +03:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Give mtsSender time to finish up then Close.
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
sender.Close()
|
|
|
|
|
|
|
|
// Check the data.
|
2019-04-01 04:32:15 +03:00
|
|
|
result := tstDst.buf
|
2019-04-08 12:32:42 +03:00
|
|
|
expectedCC := 0
|
|
|
|
for clipNo, clip := range result {
|
|
|
|
t.Logf("Checking clip: %v\n", clipNo)
|
2019-02-16 17:01:30 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Check that the clip is of expected length.
|
|
|
|
clipLen := len(clip)
|
|
|
|
if clipLen != psiSendCount*mts.PacketSize {
|
|
|
|
t.Fatalf("Clip %v is not correct length. Got: %v Want: %v\n Clip: %v\n", clipNo, clipLen, psiSendCount*mts.PacketSize, clip)
|
|
|
|
}
|
2019-03-01 07:45:44 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Also check that the first packet is a PAT.
|
|
|
|
firstPkt := clip[:mts.PacketSize]
|
|
|
|
var pkt packet.Packet
|
|
|
|
copy(pkt[:], firstPkt)
|
|
|
|
pid := pkt.PID()
|
|
|
|
if pid != mts.PatPid {
|
|
|
|
t.Fatalf("First packet of clip %v is not pat, but rather: %v\n", clipNo, pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the discontinuity indicator
|
|
|
|
discon, _ := (*packet.AdaptationField)(&pkt).Discontinuity()
|
2019-02-16 17:01:30 +03:00
|
|
|
|
2019-04-08 12:32:42 +03:00
|
|
|
// Check the continuity counter.
|
|
|
|
cc := pkt.ContinuityCounter()
|
|
|
|
if cc != expectedCC {
|
|
|
|
t.Log("discontinuity found")
|
|
|
|
expectedCC = cc
|
|
|
|
if !discon {
|
|
|
|
t.Errorf("discontinuity indicator not set where expected for clip: %v", clipNo)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if discon && clipNo != 0 {
|
|
|
|
t.Errorf("did not expect discontinuity indicator to be set for clip: %v", clipNo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expectedCC = (expectedCC + 1) & 0xf
|
2019-02-16 17:01:30 +03:00
|
|
|
}
|
2019-02-15 18:10:35 +03:00
|
|
|
}
|