62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type previewChannel struct {
|
|
Name string `json:"name"`
|
|
HlsURL string `json:"hls_url,omitempty"`
|
|
}
|
|
|
|
// handlePreviewChannels returns available video preview channels.
|
|
// GET /v1/preview/channels
|
|
func (s *Server) handlePreviewChannels(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
errorJSON(w, http.StatusMethodNotAllowed, "method not allowed")
|
|
return
|
|
}
|
|
if !s.authorize(r, false) {
|
|
errorJSON(w, http.StatusUnauthorized, "unauthorized")
|
|
return
|
|
}
|
|
|
|
channels := s.discoverChannels(r)
|
|
if channels == nil {
|
|
channels = make([]previewChannel, 0)
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"channels": channels})
|
|
}
|
|
|
|
func (s *Server) discoverChannels(r *http.Request) []previewChannel {
|
|
if s.ms == nil {
|
|
return nil
|
|
}
|
|
_, body, err := s.ms.GetGraphs(r.Context())
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var graphs []struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if err := json.Unmarshal(body, &graphs); err != nil {
|
|
return nil
|
|
}
|
|
|
|
baseURL := "http://" + s.hostname + ":" + strconv.Itoa(s.mediaPort)
|
|
var channels []previewChannel
|
|
for _, g := range graphs {
|
|
channels = append(channels, previewChannel{
|
|
Name: g.Name,
|
|
HlsURL: baseURL + "/hls/" + g.Name + "/index.m3u8",
|
|
})
|
|
}
|
|
return channels
|
|
}
|