/* NAME encoder.go DESCRIPTION See Readme.md AUTHOR Dan Kortschak Saxon Nelson-Milton LICENSE encoder.go is Copyright (C) 2017-2018 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 mts import ( "io" "time" "bitbucket.org/ausocean/av/stream/mts/meta" "bitbucket.org/ausocean/av/stream/mts/pes" "bitbucket.org/ausocean/av/stream/mts/psi" ) // Some common manifestations of PSI var ( // standardPat is a minimal PAT. standardPat = psi.PSI{ Pf: 0x00, Tid: 0x00, Ssi: true, Pb: false, Sl: 0x0d, Tss: &psi.TSS{ Tide: 0x01, V: 0, Cni: true, Sn: 0, Lsn: 0, Sd: &psi.PAT{ Pn: 0x01, Pmpid: 0x1000, }, }, } // standardPmt is a minimal PMT, without descriptors for time and location. standardPmt = psi.PSI{ Pf: 0x00, Tid: 0x02, Ssi: true, Sl: 0x12, Tss: &psi.TSS{ Tide: 0x01, V: 0, Cni: true, Sn: 0, Lsn: 0, Sd: &psi.PMT{ Pcrpid: 0x0100, Pil: 0, Essd: &psi.ESSD{ St: 0x1b, Epid: 0x0100, Esil: 0x00, }, }, }, } ) const ( psiInterval = 1 * time.Second psiSendCount = 7 ) // Meta allows addition of metadata to encoded mts from outside of this pkg. // See meta pkg for usage. // // TODO: make this not global. var Meta *meta.Data var ( patTable = standardPat.Bytes() pmtTable = standardPmt.Bytes() ) const ( 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. ptsOffset = 700 * time.Millisecond // pcrFreq is the base Program Clock Reference frequency. pcrFreq = 90000 // Hz ) // Encoder encapsulates properties of an mpegts generator. type Encoder struct { dst io.Writer clock time.Duration lastTime time.Time frameInterval time.Duration ptsOffset time.Duration tsSpace [PacketSize]byte pesSpace [pes.MaxPesSize]byte continuity map[int]byte timeBasedPsi bool pktCount int psiSendCount int psiLastTime time.Time } // NewEncoder returns an Encoder with the specified frame rate. func NewEncoder(dst io.Writer, fps float64) *Encoder { return &Encoder{ dst: dst, frameInterval: time.Duration(float64(time.Second) / fps), ptsOffset: ptsOffset, timeBasedPsi: true, pktCount: 8, continuity: map[int]byte{ patPid: 0, pmtPid: 0, videoPid: 0, }, } } const ( hasPayload = 0x1 hasAdaptationField = 0x2 ) const ( hasDTS = 0x1 hasPTS = 0x2 ) // TimeBasedPsi allows for the setting of the PSI writing method, therefore, if // PSI is written based on some time duration, or based on a packet count. // If b is true, then time based PSI is used, otherwise the PSI is written // every sendCount. func (e *Encoder) TimeBasedPsi(b bool, sendCount int) { e.timeBasedPsi = b e.psiSendCount = sendCount e.pktCount = e.psiSendCount } // Write implements io.Writer. Write takes raw h264 and encodes into mpegts, // then sending it to the encoder's io.Writer destination. func (e *Encoder) Write(nalu []byte) (int, error) { now := time.Now() if (e.timeBasedPsi && (now.Sub(e.psiLastTime) > psiInterval)) || (!e.timeBasedPsi && (e.pktCount >= e.psiSendCount)) { e.pktCount = 0 err := e.writePSI() if err != nil { return 0, err } e.psiLastTime = now } // Prepare PES data. pesPkt := pes.Packet{ StreamID: streamID, PDI: hasPTS, PTS: e.pts(), Data: nalu, HeaderLength: 5, } buf := pesPkt.Bytes(e.pesSpace[:pes.MaxPesSize]) pusi := true for len(buf) != 0 { pkt := Packet{ PUSI: pusi, PID: videoPid, RAI: pusi, CC: e.ccFor(videoPid), AFC: hasAdaptationField | hasPayload, PCRF: pusi, } n := pkt.FillPayload(buf) buf = buf[n:] if pusi { // If the packet has a Payload Unit Start Indicator // flag set then we need to write a PCR. pkt.PCR = e.pcr() pusi = false } n, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize])) if err != nil { return n, err } e.pktCount++ } e.tick() return len(nalu), nil } // writePSI creates mpegts with pat and pmt tables - with pmt table having updated // location and time data. func (e *Encoder) writePSI() error { // Write PAT. patPkt := Packet{ PUSI: true, PID: PatPid, CC: e.ccFor(PatPid), AFC: HasPayload, Payload: psi.AddPadding(patTable), } _, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize])) if err != nil { return err } e.pktCount++ pmtTable, err = updateMeta(pmtTable) if err != nil { return err } // Create mts packet from pmt table. pmtPkt := Packet{ PUSI: true, PID: PmtPid, CC: e.ccFor(PmtPid), AFC: HasPayload, Payload: psi.AddPadding(pmtTable), } _, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize])) if err != nil { return err } e.pktCount++ return nil } // tick advances the clock one frame interval. func (e *Encoder) tick() { e.clock += e.frameInterval } // pts retuns the current presentation timestamp. func (e *Encoder) pts() uint64 { return uint64((e.clock + e.ptsOffset).Seconds() * pcrFreq) } // pcr returns the current program clock reference. func (e *Encoder) pcr() uint64 { return uint64(e.clock.Seconds() * pcrFreq) } // ccFor returns the next continuity counter for pid. func (e *Encoder) ccFor(pid int) byte { cc := e.continuity[pid] const continuityCounterMask = 0xf e.continuity[pid] = (cc + 1) & continuityCounterMask return cc } // updateMeta adds/updates a metaData descriptor in the given psi bytes using data // contained in the global Meta struct. func updateMeta(b []byte) ([]byte, error) { p := psi.PSIBytes(b) err := p.AddDescriptor(psi.MetadataTag, Meta.Encode()) return []byte(p), err }