ts

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: BSD-3-Clause Imports: 6 Imported by: 6

Documentation

Overview

Package ts contains MPEG-TS realated functions

Index

Constants

View Source
const (
	PCRModulo = (1 << 33) * 300
	PCRFreq   = 27e6 // Hz
)
View Source
const (
	PktLen  = 188
	NullPid = 8191
)

Variables

View Source
var (
	ErrAFTooShort = errors.New("adaptation field is too short")
	ErrBadPCR     = errors.New("PCR decoding error")
	ErrNotInAF    = errors.New("no such entry in adaptation field")
)
View Source
var (
	// ErrSync means a lost of MPEG-TS synchronization.
	ErrSync = dvb.TemporaryError("MPEG-TS synchronization error")
)

Functions

This section is empty.

Types

type AF

type AF []byte

AF represents content of adaptation field

func (AF) Flags

func (a AF) Flags() AFFlags

Flags returns adaptation field flags. If len(af) == 0 returns zero flags (all AFFlags methods returns false).

func (AF) OPCR

func (a AF) OPCR() (PCR, error)

OPCR returns value of OPCR in a. It returns OPCR == -1 and not nil error if there is no OPCR in AF or it can't decode OPCR.

func (AF) PCR

func (a AF) PCR() (PCR, error)

PCR returns value of PCR in a. It returns PCR == -1 and not nil error if there is no PCR in AF or it can't decode PCR.

func (AF) SetPCR

func (a AF) SetPCR(pcr PCR) error

func (AF) SpliceCountdown

func (a AF) SpliceCountdown() (int8, error)

type AFFlags

type AFFlags byte
const (
	Discontinuity       AFFlags = 0x80 // discontinuity_indicator
	RandomAccess        AFFlags = 0x40 // random_access_indicator
	ESPrio              AFFlags = 0x20 // elementary_stream_priority_indicator
	ContainsPCR         AFFlags = 0x10 // PCR_flag
	ContainsOPCR        AFFlags = 0x08 // OPCR_flag
	SplicingPoint       AFFlags = 0x04 // splicing_point_flag
	ContainsPrivateData AFFlags = 0x02 // transport_private_data_flag
	HasExtension        AFFlags = 0x01 // adaptation_field_extension_flag
)

type ArrayPkt

type ArrayPkt [PktLen]byte

ArrayPkt implements Pkt interface and represents array of bytes that contains one MPEG-TS packet.

func (*ArrayPkt) AF

func (p *ArrayPkt) AF() AF

func (*ArrayPkt) Bytes

func (p *ArrayPkt) Bytes() []byte

func (*ArrayPkt) CC

func (p *ArrayPkt) CC() int8

func (*ArrayPkt) ContainsAF

func (p *ArrayPkt) ContainsAF() bool

func (*ArrayPkt) ContainsError

func (p *ArrayPkt) ContainsError() bool

func (*ArrayPkt) ContainsPayload

func (p *ArrayPkt) ContainsPayload() bool

func (*ArrayPkt) Copy

func (p *ArrayPkt) Copy(pkt Pkt)

func (*ArrayPkt) Flags

func (p *ArrayPkt) Flags() PktFlags

func (*ArrayPkt) IncCC

func (p *ArrayPkt) IncCC()

func (*ArrayPkt) Payload

func (p *ArrayPkt) Payload() []byte

func (*ArrayPkt) PayloadUnitStart

func (p *ArrayPkt) PayloadUnitStart() bool

func (*ArrayPkt) Pid

func (p *ArrayPkt) Pid() int16

func (*ArrayPkt) Prio

func (p *ArrayPkt) Prio() bool

func (*ArrayPkt) ScramblingCtrl

func (p *ArrayPkt) ScramblingCtrl() PktScramblingCtrl

func (*ArrayPkt) SetCC

func (p *ArrayPkt) SetCC(b int8)

func (*ArrayPkt) SetContainsAF

func (p *ArrayPkt) SetContainsAF(b bool)

func (*ArrayPkt) SetContainsError

func (p *ArrayPkt) SetContainsError(b bool)

func (*ArrayPkt) SetContainsPayload

func (p *ArrayPkt) SetContainsPayload(b bool)

func (*ArrayPkt) SetFlags

func (p *ArrayPkt) SetFlags(f PktFlags)

