This repository was archived by the owner on Feb 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcommand_message.go
More file actions
208 lines (189 loc) · 4.99 KB
/
command_message.go
File metadata and controls
208 lines (189 loc) · 4.99 KB
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
package rtmp
import (
"bytes"
"fmt"
"math"
"github.com/zhangpeihao/goamf"
)
type CommandCode string
const (
CodeNetConnectAppShutdown CommandCode = "NetConnection.Connect.AppShutdown"
CodeNetConnectClosed = "NetConnection.Connect.Closed"
CodeNetConnectFailed = "NetConnection.Connect.Failed"
CodeNetConnectIdleTimeout = "NetConnection.Connect.IdleTimeout"
CodeNetConnectInvalidApp = "NetConnection.Connect.InvalidApp"
CodeNetConnectNetworkChange = "NetConnection.Connect.NetworkChange"
CodeNetConnectRejected = "NetConnection.Connect.Rejected"
CodeNetConnectSuccess = "NetConnection.Connect.Success"
)
type CommandLevel string
const (
CommandLevelStatus CommandLevel = "status"
CommandLevelError = "error"
)
type ResultCommand struct {
Name string
TransactionID float64
Properties map[string]interface{}
Information map[string]interface{}
}
func (rc *ResultCommand) Bytes() []byte {
buf := new(bytes.Buffer)
amf.WriteValue(buf, rc.Name)
amf.WriteValue(buf, rc.TransactionID)
amf.WriteValue(buf, rc.Properties)
amf.WriteValue(buf, rc.Information)
return buf.Bytes()
}
func GenerateConnectResult(transactionID float64) ([]byte, error) {
cmd := &ResultCommand{
Name: "_result",
TransactionID: transactionID,
Properties: map[string]interface{}{
"fmsVer": "FMS/3,5,7,7009",
"capabilities": 31,
"mode": 1,
},
Information: map[string]interface{}{
"code": CodeNetConnectSuccess,
"description": "Connection succeeded.",
"data": map[string]interface{}{
"version": "3,5,7,7009",
},
"objectEncoding": 0,
"level": CommandLevelStatus,
},
}
payload := cmd.Bytes()
ch := &ChunkHeader{
BasicHeader: &BasicHeader{
FMT: 0,
ChunkStreamID: 3,
},
MessageHeader: &MessageHeader{
Timestamp: 0,
MessageLength: uint32(len(payload)),
MessageTypeID: 20,
MessageStreamID: 0,
},
}
header, err := genChunkHeader(ch)
if err != nil {
return []byte{}, err
}
x := append(header, payload...)
return x, nil
}
func GenerateOnFCPublishMessage(transactionID float64, streamName string) ([]byte, error) {
buf := new(bytes.Buffer)
amf.WriteValue(buf, "onFCPublish")
amf.WriteValue(buf, transactionID)
amf.WriteValue(buf, nil)
amf.WriteValue(buf, 1)
amf.WriteValue(buf, map[string]interface{}{
"level": "status",
"code": "NetStream.Publish.Start",
"description": fmt.Sprintf("FCPublish to stream %s.", streamName),
})
payload := buf.Bytes()
ch := &ChunkHeader{
BasicHeader: &BasicHeader{
FMT: 0,
ChunkStreamID: 3,
},
MessageHeader: &MessageHeader{
Timestamp: 0,
MessageLength: uint32(len(payload)),
MessageTypeID: 20,
MessageStreamID: 0,
},
}
header, err := genChunkHeader(ch)
if err != nil {
return []byte{}, err
}
x := append(header, payload...)
return x, nil
}
type CreateStreamCommand struct {
Name string
TransactionID float64
Properties map[string]interface{}
Message map[string]interface{}
}
func (c *CreateStreamCommand) Bytes() []byte {
buf := new(bytes.Buffer)
amf.WriteValue(buf, c.Name)
amf.WriteValue(buf, c.TransactionID)
amf.WriteValue(buf, nil)
amf.WriteValue(buf, 1)
return buf.Bytes()
}
func CreateStreamResponseMessage(transactionID float64) ([]byte, error) {
cmd := &CreateStreamCommand{
Name: "_result",
TransactionID: transactionID,
}
payload := cmd.Bytes()
ch := &ChunkHeader{
BasicHeader: &BasicHeader{
FMT: 0,
ChunkStreamID: 3,
},
MessageHeader: &MessageHeader{
Timestamp: 0,
MessageLength: uint32(len(payload)),
MessageTypeID: 20,
MessageStreamID: 0,
},
}
header, err := genChunkHeader(ch)
if err != nil {
return []byte{}, err
}
x := append(header, payload...)
return x, nil
}
type NetStreamStatusMessage struct {
Name string
TransactionID float64
InfoObject map[string]interface{}
}
func (m *NetStreamStatusMessage) Bytes() []byte {
buf := new(bytes.Buffer)
amf.WriteValue(buf, m.Name)
amf.WriteValue(buf, m.TransactionID)
amf.WriteValue(buf, nil)
amf.WriteValue(buf, m.InfoObject)
return buf.Bytes()
}
func CreateOnStatusPublishStartMessage(transactionID float64, streamName string) ([]byte, error) {
cmd := &NetStreamStatusMessage{
Name: "onStatus",
TransactionID: transactionID,
InfoObject: map[string]interface{}{
"code": "NetStream.Publish.Start",
"description": fmt.Sprintf("Publishing %s.", streamName),
"level": "status",
},
}
payload := cmd.Bytes()
ch := &ChunkHeader{
BasicHeader: &BasicHeader{
FMT: 0,
ChunkStreamID: 3,
},
MessageHeader: &MessageHeader{
Timestamp: 0,
MessageLength: uint32(len(payload)),
MessageTypeID: 20,
MessageStreamID: uint32(math.Pow(2, 4*6)),
},
}
header, err := genChunkHeader(ch)
if err != nil {
return []byte{}, err
}
x := append(header, payload...)
return x, nil
}