/
util.go
113 lines (100 loc) · 3 KB
/
util.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
package h264
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
)
func IsAnyKeyframe(data []byte) bool {
const (
typeSTAPA = 24
typeSPS = 7
naluTypeBitmask = 0x1F
)
var word uint32
payload := bytes.NewReader(data)
if err := binary.Read(payload, binary.BigEndian, &word); err != nil {
return false
}
naluType := (word >> 24) & naluTypeBitmask
if naluType == typeSTAPA && word&naluTypeBitmask == typeSPS {
return true
} else if naluType == typeSPS {
return true
}
return false
}
func IsKeyframePart(packetPayload []byte) bool {
// Thank you Hayden :)
// Is this packet part of a keyframe?
if len(packetPayload) < 2 {
return false
}
isKeyframePart := false
nalType := packetPayload[0] & 0b00011111
// nalType 7 = Sequence Parameter Set / nalType 8 = Picture Parameter Set
// nalType 5 = IDR
// nalType 28 = Fragmentation unit (FU-A)
// nalType 29 = Fragmentation unit (FU-A)
if (nalType == 7) || (nalType == 8) {
// SPS often precedes an IDR (Instantaneous Decoder Refresh) aka Keyframe
// and provides information on how to decode it. We should keep this around.
isKeyframePart = true
} else if nalType == 5 {
// Managed to fit an entire IDR into one packet!
isKeyframePart = true
} else if nalType == 28 || nalType == 29 {
// See https://tools.ietf.org/html/rfc3984#section-5.8
fragmentType := packetPayload[1] & 0b00011111
// fragmentType 7 = Fragment of SPS
// fragmentType 5 = Fragment of IDR
if (fragmentType == 7) || (fragmentType == 5) {
isKeyframePart = true
}
}
return isKeyframePart
}
func WhichKeyframePart(packetPayload []byte) string {
// Thank you Hayden :)
// Is this packet part of a keyframe?
if len(packetPayload) < 2 {
return "too small"
}
nalType := packetPayload[0] & 0b00011111
// nalType 7 = Sequence Parameter Set / nalType 8 = Picture Parameter Set
// nalType 5 = IDR
// nalType 28 = Fragmentation unit (FU-A)
// nalType 29 = Fragmentation unit (FU-A)
switch nalType {
case 7:
return "7 - Sequence Parameter Set"
case 8:
return "8 - Picture Parameter Set"
case 5:
return "5 - IDR In one packet"
case 28:
fragmentType := packetPayload[1] & 0b00011111
// fragmentType 7 = Fragment of SPS
// fragmentType 5 = Fragment of IDR
if fragmentType == 7 {
return fmt.Sprintf("%d - %s", nalType, "7 - Fragment of SPS")
} else if fragmentType == 5 {
return fmt.Sprintf("%d - %s", nalType, "5 - Fragment of IDR")
} else {
return fmt.Sprintf("%d - %d - %s ", nalType, fragmentType, "Unknown Type")
}
case 29:
fragmentType := packetPayload[1] & 0b00011111
// fragmentType 7 = Fragment of SPS
// fragmentType 5 = Fragment of IDR
if fragmentType == 7 {
return fmt.Sprintf("%d - %s", nalType, "7 - Fragment of SPS")
} else if fragmentType == 5 {
return fmt.Sprintf("%d - %s", nalType, "5 - Fragment of IDR")
} else {
return fmt.Sprintf("%d - %d - %s ", nalType, fragmentType, "Unknown Type")
}
}
fmt.Printf("%s", hex.Dump(packetPayload))
return fmt.Sprintf("%d - %s ", nalType, "Unknown Type")
}