func (*ArrayPkt) SetPayloadUnitStart

func (p *ArrayPkt) SetPayloadUnitStart(b bool)

func (*ArrayPkt) SetPid

func (p *ArrayPkt) SetPid(pid int16)

func (*ArrayPkt) SetPrio

func (p *ArrayPkt) SetPrio(b bool)

func (*ArrayPkt) SetScramblingCtrl

func (p *ArrayPkt) SetScramblingCtrl(sc PktScramblingCtrl)

func (*ArrayPkt) SetSync

func (p *ArrayPkt) SetSync()

func (*ArrayPkt) Slice

func (p *ArrayPkt) Slice() SlicePkt

Slice returns refference to the content of p as SlicePkt

func (*ArrayPkt) SyncOK

func (p *ArrayPkt) SyncOK() bool

type PCR

type PCR int64

PCR contains the number of ticks of 27MHz generator. PCR generator counts modulo PCRModulo.

func (PCR) Add

func (c PCR) Add(ns time.Duration) PCR

func (PCR) Nanosec

func (c PCR) Nanosec() time.Duration

Nanosec returns (c * 1000 + 13) / 27

type Pkt

type Pkt interface {
	// Bytes returns content of the packet as byte slice. It guarantees that
	// len of slice is equal to PktLen.
	Bytes() []byte
	// Copy copies conntent of Pkt.
	Copy(Pkt)
	// SyncOK checks does sync byte is OK.
	SyncOK() bool
	// SetSync() sets right sync byte
	SetSync()
	// Pid return value of packet identifier
	Pid() int16
	// SetPid sets packet identifier
	SetPid(int16)
	// CC returns value of continuity counter
	CC() int8
	// SetCC sets the value of continuity counter to byte&0x0f
	SetCC(int8)
	// IncCC increments  continuity counter
	IncCC()
	// Flags returns packet flags
	Flags() PktFlags
	// SetFlags sets packet flags
	SetFlags(PktFlags)
	// AF returns adaptation field bytes. If p doesn't contain AF it returns
	// AF{}. If adaptation_field_length byte has wrong value it returns nil.
	AF() AF
	// Payload returns payload bytes. It returns nil if packet dosn't contain
	// payload or adaptation_field_length byte has incorrect value.
	Payload() []byte

	// Direct access to options/flags
	ContainsError() bool
	SetContainsError(b bool)
	PayloadUnitStart() bool
	SetPayloadUnitStart(b bool)
	Prio() bool
	SetPrio(bool)
	ScramblingCtrl() PktScramblingCtrl
	SetScramblingCtrl(PktScramblingCtrl)
	ContainsAF() bool
	SetContainsAF(bool)
	ContainsPayload() bool
	SetContainsPayload(bool)
}

Pkt is common interface to any MPEG-TS packet implementation

type PktFlags

type PktFlags byte

func (PktFlags) ContainsAF

func (f PktFlags) ContainsAF() bool

ContainsAF returns true if adaptation_field_control & 2 == 1

func (PktFlags) ContainsError

func (f PktFlags) ContainsError() bool

ContainsError returns true if transport_error_indicator == 1

func (PktFlags) ContainsPayload

func (f PktFlags) ContainsPayload() bool

ContainsPayload returns true if adaptation_field_control & 1 == 1

func (PktFlags) PayloadUnitStart

func (f PktFlags) PayloadUnitStart() bool

PayloadUnitStart returns true if payload_unit_start_indicator == 1

func (PktFlags) Prio

func (f PktFlags) Prio() bool

Prio returns true if transport_priority == 1

func (PktFlags) ScramblingCtrl

func (f PktFlags) ScramblingCtrl() PktScramblingCtrl

func (*PktFlags) SetContainsAF

func (f *PktFlags) SetContainsAF(b bool)

SetContainsAF sets first bit of adaptation_field_control

func (*PktFlags) SetContainsError

func (f *PktFlags) SetContainsError(b bool)

SetContainsError sets transport_error_indicator

func (*PktFlags) SetContainsPayload

func (f *PktFlags) SetContainsPayload(b bool)

SetContainsPayload sets second bit of daptation_field_control

func (*PktFlags) SetPayloadUnitStart

func (f *PktFlags) SetPayloadUnitStart(b bool)

SetPayloadUnitStart sets payload_unit_start_indicator

