]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
支持hls的base64返回
authorqydysky <qydysky@foxmail.com>
Thu, 6 May 2021 13:19:50 +0000 (21:19 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 6 May 2021 13:19:50 +0000 (21:19 +0800)
flv协调关闭
flv接收无数据超时
更高效率的flv解码buf调整

README.md
Reply/F.go
demo/go.mod
demo/go.sum
go.mod
go.sum

index 2cd3ce6d2f9a86de2674dd5663f2afd662581a7d..7d701a0a5f630b45030084d5d2b44a7cc1905729 100644 (file)
--- a/README.md
+++ b/README.md
@@ -129,6 +129,7 @@ I: 2021/04/13 20:07:45 命令行操作 [直播Web服务: http://192.168.31.245:3
 
 测试可用项目:
 
+- [xqq/mpegts.js](https://github.com/xqq/mpegts.js)
 - [bilibili/flv.js](https://github.com/bilibili/flv.js)
 - [bytedance/xgplayer](https://github.com/bytedance/xgplayer)
 - [video-dev/hls.js](https://github.com/video-dev/hls.js)
index 039d4c8b8f408461e9b48e23054c7386a4880f80..19d6813c11c17840b87d61dea079450d71457b95 100644 (file)
@@ -16,6 +16,7 @@ import (
        "net/url"
        "errors"
        "bytes"
+       "encoding/base64"
        // "runtime"
        
        c "github.com/qydysky/bili_danmu/CV"
@@ -311,9 +312,19 @@ func Savestreamf(){
 
                        trid := query.Get("trid")
                        expires,_ = strconv.Atoi(query.Get("expires"))
+                       buf := r.Respon
+
+                       //base-64
+                       if len(buf) != 0 && !bytes.Contains(buf, []byte("#")) {
+                               buf,err = base64.StdEncoding.DecodeString(string(buf))
+                               if err != nil {
+                                       return
+                               }
+                               // fmt.Println(`base64`)
+                       }
 
                        var m4s_links []*m4s_link_item
-                       lines := bytes.Split(r.Respon, []byte("\n"))
+                       lines := bytes.Split(buf, []byte("\n"))
                        for i:=0;i<len(lines);i+=1 {
                                line := lines[i]
                                m4s_link := ""
@@ -345,7 +356,7 @@ func Savestreamf(){
                        }
 
                        if last_download == nil {
-                               m3u8_file_addition = r.Respon
+                               m3u8_file_addition = buf
                                need_download = m4s_links
                                return
                        }
@@ -514,8 +525,6 @@ func Savestreamf(){
                                keyframe [][]byte
                                // sync_buf []byte
                                close func()
-
-                               sync.RWMutex
                        }
                        
                        //chans
@@ -678,7 +687,37 @@ func Savestreamf(){
        
                                                return false
                                        },
+                                       `closereq`:func(data interface{})(bool){
+                                               req,ok := data.(link_stream)
+       
+                                               if !ok {return false}
+
+                                               req.close()
+
+                                               for i:=0;i<len(reqs_used_id);i+=1 {
+                                                       if reqs_used_id[i] == req.id.Id {
+                                                               reqs_used_id = append(reqs_used_id[:i],reqs_used_id[i+1:]...)
+                                                               if len(reqs_used_id) != 1 {
+                                                                       reqs_keyframe = append(reqs_keyframe[:i],reqs_keyframe[i+1:]...)
+                                                               }
+                                                               return false
+                                                       }
+                                               }
+                                               
+                                               for i:=0;i<len(reqs_remove_id);i+=1 {
+                                                       if reqs_remove_id[i] == req.id.Id {
+                                                               reqs_remove_id = append(reqs_remove_id[:i], reqs_remove_id[i+1:]...)
+                                                               return false
+                                                       }
+                                               }
+                                               
+                                               return false
+                                       },
                                        `close`:func(data interface{})(bool){
+                                               reqs_used_id = []uintptr{}
+                                               reqs_remove_id = []uintptr{}
+                                               reqs_keyframe = [][][]byte{}
+                                               last_time_stamp = 0
                                                return true
                                        },
                                })
@@ -714,6 +753,7 @@ func Savestreamf(){
                                        //SaveToPath:savestream.path + ".flv",
                                        SaveToChan:bc,
                                        Timeout:int(int64(exp) - p.Sys().GetSTime()),
+                                       ReadTimeout:7,
                                })
                                
                                //返回通道
@@ -724,7 +764,10 @@ func Savestreamf(){
 
                                //解析
                                go func(bc chan[]byte,item *link_stream){
-                                       var buf []byte
+                                       var (
+                                               buf []byte
+                                               skip_buf_size int
+                                       )
                                        for {
                                                select {
                                                case <-exit_chan:return;
@@ -732,6 +775,7 @@ func Savestreamf(){
                                                        if len(b) == 0 {
                                                                // fmt.Println(`req退出`,item.id.Id)
                                                                id_pool.Put(item.id)
+                                                               reqs.Push_tag(`closereq`,*item)
                                                                return
                                                        }
 
@@ -742,12 +786,11 @@ func Savestreamf(){
                                                        //      tmp_buf = make([]byte,len(buf))
                                                        //      copy(tmp_buf, buf)
                                                        // }
-                                                       
-                                                       if len(buf) < 1<<21 {break}
+                                                       // fmt.Println(buf[:11])
 
-                                                       front,list,_ := Seach_stream_tag(buf)
+                                                       if len(buf) < skip_buf_size {break}
 
-                                                       // fmt.Println(`=<`,buf[:3])
+                                                       front,list,_ := Seach_stream_tag(buf)
 
                                                        if len(front) != 0 && len(item.front) == 0 {
                                                                // fmt.Println(item.id.Id,`获取到header`,len(front))
@@ -755,7 +798,11 @@ func Savestreamf(){
                                                                copy(item.front, front)
                                                        }
 
-                                                       if len(list) == 0 || len(item.front) == 0 {break}
+                                                       if len(list) == 0 || len(item.front) == 0 {
+                                                               // fmt.Println(`再次查询bufsize`,skip_buf_size)
+                                                               skip_buf_size = 2*len(buf)
+                                                               break
+                                                       }
 
                                                        // total := 0
                                                        item.keyframe = list
@@ -775,11 +822,12 @@ func Savestreamf(){
                                                                buf = buf[cut_offset:]
                                                        }
 
+                                                       skip_buf_size = len(buf)+len(list[0])
+
                                                        reqs.Push_tag(`req`,*item)
                                                }
                                        }
                                }(bc,&item)
-                               
 
                                //等待过期/退出
                                {
@@ -802,8 +850,8 @@ func Savestreamf(){
                                if len(c.Live)==0 {break}
                        }
                        
-                       reqs.Push_tag(`close`,nil)
                        close(exit_chan)
+                       reqs.Push_tag(`close`,nil)
                        out.Close()
                        
                        l.L(`I: `,"结束")
@@ -1031,14 +1079,16 @@ func Savestreamf(){
                                }
                        }
 
-                       f := p.File()
-                       f.FileWR(p.Filel{
-                               File:savestream.path+"0.m3u8.dtmp",
-                               Write:true,
-                               Loc:-1,
-                               Context:[]interface{}{"#EXT-X-ENDLIST"},
-                       })
-                       p.FileMove(savestream.path+"0.m3u8.dtmp", savestream.path+"0.m3u8")
+                       if p.Checkfile().IsExist(savestream.path+"0.m3u8.dtmp") {
+                               f := p.File()
+                               f.FileWR(p.Filel{
+                                       File:savestream.path+"0.m3u8.dtmp",
+                                       Write:true,
+                                       Loc:-1,
+                                       Context:[]interface{}{"#EXT-X-ENDLIST"},
+                               })
+                               p.FileMove(savestream.path+"0.m3u8.dtmp", savestream.path+"0.m3u8")
+                       }
                        l.L(`I: `,"结束")
                        close(exit_chan)//hls_stream
                        Ass_f("", time.Now())//ass
index 82fccd7c4c088bd725e2bef34a9666b258b08448..1a50dda7c72472cb6a81289594f445f986035822 100644 (file)
@@ -14,7 +14,7 @@ require (
        github.com/miekg/dns v1.1.41 // indirect
        github.com/mitchellh/mapstructure v1.4.1 // indirect
        github.com/qydysky/bili_danmu v0.5.8
-       github.com/qydysky/part v0.5.12 // indirect
+       github.com/qydysky/part v0.5.13 // indirect
        github.com/shirou/gopsutil v3.21.4+incompatible // indirect
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
index 8b709cc175a90b141fcfd6e9ca1de0ddee526efb..dfbaf667fef219a1a900e2f4babeb785005a7fe0 100644 (file)
@@ -276,6 +276,8 @@ github.com/qydysky/part v0.5.10 h1:SYdPGV1DlHFDKjDGBU3dXbuR9WA8NNwTbQ2nDjdyeiA=
 github.com/qydysky/part v0.5.10/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
 github.com/qydysky/part v0.5.12 h1:HnXJ7NmL5kvpFwPSpz5D5Kmg50mTblRP6tIufVsXP+U=
 github.com/qydysky/part v0.5.12/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
+github.com/qydysky/part v0.5.13 h1:Y1SXoT5ZKCBURoP63G3iOx17kJKlXHpUVNE+oJVFp3o=
+github.com/qydysky/part v0.5.13/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
 github.com/qydysky/part/msgq v0.0.0-20201213031129-ca3253dc72ad h1:Jtzf509lQrkUMGTV0Sc6IDCAiR1VrBcHrIban7hpye4=
 github.com/qydysky/part/msgq v0.0.0-20201213031129-ca3253dc72ad/go.mod h1:w32TkJNVtTJd4LOS09cq+4uYG6itcN2vsqw+slp44Rg=
 github.com/qydysky/part/msgq v0.0.0-20201213120821-f36e49c32bba h1:1ew9dRpc0Rux0WkWeT/4AE15ynYWmL2D7onJEJIFOB8=
diff --git a/go.mod b/go.mod
index 1650bbb7cfcb9c891ddabb69dcc84ca6111f7aef..9a313a61485359a84bcbc462e70184c81d1deff3 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -12,7 +12,7 @@ require (
        github.com/mdp/qrterminal/v3 v3.0.0
        github.com/miekg/dns v1.1.41 // indirect
        github.com/mitchellh/mapstructure v1.4.1 // indirect
-       github.com/qydysky/part v0.5.12
+       github.com/qydysky/part v0.5.13
        github.com/shirou/gopsutil v3.21.4+incompatible // indirect
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
diff --git a/go.sum b/go.sum
index fce9e539778e8f3321df45a6eab34cebd154383c..524c3e985e9ba3b4fa1b685abf9a837ab6de9c1b 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -50,6 +50,8 @@ github.com/qydysky/part v0.5.10 h1:SYdPGV1DlHFDKjDGBU3dXbuR9WA8NNwTbQ2nDjdyeiA=
 github.com/qydysky/part v0.5.10/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
 github.com/qydysky/part v0.5.12 h1:HnXJ7NmL5kvpFwPSpz5D5Kmg50mTblRP6tIufVsXP+U=
 github.com/qydysky/part v0.5.12/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
+github.com/qydysky/part v0.5.13 h1:Y1SXoT5ZKCBURoP63G3iOx17kJKlXHpUVNE+oJVFp3o=
+github.com/qydysky/part v0.5.13/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
 github.com/qydysky/part/msgq v0.0.0-20201213120821-f36e49c32bba h1:1ew9dRpc0Rux0WkWeT/4AE15ynYWmL2D7onJEJIFOB8=
 github.com/qydysky/part/msgq v0.0.0-20201213120821-f36e49c32bba/go.mod h1:w32TkJNVtTJd4LOS09cq+4uYG6itcN2vsqw+slp44Rg=
 github.com/shirou/gopsutil v2.20.7+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=