mpegts

package module
v0.0.0-...-f6032e5 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 7 Imported by: 0

README

go-mpegts

Go Reference

MPEG-TS

Features:

  • TS header parser
  • TS Slicer
  • PSI Assembler/Packetizer
    • PAT
    • PMT
    • SDT
  • PES header parser
  • CRC32 (ITU V.42)
  • Textcode
    • GB 2312-1980
    • ISO/IEC 6937
    • ISO/IEC 8859

Installation

To install the library use the following command in the project directory:

go get github.com/cesbo/go-mpegts

Documentation

Index

Examples

Constants

View Source
const (
	PatHeaderSize  = 8
	PatMaximumSize = 1024
	PatItemSize    = 4
)
View Source
const (
	PmtHeaderSize  = 12
	PmtMaximumSize = 1024
	PmtItemSize    = 5
)
View Source
const (
	// First 3 bytes of the PSI packet. Contains Table ID and Section Length
	PsiHeaderSize = 3

	// The maximum number of bytes in a section of
	// a ITU-T Rec. H.222.0 | ISO/IEC 13818-1 defined PSI table is 1024 bytes.
	// The maximum number of bytes in a private_section is 4096 bytes.
	// Includes PsiHeaderSize
	PsiMaximumSize = 4096
)
View Source
const (
	SdtHeaderSize  = 11
	SdtMaximumSize = 1024
	SdtItemSize    = 5
)
View Source
const (
	SyncByte   uint8 = 0x47
	PacketSize int   = 188
)
View Source
const (
	ProgramClock = 27e6 // 27MHz
)
View Source
const (
	SystemClock = 90000 // 90kHz
)

Variables

View Source
var (
	ErrCC          = errors.New("psi: discontinuity received")
	ErrPUSI        = errors.New("psi: pointer field out of range")
	ErrAssemblePSI = errors.New("psi: assemble failed")
	ErrCRC         = errors.New("psi: checksum not match")
	ErrPsiFormat   = errors.New("psi: invalid format")
)
View Source
var (
	ErrSyncTS      = errors.New("ts slicer: sync error")
	ErrNotComplete = errors.New("ts slicer: not complete")
)
View Source
var (
	ErrDescriptorFormat = errors.New("descriptor: invalid format")
)
View Source
var (
	ErrPatFormat = errors.New("pat: invalid format")
)
View Source
var (
	ErrPmtFormat = errors.New("pmt: invalid format")
)
View Source
var (
	ErrSdtFormat = errors.New("sdt: invalid format")
)
View Source
var NullTS = TS{}/* 188 elements not displayed */

Functions

This section is empty.

Types

type AssembleFn

type AssembleFn func(error)

PSI assembler callback

type Desc_48

type Desc_48 struct {
	ServiceProviderName string
	ServiceName         string
}

func (*Desc_48) Decode

func (d *Desc_48) Decode(desc Descriptors) error

func (*Desc_48) Encode

func (d *Desc_48) Encode() (desc Descriptors)

func (*Desc_48) String

func (d *Desc_48) String() string

type Desc_7A

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

func (*Desc_7A) AsvcFlag

func (d *Desc_7A) AsvcFlag() bool

func (*Desc_7A) BsidFlag

func (d *Desc_7A) BsidFlag() bool

func (*Desc_7A) ComponentTypeFlag

func (d *Desc_7A) ComponentTypeFlag() bool

func (*Desc_7A) Decode

func (d *Desc_7A) Decode(desc Descriptors) error

func (*Desc_7A) Encode

func (d *Desc_7A) Encode() (desc Descriptors)

func (*Desc_7A) MainidFlag

func (d *Desc_7A) MainidFlag() bool

func (*Desc_7A) Mixinfoexists

func (d *Desc_7A) Mixinfoexists() bool

func (*Desc_7A) String

func (d *Desc_7A) String() string

func (*Desc_7A) Substream1Flag

func (d *Desc_7A) Substream1Flag() bool

func (*Desc_7A) Substream2Flag

func (d *Desc_7A) Substream2Flag() bool

func (*Desc_7A) Substream3Flag

func (d *Desc_7A) Substream3Flag() bool

type Descriptors

type Descriptors []byte

