Documentation
¶
Overview ¶
Package ts implements an MPEG-2 transport stream parser as defined in ISO/IEC 13818-1.
This package is experimental and is not intended for use in production environments.
This package is _not_ optimized for processing video in a production environment, instead is geared towards an educational look at some of the algorithms and processes used to decode compressed video.
Example ¶
Step through a bit stream one packet at a time.
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(shortTsStream))
br := bitreader.NewReader(reader)
packet := new(ts.Packet)
for {
// Read the next packet
err := packet.Next(br)
if err != nil {
break
}
fmt.Println(packet)
}
Output: { PID: 0x21, Counter: d } { PID: 0x31, Counter: 6 } { PID: 0x21, Counter: e } { PID: 0x41, Counter: b } { PID: 0x21, Counter: f }
Index ¶
- Constants
- Variables
- func NewPayloadUnitReader(source io.Reader, where PacketTester) io.Reader
- type AdaptationExt
- type AdaptationField
- type AdaptationFieldControl
- type Demuxer
- type Packet
- type PacketChannel
- type PacketTester
- type StreamControlReader
- type TransportPrivateData
- type TransportStreamControl
Examples ¶
Constants ¶
const MaxPayloadSize = 184
MaxPayloadSize is the maximum payload, in bytes, that a Transport Stream packet can contain.
const SyncByte = 0x47
SyncByte the the fixed 8-bit value that marks the start of a TS packet.
Variables ¶
var EOP = errors.New("end of packet")
EOP is the error returned by Read when the current payload unit has been completed. The Readonly only returns EOP only to signal graceful end of input.
var ErrNoSyncByte = errors.New("no sync byte")
ErrNoSyncByte is the error returned if a sync byte cannot be located in the bitstream.
Functions ¶
func NewPayloadUnitReader ¶
func NewPayloadUnitReader(source io.Reader, where PacketTester) io.Reader
NewPayloadUnitReader creates a payload reader from source, where packets match the packet tester.
Types ¶
type AdaptationExt ¶
type AdaptationField ¶
type AdaptationField struct {
DiscontinuityIndicator bool
RandomAccessIndicator bool
ElementaryStreamPriorityIndicator bool
PCRFlag bool
OPCRFlag bool
SplicingPointFlag bool
TransportPrivateDataFlag bool
AdaptationFieldExtensionFlag bool
// optional fields
PCR uint64
OPCR uint64
SpliceCountdown uint8
TransportPrivateData *TransportPrivateData
AdapatationExtension *AdaptationExt
// contains filtered or unexported fields
}
AdaptationField is an optional field in a transport stream packet header.
type AdaptationFieldControl ¶
type AdaptationFieldControl uint32
AdaptationFieldControl is the two bit code that appears in a transport stream packet header that determines whether an Adapation Field appears in the bit stream.
const ( PayloadOnly AdaptationFieldControl // 0b01 FieldOnly // 0b10 FieldThenPayload // 0b11 )
type Demuxer ¶
type Demuxer struct {
// contains filtered or unexported fields
}
Demuxer is the type to control and extract streams out of a multiplexed Transport Stream.
func NewDemuxer ¶
Creates a new MPEG-2 Transport Stream Demultiplexer
func (*Demuxer) SkipUntil ¶
func (tsd *Demuxer) SkipUntil(skipUntil PacketTester)
Skip any packets from the input stream until the PacketTester returns true
func (*Demuxer) TakeWhile ¶
func (tsd *Demuxer) TakeWhile(takeWhile PacketTester)
Only return packets from the stream while the PacketTester returns true
func (*Demuxer) Where ¶
func (tsd *Demuxer) Where(tester PacketTester) PacketChannel
Create a Packet Channel that will only include packets that match the PacketTester
type Packet ¶
type Packet struct {
TransportErrorIndicator bool
PayloadUnitStartIndicator bool
TransportPriority bool
PID uint32
TransportScramblingControl uint32
AdaptationFieldControl AdaptationFieldControl
ContinuityCounter uint32
AdaptationField *AdaptationField
Payload []byte
// contains filtered or unexported fields
}
Packet is a parsed Transport Stream packet from the bit stream.
┌────────────────┬──────────────────────────────────────────────────────────────────────┐
│ header │ payload │
│ │ (184 bytes)│
└────────────────┴──────────────────────────────────────────────────────────────────────┘
│ ╲
│ ╲────────────────────────────────────────────────────────────────────╲
│ ╲
┌──────────────────────┬─┬─┬─┬─────────────────────────────────────┬────┬────┬──────────┐
│ sync_byte (8) │ │ │ │ PID (13) │ │ │continuity│
│ │ │ │ │ │ │ │counter(4)│
└──────────────────────┴─┴─┴─┴─────────────────────────────────────┴────┴────┴──────────┘
╱ ╲ ╱ ╲
╱ ╲ ╱ ╲
╱────────────────────────────────────╱ ╲───────╲ ╱────────────────────╱ ╲────────────────────────╲
╱ ╲ ╱ ╲
┌──────────────────┬──────────────────┬──────────────────┐ ┌──────────────────────────────┬──────────────────────────────┐
│ transport_error │payload_unit_start│transport_priority│ │ transport_scrambling_control │ adaptation_field_control │
│ (1)│ (1)│ (1)│ │ (2)│ (2)│
└──────────────────┴──────────────────┴──────────────────┘ └──────────────────────────────┴──────────────────────────────┘
type PacketChannel ¶
type PacketChannel <-chan *Packet
PacketChannel is a delivery channel of TS Packets
func (PacketChannel) PayloadOnly ¶
func (input PacketChannel) PayloadOnly() <-chan []byte
PayloadOnly transforms a PacketChannel into a delivery channel of packet payload
func (PacketChannel) PayloadUnit ¶
func (input PacketChannel) PayloadUnit() <-chan []byte
type PacketTester ¶
PacketTester defines a function that tests a packet and returns a bool
var IsPayloadUnitStart PacketTester = func(p *Packet) bool { return p.PayloadUnitStartIndicator }
IsPayloadUnitStart is a packet tester that returns true if the tested packet has the PayloadUnitStartIndicator flag set to true
func IsPID ¶
func IsPID(pid uint32) PacketTester
IsPID creates a packet tester that returns true if the tested packet matches the selected pid
func (PacketTester) And ¶
func (pt PacketTester) And(other PacketTester) PacketTester
And joins two packet testers with a logical and
func (PacketTester) Or ¶
func (pt PacketTester) Or(other PacketTester) PacketTester
Or joins two packet testers with a logical or
type StreamControlReader ¶
type StreamControlReader interface {
io.Reader
TransportStreamControl
}
StreamControlReader is the interface that wraps the basic reading function and transport stream control functions.
func NewPayloadReader ¶
func NewPayloadReader(source io.Reader, where PacketTester) StreamControlReader
NewPayloadReader takes a transport stream and creates a reader that delivers just the packet payload bytes.
type TransportPrivateData ¶
type TransportPrivateData struct {
// contains filtered or unexported fields
}
type TransportStreamControl ¶
type TransportStreamControl interface {
SkipUntil(skipUntil PacketTester)
TakeWhile(takeWhile PacketTester)
}
TransportStreamControl is the interface that contains functions to limit a transport stream packets