mpegts

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2025 License: ISC Imports: 4 Imported by: 1

Documentation

Overview

Package mpegts implements encoding and decoding of MPEG-TS packets as specified in ITU-T H.222.0.

Example

The most common usage of mpegts involves the use of Scanner which steps through the packets of a transport stream. In this example we decode then re-encode every packet from the standard input to the standard output. If a packet contains a clock reference, we print that out to the standard error for diagnostics. This provides similar functionality to the following ffprobe command:

ffprobe -show_packets -
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/untangledco/streaming/mpegts"
)

func main() {
	sc := mpegts.NewScanner(os.Stdin)
	var i int
	for sc.Scan() {
		i++
		packet := sc.Packet()
		if packet.Adaptation != nil && packet.Adaptation.PCR != nil {
			ticks := packet.Adaptation.PCR.Ticks()
			fmt.Fprintf(os.Stderr, "packet %d\t%d\n", i, ticks)
		}
		if err := mpegts.Encode(os.Stdout, packet); err != nil {
			log.Printf("encode packet %d: %v", i, err)
		}
	}
	if sc.Err() != nil {
		log.Fatalf("scan: %v", sc.Err())
	}
}
Output:

Index

Examples

Constants

View Source
const (
	FieldPTS uint8 = 1 << (7 - iota)
	FieldDTS
	FieldESCR
	FieldESRate
	FieldTrickMode
	FieldCopyInfo
	FieldCRC
	FieldExtension
)

Flags indicating which optional fields are present in a PES header's payload.

View Source
const PacketSize int = 188

PacketSize is the length of a MPEG-TS packet in bytes.

View Source
const Sync byte = 'G'

Sync is the first byte of a MPEG-TS packet.

Variables

View Source
var ErrLongPacket = errors.New("long packet")
View Source
var ErrShortPacket = errors.New("short packet")

Functions

func Encode

func Encode(w io.Writer, p *Packet) error

func Unmarshal

func Unmarshal(buf []byte, p *Packet) error

Types

type Adaptation

type Adaptation struct {
	// If true, there is a discontinuity between this packet and
	// either the continuity conter or PCR.
	Discontinuous bool
	// If true, the packet may be used as an index from which to
	// randomly access other points in the stream.
	RandomAccess bool
	// Whether this stream is high priority.
	Priority           bool
	SpliceCountdownSet bool
	PCR                *PCR
	// Original program clock reference.
	OPCR *PCR
	// The number of packets remaining until a splicing point.
	SpliceCountdown uint8
	// Raw bytes of any application-specific data.
	Private []byte
	// Raw bytes of the adaptation extension field.
	Extension []byte
	// A slice of bytes all with the value 0xff; enough to ensure
	// a packet's length is always PacketSize.
	Stuffing []byte
}

Adaptation represents an adaptation field including the header.

type PCR

type PCR struct {
	// 33-bit integer holding the number of ticks of a 90KHz clock.
	Base uint64
	// 9-bit integer holding 27MHz ticks, intended as a
	// constant addition to Base.
	Extension uint16
}

PCR represents a Program Clock Reference.

func (PCR) Ticks

func (p PCR) Ticks() uint64

Returns the number of ticks of a 27MHz clock in p.

type PESHeader

type PESHeader struct {
	// Scrambling informs decoders how packet contents are encrypted or "scrambled".
	// Zero indicates the stream is not scrambled.
	Scrambling uint8
	// Priority signals to decoders that this packet should be processed before others.
	Priority bool
	// Alignment indicates thatheader is immediately followed by
	// the video start code or audio syncword.
	Alignment bool
	// Copyrighted signals that stream's content is copyrighted.
	Copyrighted bool
	// Original signals whether the stream is an original or copy.
	Original bool
	// Fields indicates which optional fields are present in Optional.
	Fields       uint8
	Presentation *Timestamp
	Decode       *Timestamp
	// Optional holds any bytes that we don't know how to decode (yet).
	// TOOD(otl): also contains stuffing bytes, which should be stored separately.
	Optional []byte
}

PESHeader represents the optional header of a PES packet.

type PESPacket

type PESPacket struct {
	// ID uniquely identifies this stream from other elementary streams.
	ID byte
	// Length is the number of bytes for this packet.
	// A value of zero indicates the packet can be of any length,
	// but is only valid when the underlying stream is video.
	Length uint16
	Header *PESHeader
	// Data contains raw audio/video data.
	Data []byte
}

PESPacket represents a packetised elementary stream (PES) packet. These are transported in MPEG-TS packet payloads.

type Packet

type Packet struct {
	// If true, the packet should be discarded.
	Error bool
	// Indicates whether the payload holds the first byte of a particular payload such as ... TODO(otl)
	PayloadStart bool
	// If true, this packet has higher priority than other packets
	// with the same PID.
	Priority bool
	// Identifies the type of the packet.
	PID PacketID
	// Specifies which algorithm, if any, is used to encrypt the payload.
	Scrambling Scramble
	Continuity uint8
	Adaptation *Adaptation
	PES        *PESPacket
	// Payload contains the raw bytes of any payload we do not support decoding.
	Payload []byte
	// contains filtered or unexported fields
}

func Decode

func Decode(r io.Reader) (*Packet, error)

type PacketID

type PacketID uint16
const (
	PAT  PacketID = iota // Program association table
	CAT                  // Conditional access table
	TSDT                 // Transport stream description table
	IPMP

	PacketNull PacketID = 8191 // max 13-bit integer
)

func (PacketID) String

func (id PacketID) String() string

type Scanner

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

func NewScanner

func NewScanner(rd io.Reader) *Scanner

func (*Scanner) Err

func (sc *Scanner) Err() error

func (*Scanner) Packet

func (sc *Scanner) Packet() *Packet

func (*Scanner) Scan

func (sc *Scanner) Scan() bool

type Scramble

type Scramble uint8
const (
	ScrambleNone Scramble = 0

	ScrambleEven Scramble = 0x80
	ScrambleOdd  Scramble = 0xc0
)

type Timestamp

type Timestamp struct {
	// PTS indicates the packet carrying the timestamp contains a presentation timestamp.
	PTS bool
	// DTS indicates the packet carrying this timestamp contains a decode timestamp.
	DTS bool
	// Ticks holds a 33-bit integer counting the number of ticks of a 90KHz clock.
	Ticks uint64
}

Timestamp represents the timestamp transported in a PES packet header. It is used by decoders to reliably time and synchronise playback of video/audio payloads.

Jump to

Keyboard shortcuts

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