func (Descriptors) Check

func (d Descriptors) Check() error

func (Descriptors) Next

func (d Descriptors) Next() Descriptors

type PAT

type PAT struct {
	Items []*PatItem
	// contains filtered or unexported fields
}

PAT is Program Association Table

func NewPat

func NewPat() *PAT

func (*PAT) Finalize

func (p *PAT) Finalize()

Calculates LastSectionNumber

func (*PAT) Packetizer

func (p *PAT) Packetizer() *PsiPacketizer

Packetizer returns a new PsiPacketizer to get TS packets from PAT

Example
pat := NewPat()
pat.SetVersion(1)
pat.SetTSID(1)

item := NewPatItem()
item.SetPNR(1)
item.SetPID(1031)
pat.Items = append(pat.Items, item)

pat.Finalize()

ts := NewTS(0)
for pack := pat.Packetizer(); pack.Next(ts); ts.IncrementCC() {
	// TS Header with Payload start offset
	fmt.Printf("%X...\n", ts[:25])
}
Output:

474000100000B00D0001C300000001E407FF2D3E2FFFFFFFFF...

func (*PAT) ParsePatSection

func (p *PAT) ParsePatSection(b []byte) error
Example
data := []byte{
	0x00, 0xB0, 0x0D, 0x70, 0xE9, 0xDD, 0x00, 0x00,
	0x00, 0x01, 0xE0, 0x42, 0xAE, 0xF4, 0xD8, 0x1C,
}

pat := new(PAT)
if err := pat.ParsePatSection(data); err != nil {
	panic(err)
}

fmt.Println("TSID", pat.TSID())
for _, item := range pat.Items {
	fmt.Printf("Program Number %d PID %d\n", item.PNR(), item.PID())
}
Output:

TSID 28905
Program Number 1 PID 66

func (*PAT) SetTSID

func (p *PAT) SetTSID(tsid uint16)

func (*PAT) SetVersion

func (p *PAT) SetVersion(version uint8)

func (*PAT) TSID

func (p *PAT) TSID() uint16

Returns Transport Stream ID

func (*PAT) Version

func (p *PAT) Version() uint8

type PCR

type PCR uint64

PCR is Program Clock Reference

const (
	NonPcr PCR = (1 << 33) * 300
	MaxPcr PCR = NonPcr - 1
)

func (PCR) Add

func (p PCR) Add(u PCR) PCR

Add returns the timestamp p+u

func (PCR) Bitrate

func (p PCR) Bitrate(bytes int) int

Bitrate returns bitrate in bits per second for delta PCR.

func (PCR) Delta

func (p PCR) Delta(previous PCR) PCR

Delta returns the difference p-previous considering value overflow

func (PCR) EstimatedPCR

func (p PCR) EstimatedPCR(previous PCR, lastBlock, currentBlock uint64) PCR

EstimatedPCR returns estimated PCR value

| time -->
| X---------X---------X
|  \         \         \
|   \         \         estimated PCR
|    \         current PCR
|     previous PCR

- lastBlock - bytes between PCR(previous) and PCR(current) - currentBlock - bytes between PCR(current) and PCR(estimated)

func (PCR) Jitter

func (p PCR) Jitter(previous PCR) time.Duration

Jitter returns the difference between two PCR values in nanoseconds

type PES

type PES []byte

func (PES) CheckPrefix

func (p PES) CheckPrefix() bool

CheckPrefix checks is PES prefix equal to 0x000001.

func (PES) DTS

func (p PES) DTS() Timestamp

DTS returns DTS value.

func (PES) HasDTS

func (p PES) HasDTS() bool

HasDTS checks a Decoding TimeStamp (DTS) is defined in the PES header. DTS field presents only in pair with PTS field. If DTS field is not presented than DTS value equal to PTS.

func (PES) HasPTS

func (p PES) HasPTS() bool

HasPTS checks is a Presentation Time Stamp (PTS) defined in the PES header. PTS field presents only for elementary streams.

func (PES) IsES

func (p PES) IsES() bool

Returns true if PES has an Elementary Stream data

func (PES) PTS

func (p PES) PTS() Timestamp

PTS returns PTS value.

func (PES) SetDTS

func (p PES) SetDTS(value Timestamp)

