mpegts

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mpegts reads the parts of an MPEG transport stream a TV tuner needs: the scrambling bits, what the stream says it carries (PAT, PMT, SDT, ATSC VCT) and the programme guide inside it (DVB EIT, ATSC PSIP).

It never decrypts anything. Scrambled packets are only counted, so that a stream can be refused.

Index

Constants

View Source
const (
	PIDPAT  = 0x0000
	PIDSDT  = 0x0011
	PIDEIT  = 0x0012
	PIDPSIP = 0x1FFB
)

Well-known PIDs.

View Source
const PacketSize = 188

PacketSize is the size of one transport stream packet.

Variables

View Source
var ErrScrambled = errors.New("the stream is scrambled")

ErrScrambled: the stream is encrypted, and the packets say so.

Functions

func CRC32

func CRC32(b []byte) uint32

CRC32 is the MPEG-2 section checksum.

func DVBText

func DVBText(b []byte) string

DVBText decodes a DVB text field (EN 300 468 annex A). UTF-8 and the Latin-1 family are read; the default table is close enough to Latin-1 for names and titles. Other character tables come through with the unknown characters dropped.

func MultipleString

func MultipleString(b []byte) string

MultipleString reads ATSC's multiple_string_structure: the first string, in the modes that are plain text. Huffman-compressed strings need tables this reader does not carry, and give an empty result.

func ParseMGT

func ParseMGT(s []byte) []int

ParseMGT lists the PIDs of the ATSC event tables (EIT-0, EIT-1…).

Types

type Demux

type Demux struct {
	OnSec func(pid int, section []byte)
	// contains filtered or unexported fields
}

Demux puts sections back together from packets of the PIDs it is asked for.

func NewDemux

func NewDemux(pids ...int) *Demux

NewDemux follows these PIDs.

func (*Demux) Add

func (d *Demux) Add(pid int)

Add starts following another PID.

func (*Demux) Feed

func (d *Demux) Feed(h Header)

Feed takes one parsed packet.

type ES

type ES struct {
	Type int
	PID  int
}

ES is an elementary stream in a PMT.

type Event

type Event struct {
	ServiceID int // DVB service, or the ATSC source id when ATSC is set
	ATSC      bool
	Start     time.Time
	Duration  time.Duration
	Title     string
	Desc      string
	Rating    string
}

Event is a programme from the broadcast's own guide.

func ParseATSCEIT

func ParseATSCEIT(s []byte) []Event

ParseATSCEIT reads an ATSC Event Information Table section.

func ParseEIT

func ParseEIT(s []byte) []Event

ParseEIT reads a DVB Event Information Table section.

type Guard

type Guard struct {
	OnEvent func(Event)
	// contains filtered or unexported fields
}

Guard passes a transport stream through unchanged, while watching it. When the stream turns out to be encrypted it stops with ErrScrambled. It also collects the guide data going by, which costs almost nothing.

func NewGuard

func NewGuard(r io.ReadCloser) *Guard

NewGuard watches r.

func (*Guard) Close

func (g *Guard) Close() error

Close closes the stream underneath.

func (*Guard) Fatal

func (g *Guard) Fatal() error

Fatal says why the guard stopped the stream, if it did.

func (*Guard) Read

func (g *Guard) Read(p []byte) (int, error)

Read implements io.Reader.

func (*Guard) Underneath added in v0.2.0

func (g *Guard) Underneath() io.ReadCloser

Underneath is the stream being watched, for a helper handing it to a server that watches it for itself: looking twice would cost twice, and only the server's look counts.

func (*Guard) Verified

func (g *Guard) Verified() bool

Verified says enough of the stream has gone by, unscrambled, to call it clear.

type Header struct {
	PID        int
	Start      bool
	Scrambled  bool
	HasPayload bool
	Payload    []byte
}

Header is what a packet's first four bytes say.

func ParsePacket

func ParsePacket(p []byte) (h Header, ok bool)

ParsePacket reads one packet. ok is false for anything out of sync.

type PMT

type PMT struct {
	Program int
	PCR     int
	Streams []ES
	CA      bool // a conditional access descriptor: the programme may be encrypted
}

PMT says what a programme is made of.

func ParsePMT

func ParsePMT(s []byte) (PMT, bool)

ParsePMT reads a Program Map Table section.

func (PMT) HD

func (p PMT) HD() bool

HD guesses from the codec: only MPEG-4 and later usually mean HD here.

func (PMT) HasVideo

func (p PMT) HasVideo() bool

HasVideo says whether a programme has a picture (radio services do not).

type Program

type Program struct {
	Number int
	PMTPID int
}

Program is one entry of the PAT.

func ParsePAT

func ParsePAT(s []byte) []Program

ParsePAT reads a Program Association Table section.

type ProgramFilter

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

ProgramFilter passes one programme from a stream that carries many: its PAT (rewritten to list only it), PMT, audio and video, and the guide tables.

func NewProgramFilter

func NewProgramFilter(r io.ReadCloser, program int) *ProgramFilter

NewProgramFilter keeps only this programme of r.

func (*ProgramFilter) Close

func (f *ProgramFilter) Close() error

Close closes the stream underneath.

func (*ProgramFilter) Fatal

func (f *ProgramFilter) Fatal() error

Fatal passes on a guard's reason from underneath.

func (*ProgramFilter) Read

func (f *ProgramFilter) Read(p []byte) (int, error)

Read implements io.Reader.

type Service

type Service struct {
	ID       int
	Name     string
	Provider string
	Type     int
	FreeCA   bool // conditional access: encrypted
}

Service is an entry of DVB's SDT.

func ParseSDT

func ParseSDT(s []byte) []Service

ParseSDT reads a Service Description Table section.

type VirtualChannel

type VirtualChannel struct {
	Name      string
	Major     int
	Minor     int
	Program   int
	SourceID  int
	Encrypted bool // access_controlled
	Hidden    bool
	Type      int
}

VirtualChannel is an entry of ATSC's channel table.

func ParseVCT

func ParseVCT(s []byte) []VirtualChannel

ParseVCT reads a terrestrial or cable Virtual Channel Table section.

Jump to

Keyboard shortcuts

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