]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Fix 录制退出时等待协程退出
authorqydysky <qydysky@foxmail.com>
Wed, 6 Sep 2023 13:07:29 +0000 (21:07 +0800)
committerqydysky <qydysky@foxmail.com>
Wed, 6 Sep 2023 13:07:29 +0000 (21:07 +0800)
Reply/F.go
Reply/F/reSetMp4TimeStamp/reSetMp4TimeStamp.go
Reply/stream.go
demo/config/config_disable_msg.json
go.mod
go.sum

index 05ea30935f297762a3fb99de2e3ea87bb7c798cf..9ed8969b40e67b96ac19dd23dbdcc7f3d3bd3ce4 100644 (file)
@@ -37,6 +37,7 @@ import (
 
        p "github.com/qydysky/part"
        comp "github.com/qydysky/part/component"
+       pctx "github.com/qydysky/part/ctx"
        file "github.com/qydysky/part/file"
        pio "github.com/qydysky/part/io"
        limit "github.com/qydysky/part/limit"
@@ -205,7 +206,7 @@ func init() {
 }
 
 // 设定字幕文件名,为""时停止输出
-func Ass_f(contextC context.Context, save_path string, filePath string, st time.Time) {
+func Ass_f(ctx context.Context, save_path string, filePath string, st time.Time) {
        if !IsOn(`仅保存当前直播间流`) {
                return
        }
@@ -225,7 +226,9 @@ func Ass_f(contextC context.Context, save_path string, filePath string, st time.
        _, _ = f.Write([]byte(ass.header), true)
        ass.startT = st
 
-       <-contextC.Done()
+       done := pctx.Wait(ctx)
+       defer done()
+
        ass.file = ""
        fl.L(`I: `, "结束")
 }
@@ -1677,7 +1680,7 @@ func init() {
 }
 
 // 弹幕回放
-func StartRecDanmu(c context.Context, filePath string) {
+func StartRecDanmu(ctx context.Context, filePath string) {
        if !IsOn(`仅保存当前直播间流`) || !IsOn("弹幕回放") {
                return
        }
@@ -1690,7 +1693,10 @@ func StartRecDanmu(c context.Context, filePath string) {
        } else {
                f.L(`E: `, e)
        }
-       <-c.Done()
+
+       done := pctx.Wait(ctx)
+       defer done()
+
        f.L(`I: `, `结束`)
 
        // 弹幕录制结束
index b77e3efeeaa7fb97cb1fcd95a895c9345965f8b2..1f23f67ac3508b1dc8ecaebe8802f7a62257efbb 100644 (file)
@@ -6,6 +6,7 @@ import (
        "errors"
        "fmt"
        "io"
+       "time"
 
        comp "github.com/qydysky/part/component"
        file "github.com/qydysky/part/file"
@@ -29,8 +30,9 @@ func init() {
 }
 
 func resetTS(ctx context.Context, ptr *string) error {
+       be := time.Now()
        fmt.Println("resetTS")
-       defer fmt.Println("resetTS fin")
+       defer fmt.Printf("resetTS fin (%v)\n", time.Since(be))
 
        f := file.New(*ptr+"0.mp4", 0, false)
        if !f.IsExist() {
@@ -192,7 +194,7 @@ func resetTS(ctx context.Context, ptr *string) error {
                }
                trackID := btoi32(trackBuf, 0)
                _ = f.SeekIndex(4, file.AtCurrent)
-               fmt.Printf("tkhd %v \n", int32((cuTs[trackID]-opTs[trackID])/timescale[trackID]))
+               fmt.Printf("tkhd %v %v \n", trackID, int32((cuTs[trackID]-opTs[trackID])/timescale[trackID]))
                if _, e := f.Write(itob32(int32((cuTs[trackID]-opTs[trackID])/timescale[trackID])), false); e != nil {
                        return e
                }
index 99f739f66bbdecd335a311588e3dba15b83bdac4..2e9f9acf6550a9e181f6c5df6477233fba01bbc9 100644 (file)
@@ -25,6 +25,7 @@ import (
        c "github.com/qydysky/bili_danmu/CV"
        F "github.com/qydysky/bili_danmu/F"
 
+       pctx "github.com/qydysky/part/ctx"
        file "github.com/qydysky/part/file"
        funcCtrl "github.com/qydysky/part/funcCtrl"
        pio "github.com/qydysky/part/io"
@@ -39,22 +40,20 @@ import (
 )
 
 type M4SStream struct {
-       Status               *signal.Signal     //IsLive()是否运行中
-       exitSign             *signal.Signal     //IsLive()是否等待退出中
-       log                  *log.Log_interface //日志
-       config               M4SStream_Config   //配置
-       stream_last_modified time.Time          //流地址更新时间
-       // stream_expires       int64              //流到期时间
-       // stream_hosts      psync.Map  //使用的流服务器
-       stream_type       string                //流类型
-       Stream_msg        *msgq.MsgType[[]byte] //流数据消息 tag:data
-       first_buf         []byte                //m4s起始块 or flv起始块
-       boot_buf          []byte                //快速启动缓冲
-       boot_buf_locker   funcCtrl.BlockFunc
-       last_m4s          *m4s_link_item           //最后一个切片
-       m4s_pool          *pool.Buf[m4s_link_item] //切片pool
-       common            *c.Common                //通用配置副本
-       Current_save_path string                   //明确的直播流保存目录
+       Status               *signal.Signal        //IsLive()是否运行中
+       exitSign             *signal.Signal        //IsLive()是否等待退出中
+       log                  *log.Log_interface    //日志
+       config               M4SStream_Config      //配置
+       stream_last_modified time.Time             //流地址更新时间
+       stream_type          string                //流类型
+       Stream_msg           *msgq.MsgType[[]byte] //流数据消息 tag:data
+       first_buf            []byte                //m4s起始块 or flv起始块
+       boot_buf             []byte                //快速启动缓冲
+       boot_buf_locker      funcCtrl.BlockFunc
+       last_m4s             *m4s_link_item           //最后一个切片
+       m4s_pool             *pool.Buf[m4s_link_item] //切片pool
+       common               *c.Common                //通用配置副本
+       Current_save_path    string                   //明确的直播流保存目录
        // 事件周期 start: 开始实例 startRec:开始录制 load:接收到视频头 firstFrame: 接收到第一个关键帧 cut:切 stopRec:结束录制 stop:结束实例
        msg               *msgq.MsgType[*M4SStream] //实例的各种事件回调
        Callback_start    func(*M4SStream) error    //实例开始的回调
@@ -1182,10 +1181,12 @@ func (t *M4SStream) Start() bool {
 
        t.Status = signal.Init()
        go func() {
-               defer t.Status.Done()
-
                t.log.L(`I: `, `初始化录制(`+strconv.Itoa(t.common.Roomid)+`)`)
 
+               defer t.log.L(`I: `, `结束录制(`+strconv.Itoa(t.common.Roomid)+`)`)
+               defer t.exitSign.Done()
+               defer t.Status.Done()
+
                // 初始化请求池
                t.reqPool = t.common.ReqPool
 
@@ -1194,7 +1195,14 @@ func (t *M4SStream) Start() bool {
 
                // 设置事件
                // 当录制停止时,取消全部录制
-               mainContextC, maincancel := context.WithCancel(context.Background())
+               mainCtx, mainCancel := context.WithCancel(context.Background())
+               mainCtx, done := pctx.WithWait(mainCtx, time.Minute)
+               defer func() {
+                       if done() != nil {
+                               t.log.L(`E: `, `结束超时`)
+                       }
+               }()
+
                if t.Callback_stopRec != nil {
                        cancel := t.msg.Pull_tag_only(`stopRec`, func(ms *M4SStream) (disable bool) {
                                ms.Callback_stopRec(ms)
@@ -1206,7 +1214,7 @@ func (t *M4SStream) Start() bool {
                        if ms.Callback_stop != nil {
                                ms.Callback_stop(ms)
                        }
-                       maincancel()
+                       mainCancel()
                        t.msg.ClearAll()
                        return true
                })
@@ -1217,7 +1225,7 @@ func (t *M4SStream) Start() bool {
                        cancel := t.msg.Pull_tag_async(map[string]func(*M4SStream) (disable bool){
                                `cut`: func(ms *M4SStream) (disable bool) {
                                        // 当cut时,取消上次录制
-                                       contextC, cancel := context.WithCancel(mainContextC)
+                                       contextC, cancel := context.WithCancel(mainCtx)
                                        fc.FlashWithCallback(cancel)
 
                                        // 分段时长min
@@ -1313,7 +1321,6 @@ func (t *M4SStream) Start() bool {
                                                        }
                                                }
                                        }
-
                                        return false
                                },
                        })
@@ -1342,11 +1349,8 @@ func (t *M4SStream) Start() bool {
                        }
                }
 
-               t.log.L(`I: `, `结束录制(`+strconv.Itoa(t.common.Roomid)+`)`)
-
                // 退出
                t.msg.Push_tag(`stop`, t)
-               t.exitSign.Done()
        }()
        return true
 }
@@ -1372,6 +1376,9 @@ func (t *M4SStream) PusherToFile(contextC context.Context, filepath string, star
                return e
        }
 
+       contextC, done := pctx.WaitCtx(contextC)
+       defer done()
+
        _, _ = f.Write(t.getFirstBuf(), true)
        cancelRec := t.Stream_msg.Pull_tag_async(map[string]func([]byte) bool{
                `data`: func(b []byte) bool {
index abe1678302819e5320b433441b29ae1da9b8e3a9..7bd6e90896478f3569fcb369889400c4a2426128 100644 (file)
@@ -1,5 +1,6 @@
 {
   "help": "禁用指定消息类型,false将忽略此类消息,true将显示此类消息的json纯文本",
+  "USER_TASK_PROGRESS_V2": false,
   "SHOPPING_EXPLAIN_CARD": false,
   "RANK_REM":false,
   "PLAY_TAG":false,
diff --git a/go.mod b/go.mod
index 625f7d2dbe4672a1de7094afca05a1cc61d6d7c0..2fcdfbbefdf619378a7bd012c555daa77e8a2f04 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.21
 require (
        github.com/gotk3/gotk3 v0.6.2
        github.com/mdp/qrterminal/v3 v3.1.1
-       github.com/qydysky/part v0.28.1-0.20230827094611-9921d2be4ddb
+       github.com/qydysky/part v0.28.1-0.20230906125703-9c3051ab7a3c
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
        golang.org/x/text v0.12.0
@@ -55,4 +55,4 @@ require (
        rsc.io/qr v0.2.0 // indirect
 )
 
-// replace github.com/qydysky/part => ../part
+replace github.com/qydysky/part => ../part
diff --git a/go.sum b/go.sum
index 2a83c69e9d2c0c66cf5e87a7ca6d39366328b006..575b181d688ba8f8363f843a335937ce94f16e00 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -34,8 +34,6 @@ github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo=
 github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/qydysky/part v0.28.1-0.20230827094611-9921d2be4ddb h1:Ic3I0DS8DaCP3v2zg6GGYJDf7ZUy5b+0S69SnA77IYI=
-github.com/qydysky/part v0.28.1-0.20230827094611-9921d2be4ddb/go.mod h1:iOK6EzUOqdqTyQZm+pf1qxYpKvLmdK+YHZehNN/2J3U=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
 github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=