-
Notifications
You must be signed in to change notification settings - Fork 161
/
inboundconn.go
323 lines (290 loc) · 8.47 KB
/
inboundconn.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
// Copyright 2013, zhangpeihao All rights reserved.
package gortmp
import (
"bufio"
"bytes"
"errors"
"fmt"
"github.com/zhangpeihao/goamf"
"github.com/zhangpeihao/log"
"net"
"sync"
"time"
)
const (
INBOUND_CONN_STATUS_CLOSE = uint(0)
INBOUND_CONN_STATUS_CONNECT_OK = uint(1)
INBOUND_CONN_STATUS_CREATE_STREAM_OK = uint(2)
)
// A handler for inbound connection
type InboundAuthHandler interface {
OnConnectAuth(ibConn InboundConn, connectReq *Command) bool
}
// A handler for inbound connection
type InboundConnHandler interface {
ConnHandler
// When connection status changed
OnStatus(ibConn InboundConn)
// On stream created
OnStreamCreated(ibConn InboundConn, stream InboundStream)
// On stream closed
OnStreamClosed(ibConn InboundConn, stream InboundStream)
}
type InboundConn interface {
// Close a connection
Close()
// Connection status
Status() (uint, error)
// Send a message
Send(message *Message) error
// Calls a command or method on Flash Media Server
// or on an application server running Flash Remoting.
Call(customParameters ...interface{}) (err error)
// Get network connect instance
Conn() Conn
// Attach handler
Attach(handler InboundConnHandler)
// Get connect request
ConnectRequest() *Command
}
type inboundConn struct {
connectReq *Command
app string
handler InboundConnHandler
authHandler InboundAuthHandler
conn Conn
status uint
err error
streams map[uint32]*inboundStream
streamsLocker sync.Mutex
}
func NewInboundConn(c net.Conn, br *bufio.Reader, bw *bufio.Writer,
authHandler InboundAuthHandler, maxChannelNumber int) (InboundConn, error) {
ibConn := &inboundConn{
authHandler: authHandler,
status: INBOUND_CONN_STATUS_CLOSE,
streams: make(map[uint32]*inboundStream),
}
ibConn.conn = NewConn(c, br, bw, ibConn, maxChannelNumber)
return ibConn, nil
}
// Callback when recieved message. Audio & Video data
func (ibConn *inboundConn) OnReceived(conn Conn, message *Message) {
stream, found := ibConn.streams[message.StreamID]
if found {
if !stream.Received(message) {
ibConn.handler.OnReceived(ibConn.conn, message)
}
} else {
ibConn.handler.OnReceived(ibConn.conn, message)
}
}
// Callback when recieved message.
func (ibConn *inboundConn) OnReceivedRtmpCommand(conn Conn, command *Command) {
command.Dump()
switch command.Name {
case "connect":
ibConn.onConnect(command)
// Connect from client
case "createStream":
// Create a new stream
ibConn.onCreateStream(command)
default:
logger.ModulePrintf(logHandler, log.LOG_LEVEL_TRACE, "inboundConn::ReceivedRtmpCommand: %+v\n", command)
}
}
// Connection closed
func (ibConn *inboundConn) OnClosed(conn Conn) {
ibConn.status = INBOUND_CONN_STATUS_CLOSE
ibConn.handler.OnStatus(ibConn)
}
// Close a connection
func (ibConn *inboundConn) Close() {
for _, stream := range ibConn.streams {
stream.Close()
}
time.Sleep(time.Second)
ibConn.status = INBOUND_CONN_STATUS_CLOSE
ibConn.conn.Close()
}
// Send a message
func (ibConn *inboundConn) Send(message *Message) error {
return ibConn.conn.Send(message)
}
// Calls a command or method on Flash Media Server
// or on an application server running Flash Remoting.
func (ibConn *inboundConn) Call(customParameters ...interface{}) (err error) {
return errors.New("Unimplemented")
}
// Get network connect instance
func (ibConn *inboundConn) Conn() Conn {
return ibConn.conn
}
// Connection status
func (ibConn *inboundConn) Status() (uint, error) {
return ibConn.status, ibConn.err
}
func (ibConn *inboundConn) Attach(handler InboundConnHandler) {
ibConn.handler = handler
}
////////////////////////////////
// Local functions
func (ibConn *inboundConn) allocStream(stream *inboundStream) uint32 {
ibConn.streamsLocker.Lock()
i := uint32(1)
for {
_, found := ibConn.streams[i]
if !found {
ibConn.streams[i] = stream
stream.id = i
break
}
i++
}
ibConn.streamsLocker.Unlock()
return i
}
func (ibConn *inboundConn) releaseStream(streamID uint32) {
ibConn.streamsLocker.Lock()
delete(ibConn.streams, streamID)
ibConn.streamsLocker.Unlock()
}
func (ibConn *inboundConn) onConnect(cmd *Command) {
logger.ModulePrintln(logHandler, log.LOG_LEVEL_TRACE,
"inboundConn::onConnect")
ibConn.connectReq = cmd
if cmd.Objects == nil {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundConn::onConnect cmd.Object == nil\n")
ibConn.sendConnectErrorResult(cmd)
return
}
if len(cmd.Objects) == 0 {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundConn::onConnect len(cmd.Object) == 0\n")
ibConn.sendConnectErrorResult(cmd)
return
}
params, ok := cmd.Objects[0].(amf.Object)
if !ok {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundConn::onConnect cmd.Object[0] is not an amd object\n")
ibConn.sendConnectErrorResult(cmd)
return
}
// Get app
app, found := params["app"]
if !found {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundConn::onConnect no app value in cmd.Object[0]\n")
ibConn.sendConnectErrorResult(cmd)
return
}
ibConn.app, ok = app.(string)
if !ok {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundConn::onConnect cmd.Object[0].app is not a string\n")
ibConn.sendConnectErrorResult(cmd)
return
}
// Todo: Get version for log
// Todo: Get other paramters
// Todo: Auth by logical
if ibConn.authHandler.OnConnectAuth(ibConn, cmd) {
ibConn.conn.SetWindowAcknowledgementSize()
ibConn.conn.SetPeerBandwidth(2500000, SET_PEER_BANDWIDTH_DYNAMIC)
ibConn.conn.SetChunkSize(4096)
ibConn.sendConnectSucceededResult(cmd)
} else {
ibConn.sendConnectErrorResult(cmd)
}
}
func (ibConn *inboundConn) onCreateStream(cmd *Command) {
logger.ModulePrintln(logHandler, log.LOG_LEVEL_TRACE,
"inboundConn::onCreateStream")
// New inbound stream
newChunkStream, err := ibConn.conn.CreateMediaChunkStream()
if err != nil {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"outboundConn::ReceivedCommand() CreateMediaChunkStream err:", err)
return
}
stream := &inboundStream{
conn: ibConn,
chunkStreamID: newChunkStream.ID,
}
ibConn.allocStream(stream)
ibConn.status = INBOUND_CONN_STATUS_CREATE_STREAM_OK
ibConn.handler.OnStatus(ibConn)
ibConn.handler.OnStreamCreated(ibConn, stream)
// Response result
ibConn.sendCreateStreamSuccessResult(cmd)
}
func (ibConn *inboundConn) onCloseStream(stream *inboundStream) {
ibConn.releaseStream(stream.id)
ibConn.handler.OnStreamClosed(ibConn, stream)
}
func (ibConn *inboundConn) sendConnectSucceededResult(req *Command) {
obj1 := make(amf.Object)
obj1["fmsVer"] = fmt.Sprintf("FMS/%s", FMS_VERSION_STRING)
obj1["capabilities"] = float64(255)
obj2 := make(amf.Object)
obj2["level"] = "status"
obj2["code"] = RESULT_CONNECT_OK
obj2["description"] = RESULT_CONNECT_OK_DESC
ibConn.sendConnectResult(req, "_result", obj1, obj2)
}
func (ibConn *inboundConn) sendConnectErrorResult(req *Command) {
obj2 := make(amf.Object)
obj2["level"] = "status"
obj2["code"] = RESULT_CONNECT_REJECTED
obj2["description"] = RESULT_CONNECT_REJECTED_DESC
ibConn.sendConnectResult(req, "_error", nil, obj2)
}
func (ibConn *inboundConn) sendConnectResult(req *Command, name string, obj1, obj2 interface{}) (err error) {
// Create createStream command
cmd := &Command{
IsFlex: false,
Name: name,
TransactionID: req.TransactionID,
Objects: make([]interface{}, 2),
}
cmd.Objects[0] = obj1
cmd.Objects[1] = obj2
buf := new(bytes.Buffer)
err = cmd.Write(buf)
CheckError(err, "inboundConn::sendConnectResult() Create command")
message := &Message{
ChunkStreamID: CS_ID_COMMAND,
Type: COMMAND_AMF0,
Size: uint32(buf.Len()),
Buf: buf,
}
message.Dump("sendConnectResult")
return ibConn.conn.Send(message)
}
func (ibConn *inboundConn) sendCreateStreamSuccessResult(req *Command) (err error) {
// Create createStream command
cmd := &Command{
IsFlex: false,
Name: "_result",
TransactionID: req.TransactionID,
Objects: make([]interface{}, 2),
}
cmd.Objects[0] = nil
cmd.Objects[1] = int32(1)
buf := new(bytes.Buffer)
err = cmd.Write(buf)
CheckError(err, "inboundConn::sendCreateStreamSuccessResult() Create command")
message := &Message{
ChunkStreamID: CS_ID_COMMAND,
Type: COMMAND_AMF0,
Size: uint32(buf.Len()),
Buf: buf,
}
message.Dump("sendCreateStreamSuccessResult")
return ibConn.conn.Send(message)
}
func (ibConn *inboundConn) ConnectRequest() *Command {
return ibConn.connectReq
}