2017-12-11 08:20:24 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2018-08-19 13:59:22 +03:00
|
|
|
mpegts.go - provides a data structure intended to encapsulate the properties
|
2019-05-11 14:44:28 +03:00
|
|
|
of an MPEG-TS packet and also functions to allow manipulation of these packets.
|
2017-12-11 08:20:24 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
2018-01-07 17:32:56 +03:00
|
|
|
Saxon A. Nelson-Milton <saxon.milton@gmail.com>
|
2017-12-11 08:20:24 +03:00
|
|
|
|
|
|
|
LICENSE
|
2018-08-19 13:59:22 +03:00
|
|
|
mpegts.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
2017-12-11 08:20:24 +03:00
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
2017-12-11 07:54:49 +03:00
|
|
|
|
2017-12-11 08:20:24 +03:00
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
2019-05-11 09:59:15 +03:00
|
|
|
// Package mts provides MPEGT-TS (mts) encoding and related functions.
|
2018-08-19 13:59:22 +03:00
|
|
|
package mts
|
2018-01-07 17:32:56 +03:00
|
|
|
|
2018-12-14 06:05:56 +03:00
|
|
|
import (
|
2019-02-27 09:42:52 +03:00
|
|
|
"fmt"
|
2019-02-15 04:31:07 +03:00
|
|
|
|
|
|
|
"github.com/Comcast/gots/packet"
|
2019-07-23 17:34:10 +03:00
|
|
|
gotspsi "github.com/Comcast/gots/psi"
|
2019-07-01 12:38:20 +03:00
|
|
|
"github.com/pkg/errors"
|
2019-07-04 03:20:46 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/container/mts/meta"
|
|
|
|
"bitbucket.org/ausocean/av/container/mts/psi"
|
2018-12-14 06:05:56 +03:00
|
|
|
)
|
|
|
|
|
2019-05-07 17:36:05 +03:00
|
|
|
const PacketSize = 188
|
2018-01-16 08:06:51 +03:00
|
|
|
|
2019-02-07 07:36:54 +03:00
|
|
|
// Program ID for various types of ts packets.
|
2019-01-25 08:55:01 +03:00
|
|
|
const (
|
2019-02-07 07:36:54 +03:00
|
|
|
SdtPid = 17
|
|
|
|
PatPid = 0
|
|
|
|
PmtPid = 4096
|
|
|
|
VideoPid = 256
|
2019-06-05 17:58:26 +03:00
|
|
|
AudioPid = 210
|
2019-01-25 08:55:01 +03:00
|
|
|
)
|
|
|
|
|
2019-02-07 07:36:54 +03:00
|
|
|
// StreamID is the id of the first stream.
|
|
|
|
const StreamID = 0xe0
|
|
|
|
|
2019-05-11 14:44:28 +03:00
|
|
|
// HeadSize is the size of an MPEG-TS packet header.
|
2019-02-07 07:36:54 +03:00
|
|
|
const HeadSize = 4
|
|
|
|
|
|
|
|
// Consts relating to adaptation field.
|
2019-01-25 08:55:01 +03:00
|
|
|
const (
|
2019-02-15 04:31:07 +03:00
|
|
|
AdaptationIdx = 4 // Index to the adaptation field (index of AFL).
|
|
|
|
AdaptationControlIdx = 3 // Index to octet with adaptation field control.
|
|
|
|
AdaptationFieldsIdx = AdaptationIdx + 1 // Adaptation field index is the index of the adaptation fields.
|
|
|
|
DefaultAdaptationSize = 2 // Default size of the adaptation field.
|
|
|
|
AdaptationControlMask = 0x30 // Mask for the adaptation field control in octet 3.
|
2019-03-02 05:51:46 +03:00
|
|
|
DefaultAdaptationBodySize = 1 // Default size of the adaptation field body.
|
|
|
|
DiscontinuityIndicatorMask = 0x80 // Mask for the discontinuity indicator at the discontinuity indicator idk.
|
|
|
|
DiscontinuityIndicatorIdx = AdaptationIdx + 1 // The index at which the discontinuity indicator is found in an MTS packet.
|
2019-01-25 08:55:01 +03:00
|
|
|
)
|
|
|
|
|
2019-02-07 07:36:54 +03:00
|
|
|
// TODO: make this better - currently doesn't make sense.
|
2019-01-25 08:55:01 +03:00
|
|
|
const (
|
|
|
|
HasPayload = 0x1
|
|
|
|
HasAdaptationField = 0x2
|
|
|
|
)
|
|
|
|
|
2018-01-06 08:00:45 +03:00
|
|
|
/*
|
2019-04-05 08:58:25 +03:00
|
|
|
Packet encapsulates the fields of an MPEG-TS packet. Below is
|
2018-01-06 08:00:45 +03:00
|
|
|
the formatting of an MPEG-TS packet for reference!
|
2017-12-11 09:38:09 +03:00
|
|
|
|
2018-01-06 08:00:45 +03:00
|
|
|
MPEG-TS Packet Formatting
|
|
|
|
============================================================================
|
|
|
|
| octet no | bit 0 | bit 1 | bit 2 | bit 3 | bit 4 | bit 5 | bit 6 | bit 7 |
|
|
|
|
============================================================================
|
|
|
|
| octet 0 | sync byte (0x47) |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| octet 1 | TEI | PUSI | Prior | PID |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| octet 2 | PID cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| octet 3 | TSC | AFC | CC |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| octet 4 | AFL |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| octet 5 | DI | RAI | ESPI | PCRF | OPCRF | SPF | TPDF | AFEF |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| optional | PCR (48 bits => 6 bytes) |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | PCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | PCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | PCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | PCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | PCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| optional | OPCR (48 bits => 6 bytes) |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | OPCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | OPCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | OPCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | OPCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | OPCR cont. |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| optional | SC |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| optional | TPDL |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| optional | TPD (variable length) |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | ... |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| optional | Extension (variable length) |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | ... |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| optional | Stuffing (variable length) |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | ... |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| optional | Payload (variable length) |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
| - | ... |
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
*/
|
2018-08-18 04:57:36 +03:00
|
|
|
type Packet struct {
|
2018-01-07 17:32:56 +03:00
|
|
|
TEI bool // Transport Error Indicator
|
|
|
|
PUSI bool // Payload Unit Start Indicator
|
|
|
|
Priority bool // Tranposrt priority indicator
|
|
|
|
PID uint16 // Packet identifier
|
|
|
|
TSC byte // Transport Scrambling Control
|
|
|
|
AFC byte // Adaption Field Control
|
|
|
|
CC byte // Continuity Counter
|
|
|
|
DI bool // Discontinouty indicator
|
|
|
|
RAI bool // random access indicator
|
|
|
|
ESPI bool // Elementary stream priority indicator
|
|
|
|
PCRF bool // PCR flag
|
|
|
|
OPCRF bool // OPCR flag
|
|
|
|
SPF bool // Splicing point flag
|
|
|
|
TPDF bool // Transport private data flag
|
|
|
|
AFEF bool // Adaptation field extension flag
|
|
|
|
PCR uint64 // Program clock reference
|
|
|
|
OPCR uint64 // Original program clock reference
|
|
|
|
SC byte // Splice countdown
|
|
|
|
TPDL byte // Tranposrt private data length
|
|
|
|
TPD []byte // Private data
|
|
|
|
Ext []byte // Adaptation field extension
|
2018-03-13 07:33:31 +03:00
|
|
|
Payload []byte // Mpeg ts Payload
|
2017-12-11 08:20:24 +03:00
|
|
|
}
|
|
|
|
|
2019-05-11 14:44:28 +03:00
|
|
|
// FindPmt will take a clip of MPEG-TS and try to find a PMT table - if one
|
2019-01-22 03:57:24 +03:00
|
|
|
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
|
2019-02-28 03:34:40 +03:00
|
|
|
func FindPmt(d []byte) ([]byte, int, error) {
|
|
|
|
return FindPid(d, PmtPid)
|
2019-02-13 07:10:58 +03:00
|
|
|
}
|
|
|
|
|
2019-05-11 14:44:28 +03:00
|
|
|
// FindPat will take a clip of MPEG-TS and try to find a PAT table - if one
|
2019-02-13 07:10:58 +03:00
|
|
|
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
|
2019-02-28 03:34:40 +03:00
|
|
|
func FindPat(d []byte) ([]byte, int, error) {
|
|
|
|
return FindPid(d, PatPid)
|
2019-02-13 07:10:58 +03:00
|
|
|
}
|
|
|
|
|
2019-07-04 08:04:34 +03:00
|
|
|
// Errors used by FindPid.
|
2019-06-15 21:38:41 +03:00
|
|
|
var (
|
2019-07-24 06:09:14 +03:00
|
|
|
ErrInvalidLen = errors.New("MPEG-TS data not of valid length")
|
|
|
|
errCouldNotFind = errors.New("could not find packet with given PID")
|
2019-06-15 21:38:41 +03:00
|
|
|
)
|
|
|
|
|
2019-05-11 14:44:28 +03:00
|
|
|
// FindPid will take a clip of MPEG-TS and try to find a packet with given PID - if one
|
2019-02-13 07:10:58 +03:00
|
|
|
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
|
2019-02-28 03:34:40 +03:00
|
|
|
func FindPid(d []byte, pid uint16) (pkt []byte, i int, err error) {
|
2019-01-12 10:06:35 +03:00
|
|
|
if len(d) < PacketSize {
|
2019-07-24 06:09:14 +03:00
|
|
|
return nil, -1, ErrInvalidLen
|
2018-12-14 08:45:02 +03:00
|
|
|
}
|
2019-01-22 03:51:12 +03:00
|
|
|
for i = 0; i < len(d); i += PacketSize {
|
2019-02-13 07:10:58 +03:00
|
|
|
p := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2])
|
|
|
|
if p == pid {
|
2019-05-10 11:08:22 +03:00
|
|
|
pkt = d[i : i+PacketSize]
|
2018-12-14 06:05:56 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 21:38:41 +03:00
|
|
|
return nil, -1, errCouldNotFind
|
2018-12-14 06:05:56 +03:00
|
|
|
}
|
|
|
|
|
2019-07-11 08:21:23 +03:00
|
|
|
// LastPid will take a clip of MPEG-TS and try to find a packet
|
|
|
|
// with given PID searching in reverse from the end of the clip. If
|
|
|
|
// one is found, then it is returned along with its index, otherwise
|
|
|
|
// nil, -1 and an error is returned.
|
|
|
|
func LastPid(d []byte, pid uint16) (pkt []byte, i int, err error) {
|
|
|
|
if len(d) < PacketSize {
|
2019-07-24 06:09:14 +03:00
|
|
|
return nil, -1, ErrInvalidLen
|
2019-07-11 08:21:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i = len(d) - PacketSize; i >= 0; i -= PacketSize {
|
|
|
|
p := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2])
|
|
|
|
if p == pid {
|
|
|
|
pkt = d[i : i+PacketSize]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, -1, errCouldNotFind
|
|
|
|
}
|
|
|
|
|
2019-07-24 06:09:14 +03:00
|
|
|
// Errors used by FindPSI.
|
|
|
|
var (
|
|
|
|
ErrMultiplePrograms = errors.New("more than one program not supported")
|
|
|
|
ErrNoPrograms = errors.New("no programs in PAT")
|
|
|
|
ErrNotConsecutive = errors.New("could not find consecutive PIDs")
|
|
|
|
)
|
|
|
|
|
|
|
|
// FindPSI finds the index of a PAT in an a slice of MPEG-TS and returns, along
|
2019-08-06 09:08:34 +03:00
|
|
|
// with a map of meta from the PMT and the stream PIDs and their types.
|
|
|
|
func FindPSI(d []byte) (int, map[uint16]uint8, map[string]string, error) {
|
2019-07-24 06:09:14 +03:00
|
|
|
if len(d) < PacketSize {
|
|
|
|
return -1, nil, nil, ErrInvalidLen
|
2019-07-11 08:21:23 +03:00
|
|
|
}
|
2019-07-24 06:09:14 +03:00
|
|
|
|
|
|
|
// Find the PAT if it exists.
|
|
|
|
pkt, i, err := FindPid(d, PatPid)
|
|
|
|
if err != nil {
|
|
|
|
return -1, nil, nil, errors.Wrap(err, "error finding PAT")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's take this opportunity to check what programs are in this MPEG-TS
|
|
|
|
// stream, and therefore the PID of the PMT, from which we can get metadata.
|
|
|
|
// NB: currently we only support one program.
|
|
|
|
progs, err := Programs(pkt)
|
|
|
|
if err != nil {
|
|
|
|
return i, nil, nil, errors.Wrap(err, "cannot get programs from PAT")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(progs) == 0 {
|
|
|
|
return i, nil, nil, ErrNoPrograms
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(progs) > 1 {
|
|
|
|
return i, nil, nil, ErrMultiplePrograms
|
|
|
|
}
|
|
|
|
|
|
|
|
pmtPID := pmtPIDs(progs)[0]
|
|
|
|
|
|
|
|
// Now we can look for the PMT. We want to adjust d so that we're not looking
|
|
|
|
// at the same data twice.
|
|
|
|
d = d[i+PacketSize:]
|
|
|
|
pkt, pmtIdx, err := FindPid(d, pmtPID)
|
|
|
|
if err != nil {
|
|
|
|
return i, nil, nil, errors.Wrap(err, "error finding PMT")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the PMT comes straight after the PAT.
|
|
|
|
if pmtIdx != 0 {
|
|
|
|
return i, nil, nil, ErrNotConsecutive
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can try to get meta from the PMT.
|
|
|
|
meta, _ := metaFromPMT(pkt)
|
|
|
|
|
|
|
|
// Now to get the elementary streams defined for this program.
|
|
|
|
streams, err := Streams(pkt)
|
|
|
|
if err != nil {
|
|
|
|
return i, nil, meta, errors.Wrap(err, "could not get streams from PMT")
|
|
|
|
}
|
|
|
|
|
2019-08-06 09:10:37 +03:00
|
|
|
streamMap := make(map[uint16]uint8)
|
2019-08-06 09:08:34 +03:00
|
|
|
for _, s := range streams {
|
|
|
|
streamMap[s.ElementaryPid()] = s.StreamType()
|
|
|
|
}
|
|
|
|
|
|
|
|
return i, streamMap, meta, nil
|
2018-12-14 06:05:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 07:33:31 +03:00
|
|
|
// FillPayload takes a channel and fills the packets Payload field until the
|
2018-03-13 03:54:37 +03:00
|
|
|
// channel is empty or we've the packet reaches capacity
|
2018-08-18 04:57:36 +03:00
|
|
|
func (p *Packet) FillPayload(data []byte) int {
|
2019-05-07 16:40:03 +03:00
|
|
|
currentPktLen := 6 + asInt(p.PCRF)*6
|
|
|
|
if len(data) > PacketSize-currentPktLen {
|
|
|
|
p.Payload = make([]byte, PacketSize-currentPktLen)
|
2019-04-05 08:58:25 +03:00
|
|
|
} else {
|
|
|
|
p.Payload = make([]byte, len(data))
|
|
|
|
}
|
2018-08-18 04:57:36 +03:00
|
|
|
return copy(p.Payload, data)
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:40:03 +03:00
|
|
|
// Bytes interprets the fields of the ts packet instance and outputs a
|
|
|
|
// corresponding byte slice
|
|
|
|
func (p *Packet) Bytes(buf []byte) []byte {
|
|
|
|
if buf == nil || cap(buf) < PacketSize {
|
|
|
|
buf = make([]byte, PacketSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.OPCRF {
|
|
|
|
panic("original program clock reference field unsupported")
|
|
|
|
}
|
|
|
|
if p.SPF {
|
|
|
|
panic("splicing countdown unsupported")
|
|
|
|
}
|
|
|
|
if p.TPDF {
|
|
|
|
panic("transport private data unsupported")
|
|
|
|
}
|
|
|
|
if p.AFEF {
|
|
|
|
panic("adaptation field extension unsupported")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[:6]
|
|
|
|
buf[0] = 0x47
|
|
|
|
buf[1] = (asByte(p.TEI)<<7 | asByte(p.PUSI)<<6 | asByte(p.Priority)<<5 | byte((p.PID&0xFF00)>>8))
|
|
|
|
buf[2] = byte(p.PID & 0x00FF)
|
|
|
|
buf[3] = (p.TSC<<6 | p.AFC<<4 | p.CC)
|
|
|
|
|
|
|
|
var maxPayloadSize int
|
2019-05-07 17:36:05 +03:00
|
|
|
if p.AFC&0x2 != 0 {
|
2019-05-07 16:40:03 +03:00
|
|
|
maxPayloadSize = PacketSize - 6 - asInt(p.PCRF)*6
|
|
|
|
} else {
|
|
|
|
maxPayloadSize = PacketSize - 4
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:36:05 +03:00
|
|
|
stuffingLen := maxPayloadSize - len(p.Payload)
|
|
|
|
if p.AFC&0x2 != 0 {
|
2019-05-07 16:40:03 +03:00
|
|
|
buf[4] = byte(1 + stuffingLen + asInt(p.PCRF)*6)
|
|
|
|
buf[5] = (asByte(p.DI)<<7 | asByte(p.RAI)<<6 | asByte(p.ESPI)<<5 | asByte(p.PCRF)<<4 | asByte(p.OPCRF)<<3 | asByte(p.SPF)<<2 | asByte(p.TPDF)<<1 | asByte(p.AFEF))
|
|
|
|
} else {
|
|
|
|
buf = buf[:4]
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 40; p.PCRF && i >= 0; i -= 8 {
|
|
|
|
buf = append(buf, byte((p.PCR<<15)>>uint(i)))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < stuffingLen; i++ {
|
|
|
|
buf = append(buf, 0xff)
|
|
|
|
}
|
|
|
|
curLen := len(buf)
|
|
|
|
buf = buf[:PacketSize]
|
2019-05-07 17:36:05 +03:00
|
|
|
copy(buf[curLen:], p.Payload)
|
2019-05-07 16:40:03 +03:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
func asInt(b bool) int {
|
|
|
|
if b {
|
|
|
|
return 1
|
2018-01-08 04:12:26 +03:00
|
|
|
}
|
2018-08-18 04:57:36 +03:00
|
|
|
return 0
|
2018-01-08 04:12:26 +03:00
|
|
|
}
|
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
func asByte(b bool) byte {
|
|
|
|
if b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
2018-02-28 16:46:59 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 05:45:18 +03:00
|
|
|
type Option func(p *packet.Packet)
|
2019-02-15 04:31:07 +03:00
|
|
|
|
|
|
|
// addAdaptationField adds an adaptation field to p, and applys the passed options to this field.
|
|
|
|
// TODO: this will probably break if we already have adaptation field.
|
2019-03-02 05:45:18 +03:00
|
|
|
func addAdaptationField(p *packet.Packet, options ...Option) error {
|
2019-02-15 06:13:01 +03:00
|
|
|
if packet.ContainsAdaptationField((*packet.Packet)(p)) {
|
2019-02-15 04:31:07 +03:00
|
|
|
return errors.New("Adaptation field is already present in packet")
|
|
|
|
}
|
|
|
|
// Create space for adaptation field.
|
|
|
|
copy(p[HeadSize+DefaultAdaptationSize:], p[HeadSize:len(p)-DefaultAdaptationSize])
|
|
|
|
|
|
|
|
// TODO: seperate into own function
|
|
|
|
// Update adaptation field control.
|
|
|
|
p[AdaptationControlIdx] &= 0xff ^ AdaptationControlMask
|
|
|
|
p[AdaptationControlIdx] |= AdaptationControlMask
|
|
|
|
// Default the adaptationfield.
|
|
|
|
resetAdaptation(p)
|
|
|
|
|
|
|
|
// Apply and options that have bee passed.
|
|
|
|
for _, option := range options {
|
|
|
|
option(p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resetAdaptation sets fields in ps adaptation field to 0 if the adaptation field
|
|
|
|
// exists, otherwise an error is returned.
|
2019-03-02 05:45:18 +03:00
|
|
|
func resetAdaptation(p *packet.Packet) error {
|
2019-02-15 06:13:01 +03:00
|
|
|
if !packet.ContainsAdaptationField((*packet.Packet)(p)) {
|
2019-02-15 04:31:07 +03:00
|
|
|
return errors.New("No adaptation field in this packet")
|
|
|
|
}
|
|
|
|
p[AdaptationIdx] = DefaultAdaptationBodySize
|
|
|
|
p[AdaptationIdx+1] = 0x00
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscontinuityIndicator returns and Option that will set p's discontinuity
|
|
|
|
// indicator according to f.
|
|
|
|
func DiscontinuityIndicator(f bool) Option {
|
2019-03-02 05:45:18 +03:00
|
|
|
return func(p *packet.Packet) {
|
2019-02-15 04:31:07 +03:00
|
|
|
set := byte(DiscontinuityIndicatorMask)
|
|
|
|
if !f {
|
|
|
|
set = 0x00
|
|
|
|
}
|
|
|
|
p[DiscontinuityIndicatorIdx] &= 0xff ^ DiscontinuityIndicatorMask
|
|
|
|
p[DiscontinuityIndicatorIdx] |= DiscontinuityIndicatorMask & set
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 07:48:55 +03:00
|
|
|
|
2019-07-04 03:20:46 +03:00
|
|
|
// Error used by GetPTSRange.
|
|
|
|
var errNoPTS = errors.New("could not find PTS")
|
|
|
|
|
2019-05-10 07:48:55 +03:00
|
|
|
// GetPTSRange retreives the first and last PTS of an MPEGTS clip.
|
2019-07-11 08:21:23 +03:00
|
|
|
// If there is only one PTS, it is included twice in the pts return value.
|
2019-05-11 10:00:29 +03:00
|
|
|
func GetPTSRange(clip []byte, pid uint16) (pts [2]uint64, err error) {
|
2019-07-12 07:31:49 +03:00
|
|
|
var _pts int64
|
|
|
|
// Get the first PTS for the given PID.
|
2019-07-01 06:06:11 +03:00
|
|
|
var i int
|
|
|
|
for {
|
2019-07-01 07:24:18 +03:00
|
|
|
if i >= len(clip) {
|
2019-07-11 08:21:23 +03:00
|
|
|
return pts, errNoPTS
|
2019-07-01 07:24:18 +03:00
|
|
|
}
|
2019-07-01 06:06:11 +03:00
|
|
|
pkt, _i, err := FindPid(clip[i:], pid)
|
|
|
|
if err != nil {
|
2019-07-11 08:21:23 +03:00
|
|
|
return pts, errors.Wrap(err, fmt.Sprintf("could not find packet of PID: %d", pid))
|
2019-07-01 06:06:11 +03:00
|
|
|
}
|
2019-07-12 07:31:49 +03:00
|
|
|
_pts, err = GetPTS(pkt)
|
|
|
|
if err == nil {
|
2019-07-01 06:06:11 +03:00
|
|
|
break
|
|
|
|
}
|
2019-07-01 07:31:49 +03:00
|
|
|
i += _i + PacketSize
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 07:31:49 +03:00
|
|
|
pts[0] = uint64(_pts)
|
2019-07-11 08:21:23 +03:00
|
|
|
pts[1] = pts[0] // Until we have find a second PTS.
|
2019-05-10 07:48:55 +03:00
|
|
|
|
2019-07-11 08:21:23 +03:00
|
|
|
// Get the last PTS searching in reverse from end of the clip.
|
|
|
|
first := i
|
|
|
|
i = len(clip)
|
|
|
|
for {
|
|
|
|
pkt, _i, err := LastPid(clip[:i], pid)
|
|
|
|
if err != nil || i <= first {
|
|
|
|
return pts, nil
|
|
|
|
}
|
2019-07-12 07:31:49 +03:00
|
|
|
_pts, err = GetPTS(pkt)
|
|
|
|
if err == nil {
|
2019-07-11 08:21:23 +03:00
|
|
|
break
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
2019-07-11 08:21:23 +03:00
|
|
|
i = _i
|
|
|
|
}
|
|
|
|
|
2019-07-12 07:31:49 +03:00
|
|
|
pts[1] = uint64(_pts)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-13 02:47:44 +03:00
|
|
|
var (
|
2019-07-23 17:34:10 +03:00
|
|
|
errNoPesPayload = errors.New("no PES payload")
|
|
|
|
errNoPesPTS = errors.New("no PES PTS")
|
|
|
|
errInvalidPesHeader = errors.New("invalid PES header")
|
2019-07-13 02:47:44 +03:00
|
|
|
errInvalidPesPayload = errors.New("invalid PES payload")
|
|
|
|
)
|
2019-07-12 07:31:49 +03:00
|
|
|
|
2019-07-12 07:54:30 +03:00
|
|
|
// GetPTS returns a PTS from a packet that has PES payload, or an error otherwise.
|
2019-07-12 07:31:49 +03:00
|
|
|
func GetPTS(pkt []byte) (pts int64, err error) {
|
2019-07-12 07:54:30 +03:00
|
|
|
// Check the Payload Unit Start Indicator.
|
2019-07-12 07:31:49 +03:00
|
|
|
if pkt[1]&0x040 == 0 {
|
|
|
|
err = errNoPesPayload
|
2019-07-11 08:21:23 +03:00
|
|
|
return
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 07:31:49 +03:00
|
|
|
// Compute start of PES payload and check its length.
|
|
|
|
start := HeadSize
|
|
|
|
if pkt[3]&0x20 != 0 {
|
2019-07-12 07:54:30 +03:00
|
|
|
// Adaptation field is present, so adjust start of payload accordingly.
|
2019-07-12 07:31:49 +03:00
|
|
|
start += 1 + int(pkt[4])
|
|
|
|
}
|
|
|
|
pes := pkt[start:]
|
|
|
|
|
2019-07-12 07:54:30 +03:00
|
|
|
if len(pes) < 14 {
|
2019-07-12 07:31:49 +03:00
|
|
|
err = errInvalidPesHeader
|
2019-07-11 08:21:23 +03:00
|
|
|
return
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
2019-07-11 08:21:23 +03:00
|
|
|
|
2019-07-12 07:54:30 +03:00
|
|
|
// Check the PTS DTS indicator.
|
2019-07-12 07:31:49 +03:00
|
|
|
if pes[7]&0xc0 == 0 {
|
|
|
|
err = errNoPesPTS
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pts = extractPTS(pes[9:14])
|
2019-06-29 08:28:16 +03:00
|
|
|
return
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
2019-06-11 18:31:51 +03:00
|
|
|
|
2019-07-12 07:31:49 +03:00
|
|
|
// extractTime extracts a PTS from the given data.
|
|
|
|
func extractPTS(d []byte) int64 {
|
|
|
|
return (int64((d[0]>>1)&0x07) << 30) | (int64(d[1]) << 22) | (int64((d[2]>>1)&0x7f) << 15) | (int64(d[3]) << 7) | int64((d[4]>>1)&0x7f)
|
|
|
|
}
|
|
|
|
|
2019-06-15 21:38:41 +03:00
|
|
|
var errNoMeta = errors.New("PMT does not contain meta")
|
|
|
|
|
2019-06-11 18:31:51 +03:00
|
|
|
// ExtractMeta returns a map of metadata from the first PMT's metaData
|
|
|
|
// descriptor, that is found in the MPEG-TS clip d. d must contain a series of
|
|
|
|
// complete MPEG-TS packets.
|
|
|
|
func ExtractMeta(d []byte) (map[string]string, error) {
|
2019-07-11 08:21:23 +03:00
|
|
|
pmt, _, err := FindPid(d, PmtPid)
|
2019-05-10 07:48:55 +03:00
|
|
|
if err != nil {
|
2019-06-11 18:31:51 +03:00
|
|
|
return nil, err
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
2019-07-11 08:21:23 +03:00
|
|
|
return metaFromPMT(pmt)
|
|
|
|
}
|
2019-06-11 18:31:51 +03:00
|
|
|
|
2019-07-11 08:21:23 +03:00
|
|
|
// metaFromPMT returns metadata, if any, from a PMT.
|
|
|
|
func metaFromPMT(d []byte) (m map[string]string, err error) {
|
|
|
|
// Get as PSI type, skipping the MTS header.
|
2019-07-12 07:31:49 +03:00
|
|
|
pmt := psi.PSIBytes(d[HeadSize:])
|
2019-06-11 18:31:51 +03:00
|
|
|
|
2019-07-11 08:21:23 +03:00
|
|
|
// Get the metadata descriptor.
|
|
|
|
_, desc := pmt.HasDescriptor(psi.MetadataTag)
|
|
|
|
if desc == nil {
|
|
|
|
return m, errNoMeta
|
2019-06-11 18:31:51 +03:00
|
|
|
}
|
2019-07-11 08:21:23 +03:00
|
|
|
// Get the metadata as a map, skipping the descriptor head.
|
|
|
|
return meta.GetAllAsMap(desc[2:])
|
2019-06-11 18:31:51 +03:00
|
|
|
}
|
2019-05-10 07:48:55 +03:00
|
|
|
|
2019-06-15 21:38:41 +03:00
|
|
|
// TrimToMetaRange trims a slice of MPEG-TS to a segment between two points of
|
|
|
|
// meta data described by key, from and to.
|
|
|
|
func TrimToMetaRange(d []byte, key, from, to string) ([]byte, error) {
|
|
|
|
if len(d)%PacketSize != 0 {
|
|
|
|
return nil, errors.New("MTS clip is not of valid size")
|
|
|
|
}
|
|
|
|
|
|
|
|
if from == to {
|
2019-07-04 13:17:34 +03:00
|
|
|
return nil, errors.New("'from' and 'to' cannot be identical")
|
2019-06-15 21:38:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
start = -1 // Index of the start of the segment in d.
|
|
|
|
end = -1 // Index of the end of segment in d.
|
|
|
|
off int // Index of remaining slice of d to check after each PMT found.
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Find the next PMT.
|
|
|
|
pmt, idx, err := FindPid(d[off:], PmtPid)
|
|
|
|
if err != nil {
|
|
|
|
switch -1 {
|
|
|
|
case start:
|
|
|
|
return nil, errMetaLowerBound
|
|
|
|
case end:
|
|
|
|
return nil, errMetaUpperBound
|
|
|
|
default:
|
|
|
|
panic("should not have got error from FindPid")
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
2019-06-15 21:38:41 +03:00
|
|
|
}
|
|
|
|
off += idx + PacketSize
|
|
|
|
|
|
|
|
meta, err := ExtractMeta(pmt)
|
|
|
|
switch err {
|
|
|
|
case nil: // do nothing
|
|
|
|
case errNoMeta:
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if start == -1 {
|
|
|
|
if meta[key] == from {
|
|
|
|
start = off - PacketSize
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
2019-06-15 21:38:41 +03:00
|
|
|
} else if meta[key] == to {
|
|
|
|
end = off
|
|
|
|
return d[start:end], nil
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 21:38:41 +03:00
|
|
|
}
|
2019-06-16 22:00:52 +03:00
|
|
|
|
2019-06-17 10:04:22 +03:00
|
|
|
// SegmentForMeta returns segments of MTS slice d that correspond to a value of
|
|
|
|
// meta for key and val. Therefore, any sequence of packets corresponding to
|
|
|
|
// key and val will be appended to the returned [][]byte.
|
2019-06-16 22:00:52 +03:00
|
|
|
func SegmentForMeta(d []byte, key, val string) ([][]byte, error) {
|
|
|
|
var (
|
2019-06-17 10:04:22 +03:00
|
|
|
pkt packet.Packet // We copy data to this so that we can use comcast gots stuff.
|
|
|
|
segmenting bool // If true we are currently in a segment corresponsing to given meta.
|
|
|
|
res [][]byte // The resultant [][]byte holding the segments.
|
|
|
|
start int // The start index of the current segment.
|
2019-06-16 22:00:52 +03:00
|
|
|
)
|
|
|
|
|
2019-06-17 10:04:22 +03:00
|
|
|
// Go through packets.
|
2019-06-16 22:00:52 +03:00
|
|
|
for i := 0; i < len(d); i += PacketSize {
|
|
|
|
copy(pkt[:], d[i:i+PacketSize])
|
|
|
|
if pkt.PID() == PmtPid {
|
|
|
|
_meta, err := ExtractMeta(pkt[:])
|
|
|
|
switch err {
|
2019-06-17 10:04:22 +03:00
|
|
|
// If there's no meta or a problem with meta, we consider this the end
|
|
|
|
// of the segment.
|
2019-06-16 22:00:52 +03:00
|
|
|
case errNoMeta, meta.ErrUnexpectedMetaFormat:
|
|
|
|
if segmenting {
|
|
|
|
res = append(res, d[start:i])
|
|
|
|
segmenting = false
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
case nil: // do nothing.
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:04:22 +03:00
|
|
|
// If we've got the meta of interest in the PMT and we're not segmenting
|
|
|
|
// then start segmenting. If we don't have the meta of interest in the PMT
|
|
|
|
// and we are segmenting then we want to stop and append the segment to result.
|
2019-06-16 22:00:52 +03:00
|
|
|
if _meta[key] == val && !segmenting {
|
|
|
|
start = i
|
|
|
|
segmenting = true
|
|
|
|
} else if _meta[key] != val && segmenting {
|
|
|
|
res = append(res, d[start:i])
|
|
|
|
segmenting = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:04:22 +03:00
|
|
|
// We've reached the end of the entire MTS clip so if we're segmenting we need
|
|
|
|
// to append current segment to res.
|
2019-06-16 22:00:52 +03:00
|
|
|
if segmenting {
|
|
|
|
res = append(res, d[start:len(d)])
|
|
|
|
}
|
2019-05-10 07:48:55 +03:00
|
|
|
|
2019-06-16 22:00:52 +03:00
|
|
|
return res, nil
|
2019-05-10 07:48:55 +03:00
|
|
|
}
|
2019-07-23 17:34:10 +03:00
|
|
|
|
|
|
|
// pid returns the packet identifier for the given packet.
|
|
|
|
func pid(p []byte) uint16 {
|
|
|
|
return uint16(p[1]&0x1f)<<8 | uint16(p[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Programs returns a map of program numbers and corresponding PMT PIDs for a
|
|
|
|
// given MPEG-TS PAT packet.
|
|
|
|
func Programs(p []byte) (map[uint16]uint16, error) {
|
|
|
|
pat, err := gotspsi.NewPAT(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pat.ProgramMap(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Streams returns elementary streams defined in a given MPEG-TS PMT packet.
|
|
|
|
func Streams(p []byte) ([]gotspsi.PmtElementaryStream, error) {
|
2019-07-26 07:46:05 +03:00
|
|
|
payload, err := Payload(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot get packet payload")
|
|
|
|
}
|
|
|
|
pmt, err := gotspsi.NewPMT(payload)
|
2019-07-23 17:34:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pmt.ElementaryStreams(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MediaStreams retrieves the PmtElementaryStreams from the given PSI. This
|
|
|
|
// function currently assumes that PSI contain a PAT followed by a PMT directly
|
|
|
|
// after. We also assume that this MPEG-TS stream contains just one program,
|
|
|
|
// but this program may contain different streams, i.e. a video stream + audio
|
|
|
|
// stream.
|
|
|
|
func MediaStreams(p []byte) ([]gotspsi.PmtElementaryStream, error) {
|
|
|
|
pat := p[:PacketSize]
|
|
|
|
pmt := p[PacketSize : 2*PacketSize]
|
|
|
|
|
|
|
|
if pid(pat) != PatPid {
|
|
|
|
return nil, errors.New("first packet is not a PAT")
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := Programs(pat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get programs from PAT")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m) == 0 {
|
2019-07-24 06:09:14 +03:00
|
|
|
return nil, ErrNoPrograms
|
2019-07-23 17:34:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(m) > 1 {
|
2019-07-24 06:09:14 +03:00
|
|
|
return nil, ErrMultiplePrograms
|
2019-07-23 17:34:10 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 06:09:14 +03:00
|
|
|
if pid(pmt) != pmtPIDs(m)[0] {
|
2019-07-23 17:34:10 +03:00
|
|
|
return nil, errors.New("second packet is not desired PMT")
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := Streams(pmt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get streams from PMT")
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
2019-07-24 06:09:14 +03:00
|
|
|
|
|
|
|
// pmtPIDs returns PMT PIDS from a map containing program number as keys and
|
|
|
|
// corresponding PMT PIDs as values.
|
|
|
|
func pmtPIDs(m map[uint16]uint16) []uint16 {
|
|
|
|
r := make([]uint16, 0, len(m))
|
|
|
|
for _, v := range m {
|
|
|
|
r = append(r, v)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
2019-07-26 07:46:05 +03:00
|
|
|
|
|
|
|
// Errors used by Payload.
|
|
|
|
var ErrNoPayload = errors.New("no payload")
|
|
|
|
|
|
|
|
// Payload returns the payload of an MPEG-TS packet p.
|
|
|
|
// NB: this is not a copy of the payload in the interest of performance.
|
|
|
|
// TODO: offer function that will do copy if we have interests in safety.
|
|
|
|
func Payload(p []byte) ([]byte, error) {
|
|
|
|
c := byte((p[3] & 0x30) >> 4)
|
|
|
|
if c == 2 {
|
|
|
|
return nil, ErrNoPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there is an adaptation field.
|
|
|
|
off := 4
|
|
|
|
if p[3]&0x20 == 1 {
|
|
|
|
off = int(5 + p[4])
|
|
|
|
}
|
|
|
|
return p[off:], nil
|
|
|
|
}
|