]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Improve 录制保存到文件添加context
authorqydysky <qydysky@foxmail.com>
Thu, 30 Mar 2023 04:15:12 +0000 (04:15 +0000)
committerqydysky <qydysky@foxmail.com>
Thu, 30 Mar 2023 04:15:12 +0000 (04:15 +0000)
Reply/stream.go

index 307311ba86689a9c5b51be7c579702f2c13eff1f..88d7ef836c7fe84bbb98dcd0a00bf5b8e27b5f32 100644 (file)
@@ -1255,7 +1255,7 @@ func (t *M4SStream) Stop() {
 
 // 保存到文件
 // filepath: 不包含后缀,会自动添加后缀
-func (t *M4SStream) PusherToFile(filepath string, startFunc func(*M4SStream) error, stopFunc func(*M4SStream) error) error {
+func (t *M4SStream) PusherToFile(cont context.Context, filepath string, startFunc func(*M4SStream) error, stopFunc func(*M4SStream) error) error {
        f := file.New(filepath+"."+t.stream_type, 0, true)
        if e := f.Delete(); e != nil {
                return e
@@ -1269,9 +1269,14 @@ func (t *M4SStream) PusherToFile(filepath string, startFunc func(*M4SStream) err
        if len(t.boot_buf) != 0 {
                f.Write(t.boot_buf, true)
        }
-       contextC, cancel := context.WithCancel(context.Background())
+       contextC, cancel := context.WithCancel(cont)
        t.Stream_msg.Pull_tag(map[string]func([]byte) bool{
                `data`: func(b []byte) bool {
+                       select {
+                       case <-contextC.Done():
+                               return true
+                       default:
+                       }
                        if len(b) == 0 {
                                cancel()
                                return true