SetDTS sets DTS value and turn on DTS flag.

func (PES) SetLength

func (p PES) SetLength(value int)

SetLength sets PES_packet_length field. Value specifying the number of bytes in the PES packet following the last byte of the field. 0 allowed only for video elementary stream.

func (PES) SetPTS

func (p PES) SetPTS(value Timestamp)

SetPTS sets PTS value and turn on PTS flag.

func (PES) SetPrefix

func (p PES) SetPrefix()

SetPrefix sets PES prefix to 0x000001.

func (PES) SetStreamID

func (p PES) SetStreamID(streamID uint8)

SetStreamID sets Stream ID field.

func (PES) StreamID

func (p PES) StreamID() uint8

StreamID returns the type and number of the elementary stream. Audio streams (0xC0-0xDF), Video streams (0xE0-0xEF)

type PID

type PID uint16

PID is Packet Identifier

const (
	NonPid PID = 8192
	MaxPid PID = NonPid - 1
)

type PMT

type PMT struct {
	Items []*PmtItem
	// contains filtered or unexported fields
}

PMT is Program Map Table

func NewPmt

func NewPmt() *PMT

func (*PMT) AppendDescriptors

func (p *PMT) AppendDescriptors(desc Descriptors)

func (*PMT) Descriptors

func (p *PMT) Descriptors() Descriptors

func (*PMT) Finalize

func (p *PMT) Finalize()

Calculates LastSectionNumber

func (*PMT) PCR

func (p *PMT) PCR() PID

func (*PMT) PNR

func (p *PMT) PNR() uint16

func (*PMT) Packetizer

func (p *PMT) Packetizer() *PsiPacketizer

Packetizer returns a new PsiPacketizer to get TS packets from PMT

func (*PMT) ParsePmtSection

func (p *PMT) ParsePmtSection(b []byte) error

func (*PMT) SetPCR

func (p *PMT) SetPCR(pcr PID)

func (*PMT) SetPNR

func (p *PMT) SetPNR(pnr uint16)

func (*PMT) SetVersion

func (p *PMT) SetVersion(version uint8)

func (*PMT) Version

func (p *PMT) Version() uint8

type PSI

type PSI struct {
	TableID           uint8
	Version           uint8
	SectionNumber     uint8
	LastSectionNumber uint8
	CRC               uint32
	// contains filtered or unexported fields
}

Program Specific Information (ISO 13818-1 / 2.4.4)

func (*PSI) Assemble

func (p *PSI) Assemble(packet TS, fn AssembleFn)

Assembles TS packets into single PSI. Calls fn when PSI is ready or error occurs

func (*PSI) Clear

func (p *PSI) Clear()

Clears buffer

func (*PSI) Payload

func (p *PSI) Payload() []byte

Returns PSI payload and assembling error status. Should be used in AssembleFn. Buffer length is equal to PsiHeaderSize + Section Length.

type PatItem

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

func NewPatItem

func NewPatItem() *PatItem

func (*PatItem) PID

func (p *PatItem) PID() PID

Returns PID of the Program Map Table

func (*PatItem) PNR

func (p *PatItem) PNR() uint16

Returns Program Number

func (*PatItem) SetPID

func (p *PatItem) SetPID(pid PID)

func (*PatItem) SetPNR

func (p *PatItem) SetPNR(pnr uint16)

type PmtItem

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

PMT Item contains information about elementary stream

func NewPmtItem

func NewPmtItem() *PmtItem

func (*PmtItem) AppendDescriptors

func (p *PmtItem) AppendDescriptors(desc Descriptors)

func (*PmtItem) Clone

func (p *PmtItem) Clone() *PmtItem

func (*PmtItem) Descriptors

func (p *PmtItem) Descriptors() Descriptors

func (*PmtItem) PID

func (p *PmtItem) PID() PID

func (*PmtItem) SetPID

func (p *PmtItem) SetPID(pid PID)

func (*PmtItem) SetType

func (p *PmtItem) SetType(ty uint8)

func (*PmtItem) StreamType

func (p *PmtItem) StreamType() StreamType

StreamType returns stream type by element ID and related descriptors

func (*PmtItem) Type

func (p *PmtItem) Type() uint8

