forked from yutopp/go-rtmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk_header.go
238 lines (194 loc) · 5.56 KB
/
chunk_header.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
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// Copyright (c) 2018- yutopp (yutopp@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
//
package rtmp
import (
"encoding/binary"
"fmt"
"io"
)
type chunkBasicHeader struct {
fmt byte
chunkStreamID int /* [0, 65599] */
}
func decodeChunkBasicHeader(r io.Reader, buf []byte, bh *chunkBasicHeader) error {
if buf == nil || len(buf) < 3 {
buf = make([]byte, 3)
}
if _, err := io.ReadAtLeast(r, buf[:1], 1); err != nil {
return err
}
fmtTy := (buf[0] & 0xc0) >> 6 // 0b11000000 >> 6
csID := int(buf[0] & 0x3f) // 0b00111111
switch csID {
case 0:
if _, err := io.ReadAtLeast(r, buf[1:2], 1); err != nil {
return err
}
csID = int(buf[1]) + 64
case 1:
if _, err := io.ReadAtLeast(r, buf[1:], 2); err != nil {
return err
}
csID = int(buf[2])*256 + int(buf[1]) + 64
}
bh.fmt = fmtTy
bh.chunkStreamID = csID
return nil
}
func encodeChunkBasicHeader(w io.Writer, mh *chunkBasicHeader) error {
buf := make([]byte, 3)
buf[0] = byte(mh.fmt&0x03) << 6 // 0b00000011 << 6
switch {
case mh.chunkStreamID >= 2 && mh.chunkStreamID <= 63:
buf[0] |= byte(mh.chunkStreamID & 0x3f) // 0x00111111
_, err := w.Write(buf[:1]) // TODO: should check length?
return err
case mh.chunkStreamID >= 64 && mh.chunkStreamID <= 319:
buf[0] |= byte(0 & 0x3f) // 0x00111111
buf[1] = byte(mh.chunkStreamID - 64)
_, err := w.Write(buf[:2]) // TODO: should check length?
return err
case mh.chunkStreamID >= 320 && mh.chunkStreamID <= 65599:
buf[0] |= byte(1 & 0x3f) // 0x00111111
buf[1] = byte(int(mh.chunkStreamID-64) % 256)
buf[2] = byte(int(mh.chunkStreamID-64) / 256)
_, err := w.Write(buf) // TODO: should check length?
return err
default:
return fmt.Errorf("Chunk stream id is out of range: %d must be in range [2, 65599]", mh.chunkStreamID)
}
}
type chunkMessageHeader struct {
timestamp uint32 // fmt = 0
timestampDelta uint32 // fmt = 1 | 2
messageLength uint32 // fmt = 0 | 1
messageTypeID byte // fmt = 0 | 1
messageStreamID uint32 // fmt = 0
}
func decodeChunkMessageHeader(r io.Reader, fmt byte, buf []byte, mh *chunkMessageHeader) error {
if buf == nil || len(buf) < 11 {
buf = make([]byte, 11)
}
cache32bits := make([]byte, 4)
switch fmt {
case 0:
if _, err := io.ReadAtLeast(r, buf[:11], 11); err != nil {
return err
}
copy(cache32bits[1:], buf[0:3]) // 24bits BE
mh.timestamp = binary.BigEndian.Uint32(cache32bits)
copy(cache32bits[1:], buf[3:6]) // 24bits BE
mh.messageLength = binary.BigEndian.Uint32(cache32bits)
mh.messageTypeID = buf[6] // 8bits
mh.messageStreamID = binary.LittleEndian.Uint32(buf[7:11]) // 32bits
if mh.timestamp == 0xffffff {
_, err := io.ReadAtLeast(r, cache32bits, 4)
if err != nil {
return err
}
mh.timestamp = binary.BigEndian.Uint32(cache32bits)
}
case 1:
if _, err := io.ReadAtLeast(r, buf[:7], 7); err != nil {
return err
}
copy(cache32bits[1:], buf[0:3]) // 24bits BE
mh.timestampDelta = binary.BigEndian.Uint32(cache32bits)
copy(cache32bits[1:], buf[3:6]) // 24bits BE
mh.messageLength = binary.BigEndian.Uint32(cache32bits)
mh.messageTypeID = buf[6] // 8bits
if mh.timestampDelta == 0xffffff {
_, err := io.ReadAtLeast(r, cache32bits, 4)
if err != nil {
return err
}
mh.timestampDelta = binary.BigEndian.Uint32(cache32bits)
}
case 2:
if _, err := io.ReadAtLeast(r, buf[:3], 3); err != nil {
return err
}
copy(cache32bits[1:], buf[0:3]) // 24bits BE
mh.timestampDelta = binary.BigEndian.Uint32(cache32bits)
if mh.timestampDelta == 0xffffff {
_, err := io.ReadAtLeast(r, cache32bits, 4)
if err != nil {
return err
}
mh.timestampDelta = binary.BigEndian.Uint32(cache32bits)
}
case 3:
// DO NOTHING
default:
panic("Unexpected fmt")
}
return nil
}
func encodeChunkMessageHeader(w io.Writer, fmt byte, mh *chunkMessageHeader) error {
buf := make([]byte, 11+4)
cache32bits := make([]byte, 4)
ext := false
switch fmt {
case 0:
buflen := 11
ts := mh.timestamp
if ts >= 0xffffff {
ts = 0xffffff
ext = true
buflen += 4
}
binary.BigEndian.PutUint32(cache32bits, ts)
copy(buf[0:3], cache32bits[1:]) // 24 bits BE
binary.BigEndian.PutUint32(cache32bits, mh.messageLength)
copy(buf[3:6], cache32bits[1:]) // 24 bits BE
buf[6] = mh.messageTypeID // 8bits
binary.LittleEndian.PutUint32(buf[7:11], mh.messageStreamID)
if ext {
binary.BigEndian.PutUint32(buf[11:], mh.timestamp)
}
_, err := w.Write(buf[:buflen]) // TODO: should check length?
return err
case 1:
buflen := 7
td := mh.timestampDelta
if td >= 0xffffff {
td = 0xffffff
ext = true
buflen += 4
}
binary.BigEndian.PutUint32(cache32bits, td)
copy(buf[0:3], cache32bits[1:]) // 24bits BE
binary.BigEndian.PutUint32(cache32bits, mh.messageLength)
copy(buf[3:6], cache32bits[1:]) // 24bits BE
buf[6] = mh.messageTypeID // 8bits
if ext {
binary.BigEndian.PutUint32(buf[7:], mh.timestampDelta)
}
_, err := w.Write(buf[:buflen]) // TODO: should check length?
return err
case 2:
buflen := 3
td := mh.timestampDelta
if td >= 0xffffff {
td = 0xffffff
ext = true
buflen += 4
}
binary.BigEndian.PutUint32(cache32bits, td)
copy(buf[0:3], cache32bits[1:]) // 24bits BE
if ext {
binary.BigEndian.PutUint32(buf[3:], mh.timestampDelta)
}
_, err := w.Write(buf[:buflen]) // TODO: should check length?
return err
case 3:
// DO NOTHING
return nil
default:
panic("Unexpected fmt")
}
}