forked from muesli/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (150 loc) · 2.94 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"time"
// TODO: switch to joy5?
rtmp "github.com/notedit/rtmp-lib"
"github.com/notedit/rtmp-lib/av"
)
var (
bind = flag.String("bind", ":1935", "bind address")
)
type RTMPConnection struct {
url string
conn *rtmp.Conn
header []av.CodecData
packets chan av.Packet
}
func NewRTMPConnection(u string) *RTMPConnection {
r := &RTMPConnection{
url: u,
}
r.reset()
return r
}
func (r *RTMPConnection) reset() {
r.packets = make(chan av.Packet, 2)
r.conn = nil
r.header = nil
}
func (r *RTMPConnection) Dial() error {
c, err := rtmp.Dial(r.url)
if err != nil {
return err
}
if len(r.header) > 0 {
err = c.WriteHeader(r.header)
if err != nil {
fmt.Println("can't write header:", err)
return err
}
}
fmt.Println("connection established:", r.url)
r.conn = c
return nil
}
func (r *RTMPConnection) Disconnect() error {
err := r.conn.Close()
if err != nil {
return err
}
close(r.packets)
r.reset()
fmt.Println("connection closed:", r.url)
return nil
}
func (r *RTMPConnection) WriteHeader(h []av.CodecData) error {
r.header = h
if r.conn == nil {
return r.Dial()
}
return r.conn.WriteHeader(h)
}
func (r *RTMPConnection) WritePacket(p av.Packet) {
if r.conn == nil {
return
}
r.packets <- p
}
func (r *RTMPConnection) Loop() error {
for p := range r.packets {
if err := r.conn.WritePacket(p); err != nil {
r.conn = nil
fmt.Println(err)
for {
time.Sleep(time.Second)
err := r.Dial()
if err != nil {
fmt.Println("can't re-connect:", err)
continue
}
// successful re-connect
break
}
}
}
return nil
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Println("usage: prism URL [URL...]")
os.Exit(1)
}
fmt.Println("Starting RTMP server...")
config := &rtmp.Config{
ChunkSize: 128,
BufferSize: 0,
}
server := rtmp.NewServer(config)
server.Addr = *bind
conns := make([]*RTMPConnection, len(flag.Args()))
for i, u := range flag.Args() {
conns[i] = NewRTMPConnection(u)
}
server.HandlePublish = func(conn *rtmp.Conn) {
fmt.Println("New connection!")
streams, err := conn.Streams()
if err != nil {
fmt.Println("can't retrieve streams:", err)
os.Exit(1)
}
for _, c := range conns {
if err := c.WriteHeader(streams); err != nil {
fmt.Println("can't write header:", err)
// os.Exit(1)
}
go c.Loop()
}
lastTime := time.Now()
for {
packet, err := conn.ReadPacket()
if err != nil {
fmt.Println("can't read packet:", err)
break
}
if time.Since(lastTime) > time.Second {
fmt.Println("Duration:", packet.Time)
lastTime = time.Now()
}
for _, c := range conns {
c.WritePacket(packet)
}
}
for _, c := range conns {
err := c.Disconnect()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
fmt.Println("Waiting for incoming connection...")
err := server.ListenAndServe()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}