]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add 配置 fmp4获取更多服务器
authorqydysky <qydysky@foxmail.com>
Fri, 24 May 2024 15:37:05 +0000 (15:37 +0000)
committerqydysky <qydysky@foxmail.com>
Fri, 24 May 2024 15:37:05 +0000 (15:37 +0000)
CV/Var.go
F/api.go
Reply/stream.go
demo/config/config_K_v.json

index cf5ce9519e2b7841af993f962ea796a00a2e76bd..89bb4cc40c5f358c22a08e6d5edde7e1939972a2 100644 (file)
--- 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) {
index 292e724fe41c5d095284c26d253fee68100813de..6511241b3d0b4c9f141d6518101c41cfb9d45e1e 100644 (file)
--- 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)))
                                                        }
                                                }
 
index 509044d21e00b481d43f092b60eae6abbd861548..60e8a2b7118c2035e4995311fc3a534b011eed86 100644 (file)
@@ -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()
 
index c033b77c67d2a83808891bef88ce728160642b8e..50e39d1fadf828e6da58a6c456036b9e9a08b147 100644 (file)
@@ -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,