Skip to content

Commit 1a947aa

Browse files
committed
暂时解决nalu分帧问题
1 parent cb18376 commit 1a947aa

1 file changed

Lines changed: 24 additions & 34 deletions

File tree

nalu.go

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package engine
22

33
import (
4-
"bytes"
54
"encoding/binary"
5+
66
. "github.com/Monibuca/engine/v2/avformat"
7-
"github.com/Monibuca/engine/v2/pool"
8-
"github.com/Monibuca/engine/v2/util"
97
)
108

119
const (
@@ -15,22 +13,35 @@ const (
1513

1614
naluTypeBitmask = 0x1F
1715
naluRefIdcBitmask = 0x60
18-
fuaStartBitmask = 0x80
19-
fuaEndBitmask = 0x40
16+
fuaStartBitmask = 0x80 //1000 0000
17+
fuaEndBitmask = 0x40 //0100 0000
2018
)
2119

2220
type NALU struct {
2321
Publisher
2422
fuBuffer []byte //用于fua的解析暂存的缓存
23+
lastTs uint32
24+
buffer []byte //用于存储分帧的Nalu数据
2525
}
2626

27+
func (r *NALU) writePicture(ts uint32, head, payload []byte) {
28+
if r.VideoTag == nil {
29+
return
30+
}
31+
if payload[1]&fuaStartBitmask != 0 {
32+
if len(r.buffer) > 0 {
33+
r.PushVideo(r.lastTs, r.buffer)
34+
r.buffer = nil
35+
}
36+
r.buffer = append(r.buffer, head...)
37+
r.lastTs = ts
38+
}
39+
nl := len(payload)
40+
r.buffer = append(r.buffer, byte(nl>>24), byte(nl>>16), byte(nl>>8), byte(nl))
41+
r.buffer = append(r.buffer, payload...)
42+
}
2743
func (r *NALU) WriteNALU(ts uint32, payload []byte) {
2844
nalType := payload[0] & naluTypeBitmask
29-
buffer := r.AVRing.Buffer
30-
if buffer == nil {
31-
buffer = bytes.NewBuffer([]byte{})
32-
r.AVRing.Buffer = buffer
33-
}
3445
switch nalType {
3546
case NALU_STAPA:
3647
for currOffset, naluSize := stapaHeaderSize, 0; currOffset < len(payload); currOffset += naluSize {
@@ -63,32 +74,11 @@ func (r *NALU) WriteNALU(ts uint32, payload []byte) {
6374
case NALU_PPS:
6475
r.WritePPS(payload)
6576
case NALU_Access_Unit_Delimiter:
66-
r.PushVideo(ts, r.AVRing.Bytes())
67-
r.GetBuffer()
77+
6878
case NALU_IDR_Picture:
69-
if r.VideoTag == nil {
70-
break
71-
}
72-
if buffer.Len() == 0 {
73-
buffer.Write(RTMP_KEYFRAME_HEAD)
74-
}
75-
nl := pool.GetSlice(4)
76-
util.BigEndian.PutUint32(nl, uint32(len(payload)))
77-
buffer.Write(nl)
78-
pool.RecycleSlice(nl)
79-
buffer.Write(payload)
79+
r.writePicture(ts, RTMP_KEYFRAME_HEAD, payload)
8080
case NALU_Non_IDR_Picture:
81-
if r.VideoTag == nil {
82-
break
83-
}
84-
if buffer.Len() == 0 {
85-
buffer.Write(RTMP_NORMALFRAME_HEAD)
86-
}
87-
nl := pool.GetSlice(4)
88-
util.BigEndian.PutUint32(nl, uint32(len(payload)))
89-
buffer.Write(nl)
90-
pool.RecycleSlice(nl)
91-
buffer.Write(payload)
81+
r.writePicture(ts, RTMP_NORMALFRAME_HEAD, payload)
9282
default:
9383
Printf("nalType not support yet:%d", nalType)
9484
}

0 commit comments

Comments
 (0)