/* NAME mpegts_generator.go DESCRIPTION See Readme.md AUTHOR Dan Kortschak Saxon Nelson-Milton LICENSE mpegts_generator.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean) 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 along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. */ package generator import ( "time" "bitbucket.org/ausocean/av/mpegts" "bitbucket.org/ausocean/av/pes" ) const psiPacketSize = 184 // TODO: really need to finish the at and pmt stuff - this is too hacky var ( 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} ) func init() { for len(patTable) < psiPacketSize { patTable = append(patTable, 0xff) } for len(pmtTable) < psiPacketSize { pmtTable = append(pmtTable, 0xff) } } const ( sdtPid = 17 patPid = 0 pmtPid = 4096 videoPid = 256 streamID = 0xe0 ptsOffset = 700 * time.Millisecond ptsFreq = 90000 // Hz maxCC = 0xf ) // tsGenerator encapsulates properties of an mpegts generator. type tsGenerator struct { outputChan chan []byte nalInputChan chan []byte clock time.Duration frameInterval time.Duration ptsOffset time.Duration continuity map[int]byte } // NewTsGenerator returns an instance of the tsGenerator struct func NewTsGenerator(fps float64) (g *tsGenerator) { return &tsGenerator{ outputChan: make(chan []byte, 1), nalInputChan: make(chan []byte, 1), frameInterval: time.Duration(float64(time.Second) / fps), ptsOffset: ptsOffset, continuity: map[int]byte{ patPid: 0, pmtPid: 0, videoPid: 0, }, } } // 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 func (g *tsGenerator) Start() { go g.generate() } 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 } // 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 } // generate handles the incoming data and generates equivalent mpegts packets - // sending them to the output channel func (g *tsGenerator) generate() { for { nalu := <-g.nalInputChan pesPkt := pes.Packet{ StreamID: streamID, PDI: 2, 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), AFC: 3, PCRF: pusi, } n := pkt.FillPayload(buf) buf = buf[n:] // TODO: create consts for AFC parameters if pusi { // Create pat table patPkt := mpegts.Packet{ PUSI: pusi, PID: patPid, CC: g.ccFor(patPid), AFC: 1, Payload: patTable, } g.outputChan <- patPkt.Bytes() // Create pmt table pmtPkt := mpegts.Packet{ PUSI: pusi, PID: pmtPid, CC: g.ccFor(pmtPid), AFC: 1, Payload: pmtTable, } g.outputChan <- pmtPkt.Bytes() // If pusi then we need to gen a pcr pkt.PCR = g.pcr() g.tick() pusi = false } g.outputChan <- pkt.Bytes() } } } // tick advances the clock one frame interval. func (g *tsGenerator) tick() { g.clock += g.frameInterval } // pts retuns the current presentation timestamp. func (g *tsGenerator) pts() uint64 { return uint64((g.clock + g.ptsOffset).Seconds() * ptsFreq) } // pcr returns the current program clock reference. func (g *tsGenerator) pcr() uint64 { return uint64(g.clock.Seconds() * ptsFreq) } // ccFor returns the next continuity counter for pid. func (g *tsGenerator) ccFor(pid int) byte { cc := g.continuity[pid] g.continuity[pid] = (cc + 1) & maxCC return cc }