fix: track named

This commit is contained in:
langhuihui
2023-09-26 12:31:18 +08:00
parent fca43417bf
commit 8305fe5765
6 changed files with 29 additions and 20 deletions

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}
}
}

View File

@@ -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)))
}
}

View File

@@ -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)))
}