packet

package
v2.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2023 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package packet is used for reading and manipulating packets in an MPEG transport stream

Index

Constants

View Source
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

View Source
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

func ContainsAdaptationField(packet *Packet) bool

ContainsAdaptationField is a flag that indicates the packet has an adaptation field.

func ContainsPayload

func ContainsPayload(packet *Packet) bool

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

func ContinuityCounter(packet *Packet) uint8

ContinuityCounter is a 4-bit sequence number of payload packets. Incremented only when a payload is present (see ContainsPayload() above).

func Equal

func Equal(a, b *Packet) bool

Equal returns true if the bytes of the two packets are equal

func Header(packet *Packet) []byte

Header Returns a slice containing the Packer Header.

func IsNull

func IsNull(packet *Packet) bool

IsNull returns true if the provided packet is a Null packet (i.e., PID == 0x1fff (8191)).

func IsPat

func IsPat(packet *Packet) bool

IsPat returns true if the provided packet is a PAT

func IsSynced

func IsSynced(r Peeker) (ok bool, err error)

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

func PESHeader(packet *Packet) ([]byte, error)

PESHeader returns a byte slice containing the PES header if the Packet contains one, otherwise returns an error

func Payload

func Payload(packet *Packet) ([]byte, error)

Payload returns a slice containing the packet payload. If the packet does not have a payload, an empty byte slice is returned

func PayloadUnitStartIndicator

func PayloadUnitStartIndicator(packet *Packet) bool

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

func Pid(packet *Packet) int

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

func SetPayload(pkt *Packet, pay []byte) int

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

func WithPES

func WithPES(pkt *Packet, pts uint64)

WithPES is an option function for creating a packet with a PES header

func WithPUSI

func WithPUSI(pkt *Packet)

WithPUSI is an option function for creating a packet with the payload unit start indicator flag set

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

func CopyPackets(packets []*Packet) []*Packet

CopyPackets returns a copy of the given packets with new memory

func Create

func Create(pid int, options ...func(*Packet)) *Packet

Example usage pkt, _ = SetCC(

Create(pid,
      WithHasPayloadFlag,
      WithContinuousAF,
      WithPUSI),
cc)

func CreateDCPacket

func CreateDCPacket(pid int, cc uint8) *Packet

CreateDCPacket creates a new packet with a discontinuous adapataion field and the given PID and CC

func CreatePacketWithPayload

func CreatePacketWithPayload(pid int, cc uint8, pay []byte) *Packet

CreatePacketWithPayload creates a new packet with the given PID, CC and payload

func CreateTestPacket

func CreateTestPacket(pid int, cc uint8, pusi, hasPay bool) *Packet

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

func FromBytes(bytes []byte) (*Packet, error)

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

func IncrementCC(packet *Packet) *Packet

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 SetCC

func SetCC(packet *Packet, newCC uint8) *Packet

SetCC creates a new packet where the new packet has the continuity counter provided

func ZeroCC

func ZeroCC(packet *Packet) *Packet

ZeroCC creates a new packet where the new packet has a continuity counter that zero

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

func (p *Packet) CheckErrors() error

CheckErrors checks the packet for errors

func (*Packet) ContinuityCounter

func (p *Packet) ContinuityCounter() int

ContinuityCounter returns the continuity counter. The continuity counter is an integer between 0 and 15.

func (*Packet) Equals

func (p *Packet) Equals(r *Packet) bool

Equal returns true if the bytes of the two packets are equal

func (*Packet) HasAdaptationField

func (p *Packet) HasAdaptationField() bool

HasPayload returns true if the adaptation field control specifies that there is an adaptation field.

func (*Packet) HasPayload

func (p *Packet) HasPayload() bool

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

func (p *Packet) IsNull() bool

IsNull returns true if the packet PID is equal to 8191, the null packet pid.

func (*Packet) IsPAT

func (p *Packet) IsPAT() bool

IsNull returns true if the packet PID is equal to 0, the PAT packet pid.

func (*Packet) PID

func (p *Packet) PID() int

PID Returns the program ID

func (*Packet) Payload

func (p *Packet) Payload() ([]byte, error)

Payload returns a slice to a copy of the payload bytes in the packet.

func (*Packet) PayloadUnitStartIndicator

func (p *Packet) PayloadUnitStartIndicator() bool

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

func (p *Packet) SetContinuityCounter(value int)

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) SetPID

func (p *Packet) SetPID(pid int)

SetPID sets the program ID

func (*Packet) SetPayload

func (p *Packet) SetPayload(data []byte) (int, error)

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

func (p *Packet) SetPayloadUnitStartIndicator(value bool)

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

func (p *Packet) SetTransportErrorIndicator(value bool)

SetTransportErrorIndicator sets the Transport Error Indicator flag.

func (*Packet) SetTransportPriority

func (p *Packet) SetTransportPriority(value bool)

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

func (p *Packet) TransportErrorIndicator() bool

TransportErrorIndicator returns the Transport Error Indicator

func (*Packet) TransportPriority

func (p *Packet) TransportPriority() bool

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

type PacketWriter interface {
	WritePacket(p *Packet) (n int, err error)
}

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

type PacketWriterFunc func(*Packet) (int, error)

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.

Directories

Path Synopsis
AdaptationField provides functions for accessing and reading packet adaptation fields
AdaptationField provides functions for accessing and reading packet adaptation fields

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL