Documentation ¶
Overview ¶
Package packet is used for reading and manipulating packets in an MPEG transport stream
Index ¶
- Constants
- Variables
- func ContainsAdaptationField(packet *Packet) bool
- func ContainsPayload(packet *Packet) bool
- func ContinuityCounter(packet *Packet) uint8
- func Equal(a, b *Packet) bool
- func Header(packet *Packet) []byte
- func IsNull(packet *Packet) bool
- func IsPat(packet *Packet) bool
- func IsSynced(r Peeker) (ok bool, err error)
- func PESHeader(packet *Packet) ([]byte, error)
- func Payload(packet *Packet) ([]byte, error)
- func PayloadUnitStartIndicator(packet *Packet) bool
- func Pid(packet *Packet) int
- func SetPayload(pkt *Packet, pay []byte) int
- func Sync(r PeekScanner) (off int64, err error)
- func WithAFPrivateDataFlag(pkt *Packet)
- func WithContinuousAF(pkt *Packet)
- func WithDiscontinuousAF(pkt *Packet)
- func WithHasAdaptationFieldFlag(pkt *Packet)
- func WithHasPayloadFlag(pkt *Packet)
- func WithPES(pkt *Packet, pts uint64)
- func WithPUSI(pkt *Packet)
- type Accumulator
- type AdaptationField
- func (af *AdaptationField) AdaptationFieldExtension() ([]byte, error)
- func (af *AdaptationField) Discontinuity() (bool, error)
- func (af *AdaptationField) ElementaryStreamPriority() (bool, error)
- func (af *AdaptationField) HasAdaptationFieldExtension() (bool, error)
- func (af *AdaptationField) HasOPCR() (bool, error)
- func (af *AdaptationField) HasPCR() (bool, error)
- func (af *AdaptationField) HasSplicingPoint() (bool, error)
- func (af *AdaptationField) HasTransportPrivateData() (bool, error)
- func (af *AdaptationField) Length() int
- func (af *AdaptationField) OPCR() (uint64, error)
- func (af *AdaptationField) PCR() (uint64, error)
- func (af *AdaptationField) RandomAccess() (bool, error)
- func (af *AdaptationField) SetAdaptationFieldExtension(data []byte) error
- func (af *AdaptationField) SetDiscontinuity(value bool) error
- func (af *AdaptationField) SetElementaryStreamPriority(value bool) error
- func (af *AdaptationField) SetHasAdaptationFieldExtension(value bool) error
- func (af *AdaptationField) SetHasOPCR(value bool) error
- func (af *AdaptationField) SetHasPCR(value bool) error
- func (af *AdaptationField) SetHasSplicingPoint(value bool) error
- func (af *AdaptationField) SetHasTransportPrivateData(value bool) error
- func (af *AdaptationField) SetOPCR(PCR uint64) error
- func (af *AdaptationField) SetPCR(PCR uint64) error
- func (af *AdaptationField) SetRandomAccess(value bool) error
- func (af *AdaptationField) SetSpliceCountdown(value byte) error
- func (af *AdaptationField) SetTransportPrivateData(data []byte) error
- func (af *AdaptationField) SpliceCountdown() (int, error)
- func (af *AdaptationField) TransportPrivateData() ([]byte, error)
- type AdaptationFieldControlOptions
- type Packet
- func CopyPackets(packets []*Packet) []*Packet
- func Create(pid int, options ...func(*Packet)) *Packet
- func CreateDCPacket(pid int, cc uint8) *Packet
- func CreatePacketWithPayload(pid int, cc uint8, pay []byte) *Packet
- func CreateTestPacket(pid int, cc uint8, pusi, hasPay bool) *Packet
- func FromBytes(bytes []byte) (*Packet, error)
- func IncrementCC(packet *Packet) *Packet
- func New() *Packet
- func SetCC(packet *Packet, newCC uint8) *Packet
- func ZeroCC(packet *Packet) *Packet
- func (p *Packet) AdaptationField() (*AdaptationField, error)
- func (p *Packet) AdaptationFieldControl() AdaptationFieldControlOptions
- func (p *Packet) CheckErrors() error
- func (p *Packet) ContinuityCounter() int
- func (p *Packet) Equals(r *Packet) bool
- func (p *Packet) HasAdaptationField() bool
- func (p *Packet) HasPayload() bool
- func (p *Packet) IncContinuityCounter()
- func (p *Packet) IsNull() bool
- func (p *Packet) IsPAT() bool
- func (p *Packet) PID() int
- func (p *Packet) Payload() ([]byte, error)
- func (p *Packet) PayloadUnitStartIndicator() bool
- func (p *Packet) SetAdaptationField(af *AdaptationField) error
- func (p *Packet) SetAdaptationFieldControl(value AdaptationFieldControlOptions) error
- func (p *Packet) SetContinuityCounter(value int)
- func (p *Packet) SetPID(pid int)
- func (p *Packet) SetPayload(data []byte) (int, error)
- func (p *Packet) SetPayloadUnitStartIndicator(value bool)
- func (p *Packet) SetTransportErrorIndicator(value bool)
- func (p *Packet) SetTransportPriority(value bool)
- func (p *Packet) SetTransportScramblingControl(value TransportScramblingControlOptions)
- func (p *Packet) TransportErrorIndicator() bool
- func (p *Packet) TransportPriority() bool
- func (p *Packet) TransportScramblingControl() TransportScramblingControlOptions
- func (p *Packet) ZeroContinuityCounter()
- type PacketWriteCloser
- type PacketWriter
- type PacketWriterFunc
- type PeekScanner
- type Peeker
- type TransportScramblingControlOptions
- type WriteCloser
- type Writer
Constants ¶
const ( // PacketSize is the expected size of a packet in bytes PacketSize = 188 // SyncByte is the expected value of the sync byte SyncByte = 71 // 0x47 (0100 0111) // NullPacketPid is the pid reserved for null packets NullPacketPid = 8191 // 0x1FFF )
Variables ¶
var ( // TestPatPacket is a minimal PAT packet for testing. It contains a single program stream with no payload. TestPatPacket = Packet{}/* 188 elements not displayed */ TestPmtPacket = Packet{}/* 188 elements not displayed */ )
Functions ¶
func ContainsAdaptationField ¶
ContainsAdaptationField is a flag that indicates the packet has an adaptation field.
func ContainsPayload ¶
ContainsPayload is a flag that indicates the packet has a payload. The flag is contained in the 3rd bit of the 4th byte of the Packet.
func ContinuityCounter ¶
ContinuityCounter is a 4-bit sequence number of payload packets. Incremented only when a payload is present (see ContainsPayload() above).
func IsNull ¶
IsNull returns true if the provided packet is a Null packet (i.e., PID == 0x1fff (8191)).
func IsSynced ¶
IsSynced returns whether r is synced to a packet boundary.
IsSynced checks whether the first byte is the MPEG-TS sync byte, the PID is not within the reserved range of 0x4-0xf and that the AFC is not the reserved value 0x0.
func PESHeader ¶
PESHeader returns a byte slice containing the PES header if the Packet contains one, otherwise returns an error
func Payload ¶
Payload returns a slice containing the packet payload. If the packet does not have a payload, an empty byte slice is returned
func PayloadUnitStartIndicator ¶
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 flag is contained in the second bit of the second byte of the Packet.
func Pid ¶
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 SetPayload ¶
SetPayload sets the payload of a given packet
func Sync ¶
func Sync(r PeekScanner) (off int64, err error)
Sync finds the offset of the next packet header and advances the reader to the packet start. It returns the offset of the sync relative to the original reader position.
Sync uses IsSynced to determine whether a position is at packet header.
func WithAFPrivateDataFlag ¶
func WithAFPrivateDataFlag(pkt *Packet)
WithAFPrivateDataFlag is an option function for creating a packet with a adaptation field private data flag
func WithContinuousAF ¶
func WithContinuousAF(pkt *Packet)
WithContinuousAF is an option function for creating a packet with a continuous adaptation field
func WithDiscontinuousAF ¶
func WithDiscontinuousAF(pkt *Packet)
WithDisconinuousAF is an option function for creating a packet with a discontinuous adaptation field
func WithHasAdaptationFieldFlag ¶
func WithHasAdaptationFieldFlag(pkt *Packet)
WithHasAdaptationFieldFlag is an option function for creating a packet with an adaptation field
func WithHasPayloadFlag ¶
func WithHasPayloadFlag(pkt *Packet)
WithHasPayloadFlag is an option function for creating a packet with a payload flag
Types ¶
type Accumulator ¶
type Accumulator interface { // WritePacket adds a packet to the accumulator and returns got.ErrAccumulatorDone if done WritePacket(*Packet) (int, error) // Bytes returns the payload bytes from the underlying buffer Bytes() []byte // Packets returns the packets used to fill the payload buffer Packets() []*Packet // Reset resets the accumulator state Reset() }
Accumulator is used to gather multiple packets and return their concatenated payloads. Accumulator is not thread safe.
func NewAccumulator ¶
func NewAccumulator(f func(data []byte) (done bool, err error)) Accumulator
NewAccumulator creates a new packet accumulator that is done when the provided function returns done as true.
type AdaptationField ¶
type AdaptationField Packet
AdaptationField is an optional part of the packet.
func NewAdaptationField ¶
func NewAdaptationField() *AdaptationField
NewPacket creates a new packet with a Null ID, sync byte, and with the adaptation field control set to payload only. This function is error free.
func (*AdaptationField) AdaptationFieldExtension ¶
func (af *AdaptationField) AdaptationFieldExtension() ([]byte, error)
AdaptationFieldExtension returns the Adaptation Field Extension from the adaptation field if possible. if it is not possible an error is returned.
func (*AdaptationField) Discontinuity ¶
func (af *AdaptationField) Discontinuity() (bool, error)
Discontinuity returns the value of the discontinuity field in the packet.
func (*AdaptationField) ElementaryStreamPriority ¶
func (af *AdaptationField) ElementaryStreamPriority() (bool, error)
ElementaryStreamPriority returns the Elementary Stream Priority Flag.
func (*AdaptationField) HasAdaptationFieldExtension ¶
func (af *AdaptationField) HasAdaptationFieldExtension() (bool, error)
HasAdaptationFieldExtension returns if the packet has an Adaptation Field Extension
func (*AdaptationField) HasOPCR ¶
func (af *AdaptationField) HasOPCR() (bool, error)
HasOPCR returns if the packet has an OPCR
func (*AdaptationField) HasPCR ¶
func (af *AdaptationField) HasPCR() (bool, error)
HasPCR returns if the packet has a PCR
func (*AdaptationField) HasSplicingPoint ¶
func (af *AdaptationField) HasSplicingPoint() (bool, error)
HasSplicingPoint returns if the packet has a Splice Countdown
func (*AdaptationField) HasTransportPrivateData ¶
func (af *AdaptationField) HasTransportPrivateData() (bool, error)
HasTransportPrivateData returns if the packet has an Transport Private Data
func (*AdaptationField) Length ¶
func (af *AdaptationField) Length() int
Length returns the length of the adaptation field
func (*AdaptationField) OPCR ¶
func (af *AdaptationField) OPCR() (uint64, error)
OPCR returns the OPCR from the adaptation field if possible. if it is not possible an error is returned.
func (*AdaptationField) PCR ¶
func (af *AdaptationField) PCR() (uint64, error)
PCR returns the PCR from the adaptation field if possible. if it is not possible an error is returned.
func (*AdaptationField) RandomAccess ¶
func (af *AdaptationField) RandomAccess() (bool, error)
RandomAccess returns the value of the random access field in the packet.
func (*AdaptationField) SetAdaptationFieldExtension ¶
func (af *AdaptationField) SetAdaptationFieldExtension(data []byte) error
SetAdaptationFieldExtension sets the Adaptation Field Extension of the adaptation field. If impossible an error is returned.
func (*AdaptationField) SetDiscontinuity ¶
func (af *AdaptationField) SetDiscontinuity(value bool) error
SetDiscontinuity sets the Discontinuity field of the packet.
func (*AdaptationField) SetElementaryStreamPriority ¶
func (af *AdaptationField) SetElementaryStreamPriority(value bool) error
SetElementaryStreamPriority sets the Elementary Stream Priority Flag.
func (*AdaptationField) SetHasAdaptationFieldExtension ¶
func (af *AdaptationField) SetHasAdaptationFieldExtension(value bool) error
SetHasAdaptationFieldExtension sets HasAdaptationFieldExtension HasAdaptationFieldExtension determines if the packet has an Adaptation Field Extension
func (*AdaptationField) SetHasOPCR ¶
func (af *AdaptationField) SetHasOPCR(value bool) error
SetHasOPCR sets HasOPCR HasOPCR determines if the packet has a OPCR
func (*AdaptationField) SetHasPCR ¶
func (af *AdaptationField) SetHasPCR(value bool) error
SetHasPCR sets HasPCR HasPCR determines if the packet has a PCR
func (*AdaptationField) SetHasSplicingPoint ¶
func (af *AdaptationField) SetHasSplicingPoint(value bool) error
SetHasSplicingPoint sets HasSplicingPoint HasSplicingPoint determines if the packet has a Splice Countdown
func (*AdaptationField) SetHasTransportPrivateData ¶
func (af *AdaptationField) SetHasTransportPrivateData(value bool) error
SetHasTransportPrivateData sets HasTransportPrivateData HasTransportPrivateData determines if the packet has Transport Private Data
func (*AdaptationField) SetOPCR ¶
func (af *AdaptationField) SetOPCR(PCR uint64) error
SetOPCR sets the OPCR of the adaptation field. If impossible an error is returned.
func (*AdaptationField) SetPCR ¶
func (af *AdaptationField) SetPCR(PCR uint64) error
SetPCR sets the PCR of the adaptation field. If impossible an error is returned.
func (*AdaptationField) SetRandomAccess ¶
func (af *AdaptationField) SetRandomAccess(value bool) error
SetRandomAccess sets the value of the random access field in the packet.
func (*AdaptationField) SetSpliceCountdown ¶
func (af *AdaptationField) SetSpliceCountdown(value byte) error
SetSpliceCountdown sets the Splice Countdown of the adaptation field. If impossible an error is returned.
func (*AdaptationField) SetTransportPrivateData ¶
func (af *AdaptationField) SetTransportPrivateData(data []byte) error
SetTransportPrivateData sets the Transport Private Data of the adaptation field. If impossible an error is returned.
func (*AdaptationField) SpliceCountdown ¶
func (af *AdaptationField) SpliceCountdown() (int, error)
SpliceCountdown returns the Splice Countdown from the adaptation field if possible. if it is not possible an error is returned.
func (*AdaptationField) TransportPrivateData ¶
func (af *AdaptationField) TransportPrivateData() ([]byte, error)
TransportPrivateData returns the Transport Private Data from the adaptation field if possible. if it is not possible an error is returned.
type AdaptationFieldControlOptions ¶
type AdaptationFieldControlOptions byte
AdaptationFieldControlOptions is a set of constants for selecting the adaptation field control.
const ( PayloadFlag AdaptationFieldControlOptions = 1 // 10 AdaptationFieldFlag AdaptationFieldControlOptions = 2 // 01 PayloadAndAdaptationFieldFlag AdaptationFieldControlOptions = 3 // 11 )
type Packet ¶
type Packet [PacketSize]byte
Packet is the basic unit in a transport stream.
func CopyPackets ¶
CopyPackets returns a copy of the given packets with new memory
func Create ¶
Example usage pkt, _ = SetCC(
Create(pid, WithHasPayloadFlag, WithContinuousAF, WithPUSI), cc)
func CreateDCPacket ¶
CreateDCPacket creates a new packet with a discontinuous adapataion field and the given PID and CC
func CreatePacketWithPayload ¶
CreatePacketWithPayload creates a new packet with the given PID, CC and payload
func CreateTestPacket ¶
CreateTestPacket creates a test packet with the given PID, continuity counter, payload unit start indicator and payload flag This is a convenience function for often used packet creatio options functions
func FromBytes ¶
FromBytes creates a ts packet from a slice of bytes 188 in length. If the bytes provided have errors or the slice is not 188 in length, then an error vill be returned along with a nill slice.
func IncrementCC ¶
IncrementCC creates a new packet where the new packet has a continuity counter that is increased by one
func New ¶
func New() *Packet
New creates a new packet with a Null ID, sync byte, and with the adaptation field control set to payload only.
func (*Packet) AdaptationField ¶
func (p *Packet) AdaptationField() (*AdaptationField, error)
AdaptationField returns the AdaptationField of the packet. If the packet does not have an adaptation field then a nil AdaptationField is returned.
func (*Packet) AdaptationFieldControl ¶
func (p *Packet) AdaptationFieldControl() AdaptationFieldControlOptions
AdaptationFieldControl returns the Adaptation Field Control.
func (*Packet) CheckErrors ¶
CheckErrors checks the packet for errors
func (*Packet) ContinuityCounter ¶
ContinuityCounter returns the continuity counter. The continuity counter is an integer between 0 and 15.
func (*Packet) HasAdaptationField ¶
HasPayload returns true if the adaptation field control specifies that there is an adaptation field.
func (*Packet) HasPayload ¶
HasPayload returns true if the adaptation field control specifies that there is a payload.
func (*Packet) IncContinuityCounter ¶
func (p *Packet) IncContinuityCounter()
IncContinuityCounter increments the continuity counter. The continuity counter is an integer between 0 and 15. If the number is out of this range (overflow) after incrementing then it will discard the extra bits. The effect is the same as modulus by 16.
func (*Packet) IsNull ¶
IsNull returns true if the packet PID is equal to 8191, the null packet pid.
func (*Packet) PayloadUnitStartIndicator ¶
PayloadUnitStartIndicator returns the Payload unit start indicator (PUSI) flag 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 flag is contained in the second bit of the second byte of the Packet.
func (*Packet) SetAdaptationField ¶
func (p *Packet) SetAdaptationField(af *AdaptationField) error
SetAdaptationField copies the AdaptationField into a packet. If the packet does not have an adaptation field then an error is returned AdaptationField must fit in the same size as the existing adaptation field and its stuffing bytes.
func (*Packet) SetAdaptationFieldControl ¶
func (p *Packet) SetAdaptationFieldControl(value AdaptationFieldControlOptions) error
SetAdaptationFieldControl sets the Adaptation Field Control flag.
func (*Packet) SetContinuityCounter ¶
SetContinuityCounter sets the continuity counter. The continuity counter should be an integer between 0 and 15. If the number is out of this range then it will discard the extra bits. The effect is the same as modulus by 16.
func (*Packet) SetPayload ¶
SetPayload sets the payload of the packet. If the payload cannot fit in the packet an integer will be returned that is the number of bytes that were able to fit in the packet.
func (*Packet) SetPayloadUnitStartIndicator ¶
SetPayloadUnitStartIndicator sets the Payload unit start indicator (PUSI) flag 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 flag is contained in the second bit of the second byte of the Packet.
func (*Packet) SetTransportErrorIndicator ¶
SetTransportErrorIndicator sets the Transport Error Indicator flag.
func (*Packet) SetTransportPriority ¶
SetTransportPriority sets the Transport Priority flag
func (*Packet) SetTransportScramblingControl ¶
func (p *Packet) SetTransportScramblingControl(value TransportScramblingControlOptions)
SetTransportScramblingControl sets the Transport Scrambling Control flag.
func (*Packet) TransportErrorIndicator ¶
TransportErrorIndicator returns the Transport Error Indicator
func (*Packet) TransportPriority ¶
TransportPriority returns the Transport Priority flag
func (*Packet) TransportScramblingControl ¶
func (p *Packet) TransportScramblingControl() TransportScramblingControlOptions
TransportScramblingControl returns the Transport Scrambling Control flag.
func (*Packet) ZeroContinuityCounter ¶
func (p *Packet) ZeroContinuityCounter()
SetContinuityCounter sets the continuity counter to 0.
type PacketWriteCloser ¶
type PacketWriteCloser interface { PacketWriter io.Closer }
PacketWriteCloser is subject to all rules governing implementations of io.Writer
Additionally PacketWriter implementations must not modify or retain packet array pointers, even temporarily.
func NopCloser ¶
func NopCloser(r PacketWriter) PacketWriteCloser
NopCloser returns a PacketWriteCloser with a no-op Close method wrapping the provided PacketWriter r.
type PacketWriter ¶
PacketWriter is subject to all rules governing implementations of io.Writer
Additionally PacketWriter implementations must not modify or retain packet pointers, even temporarily.
type PacketWriterFunc ¶
The PacketWriterFunc type is an adapter to allow the use of ordinary functions as PacketWriters. If f is a function with the appropriate signature, PacketWriterFunc(f) is a PacketWriter that calls f.
func (PacketWriterFunc) WritePacket ¶
func (f PacketWriterFunc) WritePacket(p *Packet) (n int, err error)
WritePacket calls f(p).
type PeekScanner ¶
type PeekScanner interface { io.ByteScanner Peeker }
PeekScanner is an extended io.ByteScanner with peek capacity.
type Peeker ¶
type Peeker interface { // Peek returns the next n bytes without advancing the reader. Peek(n int) ([]byte, error) }
Peeker wraps the Peek method.
type TransportScramblingControlOptions ¶
type TransportScramblingControlOptions byte
TransportScramblingControlOptions is a set of constants for selecting the transport scrambling control.
const ( NoScrambleFlag TransportScramblingControlOptions = 0 // 00 ScrambleEvenKeyFlag TransportScramblingControlOptions = 2 // 10 ScrambleOddKeyFlag TransportScramblingControlOptions = 3 // 11 )
type WriteCloser ¶
type WriteCloser interface { PacketWriteCloser io.Writer }
WriteCloser is the interface that groups PacketWriteCloser and io.Writer methods
func IOWriteCloser ¶
func IOWriteCloser(w PacketWriteCloser) WriteCloser
IOWriteCloser returns a WriteCloser with a default implementation of Write method wrapping the provided PacketWriteCloser w.
type Writer ¶
type Writer interface { PacketWriter io.Writer }
Writer is the interface that groups PacketWriter and io.Writer methods
func IOWriter ¶
func IOWriter(w PacketWriter) Writer
IOWriter returns a Writer with a default implementation of Write method wrapping the provided PacketWriter w.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
AdaptationField provides functions for accessing and reading packet adaptation fields
|
AdaptationField provides functions for accessing and reading packet adaptation fields |