diff --git a/cli/parsefile.go b/cli/parsefile.go index d735b97..e27fbe9 100644 --- a/cli/parsefile.go +++ b/cli/parsefile.go @@ -35,7 +35,6 @@ import ( "github.com/Comcast/gots/ebp" "github.com/Comcast/gots/packet" - "github.com/Comcast/gots/packet/adaptationfield" "github.com/Comcast/gots/psi" "github.com/Comcast/gots/scte35" ) @@ -138,7 +137,7 @@ func main() { } if *showEbp { - ebpBytes, err := adaptationfield.EncoderBoundaryPoint(&pkt) + ebpBytes, err := packet.EncoderBoundaryPointBytes(&pkt) if err != nil { // Not an EBP continue diff --git a/ebp/ebp.go b/ebp/ebp.go index bffbf2f..b8087d7 100644 --- a/ebp/ebp.go +++ b/ebp/ebp.go @@ -26,9 +26,10 @@ package ebp import ( "encoding/binary" - "github.com/Comcast/gots" "io" "time" + + "github.com/Comcast/gots" ) // EBP tags diff --git a/packet/adaptationfield.go b/packet/adaptationfield.go index 854026b..e7a0a8c 100644 --- a/packet/adaptationfield.go +++ b/packet/adaptationfield.go @@ -28,7 +28,7 @@ import ( "github.com/Comcast/gots" ) -// NewPacket creates a new packet with a Null ID, sync byte, and with the adaptation field control set to payload only. +// NewAdaptationField creates a new *AdaptationField with the flag set for AdaptationField and no payload // This function is error free. func NewAdaptationField() *AdaptationField { p := New() @@ -305,8 +305,7 @@ func (af *AdaptationField) ElementaryStreamPriority() (bool, error) { return af.getBit(5, 0x20), nil } -// SetHasPCR sets HasPCR -// HasPCR determines if the packet has a PCR +// SetHasPCR sets HasPCR flag func (af *AdaptationField) SetHasPCR(value bool) error { if err := af.valid(); err != nil { return err @@ -320,7 +319,7 @@ func (af *AdaptationField) SetHasPCR(value bool) error { return nil } -// HasPCR returns if the packet has a PCR +// HasPCR returns true if the packet has a PCR func (af *AdaptationField) HasPCR() (bool, error) { if err := af.valid(); err != nil { return false, err @@ -564,3 +563,27 @@ func (af *AdaptationField) AdaptationFieldExtension() ([]byte, error) { } return af[af.adaptationExtensionStart():af.stuffingStart()], nil } + +// EncoderBoundaryPointBytes returns the byte array located in the optional TransportPrivateData of the (also optional) +// AdaptationField of the Packet. If either of these optional fields are missing an empty byte array is returned with an error +func EncoderBoundaryPointBytes(packet *Packet) ([]byte, error) { + + af, err := packet.AdaptationField() + if err != nil { + return nil, err + } + + hasTransPriv, err := af.HasTransportPrivateData() + if err != nil { + return nil, err + } + + if af.Length() > 0 && hasTransPriv { + ebp, err := af.TransportPrivateData() + if err != nil { + return nil, err + } + return ebp, nil + } + return nil, gots.ErrNoEBP +} diff --git a/packet/adaptationfield/adaptationfield.go b/packet/adaptationfield/adaptationfield.go deleted file mode 100644 index 6fe23fc..0000000 --- a/packet/adaptationfield/adaptationfield.go +++ /dev/null @@ -1,136 +0,0 @@ -package adaptationfield - -import ( - "github.com/Comcast/gots" - "github.com/Comcast/gots/packet" -) - -// Length returns the length of the adaptation field in bytes -func Length(pkt *packet.Packet) uint8 { - return uint8(pkt[4]) -} - -// IsDiscontinuous returns the discontinuity indicator for this adaptation field -func IsDiscontinuous(pkt *packet.Packet) bool { - return pkt[5]&0x80 != 0 -} - -// IsRandomAccess returns the random access indicator for this adaptation field -func IsRandomAccess(pkt *packet.Packet) bool { - return pkt[5]&0x40 != 0 -} - -// IsESHigherPriority returns true if this elementary stream is -// high priority. Corresponds to the elementary stream -// priority indicator. -func IsESHigherPriority(pkt *packet.Packet) bool { - return pkt[5]&0x20 != 0 -} - -// HasPCR returns true when the PCR flag is set -func HasPCR(pkt *packet.Packet) bool { - return pkt[5]&0x10 != 0 -} - -// HasOPCR returns true when the OPCR flag is set -func HasOPCR(pkt *packet.Packet) bool { - return pkt[5]&0x08 != 0 -} - -// HasSplicingPoint returns true when the splicing countdown field is present -func HasSplicingPoint(pkt *packet.Packet) bool { - return pkt[5]&0x04 != 0 -} - -// HasTransportPrivateData returns true when the private data field is present -func HasTransportPrivateData(pkt *packet.Packet) bool { - return pkt[5]&0x02 != 0 -} - -// HasAdaptationFieldExtension returns true if this adaptation field contains an extension field -func HasAdaptationFieldExtension(pkt *packet.Packet) bool { - return pkt[5]&0x01 != 0 -} - -// EncoderBoundaryPoint returns the byte array located in the optional TransportPrivateData of the (also optional) -// AdaptationField of the Packet. If either of these optional fields are missing an empty byte array is returned with an error -func EncoderBoundaryPoint(pkt *packet.Packet) ([]byte, error) { - hasAdapt, err := packet.ContainsAdaptationField(pkt) - if err != nil { - return nil, nil - } - if hasAdapt && Length(pkt) > 0 && HasTransportPrivateData(pkt) { - ebp, err := TransportPrivateData(pkt) - if err != nil { - return nil, err - } - return ebp, nil - } - return nil, gots.ErrNoEBP -} - -// PCR is the Program Clock Reference. -// First 33 bits are PCR base. -// Next 6 bits are reserved. -// Final 9 bits are PCR extension. -func PCR(pkt *packet.Packet) ([]byte, error) { - if !HasPCR(pkt) { - return nil, gots.ErrNoPCR - } - offset := 6 - return pkt[offset : offset+6], nil -} - -// OPCR is the Original Program Clock Reference. -// First 33 bits are original PCR base. -// Next 6 bits are reserved. -// Final 9 bits are original PCR extension. -func OPCR(pkt *packet.Packet) ([]byte, error) { - if !HasOPCR(pkt) { - return nil, gots.ErrNoOPCR - } - offset := 6 - if HasPCR(pkt) { - offset += 6 - } - return pkt[offset : offset+6], nil -} - -// SpliceCountdown returns a count of how many packets after this one until -// a splice point occurs or an error if none exist. This function calls -// HasSplicingPoint to check for the existence of a splice countdown. -func SpliceCountdown(pkt *packet.Packet) (uint8, error) { - if !HasSplicingPoint(pkt) { - return 0, gots.ErrNoSplicePoint - } - offset := 6 - if HasPCR(pkt) { - offset += 6 - } - if HasOPCR(pkt) { - offset += 6 - } - return pkt[offset], nil -} - -// TransportPrivateData returns the private data from this adaptation field -// or an empty array and an error if there is none. This function calls -// HasTransportPrivateData to check for the existence of private data. -func TransportPrivateData(pkt *packet.Packet) ([]byte, error) { - if !HasTransportPrivateData(pkt) { - return nil, gots.ErrNoPrivateTransportData - } - offset := 6 - if HasPCR(pkt) { - offset += 6 - } - if HasOPCR(pkt) { - offset += 6 - } - if HasSplicingPoint(pkt) { - offset++ - } - dataLength := uint8(pkt[offset]) - offset++ - return pkt[uint8(offset) : uint8(offset)+dataLength], nil -} diff --git a/packet/adaptationfield/doc.go b/packet/adaptationfield/doc.go deleted file mode 100644 index e56e54e..0000000 --- a/packet/adaptationfield/doc.go +++ /dev/null @@ -1,26 +0,0 @@ -/* -MIT License - -Copyright 2016 Comcast Cable Communications Management, LLC - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -// AdaptationField provides functions for accessing and reading packet adaptation fields -package adaptationfield diff --git a/packet/doc.go b/packet/doc.go index dcf80a6..db8553f 100644 --- a/packet/doc.go +++ b/packet/doc.go @@ -49,8 +49,8 @@ const ( type AdaptationFieldControlOptions byte const ( - PayloadFlag AdaptationFieldControlOptions = 1 // 10 - AdaptationFieldFlag AdaptationFieldControlOptions = 2 // 01 + PayloadFlag AdaptationFieldControlOptions = 1 // 01 + AdaptationFieldFlag AdaptationFieldControlOptions = 2 // 10 PayloadAndAdaptationFieldFlag AdaptationFieldControlOptions = 3 // 11 ) diff --git a/packet/packet.go b/packet/packet.go index 81c13c8..66bbc41 100644 --- a/packet/packet.go +++ b/packet/packet.go @@ -24,7 +24,9 @@ SOFTWARE. package packet -import "github.com/Comcast/gots" +import ( + "github.com/Comcast/gots" +) // PayloadUnitStartIndicator (PUSI) is a flag that indicates the start of PES data // or PSI (Program-Specific Information) such as AT, CAT, PMT or NIT. The PUSI @@ -36,7 +38,7 @@ func payloadUnitStartIndicator(packet *Packet) bool { return packet[1]&0x040 != 0 } -// PID is the Packet Identifier. Each table or elementary stream in the +// Pid is the Packet Identifier. Each table or elementary stream in the // transport stream is identified by a PID. The PID is contained in the 13 // bits that span the last 5 bits of second byte and all bits in the byte. func Pid(packet *Packet) (uint16, error) { @@ -148,7 +150,7 @@ func SetCC(packet *Packet, newCC uint8) (*Packet, error) { return &newPacket, nil } -// Returns a byte slice containing the PES header if the Packet contains one, +// PESHeader Returns a byte slice containing the PES header if the Packet contains one, // otherwise returns an error func PESHeader(packet *Packet) ([]byte, error) { if containsPayload(packet) && payloadUnitStartIndicator(packet) {