func (*PktFlags) SetPrio

func (f *PktFlags) SetPrio(b bool)

func (*PktFlags) SetScramblingCtrl

func (f *PktFlags) SetScramblingCtrl(sc PktScramblingCtrl)

type PktPktReader

type PktPktReader struct {
	// contains filtered or unexported fields
}

PktPktReader wraps any io.Reader interface and returns PktReader and PktReplacer implementation for read MPEG-TS packets from stream of packets. Internally it doesn't allocate any memory so is friendly for real-time applications (it doesn't cause GC to run).

PktPktReader assumes that every call of io.Reader's Read method returns one transport packet that fits in provided buffer and contains integer number of MPEG-TS packets that are properly aligned to the begining of transport packet.

func NewPktPktReader

func NewPktPktReader(r io.Reader, buf []byte) *PktPktReader

NewPktPktReader is equivalent to:

p := new(PktPktReader); p.SetReader(r); p.SetBuffer(buf)

func (*PktPktReader) ReadPkt

func (p *PktPktReader) ReadPkt(pkt Pkt) error

ReadPkt reads one MPEG-TS packet

func (*PktPktReader) SetBuffer

func (p *PktPktReader) SetBuffer(buf []byte)

SetReader sets new io.Reader as stream source. Forces resynchronization.

func (*PktPktReader) SetReader

func (p *PktPktReader) SetReader(r io.Reader)

SetReader sets new io.Reader as stream source. Forces resynchronization.

type PktQueue

type PktQueue struct {
	// contains filtered or unexported fields
}

PktQueue represents queue of TS packets.

func NewPktQueue

func NewPktQueue(n int) *PktQueue

NewPktQueue creates new queue with internall buffer of size n packets.

func (*PktQueue) Cap

func (q *PktQueue) Cap() int

Cap returns capacity of q.

func (*PktQueue) Len

func (q *PktQueue) Len() int

Len returns number of packets queued in q.

func (*PktQueue) ReadPart

func (q *PktQueue) ReadPart() *PktReadQueue

ReadPart returns read part of q that can be used only to read packets from q.

func (*PktQueue) WritePart

func (q *PktQueue) WritePart() *PktWriteQueue

WritePart returns write part of q that can be used to write packets to q and to close q.

type PktReadQueue

type PktReadQueue PktQueue

PktReadQueue represenst read part of PktQueue and implements PktReplacer interface. If reader uses raw channels insteed of ReplacePkt method it should first read filled packet from the Filled channel and next write empty packet to the Empty channel.

func (*PktReadQueue) Cap

func (q *PktReadQueue) Cap() int

Cap returns capacity of q.

func (*PktReadQueue) Empty

func (q *PktReadQueue) Empty() chan<- *ArrayPkt

Empty returns a channel that can be used to pass empty packets to q.

func (*PktReadQueue) Filled

func (q *PktReadQueue) Filled() <-chan *ArrayPkt

Filled returns a channel that can be used to obtain filled packets from q.

func (*PktReadQueue) Len

func (q *PktReadQueue) Len() int

Len returns number of packets queued in q.

func (*PktReadQueue) ReplacePkt

func (q *PktReadQueue) ReplacePkt(pkt *ArrayPkt) (*ArrayPkt, error)

ReplacePkt obtains filled packet from q and next pass empty pkt to q. It returns io.EOF error when queue was closed and there is no more packets to read.

type PktReader

type PktReader interface {
	// ReadPkt reads one MPEG-TS packet.
	// If it returns ErrSync or dvb.ErrOverflow you can try to read next
	// packet.
	ReadPkt(Pkt) error
}

PktReader is an interface that wraps the ReadPkt method.

type PktReaderAsReplacer

type PktReaderAsReplacer struct {
	R PktReader
}

PktReaderAsReplacer converts any PktReader to PktReplacer

func (PktReaderAsReplacer) ReplacePkt

func (r PktReaderAsReplacer) ReplacePkt(p *ArrayPkt) (*ArrayPkt, error)

type PktReplacer

type PktReplacer interface {
	// ReplacePkt consumes packet reffered by p an returns other packet reffered
	// by r.
	// If you use ReplacePkt for reading ErrSync or dvb.ErrOverflow are not
	// fatal errors. You can still call ReplacePkt after obtaining such errors.
	// If you use ReplacePkt for writing, any error is probably a problem.
	ReplacePkt(p *ArrayPkt) (r *ArrayPkt, e error)
}

