mirror of
https://github.com/Monibuca/plugin-rtsp.git
synced 2025-09-27 03:56:08 +08:00
更新重连逻辑
This commit is contained in:
31
client.go
31
client.go
@@ -25,6 +25,7 @@ func (rtsp *RTSP) PullStream(streamPath string, rtspUrl string) (err error) {
|
|||||||
rtsp.Stream = &Stream{
|
rtsp.Stream = &Stream{
|
||||||
StreamPath: streamPath,
|
StreamPath: streamPath,
|
||||||
Type: "RTSP Pull",
|
Type: "RTSP Pull",
|
||||||
|
ExtraProp: rtsp,
|
||||||
}
|
}
|
||||||
if result := rtsp.Publish(); result {
|
if result := rtsp.Publish(); result {
|
||||||
rtsp.TransType = TRANS_TYPE_TCP
|
rtsp.TransType = TRANS_TYPE_TCP
|
||||||
@@ -34,13 +35,7 @@ func (rtsp *RTSP) PullStream(streamPath string, rtspUrl string) (err error) {
|
|||||||
rtsp.aRTPControlChannel = 3
|
rtsp.aRTPControlChannel = 3
|
||||||
rtsp.URL = rtspUrl
|
rtsp.URL = rtspUrl
|
||||||
rtsp.UDPServer = &UDPServer{Session: rtsp}
|
rtsp.UDPServer = &UDPServer{Session: rtsp}
|
||||||
if err = rtsp.requestStream(); err != nil {
|
|
||||||
Println(err)
|
|
||||||
rtsp.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
go rtsp.startStream()
|
go rtsp.startStream()
|
||||||
collection.Store(streamPath, rtsp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return errors.New("publish badname")
|
return errors.New("publish badname")
|
||||||
@@ -266,34 +261,24 @@ func (client *RTSP) requestStream() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (client *RTSP) startStream() {
|
func (client *RTSP) startStream() {
|
||||||
|
if client.Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
//loggerTime := time.Now().Add(-10 * time.Second)
|
//loggerTime := time.Now().Add(-10 * time.Second)
|
||||||
defer func() {
|
defer func() {
|
||||||
if client.Err() == nil && config.Reconnect {
|
if client.Err() == nil && config.Reconnect {
|
||||||
Printf("reconnecting:%s", client.URL)
|
Printf("reconnecting:%s", client.URL)
|
||||||
client.RTSPClientInfo = RTSPClientInfo{}
|
client.RTSPClientInfo = RTSPClientInfo{}
|
||||||
if err := client.requestStream(); err != nil {
|
|
||||||
t := time.NewTicker(time.Second * 5)
|
|
||||||
for {
|
|
||||||
Printf("reconnecting:%s in 5 seconds", client.URL)
|
Printf("reconnecting:%s in 5 seconds", client.URL)
|
||||||
select {
|
time.AfterFunc(time.Second*5, client.startStream)
|
||||||
case <-client.Done():
|
|
||||||
client.Stop()
|
|
||||||
return
|
|
||||||
case <-t.C:
|
|
||||||
if err = client.requestStream(); err == nil {
|
|
||||||
go client.startStream()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
go client.startStream()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
client.Stop()
|
client.Stop()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
if err := client.requestStream(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
for client.Err() == nil {
|
for client.Err() == nil {
|
||||||
if time.Since(startTime) > time.Minute {
|
if time.Since(startTime) > time.Minute {
|
||||||
startTime = time.Now()
|
startTime = time.Now()
|
||||||
|
25
main.go
25
main.go
@@ -14,40 +14,31 @@ import (
|
|||||||
"github.com/teris-io/shortid"
|
"github.com/teris-io/shortid"
|
||||||
)
|
)
|
||||||
|
|
||||||
var collection sync.Map
|
|
||||||
var config = struct {
|
var config = struct {
|
||||||
ListenAddr string
|
ListenAddr string
|
||||||
AutoPull bool
|
|
||||||
RemoteAddr string
|
|
||||||
Timeout int
|
Timeout int
|
||||||
Reconnect bool
|
Reconnect bool
|
||||||
AutoPullList map[string]string
|
AutoPullList map[string]string
|
||||||
}{":554", false, "rtsp://localhost/${streamPath}", 0, false, nil}
|
}{":554", 0, false, nil}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
InstallPlugin(&PluginConfig{
|
InstallPlugin(&PluginConfig{
|
||||||
Name: "RTSP",
|
Name: "RTSP",
|
||||||
Config: &config,
|
Config: &config,
|
||||||
Run: runPlugin,
|
Run: runPlugin,
|
||||||
HotConfig: map[string]func(interface{}){
|
|
||||||
"AutoPull": func(value interface{}) {
|
|
||||||
config.AutoPull = value.(bool)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func runPlugin() {
|
func runPlugin() {
|
||||||
|
|
||||||
http.HandleFunc("/api/rtsp/list", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/api/rtsp/list", func(w http.ResponseWriter, r *http.Request) {
|
||||||
sse := NewSSE(w, r.Context())
|
sse := NewSSE(w, r.Context())
|
||||||
var err error
|
var err error
|
||||||
for tick := time.NewTicker(time.Second); err == nil; <-tick.C {
|
for tick := time.NewTicker(time.Second); err == nil; <-tick.C {
|
||||||
var info []*RTSP
|
var info []*RTSP
|
||||||
collection.Range(func(key, value interface{}) bool {
|
for _, s := range Streams.ToList() {
|
||||||
rtsp := value.(*RTSP)
|
if rtsp, ok := s.ExtraProp.(*RTSP); ok {
|
||||||
info = append(info, rtsp)
|
info = append(info, rtsp)
|
||||||
return true
|
}
|
||||||
})
|
}
|
||||||
err = sse.WriteJSON(info)
|
err = sse.WriteJSON(info)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -71,12 +62,6 @@ func runPlugin() {
|
|||||||
if config.ListenAddr != "" {
|
if config.ListenAddr != "" {
|
||||||
go log.Fatal(ListenRtsp(config.ListenAddr))
|
go log.Fatal(ListenRtsp(config.ListenAddr))
|
||||||
}
|
}
|
||||||
// AddHook(HOOK_SUBSCRIBE, func(value interface{}) {
|
|
||||||
// s := value.(*Subscriber)
|
|
||||||
// if config.AutoPull && s.Publisher == nil {
|
|
||||||
// new(RTSP).PullStream(s.StreamPath, strings.Replace(config.RemoteAddr, "${streamPath}", s.StreamPath, -1))
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListenRtsp(addr string) error {
|
func ListenRtsp(addr string) error {
|
||||||
|
@@ -77,7 +77,6 @@ func (session *RTSP) SessionString() string {
|
|||||||
func (session *RTSP) Stop() {
|
func (session *RTSP) Stop() {
|
||||||
if session.Stream != nil {
|
if session.Stream != nil {
|
||||||
session.Close()
|
session.Close()
|
||||||
collection.Delete(session.StreamPath)
|
|
||||||
}
|
}
|
||||||
if session.Conn != nil {
|
if session.Conn != nil {
|
||||||
session.connRW.Flush()
|
session.connRW.Flush()
|
||||||
@@ -396,7 +395,6 @@ func (session *RTSP) handleRequest(req *Request) {
|
|||||||
Printf("video codec[%s]\n", session.VSdp.Codec)
|
Printf("video codec[%s]\n", session.VSdp.Codec)
|
||||||
}
|
}
|
||||||
session.Stream.Type = "RTSP"
|
session.Stream.Type = "RTSP"
|
||||||
collection.Store(streamPath, session)
|
|
||||||
}
|
}
|
||||||
case "DESCRIBE":
|
case "DESCRIBE":
|
||||||
session.Type = SESSEION_TYPE_PLAYER
|
session.Type = SESSEION_TYPE_PLAYER
|
||||||
|
Reference in New Issue
Block a user