cleanup code

This commit is contained in:
aler9
2022-06-02 18:04:22 +02:00
parent 9e1595ea90
commit d40205e592
3 changed files with 25 additions and 27 deletions

View File

@@ -1939,8 +1939,6 @@ func (c *Client) processPacketRTP(ct *clientTrack, ctx *ClientOnPacketRTPCtx) {
ctx.PTSEqualsDTS = h264.IDRPresent(nalus) ctx.PTSEqualsDTS = h264.IDRPresent(nalus)
ctx.H264NALUs = nalus ctx.H264NALUs = nalus
ctx.H264PTS = pts ctx.H264PTS = pts
} else {
ctx.PTSEqualsDTS = false
} }
} else { } else {
ctx.PTSEqualsDTS = true ctx.PTSEqualsDTS = true

View File

@@ -24,9 +24,8 @@ type mpegtsEncoder struct {
b *bufio.Writer b *bufio.Writer
mux *astits.Muxer mux *astits.Muxer
dtsExtractor *h264.DTSExtractor dtsExtractor *h264.DTSExtractor
firstPacketWritten bool
startPTS time.Duration
firstIDRReceived bool firstIDRReceived bool
startPTS time.Duration
} }
// newMPEGTSEncoder allocates a mpegtsEncoder. // newMPEGTSEncoder allocates a mpegtsEncoder.
@@ -62,16 +61,13 @@ func (e *mpegtsEncoder) close() {
// encode encodes H264 NALUs into MPEG-TS. // encode encodes H264 NALUs into MPEG-TS.
func (e *mpegtsEncoder) encode(nalus [][]byte,pts time.Duration) error { func (e *mpegtsEncoder) encode(nalus [][]byte,pts time.Duration) error {
if !e.firstPacketWritten {
e.firstPacketWritten = true
e.startPTS = pts
}
// prepend an AUD. This is required by some players // prepend an AUD. This is required by some players
filteredNALUs := [][]byte{ filteredNALUs := [][]byte{
{byte(h264.NALUTypeAccessUnitDelimiter), 240}, {byte(h264.NALUTypeAccessUnitDelimiter), 240},
} }
idrPresent := false
for _, nalu := range nalus { for _, nalu := range nalus {
typ := h264.NALUType(nalu[0] & 0x1F) typ := h264.NALUType(nalu[0] & 0x1F)
switch typ { switch typ {
@@ -87,6 +83,8 @@ func (e *mpegtsEncoder) encode(nalus [][]byte, pts time.Duration) error {
continue continue
case h264.NALUTypeIDR: case h264.NALUTypeIDR:
idrPresent = true
// add SPS and PPS before every IDR // add SPS and PPS before every IDR
if e.sps != nil && e.pps != nil { if e.sps != nil && e.pps != nil {
filteredNALUs = append(filteredNALUs, e.sps, e.pps) filteredNALUs = append(filteredNALUs, e.sps, e.pps)
@@ -100,16 +98,14 @@ func (e *mpegtsEncoder) encode(nalus [][]byte, pts time.Duration) error {
return nil return nil
} }
idrPresent := h264.IDRPresent(nalus) if !e.firstIDRReceived {
if !e.firstIDRReceived && !idrPresent { // skip samples silently until we find one with a IDR
if !idrPresent {
return nil return nil
} }
e.firstIDRReceived = true
// encode into Annex-B e.firstIDRReceived = true
enc, err := h264.AnnexBEncode(filteredNALUs) e.startPTS = pts
if err != nil {
return err
} }
pts -= e.startPTS pts -= e.startPTS
@@ -134,6 +130,12 @@ func (e *mpegtsEncoder) encode(nalus [][]byte, pts time.Duration) error {
oh.PTS = &astits.ClockReference{Base: int64(pts.Seconds() * 90000)} oh.PTS = &astits.ClockReference{Base: int64(pts.Seconds() * 90000)}
} }
// encode into Annex-B
annexb, err := h264.AnnexBEncode(filteredNALUs)
if err != nil {
return err
}
// write TS packet // write TS packet
_, err = e.mux.WriteData(&astits.MuxerData{ _, err = e.mux.WriteData(&astits.MuxerData{
PID: 256, PID: 256,
@@ -145,7 +147,7 @@ func (e *mpegtsEncoder) encode(nalus [][]byte, pts time.Duration) error {
OptionalHeader: oh, OptionalHeader: oh,
StreamID: 224, // video StreamID: 224, // video
}, },
Data: enc, Data: annexb,
}, },
}) })
if err != nil { if err != nil {

View File

@@ -1226,8 +1226,6 @@ func (ss *ServerSession) processPacketRTP(at *ServerSessionAnnouncedTrack, ctx *
ctx.PTSEqualsDTS = h264.IDRPresent(nalus) ctx.PTSEqualsDTS = h264.IDRPresent(nalus)
ctx.H264NALUs = nalus ctx.H264NALUs = nalus
ctx.H264PTS = pts ctx.H264PTS = pts
} else {
ctx.PTSEqualsDTS = false
} }
} else { } else {
ctx.PTSEqualsDTS = true ctx.PTSEqualsDTS = true