PktReplacer is an interface that wraps the ReplacePkt method. After ReplacePkt call caller should not reffer to p content any more. If ReplacePkt returns an error it is guaranteed that r == p (but content reffered by p can be modified). Generally ReplacePkt should be used in this way:

p, err = q.ReplacePkt(p)
if err != nil {
    ...
}

type PktScramblingCtrl

type PktScramblingCtrl byte
const (
	PktNotScrambled PktScramblingCtrl = iota
	PktScrambled1
	PktScrambled2
	PktScrambled3
)

type PktStreamReader

type PktStreamReader struct {
	// contains filtered or unexported fields
}

PktStreamReader wraps any io.Reader interface and returns PktReader and PktReplacer implementation for read MPEG-TS packets from stream of bytes. Internally it doesn't allocate any memory so is friendly for real-time applications (it doesn't cause GC to run).

Using PktStreamReader you can start read at any point in stream. If the start point doesn't match a beginning of a packet, PktReader returns ErrSync and tries to synchronize during next read.

func NewPktStreamReader

func NewPktStreamReader(r io.Reader) *PktStreamReader

NewPktStreamReader is equivalent to:

s := new(PktStreamReader); s.SetReader(r)

func (*PktStreamReader) ReadPkt

func (s *PktStreamReader) ReadPkt(pkt Pkt) error

ReadPkt reads one MPEG-TS packet directly to provided buffer with exception for out of sync state when it reads more than one packet to internal buffer and tries to synchronize. ReadPkt check len(pkt) and panics if it isn't PktLen (usefull if bound checking is disabled at compile time). ReadPkt converts os.PathError{Err: syscall.EOVERFLOW} to dvb.ErrOverflow.

func (*PktStreamReader) ReplacePkt

func (s *PktStreamReader) ReplacePkt(pkt *ArrayPkt) (*ArrayPkt, error)

ReplacePkt works like ReadPkt but implements PktReplacer interface.

func (*PktStreamReader) SetReader

func (s *PktStreamReader) SetReader(r io.Reader)

SetReader sets new io.Reader as stream source. Forces resynchronization.

type PktStreamWriter

type PktStreamWriter struct {
	W io.Writer
}

PktStreamReader wraps any io.Writer interface and returns PktWriter and PktReplacer implementation for write MPEG-TS packets as stream of bytes.

func (PktStreamWriter) ReplacePkt

func (s PktStreamWriter) ReplacePkt(pkt *ArrayPkt) (*ArrayPkt, error)

ReplacePkt works like WritePkt but implements PktReplacer interface.

func (PktStreamWriter) WritePkt

func (s PktStreamWriter) WritePkt(pkt Pkt) error

WritePkt wraps s.W.Write method

type PktWriteQueue

type PktWriteQueue PktQueue

PktWriteQueue represenst write part of PktQueue and implements PktReplacer interface. If writer uses raw channels insteed of ReplacePkt method it should read empty packet from Empty channel and next write filled packet to Filled channel.

func (*PktWriteQueue) Cap

func (q *PktWriteQueue) Cap() int

Cap returns capacity of q.

func (*PktWriteQueue) Close

func (q *PktWriteQueue) Close()

Close closes write part of queue. After close on write part, ReplacePkt method on read part returns io.EOF error if there is no more packets to read.

func (*PktWriteQueue) Empty

func (q *PktWriteQueue) Empty() <-chan *ArrayPkt

Empty returns a channel that can be used to obtain empty packets from q.

func (*PktWriteQueue) Filled

func (q *PktWriteQueue) Filled() chan<- *ArrayPkt

Filled returns a channel that can be used to pass filled packets to q.

func (*PktWriteQueue) Len

func (q *PktWriteQueue) Len() int

Len returns number of packet queued in q.

func (*PktWriteQueue) ReplacePkt

func (q *PktWriteQueue) ReplacePkt(pkt *ArrayPkt) (*ArrayPkt, error)

ReplacePkt obtains empty packet from q and next pass pkt to q. It always returns nil error.

type PktWriter

type PktWriter interface {
	// WritePkt writes one MPEG-TS packet.
	WritePkt(Pkt) error
}

