stream/mts: created new type to represent Descriptor (typedef of []bytes) so that we can create receiver functions. Wrote AddDescriptor func to add or update a descriptor in a pmt. Wrote ProgramInfoLen func to return the program info length i.e. len of descriptors. Wrote HasDescriptor to check if descriptor exists, if so return the descriptor so that we can update. Wrote descriptors which returns []byte of all descriptors. Wrote create descriptor, which adds a descriptor to the existing if any i.e. shifts data downwards to accomodate new data. Wrote update func to update a descriptor.

This commit is contained in:
saxon 2019-01-27 16:55:00 +10:30
parent 6f421ab706
commit 9171b56d31
2 changed files with 72 additions and 24 deletions

View File

@ -253,6 +253,6 @@ func updateMeta(b []byte) error {
var p psi.PSIBytes var p psi.PSIBytes
p = b p = b
m := meta.Format() m := meta.Format()
p.AddDescriptor(psi.MetadataTag, len(m), m) p.Descriptor(psi.MetadataTag, len(m), m)
return nil return nil
} }

View File

@ -85,29 +85,10 @@ const (
const MetadataTag = 0x26 const MetadataTag = 0x26
type PSIBytes []byte type (
PSIBytes []byte
func (p *PSIBytes) AddDescriptor(tag int, data []byte) error { Descriptor []byte
// first check that we actually have table syntax section )
// NB: if table syntax section doesn't exist (unlikely I think) then we could
// just add it
// Check that this is actually a PMT
if psi.TableID(*p) != pmtID {
return errors.New("trying to add descriptor, but not pmt")
}
if !p.editDesc(tag, data) {
}
// if exists update
// otherwise add
return nil
}
func (p *PSIBytes) editDesc(tag int, data []byte) bool {
return true
}
// Program specific information // Program specific information
type PSI struct { type PSI struct {
@ -246,3 +227,70 @@ func asByte(b bool) byte {
} }
return 0x00 return 0x00
} }
func (p *PSIBytes) AddDescriptor(tag int, data []byte) error {
if psi.TableID(*p) != pmtID {
return errors.New("trying to add descriptor, but not pmt")
}
desc := p.HasDescriptor(tag)
if desc == nil {
p.createDescriptor(tag, data)
return nil
}
desc.update(data)
return nil
}
func (p *PSIBytes) ProgramInfoLen() int {
return int((((*p)[ProgramInfoLenIdx1] & ProgramInfoLenMask1) << 8) | (*p)[ProgramInfoLenIdx2])
}
func (p *PSIBytes) HasDescriptor(int tag) Descriptor {
descs := p.descriptors()
if descs == nil {
return nil
}
for i := 0; i < len(descs); {
t := descs[i]
if t == tag {
return esc[i : i+int(descs[i+1])]
}
i += 1 + desc[i+1]
}
return nil
}
func (p *PSIBytes) descriptors() []byte {
return p[DescriptorsIdx : DescriptorsIdx+p.ProgramInfoLen()]
}
func (p *PSIBytes) createDescriptor(tag, data) {
curProgLen := p.ProgramInfoLen()
dataLen := len(data)
// Calculate the new descriptors index and length.
newDescIdx := DescriptorsIdx + curProgLen
newDescLen := dLen + 2
// Copy data down from newDescIdx to create room for the new descriptor.
copy(p[newDescIdx+newDescLen:], p[newDescIdx:dataLen-newDescLen])
// Set the tag, data len and data of the new desriptor.
p[newDescIdx] = tag
p[newDescIdx+1] = dataLen
copy(p[newDescIdx+2:newDescIdx+2+dataLen], data)
// Update the program info length to account for the new descriptor.
newProgInfoLen := curProgLen + dataLen
p[ProgramInfoLenIdx1] = newProgInfoLen >> 8
p[ProgramInfoLenIdx2] = byte(newProgInfoLen)
}
func (d *Descriptor) update(data) {
if len(data) > d[1] {
// TODO: implement resizing of descriptor
panic("Can't resize descriptor data")
}
}