diff --git a/codec/mpegts/mpegts.go b/codec/mpegts/mpegts.go index 5b1f973..ea60372 100644 --- a/codec/mpegts/mpegts.go +++ b/codec/mpegts/mpegts.go @@ -219,14 +219,15 @@ func ReadTsHeader(r io.Reader) (header MpegTsHeader, err error) { // header.payloadUnitStartIndicator = uint8(h & 0x400000) // | 1111 1111 | 0000 0000 | 0000 0000 | 0000 0000 | - if (h&0xff000000)>>24 != 0x47 { - err = errors.New("mpegts header sync error!") - return - } // | 1111 1111 | 0000 0000 | 0000 0000 | 0000 0000 | header.SyncByte = byte((h & 0xff000000) >> 24) + if header.SyncByte != 0x47 { + err = errors.New("mpegts header sync error!") + return + } + // | 0000 0000 | 1000 0000 | 0000 0000 | 0000 0000 | header.TransportErrorIndicator = byte((h & 0x800000) >> 23) @@ -542,9 +543,6 @@ func (s *MpegTsStream) Feed(ts io.Reader) (err error) { if tsHeader, err = ReadTsHeader(&lr); err != nil { return } - if tsHeader.SyncByte != 0x47 { - return errors.New("sync byte error") - } if tsHeader.Pid == PID_PAT { if s.PAT, err = ReadPAT(&lr); err != nil { return diff --git a/track/aac.go b/track/aac.go index 993c6cf..da73892 100644 --- a/track/aac.go +++ b/track/aac.go @@ -24,8 +24,7 @@ func NewAAC(stream IStream, stuff ...any) (aac *AAC) { aac.CodecID = codec.CodecID_AAC aac.Channels = 2 aac.SampleSize = 16 - aac.SetStuff("aac", stream, byte(97), aac) - aac.SetStuff(stuff...) + aac.SetStuff("aac", byte(97), aac, stuff, stream) if aac.BytesPool == nil { aac.BytesPool = make(util.BytesPool, 17) } diff --git a/track/base.go b/track/base.go index 793c34b..d13d3dc 100644 --- a/track/base.go +++ b/track/base.go @@ -164,6 +164,8 @@ func (av *Media) SetStuff(stuff ...any) { av.BytesPool = v case SpesificTrack: av.SpesificTrack = v + case []any: + av.SetStuff(v...) default: av.Base.SetStuff(v) } diff --git a/track/g711.go b/track/g711.go index dfd5167..c3e33df 100644 --- a/track/g711.go +++ b/track/g711.go @@ -28,8 +28,7 @@ func NewG711(stream IStream, alaw bool, stuff ...any) (g711 *G711) { g711.SampleSize = 8 g711.Channels = 1 g711.AVCCHead = []byte{(byte(g711.CodecID) << 4) | (1 << 1)} - g711.SetStuff(stream, uint32(8000), g711) - g711.SetStuff(stuff...) + g711.SetStuff(uint32(8000), g711, stuff, stream) if g711.BytesPool == nil { g711.BytesPool = make(util.BytesPool, 17) } @@ -78,4 +77,4 @@ func (g711 *G711) CompleteRTP(value *AVFrame) { } else { g711.Audio.CompleteRTP(value) } -} \ No newline at end of file +} diff --git a/track/h264.go b/track/h264.go index 1e73fd7..d71c60d 100644 --- a/track/h264.go +++ b/track/h264.go @@ -19,8 +19,7 @@ type H264 struct { func NewH264(stream IStream, stuff ...any) (vt *H264) { vt = &H264{} vt.Video.CodecID = codec.CodecID_H264 - vt.SetStuff("h264", byte(96), uint32(90000), stream, vt) - vt.SetStuff(stuff...) + vt.SetStuff("h264", byte(96), uint32(90000), vt, stuff, stream) if vt.BytesPool == nil { vt.BytesPool = make(util.BytesPool, 17) } @@ -35,10 +34,6 @@ func (vt *H264) WriteSliceBytes(slice []byte) { if log.Trace { vt.Trace("naluType", zap.Uint8("naluType", naluType.Byte())) } - if vt.Value.IFrame { - vt.AppendAuBytes(slice) - return - } switch naluType { case codec.NALU_SPS: spsInfo, _ := codec.ParseSPS(slice) @@ -69,12 +64,18 @@ func (vt *H264) WriteSliceBytes(slice []byte) { b.Write(vt.Video.PPS) vt.WriteSequenceHead(b) case codec.NALU_IDR_Picture: + if vt.Value.AUList.ByteLength > 0 && !vt.Value.IFrame { + vt.Flush() + } vt.Value.IFrame = true vt.AppendAuBytes(slice) case codec.NALU_Non_IDR_Picture, codec.NALU_Data_Partition_A, codec.NALU_Data_Partition_B, codec.NALU_Data_Partition_C: + if vt.Value.AUList.ByteLength > 0 { + vt.Flush() + } vt.Value.IFrame = false vt.AppendAuBytes(slice) case codec.NALU_SEI: @@ -82,6 +83,10 @@ func (vt *H264) WriteSliceBytes(slice []byte) { case codec.NALU_Access_Unit_Delimiter: case codec.NALU_Filler_Data: default: + if vt.Value.IFrame { + vt.AppendAuBytes(slice) + return + } vt.Error("nalu type not support", zap.Int("type", int(naluType))) } } diff --git a/track/h265.go b/track/h265.go index 1d4c75b..1fc2a8d 100644 --- a/track/h265.go +++ b/track/h265.go @@ -20,8 +20,7 @@ type H265 struct { func NewH265(stream IStream, stuff ...any) (vt *H265) { vt = &H265{} vt.Video.CodecID = codec.CodecID_H265 - vt.SetStuff("h265", byte(96), uint32(90000), stream, vt) - vt.SetStuff(stuff...) + vt.SetStuff("h265", byte(96), uint32(90000), vt, stuff, stream) if vt.BytesPool == nil { vt.BytesPool = make(util.BytesPool, 17) } @@ -67,13 +66,20 @@ func (vt *H265) WriteSliceBytes(slice []byte) { codec.NAL_UNIT_CODED_SLICE_IDR, codec.NAL_UNIT_CODED_SLICE_IDR_N_LP, codec.NAL_UNIT_CODED_SLICE_CRA: + if vt.Value.AUList.ByteLength > 0 && !vt.Value.IFrame { + vt.Flush() + } vt.Value.IFrame = true vt.AppendAuBytes(slice) case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9: + if vt.Value.AUList.ByteLength > 0 { + vt.Flush() + } vt.Value.IFrame = false vt.AppendAuBytes(slice) case codec.NAL_UNIT_SEI, codec.NAL_UNIT_SEI_SUFFIX: vt.AppendAuBytes(slice) + case codec.NAL_UNIT_ACCESS_UNIT_DELIMITER: default: vt.Warn("nalu type not supported", zap.Uint("type", uint(t))) }