2018-02-13 11:39:26 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2018-05-31 12:54:20 +03:00
|
|
|
mpegts_generator.go
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
2018-08-18 04:57:36 +03:00
|
|
|
Dan Kortschak <dan@ausocean.org>
|
2018-02-28 16:46:59 +03:00
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
LICENSE
|
2018-05-31 12:54:20 +03:00
|
|
|
mpegts_generator.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
2018-02-13 11:39:26 +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.
|
|
|
|
|
|
|
|
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
|
2018-08-18 04:57:36 +03:00
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2018-02-13 11:39:26 +03:00
|
|
|
*/
|
2018-08-18 04:57:36 +03:00
|
|
|
|
2018-02-10 09:59:56 +03:00
|
|
|
package generator
|
2017-12-30 11:31:50 +03:00
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
import (
|
2018-08-18 06:51:50 +03:00
|
|
|
"time"
|
|
|
|
|
2018-03-13 08:15:42 +03:00
|
|
|
"bitbucket.org/ausocean/av/mpegts"
|
|
|
|
"bitbucket.org/ausocean/av/pes"
|
2018-05-31 12:54:20 +03:00
|
|
|
)
|
2018-02-12 10:58:29 +03:00
|
|
|
|
2018-08-17 18:30:51 +03:00
|
|
|
const psiPacketSize = 184
|
2018-04-17 08:20:23 +03:00
|
|
|
|
2018-02-28 17:42:00 +03:00
|
|
|
// TODO: really need to finish the at and pmt stuff - this is too hacky
|
2018-01-16 08:49:18 +03:00
|
|
|
var (
|
2018-08-17 18:18:13 +03:00
|
|
|
patTable = []byte{0, 0, 176, 13, 0, 1, 193, 0, 0, 0, 1, 240, 0, 42, 177, 4, 178}
|
|
|
|
pmtTable = []byte{0, 2, 176, 18, 0, 1, 193, 0, 0, 0xE1, 0x00, 0xF0, 0, 0x1B, 0xE1, 0, 0xF0, 0, 0x15, 0xBD, 0x4D, 0x56}
|
2018-01-16 08:49:18 +03:00
|
|
|
)
|
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
func init() {
|
2018-08-17 18:30:51 +03:00
|
|
|
for len(patTable) < psiPacketSize {
|
2018-08-18 04:57:36 +03:00
|
|
|
patTable = append(patTable, 0xff)
|
2018-02-28 17:42:00 +03:00
|
|
|
}
|
2018-08-17 18:30:51 +03:00
|
|
|
for len(pmtTable) < psiPacketSize {
|
2018-08-18 04:57:36 +03:00
|
|
|
pmtTable = append(pmtTable, 0xff)
|
2018-02-28 17:42:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 08:49:18 +03:00
|
|
|
const (
|
2018-08-18 10:49:26 +03:00
|
|
|
sdtPid = 17
|
|
|
|
patPid = 0
|
|
|
|
pmtPid = 4096
|
|
|
|
videoPid = 256
|
|
|
|
streamID = 0xe0 // First video stream ID.
|
|
|
|
)
|
|
|
|
|
|
|
|
// Time related constants.
|
|
|
|
const (
|
|
|
|
// ptsOffset is the offset added to the clock to determine
|
|
|
|
// the current presentation timestamp,
|
2018-08-18 06:51:50 +03:00
|
|
|
ptsOffset = 700 * time.Millisecond
|
2018-08-18 10:49:26 +03:00
|
|
|
|
|
|
|
// pcrFreq is the base Program Clock Reference frequency.
|
|
|
|
pcrFreq = 90000 // Hz
|
2018-01-16 08:49:18 +03:00
|
|
|
)
|
|
|
|
|
2018-02-28 17:42:00 +03:00
|
|
|
// tsGenerator encapsulates properties of an mpegts generator.
|
2018-01-10 04:32:16 +03:00
|
|
|
type tsGenerator struct {
|
2018-08-18 07:06:14 +03:00
|
|
|
outputChan chan []byte
|
|
|
|
nalInputChan chan []byte
|
|
|
|
|
|
|
|
clock time.Duration
|
|
|
|
frameInterval time.Duration
|
|
|
|
ptsOffset time.Duration
|
|
|
|
|
|
|
|
continuity map[int]byte
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 17:42:00 +03:00
|
|
|
// NewTsGenerator returns an instance of the tsGenerator struct
|
2018-08-18 04:57:36 +03:00
|
|
|
func NewTsGenerator(fps float64) (g *tsGenerator) {
|
|
|
|
return &tsGenerator{
|
2018-08-18 07:06:14 +03:00
|
|
|
outputChan: make(chan []byte, 1),
|
|
|
|
nalInputChan: make(chan []byte, 1),
|
|
|
|
|
|
|
|
frameInterval: time.Duration(float64(time.Second) / fps),
|
|
|
|
ptsOffset: ptsOffset,
|
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
continuity: map[int]byte{
|
2018-08-18 05:36:34 +03:00
|
|
|
patPid: 0,
|
2018-08-18 04:57:36 +03:00
|
|
|
pmtPid: 0,
|
|
|
|
videoPid: 0,
|
|
|
|
},
|
|
|
|
}
|
2018-01-08 04:12:26 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 17:42:00 +03:00
|
|
|
// Start is called when we would like generation to begin, i.e. we would like
|
|
|
|
// the generator to start taking input data and creating mpegts packets
|
2018-02-28 16:46:59 +03:00
|
|
|
func (g *tsGenerator) Start() {
|
2018-01-24 07:12:22 +03:00
|
|
|
go g.generate()
|
|
|
|
}
|
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
func (g *tsGenerator) Stop() {}
|
|
|
|
|
|
|
|
// InputChan returns a handle to the nalInputChan (inputChan) so that nal units
|
|
|
|
// can be passed to the generator and processed
|
|
|
|
func (g *tsGenerator) InputChan() chan []byte {
|
|
|
|
return g.nalInputChan
|
2018-05-06 10:32:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
// OutputChan returns a handle to the generator output chan where the mpegts
|
|
|
|
// packets will show up once ready to go
|
|
|
|
func (g *tsGenerator) OutputChan() <-chan []byte {
|
|
|
|
return g.outputChan
|
2018-02-28 17:42:00 +03:00
|
|
|
}
|
|
|
|
|
2018-08-18 10:49:26 +03:00
|
|
|
const (
|
|
|
|
hasPayload = 0x1
|
|
|
|
hasAdaptationField = 0x2
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hasDTS = 0x1
|
|
|
|
hasPTS = 0x2
|
|
|
|
)
|
|
|
|
|
2018-02-28 17:42:00 +03:00
|
|
|
// generate handles the incoming data and generates equivalent mpegts packets -
|
|
|
|
// sending them to the output channel
|
2018-01-24 07:12:22 +03:00
|
|
|
func (g *tsGenerator) generate() {
|
2018-01-17 06:48:47 +03:00
|
|
|
for {
|
2018-08-18 04:57:36 +03:00
|
|
|
nalu := <-g.nalInputChan
|
2018-08-18 10:49:26 +03:00
|
|
|
|
|
|
|
// Write PAT
|
|
|
|
patPkt := mpegts.Packet{
|
|
|
|
PUSI: true,
|
|
|
|
PID: patPid,
|
|
|
|
CC: g.ccFor(patPid),
|
|
|
|
AFC: hasPayload,
|
|
|
|
Payload: patTable,
|
|
|
|
}
|
|
|
|
g.outputChan <- patPkt.Bytes()
|
|
|
|
|
|
|
|
// Write PMT.
|
|
|
|
pmtPkt := mpegts.Packet{
|
|
|
|
PUSI: true,
|
|
|
|
PID: pmtPid,
|
|
|
|
CC: g.ccFor(pmtPid),
|
|
|
|
AFC: hasPayload,
|
|
|
|
Payload: pmtTable,
|
|
|
|
}
|
|
|
|
g.outputChan <- pmtPkt.Bytes()
|
|
|
|
|
|
|
|
// Prepare PES data.
|
2018-08-18 04:57:36 +03:00
|
|
|
pesPkt := pes.Packet{
|
|
|
|
StreamID: streamID,
|
2018-08-18 10:49:26 +03:00
|
|
|
PDI: hasPTS,
|
2018-08-18 04:57:36 +03:00
|
|
|
PTS: g.pts(),
|
|
|
|
Data: nalu,
|
|
|
|
HeaderLength: 5,
|
|
|
|
}
|
|
|
|
buf := pesPkt.Bytes()
|
|
|
|
|
|
|
|
pusi := true
|
|
|
|
for len(buf) != 0 {
|
|
|
|
pkt := mpegts.Packet{
|
|
|
|
PUSI: pusi,
|
|
|
|
PID: videoPid,
|
|
|
|
RAI: pusi,
|
|
|
|
CC: g.ccFor(videoPid),
|
2018-08-18 10:49:26 +03:00
|
|
|
AFC: hasAdaptationField | hasPayload,
|
2018-08-18 04:57:36 +03:00
|
|
|
PCRF: pusi,
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
2018-08-18 04:57:36 +03:00
|
|
|
n := pkt.FillPayload(buf)
|
|
|
|
buf = buf[n:]
|
|
|
|
|
|
|
|
if pusi {
|
2018-08-18 10:49:26 +03:00
|
|
|
// If the packet has a Payload Unit Start Indicator
|
|
|
|
// flag set then we need to write a PCR.
|
2018-08-18 04:57:36 +03:00
|
|
|
pkt.PCR = g.pcr()
|
|
|
|
pusi = false
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
2018-08-18 07:06:14 +03:00
|
|
|
|
2018-08-18 04:57:36 +03:00
|
|
|
g.outputChan <- pkt.Bytes()
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
2018-08-18 10:49:26 +03:00
|
|
|
|
|
|
|
g.tick()
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
2018-08-18 04:57:36 +03:00
|
|
|
|
2018-08-18 07:06:14 +03:00
|
|
|
// tick advances the clock one frame interval.
|
|
|
|
func (g *tsGenerator) tick() {
|
|
|
|
g.clock += g.frameInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
// pts retuns the current presentation timestamp.
|
2018-08-18 04:57:36 +03:00
|
|
|
func (g *tsGenerator) pts() uint64 {
|
2018-08-18 10:49:26 +03:00
|
|
|
return uint64((g.clock + g.ptsOffset).Seconds() * pcrFreq)
|
2018-08-18 04:57:36 +03:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:06:14 +03:00
|
|
|
// pcr returns the current program clock reference.
|
2018-08-18 04:57:36 +03:00
|
|
|
func (g *tsGenerator) pcr() uint64 {
|
2018-08-18 10:49:26 +03:00
|
|
|
return uint64(g.clock.Seconds() * pcrFreq)
|
2018-08-18 04:57:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ccFor returns the next continuity counter for pid.
|
|
|
|
func (g *tsGenerator) ccFor(pid int) byte {
|
|
|
|
cc := g.continuity[pid]
|
2018-08-18 10:49:26 +03:00
|
|
|
const continuityCounterMask = 0xf
|
|
|
|
g.continuity[pid] = (cc + 1) & continuityCounterMask
|
2018-08-18 04:57:36 +03:00
|
|
|
return cc
|
|
|
|
}
|