mirror of https://bitbucket.org/ausocean/av.git
mts: Limited size of encoder writes and updated audio_test
Previously the encoder would not care if a write was given that exceeded the max PES packet size because we were never using PES packets bigger than a frame of video. Now I have changed it so that the encoder will check the write length and create a new PES packet if needed. I have also restructured my test so that it can extract the data from PES packets that span accross multiple MTS packets.
This commit is contained in:
parent
7e4e2fe7c6
commit
d15d0ddb86
|
@ -47,8 +47,8 @@ func TestEncodePcm(t *testing.T) {
|
||||||
buf := bytes.NewBuffer(b)
|
buf := bytes.NewBuffer(b)
|
||||||
sampleRate := 48000
|
sampleRate := 48000
|
||||||
sampleSize := 2
|
sampleSize := 2
|
||||||
writeSize := 16000
|
blockSize := 16000
|
||||||
writeFreq := float64(sampleRate*sampleSize) / float64(writeSize)
|
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"
|
inPath := "../../../test/test-data/av/input/sweep_400Hz_20000Hz_-3dBFS_5s_48khz.pcm"
|
||||||
|
@ -58,19 +58,33 @@ func TestEncodePcm(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode pcm to mts and get the resulting bytes.
|
// Encode pcm to mts and get the resulting bytes.
|
||||||
_, err = e.Write(inPcm)
|
for i := 0; i < len(inPcm); i += blockSize {
|
||||||
if err != nil {
|
if len(inPcm)-i < blockSize {
|
||||||
log.Fatal(err)
|
block := inPcm[i:]
|
||||||
|
_, err = e.Write(block)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
block := inPcm[i : i+blockSize]
|
||||||
|
_, err = e.Write(block)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
clip := buf.Bytes()
|
clip := buf.Bytes()
|
||||||
|
|
||||||
// Decode the mts packets to extract the original data
|
// Decode the mts packets to extract the original data
|
||||||
var pkt packet.Packet
|
var pkt packet.Packet
|
||||||
pesPacket := make([]byte, 0, writeSize)
|
pesPacket := make([]byte, 0, blockSize)
|
||||||
got := make([]byte, 0, len(inPcm))
|
got := make([]byte, 0, len(inPcm))
|
||||||
i := 0
|
i := 0
|
||||||
for i+PacketSize <= len(clip) {
|
if i+PacketSize <= len(clip) {
|
||||||
copy(pkt[:], clip[i:i+PacketSize])
|
copy(pkt[:], clip[i:i+PacketSize])
|
||||||
|
}
|
||||||
|
|
||||||
|
for i+PacketSize <= len(clip) {
|
||||||
if pkt.PID() == audioPid {
|
if pkt.PID() == audioPid {
|
||||||
if pkt.PayloadUnitStartIndicator() {
|
if pkt.PayloadUnitStartIndicator() {
|
||||||
payload, err := pkt.Payload()
|
payload, err := pkt.Payload()
|
||||||
|
@ -78,18 +92,28 @@ func TestEncodePcm(t *testing.T) {
|
||||||
t.Fatalf("Unexpected err: %v\n", err)
|
t.Fatalf("Unexpected err: %v\n", err)
|
||||||
}
|
}
|
||||||
pesPacket = append(pesPacket, payload...)
|
pesPacket = append(pesPacket, payload...)
|
||||||
|
|
||||||
i += PacketSize
|
i += PacketSize
|
||||||
first := true
|
if i+PacketSize <= len(clip) {
|
||||||
for (first || !pkt.PayloadUnitStartIndicator()) && i+PacketSize <= len(clip) {
|
|
||||||
first = false
|
|
||||||
copy(pkt[:], clip[i:i+PacketSize])
|
copy(pkt[:], clip[i:i+PacketSize])
|
||||||
payload, err := pkt.Payload()
|
}
|
||||||
|
|
||||||
|
for (!pkt.PayloadUnitStartIndicator()) && i+PacketSize <= len(clip) {
|
||||||
|
payload, err = pkt.Payload()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected err: %v\n", err)
|
t.Fatalf("Unexpected err: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pesPacket = append(pesPacket, payload...)
|
pesPacket = append(pesPacket, payload...)
|
||||||
|
|
||||||
i += PacketSize
|
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])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pesHeader, err := pes.NewPESHeader(pesPacket)
|
pesHeader, err := pes.NewPESHeader(pesPacket)
|
||||||
|
@ -97,8 +121,12 @@ func TestEncodePcm(t *testing.T) {
|
||||||
t.Fatalf("Unexpected err: %v\n", err)
|
t.Fatalf("Unexpected err: %v\n", err)
|
||||||
}
|
}
|
||||||
got = append(got, pesHeader.Data()...)
|
got = append(got, pesHeader.Data()...)
|
||||||
|
pesPacket = pesPacket[:0]
|
||||||
} else {
|
} else {
|
||||||
i += PacketSize
|
i += PacketSize
|
||||||
|
if i+PacketSize <= len(clip) {
|
||||||
|
copy(pkt[:], clip[i:i+PacketSize])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ LICENSE
|
||||||
package mts
|
package mts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -206,6 +207,9 @@ func (e *Encoder) TimeBasedPsi(b bool, sendCount int) {
|
||||||
// Write implements io.Writer. Write takes raw h264 and encodes into mpegts,
|
// Write implements io.Writer. Write takes raw h264 and encodes into mpegts,
|
||||||
// then sending it to the encoder's io.Writer destination.
|
// then sending it to the encoder's io.Writer destination.
|
||||||
func (e *Encoder) Write(data []byte) (int, error) {
|
func (e *Encoder) Write(data []byte) (int, error) {
|
||||||
|
if len(data) > pes.MaxPesSize {
|
||||||
|
return 0, fmt.Errorf("data size too large (Max is %v): %v", pes.MaxPesSize, len(data))
|
||||||
|
}
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
if (e.timeBasedPsi && (now.Sub(e.psiLastTime) > psiInterval)) || (!e.timeBasedPsi && (e.pktCount >= e.psiSendCount)) {
|
if (e.timeBasedPsi && (now.Sub(e.psiLastTime) > psiInterval)) || (!e.timeBasedPsi && (e.pktCount >= e.psiSendCount)) {
|
||||||
e.pktCount = 0
|
e.pktCount = 0
|
||||||
|
|
Loading…
Reference in New Issue