PktWriter is an interface that wraps the WritePkt method.

type PktWriterAsReplacer

type PktWriterAsReplacer struct {
	W PktWriter
}

PktWriterAsReplacer converts any PktWriter to PktReplacer

func (PktWriterAsReplacer) ReplacePkt

func (r PktWriterAsReplacer) ReplacePkt(p *ArrayPkt) (*ArrayPkt, error)

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader wraps PktReplacer or PktReader to implement a standard io.Reader interface. Internally it doesn't allocate any memory so it is friendly for real-time applications (it doesn't cause GC to run).

func (*Reader) Read

func (r *Reader) Read(buf []byte) (n int, err error)

Read allow to read from MPEG-TS packet stream as from ordinary byte stream. It reads no more than 2*PktLen-1 bytes. If len(buf) >= 2*PktLen-1, buf always contains one packet at the end of data, plus (possibly) some data from previously not fully read packet. If you always use len(buf) >= PktLen it always read one MPEG-TS packet without internal buffering. If you need to fill big buffer for multiple packets, use io.ReadFull helper function.

func (*Reader) SetPktReader

func (r *Reader) SetPktReader(p PktReader)

SetPktReader sets new PktReader as packets source. If internal buffer contains some data from previous source they will be returned before any new read from new source.

type SlicePkt

type SlicePkt []byte

SlicePkt implements Pkt interface and represents MPEG-TS packet that can be a slice of some more data (eg. slice of buffer that contains more packets). Use it only when you can't use ArrayPkt. Assigning variable of type SlicePkt to variable of type Pkt causes memory allocation (you can use *SlicePkt to avoid this).

func AsPkt

func AsPkt(buf []byte) SlicePkt

AsPkt returns beginning of buf as SlicePkt. It panics if len(buf) < PktLen.

func (SlicePkt) AF

func (p SlicePkt) AF() AF

func (SlicePkt) Bytes

func (p SlicePkt) Bytes() []byte

func (SlicePkt) CC

func (p SlicePkt) CC() int8

func (SlicePkt) ContainsAF

func (p SlicePkt) ContainsAF() bool

func (SlicePkt) ContainsError

func (p SlicePkt) ContainsError() bool

func (SlicePkt) ContainsPayload

func (p SlicePkt) ContainsPayload() bool

func (SlicePkt) Copy

func (p SlicePkt) Copy(pkt Pkt)

func (SlicePkt) Flags

func (p SlicePkt) Flags() PktFlags

func (SlicePkt) IncCC

func (p SlicePkt) IncCC()

func (SlicePkt) Payload

func (p SlicePkt) Payload() []byte

func (SlicePkt) PayloadUnitStart

func (p SlicePkt) PayloadUnitStart() bool

func (SlicePkt) Pid

func (p SlicePkt) Pid() int16

func (SlicePkt) Prio

func (p SlicePkt) Prio() bool

func (SlicePkt) ScramblingCtrl

func (p SlicePkt) ScramblingCtrl() PktScramblingCtrl

func (SlicePkt) SetCC

func (p SlicePkt) SetCC(b int8)

func (SlicePkt) SetContainsAF

func (p SlicePkt) SetContainsAF(b bool)

func (SlicePkt) SetContainsError

func (p SlicePkt) SetContainsError(b bool)

func (SlicePkt) SetContainsPayload

func (p SlicePkt) SetContainsPayload(b bool)

func (SlicePkt) SetFlags

func (p SlicePkt) SetFlags(f PktFlags)

func (SlicePkt) SetPayloadUnitStart

func (p SlicePkt) SetPayloadUnitStart(b bool)

func (SlicePkt) SetPid

func (p SlicePkt) SetPid(pid int16)

func (SlicePkt) SetPrio

func (p SlicePkt) SetPrio(b bool)

func (SlicePkt) SetScramblingCtrl

func (p SlicePkt) SetScramblingCtrl(sc PktScramblingCtrl)

func (SlicePkt) SetSync

func (p SlicePkt) SetSync()

func (SlicePkt) SyncOK

func (p SlicePkt) SyncOK() bool

Directories

Path Synopsis
Package psi provides functionality for read program specific information from MPEG transport stream.
Package psi provides functionality for read program specific information from MPEG transport stream.

Jump to

Keyboard shortcuts

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