From 46e8ad98b92873237e1f8238eddc47cbdae484bf Mon Sep 17 00:00:00 2001 From: qydysky Date: Fri, 24 May 2024 15:37:05 +0000 Subject: [PATCH] =?utf8?q?Add=20=E9=85=8D=E7=BD=AE=20fmp4=E8=8E=B7?= =?utf8?q?=E5=8F=96=E6=9B=B4=E5=A4=9A=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CV/Var.go | 23 ++++++++++++++++++----- F/api.go | 9 +++++++-- Reply/stream.go | 24 +++++++++++++++++------- demo/config/config_K_v.json | 1 + 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/CV/Var.go b/CV/Var.go index cf5ce95..89bb4cc 100644 --- a/CV/Var.go +++ b/CV/Var.go @@ -89,7 +89,7 @@ type LiveQn struct { Url string ReUpTime time.Time disableCount int - Expires int //流到期时间 + Expires time.Time //流到期时间 } func (t *LiveQn) SetUrl(url string) { @@ -109,12 +109,16 @@ func (t *LiveQn) Valid() bool { } // 自动停用机制 -func (t *LiveQn) DisableAuto() { +func (t *LiveQn) DisableAuto() (hadDisable bool) { + if !t.Valid() { + return true + } if time.Now().After(t.ReUpTime.Add(time.Minute).Add(time.Second * time.Duration(10*t.disableCount))) { t.disableCount = 0 } t.disableCount += 1 t.ReUpTime = time.Now().Add(time.Minute).Add(time.Second * time.Duration(10*t.disableCount)) + return } func (t *LiveQn) Disable(reUpTime time.Time) { @@ -174,15 +178,15 @@ func (t *Common) Copy() *Common { } // 自动停用机制 -func (t *Common) DisableLiveAuto(host string) { +func (t *Common) DisableLiveAuto(host string) (hadDisable bool) { for i := 0; i < len(t.Live); i++ { if liveUrl, e := url.Parse(t.Live[i].Url); e == nil { if host == liveUrl.Host { - t.Live[i].DisableAuto() - break + return t.Live[i].DisableAuto() } } } + return } func (t *Common) DisableLive(host string, reUpTime time.Time) { @@ -196,6 +200,15 @@ func (t *Common) DisableLive(host string, reUpTime time.Time) { } } +func (t *Common) ValidNum() (num int) { + for i := 0; i < len(t.Live); i++ { + if time.Now().After(t.Live[i].ReUpTime) { + num += 1 + } + } + return +} + func (t *Common) ValidLive() *LiveQn { for i := 0; i < len(t.Live); i++ { if time.Now().Before(t.Live[i].ReUpTime) { diff --git a/F/api.go b/F/api.go index 292e724..6511241 100644 --- a/F/api.go +++ b/F/api.go @@ -420,7 +420,12 @@ func (t *GetFunc) configStreamType(sts []struct { // 默认类型 wantTypes = append(wantTypes, t.AllStreamType[`fmp4`], t.AllStreamType[`flv`]) - t.Live = t.Live[:0] + // t.Live = t.Live[:0] + for i := 0; i < len(t.Live); i++ { + if time.Now().Add(time.Minute).Before(t.Live[i].ReUpTime) { + t.Live = append(t.Live[:i], t.Live[i+1:]...) + } + } for k, streamType := range wantTypes { for _, v := range sts { @@ -462,7 +467,7 @@ func (t *GetFunc) configStreamType(sts []struct { if query, e := url.ParseQuery(v1.Extra); e == nil { if expires, e := strconv.Atoi(query.Get("expires")); e == nil { - item.Expires = expires + item.Expires = time.Now().Add(time.Duration(expires * int(time.Second))) } } diff --git a/Reply/stream.go b/Reply/stream.go index 509044d..60e8a2b 100644 --- a/Reply/stream.go +++ b/Reply/stream.go @@ -296,9 +296,18 @@ func (t *M4SStream) fetchCheckStream() bool { } func (t *M4SStream) fetchParseM3U8(lastM4s *m4s_link_item, fmp4ListUpdateTo float64) (m4s_links []*m4s_link_item, e error) { - if t.common.ValidLive() == nil { - e = errors.New("全部流服务器发生故障") - return + { + n := t.common.ValidNum() + if d, ok := t.common.K_v.LoadV("fmp4获取更多服务器").(bool); ok && d && n <= 1 { + t.log.L("I: ", "获取更多服务器...") + if !t.fetchCheckStream() { + e = errors.New("全部流服务器发生故障") + return + } + } else if n == 0 { + e = errors.New("全部流服务器发生故障") + return + } } // 开始请求 @@ -961,25 +970,26 @@ func (t *M4SStream) saveStreamM4s() (e error) { } dCount += 1 - done := downloadLimit.Block() - // 故障转移 if download_seq[i].status == 3 { if linkUrl, e := url.Parse(download_seq[i].Url); e == nil { oldHost := linkUrl.Host // 将此切片服务器设置停用 - t.common.DisableLiveAuto(oldHost) + hadDisable := t.common.DisableLiveAuto(oldHost) // 从其他服务器获取此切片 if vl := t.common.ValidLive(); vl == nil { return errors.New(`全部流服务器故障`) } else { linkUrl.Host = vl.Host() - t.log.L(`W: `, `切片下载失败,故障转移`, oldHost, ` -> `, linkUrl.Host) + if !hadDisable { + t.log.L(`W: `, `切片下载失败,故障转移`, oldHost, ` -> `, linkUrl.Host) + } } download_seq[i].Url = linkUrl.String() } } + done := downloadLimit.Block() go func(link *m4s_link_item) { defer done() diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index c033b77..50e39d1 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -74,6 +74,7 @@ "flv断流超时s": 5, "flv断流续接": true, "fmp4切片下载超时s": 3, + "fmp4获取更多服务器": true, "fmp4列表更新超时s": 7, "fmp4音视频时间戳容差s-help": "默认0.1,小于默认无效,调大可以允许较差的流,但可能会音画不同步", "fmp4音视频时间戳容差s": 0.2, -- 2.39.2