-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathconvert.go
407 lines (349 loc) · 7.37 KB
/
convert.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package util
import (
"errors"
"io"
"strconv"
"strings"
)
/*
func ReadByteToUintX(r io.Reader, l int) (data uint64, err error) {
if l%8 != 0 || l > 64 {
return 0, errors.New("disable convert")
}
bb := make([]byte, l)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
switch l / 8 {
case 1:
{
return uint8(bb[0]), nil
}
case 2:
{
return BigEndian.Uint16(bb), nil
}
case 3:
{
return BigEndian.Uint24(bb), nil
}
case 4:
{
return BigEndian.Uint32(bb), nil
}
case 5:
{
//return BigEndian.Uint40(bb), nil
return 0, errors.New("disable convert")
}
case 6:
{
return BigEndian.Uint48(bb), nil
}
case 7:
{
//return BigEndian.Uint56(bb), nil
return 0, errors.New("disable convert")
}
case 8:
{
return BigEndian.Uint64(bb), nil
}
}
return 0, errors.New("convert not exist")
}
*/
// // 千万注意大小端,RTMP是大端
func ByteToUint32N(data []byte) (ret uint32, err error) {
if len(data) > 4 {
return 0, errors.New("ByteToUint32N error!")
}
for i := 0; i < len(data); i++ {
ret <<= 8
ret |= uint32(data[i])
}
return
}
// // 千万注意大小端,RTMP是大端
func ByteToUint64N(data []byte) (ret uint64, err error) {
if len(data) > 8 {
return 0, errors.New("ByteToUint64N error!")
}
for i := 0; i < len(data); i++ {
ret <<= 8
ret |= uint64(data[i])
}
return
}
// 千万注意大小端,RTMP是大端
func ByteToUint32(data []byte, bigEndian bool) (ret uint32, err error) {
if bigEndian {
return BigEndian.Uint32(data), nil
} else {
return LittleEndian.Uint32(data), nil
}
}
func Uint32ToByte(data uint32, bigEndian bool) (ret []byte, err error) {
if bigEndian {
return BigEndian.ToUint32(data), nil
} else {
return LittleEndian.ToUint32(data), nil
}
}
func ReadByteToUint8(r io.Reader) (data uint8, err error) {
bb := make([]byte, 1)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
return uint8(bb[0]), nil
}
func ReadByteToUint16(r io.Reader, bigEndian bool) (data uint16, err error) {
bb := make([]byte, 2)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
if bigEndian {
return BigEndian.Uint16(bb), nil
} else {
return LittleEndian.Uint16(bb), nil
}
}
func ReadByteToUint24(r io.Reader, bigEndian bool) (data uint32, err error) {
bb := make([]byte, 3)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
if bigEndian {
return BigEndian.Uint24(bb), nil
} else {
return LittleEndian.Uint24(bb), nil
}
}
func ReadByteToUint32(r io.Reader, bigEndian bool) (data uint32, err error) {
bb := make([]byte, 4)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
if bigEndian {
return BigEndian.Uint32(bb), nil
} else {
return LittleEndian.Uint32(bb), nil
}
}
func ReadByteToUint40(r io.Reader, bigEndian bool) (data uint64, err error) {
bb := make([]byte, 5)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
if bigEndian {
return BigEndian.Uint40(bb), nil
} else {
return LittleEndian.Uint40(bb), nil
}
}
func ReadByteToUint48(r io.Reader, bigEndian bool) (data uint64, err error) {
bb := make([]byte, 6)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
if bigEndian {
return BigEndian.Uint48(bb), nil
} else {
return LittleEndian.Uint48(bb), nil
}
}
/*
func ReadByteToUint56(r io.Reader) (data uint64, err error) {
bb := make([]byte, 7)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
return uint8(bb[0]), nil
}
*/
func ReadByteToUint64(r io.Reader, bigEndian bool) (data uint64, err error) {
bb := make([]byte, 8)
if _, err := io.ReadFull(r, bb); err != nil {
return 0, err
}
if bigEndian {
return BigEndian.Uint64(bb), nil
} else {
return LittleEndian.Uint64(bb), nil
}
}
func WriteUint8ToByte(w io.Writer, data uint8) error {
bb := make([]byte, 8)
bb[0] = byte(data)
_, err := w.Write(bb[:1])
if err != nil {
return err
}
return nil
}
func WriteUint16ToByte(w io.Writer, data uint16, bigEndian bool) error {
var bb []byte
if bigEndian {
bb = BigEndian.ToUint16(data)
} else {
bb = LittleEndian.ToUint16(data)
}
_, err := w.Write(bb)
if err != nil {
return err
}
return nil
}
func WriteUint24ToByte(w io.Writer, data uint32, bigEndian bool) error {
var bb []byte
if bigEndian {
bb = BigEndian.ToUint24(data)
} else {
bb = LittleEndian.ToUint24(data)
}
_, err := w.Write(bb)
if err != nil {
return err
}
return nil
}
func WriteUint32ToByte(w io.Writer, data uint32, bigEndian bool) error {
var bb []byte
if bigEndian {
bb = BigEndian.ToUint32(data)
} else {
bb = LittleEndian.ToUint32(data)
}
_, err := w.Write(bb)
if err != nil {
return err
}
return nil
}
func WriteUint40ToByte(w io.Writer, data uint64, bigEndian bool) error {
var bb []byte
if bigEndian {
bb = BigEndian.ToUint40(data)
} else {
bb = LittleEndian.ToUint40(data)
}
_, err := w.Write(bb)
if err != nil {
return err
}
return nil
}
func WriteUint48ToByte(w io.Writer, data uint64, bigEndian bool) error {
var bb []byte
if bigEndian {
bb = BigEndian.ToUint48(data)
} else {
bb = LittleEndian.ToUint48(data)
}
_, err := w.Write(bb)
if err != nil {
return err
}
return nil
}
func WriteUint64ToByte(w io.Writer, data uint64, bigEndian bool) error {
var bb []byte
if bigEndian {
bb = BigEndian.ToUint64(data)
} else {
bb = LittleEndian.ToUint64(data)
}
_, err := w.Write(bb)
if err != nil {
return err
}
return nil
}
func GetPtsDts(v uint64) uint64 {
// 4 + 3 + 1 + 15 + 1 + 15 + 1
// 0011
// 0010 + PTS[30-32] + marker_bit + PTS[29-15] + marker_bit + PTS[14-0] + marker_bit
pts1 := ((v >> 33) & 0x7) << 30
pts2 := ((v >> 17) & 0x7fff) << 15
pts3 := ((v >> 1) & 0x7fff)
return pts1 | pts2 | pts3
}
func PutPtsDts(v uint64) uint64 {
// 4 + 3 + 1 + 15 + 1 + 15 + 1
// 0011
// 0010 + PTS[30-32] + marker_bit + PTS[29-15] + marker_bit + PTS[14-0] + marker_bit
// 0x100010001
// 0001 0000 0000 0000 0001 0000 0000 0000 0001
// 3个 market_it
pts1 := (v >> 30) & 0x7 << 33
pts2 := (v >> 15) & 0x7fff << 17
pts3 := (v & 0x7fff) << 1
return pts1 | pts2 | pts3 | 0x100010001
}
func GetPCR(v uint64) uint64 {
// program_clock_reference_base(33) + Reserved(6) + program_clock_reference_extension(9)
base := v >> 15
ext := v & 0x1ff
return base*300 + ext
}
func PutPCR(pcr uint64) uint64 {
base := pcr / 300
ext := pcr % 300
return base<<15 | 0x3f<<9 | ext
}
func GetFillBytes(data byte, n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = data
}
return b
}
func ToFloat64(num interface{}) float64 {
switch v := num.(type) {
case uint:
return float64(v)
case int:
return float64(v)
case uint8:
return float64(v)
case uint16:
return float64(v)
case uint32:
return float64(v)
case uint64:
return float64(v)
case int8:
return float64(v)
case int16:
return float64(v)
case int32:
return float64(v)
case int64:
return float64(v)
case float64:
return v
case float32:
return float64(v)
}
return 0
}
func Conf2Listener(conf string) (protocol string, ports []uint16) {
var port string
protocol, port, _ = strings.Cut(conf, ":")
if r := strings.Split(port, "-"); len(r) == 2 {
min, err := strconv.Atoi(r[0])
if err != nil {
return
}
max, err := strconv.Atoi(r[1])
if err != nil {
return
}
if min < max {
ports = append(ports, uint16(min), uint16(max))
}
} else if p, err := strconv.Atoi(port); err == nil {
ports = append(ports, uint16(p))
}
return
}