-
Notifications
You must be signed in to change notification settings - Fork 161
/
outboundconn.go
426 lines (388 loc) · 12.1 KB
/
outboundconn.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2013, zhangpeihao All rights reserved.
package gortmp
import (
"bufio"
"bytes"
"crypto/tls"
"errors"
"fmt"
"github.com/zhangpeihao/goamf"
"github.com/zhangpeihao/log"
"net"
"time"
)
const (
OUTBOUND_CONN_STATUS_CLOSE = uint(0)
OUTBOUND_CONN_STATUS_HANDSHAKE_OK = uint(1)
OUTBOUND_CONN_STATUS_CONNECT = uint(2)
OUTBOUND_CONN_STATUS_CONNECT_OK = uint(3)
OUTBOUND_CONN_STATUS_CREATE_STREAM = uint(4)
OUTBOUND_CONN_STATUS_CREATE_STREAM_OK = uint(5)
)
// A handler for outbound connection
type OutboundConnHandler interface {
ConnHandler
// When connection status changed
OnStatus(obConn OutboundConn)
// On stream created
OnStreamCreated(obConn OutboundConn, stream OutboundStream)
}
type OutboundConn interface {
// Connect an appliction on FMS after handshake.
Connect(extendedParameters ...interface{}) (err error)
// Create a stream
CreateStream() (err error)
// Close a connection
Close()
// URL to connect
URL() string
// 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(name string, customParameters ...interface{}) (err error)
// Get network connect instance
Conn() Conn
}
// High-level interface
//
// A RTMP connection(based on TCP) to RTMP server(FMS or crtmpserver).
// In one connection, we can create many chunk streams.
type outboundConn struct {
url string
rtmpURL RtmpURL
status uint
err error
handler OutboundConnHandler
conn Conn
transactions map[uint32]string
streams map[uint32]OutboundStream
}
// Connect to FMS server, and finish handshake process
func Dial(url string, handler OutboundConnHandler, maxChannelNumber int) (OutboundConn, error) {
rtmpURL, err := ParseURL(url)
if err != nil {
return nil, err
}
var c net.Conn
switch rtmpURL.protocol {
case "rtmp":
c, err = net.Dial("tcp", fmt.Sprintf("%s:%d", rtmpURL.host, rtmpURL.port))
case "rtmps":
c, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", rtmpURL.host, rtmpURL.port), &tls.Config{InsecureSkipVerify: true})
default:
err = errors.New(fmt.Sprintf("Unsupport protocol %s", rtmpURL.protocol))
}
if err != nil {
return nil, err
}
ipConn, ok := c.(*net.TCPConn)
if ok {
ipConn.SetWriteBuffer(128 * 1024)
}
br := bufio.NewReader(c)
bw := bufio.NewWriter(c)
timeout := time.Duration(10*time.Second)
err = Handshake(c, br, bw, timeout)
//err = HandshakeSample(c, br, bw, timeout)
if err == nil {
logger.ModulePrintln(logHandler, log.LOG_LEVEL_DEBUG, "Handshake OK")
obConn := &outboundConn{
url: url,
rtmpURL: rtmpURL,
handler: handler,
status: OUTBOUND_CONN_STATUS_HANDSHAKE_OK,
transactions: make(map[uint32]string),
streams: make(map[uint32]OutboundStream),
}
obConn.handler.OnStatus(obConn)
obConn.conn = NewConn(c, br, bw, obConn, maxChannelNumber)
return obConn, nil
}
return nil, err
}
// Connect to FMS server, and finish handshake process
func NewOutbounConn(c net.Conn, url string, handler OutboundConnHandler, maxChannelNumber int) (OutboundConn, error) {
rtmpURL, err := ParseURL(url)
if err != nil {
return nil, err
}
if rtmpURL.protocol != "rtmp" {
return nil, errors.New(fmt.Sprintf("Unsupport protocol %s", rtmpURL.protocol))
}
/*
ipConn, ok := c.(*net.TCPConn)
if ok {
ipConn.SetWriteBuffer(128 * 1024)
}
*/
br := bufio.NewReader(c)
bw := bufio.NewWriter(c)
obConn := &outboundConn{
url: url,
rtmpURL: rtmpURL,
handler: handler,
status: OUTBOUND_CONN_STATUS_HANDSHAKE_OK,
transactions: make(map[uint32]string),
streams: make(map[uint32]OutboundStream),
}
obConn.conn = NewConn(c, br, bw, obConn, maxChannelNumber)
return obConn, nil
}
// Connect an appliction on FMS after handshake.
func (obConn *outboundConn) Connect(extendedParameters ...interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
if obConn.err == nil {
obConn.err = err
}
}
}()
// Create connect command
buf := new(bytes.Buffer)
// Command name
_, err = amf.WriteString(buf, "connect")
CheckError(err, "Connect() Write name: connect")
transactionID := obConn.conn.NewTransactionID()
obConn.transactions[transactionID] = "connect"
_, err = amf.WriteDouble(buf, float64(transactionID))
CheckError(err, "Connect() Write transaction ID")
_, err = amf.WriteObjectMarker(buf)
CheckError(err, "Connect() Write object marker")
_, err = amf.WriteObjectName(buf, "app")
CheckError(err, "Connect() Write app name")
_, err = amf.WriteString(buf, obConn.rtmpURL.App())
CheckError(err, "Connect() Write app value")
_, err = amf.WriteObjectName(buf, "flashVer")
CheckError(err, "Connect() Write flashver name")
_, err = amf.WriteString(buf, FLASH_PLAYER_VERSION_STRING)
CheckError(err, "Connect() Write flashver value")
// _, err = amf.WriteObjectName(buf, "swfUrl")
// CheckError(err, "Connect() Write swfUrl name")
// _, err = amf.WriteString(buf, SWF_URL_STRING)
// CheckError(err, "Connect() Write swfUrl value")
_, err = amf.WriteObjectName(buf, "tcUrl")
CheckError(err, "Connect() Write tcUrl name")
_, err = amf.WriteString(buf, obConn.url)
CheckError(err, "Connect() Write tcUrl value")
_, err = amf.WriteObjectName(buf, "fpad")
CheckError(err, "Connect() Write fpad name")
_, err = amf.WriteBoolean(buf, false)
CheckError(err, "Connect() Write fpad value")
_, err = amf.WriteObjectName(buf, "capabilities")
CheckError(err, "Connect() Write capabilities name")
_, err = amf.WriteDouble(buf, DEFAULT_CAPABILITIES)
CheckError(err, "Connect() Write capabilities value")
_, err = amf.WriteObjectName(buf, "audioCodecs")
CheckError(err, "Connect() Write audioCodecs name")
_, err = amf.WriteDouble(buf, DEFAULT_AUDIO_CODECS)
CheckError(err, "Connect() Write audioCodecs value")
_, err = amf.WriteObjectName(buf, "videoCodecs")
CheckError(err, "Connect() Write videoCodecs name")
_, err = amf.WriteDouble(buf, DEFAULT_VIDEO_CODECS)
CheckError(err, "Connect() Write videoCodecs value")
_, err = amf.WriteObjectName(buf, "videoFunction")
CheckError(err, "Connect() Write videoFunction name")
_, err = amf.WriteDouble(buf, float64(1))
CheckError(err, "Connect() Write videoFunction value")
// _, err = amf.WriteObjectName(buf, "pageUrl")
// CheckError(err, "Connect() Write pageUrl name")
// _, err = amf.WriteString(buf, PAGE_URL_STRING)
// CheckError(err, "Connect() Write pageUrl value")
//_, err = amf.WriteObjectName(buf, "objectEncoding")
//CheckError(err, "Connect() Write objectEncoding name")
//_, err = amf.WriteDouble(buf, float64(amf.AMF0))
//CheckError(err, "Connect() Write objectEncoding value")
_, err = amf.WriteObjectEndMarker(buf)
CheckError(err, "Connect() Write ObjectEndMarker")
// extended parameters
for _, param := range extendedParameters {
_, err = amf.WriteValue(buf, param)
CheckError(err, "Connect() Write extended parameters")
}
connectMessage := &Message{
ChunkStreamID: CS_ID_COMMAND,
Type: COMMAND_AMF0,
Size: uint32(buf.Len()),
Buf: buf,
}
connectMessage.Dump("connect")
obConn.status = OUTBOUND_CONN_STATUS_CONNECT
return obConn.conn.Send(connectMessage)
}
// Close a connection
func (obConn *outboundConn) Close() {
for _, stream := range obConn.streams {
stream.Close()
}
obConn.status = OUTBOUND_CONN_STATUS_CLOSE
go func() {
time.Sleep(time.Second)
obConn.conn.Close()
}()
}
// URL to connect
func (obConn *outboundConn) URL() string {
return obConn.url
}
// Connection status
func (obConn *outboundConn) Status() (uint, error) {
return obConn.status, obConn.err
}
// Callback when recieved message. Audio & Video data
func (obConn *outboundConn) OnReceived(conn Conn, message *Message) {
stream, found := obConn.streams[message.StreamID]
if found {
if !stream.Received(message) {
obConn.handler.OnReceived(conn, message)
}
} else {
obConn.handler.OnReceived(conn, message)
}
}
// Callback when recieved message.
func (obConn *outboundConn) OnReceivedRtmpCommand(conn Conn, command *Command) {
command.Dump()
switch command.Name {
case "_result":
transaction, found := obConn.transactions[command.TransactionID]
if found {
switch transaction {
case "connect":
if command.Objects != nil && len(command.Objects) >= 2 {
information, ok := command.Objects[1].(amf.Object)
if ok {
code, ok := information["code"]
if ok && code == RESULT_CONNECT_OK {
// Connect OK
//time.Sleep(time.Duration(200) * time.Millisecond)
obConn.conn.SetWindowAcknowledgementSize()
obConn.status = OUTBOUND_CONN_STATUS_CONNECT_OK
obConn.handler.OnStatus(obConn)
obConn.status = OUTBOUND_CONN_STATUS_CREATE_STREAM
obConn.CreateStream()
}
}
}
case "createStream":
if command.Objects != nil && len(command.Objects) >= 2 {
streamID, ok := command.Objects[1].(float64)
if ok {
newChunkStream, err := obConn.conn.CreateMediaChunkStream()
if err != nil {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"outboundConn::ReceivedRtmpCommand() CreateMediaChunkStream err:", err)
return
}
stream := &outboundStream{
id: uint32(streamID),
conn: obConn,
chunkStreamID: newChunkStream.ID,
}
obConn.streams[stream.ID()] = stream
obConn.status = OUTBOUND_CONN_STATUS_CREATE_STREAM_OK
obConn.handler.OnStatus(obConn)
obConn.handler.OnStreamCreated(obConn, stream)
}
}
}
delete(obConn.transactions, command.TransactionID)
}
case "_error":
transaction, found := obConn.transactions[command.TransactionID]
if found {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_TRACE,
"Command(%d) %s error\n", command.TransactionID, transaction)
} else {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_TRACE,
"Command(%d) not been found\n", command.TransactionID)
}
case "onBWCheck":
}
obConn.handler.OnReceivedRtmpCommand(obConn.conn, command)
}
// Connection closed
func (obConn *outboundConn) OnClosed(conn Conn) {
obConn.status = OUTBOUND_CONN_STATUS_CLOSE
obConn.handler.OnStatus(obConn)
obConn.handler.OnClosed(conn)
}
// Create a stream
func (obConn *outboundConn) CreateStream() (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
if obConn.err == nil {
obConn.err = err
}
}
}()
// Create createStream command
transactionID := obConn.conn.NewTransactionID()
cmd := &Command{
IsFlex: false,
Name: "createStream",
TransactionID: transactionID,
Objects: make([]interface{}, 1),
}
cmd.Objects[0] = nil
buf := new(bytes.Buffer)
err = cmd.Write(buf)
CheckError(err, "createStream() Create command")
obConn.transactions[transactionID] = "createStream"
message := &Message{
ChunkStreamID: CS_ID_COMMAND,
Type: COMMAND_AMF0,
Size: uint32(buf.Len()),
Buf: buf,
}
message.Dump("createStream")
return obConn.conn.Send(message)
}
// Send a message
func (obConn *outboundConn) Send(message *Message) error {
return obConn.conn.Send(message)
}
// Calls a command or method on Flash Media Server
// or on an application server running Flash Remoting.
func (obConn *outboundConn) Call(name string, customParameters ...interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
if obConn.err == nil {
obConn.err = err
}
}
}()
// Create command
transactionID := obConn.conn.NewTransactionID()
cmd := &Command{
IsFlex: false,
Name: name,
TransactionID: transactionID,
Objects: make([]interface{}, 1+len(customParameters)),
}
cmd.Objects[0] = nil
for index, param := range customParameters {
cmd.Objects[index+1] = param
}
buf := new(bytes.Buffer)
err = cmd.Write(buf)
CheckError(err, "Call() Create command")
obConn.transactions[transactionID] = name
message := &Message{
ChunkStreamID: CS_ID_COMMAND,
Type: COMMAND_AMF0,
Size: uint32(buf.Len()),
Buf: buf,
}
message.Dump(name)
return obConn.conn.Send(message)
}
// Get network connect instance
func (obConn *outboundConn) Conn() Conn {
return obConn.conn
}