EasyDarwin/routers/streams.go

129 lines
3.6 KiB
Go
Raw Normal View History

2018-11-24 02:46:37 +00:00
package routers
import (
"fmt"
"log"
"net/http"
"strings"
"time"
2018-11-24 14:07:02 +00:00
2019-01-06 13:10:13 +00:00
"github.com/EasyDarwin/EasyDarwin/models"
"github.com/penggy/EasyGoLib/db"
2018-11-24 14:07:02 +00:00
"github.com/EasyDarwin/EasyDarwin/rtsp"
"github.com/gin-gonic/gin"
2018-11-24 02:46:37 +00:00
)
/**
2018-12-03 05:26:42 +00:00
* @apiDefine stream
*/
/**
2018-12-03 05:53:41 +00:00
* @api {get} /api/v1/stream/start
2018-12-03 05:26:42 +00:00
* @apiGroup stream
* @apiName StreamStart
* @apiParam {String} url RTSP
* @apiParam {String} [customPath] PATH
2019-01-06 13:10:13 +00:00
* @apiParam {String=TCP,UDP} [transType=TCP]
2018-12-03 05:26:42 +00:00
* @apiParam {Number} [idleTimeout]
* @apiParam {Number} [heartbeatInterval] 0OPTION
* @apiSuccess (200) {String} ID IDID
*/
2018-11-24 02:46:37 +00:00
func (h *APIHandler) StreamStart(c *gin.Context) {
type Form struct {
2018-11-24 14:07:02 +00:00
URL string `form:"url" binding:"required"`
CustomPath string `form:"customPath"`
2019-01-06 13:10:13 +00:00
TransType string `form:"transType"`
2018-11-24 14:07:02 +00:00
IdleTimeout int `form:"idleTimeout"`
HeartbeatInterval int `form:"heartbeatInterval"`
2018-11-24 02:46:37 +00:00
}
var form Form
err := c.Bind(&form)
if err != nil {
log.Printf("Pull to push err:%v", err)
2018-11-24 02:46:37 +00:00
return
}
agent := fmt.Sprintf("EasyDarwinGo/%s", BuildVersion)
if BuildDateTime != "" {
agent = fmt.Sprintf("%s(%s)", agent, BuildDateTime)
}
client, err := rtsp.NewRTSPClient(rtsp.GetServer(), form.URL, int64(form.HeartbeatInterval)*1000, agent)
2018-11-24 14:07:02 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
return
}
if form.CustomPath != "" && !strings.HasPrefix(form.CustomPath, "/") {
form.CustomPath = "/" + form.CustomPath
}
client.CustomPath = form.CustomPath
2019-01-06 13:10:13 +00:00
switch strings.ToLower(form.TransType) {
case "udp":
client.TransType = rtsp.TRANS_TYPE_UDP
case "tcp":
fallthrough
default:
client.TransType = rtsp.TRANS_TYPE_TCP
}
2018-11-24 14:07:02 +00:00
2018-11-24 02:46:37 +00:00
pusher := rtsp.NewClientPusher(client)
2018-11-24 14:07:02 +00:00
if rtsp.GetServer().GetPusher(pusher.Path()) != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, fmt.Sprintf("Path %s already exists", client.Path))
return
}
err = client.Start(time.Duration(form.IdleTimeout) * time.Second)
if err != nil {
log.Printf("Pull stream err :%v", err)
c.AbortWithStatusJSON(http.StatusBadRequest, fmt.Sprintf("Pull stream err: %v", err))
return
}
log.Printf("Pull to push %v success ", form)
2018-11-24 02:46:37 +00:00
rtsp.GetServer().AddPusher(pusher)
// save to db.
var stream = models.Stream{
URL: form.URL,
CustomPath: form.CustomPath,
IdleTimeout: form.IdleTimeout,
HeartbeatInterval: form.HeartbeatInterval,
}
if db.SQLite.Where(&models.Stream{URL: form.URL}).First(&models.Stream{}).RecordNotFound() {
db.SQLite.Create(&stream)
} else {
db.SQLite.Save(&stream)
}
c.IndentedJSON(200, pusher.ID())
2018-11-24 02:46:37 +00:00
}
/**
2018-12-03 05:53:41 +00:00
* @api {get} /api/v1/stream/stop
2018-12-03 05:26:42 +00:00
* @apiGroup stream
* @apiName StreamStop
* @apiParam {String} id ID
* @apiUse simpleSuccess
*/
2018-11-24 02:46:37 +00:00
func (h *APIHandler) StreamStop(c *gin.Context) {
type Form struct {
ID string `form:"id" binding:"required"`
}
var form Form
err := c.Bind(&form)
if err != nil {
log.Printf("stop pull to push err:%v", err)
return
}
pushers := rtsp.GetServer().GetPushers()
for _, v := range pushers {
if v.ID() == form.ID {
v.Stop()
c.IndentedJSON(200, "OK")
log.Printf("Stop %v success ", v)
if v.RTSPClient != nil {
var stream models.Stream
stream.URL = v.RTSPClient.URL
db.SQLite.Delete(stream)
}
return
}
}
c.AbortWithStatusJSON(http.StatusBadRequest, fmt.Sprintf("Pusher[%s] not found", form.ID))
}