2019-06-11 18:47:14 +03:00
|
|
|
package mts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-06-13 18:39:55 +03:00
|
|
|
"sort"
|
2019-06-11 18:47:14 +03:00
|
|
|
|
|
|
|
"github.com/Comcast/gots/packet"
|
|
|
|
"github.com/Comcast/gots/pes"
|
|
|
|
)
|
|
|
|
|
2019-06-14 11:11:34 +03:00
|
|
|
// Extract extracts the media, PTS, stream ID and meta for an MPEG-TS clip given
|
|
|
|
// by p, and returns as a Clip. The MPEG-TS must contain only complete packets.
|
2019-06-13 10:00:39 +03:00
|
|
|
func Extract(p []byte) (*Clip, error) {
|
2019-06-11 18:47:14 +03:00
|
|
|
l := len(p)
|
|
|
|
// Check that clip is divisible by 188, i.e. contains a series of full MPEG-TS clips.
|
|
|
|
if l%PacketSize != 0 {
|
|
|
|
return nil, errors.New("MTS clip is not of valid size")
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:22:25 +03:00
|
|
|
var (
|
2019-06-13 10:00:39 +03:00
|
|
|
frameStart int // Index used to indicate the start of current frame in backing slice.
|
|
|
|
clip = &Clip{} // The data that will be returned.
|
2019-06-13 09:22:25 +03:00
|
|
|
meta map[string]string // Holds the most recently extracted meta.
|
|
|
|
lenOfFrame int // Len of current frame.
|
|
|
|
dataLen int // Len of data from MPEG-TS packet.
|
|
|
|
curPTS uint64 // Holds the current PTS.
|
|
|
|
curStreamID uint8 // Holds current StreamID (shouldn't change)
|
|
|
|
firstPUSI = true // Indicates that we have not yet received a PUSI.
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2019-06-13 10:00:39 +03:00
|
|
|
// This will hold a copy of all the media in the MPEG-TS clip.
|
|
|
|
clip.backing = make([]byte, 0, l/PacketSize)
|
|
|
|
|
2019-06-13 09:22:25 +03:00
|
|
|
// Go through the MPEGT-TS packets.
|
2019-06-11 18:47:14 +03:00
|
|
|
var pkt packet.Packet
|
|
|
|
for i := 0; i < l; i += PacketSize {
|
2019-06-13 09:22:25 +03:00
|
|
|
// We will use comcast/gots Packet type, so copy in.
|
2019-06-11 18:47:14 +03:00
|
|
|
copy(pkt[:], p[i:i+PacketSize])
|
|
|
|
|
|
|
|
switch pkt.PID() {
|
|
|
|
case PatPid: // Do nothing.
|
|
|
|
case PmtPid:
|
|
|
|
meta, err = ExtractMeta(pkt[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-13 09:22:25 +03:00
|
|
|
default: // Must be media.
|
|
|
|
// Get the MPEG-TS payload.
|
2019-06-11 18:47:14 +03:00
|
|
|
payload, err := pkt.Payload()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If PUSI is true then we know it's the start of a new frame, and we have
|
|
|
|
// a PES header in the MTS payload.
|
2019-06-13 09:22:25 +03:00
|
|
|
|
2019-06-11 18:47:14 +03:00
|
|
|
if pkt.PayloadUnitStartIndicator() {
|
|
|
|
_pes, err := pes.NewPESHeader(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:22:25 +03:00
|
|
|
// Extract the PTS and ID, then add a new frame to the clip.
|
|
|
|
curPTS = _pes.PTS()
|
|
|
|
curStreamID = _pes.StreamId()
|
2019-06-13 10:00:39 +03:00
|
|
|
clip.frames = append(clip.frames, Frame{
|
2019-06-13 09:22:25 +03:00
|
|
|
PTS: curPTS,
|
|
|
|
ID: curStreamID,
|
|
|
|
Meta: meta,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Append the data to the underlying buffer and get appended lenghth.
|
2019-06-13 10:00:39 +03:00
|
|
|
clip.backing = append(clip.backing, _pes.Data()...)
|
2019-06-13 09:22:25 +03:00
|
|
|
dataLen = len(_pes.Data())
|
2019-06-11 18:47:14 +03:00
|
|
|
|
2019-06-13 09:22:25 +03:00
|
|
|
// If we haven't hit the first PUSI, then we know we have a full frame
|
|
|
|
// and can add this data to the frame pertaining to the finish frame.
|
|
|
|
if !firstPUSI {
|
2019-06-13 10:00:39 +03:00
|
|
|
clip.frames[len(clip.frames)-2].Media = clip.backing[frameStart:lenOfFrame]
|
2019-06-13 18:39:55 +03:00
|
|
|
clip.frames[len(clip.frames)-2].idx = frameStart
|
2019-06-13 10:00:39 +03:00
|
|
|
frameStart = lenOfFrame
|
2019-06-11 18:47:14 +03:00
|
|
|
}
|
2019-06-13 09:22:25 +03:00
|
|
|
firstPUSI = false
|
2019-06-11 18:47:14 +03:00
|
|
|
} else {
|
2019-06-13 09:22:25 +03:00
|
|
|
// We're not at the start of the frame, so we don't have a PES header.
|
|
|
|
// We can append the MPEG-TS data directly to the underlying buf.
|
|
|
|
dataLen = len(payload)
|
2019-06-13 10:00:39 +03:00
|
|
|
clip.backing = append(clip.backing, payload...)
|
2019-06-11 18:47:14 +03:00
|
|
|
}
|
2019-06-13 09:22:25 +03:00
|
|
|
lenOfFrame += dataLen
|
2019-06-11 18:47:14 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-13 09:22:25 +03:00
|
|
|
// We're finished up with media frames, so give the final Frame it's data.
|
2019-06-13 10:00:39 +03:00
|
|
|
clip.frames[len(clip.frames)-1].Media = clip.backing[frameStart:lenOfFrame]
|
2019-06-13 18:39:55 +03:00
|
|
|
clip.frames[len(clip.frames)-1].idx = frameStart
|
2019-06-13 09:22:25 +03:00
|
|
|
return clip, nil
|
2019-06-11 18:47:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clip represents a clip of media, i.e. a sequence of media frames.
|
2019-06-13 10:00:39 +03:00
|
|
|
type Clip struct {
|
|
|
|
frames []Frame
|
|
|
|
backing []byte
|
|
|
|
}
|
2019-06-11 18:47:14 +03:00
|
|
|
|
|
|
|
// Frame describes a media frame that may be extracted from a PES packet.
|
|
|
|
type Frame struct {
|
|
|
|
Media []byte // Contains the media from the frame.
|
|
|
|
PTS uint64 // PTS from PES packet (this gives time relative from start of stream).
|
2019-06-13 09:22:25 +03:00
|
|
|
ID uint8 // StreamID from the PES packet, identifying media codec.
|
2019-06-11 18:47:14 +03:00
|
|
|
Meta map[string]string // Contains metadata from PMT relevant to this frame.
|
2019-06-13 18:39:55 +03:00
|
|
|
idx int // Index in the backing slice.
|
2019-06-11 18:47:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns the concatentated media bytes from each frame in the Clip c.
|
|
|
|
func (c *Clip) Bytes() []byte {
|
2019-06-13 10:00:39 +03:00
|
|
|
if c.backing == nil {
|
|
|
|
panic("the clip backing array cannot be nil")
|
|
|
|
}
|
|
|
|
return c.backing
|
2019-06-11 18:47:14 +03:00
|
|
|
}
|
2019-06-13 18:39:55 +03:00
|
|
|
|
2019-06-14 07:51:45 +03:00
|
|
|
// Errors used in BytesForPTSInterval.
|
|
|
|
var (
|
2019-06-14 11:11:34 +03:00
|
|
|
errPTSLowerBound = errors.New("PTS 'from' cannot be found")
|
|
|
|
errPTSUpperBound = errors.New("PTS 'to' cannot be found")
|
|
|
|
errPTSRange = errors.New("PTS interval invalid")
|
2019-06-14 07:51:45 +03:00
|
|
|
)
|
|
|
|
|
2019-06-13 18:39:55 +03:00
|
|
|
// BytesForPTSInterval returns the media data between PTS' from and to. If from
|
|
|
|
// sits between two PTS, the Frame posessing lower PTS will be considered the start.
|
|
|
|
// The Frame before the Frame corresponding to to will be considered the final
|
|
|
|
// Frame.
|
|
|
|
func (c *Clip) BytesForPTSInterval(from, to uint64) ([]byte, error) {
|
|
|
|
// First check that the interval makes sense.
|
|
|
|
if from >= to {
|
2019-06-14 11:11:34 +03:00
|
|
|
return nil, errPTSRange
|
2019-06-13 18:39:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use binary search to find 'from'.
|
2019-06-14 07:51:45 +03:00
|
|
|
n := len(c.frames) - 1
|
2019-06-13 18:39:55 +03:00
|
|
|
idx := sort.Search(
|
|
|
|
n,
|
|
|
|
func(i int) bool {
|
2019-06-14 07:51:45 +03:00
|
|
|
if from < c.frames[i+1].PTS {
|
2019-06-13 18:39:55 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if idx == n {
|
2019-06-14 11:11:34 +03:00
|
|
|
return nil, errPTSLowerBound
|
2019-06-13 18:39:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now get the start index for the backing slice from this Frame.
|
|
|
|
start := c.frames[idx].idx
|
|
|
|
|
|
|
|
// Now use binary search again to find 'to'.
|
|
|
|
off := idx + 1
|
|
|
|
n = n - (off)
|
|
|
|
idx = sort.Search(
|
|
|
|
n,
|
|
|
|
func(i int) bool {
|
2019-06-14 07:51:45 +03:00
|
|
|
if to <= c.frames[i+off].PTS {
|
2019-06-13 18:39:55 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if idx == n {
|
2019-06-14 11:11:34 +03:00
|
|
|
return nil, errPTSUpperBound
|
2019-06-13 18:39:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now get the end index for the backing slice from this Frame, and return
|
|
|
|
// segment from backing slice corresponding to start and end.
|
2019-06-14 07:51:45 +03:00
|
|
|
end := c.frames[idx+off-1].idx
|
|
|
|
return c.backing[start : end+len(c.frames[idx+off].Media)], nil
|
2019-06-13 18:39:55 +03:00
|
|
|
}
|
2019-06-14 11:11:34 +03:00
|
|
|
|
|
|
|
// Errors that maybe returned from BytesForMetaInterval.
|
|
|
|
var (
|
|
|
|
errMetaRange = errors.New("invalid meta range")
|
|
|
|
errMetaLowerBound = errors.New("meta 'from' cannot be found")
|
|
|
|
errMetaUpperBound = errors.New("meta 'to' cannot be found")
|
|
|
|
)
|
|
|
|
|
2019-06-14 13:03:40 +03:00
|
|
|
// BytesForMetaInterval will return the media data as a slice between two meta
|
|
|
|
// values 'from' and 'to', of key 'key'. The meta values must not be the same.
|
2019-06-14 11:11:34 +03:00
|
|
|
func (c *Clip) BytesForMetaInterval(key, from, to string) ([]byte, error) {
|
|
|
|
// First check that the interval makes sense.
|
2019-06-14 12:57:19 +03:00
|
|
|
if from == to {
|
2019-06-14 11:11:34 +03:00
|
|
|
return nil, errMetaRange
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:57:19 +03:00
|
|
|
var start, end int
|
2019-06-14 11:11:34 +03:00
|
|
|
|
2019-06-14 12:57:19 +03:00
|
|
|
// Try and find from.
|
|
|
|
for i := 0; i < len(c.frames); i++ {
|
|
|
|
f := c.frames[i]
|
|
|
|
if f.Meta[key] == from {
|
|
|
|
start = f.idx
|
2019-06-14 11:11:34 +03:00
|
|
|
|
2019-06-14 12:57:19 +03:00
|
|
|
// Now try and find to.
|
|
|
|
for ; i < len(c.frames); i++ {
|
|
|
|
f = c.frames[i]
|
|
|
|
if f.Meta[key] == to {
|
|
|
|
end = f.idx
|
|
|
|
return c.backing[start : end+len(f.Media)], nil
|
|
|
|
}
|
2019-06-14 11:11:34 +03:00
|
|
|
}
|
2019-06-14 12:57:19 +03:00
|
|
|
return nil, errMetaUpperBound
|
|
|
|
}
|
2019-06-14 11:11:34 +03:00
|
|
|
}
|
2019-06-14 12:57:19 +03:00
|
|
|
return nil, errMetaLowerBound
|
2019-06-14 11:11:34 +03:00
|
|
|
}
|