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 ¶
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.
const PacketSize int = 188
PacketSize is the length of a MPEG-TS packet in bytes.
const Sync byte = 'G'
Sync is the first byte of a MPEG-TS packet.
Variables ¶
var ErrLongPacket = errors.New("long packet")
var ErrShortPacket = errors.New("short packet")
Functions ¶
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.
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 }
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.