stream/mts/psi: finished writing AddDescriptor

This commit is contained in:
saxon 2019-01-29 16:14:00 +10:30
parent 252e6680ed
commit 1786ed2661
3 changed files with 53 additions and 33 deletions

View File

@ -196,12 +196,12 @@ func (e *Encoder) Encode(nalu []byte) error {
func (e *Encoder) writePSI() error {
// Write PAT.
patPkt := Packet{
PUSI: true,
PID: PatPid,
CC: e.ccFor(PatPid),
AFC: HasPayload,
Payload: patTable,
PUSI: true,
PID: PatPid,
CC: e.ccFor(PatPid),
AFC: HasPayload,
}
patPkt.FillPayload(patTable)
_, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil {
return err
@ -211,12 +211,12 @@ func (e *Encoder) writePSI() error {
// Create mts packet from pmt table.
pmtPkt := Packet{
PUSI: true,
PID: PmtPid,
CC: e.ccFor(PmtPid),
AFC: HasPayload,
Payload: pmtTable,
PUSI: true,
PID: PmtPid,
CC: e.ccFor(PmtPid),
AFC: HasPayload,
}
pmtPkt.FillPayload(pmtTable)
_, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil {
return err

View File

@ -26,6 +26,12 @@ LICENSE
package psi
import (
"errors"
"github.com/Comcast/gots/psi"
)
const (
PacketSize = 184 // packet size of a psi.
)
@ -234,31 +240,45 @@ func asByte(b bool) byte {
}
func (p *PSIBytes) AddDescriptor(tag int, data []byte) error {
/*
if psi.TableID(*p) != pmtID {
return errors.New("trying to add descriptor, but not pmt")
}
if psi.TableID(*p) != pmtID {
return errors.New("trying to add descriptor, but not pmt")
}
i, desc := p.HasDescriptor(tag)
if desc == nil {
p.createDescriptor(tag, data)
return nil
}
oldDescLen := desc.len()
// update the descriptor
oldDataLen := int(desc[1])
newDataLen := len(data)
newDescLen := 2 + newDataLen
i, desc := p.HasDescriptor(tag)
if desc == nil {
p.createDescriptor(tag, data)
return nil
}
// Shift data
// copy(p[i+newDescLen:], data)
// Old lengths
oldDescLen := desc.len()
oldDataLen := int(desc[1])
// New lengths
newDataLen := len(data)
newDescLen := 2 + newDataLen
delta := newDescLen - oldDescLen
if oldDataLen > newDataLen {
copy((*p)[i+newDescLen:], (*p)[i+oldDescLen:])
*p = (*p)[:len(*p)+delta]
} else if oldDataLen < newDataLen {
tmp := make([]byte, len(*p)+delta)
copy(tmp, *p)
*p = tmp
copy((*p)[i+newDescLen:], (*p)[i+oldDescLen:])
} else {
copy((*p)[i+2:], data)
}
newProgInfoLen := p.ProgramInfoLen() + delta
p.SetProgInfoLen(newProgInfoLen)
newSectionLen := int(psi.SectionLength(*p)) + delta
p.SetSectionLen(newSectionLen)
deltaLen := desc.len() - oldDescLen
newProgInfoLen := p.ProgramInfoLen() + deltaLen
p.SetProgInfoLen(newProgInfoLen)
newSectionLen := int(psi.SectionLength(*p)) + deltaLen
p.SetSectionLen(newSectionLen)
*/
return nil
}

View File

@ -282,7 +282,7 @@ var bytesTests = []struct {
func TestBytes(t *testing.T) {
for _, test := range bytesTests {
got := test.input.Bytes()
if !bytes.Equal(got, addPadding(addCrc(test.want))) {
if !bytes.Equal(got, addCrc(test.want)) {
t.Errorf("unexpected error for test %v: got:%v want:%v", test.name, got,
test.want)
}