2023-12-26 12:41:15 +00:00
|
|
|
// Package api contains the API server.
|
|
|
|
package api
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-09-27 13:18:02 +00:00
|
|
|
"fmt"
|
2021-07-04 16:13:49 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2023-10-07 21:32:15 +00:00
|
|
|
"sort"
|
2023-05-16 17:48:13 +00:00
|
|
|
"strconv"
|
2021-07-04 16:13:49 +00:00
|
|
|
"sync"
|
2023-08-07 15:16:33 +00:00
|
|
|
"time"
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-05-16 13:59:37 +00:00
|
|
|
"github.com/google/uuid"
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
2023-10-31 13:19:04 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/defs"
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2023-10-26 19:46:18 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/protocols/httpserv"
|
2023-10-31 13:19:04 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/restrictnetwork"
|
2021-07-04 16:13:49 +00:00
|
|
|
)
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
func interfaceIsEmpty(i interface{}) bool {
|
|
|
|
return reflect.ValueOf(i).Kind() != reflect.Ptr || reflect.ValueOf(i).IsNil()
|
|
|
|
}
|
|
|
|
|
2023-05-16 17:48:13 +00:00
|
|
|
func paginate2(itemsPtr interface{}, itemsPerPage int, page int) int {
|
|
|
|
ritems := reflect.ValueOf(itemsPtr).Elem()
|
|
|
|
|
|
|
|
itemsLen := ritems.Len()
|
|
|
|
if itemsLen == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:59:51 +00:00
|
|
|
pageCount := (itemsLen / itemsPerPage)
|
|
|
|
if (itemsLen % itemsPerPage) != 0 {
|
|
|
|
pageCount++
|
|
|
|
}
|
2023-05-16 17:48:13 +00:00
|
|
|
|
|
|
|
min := page * itemsPerPage
|
2023-10-30 18:53:16 +00:00
|
|
|
if min > itemsLen {
|
|
|
|
min = itemsLen
|
2023-05-16 17:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
max := (page + 1) * itemsPerPage
|
2023-10-30 18:53:16 +00:00
|
|
|
if max > itemsLen {
|
2023-05-16 17:48:13 +00:00
|
|
|
max = itemsLen
|
|
|
|
}
|
|
|
|
|
|
|
|
ritems.Set(ritems.Slice(min, max))
|
|
|
|
|
|
|
|
return pageCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func paginate(itemsPtr interface{}, itemsPerPageStr string, pageStr string) (int, error) {
|
|
|
|
itemsPerPage := 100
|
|
|
|
|
|
|
|
if itemsPerPageStr != "" {
|
|
|
|
tmp, err := strconv.ParseUint(itemsPerPageStr, 10, 31)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
itemsPerPage = int(tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
page := 0
|
|
|
|
|
|
|
|
if pageStr != "" {
|
|
|
|
tmp, err := strconv.ParseUint(pageStr, 10, 31)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
page = int(tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return paginate2(itemsPtr, itemsPerPage, page), nil
|
|
|
|
}
|
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
func sortedKeys(paths map[string]*conf.Path) []string {
|
2023-10-07 21:32:15 +00:00
|
|
|
ret := make([]string, len(paths))
|
|
|
|
i := 0
|
|
|
|
for name := range paths {
|
|
|
|
ret[i] = name
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
sort.Strings(ret)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-07-11 14:51:23 +00:00
|
|
|
func paramName(ctx *gin.Context) (string, bool) {
|
|
|
|
name := ctx.Param("name")
|
|
|
|
|
|
|
|
if len(name) < 2 || name[0] != '/' {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
return name[1:], true
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// PathManager contains methods used by the API and Metrics server.
|
|
|
|
type PathManager interface {
|
|
|
|
APIPathsList() (*defs.APIPathList, error)
|
|
|
|
APIPathsGet(string) (*defs.APIPath, error)
|
2021-08-11 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// HLSServer contains methods used by the API and Metrics server.
|
|
|
|
type HLSServer interface {
|
2023-12-08 18:17:17 +00:00
|
|
|
APIMuxersList() (*defs.APIHLSMuxerList, error)
|
|
|
|
APIMuxersGet(string) (*defs.APIHLSMuxer, error)
|
2022-12-15 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// RTSPServer contains methods used by the API and Metrics server.
|
|
|
|
type RTSPServer interface {
|
2023-12-08 18:17:17 +00:00
|
|
|
APIConnsList() (*defs.APIRTSPConnsList, error)
|
|
|
|
APIConnsGet(uuid.UUID) (*defs.APIRTSPConn, error)
|
|
|
|
APISessionsList() (*defs.APIRTSPSessionList, error)
|
|
|
|
APISessionsGet(uuid.UUID) (*defs.APIRTSPSession, error)
|
|
|
|
APISessionsKick(uuid.UUID) error
|
2021-08-11 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// RTMPServer contains methods used by the API and Metrics server.
|
|
|
|
type RTMPServer interface {
|
2023-12-08 18:17:17 +00:00
|
|
|
APIConnsList() (*defs.APIRTMPConnList, error)
|
|
|
|
APIConnsGet(uuid.UUID) (*defs.APIRTMPConn, error)
|
|
|
|
APIConnsKick(uuid.UUID) error
|
2023-05-18 13:07:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// SRTServer contains methods used by the API and Metrics server.
|
|
|
|
type SRTServer interface {
|
2023-12-08 18:17:17 +00:00
|
|
|
APIConnsList() (*defs.APISRTConnList, error)
|
|
|
|
APIConnsGet(uuid.UUID) (*defs.APISRTConn, error)
|
|
|
|
APIConnsKick(uuid.UUID) error
|
2021-08-11 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// WebRTCServer contains methods used by the API and Metrics server.
|
|
|
|
type WebRTCServer interface {
|
2023-12-08 18:17:17 +00:00
|
|
|
APISessionsList() (*defs.APIWebRTCSessionList, error)
|
|
|
|
APISessionsGet(uuid.UUID) (*defs.APIWebRTCSession, error)
|
|
|
|
APISessionsKick(uuid.UUID) error
|
2023-07-31 19:20:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
type apiParent interface {
|
2023-05-04 18:16:41 +00:00
|
|
|
logger.Writer
|
2023-12-26 12:41:15 +00:00
|
|
|
APIConfigSet(conf *conf.Conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// API is an API server.
|
|
|
|
type API struct {
|
|
|
|
Address string
|
|
|
|
ReadTimeout conf.StringDuration
|
|
|
|
Conf *conf.Conf
|
|
|
|
PathManager PathManager
|
|
|
|
RTSPServer RTSPServer
|
|
|
|
RTSPSServer RTSPServer
|
|
|
|
RTMPServer RTMPServer
|
|
|
|
RTMPSServer RTMPServer
|
|
|
|
HLSServer HLSServer
|
|
|
|
WebRTCServer WebRTCServer
|
|
|
|
SRTServer SRTServer
|
|
|
|
Parent apiParent
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-07-30 21:03:00 +00:00
|
|
|
httpServer *httpserv.WrappedServer
|
2023-04-11 18:42:40 +00:00
|
|
|
mutex sync.Mutex
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// Initialize initializes API.
|
|
|
|
func (a *API) Initialize() error {
|
2021-07-04 16:13:49 +00:00
|
|
|
router := gin.New()
|
2023-08-13 14:38:23 +00:00
|
|
|
router.SetTrustedProxies(nil) //nolint:errcheck
|
2023-04-09 15:12:50 +00:00
|
|
|
|
2023-08-07 15:16:33 +00:00
|
|
|
group := router.Group("/")
|
2021-11-05 16:18:21 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
group.GET("/v3/config/global/get", a.onConfigGlobalGet)
|
|
|
|
group.PATCH("/v3/config/global/patch", a.onConfigGlobalPatch)
|
|
|
|
|
|
|
|
group.GET("/v3/config/pathdefaults/get", a.onConfigPathDefaultsGet)
|
|
|
|
group.PATCH("/v3/config/pathdefaults/patch", a.onConfigPathDefaultsPatch)
|
|
|
|
|
|
|
|
group.GET("/v3/config/paths/list", a.onConfigPathsList)
|
|
|
|
group.GET("/v3/config/paths/get/*name", a.onConfigPathsGet)
|
|
|
|
group.POST("/v3/config/paths/add/*name", a.onConfigPathsAdd)
|
|
|
|
group.PATCH("/v3/config/paths/patch/*name", a.onConfigPathsPatch)
|
|
|
|
group.POST("/v3/config/paths/replace/*name", a.onConfigPathsReplace)
|
|
|
|
group.DELETE("/v3/config/paths/delete/*name", a.onConfigPathsDelete)
|
|
|
|
|
|
|
|
group.GET("/v3/paths/list", a.onPathsList)
|
|
|
|
group.GET("/v3/paths/get/*name", a.onPathsGet)
|
2021-11-05 16:53:24 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
if !interfaceIsEmpty(a.HLSServer) {
|
2023-10-07 21:32:15 +00:00
|
|
|
group.GET("/v3/hlsmuxers/list", a.onHLSMuxersList)
|
|
|
|
group.GET("/v3/hlsmuxers/get/*name", a.onHLSMuxersGet)
|
2022-12-15 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
if !interfaceIsEmpty(a.RTSPServer) {
|
2023-10-07 21:32:15 +00:00
|
|
|
group.GET("/v3/rtspconns/list", a.onRTSPConnsList)
|
|
|
|
group.GET("/v3/rtspconns/get/:id", a.onRTSPConnsGet)
|
|
|
|
group.GET("/v3/rtspsessions/list", a.onRTSPSessionsList)
|
|
|
|
group.GET("/v3/rtspsessions/get/:id", a.onRTSPSessionsGet)
|
|
|
|
group.POST("/v3/rtspsessions/kick/:id", a.onRTSPSessionsKick)
|
2021-11-05 16:18:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
if !interfaceIsEmpty(a.RTSPSServer) {
|
2023-10-07 21:32:15 +00:00
|
|
|
group.GET("/v3/rtspsconns/list", a.onRTSPSConnsList)
|
|
|
|
group.GET("/v3/rtspsconns/get/:id", a.onRTSPSConnsGet)
|
|
|
|
group.GET("/v3/rtspssessions/list", a.onRTSPSSessionsList)
|
|
|
|
group.GET("/v3/rtspssessions/get/:id", a.onRTSPSSessionsGet)
|
|
|
|
group.POST("/v3/rtspssessions/kick/:id", a.onRTSPSSessionsKick)
|
2021-11-05 16:18:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
if !interfaceIsEmpty(a.RTMPServer) {
|
2023-10-07 21:32:15 +00:00
|
|
|
group.GET("/v3/rtmpconns/list", a.onRTMPConnsList)
|
|
|
|
group.GET("/v3/rtmpconns/get/:id", a.onRTMPConnsGet)
|
|
|
|
group.POST("/v3/rtmpconns/kick/:id", a.onRTMPConnsKick)
|
2021-11-05 16:18:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
if !interfaceIsEmpty(a.RTMPSServer) {
|
2023-10-07 21:32:15 +00:00
|
|
|
group.GET("/v3/rtmpsconns/list", a.onRTMPSConnsList)
|
|
|
|
group.GET("/v3/rtmpsconns/get/:id", a.onRTMPSConnsGet)
|
|
|
|
group.POST("/v3/rtmpsconns/kick/:id", a.onRTMPSConnsKick)
|
2022-08-16 11:53:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
if !interfaceIsEmpty(a.WebRTCServer) {
|
2023-10-07 21:32:15 +00:00
|
|
|
group.GET("/v3/webrtcsessions/list", a.onWebRTCSessionsList)
|
|
|
|
group.GET("/v3/webrtcsessions/get/:id", a.onWebRTCSessionsGet)
|
|
|
|
group.POST("/v3/webrtcsessions/kick/:id", a.onWebRTCSessionsKick)
|
2021-11-05 16:18:21 +00:00
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
if !interfaceIsEmpty(a.SRTServer) {
|
2023-10-07 21:32:15 +00:00
|
|
|
group.GET("/v3/srtconns/list", a.onSRTConnsList)
|
|
|
|
group.GET("/v3/srtconns/get/:id", a.onSRTConnsGet)
|
|
|
|
group.POST("/v3/srtconns/kick/:id", a.onSRTConnsKick)
|
2023-07-31 19:20:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
network, address := restrictnetwork.Restrict("tcp", a.Address)
|
2023-07-30 21:03:00 +00:00
|
|
|
|
2023-05-16 18:12:45 +00:00
|
|
|
var err error
|
2023-07-30 21:03:00 +00:00
|
|
|
a.httpServer, err = httpserv.NewWrappedServer(
|
|
|
|
network,
|
2023-05-16 18:12:45 +00:00
|
|
|
address,
|
2023-12-26 12:41:15 +00:00
|
|
|
time.Duration(a.ReadTimeout),
|
2023-05-16 18:12:45 +00:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
router,
|
2023-08-07 15:16:33 +00:00
|
|
|
a,
|
2023-05-16 18:12:45 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2023-12-26 12:41:15 +00:00
|
|
|
return err
|
2023-01-08 12:23:11 +00:00
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
a.Log(logger.Info, "listener opened on "+address)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
return nil
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// Close closes the API.
|
|
|
|
func (a *API) Close() {
|
2023-05-04 18:16:41 +00:00
|
|
|
a.Log(logger.Info, "listener is closing")
|
2023-07-30 21:03:00 +00:00
|
|
|
a.httpServer.Close()
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 18:17:17 +00:00
|
|
|
// Log implements logger.Writer.
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) Log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
a.Parent.Log(level, "[API] "+format, args...)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 13:18:02 +00:00
|
|
|
// error coming from something the user inserted into the request.
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) writeError(ctx *gin.Context, status int, err error) {
|
2023-10-27 18:43:34 +00:00
|
|
|
// show error in logs
|
2023-09-27 13:18:02 +00:00
|
|
|
a.Log(logger.Error, err.Error())
|
2023-10-27 18:43:34 +00:00
|
|
|
|
|
|
|
// send error in response
|
2023-10-31 13:19:04 +00:00
|
|
|
ctx.JSON(status, &defs.APIError{
|
2023-10-27 18:43:34 +00:00
|
|
|
Error: err.Error(),
|
|
|
|
})
|
2023-09-27 13:18:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigGlobalGet(ctx *gin.Context) {
|
2021-07-04 16:13:49 +00:00
|
|
|
a.mutex.Lock()
|
2023-12-26 12:41:15 +00:00
|
|
|
c := a.Conf
|
2021-07-04 16:13:49 +00:00
|
|
|
a.mutex.Unlock()
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
ctx.JSON(http.StatusOK, c.Global())
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigGlobalPatch(ctx *gin.Context) {
|
2023-10-07 21:32:15 +00:00
|
|
|
var c conf.OptionalGlobal
|
|
|
|
err := json.NewDecoder(ctx.Request.Body).Decode(&c)
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mutex.Lock()
|
2021-09-06 16:39:15 +00:00
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
newConf := a.Conf.Clone()
|
2023-03-31 14:22:08 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
newConf.PatchGlobal(&c)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
err = newConf.Check()
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
a.Conf = newConf
|
2021-08-07 14:07:08 +00:00
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
// since reloading the configuration can cause the shutdown of the API,
|
|
|
|
// call it in a goroutine
|
2023-12-26 12:41:15 +00:00
|
|
|
go a.Parent.APIConfigSet(newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigPathDefaultsGet(ctx *gin.Context) {
|
2023-10-07 21:32:15 +00:00
|
|
|
a.mutex.Lock()
|
2023-12-26 12:41:15 +00:00
|
|
|
c := a.Conf
|
2023-10-07 21:32:15 +00:00
|
|
|
a.mutex.Unlock()
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, c.PathDefaults)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigPathDefaultsPatch(ctx *gin.Context) {
|
2023-10-07 21:32:15 +00:00
|
|
|
var p conf.OptionalPath
|
|
|
|
err := json.NewDecoder(ctx.Request.Body).Decode(&p)
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
newConf := a.Conf.Clone()
|
2023-10-07 21:32:15 +00:00
|
|
|
|
|
|
|
newConf.PatchPathDefaults(&p)
|
|
|
|
|
|
|
|
err = newConf.Check()
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
a.Conf = newConf
|
|
|
|
a.Parent.APIConfigSet(newConf)
|
2023-10-07 21:32:15 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigPathsList(ctx *gin.Context) {
|
2023-10-07 21:32:15 +00:00
|
|
|
a.mutex.Lock()
|
2023-12-26 12:41:15 +00:00
|
|
|
c := a.Conf
|
2023-10-07 21:32:15 +00:00
|
|
|
a.mutex.Unlock()
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
data := &defs.APIPathConfList{
|
2023-10-28 12:53:23 +00:00
|
|
|
Items: make([]*conf.Path, len(c.Paths)),
|
2023-10-07 21:32:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
for i, key := range sortedKeys(c.Paths) {
|
|
|
|
data.Items[i] = c.Paths[key]
|
2023-10-07 21:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data.ItemCount = len(data.Items)
|
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.PageCount = pageCount
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigPathsGet(ctx *gin.Context) {
|
2023-10-07 21:32:15 +00:00
|
|
|
name, ok := paramName(ctx)
|
|
|
|
if !ok {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mutex.Lock()
|
2023-12-26 12:41:15 +00:00
|
|
|
c := a.Conf
|
2023-10-07 21:32:15 +00:00
|
|
|
a.mutex.Unlock()
|
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
p, ok := c.Paths[name]
|
2023-10-07 21:32:15 +00:00
|
|
|
if !ok {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, fmt.Errorf("path configuration not found"))
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, p)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigPathsAdd(ctx *gin.Context) { //nolint:dupl
|
2023-07-11 14:51:23 +00:00
|
|
|
name, ok := paramName(ctx)
|
|
|
|
if !ok {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
|
2021-09-23 07:07:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
var p conf.OptionalPath
|
|
|
|
err := json.NewDecoder(ctx.Request.Body).Decode(&p)
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mutex.Lock()
|
2021-09-06 16:39:15 +00:00
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
newConf := a.Conf.Clone()
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
err = newConf.AddPath(name, &p)
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = newConf.Check()
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
a.Conf = newConf
|
|
|
|
a.Parent.APIConfigSet(newConf)
|
2023-10-07 21:32:15 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigPathsPatch(ctx *gin.Context) { //nolint:dupl
|
2023-10-07 21:32:15 +00:00
|
|
|
name, ok := paramName(ctx)
|
|
|
|
if !ok {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
var p conf.OptionalPath
|
|
|
|
err := json.NewDecoder(ctx.Request.Body).Decode(&p)
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-02 16:43:04 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
2023-06-02 16:43:04 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
newConf := a.Conf.Clone()
|
2021-09-27 12:59:27 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
err = newConf.PatchPath(name, &p)
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-10-07 21:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
err = newConf.Check()
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
a.Conf = newConf
|
|
|
|
a.Parent.APIConfigSet(newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigPathsReplace(ctx *gin.Context) { //nolint:dupl
|
2023-07-11 14:51:23 +00:00
|
|
|
name, ok := paramName(ctx)
|
|
|
|
if !ok {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
|
2021-09-23 07:07:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
var p conf.OptionalPath
|
|
|
|
err := json.NewDecoder(ctx.Request.Body).Decode(&p)
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mutex.Lock()
|
2021-09-06 16:39:15 +00:00
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
newConf := a.Conf.Clone()
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
err = newConf.ReplacePath(name, &p)
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
err = newConf.Check()
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
a.Conf = newConf
|
|
|
|
a.Parent.APIConfigSet(newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onConfigPathsDelete(ctx *gin.Context) {
|
2023-07-11 14:51:23 +00:00
|
|
|
name, ok := paramName(ctx)
|
|
|
|
if !ok {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
|
2021-09-23 07:07:36 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
a.mutex.Lock()
|
2021-09-06 16:39:15 +00:00
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
newConf := a.Conf.Clone()
|
2023-10-07 21:32:15 +00:00
|
|
|
|
|
|
|
err := newConf.RemovePath(name)
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
err = newConf.Check()
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
a.Conf = newConf
|
|
|
|
a.Parent.APIConfigSet(newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onPathsList(ctx *gin.Context) {
|
|
|
|
data, err := a.PathManager.APIPathsList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2022-11-09 17:31:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-18 13:07:47 +00:00
|
|
|
data.PageCount = pageCount
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onPathsGet(ctx *gin.Context) {
|
2023-07-11 14:51:23 +00:00
|
|
|
name, ok := paramName(ctx)
|
|
|
|
if !ok {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
|
2023-07-10 17:53:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.PathManager.APIPathsGet(name)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-16 17:48:13 +00:00
|
|
|
|
2023-05-18 13:07:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, data)
|
2022-11-09 17:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPConnsList(ctx *gin.Context) {
|
|
|
|
data, err := a.RTSPServer.APIConnsList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-18 13:07:47 +00:00
|
|
|
data.PageCount = pageCount
|
2023-05-16 17:48:13 +00:00
|
|
|
|
2023-05-18 13:07:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPConnsGet(ctx *gin.Context) {
|
2023-05-18 13:07:47 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.RTSPServer.APIConnsGet(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPSessionsList(ctx *gin.Context) {
|
|
|
|
data, err := a.RTSPServer.APISessionsList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2021-08-11 15:32:47 +00:00
|
|
|
return
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.PageCount = pageCount
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPSessionsGet(ctx *gin.Context) {
|
2023-05-18 13:07:47 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.RTSPServer.APISessionsGet(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPSessionsKick(ctx *gin.Context) {
|
2023-05-16 13:59:37 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 13:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
err = a.RTSPServer.APISessionsKick(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2021-08-11 15:32:47 +00:00
|
|
|
return
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 15:32:47 +00:00
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPSConnsList(ctx *gin.Context) {
|
|
|
|
data, err := a.RTSPSServer.APIConnsList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2022-11-09 17:31:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.PageCount = pageCount
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPSConnsGet(ctx *gin.Context) {
|
2023-05-18 13:07:47 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.RTSPSServer.APIConnsGet(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
2022-11-09 17:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPSSessionsList(ctx *gin.Context) {
|
|
|
|
data, err := a.RTSPSServer.APISessionsList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2021-08-11 15:32:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-18 13:07:47 +00:00
|
|
|
data.PageCount = pageCount
|
2023-05-16 17:48:13 +00:00
|
|
|
|
2023-05-18 13:07:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPSSessionsGet(ctx *gin.Context) {
|
2023-05-18 13:07:47 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.RTSPSServer.APISessionsGet(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
2021-08-11 15:32:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTSPSSessionsKick(ctx *gin.Context) {
|
2023-05-16 13:59:37 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 13:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-11 15:32:47 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
err = a.RTSPSServer.APISessionsKick(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2021-08-11 15:32:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTMPConnsList(ctx *gin.Context) {
|
|
|
|
data, err := a.RTMPServer.APIConnsList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.PageCount = pageCount
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTMPConnsGet(ctx *gin.Context) {
|
2023-05-18 13:07:47 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.RTMPServer.APIConnsGet(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTMPConnsKick(ctx *gin.Context) {
|
2023-05-16 13:59:37 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 13:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
err = a.RTMPServer.APIConnsKick(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2021-07-04 16:13:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
2021-11-05 16:14:31 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTMPSConnsList(ctx *gin.Context) {
|
|
|
|
data, err := a.RTMPSServer.APIConnsList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2022-08-16 11:53:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-18 13:07:47 +00:00
|
|
|
data.PageCount = pageCount
|
2023-05-16 17:48:13 +00:00
|
|
|
|
2023-05-18 13:07:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTMPSConnsGet(ctx *gin.Context) {
|
2023-05-18 13:07:47 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.RTMPSServer.APIConnsGet(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
2022-08-16 11:53:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onRTMPSConnsKick(ctx *gin.Context) {
|
2023-05-16 13:59:37 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 13:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-16 11:53:04 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
err = a.RTMPSServer.APIConnsKick(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2022-08-16 11:53:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onHLSMuxersList(ctx *gin.Context) {
|
|
|
|
data, err := a.HLSServer.APIMuxersList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2022-12-15 23:50:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-18 13:07:47 +00:00
|
|
|
data.PageCount = pageCount
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onHLSMuxersGet(ctx *gin.Context) {
|
2023-07-11 14:51:23 +00:00
|
|
|
name, ok := paramName(ctx)
|
|
|
|
if !ok {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid name"))
|
2023-07-10 17:53:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.HLSServer.APIMuxersGet(name)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-16 17:48:13 +00:00
|
|
|
|
2023-05-18 13:07:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, data)
|
2022-12-15 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onWebRTCSessionsList(ctx *gin.Context) {
|
|
|
|
data, err := a.WebRTCServer.APISessionsList()
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2021-11-05 16:14:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:05:59 +00:00
|
|
|
data.ItemCount = len(data.Items)
|
2023-05-18 13:07:47 +00:00
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.PageCount = pageCount
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onWebRTCSessionsGet(ctx *gin.Context) {
|
2023-05-18 13:07:47 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
2023-05-16 17:48:13 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 17:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.WebRTCServer.APISessionsGet(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-05-18 13:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
2021-11-05 16:14:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onWebRTCSessionsKick(ctx *gin.Context) {
|
2023-05-16 13:59:37 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-05-16 13:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-15 23:50:47 +00:00
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
err = a.WebRTCServer.APISessionsKick(uuid)
|
2023-05-18 13:07:47 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2022-12-15 23:50:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onSRTConnsList(ctx *gin.Context) {
|
|
|
|
data, err := a.SRTServer.APIConnsList()
|
2023-07-31 19:20:09 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-07-31 19:20:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data.ItemCount = len(data.Items)
|
|
|
|
pageCount, err := paginate(&data.Items, ctx.Query("itemsPerPage"), ctx.Query("page"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-07-31 19:20:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.PageCount = pageCount
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onSRTConnsGet(ctx *gin.Context) {
|
2023-07-31 19:20:09 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-07-31 19:20:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
data, err := a.SRTServer.APIConnsGet(uuid)
|
2023-07-31 19:20:09 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-07-31 19:20:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
func (a *API) onSRTConnsKick(ctx *gin.Context) {
|
2023-07-31 19:20:09 +00:00
|
|
|
uuid, err := uuid.Parse(ctx.Param("id"))
|
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusBadRequest, err)
|
2023-07-31 19:20:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
err = a.SRTServer.APIConnsKick(uuid)
|
2023-07-31 19:20:09 +00:00
|
|
|
if err != nil {
|
2023-10-28 12:53:23 +00:00
|
|
|
a.writeError(ctx, http.StatusInternalServerError, err)
|
2023-07-31 19:20:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:41:15 +00:00
|
|
|
// ReloadConf is called by core.
|
|
|
|
func (a *API) ReloadConf(conf *conf.Conf) {
|
2021-11-05 16:14:31 +00:00
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
2023-12-26 12:41:15 +00:00
|
|
|
a.Conf = conf
|
2021-11-05 16:14:31 +00:00
|
|
|
}
|