diff --git a/pkg/h264/annexb/annexb.go b/pkg/h264/annexb/annexb.go index 3d97704a..13f06622 100644 --- a/pkg/h264/annexb/annexb.go +++ b/pkg/h264/annexb/annexb.go @@ -139,3 +139,22 @@ func IndexFrame(b []byte) int { return -1 } + +func FixAnnexBInAVCC(b []byte) []byte { + for i := 0; i < len(b); { + if i+4 >= len(b) { + break + } + + size := bytes.Index(b[i+4:], []byte{0, 0, 0, 1}) + if size < 0 { + size = len(b) - (i + 4) + } + + binary.BigEndian.PutUint32(b[i:], uint32(size)) + + i += size + 4 + } + + return b +} diff --git a/pkg/h264/rtp.go b/pkg/h264/rtp.go index 1864423f..c928f54b 100644 --- a/pkg/h264/rtp.go +++ b/pkg/h264/rtp.go @@ -83,10 +83,10 @@ func RTPDepay(codec *core.Codec, handler core.HandlerFunc) core.HandlerFunc { // some Chinese buggy cameras has single packet with SPS+PPS+IFrame separated by 00 00 00 01 // https://github.com/AlexxIT/WebRTC/issues/391 // https://github.com/AlexxIT/WebRTC/issues/392 - payload = annexb.EncodeToAVCC(payload, false) + payload = annexb.FixAnnexBInAVCC(payload) } - //log.Printf("[AVC] %v, len: %d, ts: %10d, seq: %d", Types(payload), len(payload), packet.Timestamp, packet.SequenceNumber) + //log.Printf("[AVC] %v, len: %d, ts: %10d, seq: %d", NALUTypes(payload), len(payload), packet.Timestamp, packet.SequenceNumber) clone := *packet clone.Version = RTPPacketVersionAVC