mirror of https://bitbucket.org/ausocean/av.git
140 lines
3.3 KiB
Go
140 lines
3.3 KiB
Go
/*
|
|
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
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
*/
|
|
package revid
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"bitbucket.org/ausocean/av/stream/mts"
|
|
"bitbucket.org/ausocean/utils/logger"
|
|
"bitbucket.org/ausocean/utils/ring"
|
|
"github.com/Comcast/gots/packet"
|
|
)
|
|
|
|
// Ring buffer sizes and read/write timeouts.
|
|
const (
|
|
rbSize = 100
|
|
rbElementSize = 150000
|
|
wTimeout = 10 * time.Millisecond
|
|
rTimeout = 10 * time.Millisecond
|
|
)
|
|
|
|
type testSender struct {
|
|
Buf [][]byte
|
|
}
|
|
|
|
func (ts *testSender) send(d []byte) error {
|
|
ts.Buf = append(ts.Buf, d)
|
|
return nil
|
|
}
|
|
|
|
func log(lvl int8, msg string, args ...interface{}) {
|
|
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"
|
|
}
|
|
fmt.Printf(msg, args)
|
|
}
|
|
|
|
type Chunk struct {
|
|
buf []byte
|
|
off int
|
|
owner *ring.Buffer
|
|
}
|
|
|
|
func TestSegment(t *testing.T) {
|
|
// Create ringbuffer tst sender, loadsender and the mpegts encoder
|
|
rb := ring.NewBuffer(rbSize, rbElementSize, wTimeout)
|
|
tstSender := &testSender{}
|
|
loadSender := newMtsSender(tstSender, log)
|
|
encoder := mts.NewEncoder(rb, 25)
|
|
|
|
// Turn time based psi writing off for encoder
|
|
const psiSendCount = 10
|
|
encoder.TimeBasedPsi(false, psiSendCount)
|
|
|
|
const noOfPacketsToWrite = 100
|
|
for i := 0; i < noOfPacketsToWrite; i++ {
|
|
// Our payload will just be packet no
|
|
encoder.Encode([]byte{byte(i)})
|
|
|
|
next, err := rb.Next(rTimeout)
|
|
if err != nil {
|
|
t.Errorf("Unexpected err: %v\n", err)
|
|
}
|
|
|
|
err = loadSender.load(next)
|
|
if err != nil {
|
|
t.Errorf("Unexpected err: %v\n", err)
|
|
}
|
|
|
|
err = loadSender.send()
|
|
if err != nil {
|
|
t.Errorf("Unexpected err: %v\n", err)
|
|
}
|
|
}
|
|
|
|
result := tstSender.Buf
|
|
for clipNo, clip := range result {
|
|
// Check that the clip is the right length
|
|
clipLen := len(clip)
|
|
if clipLen != psiSendCount {
|
|
t.Errorf("Clip %v is not correct length. Got: %v Want: %v\n Clip: %v\n", clipNo, clipLen, psiSendCount, clip)
|
|
}
|
|
|
|
// Also check that the first packet is a PAT
|
|
firstPkt := clip[:mts.PacketSize]
|
|
var pkt [mts.PacketSize]byte
|
|
copy(pkt[:], firstPkt)
|
|
pid := (*packet.Packet)(&pkt).PID()
|
|
if pid != mts.PatPid {
|
|
t.Errorf("First packte of clip %v is not pat, but rather: %v\n", clipNo, pid)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDiscontinuity(t *testing.T) {
|
|
|
|
}
|