mts: reordered, neatened and clarified code.

This commit is contained in:
Trek H 2019-04-10 17:18:42 +09:30
parent 669a7d3c22
commit 7c990b3bb5
3 changed files with 43 additions and 43 deletions

View File

@ -29,9 +29,10 @@ import (
"io/ioutil"
"testing"
"bitbucket.org/ausocean/av/container/mts/meta"
"github.com/Comcast/gots/packet"
"github.com/Comcast/gots/pes"
"bitbucket.org/ausocean/av/container/mts/meta"
)
// TestEncodePcm tests the mpegts encoder's ability to encode pcm audio data.
@ -39,13 +40,12 @@ import (
func TestEncodePcm(t *testing.T) {
Meta = meta.New()
var b []byte
buf := bytes.NewBuffer(b)
var buf bytes.Buffer
sampleRate := 48000
sampleSize := 2
blockSize := 16000
writeFreq := float64(sampleRate*sampleSize) / float64(blockSize)
e := NewEncoder(buf, writeFreq, Audio)
e := NewEncoder(&buf, writeFreq, Audio)
inPath := "../../../test/test-data/av/input/sweep_400Hz_20000Hz_-3dBFS_5s_48khz.pcm"
inPcm, err := ioutil.ReadFile(inPath)
@ -84,11 +84,34 @@ func TestEncodePcm(t *testing.T) {
for i+PacketSize <= len(clip) {
// Check MTS packet
if pkt.PID() == audioPid {
if pkt.PayloadUnitStartIndicator() {
if !(pkt.PID() == audioPid) {
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
continue
}
if !pkt.PayloadUnitStartIndicator() {
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
} else {
// Copy the first MTS payload
payload, err := pkt.Payload()
if err != nil {
t.Errorf("unable to get MTS payload: %v", err)
}
pesPacket = append(pesPacket, payload...)
// Copy the first MTS payload
payload, err := pkt.Payload()
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
// Copy the rest of the MTS payloads that are part of the same PES packet
for (!pkt.PayloadUnitStartIndicator()) && i+PacketSize <= len(clip) {
payload, err = pkt.Payload()
if err != nil {
t.Errorf("unable to get MTS payload: %v", err)
}
@ -98,39 +121,15 @@ func TestEncodePcm(t *testing.T) {
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
// Copy the rest of the MTS payloads that are part of the same PES packet
for (!pkt.PayloadUnitStartIndicator()) && i+PacketSize <= len(clip) {
payload, err = pkt.Payload()
if err != nil {
t.Errorf("unable to get MTS payload: %v", err)
}
pesPacket = append(pesPacket, payload...)
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
}
} else {
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
}
// Get the audio data from the current PES packet
pesHeader, err := pes.NewPESHeader(pesPacket)
if err != nil {
t.Errorf("unable to read PES packet: %v", err)
}
got = append(got, pesHeader.Data()...)
pesPacket = pesPacket[:0]
} else {
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])
}
}
// Get the audio data from the current PES packet
pesHeader, err := pes.NewPESHeader(pesPacket)
if err != nil {
t.Errorf("unable to read PES packet: %v", err)
}
got = append(got, pesHeader.Data()...)
pesPacket = pesPacket[:0]
}
// Compare data from MTS with original data.

View File

@ -147,8 +147,9 @@ type Encoder struct {
psiLastTime time.Time
}
// NewEncoder returns an Encoder with the specified frame rate.
func NewEncoder(dst io.Writer, writeFreq float64, mediaType int) *Encoder {
// 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.Writer, rate float64, mediaType int) *Encoder {
var mPid int
var sid byte
switch mediaType {
@ -163,7 +164,7 @@ func NewEncoder(dst io.Writer, writeFreq float64, mediaType int) *Encoder {
return &Encoder{
dst: dst,
writePeriod: time.Duration(float64(time.Second) / writeFreq),
writePeriod: time.Duration(float64(time.Second) / rate),
ptsOffset: ptsOffset,
timeBasedPsi: true,

View File

@ -26,7 +26,7 @@ LICENSE
package pes
const MaxPesSize = 65536
const MaxPesSize = 64 * 1 << 10
/*
The below data struct encapsulates the fields of an PES packet. Below is