forked from fragforce/fragcenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragcenter.go
233 lines (197 loc) · 4.83 KB
/
fragcenter.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
package main
import (
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"
)
var (
streamHost string
streamPort string
webPort string
pollInterval int
)
//LiveStreams is the datastructure of the stats xml page
type LiveStreams struct {
Applications []struct {
Name string `xml:"name"`
Live struct {
Streams []struct {
Name string `xml:"name"`
BWIn int `xml:"bw_in"`
} `xml:"stream"`
} `xml:"live"`
} `xml:"server>application"`
}
func main() {
time.Sleep(2 * time.Second)
host, b := os.LookupEnv("STREAMHOST")
if b {
streamHost = host
} else {
streamHost = *flag.String("host", "127.0.0.1", "Host that the rtmp server is running on.")
}
port, b := os.LookupEnv("STREAMPORT")
if b {
streamPort = port
} else {
streamPort = *flag.String("port", "8080", "Port the rtmp server is outputting http traffic")
}
web, b := os.LookupEnv("WEBPORT")
if b {
webPort = web
} else {
webPort = *flag.String("web", "3000", "Port the webserver runs on.")
}
poll, b := os.LookupEnv("POLL")
if b {
pollInt, err := strconv.Atoi(poll)
if err != nil {
fmt.Println("poll interval is not an integer")
}
pollInterval = pollInt
} else {
pollInterval = *flag.Int("poll", 10, "Polling interval")
}
flag.Parse()
fmt.Printf("Monitoring RTMP host %s:%s for live streams.\n", streamHost, streamPort)
fmt.Printf("Starting stats checker, polling every %d seconds.\n", pollInterval)
go statsCheck(streamHost, streamPort, pollInterval)
fmt.Printf("Fragcenter is now running on port %s. Hit 'ctrl + c' to stop.\n", webPort)
http.Handle("/", http.FileServer(http.Dir("./public")))
http.ListenAndServe(fmt.Sprintf(":%s", webPort), nil)
}
func marshalLiveStream(body []byte) (*LiveStreams, error) {
var streams LiveStreams
err := xml.Unmarshal(body, &streams)
if err != nil {
return nil, err
}
return &streams, nil
}
func statsCheck(host, port string, pollInterval int) {
url := fmt.Sprintf("http://%s:%s/stats", host, port)
for {
fmt.Println("Checking Stats...")
resp, err := http.Get(url)
if err != nil {
log.Fatal("Problem getting stats page.\n", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic("Couldn't read the response body.")
}
liveStreams, err := marshalLiveStream(body)
if err != nil {
panic("Couldn't marshal the XML body to a struct.")
}
var active []string
for _, application := range liveStreams.Applications {
if application.Name == "stream" {
for _, stream := range application.Live.Streams {
if stream.BWIn == 0 {
fmt.Printf("Stream '%s' is stopped. Ignoring.\n", stream.Name)
continue
}
active = append(active, stream.Name)
fmt.Printf("Found live stream '%s'.\n", stream.Name)
}
}
}
sort.Strings(active)
writeHTML(active, host, port)
time.Sleep(time.Duration(pollInterval) * time.Second)
}
}
func fileCheck() {
for {
time.Sleep(10 * time.Second)
files, err := ioutil.ReadDir("/tmp/rtmp/active")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
if !f.IsDir() {
fmt.Println(strings.TrimSuffix(f.Name(), ".m3u8"))
}
}
}
}
func writeHTML(streams []string, host string, port string) error {
var bodyLines []string
htmlBody := ""
htmlStart := `<html>
<head>
<title>Fragcenter</title>
<script src="https://cdn.dashjs.org/latest/dash.all.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function getPage(){
var result;
$.ajax({
url: 'index.html',
type: 'get',
async: false,
success: function(data) {
result = data;
}
});
return result;
}
current = getPage();
function checkChanges(){
check = getPage();
if ( check != current) {
location.reload();
};
}
setInterval(checkChanges, 10000);
</script>
<style>
video {
width: 100%;
padding-left: 1%;
padding-right: 1%;
}
#container {
width: 30%;
padding-left: 1%;
padding-right: 1%;
float: left;
}
</style>
</head>
<body style="background-color:slategray;">
<div align="center">
`
htmlEnd := `
</div>
</body>
</html>`
baseVideo := ` <div id="container">
<video data-dashjs-player autoplay muted src="http://<streamHost>:<streamPort>/dash/<streamName>/index.mpd"></video>
<br/>
<q><streamName></q>
</div>`
for count, name := range streams {
if count < 3 {
bodyLines = append(bodyLines, strings.Replace(strings.Replace(strings.Replace(baseVideo, "<streamName>", name, -1), "<streamHost>", host, -1), "<streamPort>", port, -1))
}
}
htmlBody = strings.Join(bodyLines, "\n")
fo, err := os.Create("./public/index.html")
if err != nil {
return err
}
defer fo.Close()
fo.WriteString(htmlStart + htmlBody + htmlEnd)
fo.Close()
return nil
}