2019-03-10 16:42:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-03-21 21:20:50 +01:00
|
|
|
"os/signal"
|
2019-03-10 16:42:12 +01:00
|
|
|
|
|
|
|
"github.com/nareix/joy4/format"
|
|
|
|
"github.com/nareix/joy4/format/rtmp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-03-21 21:20:50 +01:00
|
|
|
addr string
|
|
|
|
sKey string
|
|
|
|
stats streamStats
|
2019-03-10 16:42:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
format.RegisterAll()
|
2019-03-21 21:20:50 +01:00
|
|
|
|
|
|
|
flag.StringVar(&addr, "l", ":8089", "host:port of the MovieNight")
|
|
|
|
flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream")
|
|
|
|
|
|
|
|
stats = newStreamStats()
|
2019-03-10 16:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2019-03-21 21:20:50 +01:00
|
|
|
exit := make(chan bool)
|
|
|
|
go handleInterrupt(exit)
|
|
|
|
|
|
|
|
// Load emotes before starting server.
|
|
|
|
var err error
|
|
|
|
if chat, err = newChatRoom(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if addr != "" {
|
|
|
|
addr = settings.ListenAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
// A stream key was passed on the command line. Use it, but don't save
|
|
|
|
// it over the stream key in the settings.json file.
|
|
|
|
if sKey != "" {
|
|
|
|
settings.SetTempKey(sKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Stream key: ", settings.GetStreamKey())
|
|
|
|
fmt.Println("Admin password: ", settings.AdminPassword)
|
2019-03-23 02:39:55 +01:00
|
|
|
fmt.Println("RoomAccess: ", settings.RoomAccess)
|
|
|
|
fmt.Println("RoomAccessPin: ", settings.RoomAccessPin)
|
2019-03-21 21:20:50 +01:00
|
|
|
fmt.Println("Listen and serve ", addr)
|
|
|
|
|
|
|
|
go startServer()
|
2019-03-23 19:46:49 +01:00
|
|
|
go startRmtpServer()
|
2019-03-21 21:20:50 +01:00
|
|
|
|
|
|
|
<-exit
|
|
|
|
}
|
|
|
|
|
2019-03-23 19:46:49 +01:00
|
|
|
func startRmtpServer() {
|
2019-03-21 21:20:50 +01:00
|
|
|
server := &rtmp.Server{
|
|
|
|
HandlePlay: handlePlay,
|
|
|
|
HandlePublish: handlePublish,
|
|
|
|
}
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error trying to start server: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 16:42:12 +01:00
|
|
|
|
2019-03-21 21:20:50 +01:00
|
|
|
func startServer() {
|
2019-03-10 16:42:12 +01:00
|
|
|
// Chat websocket
|
|
|
|
http.HandleFunc("/ws", wsHandler)
|
2019-03-10 21:45:10 +01:00
|
|
|
http.HandleFunc("/static/js/", wsStaticFiles)
|
2019-03-16 15:13:30 +01:00
|
|
|
http.HandleFunc("/static/css/", wsStaticFiles)
|
2019-03-16 23:11:20 +01:00
|
|
|
http.HandleFunc("/static/img/", wsImages)
|
2019-03-20 15:59:28 +01:00
|
|
|
http.HandleFunc("/static/main.wasm", wsWasmFile)
|
2019-03-10 16:42:12 +01:00
|
|
|
http.HandleFunc("/emotes/", wsEmotes)
|
|
|
|
http.HandleFunc("/favicon.ico", wsStaticFiles)
|
2019-03-12 06:53:51 +01:00
|
|
|
http.HandleFunc("/chat", handleIndexTemplate)
|
|
|
|
http.HandleFunc("/video", handleIndexTemplate)
|
2019-03-16 21:15:45 +01:00
|
|
|
http.HandleFunc("/help", handleHelpTemplate)
|
2019-03-23 02:39:55 +01:00
|
|
|
http.HandleFunc("/pin", handlePin)
|
2019-03-10 16:42:12 +01:00
|
|
|
|
2019-03-12 17:37:32 +01:00
|
|
|
http.HandleFunc("/", handleDefault)
|
2019-03-10 16:42:12 +01:00
|
|
|
|
2019-03-21 21:20:50 +01:00
|
|
|
err := http.ListenAndServe(addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error trying to start rmtp server: %v\n", err)
|
2019-03-10 16:42:12 +01:00
|
|
|
}
|
2019-03-21 21:20:50 +01:00
|
|
|
}
|
2019-03-10 16:42:12 +01:00
|
|
|
|
2019-03-21 21:20:50 +01:00
|
|
|
func handleInterrupt(exit chan bool) {
|
|
|
|
ch := make(chan os.Signal)
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
|
|
|
<-ch
|
|
|
|
fmt.Println("Closing server")
|
|
|
|
if settings.StreamStats {
|
|
|
|
stats.Print()
|
2019-03-12 04:45:23 +01:00
|
|
|
}
|
2019-03-21 21:20:50 +01:00
|
|
|
exit <- true
|
2019-03-10 16:42:12 +01:00
|
|
|
}
|