-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
548 lines (453 loc) · 11.4 KB
/
conn.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package rtmp
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"github.com/gnolizuh/rtmp/amf"
"io"
"log"
"net"
"sync"
"time"
)
const (
// MaxBasicHeaderSize : Basic Header (1 to 3 bytes)
MaxBasicHeaderSize = 3
// MaxMessageHeaderSize : Message Header (0, 3, 7, or 11 bytes)
MaxMessageHeaderSize = 11
// MaxExtendHeaderSize : Extended Timestamp (0 or 4 bytes)
MaxExtendHeaderSize = 4
// MaxHeaderSize : Chunk header:
// max 3 basic header
// + max 11 message header
// + max 4 extended header (timestamp)
MaxHeaderSize = MaxBasicHeaderSize + MaxMessageHeaderSize + MaxExtendHeaderSize
DefaultReadChunkSize = 128
DefaultSendChunkSize = 4096
MaxStreamsNum = 32
DefaultAckWindowSize = 5000000
DefaultLimitDynamic = 2
DefaultFMSVersion = "FMS/3,0,1,123"
DefaultCapabilities = 31
DefaultMessageStreamID = 1
)
type ServeHandler interface {
serveNew(*Peer) error
serveMessage(MessageType, *Peer) error
}
type MessageHandler func(*Peer) error
type UserMessageHandler func(*Peer) error
type AMFCommandHandler func(*Peer) error
// Header declare.
type Header struct {
fmt uint8
csid uint32
timestamp uint32
mlen uint32
typo MessageType // typo means type, u know why.
msid uint32
}
func NewHeader(typo MessageType, csid uint32) *Header {
return &Header{
typo: typo,
csid: csid,
}
}
// to prevent GC.
var headerSharedPool = sync.Pool{
New: func() interface{} {
hd := make([]byte, MaxHeaderSize)
return &hd
},
}
const (
AudioChunkStream = iota
VideoChunkStream
MaxChunkStream
)
type ChunkStream struct {
active bool
timestamp uint32
csid uint32
dropped uint32
}
// Stream : RTMP stream declare.
type Stream struct {
hdr Header
msg *Message
len uint32
}
// The Conn type represents a RTMP connection.
type Conn struct {
conn net.Conn
// Connection incoming time(microsecond) and incoming time from remote peer.
epoch uint32
incomingEpoch uint32
// State and digest be used in RTMP handshake.
state HandshakeState
digest []byte
// Read and write buffer.
bufReader *bufio.Reader
bufWriter *bufio.Writer
// stream message
streams []Stream
// chunk stream
cs [MaxChunkStream]ChunkStream
// chunk message
chunkSize uint32
chunkPool *sync.Pool
// ack window size
ackWinSize uint32
inBytes uint32
inLastAck uint32
// peer wrapper
peer Peer
// message callback function.
handler ServeHandler
}
// Create new connection from conn.
func newConn(conn net.Conn) *Conn {
c := &Conn{
conn: conn,
chunkSize: DefaultReadChunkSize,
}
c.epoch = uint32(time.Now().UnixNano() / 1000)
c.bufReader = bufio.NewReader(conn)
c.bufWriter = bufio.NewWriter(conn)
c.streams = make([]Stream, MaxStreamsNum)
c.chunkPool = &sync.Pool{
New: func() interface{} {
return newChunk(c.chunkSize)
},
}
c.peer = Peer{
RemoteAddr: c.conn.RemoteAddr().String(),
Conn: c,
}
return c
}
func (c *Conn) setState(state HandshakeState) {
c.state = state
}
// Serve a new connection.
func (c *Conn) serve() {
peer := &c.peer
err := c.handshake()
if err != nil {
log.Print(err)
return
} else {
err := c.handler.serveNew(peer)
if err != nil {
return
}
}
for {
msg, err := c.pumpMessage()
if err != nil {
log.Println(err)
return
} else {
peer.setReader(msg)
err := c.handler.serveMessage(msg.hdr.typo, peer)
if err != nil {
return
}
}
}
}
func (c *Conn) SetChunkSize(chunkSize uint32) {
if c.chunkSize != chunkSize {
c.chunkSize = chunkSize
c.chunkPool = &sync.Pool{
New: func() interface{} {
return newChunk(c.chunkSize)
},
}
}
}
func (c *Conn) getReadChunkSize() uint32 {
return c.chunkSize
}
func (c *Conn) readFull(buf []byte) error {
n, err := io.ReadFull(c.bufReader, buf)
if err != nil {
return err
}
c.inBytes += uint32(n)
if c.inBytes >= 0xf0000000 {
log.Printf("resetting byte counter")
c.inBytes = 0
c.inLastAck = 0
}
if c.ackWinSize > 0 && c.inBytes-c.inLastAck >= c.ackWinSize {
c.inLastAck = c.inBytes
err := c.SendAck(c.inBytes)
if err != nil {
return err
}
}
return nil
}
func (c *Conn) readBasicHeader(b []byte, hdr *Header) (uint32, error) {
off := uint32(0)
err := c.readFull(b[off : off+1])
if err != nil {
return 0, err
}
/*
* Chunk stream IDs 2-63 can be encoded in
* the 1-byte version of this field.
*
* 0 1 2 3 4 5 6 7
* +-+-+-+-+-+-+-+-+
* |fmt| cs id |
* +-+-+-+-+-+-+-+-+
*/
hdr.fmt = uint8(b[off]>>6) & 0x03
hdr.csid = uint32(uint8(b[off]) & 0x3f)
off += 1
if hdr.csid == 0 {
/*
* ID is computed as (the second byte + 64).
*
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |fmt| 0 | cs id - 64 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
hdr.csid = 64
err := c.readFull(b[off : off+1])
if err != nil {
return 0, err
}
hdr.csid += uint32(b[off])
off += 1
} else if hdr.csid == 1 {
/*
* ID is computed as ((the third byte)*256 +
* (the second byte) + 64).
*
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |fmt| 1 | cs id - 64 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
hdr.csid = 64
err := c.readFull(b[off : off+2])
if err != nil {
return 0, err
}
hdr.csid += uint32(b[off])
hdr.csid += 256 * uint32(b[off+1])
off += 2
}
return off, nil
}
func (c *Conn) readChunkMessageHeader(b []byte, hdr *Header) (uint32, error) {
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | timestamp |message length |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | message length (cont) |message type id| msg stream id |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | message stream id (cont) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
off := uint32(0)
if hdr.fmt <= 2 {
err := c.readFull(b[off : off+3])
if err != nil {
return 0, err
}
hdr.timestamp = uint32(b[off+2]) | uint32(b[off+1])<<8 | uint32(b[off])<<16
off += 3
if hdr.fmt <= 1 {
err := c.readFull(b[off : off+4])
if err != nil {
return 0, err
}
hdr.mlen = uint32(b[off+2]) | uint32(b[off+1])<<8 | uint32(b[off])<<16
off += 3
hdr.typo = MessageType(b[off])
off += 1
if hdr.fmt == 0 {
err := c.readFull(b[off : off+4])
if err != nil {
return 0, err
}
hdr.msid = binary.BigEndian.Uint32(b[off : off+4])
off += 4
}
}
}
if hdr.timestamp == 0x00ffffff {
err := c.readFull(b[off : off+4])
if err != nil {
return 0, err
}
hdr.timestamp = binary.BigEndian.Uint32(b[off : off+4])
off += 4
}
return off, nil
}
func (c *Conn) pumpMessage() (*Message, error) {
hdr := Header{}
// alloc shared buffer.
b := headerSharedPool.Get().(*[]byte)
p := *b
// recycle shared buffer.
defer headerSharedPool.Put(b)
// read basic header.
n, err := c.readBasicHeader(p, &hdr)
if err != nil {
return nil, err
}
p = p[n:]
if hdr.csid > MaxStreamsNum {
return nil, errors.New(fmt.Sprintf("RTMP in chunk stream too big: %d >= %d", hdr.csid, MaxStreamsNum))
}
// read chunk message header.
_, err = c.readChunkMessageHeader(p, &hdr)
if err != nil {
return nil, err
}
// indicate timestamp whether is absolute or relate.
stm := c.streams[hdr.csid]
if hdr.fmt > 0 {
stm.hdr.timestamp += hdr.timestamp
} else {
stm.hdr.timestamp = hdr.timestamp
}
// copy temp header to chunk stream header.
stm.hdr.fmt = hdr.fmt
stm.hdr.csid = hdr.csid
if hdr.fmt <= 1 {
stm.hdr.mlen = hdr.mlen
stm.hdr.typo = hdr.typo
if hdr.fmt == 0 {
stm.hdr.msid = hdr.msid
}
}
n = min(stm.hdr.mlen-stm.len, c.getReadChunkSize())
ck := c.chunkPool.Get().(*Chunk)
// TODO: ck need to free
// read message body.
err = c.readFull(ck.Bytes(n))
if err != nil {
return nil, err
}
if stm.msg == nil {
stm.msg = NewMessage(&stm.hdr)
}
stm.msg.appendChunk(ck)
stm.len += n
if stm.hdr.mlen == stm.len {
if hdr.typo >= MessageMax {
return nil, errors.New(fmt.Sprintf("unexpected RTMP message type: %d", hdr.typo))
}
return stm.msg, nil
} else {
return nil, errors.New(fmt.Sprintf("unexpected RTMP message len: %d", stm.len))
}
}
func (c *Conn) SendAck(seq uint32) error {
msg := NewMessage(NewHeader(MessageAck, 2))
msg.alloc(4)
_ = binary.Write(msg, binary.BigEndian, seq)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendAckWinSize(win uint32) error {
msg := NewMessage(NewHeader(MessageWindowAckSize, 2))
msg.alloc(4)
_ = binary.Write(msg, binary.BigEndian, win)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendSetPeerBandwidth(win uint32, limit uint8) error {
msg := NewMessage(NewHeader(MessageSetPeerBandwidth, 2))
msg.alloc(5)
_ = binary.Write(msg, binary.BigEndian, win)
_ = msg.WriteByte(limit)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendSetChunkSize(cs uint32) error {
msg := NewMessage(NewHeader(MessageSetChunkSize, 2))
msg.alloc(4)
_ = binary.Write(msg, binary.BigEndian, cs)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendOnBWDone() error {
msg := NewMessage(NewHeader(MessageAmf0Cmd, 3))
b, _ := amf.Marshal([]interface{}{"onBWDone", 0, nil})
msg.alloc(uint32(len(b)))
_, _ = msg.Write(b)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendConnectResult(trans uint32, encoding uint32) error {
msg := NewMessage(NewHeader(MessageAmf0Cmd, 3))
type Object struct {
FMSVer string `amf:"fmsVer"`
Capabilities uint32 `amf:"capabilities"`
}
type Info struct {
Level string `amf:"level"`
Code string `amf:"code"`
Description string `amf:"description"`
ObjectEncoding uint32 `amf:"objectEncoding"`
}
obj := Object{
FMSVer: DefaultFMSVersion,
Capabilities: DefaultCapabilities,
}
inf := Info{
Level: "status",
Code: "NetConnection.Connect.Success",
Description: "Connection succeeded.",
ObjectEncoding: encoding,
}
b, _ := amf.Marshal([]interface{}{"_result", trans, obj, inf})
msg.alloc(uint32(len(b)))
_, _ = msg.Write(b)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendReleaseStreamResult(trans uint32) error {
msg := NewMessage(NewHeader(MessageAmf0Cmd, 3))
var nullArray []uint32
b, _ := amf.Marshal([]interface{}{"_result", trans, nil, nullArray})
msg.alloc(uint32(len(b)))
_, _ = msg.Write(b)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendOnFCPublish(trans uint32) error {
msg := NewMessage(NewHeader(MessageAmf0Cmd, 3))
b, _ := amf.Marshal([]interface{}{"onFCPublish", trans, nil})
msg.alloc(uint32(len(b)))
_, _ = msg.Write(b)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendFCPublishResult(trans uint32) error {
msg := NewMessage(NewHeader(MessageAmf0Cmd, 3))
var nullArray []uint32
b, _ := amf.Marshal([]interface{}{"_result", trans, nil, nullArray})
msg.alloc(uint32(len(b)))
_, _ = msg.Write(b)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}
func (c *Conn) SendCreateStreamResult(trans uint32, stream uint32) error {
msg := NewMessage(NewHeader(MessageAmf0Cmd, 3))
b, _ := amf.Marshal([]interface{}{"_result", trans, nil, stream})
msg.alloc(uint32(len(b)))
_, _ = msg.Write(b)
_ = msg.prepare(nil)
return msg.Send(c.conn)
}