mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 15:16:51 +08:00
cleanup code
This commit is contained in:
@@ -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
|
||||||
|
@@ -20,13 +20,12 @@ type mpegtsEncoder struct {
|
|||||||
sps []byte
|
sps []byte
|
||||||
pps []byte
|
pps []byte
|
||||||
|
|
||||||
f *os.File
|
f *os.File
|
||||||
b *bufio.Writer
|
b *bufio.Writer
|
||||||
mux *astits.Muxer
|
mux *astits.Muxer
|
||||||
dtsExtractor *h264.DTSExtractor
|
dtsExtractor *h264.DTSExtractor
|
||||||
firstPacketWritten bool
|
firstIDRReceived bool
|
||||||
startPTS time.Duration
|
startPTS time.Duration
|
||||||
firstIDRReceived bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newMPEGTSEncoder allocates a mpegtsEncoder.
|
// newMPEGTSEncoder allocates a mpegtsEncoder.
|
||||||
@@ -61,17 +60,14 @@ 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
|
||||||
return nil
|
if !idrPresent {
|
||||||
}
|
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 {
|
||||||
|
@@ -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
|
||||||
|
Reference in New Issue
Block a user