2022-11-10 11:24:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-09 19:57:15 +00:00
|
|
|
"encoding/json"
|
2022-11-11 05:03:55 +00:00
|
|
|
"fmt"
|
2023-03-09 19:57:15 +00:00
|
|
|
"io/ioutil"
|
2022-11-10 11:24:33 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-11-11 03:40:53 +00:00
|
|
|
"os"
|
2022-11-10 11:24:33 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2022-11-10 12:27:29 +00:00
|
|
|
c *Config
|
2022-11-10 11:24:33 +00:00
|
|
|
managers map[string]*Manager
|
|
|
|
mutex sync.RWMutex
|
|
|
|
close chan string
|
|
|
|
}
|
|
|
|
|
2022-11-10 12:27:29 +00:00
|
|
|
func NewHandler(c *Config) *Handler {
|
|
|
|
h := &Handler{
|
|
|
|
c: c,
|
|
|
|
managers: make(map[string]*Manager),
|
|
|
|
close: make(chan string),
|
|
|
|
}
|
2022-11-11 03:40:53 +00:00
|
|
|
|
|
|
|
// Recreate tempdir
|
2023-03-09 19:57:15 +00:00
|
|
|
os.RemoveAll(c.TempDir)
|
|
|
|
os.MkdirAll(c.TempDir, 0755)
|
2022-11-11 03:40:53 +00:00
|
|
|
|
2022-11-10 11:24:33 +00:00
|
|
|
go h.watchClose()
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
url := r.URL.Path
|
|
|
|
parts := make([]string, 0)
|
|
|
|
|
|
|
|
for _, part := range strings.Split(url, "/") {
|
|
|
|
if part != "" {
|
|
|
|
parts = append(parts, part)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 12:09:35 +00:00
|
|
|
if len(parts) < 3 {
|
|
|
|
log.Println("Invalid URL", url)
|
2022-11-10 11:24:33 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-10 12:09:35 +00:00
|
|
|
streamid := parts[0]
|
2022-11-10 12:27:29 +00:00
|
|
|
path := "/" + strings.Join(parts[1:len(parts)-1], "/")
|
2022-11-10 12:09:35 +00:00
|
|
|
chunk := parts[len(parts)-1]
|
|
|
|
|
2022-11-11 02:20:47 +00:00
|
|
|
// log.Println("Serving", path, streamid, chunk)
|
2022-11-10 11:24:33 +00:00
|
|
|
|
|
|
|
if streamid == "" || chunk == "" || path == "" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 03:23:28 +00:00
|
|
|
manager := h.getManager(path, streamid)
|
2022-11-10 11:24:33 +00:00
|
|
|
if manager == nil {
|
|
|
|
manager = h.createManager(path, streamid)
|
|
|
|
}
|
2022-11-10 12:27:29 +00:00
|
|
|
|
|
|
|
if manager == nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-11-10 11:24:33 +00:00
|
|
|
manager.ServeHTTP(w, r, chunk)
|
|
|
|
}
|
|
|
|
|
2022-11-11 03:23:28 +00:00
|
|
|
func (h *Handler) getManager(path string, streamid string) *Manager {
|
2022-11-10 11:24:33 +00:00
|
|
|
h.mutex.RLock()
|
|
|
|
defer h.mutex.RUnlock()
|
2022-11-11 03:23:28 +00:00
|
|
|
|
|
|
|
m := h.managers[streamid]
|
|
|
|
if m == nil || m.path != path {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return m
|
2022-11-10 11:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) createManager(path string, streamid string) *Manager {
|
2022-11-10 12:27:29 +00:00
|
|
|
manager, err := NewManager(h.c, path, streamid, h.close)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error creating manager", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:54:32 +00:00
|
|
|
h.mutex.Lock()
|
|
|
|
defer h.mutex.Unlock()
|
|
|
|
|
2022-11-11 03:23:28 +00:00
|
|
|
old := h.managers[streamid]
|
|
|
|
if old != nil {
|
|
|
|
old.Destroy()
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:24:33 +00:00
|
|
|
h.managers[streamid] = manager
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) removeManager(streamid string) {
|
|
|
|
h.mutex.Lock()
|
|
|
|
defer h.mutex.Unlock()
|
|
|
|
delete(h.managers, streamid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) watchClose() {
|
|
|
|
for {
|
|
|
|
id := <-h.close
|
|
|
|
if id == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.removeManager(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Close() {
|
|
|
|
h.close <- ""
|
|
|
|
}
|
|
|
|
|
2023-03-09 19:57:15 +00:00
|
|
|
func loadConfig(path string, c *Config) {
|
|
|
|
// load json config
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error when opening file: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(content, &c)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error loading config file", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print loaded config
|
|
|
|
fmt.Printf("%+v\n", c)
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:24:33 +00:00
|
|
|
func main() {
|
2022-11-11 05:03:55 +00:00
|
|
|
if len(os.Args) >= 2 && os.Args[1] == "test" {
|
|
|
|
fmt.Println("test successful")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-09 19:57:15 +00:00
|
|
|
c := &Config{
|
|
|
|
Bind: ":47788",
|
|
|
|
ChunkSize: 3,
|
|
|
|
LookBehind: 5,
|
|
|
|
GoalBufferMin: 1,
|
|
|
|
GoalBufferMax: 4,
|
|
|
|
StreamIdleTime: 60,
|
|
|
|
ManagerIdleTime: 60,
|
2022-11-15 10:09:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 19:57:15 +00:00
|
|
|
// Load config file from second argument
|
|
|
|
if len(os.Args) >= 2 {
|
|
|
|
loadConfig(os.Args[1], c)
|
|
|
|
} else {
|
|
|
|
log.Fatal("Missing config file")
|
2022-11-15 10:09:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 19:57:15 +00:00
|
|
|
if c.FFmpeg == "" || c.FFprobe == "" || c.TempDir == "" {
|
|
|
|
log.Fatal("Missing critical param -- check config file")
|
2023-02-01 03:45:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 19:57:15 +00:00
|
|
|
log.Println("Starting VOD server")
|
2022-11-10 11:24:33 +00:00
|
|
|
|
2023-03-09 19:57:15 +00:00
|
|
|
h := NewHandler(c)
|
2022-11-10 11:24:33 +00:00
|
|
|
http.Handle("/", h)
|
2023-03-09 19:57:15 +00:00
|
|
|
http.ListenAndServe(c.Bind, nil)
|
2022-11-10 11:24:33 +00:00
|
|
|
|
|
|
|
log.Println("Exiting VOD server")
|
|
|
|
h.Close()
|
|
|
|
}
|