-
Notifications
You must be signed in to change notification settings - Fork 3
/
conn_server.go
executable file
·358 lines (320 loc) · 8.75 KB
/
conn_server.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
package core
import (
"bytes"
"fmt"
"io"
"github.com/dudebing99/livego/av"
"github.com/dudebing99/livego/protocol/amf"
log "github.com/sirupsen/logrus"
)
var (
publishLive = "live"
publishRecord = "record"
publishAppend = "append"
)
var (
ErrReq = fmt.Errorf("req error")
)
var (
cmdConnect = "connect"
cmdFcpublish = "FCPublish"
cmdReleaseStream = "releaseStream"
cmdCreateStream = "createStream"
cmdPublish = "publish"
cmdFCUnpublish = "FCUnpublish"
cmdDeleteStream = "deleteStream"
cmdPlay = "play"
)
type ConnectInfo struct {
App string `amf:"app" json:"app"`
Flashver string `amf:"flashVer" json:"flashVer"`
SwfUrl string `amf:"swfUrl" json:"swfUrl"`
TcUrl string `amf:"tcUrl" json:"tcUrl"`
Fpad bool `amf:"fpad" json:"fpad"`
AudioCodecs int `amf:"audioCodecs" json:"audioCodecs"`
VideoCodecs int `amf:"videoCodecs" json:"videoCodecs"`
VideoFunction int `amf:"videoFunction" json:"videoFunction"`
PageUrl string `amf:"pageUrl" json:"pageUrl"`
ObjectEncoding int `amf:"objectEncoding" json:"objectEncoding"`
}
type ConnectResp struct {
FMSVer string `amf:"fmsVer"`
Capabilities int `amf:"capabilities"`
}
type ConnectEvent struct {
Level string `amf:"level"`
Code string `amf:"code"`
Description string `amf:"description"`
ObjectEncoding int `amf:"objectEncoding"`
}
type PublishInfo struct {
Name string
Type string
}
type ConnServer struct {
done bool
streamID int
isPublisher bool
conn *Conn
transactionID int
ConnInfo ConnectInfo
PublishInfo PublishInfo
decoder *amf.Decoder
encoder *amf.Encoder
bytesw *bytes.Buffer
}
func NewConnServer(conn *Conn) *ConnServer {
return &ConnServer{
conn: conn,
streamID: 1,
bytesw: bytes.NewBuffer(nil),
decoder: &amf.Decoder{},
encoder: &amf.Encoder{},
}
}
func (connServer *ConnServer) writeMsg(csid, streamID uint32, args ...interface{}) error {
connServer.bytesw.Reset()
for _, v := range args {
if _, err := connServer.encoder.Encode(connServer.bytesw, v, amf.AMF0); err != nil {
return err
}
}
msg := connServer.bytesw.Bytes()
c := ChunkStream{
Format: 0,
CSID: csid,
Timestamp: 0,
TypeID: 20,
StreamID: streamID,
Length: uint32(len(msg)),
Data: msg,
}
connServer.conn.Write(&c)
return connServer.conn.Flush()
}
func (connServer *ConnServer) connect(vs []interface{}) error {
for _, v := range vs {
switch v.(type) {
case string:
case float64:
id := int(v.(float64))
if id != 1 {
return ErrReq
}
connServer.transactionID = id
case amf.Object:
obimap := v.(amf.Object)
if app, ok := obimap["app"]; ok {
connServer.ConnInfo.App = app.(string)
}
if flashVer, ok := obimap["flashVer"]; ok {
connServer.ConnInfo.Flashver = flashVer.(string)
}
if tcurl, ok := obimap["tcUrl"]; ok {
connServer.ConnInfo.TcUrl = tcurl.(string)
}
if encoding, ok := obimap["objectEncoding"]; ok {
connServer.ConnInfo.ObjectEncoding = int(encoding.(float64))
}
}
}
return nil
}
func (connServer *ConnServer) releaseStream(vs []interface{}) error {
return nil
}
func (connServer *ConnServer) fcPublish(vs []interface{}) error {
return nil
}
func (connServer *ConnServer) connectResp(cur *ChunkStream) error {
c := connServer.conn.NewWindowAckSize(2500000)
connServer.conn.Write(&c)
c = connServer.conn.NewSetPeerBandwidth(2500000)
connServer.conn.Write(&c)
c = connServer.conn.NewSetChunkSize(uint32(1024))
connServer.conn.Write(&c)
resp := make(amf.Object)
resp["fmsVer"] = "FMS/3,0,1,123"
resp["capabilities"] = 31
event := make(amf.Object)
event["level"] = "status"
event["code"] = "NetConnection.Connect.Success"
event["description"] = "Connection succeeded."
event["objectEncoding"] = connServer.ConnInfo.ObjectEncoding
return connServer.writeMsg(cur.CSID, cur.StreamID, "_result", connServer.transactionID, resp, event)
}
func (connServer *ConnServer) createStream(vs []interface{}) error {
for _, v := range vs {
switch v.(type) {
case string:
case float64:
connServer.transactionID = int(v.(float64))
case amf.Object:
}
}
return nil
}
func (connServer *ConnServer) createStreamResp(cur *ChunkStream) error {
return connServer.writeMsg(cur.CSID, cur.StreamID, "_result", connServer.transactionID, nil, connServer.streamID)
}
func (connServer *ConnServer) publishOrPlay(vs []interface{}) error {
for k, v := range vs {
switch v.(type) {
case string:
if k == 2 {
connServer.PublishInfo.Name = v.(string)
} else if k == 3 {
connServer.PublishInfo.Type = v.(string)
}
case float64:
id := int(v.(float64))
connServer.transactionID = id
case amf.Object:
}
}
return nil
}
func (connServer *ConnServer) publishResp(cur *ChunkStream) error {
event := make(amf.Object)
event["level"] = "status"
event["code"] = "NetStream.Publish.Start"
event["description"] = "Start publishing."
return connServer.writeMsg(cur.CSID, cur.StreamID, "onStatus", 0, nil, event)
}
func (connServer *ConnServer) playResp(cur *ChunkStream) error {
connServer.conn.SetRecorded()
connServer.conn.SetBegin()
event := make(amf.Object)
event["level"] = "status"
event["code"] = "NetStream.Play.Reset"
event["description"] = "Playing and resetting stream."
if err := connServer.writeMsg(cur.CSID, cur.StreamID, "onStatus", 0, nil, event); err != nil {
return err
}
event["level"] = "status"
event["code"] = "NetStream.Play.Start"
event["description"] = "Started playing stream."
if err := connServer.writeMsg(cur.CSID, cur.StreamID, "onStatus", 0, nil, event); err != nil {
return err
}
event["level"] = "status"
event["code"] = "NetStream.Data.Start"
event["description"] = "Started playing stream."
if err := connServer.writeMsg(cur.CSID, cur.StreamID, "onStatus", 0, nil, event); err != nil {
return err
}
event["level"] = "status"
event["code"] = "NetStream.Play.PublishNotify"
event["description"] = "Started playing notify."
if err := connServer.writeMsg(cur.CSID, cur.StreamID, "onStatus", 0, nil, event); err != nil {
return err
}
return connServer.conn.Flush()
}
func (connServer *ConnServer) handleCmdMsg(c *ChunkStream) error {
amfType := amf.AMF0
if c.TypeID == 17 {
c.Data = c.Data[1:]
}
r := bytes.NewReader(c.Data)
vs, err := connServer.decoder.DecodeBatch(r, amf.Version(amfType))
if err != nil && err != io.EOF {
return err
}
// log.Debugf("rtmp req: %#v", vs)
switch vs[0].(type) {
case string:
switch vs[0].(string) {
case cmdConnect:
if err = connServer.connect(vs[1:]); err != nil {
return err
}
if err = connServer.connectResp(c); err != nil {
return err
}
case cmdCreateStream:
if err = connServer.createStream(vs[1:]); err != nil {
return err
}
if err = connServer.createStreamResp(c); err != nil {
return err
}
case cmdPublish:
if err = connServer.publishOrPlay(vs[1:]); err != nil {
return err
}
if err = connServer.publishResp(c); err != nil {
return err
}
connServer.done = true
connServer.isPublisher = true
log.Debug("handle publish req done")
case cmdPlay:
if err = connServer.publishOrPlay(vs[1:]); err != nil {
return err
}
if err = connServer.playResp(c); err != nil {
return err
}
connServer.done = true
connServer.isPublisher = false
log.Debug("handle play req done")
case cmdFcpublish:
connServer.fcPublish(vs)
case cmdReleaseStream:
connServer.releaseStream(vs)
case cmdFCUnpublish:
case cmdDeleteStream:
default:
log.Debug("no support command=", vs[0].(string))
}
}
return nil
}
func (connServer *ConnServer) ReadMsg() error {
var c ChunkStream
for {
if err := connServer.conn.Read(&c); err != nil {
return err
}
switch c.TypeID {
case 20, 17:
if err := connServer.handleCmdMsg(&c); err != nil {
return err
}
}
if connServer.done {
break
}
}
return nil
}
func (connServer *ConnServer) IsPublisher() bool {
return connServer.isPublisher
}
func (connServer *ConnServer) Write(c ChunkStream) error {
if c.TypeID == av.TAG_SCRIPTDATAAMF0 ||
c.TypeID == av.TAG_SCRIPTDATAAMF3 {
var err error
if c.Data, err = amf.MetaDataReform(c.Data, amf.DEL); err != nil {
return err
}
c.Length = uint32(len(c.Data))
}
return connServer.conn.Write(&c)
}
func (connServer *ConnServer) Flush() error {
return connServer.conn.Flush()
}
func (connServer *ConnServer) Read(c *ChunkStream) (err error) {
return connServer.conn.Read(c)
}
func (connServer *ConnServer) GetInfo() (app string, name string, url string) {
app = connServer.ConnInfo.App
name = connServer.PublishInfo.Name
url = connServer.ConnInfo.TcUrl + "/" + connServer.PublishInfo.Name
return
}
func (connServer *ConnServer) Close(err error) {
connServer.conn.Close()
}