ghostream/web/web.go

108 lines
2.4 KiB
Go
Raw Normal View History

2020-10-14 19:37:05 +00:00
// Package web serves the JavaScript player and WebRTC negotiation
2020-09-21 15:47:31 +00:00
package web
import (
"html/template"
2020-09-28 15:36:40 +00:00
"io/ioutil"
2020-09-21 15:47:31 +00:00
"log"
"net/http"
"os"
2020-09-28 15:36:40 +00:00
"strings"
2020-09-21 17:59:41 +00:00
2020-09-28 15:36:40 +00:00
"github.com/markbates/pkger"
2020-09-23 11:52:12 +00:00
"github.com/pion/webrtc/v3"
2020-10-17 10:38:18 +00:00
"gitlab.crans.org/nounous/ghostream/stream"
2020-09-21 15:47:31 +00:00
)
2020-09-22 09:42:57 +00:00
// Options holds web package configuration
type Options struct {
2020-10-09 20:06:30 +00:00
Enabled bool
2020-10-11 09:04:40 +00:00
CustomCSS string
2020-10-04 15:56:03 +00:00
Favicon string
Hostname string
ListenAddress string
Name string
MapDomainToStream map[string]string
PlayerPoster string
2020-10-04 09:45:16 +00:00
SRTServerPort string
2020-10-04 15:56:03 +00:00
STUNServers []string
ViewersCounterRefreshPeriod int
2020-10-04 15:56:03 +00:00
WidgetURL string
2020-09-22 09:42:57 +00:00
}
2020-09-24 09:24:13 +00:00
var (
cfg *Options
// WebRTC session description channels
remoteSdpChan chan struct {
StreamID string
RemoteDescription webrtc.SessionDescription
}
localSdpChan chan webrtc.SessionDescription
2020-09-24 09:24:13 +00:00
// Preload templates
2020-09-28 15:36:40 +00:00
templates *template.Template
2020-10-17 10:38:18 +00:00
// Streams to get statistics
streams map[string]stream.Stream
2020-09-24 09:24:13 +00:00
)
2020-09-21 15:47:31 +00:00
2020-09-28 15:36:40 +00:00
// Load templates with pkger
// templates will be packed in the compiled binary
func loadTemplates() error {
templates = template.New("")
return pkger.Walk("/web/template", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip non-templates
if info.IsDir() || !strings.HasSuffix(path, ".html") {
return nil
}
// Open file with pkger
f, err := pkger.Open(path)
if err != nil {
return err
}
// Read and parse template
temp, err := ioutil.ReadAll(f)
if err != nil {
return err
}
templates, err = templates.Parse(string(temp))
return err
})
}
2020-09-24 09:24:13 +00:00
// Serve HTTP server
2020-10-17 10:38:18 +00:00
func Serve(s map[string]stream.Stream, rSdpChan chan struct {
StreamID string
RemoteDescription webrtc.SessionDescription
}, lSdpChan chan webrtc.SessionDescription, c *Options) {
2020-10-17 10:38:18 +00:00
streams = s
2020-09-24 09:24:13 +00:00
remoteSdpChan = rSdpChan
localSdpChan = lSdpChan
cfg = c
2020-09-21 18:38:21 +00:00
2020-10-09 20:06:30 +00:00
if !cfg.Enabled {
2020-10-09 21:57:39 +00:00
// Web server is not enabled, ignore
2020-10-09 20:06:30 +00:00
return
}
2020-09-28 15:36:40 +00:00
// Load templates
if err := loadTemplates(); err != nil {
log.Fatalln("Failed to load templates:", err)
}
2020-09-21 15:47:31 +00:00
// Set up HTTP router and server
2020-09-21 19:33:32 +00:00
mux := http.NewServeMux()
2020-09-24 09:24:13 +00:00
mux.HandleFunc("/", viewerHandler)
2020-09-28 16:06:10 +00:00
mux.Handle("/static/", staticHandler())
mux.HandleFunc("/_stats/", statisticsHandler)
2020-09-22 09:42:57 +00:00
log.Printf("HTTP server listening on %s", cfg.ListenAddress)
log.Fatal(http.ListenAndServe(cfg.ListenAddress, mux))
2020-09-21 15:47:31 +00:00
}