type PsiPacketizer

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

PsiPacketizer is a helper to splits PSI section into multiple TS packets. If data more than fits into one section, it will be split into multiple sections.

func (*PsiPacketizer) Next

func (p *PsiPacketizer) Next(ts TS) bool

type SDT

type SDT struct {
	Items []*SdtItem
	// contains filtered or unexported fields
}

SDT is Service Description Table

func NewSdt

func NewSdt() *SDT

func (*SDT) Actual

func (s *SDT) Actual() bool

func (*SDT) Finalize

func (s *SDT) Finalize()

Calculates LastSectionNumber

func (*SDT) ONID

func (s *SDT) ONID() uint16

func (*SDT) Packetizer

func (s *SDT) Packetizer() *PsiPacketizer

Packetizer returns a new PsiPacketizer to get TS packets from SDT

func (*SDT) ParseSdtSection

func (s *SDT) ParseSdtSection(b []byte) error

func (*SDT) SetONID

func (s *SDT) SetONID(onid uint16)

func (*SDT) SetTSID

func (s *SDT) SetTSID(tsid uint16)

func (*SDT) SetVersion

func (s *SDT) SetVersion(version uint8)

func (*SDT) TSID

func (s *SDT) TSID() uint16

func (*SDT) Version

func (s *SDT) Version() uint8

type ScramblingControl

type ScramblingControl byte
const (
	NotScrambled     ScramblingControl = 0
	ScrambledEvenKey ScramblingControl = 2 // 10
	ScrambledOddKey  ScramblingControl = 3 // 11
)

type SdtItem

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

func NewSdtItem

func NewSdtItem() *SdtItem

func (*SdtItem) AppendDescriptors

func (s *SdtItem) AppendDescriptors(desc Descriptors)

Appends descriptors to the last added item

func (*SdtItem) Descriptors

func (s *SdtItem) Descriptors() Descriptors

func (*SdtItem) IsPresentFollowing

func (s *SdtItem) IsPresentFollowing() bool

Returns true if present-following information is present in the stream EIT_present_following_flag

func (*SdtItem) IsSchedule

func (s *SdtItem) IsSchedule() bool

Returns true if schedule information is present in the stream EIT_schedule_flag

func (*SdtItem) IsScrambled

func (s *SdtItem) IsScrambled() bool

Returns false if stream is not scrambled Returns true if one or more streams may be controlled by a CA system free_CA_mode

func (*SdtItem) RunningStatus

func (s *SdtItem) RunningStatus() uint8

func (*SdtItem) ServiceID

func (s *SdtItem) ServiceID() uint16

func (*SdtItem) SetPresentFollowing

func (s *SdtItem) SetPresentFollowing(flag bool)

func (*SdtItem) SetRunningStatus

func (s *SdtItem) SetRunningStatus(status uint8)

func (*SdtItem) SetSchedule

func (s *SdtItem) SetSchedule(flag bool)

func (*SdtItem) SetScrambled

func (s *SdtItem) SetScrambled(flag bool)

func (*SdtItem) SetServiceID

func (s *SdtItem) SetServiceID(id uint16)

type Slicer

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

Slicer is a tool for split TS buffer to TS packets. If buffer length is not multiple to TS packet size it store remain data in the internal buffer and will be used on the next iteration.

Example
slicer := Slicer{}
buffer := NullTS

for packet := slicer.Begin(buffer); packet != nil; packet = slicer.Next() {
	fmt.Println("PID", packet.PID())
}
Output:

PID 8191

func (*Slicer) Begin

func (s *Slicer) Begin(buffer []byte) TS

Prepares buffer and get first packet

func (*Slicer) Err

func (s *Slicer) Err() error

Returns number of bytes processed and error if happens

func (*Slicer) Next

func (s *Slicer) Next() TS

Get next packet

type StreamType

type StreamType int

StreamType is an elementary stream type

const (
	StreamData StreamType = iota
	StreamVideoH261
	StreamVideoH262
	StreamVideoH263
	StreamVideoH264
	StreamVideoH265
	StreamAudioMP2
	StreamAudioMP3
	StreamAudioAAC
	StreamAudioLATM
	StreamAudioAC3
	StreamAudioEAC3
	StreamDataSCTE35
	StreamDataSubtitles
	StreamDataTeletext
	StreamDataAIT
)

