-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
172 lines (138 loc) · 3.96 KB
/
packet.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
package rtmp
import (
"encoding/binary"
)
type RTMPPacketHeader struct {
timestamp int64
fmt uint32
cid uint32
packet_type uint32
StreamID uint32
length uint32 // Payload length
}
type RTMPPacket struct {
header RTMPPacketHeader
clock int64
payload []byte
capacity uint32
bytes uint32
handled bool
}
const RTMP_PACKET_BASE_SIZE = 65
func createBlankRTMPPacket() RTMPPacket {
return RTMPPacket{
header: RTMPPacketHeader{
timestamp: 0,
fmt: 0,
cid: 0,
packet_type: 0,
StreamID: 0,
length: 0,
},
clock: 0,
payload: []byte{},
capacity: 0,
bytes: 0,
handled: false,
}
}
func rtmpChunkBasicHeaderCreate(fmt uint32, cid uint32) []byte {
var out []byte
if cid >= 64+255 {
out = make([]byte, 3)
out[0] = byte(fmt<<6) | 1
out[1] = byte(cid-64) & 0xff
out[2] = byte(cid-64>>8) & 0xff
} else if cid >= 64 {
out = make([]byte, 2)
out[0] = byte(fmt << 6)
out[1] = byte(cid-64) & 0xff
} else {
out = make([]byte, 1)
out[0] = byte(fmt<<6) | byte(cid)
}
return out
}
func rtmpChunkMessageHeaderCreate(packet *RTMPPacket) []byte {
out := make([]byte, 0)
if packet.header.fmt <= RTMP_CHUNK_TYPE_2 {
b := make([]byte, 4)
if packet.header.timestamp >= 0xffffff {
binary.BigEndian.PutUint32(b, 0xffffff)
} else {
binary.BigEndian.PutUint32(b, uint32(packet.header.timestamp))
}
out = append(out, b[1:]...)
}
if packet.header.fmt <= RTMP_CHUNK_TYPE_1 {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, packet.header.length)
out = append(out, b[1:]...)
out = append(out, byte(packet.header.packet_type))
}
if packet.header.fmt == RTMP_CHUNK_TYPE_0 {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, packet.header.StreamID)
out = append(out, b...)
}
return out
}
func (packet *RTMPPacket) CreateChunks(outChunkSize int) []byte {
chunkBasicHeader := rtmpChunkBasicHeaderCreate(packet.header.fmt, packet.header.cid)
chunkBasicHeader3 := rtmpChunkBasicHeaderCreate(RTMP_CHUNK_TYPE_3, packet.header.cid)
chunkMessageHeader := rtmpChunkMessageHeaderCreate(packet)
useExtendedTimestamp := (packet.header.timestamp >= 0xffffff)
var headerSize int
var payloadSize int
var chunksOffset int
var payloadOffset int
var n int
headerSize = len(chunkBasicHeader) + len(chunkMessageHeader)
payloadSize = int(packet.header.length)
chunksOffset = 0
payloadOffset = 0
if useExtendedTimestamp {
headerSize += 4
}
n = headerSize + payloadSize + (payloadSize / outChunkSize)
if useExtendedTimestamp {
n += (payloadSize / outChunkSize) * 4
}
if (payloadSize % outChunkSize) == 0 {
n--
if useExtendedTimestamp {
n -= 4
}
}
chunks := make([]byte, n)
copy(chunks[chunksOffset:], chunkBasicHeader[:])
chunksOffset += len(chunkBasicHeader)
copy(chunks[chunksOffset:], chunkMessageHeader[:])
chunksOffset += len(chunkMessageHeader)
if useExtendedTimestamp {
binary.BigEndian.PutUint32(chunks[chunksOffset:chunksOffset+4], uint32(packet.header.timestamp))
chunksOffset += 4
}
for payloadSize > 0 {
if payloadSize > outChunkSize {
copy(chunks[chunksOffset:], packet.payload[payloadOffset:payloadOffset+outChunkSize])
payloadSize -= outChunkSize
chunksOffset += outChunkSize
payloadOffset += outChunkSize
copy(chunks[chunksOffset:], chunkBasicHeader3[:])
chunksOffset += len(chunkBasicHeader3)
if useExtendedTimestamp {
binary.BigEndian.PutUint32(chunks[chunksOffset:chunksOffset+4], uint32(packet.header.timestamp))
chunksOffset += 4
}
} else {
copy(chunks[chunksOffset:], packet.payload[payloadOffset:payloadOffset+payloadSize])
payloadSize -= payloadSize
chunksOffset += payloadSize
payloadOffset += payloadSize
}
}
//LogDebug("PAYLOAD: " + hex.EncodeToString(packet.payload))
//LogDebug("CHUNKS: " + hex.EncodeToString(chunks))
return chunks
}