2021-07-04 16:13:49 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-08-07 13:09:01 +00:00
|
|
|
"net/http/httputil"
|
2021-07-04 16:13:49 +00:00
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
|
|
)
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
func interfaceIsEmpty(i interface{}) bool {
|
|
|
|
return reflect.ValueOf(i).Kind() != reflect.Ptr || reflect.ValueOf(i).IsNil()
|
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
func fillStruct(dest interface{}, source interface{}) {
|
|
|
|
rvsource := reflect.ValueOf(source)
|
|
|
|
rvdest := reflect.ValueOf(dest)
|
|
|
|
nf := rvsource.NumField()
|
|
|
|
for i := 0; i < nf; i++ {
|
|
|
|
fnew := rvsource.Field(i)
|
|
|
|
if !fnew.IsNil() {
|
|
|
|
f := rvdest.Elem().FieldByName(rvsource.Type().Field(i).Name)
|
|
|
|
if f.Kind() == reflect.Ptr {
|
|
|
|
f.Set(fnew)
|
|
|
|
} else {
|
|
|
|
f.Set(fnew.Elem())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneStruct(dest interface{}, source interface{}) {
|
2021-09-27 12:59:27 +00:00
|
|
|
enc, _ := json.Marshal(source)
|
|
|
|
_ = json.Unmarshal(enc, dest)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfData(ctx *gin.Context) (interface{}, error) {
|
|
|
|
var in struct {
|
|
|
|
// general
|
2021-09-27 12:59:27 +00:00
|
|
|
LogLevel *conf.LogLevel `json:"logLevel"`
|
|
|
|
LogDestinations *conf.LogDestinations `json:"logDestinations"`
|
|
|
|
LogFile *string `json:"logFile"`
|
|
|
|
ReadTimeout *conf.StringDuration `json:"readTimeout"`
|
|
|
|
WriteTimeout *conf.StringDuration `json:"writeTimeout"`
|
|
|
|
ReadBufferCount *int `json:"readBufferCount"`
|
|
|
|
API *bool `json:"api"`
|
|
|
|
APIAddress *string `json:"apiAddress"`
|
|
|
|
Metrics *bool `json:"metrics"`
|
|
|
|
MetricsAddress *string `json:"metricsAddress"`
|
|
|
|
PPROF *bool `json:"pprof"`
|
|
|
|
PPROFAddress *string `json:"pprofAddress"`
|
|
|
|
RunOnConnect *string `json:"runOnConnect"`
|
|
|
|
RunOnConnectRestart *bool `json:"runOnConnectRestart"`
|
|
|
|
|
|
|
|
// RTSP
|
|
|
|
RTSPDisable *bool `json:"rtspDisable"`
|
|
|
|
Protocols *conf.Protocols `json:"protocols"`
|
|
|
|
Encryption *conf.Encryption `json:"encryption"`
|
|
|
|
RTSPAddress *string `json:"rtspAddress"`
|
|
|
|
RTSPSAddress *string `json:"rtspsAddress"`
|
|
|
|
RTPAddress *string `json:"rtpAddress"`
|
|
|
|
RTCPAddress *string `json:"rtcpAddress"`
|
|
|
|
MulticastIPRange *string `json:"multicastIPRange"`
|
|
|
|
MulticastRTPPort *int `json:"multicastRTPPort"`
|
|
|
|
MulticastRTCPPort *int `json:"multicastRTCPPort"`
|
|
|
|
ServerKey *string `json:"serverKey"`
|
|
|
|
ServerCert *string `json:"serverCert"`
|
|
|
|
AuthMethods *conf.AuthMethods `json:"authMethods"`
|
|
|
|
ReadBufferSize *int `json:"readBufferSize"`
|
|
|
|
|
|
|
|
// RTMP
|
2021-07-04 16:13:49 +00:00
|
|
|
RTMPDisable *bool `json:"rtmpDisable"`
|
|
|
|
RTMPAddress *string `json:"rtmpAddress"`
|
|
|
|
|
2021-09-27 12:59:27 +00:00
|
|
|
// HLS
|
2021-09-26 21:06:40 +00:00
|
|
|
HLSDisable *bool `json:"hlsDisable"`
|
|
|
|
HLSAddress *string `json:"hlsAddress"`
|
|
|
|
HLSAlwaysRemux *bool `json:"hlsAlwaysRemux"`
|
|
|
|
HLSSegmentCount *int `json:"hlsSegmentCount"`
|
|
|
|
HLSSegmentDuration *conf.StringDuration `json:"hlsSegmentDuration"`
|
|
|
|
HLSAllowOrigin *string `json:"hlsAllowOrigin"`
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
err := json.NewDecoder(ctx.Request.Body).Decode(&in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return in, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfPathData(ctx *gin.Context) (interface{}, error) {
|
|
|
|
var in struct {
|
|
|
|
// source
|
2021-09-26 21:06:40 +00:00
|
|
|
Source *string `json:"source"`
|
2021-09-27 12:59:27 +00:00
|
|
|
SourceProtocol *conf.SourceProtocol `json:"sourceProtocol"`
|
2021-09-26 21:06:40 +00:00
|
|
|
SourceAnyPortEnable *bool `json:"sourceAnyPortEnable"`
|
|
|
|
SourceFingerprint *string `json:"sourceFingerprint"`
|
|
|
|
SourceOnDemand *bool `json:"sourceOnDemand"`
|
|
|
|
SourceOnDemandStartTimeout *conf.StringDuration `json:"sourceOnDemandStartTimeout"`
|
|
|
|
SourceOnDemandCloseAfter *conf.StringDuration `json:"sourceOnDemandCloseAfter"`
|
|
|
|
SourceRedirect *string `json:"sourceRedirect"`
|
|
|
|
DisablePublisherOverride *bool `json:"disablePublisherOverride"`
|
|
|
|
Fallback *string `json:"fallback"`
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
// authentication
|
2021-09-27 13:45:51 +00:00
|
|
|
PublishUser *conf.Credential `json:"publishUser"`
|
|
|
|
PublishPass *conf.Credential `json:"publishPass"`
|
|
|
|
PublishIPs *conf.IPsOrNets `json:"publishIPs"`
|
|
|
|
ReadUser *conf.Credential `json:"readUser"`
|
|
|
|
ReadPass *conf.Credential `json:"readPass"`
|
|
|
|
ReadIPs *conf.IPsOrNets `json:"readIPs"`
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
// custom commands
|
2021-09-26 21:06:40 +00:00
|
|
|
RunOnInit *string `json:"runOnInit"`
|
|
|
|
RunOnInitRestart *bool `json:"runOnInitRestart"`
|
|
|
|
RunOnDemand *string `json:"runOnDemand"`
|
|
|
|
RunOnDemandRestart *bool `json:"runOnDemandRestart"`
|
|
|
|
RunOnDemandStartTimeout *conf.StringDuration `json:"runOnDemandStartTimeout"`
|
|
|
|
RunOnDemandCloseAfter *conf.StringDuration `json:"runOnDemandCloseAfter"`
|
|
|
|
RunOnPublish *string `json:"runOnPublish"`
|
|
|
|
RunOnPublishRestart *bool `json:"runOnPublishRestart"`
|
|
|
|
RunOnRead *string `json:"runOnRead"`
|
|
|
|
RunOnReadRestart *bool `json:"runOnReadRestart"`
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
err := json.NewDecoder(ctx.Request.Body).Decode(&in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return in, err
|
|
|
|
}
|
|
|
|
|
2021-08-11 10:45:53 +00:00
|
|
|
type apiPathManager interface {
|
2021-11-05 16:53:24 +00:00
|
|
|
onAPIPathsList(req pathAPIPathsListReq) pathAPIPathsListRes
|
2021-08-11 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type apiRTSPServer interface {
|
2021-11-05 16:53:24 +00:00
|
|
|
onAPISessionsList(req rtspServerAPISessionsListReq) rtspServerAPISessionsListRes
|
|
|
|
onAPISessionsKick(req rtspServerAPISessionsKickReq) rtspServerAPISessionsKickRes
|
2021-08-11 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type apiRTMPServer interface {
|
2021-11-05 16:53:24 +00:00
|
|
|
onAPIConnsList(req rtmpServerAPIConnsListReq) rtmpServerAPIConnsListRes
|
|
|
|
onAPIConnsKick(req rtmpServerAPIConnsKickReq) rtmpServerAPIConnsKickRes
|
2021-08-11 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 16:14:31 +00:00
|
|
|
type apiHLSServer interface {
|
2021-11-06 11:52:12 +00:00
|
|
|
onAPIHLSMuxersList(req hlsServerAPIMuxersListReq) hlsServerAPIMuxersListRes
|
2021-11-05 16:14:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
type apiParent interface {
|
|
|
|
Log(logger.Level, string, ...interface{})
|
2021-10-27 19:01:00 +00:00
|
|
|
onAPIConfigSet(conf *conf.Conf)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type api struct {
|
|
|
|
conf *conf.Conf
|
2021-08-11 10:45:53 +00:00
|
|
|
pathManager apiPathManager
|
|
|
|
rtspServer apiRTSPServer
|
|
|
|
rtspsServer apiRTSPServer
|
|
|
|
rtmpServer apiRTMPServer
|
2021-11-05 16:14:31 +00:00
|
|
|
hlsServer apiHLSServer
|
2021-07-04 16:13:49 +00:00
|
|
|
parent apiParent
|
|
|
|
|
|
|
|
mutex sync.Mutex
|
|
|
|
s *http.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAPI(
|
|
|
|
address string,
|
|
|
|
conf *conf.Conf,
|
2021-08-11 10:45:53 +00:00
|
|
|
pathManager apiPathManager,
|
|
|
|
rtspServer apiRTSPServer,
|
|
|
|
rtspsServer apiRTSPServer,
|
|
|
|
rtmpServer apiRTMPServer,
|
2021-11-05 16:14:31 +00:00
|
|
|
hlsServer apiHLSServer,
|
2021-07-04 16:13:49 +00:00
|
|
|
parent apiParent,
|
|
|
|
) (*api, error) {
|
|
|
|
ln, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a := &api{
|
|
|
|
conf: conf,
|
|
|
|
pathManager: pathManager,
|
|
|
|
rtspServer: rtspServer,
|
|
|
|
rtspsServer: rtspsServer,
|
|
|
|
rtmpServer: rtmpServer,
|
2021-11-05 16:14:31 +00:00
|
|
|
hlsServer: hlsServer,
|
2021-07-04 16:13:49 +00:00
|
|
|
parent: parent,
|
|
|
|
}
|
|
|
|
|
|
|
|
router := gin.New()
|
2021-08-14 09:00:56 +00:00
|
|
|
router.NoRoute(a.mwLog)
|
2021-08-07 13:09:01 +00:00
|
|
|
group := router.Group("/", a.mwLog)
|
2021-11-05 16:18:21 +00:00
|
|
|
|
2021-08-07 16:28:27 +00:00
|
|
|
group.GET("/v1/config/get", a.onConfigGet)
|
|
|
|
group.POST("/v1/config/set", a.onConfigSet)
|
2021-09-23 07:07:36 +00:00
|
|
|
group.POST("/v1/config/paths/add/*name", a.onConfigPathsAdd)
|
|
|
|
group.POST("/v1/config/paths/edit/*name", a.onConfigPathsEdit)
|
|
|
|
group.POST("/v1/config/paths/remove/*name", a.onConfigPathsDelete)
|
2021-11-05 16:53:24 +00:00
|
|
|
|
2021-08-07 16:28:27 +00:00
|
|
|
group.GET("/v1/paths/list", a.onPathsList)
|
2021-11-05 16:18:21 +00:00
|
|
|
|
|
|
|
if !interfaceIsEmpty(a.rtspServer) {
|
|
|
|
group.GET("/v1/rtspsessions/list", a.onRTSPSessionsList)
|
|
|
|
group.POST("/v1/rtspsessions/kick/:id", a.onRTSPSessionsKick)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !interfaceIsEmpty(a.rtspsServer) {
|
|
|
|
group.GET("/v1/rtspssessions/list", a.onRTSPSSessionsList)
|
|
|
|
group.POST("/v1/rtspssessions/kick/:id", a.onRTSPSSessionsKick)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !interfaceIsEmpty(a.rtmpServer) {
|
|
|
|
group.GET("/v1/rtmpconns/list", a.onRTMPConnsList)
|
|
|
|
group.POST("/v1/rtmpconns/kick/:id", a.onRTMPConnsKick)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !interfaceIsEmpty(a.hlsServer) {
|
|
|
|
group.GET("/v1/hlsmuxers/list", a.onHLSMuxersList)
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2021-10-17 14:49:49 +00:00
|
|
|
a.s = &http.Server{Handler: router}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
go a.s.Serve(ln)
|
|
|
|
|
|
|
|
a.log(logger.Info, "listener opened on "+address)
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) close() {
|
|
|
|
a.s.Shutdown(context.Background())
|
2021-11-15 19:13:54 +00:00
|
|
|
a.log(logger.Info, "listener closed")
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
a.parent.Log(level, "[API] "+format, args...)
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:09:01 +00:00
|
|
|
func (a *api) mwLog(ctx *gin.Context) {
|
2021-10-17 15:05:22 +00:00
|
|
|
a.log(logger.Info, "[conn %v] %s %s", ctx.Request.RemoteAddr, ctx.Request.Method, ctx.Request.URL.Path)
|
|
|
|
|
2021-08-07 13:09:01 +00:00
|
|
|
byts, _ := httputil.DumpRequest(ctx.Request, true)
|
2021-10-17 15:05:22 +00:00
|
|
|
a.log(logger.Debug, "[conn %v] [c->s] %s", ctx.Request.RemoteAddr, string(byts))
|
2021-08-07 13:09:01 +00:00
|
|
|
|
2021-10-17 15:02:15 +00:00
|
|
|
logw := &httpLogWriter{ResponseWriter: ctx.Writer}
|
|
|
|
ctx.Writer = logw
|
2021-08-07 13:09:01 +00:00
|
|
|
|
|
|
|
ctx.Writer.Header().Set("Server", "rtsp-simple-server")
|
|
|
|
|
2021-10-17 14:51:35 +00:00
|
|
|
ctx.Next()
|
|
|
|
|
2021-10-17 15:05:22 +00:00
|
|
|
a.log(logger.Debug, "[conn %v] [s->c] %s", ctx.Request.RemoteAddr, logw.dump())
|
2021-08-07 13:09:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
func (a *api) onConfigGet(ctx *gin.Context) {
|
|
|
|
a.mutex.Lock()
|
|
|
|
c := a.conf
|
|
|
|
a.mutex.Unlock()
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onConfigSet(ctx *gin.Context) {
|
|
|
|
in, err := loadConfData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mutex.Lock()
|
2021-09-06 16:39:15 +00:00
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
var newConf conf.Conf
|
2021-09-27 12:59:27 +00:00
|
|
|
cloneStruct(&newConf, a.conf)
|
2021-07-04 16:13:49 +00:00
|
|
|
fillStruct(&newConf, in)
|
|
|
|
|
|
|
|
err = newConf.CheckAndFillMissing()
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:07:08 +00:00
|
|
|
a.conf = &newConf
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
// since reloading the configuration can cause the shutdown of the API,
|
|
|
|
// call it in a goroutine
|
2021-10-27 19:01:00 +00:00
|
|
|
go a.parent.onAPIConfigSet(&newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onConfigPathsAdd(ctx *gin.Context) {
|
2021-09-23 07:07:36 +00:00
|
|
|
name := ctx.Param("name")
|
|
|
|
if len(name) < 2 || name[0] != '/' {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name = name[1:]
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
in, err := loadConfPathData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mutex.Lock()
|
2021-09-06 16:39:15 +00:00
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
var newConf conf.Conf
|
2021-09-27 12:59:27 +00:00
|
|
|
cloneStruct(&newConf, a.conf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
if _, ok := newConf.Paths[name]; ok {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:59:27 +00:00
|
|
|
newConfPath := &conf.PathConf{}
|
2021-07-04 16:13:49 +00:00
|
|
|
fillStruct(newConfPath, in)
|
2021-09-27 12:59:27 +00:00
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
newConf.Paths[name] = newConfPath
|
|
|
|
|
|
|
|
err = newConf.CheckAndFillMissing()
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:07:08 +00:00
|
|
|
a.conf = &newConf
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
// since reloading the configuration can cause the shutdown of the API,
|
|
|
|
// call it in a goroutine
|
2021-10-27 19:01:00 +00:00
|
|
|
go a.parent.onAPIConfigSet(&newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onConfigPathsEdit(ctx *gin.Context) {
|
2021-09-23 07:07:36 +00:00
|
|
|
name := ctx.Param("name")
|
|
|
|
if len(name) < 2 || name[0] != '/' {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name = name[1:]
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
in, err := loadConfPathData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mutex.Lock()
|
2021-09-06 16:39:15 +00:00
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
var newConf conf.Conf
|
2021-09-27 12:59:27 +00:00
|
|
|
cloneStruct(&newConf, a.conf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
newConfPath, ok := newConf.Paths[name]
|
|
|
|
if !ok {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fillStruct(newConfPath, in)
|
|
|
|
|
|
|
|
err = newConf.CheckAndFillMissing()
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:07:08 +00:00
|
|
|
a.conf = &newConf
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
// since reloading the configuration can cause the shutdown of the API,
|
|
|
|
// call it in a goroutine
|
2021-10-27 19:01:00 +00:00
|
|
|
go a.parent.onAPIConfigSet(&newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onConfigPathsDelete(ctx *gin.Context) {
|
|
|
|
name := ctx.Param("name")
|
2021-09-23 07:07:36 +00:00
|
|
|
if len(name) < 2 || name[0] != '/' {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name = name[1:]
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
a.mutex.Lock()
|
2021-09-06 16:39:15 +00:00
|
|
|
defer a.mutex.Unlock()
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
var newConf conf.Conf
|
2021-09-27 12:59:27 +00:00
|
|
|
cloneStruct(&newConf, a.conf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
if _, ok := newConf.Paths[name]; !ok {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(newConf.Paths, name)
|
|
|
|
|
|
|
|
err := newConf.CheckAndFillMissing()
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:07:08 +00:00
|
|
|
a.conf = &newConf
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
// since reloading the configuration can cause the shutdown of the API,
|
|
|
|
// call it in a goroutine
|
2021-10-27 19:01:00 +00:00
|
|
|
go a.parent.onAPIConfigSet(&newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onPathsList(ctx *gin.Context) {
|
2021-11-05 16:53:24 +00:00
|
|
|
res := a.pathManager.onAPIPathsList(pathAPIPathsListReq{})
|
2021-07-04 16:13:49 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, res.Data)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onRTSPSessionsList(ctx *gin.Context) {
|
2021-11-05 16:53:24 +00:00
|
|
|
res := a.rtspServer.onAPISessionsList(rtspServerAPISessionsListReq{})
|
2021-08-11 15:32:47 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, res.Data)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onRTSPSessionsKick(ctx *gin.Context) {
|
|
|
|
id := ctx.Param("id")
|
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
res := a.rtspServer.onAPISessionsKick(rtspServerAPISessionsKickReq{ID: id})
|
2021-08-11 15:32:47 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 15:32:47 +00:00
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onRTSPSSessionsList(ctx *gin.Context) {
|
2021-11-05 16:53:24 +00:00
|
|
|
res := a.rtspsServer.onAPISessionsList(rtspServerAPISessionsListReq{})
|
2021-08-11 15:32:47 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, res.Data)
|
2021-08-11 15:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onRTSPSSessionsKick(ctx *gin.Context) {
|
|
|
|
id := ctx.Param("id")
|
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
res := a.rtspsServer.onAPISessionsKick(rtspServerAPISessionsKickReq{ID: id})
|
2021-08-11 15:32:47 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onRTMPConnsList(ctx *gin.Context) {
|
2021-11-05 16:53:24 +00:00
|
|
|
res := a.rtmpServer.onAPIConnsList(rtmpServerAPIConnsListReq{})
|
2021-07-04 16:13:49 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
ctx.JSON(http.StatusOK, res.Data)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) onRTMPConnsKick(ctx *gin.Context) {
|
|
|
|
id := ctx.Param("id")
|
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
res := a.rtmpServer.onAPIConnsKick(rtmpServerAPIConnsKickReq{ID: id})
|
2021-07-04 16:13:49 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
2021-11-05 16:14:31 +00:00
|
|
|
|
|
|
|
func (a *api) onHLSMuxersList(ctx *gin.Context) {
|
2021-11-06 11:52:12 +00:00
|
|
|
res := a.hlsServer.onAPIHLSMuxersList(hlsServerAPIMuxersListReq{})
|
2021-11-05 16:14:31 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
ctx.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, res.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// onConfReload is called by core.
|
|
|
|
func (a *api) onConfReload(conf *conf.Conf) {
|
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
|
|
|
a.conf = conf
|
|
|
|
}
|