Merged in packet-conv (pull request #462)

mts: added function for converting byte slice to Packet to use comcast/gots code

Approved-by: kortschak
This commit is contained in:
Trek Hopton 2021-03-31 10:32:06 +00:00
commit 44b693f005
2 changed files with 28 additions and 6 deletions

View File

@ -6,11 +6,12 @@ NAME
DESCRIPTION DESCRIPTION
See Readme.md See Readme.md
AUTHOR AUTHORS
Saxon A. Nelson-Milton <saxon.milton@gmail.com> Saxon A. Nelson-Milton <saxon.milton@gmail.com>
Trek Hopton <trek@ausocean.org>
LICENSE LICENSE
mpegts.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean) mpegts.go is Copyright (C) 2017-2021 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the under the terms of the GNU General Public License as published by the
@ -31,6 +32,7 @@ package mts
import ( import (
"fmt" "fmt"
"unsafe"
"github.com/Comcast/gots/packet" "github.com/Comcast/gots/packet"
gotspsi "github.com/Comcast/gots/psi" gotspsi "github.com/Comcast/gots/psi"
@ -374,6 +376,16 @@ func asByte(b bool) byte {
return 0 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
// when it becomes available in Go 1.17.
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) type Option func(p *packet.Packet)
// addAdaptationField adds an adaptation field to p, and applys the passed options to this field. // addAdaptationField adds an adaptation field to p, and applys the passed options to this field.
@ -410,7 +422,7 @@ func resetAdaptation(p *packet.Packet) error {
return nil return nil
} }
// DiscontinuityIndicator returns and Option that will set p's discontinuity // DiscontinuityIndicator returns an Option that will set p's discontinuity
// indicator according to f. // indicator according to f.
func DiscontinuityIndicator(f bool) Option { func DiscontinuityIndicator(f bool) Option {
return func(p *packet.Packet) { return func(p *packet.Packet) {
@ -638,13 +650,13 @@ func SegmentForMeta(d []byte, key, val string) ([][]byte, error) {
// We've reached the end of the entire MTS clip so if we're segmenting we need // We've reached the end of the entire MTS clip so if we're segmenting we need
// to append current segment to res. // to append current segment to res.
if segmenting { if segmenting {
res = append(res, d[start:len(d)]) res = append(res, d[start:])
} }
return res, nil return res, nil
} }
// pid returns the packet identifier for the given packet. // PID returns the packet identifier for the given packet.
func PID(p []byte) (uint16, error) { func PID(p []byte) (uint16, error) {
if len(p) < PacketSize { if len(p) < PacketSize {
return 0, errors.New("packet length less than 188") return 0, errors.New("packet length less than 188")

View File

@ -7,9 +7,10 @@ DESCRIPTION
AUTHORS AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org> Saxon A. Nelson-Milton <saxon@ausocean.org>
Trek Hopton <trek@ausocean.org>
LICENSE LICENSE
Copyright (C) 2019 the Australian Ocean Lab (AusOcean) Copyright (C) 2019-2021 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the under the terms of the GNU General Public License as published by the
@ -768,3 +769,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")
}
}