/
server.go
162 lines (133 loc) · 3.13 KB
/
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
package rtmp
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"net"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/flv/amf"
)
func NewServer(conn net.Conn) (*Conn, error) {
c := &Conn{
conn: conn,
rd: bufio.NewReaderSize(conn, core.BufferSize),
wr: conn,
chunks: map[uint8]*header{},
rdPacketSize: 128,
wrPacketSize: 4096,
}
if err := c.serverHandshake(); err != nil {
return nil, err
}
if err := c.writePacketSize(); err != nil {
return nil, err
}
return c, nil
}
func (c *Conn) serverHandshake() error {
b := make([]byte, 1+1536)
// read C0+C1
if _, err := io.ReadFull(c.rd, b); err != nil {
return err
}
// write S0+S1, skip random
if _, err := c.conn.Write(b); err != nil {
return err
}
// read S1, skip check
if _, err := io.ReadFull(c.rd, make([]byte, 1536)); err != nil {
return err
}
// write C1
if _, err := c.conn.Write(b[1:]); err != nil {
return err
}
return nil
}
func (c *Conn) ReadCommands() error {
for {
msgType, _, b, err := c.readMessage()
if err != nil {
return err
}
//log.Printf("%d %.256x", msgType, b)
switch msgType {
case TypeSetPacketSize:
c.rdPacketSize = binary.BigEndian.Uint32(b)
case TypeCommand:
if err = c.acceptCommand(b); err != nil {
return err
}
if c.Intent != "" {
return nil
}
}
}
}
const (
CommandConnect = "connect"
CommandReleaseStream = "releaseStream"
CommandFCPublish = "FCPublish"
CommandCreateStream = "createStream"
CommandPublish = "publish"
CommandPlay = "play"
)
func (c *Conn) acceptCommand(b []byte) error {
items, err := amf.NewReader(b).ReadItems()
if err != nil {
return nil
}
//log.Printf("%#v", items)
if len(items) < 2 {
return fmt.Errorf("rtmp: read command %x", b)
}
cmd, ok := items[0].(string)
if !ok {
return fmt.Errorf("rtmp: read command %x", b)
}
tID, ok := items[1].(float64) // transaction ID
if !ok {
return fmt.Errorf("rtmp: read command %x", b)
}
switch cmd {
case CommandConnect:
if len(items) == 3 {
if v, ok := items[2].(map[string]any); ok {
c.App, _ = v["app"].(string)
}
}
if c.App == "" {
return fmt.Errorf("rtmp: read command %x", b)
}
payload := amf.EncodeItems(
"_result", tID,
map[string]any{"fmsVer": "FMS/3,0,1,123"},
map[string]any{"code": "NetConnection.Connect.Success"},
)
return c.writeMessage(3, TypeCommand, 0, payload)
case CommandReleaseStream:
payload := amf.EncodeItems("_result", tID, nil)
return c.writeMessage(3, TypeCommand, 0, payload)
case CommandCreateStream:
payload := amf.EncodeItems("_result", tID, nil, 1)
return c.writeMessage(3, TypeCommand, 0, payload)
case CommandPublish, CommandPlay: // response later
c.Intent = cmd
c.streamID = 1
case CommandFCPublish: // no response
default:
println("rtmp: unknown command: " + cmd)
}
return nil
}
func (c *Conn) WriteStart() error {
var code string
if c.Intent == CommandPublish {
code = "NetStream.Publish.Start"
} else {
code = "NetStream.Play.Start"
}
payload := amf.EncodeItems("onStatus", 0, nil, map[string]any{"code": code})
return c.writeMessage(3, TypeCommand, 0, payload)
}