mts: add function for converting byte slice to packet.Packet

This commit is contained in:
Trek H 2021-03-31 20:31:16 +10:30
parent 86e88c0f43
commit ed40392bb5
2 changed files with 19 additions and 0 deletions

View File

@ -31,6 +31,7 @@ package mts
import (
"fmt"
"unsafe"
"github.com/Comcast/gots/packet"
gotspsi "github.com/Comcast/gots/psi"
@ -374,6 +375,15 @@ func asByte(b bool) byte {
return 0
}
// GotsPacket takes a byte slice and returns a Packet as defined in github.com/comcast/gots/packet.
// TODO: replace this with a type conversion as described here: https://github.com/golang/go/issues/395
func GotsPacket(b []byte) *packet.Packet {
if len(b) != packet.PacketSize {
panic("invalid packet size")
}
return *(**packet.Packet)(unsafe.Pointer(&b))
}
type Option func(p *packet.Packet)
// addAdaptationField adds an adaptation field to p, and applys the passed options to this field.

View File

@ -768,3 +768,12 @@ func TestFindPSI(t *testing.T) {
}
}
}
func TestGotsPacket(t *testing.T) {
b := make([]byte, 188)
p := GotsPacket(b)
p.SetPID(PIDAudio)
if p.PID() != PIDAudio {
t.Error("failed to set PID in gots Packet")
}
}