ts

package
v0.0.0-...-533afb4 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2018 License: MIT Imports: 6 Imported by: 0

README

mpeg/ts

Package ts implements an MPEG-2 transport stream parser as defined in ISO/IEC 13818-1.

GoDoc

Roadmap

  • Basic packet parser
  • Adapation feild support
  • Stream de-multiplexer (via <-chan Packet)
  • PayloadReader (implementing io.Reader)
  • PayloadUnitReader (implementing io.Reader)
  • Decoding support for other TS Packet types
    • Program Association Table support
    • Conditional Access Table support
    • Program Map Table support

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

Examples

Constants

View Source
const MaxPayloadSize = 184

MaxPayloadSize is the maximum payload, in bytes, that a Transport Stream packet can contain.

View Source
const SyncByte = 0x47

SyncByte the the fixed 8-bit value that marks the start of a TS packet.

Variables

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

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

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

AdaptationField is an optional field in a transport stream packet header. TODO(jh): Needs implementation

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

func NewDemuxer(reader io.Reader) *Demuxer

Creates a new MPEG-2 Transport Stream Demultiplexer

func (*Demuxer) Err

func (tsd *Demuxer) Err() error

Retrieve the last error from the demuxer

func (*Demuxer) Go

func (tsd *Demuxer) Go() <-chan bool

Create a goroutine to begin parsing the input stream

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)│
└──────────────────┴──────────────────┴──────────────────┘  └──────────────────────────────┴──────────────────────────────┘

func NewPacket

func NewPacket(br bitreader.BitReader) (packet *Packet, err error)

Create a new Packet and read it from the bit stream.

func (*Packet) Next

func (packet *Packet) Next(br bitreader.BitReader) (err error)

Read the next packet from the bit stream into an existing Packet.

func (*Packet) String

func (p *Packet) String() string

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

type PacketTester func(*Packet) bool

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

func (pt PacketTester) Not() PacketTester

Inverts the packet tester

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 TransportStreamControl

type TransportStreamControl interface {
	SkipUntil(skipUntil PacketTester)
	TakeWhile(takeWhile PacketTester)
}

TransportStreamControl is the interface that contains functions to limit a transport stream packets

Jump to

Keyboard shortcuts

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