streaming

Media streaming and broadcast systems in Go
Log | Files | Refs | README | LICENSE

mpegts.go (2799B)


      1 // Package mpegts implements encoding and decoding of MPEG-TS packets as specified in ITU-T H.222.0.
      2 package mpegts
      3 
      4 import "fmt"
      5 
      6 // PacketSize is the length of a MPEG-TS packet in bytes.
      7 const PacketSize int = 188
      8 
      9 // Sync is the first byte of a MPEG-TS packet.
     10 const Sync byte = 'G'
     11 
     12 type Packet struct {
     13 	// If true, the packet should be discarded.
     14 	Error bool
     15 	// Indicates whether the payload holds the first byte of a particular payload such as ... TODO(otl)
     16 	PayloadStart bool
     17 	// If true, this packet has higher priority than other packets
     18 	// with the same PID.
     19 	Priority bool
     20 	// Identifies the type of the packet.
     21 	PID PacketID
     22 	// Specifies which algorithm, if any, is used to encrypt the payload.
     23 	Scrambling Scramble
     24 	Continuity uint8
     25 	Adaptation *Adaptation
     26 	PES        *PESPacket
     27 	// Payload contains the raw bytes of any payload we do not support decoding.
     28 	Payload         []byte
     29 	emptyAdaptation bool
     30 }
     31 
     32 type Scramble uint8
     33 
     34 const (
     35 	ScrambleNone Scramble = 0
     36 	_
     37 	ScrambleEven Scramble = 0x80
     38 	ScrambleOdd  Scramble = 0xc0
     39 )
     40 
     41 type PacketID uint16
     42 
     43 const (
     44 	PAT  PacketID = iota // Program association table
     45 	CAT                  // Conditional access table
     46 	TSDT                 // Transport stream description table
     47 	IPMP
     48 	_                          // reserved through to
     49 	PacketNull PacketID = 8191 // max 13-bit integer
     50 )
     51 
     52 func (id PacketID) String() string {
     53 	switch id {
     54 	case PAT:
     55 		return "PAT"
     56 	case CAT:
     57 		return "CAT"
     58 	case TSDT:
     59 		return "TSDT"
     60 	case IPMP:
     61 		return "IPMP"
     62 	case PacketNull:
     63 		return "null"
     64 	}
     65 	return fmt.Sprintf("%d", id)
     66 }
     67 
     68 // Adaptation represents an adaptation field including the header.
     69 type Adaptation struct {
     70 	// If true, there is a discontinuity between this packet and
     71 	// either the continuity conter or PCR.
     72 	Discontinuous bool
     73 	// If true, the packet may be used as an index from which to
     74 	// randomly access other points in the stream.
     75 	RandomAccess bool
     76 	// Whether this stream is high priority.
     77 	Priority           bool
     78 	SpliceCountdownSet bool
     79 	PCR                *PCR
     80 	// Original program clock reference.
     81 	OPCR *PCR
     82 	// The number of packets remaining until a splicing point.
     83 	SpliceCountdown uint8
     84 	// Raw bytes of any application-specific data.
     85 	Private []byte
     86 	// Raw bytes of the adaptation extension field.
     87 	Extension []byte
     88 	// A slice of bytes all with the value 0xff; enough to ensure
     89 	// a packet's length is always PacketSize.
     90 	Stuffing []byte
     91 }
     92 
     93 // PCR represents a Program Clock Reference.
     94 type PCR struct {
     95 	// 33-bit integer holding the number of ticks of a 90KHz clock.
     96 	Base uint64
     97 	// 9-bit integer holding 27MHz ticks, intended as a
     98 	// constant addition to Base.
     99 	Extension uint16
    100 }
    101 
    102 // Returns the number of ticks of a 27MHz clock in p.
    103 func (p PCR) Ticks() uint64 {
    104 	return p.Base*300 + uint64(p.Extension)
    105 }