av/stream/mts/psi/psi_test.go

215 lines
4.9 KiB
Go
Raw Normal View History

2018-12-07 08:23:38 +03:00
package psi
import (
"bytes"
2018-12-07 08:23:38 +03:00
"testing"
)
const (
tstTime = 1235367435 // hex = 49A2360B
tstTime2 = 1735357535 // hex = 676F745F
)
const (
errCmp = "Incorrect output, for: %v wanted: %v, got: %v"
)
2018-12-10 10:12:45 +03:00
var (
timeSlice = []byte{
0x00, 0x00, 0x00, 0x00, 0x49, 0xA2, 0x36, 0x0B,
}
patPsi1 = PSI{
2018-12-10 10:12:45 +03:00
Pf: 0x00,
Tid: 0x00,
Ssi: true,
Pb: false,
Sl: uint16(0x0d),
Tss: &TSS{
Tide: uint16(0x01),
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PAT{
Pn: uint16(0x01),
Pmpid: uint16(0x1000),
2018-12-10 10:12:45 +03:00
},
},
}
pmtPsi1 = PSI{
Pf: 0x00,
2018-12-10 10:12:45 +03:00
Tid: 0x02,
Ssi: true,
Sl: uint16(0x12),
Tss: &TSS{
2018-12-10 10:12:45 +03:00
Tide: uint16(0x01),
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
2018-12-10 10:12:45 +03:00
Sd: &PMT{
Pcrpid: 0x0100, // wrong
Pil: 0,
2018-12-10 10:12:45 +03:00
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
2018-12-10 10:12:45 +03:00
}
// pmt with descriptor in it
StdPmtTime = []byte{
0x00, // pointer
// ---- section included in data sent to CRC32 during check
// table header
0x02, // table id
0xb0, // section syntax indicator:1|private bit:1|reserved:2|section length:2|more bytes...:2
0x12, // more bytes...
// syntax section
0x00, 0x01, // table id extension
0xc1, // reserved bits:3|version:5|use now:1
0x00, // section number
0x00, // last section number
// table data
0xe1, 0x00, // reserved:3|PCR PID:13
0xf0, 0x0a, // reserved:4|unused:2|program info length:10
// Desriptor
byte(timeDescTag), // Descriptor tag
byte(8), // Length of bytes to follow
0x00, 0x00, 0x00, 0x00, 0x49, 0xA2, 0x36, 0x0B, // timestamp
// No program descriptors since program info length is 0.
// elementary stream info data
0x1b, // stream type
0xe1, 0x00, // reserved:3|elementary PID:13
0xf0, 0x00, // reserved:4|unused:2|ES info length:10
// No elementary stream descriptors since ES info length is 0.
// 0x15, 0xbd, 0x4d, 0x56, // CRC
// ----
}
pmtPsiTime = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: uint16(0x12),
Tss: &TSS{
Tide: uint16(0x01),
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PMT{
Pcrpid: 0x0100, // wrong
Pil: 10,
Pd: []Desc{
Desc{
Dt: byte(timeDescTag),
Dl: byte(8),
Dd: TimeToBytes(tstTime),
},
},
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
}
// pmt with descriptor in it
StdPmtTime2 = []byte{
0x00, // pointer
// ---- section included in data sent to CRC32 during check
// table header
0x02, // table id
0xb0, // section syntax indicator:1|private bit:1|reserved:2|section length:2|more bytes...:2
0x12, // more bytes...
// syntax section
0x00, 0x01, // table id extension
0xc1, // reserved bits:3|version:5|use now:1
0x00, // section number
0x00, // last section number
// table data
0xe1, 0x00, // reserved:3|PCR PID:13
0xf0, 0x0a, // reserved:4|unused:2|program info length:10
// Desriptor
byte(timeDescTag), // Descriptor tag
byte(8), // Length of bytes to follow
0x00, 0x00, 0x00, 0x00, 0x67, 0x6F, 0x74, 0x5F, // timestamp
// No program descriptors since program info length is 0.
// elementary stream info data
0x1b, // stream type
0xe1, 0x00, // reserved:3|elementary PID:13
0xf0, 0x00, // reserved:4|unused:2|ES info length:10
// No elementary stream descriptors since ES info length is 0.
// 0x15, 0xbd, 0x4d, 0x56, // CRC
// ----
}
2018-12-10 10:12:45 +03:00
)
// Test Bytes for a standard pat
func TestBytesPAT1(t *testing.T) {
got := patPsi1.Bytes()
// Remove crc32
got = got[:len(got)-4]
if !bytes.Equal(StdPat, got) {
t.Errorf(errCmp, "TestBytesPAT1", StdPat, got)
}
}
2018-12-07 08:23:38 +03:00
// Test Bytes for a standard pmt
func TestBytesPMT1(t *testing.T) {
got := pmtPsi1.Bytes()
// Remove crc32
got = got[:len(got)-4]
if !bytes.Equal(StdPmt, got) {
t.Errorf(errCmp, "TestBytesPMT1", StdPmt, got)
}
2018-12-07 08:23:38 +03:00
}
// A quick sanity check of the int64 time to []byte func
func TestTimestampToBytes(t *testing.T) {
timeBytes := TimeToBytes(tstTime)
if !bytes.Equal(timeSlice, timeBytes) {
t.Errorf(errCmp, "testTimeStampToBytes", timeSlice, timeBytes)
}
}
// Test Bytes for a a standard PMT with a descripot containing a timestamp
func TestBytesPmt2(t *testing.T) {
got := pmtPsiTime.Bytes()
// Remove crc32
got = got[:len(got)-4]
if !bytes.Equal(StdPmtTime, got) {
t.Errorf(errCmp, "testBytesPmt2", StdPmtTime, got)
}
}
func TestTimeUpdate(t *testing.T) {
cpy := make([]byte, len(StdPmtTime))
copy(cpy, StdPmtTime)
err := UpdateTime(cpy, tstTime2)
if err != nil {
t.Errorf("Update time returned err: %v", err)
}
if !bytes.Equal(StdPmtTime2, cpy) {
t.Errorf(errCmp, "TestTimeUpdate", StdPmtTime2, cpy)
}
}
func TestTimeGet(t *testing.T) {
s, err := GetTime(StdPmtTime)
if err != nil {
t.Errorf("Getting timestamp failed with err: %v", err)
}
if s != tstTime {
t.Errorf(errCmp, "TestTimeGet", tstTime, s)
}
}