func (StreamType) String

func (t StreamType) String() string

type TS

type TS []byte

ISO/IEC 13818-1 : 2.4.3 Specification of the Transport Stream syntax and semantics

Example
packet := TS([]byte{0x47, 0x40, 0x11, 0x15})
fmt.Println("PID", packet.PID(), "CC", packet.CC())
Output:

PID 17 CC 5

func NewTS

func NewTS(pid PID) TS

NewTS allocates new TS packet. Sets sync byte and PID

func (TS) CC

func (p TS) CC() uint8

CC returns continuity counter value. Continuity Counter is a 4-bit field incrementing by 1 for each packet with payload on the same PID.

func (TS) CheckCC

func (p TS) CheckCC(previous uint8) (uint8, bool)

CheckCC checks continuity counter Returns current CC and true if CC is equal to expected value

func (TS) ClearAF

func (p TS) ClearAF()

ClearAF clears the Adaptation Field bit

func (TS) ClearPUSI

func (p TS) ClearPUSI()

ClearPUSI clears Payload Unit Start Indicator bit

func (TS) Fill

func (p TS) Fill(size int)

Fill fills incomplete TS packet with adaptation field stuffing bytes

func (TS) HasAF

func (p TS) HasAF() bool

HasAF checks the Adaptation Field bit

func (TS) HasPCR

func (p TS) HasPCR() bool

HasPCR returns true if PCR flag is set in the Adaptation Field. Make sure that Adaptation Field is not empty: packet HeaderSize() more or equal than 6 bytes.

func (TS) HasPUSI

func (p TS) HasPUSI() bool

HasPUSI checks is payload starts in the packet (Payload Unit Start Indicator)

func (TS) HasPayload

func (p TS) HasPayload() bool

HasPayload checks is packet has payload

func (TS) HasTEI

func (p TS) HasTEI() bool

HasTEI checks the Transport Error Indicator bit

func (TS) HeaderSize

func (p TS) HeaderSize() int

HeaderSize returns size of packet header

func (TS) IncrementCC

func (p TS) IncrementCC()

IncrementCC increments continuity counter in packet

func (TS) PCR

func (p TS) PCR() PCR

PCR returns PCR value from the Adaptation Field. Packet should be with Adaptation Field

func (TS) PID

func (p TS) PID() PID

PID returns packet identifier value. PID is a 13-bit field that identifies the payload carried in the packet.

func (TS) Payload

func (p TS) Payload() []byte

Payload returns payload if available

func (TS) SetAF

func (p TS) SetAF()

SetAF sets Adaptation Field bit

func (TS) SetCC

func (p TS) SetCC(cc uint8)

SetCC sets continuity counter in packet

func (TS) SetPCR

func (p TS) SetPCR(value PCR)

SetPCR sets PCR flag and PCR value in the Adaptation Field.

func (TS) SetPID

func (p TS) SetPID(pid PID)

SetPID sets PID in packet

func (TS) SetPUSI

func (p TS) SetPUSI()

SetPUSI sets Payload Unit Start Indicator bit

func (TS) SetPayload

func (p TS) SetPayload()

SetPayload sets Payload bit

func (TS) TSC

func (p TS) TSC() ScramblingControl

TSC returns 2-bit Transport Scrambling Control field

type Timestamp

type Timestamp uint64

Timestamp is a 33-bit MPEG-2 timestamp for PTS/DTS

const (
	NonTimestamp Timestamp = 1 << 33
	MaxTimestamp Timestamp = NonTimestamp - 1
)

func Scale

func Scale(duration, timescale int) Timestamp

Scale returns the timestamp from the duration and timescale. For example, need to get number of 90kHz ticks in 250 milliseconds. Duration is 250 and the timescale is 1000 - number of milliseconds in second. `Scale(250, 1000)` return 22500 ticks

func (Timestamp) Add

func (t Timestamp) Add(u Timestamp) Timestamp

Add returns the timestamp t+u

func (Timestamp) Delta

func (t Timestamp) Delta(previous Timestamp) Timestamp

Delta returns the difference t-u considering value overflow

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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