forked from bigsillybear/livego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chunk_stream.go
executable file
·225 lines (211 loc) · 5.16 KB
/
chunk_stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package core
import (
"encoding/binary"
"fmt"
"github.com/dudebing99/livego/av"
"github.com/dudebing99/livego/utils/pool"
)
type ChunkStream struct {
Format uint32
CSID uint32
Timestamp uint32
Length uint32
TypeID uint32
StreamID uint32
timeDelta uint32
exted bool
index uint32
remain uint32
got bool
tmpFromat uint32
Data []byte
}
func (chunkStream *ChunkStream) full() bool {
return chunkStream.got
}
func (chunkStream *ChunkStream) new(pool *pool.Pool) {
chunkStream.got = false
chunkStream.index = 0
chunkStream.remain = chunkStream.Length
chunkStream.Data = pool.Get(int(chunkStream.Length))
}
func (chunkStream *ChunkStream) writeHeader(w *ReadWriter) error {
//Chunk Basic Header
h := chunkStream.Format << 6
switch {
case chunkStream.CSID < 64:
h |= chunkStream.CSID
w.WriteUintBE(h, 1)
case chunkStream.CSID-64 < 256:
h |= 0
w.WriteUintBE(h, 1)
w.WriteUintLE(chunkStream.CSID-64, 1)
case chunkStream.CSID-64 < 65536:
h |= 1
w.WriteUintBE(h, 1)
w.WriteUintLE(chunkStream.CSID-64, 2)
}
//Chunk Message Header
ts := chunkStream.Timestamp
if chunkStream.Format == 3 {
goto END
}
if chunkStream.Timestamp > 0xffffff {
ts = 0xffffff
}
w.WriteUintBE(ts, 3)
if chunkStream.Format == 2 {
goto END
}
if chunkStream.Length > 0xffffff {
return fmt.Errorf("length=%d", chunkStream.Length)
}
w.WriteUintBE(chunkStream.Length, 3)
w.WriteUintBE(chunkStream.TypeID, 1)
if chunkStream.Format == 1 {
goto END
}
w.WriteUintLE(chunkStream.StreamID, 4)
END:
//Extended Timestamp
if ts >= 0xffffff {
w.WriteUintBE(chunkStream.Timestamp, 4)
}
return w.WriteError()
}
func (chunkStream *ChunkStream) writeChunk(w *ReadWriter, chunkSize int) error {
if chunkStream.TypeID == av.TAG_AUDIO {
chunkStream.CSID = 4
} else if chunkStream.TypeID == av.TAG_VIDEO ||
chunkStream.TypeID == av.TAG_SCRIPTDATAAMF0 ||
chunkStream.TypeID == av.TAG_SCRIPTDATAAMF3 {
chunkStream.CSID = 6
}
totalLen := uint32(0)
numChunks := (chunkStream.Length / uint32(chunkSize))
for i := uint32(0); i <= numChunks; i++ {
if totalLen == chunkStream.Length {
break
}
if i == 0 {
chunkStream.Format = uint32(0)
} else {
chunkStream.Format = uint32(3)
}
if err := chunkStream.writeHeader(w); err != nil {
return err
}
inc := uint32(chunkSize)
start := uint32(i) * uint32(chunkSize)
if uint32(len(chunkStream.Data))-start <= inc {
inc = uint32(len(chunkStream.Data)) - start
}
totalLen += inc
end := start + inc
buf := chunkStream.Data[start:end]
if _, err := w.Write(buf); err != nil {
return err
}
}
return nil
}
func (chunkStream *ChunkStream) readChunk(r *ReadWriter, chunkSize uint32, pool *pool.Pool) error {
if chunkStream.remain != 0 && chunkStream.tmpFromat != 3 {
return fmt.Errorf("invalid remain = %d", chunkStream.remain)
}
switch chunkStream.CSID {
case 0:
id, _ := r.ReadUintLE(1)
chunkStream.CSID = id + 64
case 1:
id, _ := r.ReadUintLE(2)
chunkStream.CSID = id + 64
}
switch chunkStream.tmpFromat {
case 0:
chunkStream.Format = chunkStream.tmpFromat
chunkStream.Timestamp, _ = r.ReadUintBE(3)
chunkStream.Length, _ = r.ReadUintBE(3)
chunkStream.TypeID, _ = r.ReadUintBE(1)
chunkStream.StreamID, _ = r.ReadUintLE(4)
if chunkStream.Timestamp == 0xffffff {
chunkStream.Timestamp, _ = r.ReadUintBE(4)
chunkStream.exted = true
} else {
chunkStream.exted = false
}
chunkStream.new(pool)
case 1:
chunkStream.Format = chunkStream.tmpFromat
timeStamp, _ := r.ReadUintBE(3)
chunkStream.Length, _ = r.ReadUintBE(3)
chunkStream.TypeID, _ = r.ReadUintBE(1)
if timeStamp == 0xffffff {
timeStamp, _ = r.ReadUintBE(4)
chunkStream.exted = true
} else {
chunkStream.exted = false
}
chunkStream.timeDelta = timeStamp
chunkStream.Timestamp += timeStamp
chunkStream.new(pool)
case 2:
chunkStream.Format = chunkStream.tmpFromat
timeStamp, _ := r.ReadUintBE(3)
if timeStamp == 0xffffff {
timeStamp, _ = r.ReadUintBE(4)
chunkStream.exted = true
} else {
chunkStream.exted = false
}
chunkStream.timeDelta = timeStamp
chunkStream.Timestamp += timeStamp
chunkStream.new(pool)
case 3:
if chunkStream.remain == 0 {
switch chunkStream.Format {
case 0:
if chunkStream.exted {
timestamp, _ := r.ReadUintBE(4)
chunkStream.Timestamp = timestamp
}
case 1, 2:
var timedet uint32
if chunkStream.exted {
timedet, _ = r.ReadUintBE(4)
} else {
timedet = chunkStream.timeDelta
}
chunkStream.Timestamp += timedet
}
chunkStream.new(pool)
} else {
if chunkStream.exted {
b, err := r.Peek(4)
if err != nil {
return err
}
tmpts := binary.BigEndian.Uint32(b)
if tmpts == chunkStream.Timestamp {
r.Discard(4)
}
}
}
default:
return fmt.Errorf("invalid format=%d", chunkStream.Format)
}
size := int(chunkStream.remain)
if size > int(chunkSize) {
size = int(chunkSize)
}
buf := chunkStream.Data[chunkStream.index : chunkStream.index+uint32(size)]
if _, err := r.Read(buf); err != nil {
return err
}
chunkStream.index += uint32(size)
chunkStream.remain -= uint32(size)
if chunkStream.remain == 0 {
chunkStream.got = true
}
return r.readError
}