]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
fix
authorqydysky <32743305+qydysky@users.noreply.github.com>
Thu, 19 Jan 2023 12:27:06 +0000 (20:27 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Thu, 19 Jan 2023 12:27:06 +0000 (20:27 +0800)
CV/Var.go
F/api.go
Reply/stream.go

index b1c5bdae61248aa201571caabb4a1162457baab6..d06fe374c95e048f6353f34b99b426859540d08f 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -20,7 +20,7 @@ import (
 
 type Common struct {
        Uid               int                //client uid
-       Live              []string           //直播流链接
+       Live              []LiveQn           //直播流链接
        Live_qn           int                //当前直播流质量
        Live_want_qn      int                //期望直播流质量
        Roomid            int                //房间ID
@@ -52,6 +52,11 @@ type Common struct {
        ReqPool           *idpool.Idpool     //请求池
 }
 
+type LiveQn struct {
+       Url      string
+       ReUpTime time.Time
+}
+
 func (t *Common) Init() Common {
        t.Qn = map[int]string{ // no change
                20000: "4K",
index 3832110d8f9588b0dc4b2c6727fd1ac5245698d9..f19f3bd05f13f89f9f03ced26e5cf7613693c85f 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -12,6 +12,7 @@ import (
        "time"
 
        c "github.com/qydysky/bili_danmu/CV"
+       cv "github.com/qydysky/bili_danmu/CV"
        J "github.com/qydysky/bili_danmu/Json"
        "github.com/skratchdot/open-golang/open"
 
@@ -293,7 +294,7 @@ func (c *GetFunc) Html() (missKey []string) {
                                if !c.Liveing {
                                        c.Live_qn = 0
                                        c.AcceptQn = c.Qn
-                                       c.Live = []string{}
+                                       c.Live = []cv.LiveQn{}
                                        return
                                }
 
@@ -359,9 +360,11 @@ func (c *GetFunc) Html() (missKey []string) {
                                                                        c.AcceptQn = tmp
                                                                }
                                                                //直播流链接
-                                                               c.Live = []string{}
+                                                               c.Live = []cv.LiveQn{}
                                                                for _, v1 := range v.URLInfo {
-                                                                       c.Live = append(c.Live, v1.Host+v.BaseURL+v1.Extra)
+                                                                       c.Live = append(c.Live, cv.LiveQn{
+                                                                               Url: v1.Host + v.BaseURL + v1.Extra,
+                                                                       })
                                                                }
                                                        }
                                                }
@@ -547,7 +550,7 @@ func (c *GetFunc) getRoomPlayInfo() (missKey []string) {
                if !c.Liveing {
                        c.Live_qn = 0
                        c.AcceptQn = c.Qn
-                       c.Live = []string{}
+                       c.Live = []cv.LiveQn{}
                        return
                }
 
@@ -626,9 +629,9 @@ func (c *GetFunc) getRoomPlayInfo() (missKey []string) {
                                                                c.AcceptQn = tmp
                                                        }
                                                        //直播流链接
-                                                       c.Live = []string{}
+                                                       c.Live = []cv.LiveQn{}
                                                        for _, v1 := range v.URLInfo {
-                                                               c.Live = append(c.Live, v1.Host+v.BaseURL+v1.Extra)
+                                                               c.Live = append(c.Live, cv.LiveQn{Url: v1.Host + v.BaseURL + v1.Extra})
                                                        }
 
                                                        //找到配置格式,跳出
@@ -738,7 +741,7 @@ func (c *GetFunc) getRoomPlayInfoByQn() (missKey []string) {
                if !c.Liveing {
                        c.Live_qn = 0
                        c.AcceptQn = c.Qn
-                       c.Live = []string{}
+                       c.Live = []cv.LiveQn{}
                        return
                }
 
@@ -808,9 +811,9 @@ func (c *GetFunc) getRoomPlayInfoByQn() (missKey []string) {
                                                                c.AcceptQn = tmp
                                                        }
                                                        //直播流链接
-                                                       c.Live = []string{}
+                                                       c.Live = []cv.LiveQn{}
                                                        for _, v1 := range v.URLInfo {
-                                                               c.Live = append(c.Live, v1.Host+v.BaseURL+v1.Extra)
+                                                               c.Live = append(c.Live, cv.LiveQn{Url: v1.Host + v.BaseURL + v1.Extra})
                                                        }
                                                }
                                        }
index d5780293825a6e7809fc8895b2be1e54f58966a4..68b6402925656d21602aa05d4cc277256514c1eb 100644 (file)
@@ -191,13 +191,13 @@ func (t *M4SStream) fetchCheckStream() bool {
        }
 
        // 保存流类型
-       if strings.Contains(t.common.Live[0], `m3u8`) {
+       if strings.Contains(t.common.Live[0].Url, `m3u8`) {
                if t.config.save_as_mp4 {
                        t.stream_type = "mp4"
                } else {
                        t.stream_type = "m3u8"
                }
-       } else if strings.Contains(t.common.Live[0], `flv`) {
+       } else if strings.Contains(t.common.Live[0].Url, `flv`) {
                t.stream_type = "flv"
        }
 
@@ -221,7 +221,7 @@ func (t *M4SStream) fetchCheckStream() bool {
                req := t.reqPool.Get()
                r := req.Item.(*reqf.Req)
                if e := r.Reqf(reqf.Rval{
-                       Url:       v,
+                       Url:       v.Url,
                        Retry:     10,
                        SleepTime: 1000,
                        Proxy:     t.common.Proxy,
@@ -257,9 +257,19 @@ func (t *M4SStream) fetchCheckStream() bool {
 }
 
 func (t *M4SStream) fetchParseM3U8() (m4s_links []*m4s_link_item, m3u8_addon []byte, e error) {
+       // 开始请求
+       req := t.reqPool.Get()
+       defer t.reqPool.Put(req)
+       r := req.Item.(*reqf.Req)
+
        // 请求解析m3u8内容
-       for _, v := range t.common.Live {
-               m3u8_url, err := url.Parse(v)
+       for k, v := range t.common.Live {
+               // 跳过尚未启用的live地址
+               if time.Now().Before(v.ReUpTime) {
+                       continue
+               }
+
+               m3u8_url, err := url.Parse(v.Url)
                if err != nil {
                        e = err
                        return
@@ -288,10 +298,6 @@ func (t *M4SStream) fetchParseM3U8() (m4s_links []*m4s_link_item, m3u8_addon []b
                        rval.Header[`If-Modified-Since`] = t.stream_last_modified.Add(time.Second).Format("Mon, 02 Jan 2006 15:04:05 CST")
                }
 
-               // 开始请求
-               req := t.reqPool.Get()
-               defer t.reqPool.Put(req)
-               r := req.Item.(*reqf.Req)
                if err := r.Reqf(rval); err != nil {
                        e = err
                        continue
@@ -404,7 +410,12 @@ func (t *M4SStream) fetchParseM3U8() (m4s_links []*m4s_link_item, m3u8_addon []b
                        nos, _ := tmp[len(tmp)-1].getNo()
                        noe, _ := t.last_m4s.getNo()
                        if timed > 3 && math.Abs(timed-float64(nos-noe)) > 2 {
-                               e = fmt.Errorf("服务器 %s 发生故障 %d 秒产出了 %d 切片", m3u8_url.Host, int(timed), nos-noe)
+                               // 1min后重新启用
+                               t.common.Live[k].ReUpTime = time.Now().Add(time.Minute)
+                               t.log.L("W: ", fmt.Sprintf("服务器 %s 发生故障 %d 秒产出了 %d 切片", m3u8_url.Host, int(timed), nos-noe))
+                               if k == len(t.common.Live)-1 {
+                                       e = errors.New("全部切片服务器发生故障")
+                               }
                                continue
                        }
                }
@@ -541,7 +552,7 @@ func (t *M4SStream) saveStream() (e error) {
 func (t *M4SStream) saveStreamFlv() (e error) {
        //对每个直播流进行尝试
        for _, v := range t.common.Live {
-               surl, err := url.Parse(v)
+               surl, err := url.Parse(v.Url)
                if err != nil {
                        t.log.L(`E: `, err)
                        e = err
@@ -962,6 +973,17 @@ func (t *M4SStream) saveStreamM4s() (e error) {
                                break
                        }
                }
+
+               // {
+               //      if t.last_m4s != nil {
+               //              l, _ := t.last_m4s.getNo()
+               //              fmt.Println("last", l)
+               //      }
+               //      for i := 0; i < len(m4s_links); i++ {
+               //              fmt.Println(m4s_links[i].getNo())
+               //      }
+               // }
+
                if len(m4s_links) == 0 {
                        time.Sleep(time.Second)
                        continue
@@ -970,7 +992,7 @@ func (t *M4SStream) saveStreamM4s() (e error) {
                        if t.last_m4s == nil {
                                t.last_m4s = &m4s_link_item{}
                        }
-                       for i := len(m4s_links) - 1; i > 0; i-- {
+                       for i := len(m4s_links) - 1; i >= 0; i-- {
                                // fmt.Println("set last m4s", m4s_links[i].Base)
                                if !m4s_links[i].isInit() && len(m4s_links[i].Base) > 0 {
                                        m4s_links[i].copyTo(t.last_m4s)
@@ -1067,7 +1089,7 @@ func (t *M4SStream) Start() bool {
 
                        // 设置全部服务
                        for _, v := range t.common.Live {
-                               if url_struct, e := url.Parse(v); e == nil {
+                               if url_struct, e := url.Parse(v.Url); e == nil {
                                        t.stream_hosts.Store(url_struct.Hostname(), nil)
                                }
                        }