2022-11-10 11:24:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-09 19:57:15 +00:00
|
|
|
"encoding/json"
|
2023-10-20 21:24:49 +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"
|
2023-04-10 23:44:39 +00:00
|
|
|
"os/exec"
|
2022-11-10 11:24:33 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2023-04-10 23:44:39 +00:00
|
|
|
const (
|
2023-10-20 23:02:15 +00:00
|
|
|
VERSION = "0.1.19"
|
2023-04-10 23:44:39 +00:00
|
|
|
)
|
|
|
|
|
2022-11-10 11:24:33 +00:00
|
|
|
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
|
2023-10-20 21:12:09 +00:00
|
|
|
server *http.Server
|
|
|
|
exitCode int
|
2022-11-10 11:24:33 +00:00
|
|
|
}
|
|
|
|
|
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),
|
2023-10-20 21:12:09 +00:00
|
|
|
exitCode: 0,
|
2022-11-10 12:27:29 +00:00
|
|
|
}
|
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
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
url := r.URL.Path
|
|
|
|
parts := make([]string, 0)
|
|
|
|
|
2023-03-17 21:06:54 +00:00
|
|
|
// log.Println("Serving", url)
|
|
|
|
|
2023-10-20 21:12:09 +00:00
|
|
|
// Check version if monitoring is enabled
|
|
|
|
if h.c.VersionMonitor {
|
|
|
|
expected := r.Header.Get("X-Go-Vod-Version")
|
|
|
|
if len(expected) > 0 && expected != VERSION {
|
|
|
|
log.Println("Version mismatch", expected, VERSION)
|
|
|
|
|
|
|
|
// Try again in some time
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
|
|
|
|
// Exit with status code 65
|
|
|
|
h.exitCode = 12
|
|
|
|
h.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 21:06:54 +00:00
|
|
|
// Break url into parts
|
2022-11-10 11:24:33 +00:00
|
|
|
for _, part := range strings.Split(url, "/") {
|
|
|
|
if part != "" {
|
|
|
|
parts = append(parts, part)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 21:06:54 +00:00
|
|
|
// Serve actual file from manager
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-17 21:06:54 +00:00
|
|
|
// Get streamid and chunk
|
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]
|
|
|
|
|
2023-03-17 21:06:54 +00:00
|
|
|
// Check if POST request to create temp file
|
|
|
|
if r.Method == "POST" && len(parts) >= 2 && parts[1] == "create" {
|
|
|
|
var err error
|
|
|
|
path, err = h.createTempFile(w, r, parts)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 11:24:33 +00:00
|
|
|
|
2023-04-10 23:44:39 +00:00
|
|
|
// Check if test request
|
|
|
|
if chunk == "test" {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
// check if test file is readable
|
|
|
|
size := 0
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
size = int(info.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
"version": VERSION,
|
|
|
|
"size": size,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if configuration request
|
|
|
|
if r.Method == "POST" && chunk == "config" {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
// read new config
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error reading body", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal config
|
|
|
|
if err := json.Unmarshal(body, h.c); err != nil {
|
|
|
|
log.Println("Error unmarshaling config", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set config as loaded
|
|
|
|
h.c.Configured = true
|
|
|
|
|
|
|
|
// Print loaded config
|
2023-09-29 17:24:41 +00:00
|
|
|
log.Printf("%+v\n", h.c)
|
2023-04-10 23:44:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if configured
|
|
|
|
if !h.c.Configured {
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-17 21:06:54 +00:00
|
|
|
// Check if valid
|
|
|
|
if streamid == "" || path == "" {
|
2022-11-10 11:24:33 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-17 21:06:54 +00:00
|
|
|
// Get existing manager or create new one
|
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
|
|
|
|
2023-03-17 21:06:54 +00:00
|
|
|
// Failed to create manager
|
2022-11-10 12:27:29 +00:00
|
|
|
if manager == nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-03-17 21:06:54 +00:00
|
|
|
|
|
|
|
// Serve chunk if asked for
|
|
|
|
if chunk != "" && chunk != "ignore" {
|
|
|
|
manager.ServeHTTP(w, r, chunk)
|
|
|
|
}
|
2022-11-10 11:24:33 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2023-03-17 21:06:54 +00:00
|
|
|
freeIfTemp(path)
|
2022-11-10 12:27:29 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:44:39 +00:00
|
|
|
// Set config as loaded
|
|
|
|
c.Configured = true
|
|
|
|
|
2023-03-09 19:57:15 +00:00
|
|
|
// Print loaded config
|
2023-09-29 17:24:41 +00:00
|
|
|
log.Printf("%+v\n", c)
|
2023-03-09 19:57:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 11:24:33 +00:00
|
|
|
func main() {
|
2023-10-20 21:12:09 +00:00
|
|
|
// Build initial configuration
|
2023-03-09 19:57:15 +00:00
|
|
|
c := &Config{
|
2023-10-20 21:12:09 +00:00
|
|
|
VersionMonitor: false,
|
2023-03-09 19:57:15 +00:00
|
|
|
Bind: ":47788",
|
|
|
|
ChunkSize: 3,
|
2023-03-15 17:21:44 +00:00
|
|
|
LookBehind: 3,
|
2023-03-09 19:57:15 +00:00
|
|
|
GoalBufferMin: 1,
|
|
|
|
GoalBufferMax: 4,
|
|
|
|
StreamIdleTime: 60,
|
|
|
|
ManagerIdleTime: 60,
|
2022-11-15 10:09:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 21:12:09 +00:00
|
|
|
// Parse arguments
|
|
|
|
for _, arg := range os.Args[1:] {
|
|
|
|
if arg == "-version-monitor" {
|
|
|
|
c.VersionMonitor = true
|
2023-10-20 21:24:49 +00:00
|
|
|
} else if arg == "-version" {
|
|
|
|
fmt.Print("go-vod " + VERSION)
|
2023-10-20 21:12:09 +00:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// Config file
|
|
|
|
loadConfig(arg, c)
|
|
|
|
}
|
2022-11-15 10:09:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 23:44:39 +00:00
|
|
|
// Auto-detect ffmpeg and ffprobe paths
|
|
|
|
if c.FFmpeg == "" || c.FFprobe == "" {
|
|
|
|
ffmpeg, err := exec.LookPath("ffmpeg")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not find ffmpeg")
|
|
|
|
}
|
|
|
|
|
|
|
|
ffprobe, err := exec.LookPath("ffprobe")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not find ffprobe")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.FFmpeg = ffmpeg
|
|
|
|
c.FFprobe = ffprobe
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auto-choose tempdir
|
|
|
|
if c.TempDir == "" {
|
|
|
|
c.TempDir = os.TempDir() + "/go-vod"
|
2023-02-01 03:45:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 23:44:39 +00:00
|
|
|
// Print config
|
2023-09-29 17:24:41 +00:00
|
|
|
log.Printf("%+v\n", c)
|
2023-04-10 23:44:39 +00:00
|
|
|
|
|
|
|
// Start server
|
2023-10-20 21:12:09 +00:00
|
|
|
log.Println("Starting go-vod " + VERSION + " on " + c.Bind)
|
|
|
|
handler := NewHandler(c)
|
|
|
|
server := &http.Server{Addr: c.Bind, Handler: handler}
|
|
|
|
handler.server = server
|
|
|
|
|
|
|
|
// Start listening on different thread
|
|
|
|
go func() {
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error starting server", err)
|
|
|
|
}
|
|
|
|
}()
|
2022-11-10 11:24:33 +00:00
|
|
|
|
2023-10-20 21:12:09 +00:00
|
|
|
// Wait for handler exit
|
|
|
|
handler.watchClose()
|
2022-11-10 11:24:33 +00:00
|
|
|
log.Println("Exiting VOD server")
|
2023-10-20 21:12:09 +00:00
|
|
|
|
|
|
|
// Exit with status code
|
|
|
|
os.Exit(handler.exitCode)
|
2022-11-10 11:24:33 +00:00
|
|
|
}
|