MCPcopy Create free account
hub / github.com/Monibuca/engine / WriteRTPFrame

Method WriteRTPFrame

track/h264.go:113–197  ·  view source on GitHub ↗
(rtpItem *LIRTP)

Source from the content-addressed store, hash-verified

111}
112
113func (vt *H264) WriteRTPFrame(rtpItem *LIRTP) {
114 defer func() {
115 err := recover()
116 if err != nil {
117 vt.Error("WriteRTPFrame panic", zap.Any("err", err))
118 vt.Publisher.Stop(zap.Any("err", err))
119 }
120 }()
121 if vt.lastSeq != vt.lastSeq2+1 && vt.lastSeq2 != 0 {
122 vt.lostFlag = true
123 vt.Warn("lost rtp packet", zap.Uint16("lastSeq", vt.lastSeq), zap.Uint16("lastSeq2", vt.lastSeq2))
124 }
125 frame := &rtpItem.Value
126 pts := frame.Timestamp
127 rv := vt.Value
128 // 有些流的 rtp 包中没有设置 marker 导致无法判断是否是最后一个包,此时通过时间戳变化判断,先 flush 之前的帧
129 if rv.PTS != time.Duration(pts) {
130 if rv.AUList.ByteLength > 0 {
131 if !vt.dcChanged && rv.IFrame {
132 vt.insertDCRtp()
133 }
134 vt.Flush()
135 rv = vt.Value
136 }
137 vt.generateTimestamp(pts)
138 }
139 rv.RTP.Push(rtpItem)
140 if naluType := frame.H264Type(); naluType < 24 {
141 vt.WriteSliceBytes(frame.Payload)
142 } else {
143 offset := naluType.Offset()
144 switch naluType {
145 case codec.NALU_STAPA, codec.NALU_STAPB:
146 if len(frame.Payload) <= offset {
147 vt.Error("invalid nalu size", zap.Int("naluType", int(naluType)))
148 return
149 }
150 for buffer := util.Buffer(frame.Payload[offset:]); buffer.CanRead(); {
151 nextSize := int(buffer.ReadUint16())
152 if buffer.Len() >= nextSize {
153 vt.WriteSliceBytes(buffer.ReadN(nextSize))
154 } else {
155 vt.Error("invalid nalu size", zap.Int("naluType", int(naluType)))
156 return
157 }
158 }
159 case codec.NALU_FUA, codec.NALU_FUB:
160 b1 := frame.Payload[1]
161 if util.Bit1(b1, 0) {
162 naluType = naluType.Parse(b1)
163 firstByte := naluType.Or(frame.Payload[0] & 0x60)
164 switch naluType {
165 case codec.NALU_SPS, codec.NALU_PPS:
166 vt.buf.WriteByte(firstByte)
167 default:
168 vt.WriteSliceByte(firstByte)
169 }
170 }

Callers

nothing calls this directly

Calls 15

WriteSliceBytesMethod · 0.95
BufferTypeAlias · 0.92
Bit1Function · 0.92
insertDCRtpMethod · 0.80
H264TypeMethod · 0.80
OffsetMethod · 0.80
OrMethod · 0.80
WriteSliceByteMethod · 0.80
GetShellMethod · 0.80
ErrorMethod · 0.65
StopMethod · 0.65
WarnMethod · 0.65

Tested by

no test coverage detected