2019-03-10 16:42:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-03-26 16:41:31 +01:00
|
|
|
"context"
|
2019-03-10 16:42:12 +01:00
|
|
|
"flag"
|
2019-03-26 19:30:52 +01:00
|
|
|
"fmt"
|
2019-03-10 16:42:12 +01:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-03-21 21:20:50 +01:00
|
|
|
"os/signal"
|
2019-03-26 16:41:31 +01:00
|
|
|
"time"
|
2019-03-10 16:42:12 +01:00
|
|
|
|
|
|
|
"github.com/nareix/joy4/format"
|
|
|
|
"github.com/nareix/joy4/format/rtmp"
|
2019-03-24 23:51:39 +01:00
|
|
|
"github.com/zorchenhimer/MovieNight/common"
|
2019-03-10 16:42:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-03-26 16:41:31 +01:00
|
|
|
addr string
|
|
|
|
sKey string
|
|
|
|
stats = newStreamStats()
|
|
|
|
chatServer *http.Server
|
2019-03-10 16:42:12 +01:00
|
|
|
)
|
|
|
|
|
2019-03-26 16:44:19 +01:00
|
|
|
func setupSettings() error {
|
2019-03-25 04:54:24 +01:00
|
|
|
var err error
|
|
|
|
settings, err = LoadSettings("settings.json")
|
|
|
|
if err != nil {
|
2019-03-26 16:44:19 +01:00
|
|
|
return fmt.Errorf("Unable to load settings: %s", err)
|
2019-03-25 04:54:24 +01:00
|
|
|
}
|
|
|
|
if len(settings.StreamKey) == 0 {
|
2019-03-26 16:44:19 +01:00
|
|
|
return fmt.Errorf("Missing stream key is settings.json")
|
2019-03-25 04:54:24 +01:00
|
|
|
}
|
2019-03-21 21:20:50 +01:00
|
|
|
|
2019-03-25 04:54:24 +01:00
|
|
|
if err = settings.SetupLogging(); err != nil {
|
2019-03-26 16:44:19 +01:00
|
|
|
return fmt.Errorf("Unable to setup logger: %s", err)
|
2019-03-25 04:54:24 +01:00
|
|
|
}
|
2019-03-21 21:20:50 +01:00
|
|
|
|
2019-03-25 04:54:24 +01:00
|
|
|
// Save admin password to file
|
|
|
|
if err = settings.Save(); err != nil {
|
2019-03-26 16:44:19 +01:00
|
|
|
return fmt.Errorf("Unable to save settings: %s", err)
|
2019-03-25 04:54:24 +01:00
|
|
|
}
|
2019-03-26 16:44:19 +01:00
|
|
|
|
|
|
|
return nil
|
2019-03-10 16:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-03-25 04:54:24 +01:00
|
|
|
flag.StringVar(&addr, "l", ":8089", "host:port of the MovieNight")
|
|
|
|
flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream")
|
2019-03-10 16:42:12 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2019-03-25 04:54:24 +01:00
|
|
|
format.RegisterAll()
|
|
|
|
|
2019-03-26 16:44:19 +01:00
|
|
|
if err := setupSettings(); err != nil {
|
|
|
|
fmt.Printf("Error loading settings: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-03-25 04:54:24 +01:00
|
|
|
|
2019-03-21 21:20:50 +01:00
|
|
|
// Load emotes before starting server.
|
|
|
|
var err error
|
|
|
|
if chat, err = newChatRoom(); err != nil {
|
2019-03-24 23:51:39 +01:00
|
|
|
common.LogErrorln(err)
|
2019-03-21 21:20:50 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-03-24 23:51:39 +01:00
|
|
|
common.LogInfoln("Stream key: ", settings.GetStreamKey())
|
|
|
|
common.LogInfoln("Admin password: ", settings.AdminPassword)
|
|
|
|
common.LogInfoln("Listen and serve ", addr)
|
2019-03-21 21:20:50 +01:00
|
|
|
|
|
|
|
server := &rtmp.Server{
|
|
|
|
HandlePlay: handlePlay,
|
|
|
|
HandlePublish: handlePublish,
|
|
|
|
}
|
2019-03-26 16:41:31 +01:00
|
|
|
chatServer := &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
MaxHeaderBytes: 1 << 20,
|
2019-03-21 21:20:50 +01:00
|
|
|
}
|
2019-03-10 16:42:12 +01:00
|
|
|
|
2019-03-26 16:41:31 +01:00
|
|
|
chatServer.RegisterOnShutdown(func() { chat.Shutdown() })
|
|
|
|
//server.RegisterOnShutdown(func() { common.LogDebugln("server shutdown callback called.") })
|
2019-03-10 16:42:12 +01:00
|
|
|
|
2019-03-26 16:41:31 +01:00
|
|
|
// Signal handler
|
|
|
|
exit := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
ch := make(chan os.Signal)
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
|
|
|
<-ch
|
|
|
|
common.LogInfoln("Closing server")
|
|
|
|
if settings.StreamStats {
|
|
|
|
stats.Print()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := chatServer.Shutdown(context.Background()); err != nil {
|
|
|
|
common.LogErrorf("Error shutting down chat server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
common.LogInfoln("Shutdown() sent. Sending exit.")
|
|
|
|
exit <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Chat and HTTP server
|
|
|
|
go func() {
|
|
|
|
// Chat websocket
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/ws", wsHandler)
|
|
|
|
mux.HandleFunc("/static/js/", wsStaticFiles)
|
|
|
|
mux.HandleFunc("/static/css/", wsStaticFiles)
|
|
|
|
mux.HandleFunc("/static/img/", wsImages)
|
|
|
|
mux.HandleFunc("/static/main.wasm", wsWasmFile)
|
|
|
|
mux.HandleFunc("/emotes/", wsEmotes)
|
|
|
|
mux.HandleFunc("/favicon.ico", wsStaticFiles)
|
|
|
|
mux.HandleFunc("/chat", handleIndexTemplate)
|
|
|
|
mux.HandleFunc("/video", handleIndexTemplate)
|
|
|
|
mux.HandleFunc("/help", handleHelpTemplate)
|
|
|
|
|
|
|
|
mux.HandleFunc("/", handleDefault)
|
|
|
|
|
|
|
|
chatServer.Handler = mux
|
|
|
|
err := chatServer.ListenAndServe()
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
// If the server cannot start, don't pretend we can continue.
|
|
|
|
panic("Error trying to start chat/http server: " + err.Error())
|
|
|
|
}
|
|
|
|
common.LogDebugln("ChatServer closed.")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// RTMP server
|
|
|
|
go func() {
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
// If the server cannot start, don't pretend we can continue.
|
|
|
|
panic("Error trying to start rtmp server: " + err.Error())
|
|
|
|
}
|
|
|
|
common.LogDebugln("RTMP server closed.")
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-exit
|
2019-03-10 16:42:12 +01:00
|
|
|
}
|