stream/mts/psi: changed HasDescriptor to also return index of descriptor in psi. Wrote func called SetProgInfoLen to set the program info length in a pmt. Started writing deleteDescriptor func to get rid of a descriptor.

This commit is contained in:
saxon 2019-01-29 14:38:23 +10:30
parent a61bdac2de
commit 4a3464252b
2 changed files with 32 additions and 11 deletions

View File

@ -128,7 +128,7 @@ func TestHasDescriptor(t *testing.T) {
p := PSIBytes(tstPsi3.Bytes()) p := PSIBytes(tstPsi3.Bytes())
// Try getting descriptor that exists // Try getting descriptor that exists
got := p.HasDescriptor(LocationDescTag) _, got := p.HasDescriptor(LocationDescTag)
want := []byte{ want := []byte{
LocationDescTag, LocationDescTag,
LocationDataSize, LocationDataSize,
@ -141,7 +141,7 @@ func TestHasDescriptor(t *testing.T) {
// Try getting descriptor that doesnt exist // Try getting descriptor that doesnt exist
const fakeTag = 236 const fakeTag = 236
got = p.HasDescriptor(fakeTag) _, got = p.HasDescriptor(fakeTag)
want = nil want = nil
if !bytes.Equal(got, want) { if !bytes.Equal(got, want) {
@ -151,7 +151,7 @@ func TestHasDescriptor(t *testing.T) {
// Try getting descriptor from psi that has no descriptors // Try getting descriptor from psi that has no descriptors
p = PSIBytes(tstPsi2.Bytes()) p = PSIBytes(tstPsi2.Bytes())
got = p.HasDescriptor(LocationDescTag) _, got = p.HasDescriptor(LocationDescTag)
want = nil want = nil
if !bytes.Equal(got, want) { if !bytes.Equal(got, want) {
@ -219,6 +219,10 @@ func TestCreateDescriptor(t *testing.T) {
} }
} }
func TestDeleteDescriptor(t *testing.T) {
}
func TestUpdateDescriptor(t *testing.T) { func TestUpdateDescriptor(t *testing.T) {
} }

View File

@ -245,7 +245,7 @@ func (p *PSIBytes) AddDescriptor(tag int, data []byte) error {
return errors.New("trying to add descriptor, but not pmt") return errors.New("trying to add descriptor, but not pmt")
} }
desc := p.HasDescriptor(tag) _, desc := p.HasDescriptor(tag)
if desc == nil { if desc == nil {
p.createDescriptor(tag, data) p.createDescriptor(tag, data)
return nil return nil
@ -258,17 +258,17 @@ func (p *PSIBytes) ProgramInfoLen() int {
return int((((*p)[ProgramInfoLenIdx1] & ProgramInfoLenMask1) << 8) | (*p)[ProgramInfoLenIdx2]) return int((((*p)[ProgramInfoLenIdx1] & ProgramInfoLenMask1) << 8) | (*p)[ProgramInfoLenIdx2])
} }
func (p *PSIBytes) HasDescriptor(tag int) Descriptor { func (p *PSIBytes) HasDescriptor(tag int) (int, Descriptor) {
descs := p.descriptors() descs := p.descriptors()
if descs == nil { if descs == nil {
return nil return -1, nil
} }
for i := 0; i < len(descs); i += 2 + int(descs[i+1]) { for i := 0; i < len(descs); i += 2 + int(descs[i+1]) {
if int(descs[i]) == tag { if int(descs[i]) == tag {
return descs[i : i+2+int(descs[i+1])] return i, descs[i : i+2+int(descs[i+1])]
} }
} }
return nil return -1, nil
} }
func (p *PSIBytes) descriptors() []byte { func (p *PSIBytes) descriptors() []byte {
@ -283,8 +283,10 @@ func (p *PSIBytes) createDescriptor(tag int, data []byte) {
// Calculate the new descriptors index and length. // Calculate the new descriptors index and length.
newDescIdx := DescriptorsIdx + curProgLen newDescIdx := DescriptorsIdx + curProgLen
newDescLen := dataLen + 2 newDescLen := dataLen + 2
// Copy data down from newDescIdx to create room for the new descriptor. // Copy data down from newDescIdx to create room for the new descriptor.
copy((*p)[newDescIdx+newDescLen:], (*p)[newDescIdx:newDescIdx+newDescLen]) copy((*p)[newDescIdx+newDescLen:], (*p)[newDescIdx:newDescIdx+newDescLen])
// Set the tag, data len and data of the new desriptor. // Set the tag, data len and data of the new desriptor.
(*p)[newDescIdx] = byte(tag) (*p)[newDescIdx] = byte(tag)
(*p)[newDescIdx+1] = byte(dataLen) (*p)[newDescIdx+1] = byte(dataLen)
@ -294,9 +296,7 @@ func (p *PSIBytes) createDescriptor(tag int, data []byte) {
// TODO: put this in function set program info length // TODO: put this in function set program info length
addedLen := dataLen + 2 addedLen := dataLen + 2
newProgInfoLen := curProgLen + addedLen newProgInfoLen := curProgLen + addedLen
(*p)[ProgramInfoLenIdx1] &= 0xff ^ ProgramInfoLenMask1 p.SetProgInfoLen(newProgInfoLen)
(*p)[ProgramInfoLenIdx1] |= byte(newProgInfoLen>>8) & ProgramInfoLenMask1
(*p)[ProgramInfoLenIdx2] = byte(newProgInfoLen)
// set section length // set section length
// TODO: put this in func set program info length // TODO: put this in func set program info length
@ -306,6 +306,7 @@ func (p *PSIBytes) createDescriptor(tag int, data []byte) {
(*p)[SyntaxSecLenIdx2] = byte(newSyntaxSectionLen) (*p)[SyntaxSecLenIdx2] = byte(newSyntaxSectionLen)
} }
// TODO: make this safer. If no padding, does this index out of range ?
func (p *PSIBytes) trimPadding() []byte { func (p *PSIBytes) trimPadding() []byte {
sectionLength := SyntaxSecLenFrom(*p) sectionLength := SyntaxSecLenFrom(*p)
paddingIdx := (4 + sectionLength) paddingIdx := (4 + sectionLength)
@ -313,6 +314,22 @@ func (p *PSIBytes) trimPadding() []byte {
return o return o
} }
func (p *PSIBytes) SetProgInfoLen(l int) {
// TODO: check if pmt first
(*p)[ProgramInfoLenIdx1] &= 0xff ^ ProgramInfoLenMask1
(*p)[ProgramInfoLenIdx1] |= byte(l>>8) & ProgramInfoLenMask1
(*p)[ProgramInfoLenIdx2] = byte(l)
}
func (p *PSIBytes) deleteDescriptor(tag int) error {
if i, desc := p.HasDescriptor(tag); desc != nil {
descLen := len(desc)
newProgInfoLen := p.ProgramInfoLen() - descLen
p.SetProgInfoLen(newProgInfoLen)
}
return errors.New("Descriptor doesn't exist")
}
func (d *Descriptor) update(data []byte) { func (d *Descriptor) update(data []byte) {
if len(data) > int((*d)[1]) { if len(data) > int((*d)[1]) {
// TODO: implement resizing of descriptor // TODO: implement resizing of descriptor