From 3ebb602315fa8154338884c0ff5c788712fbec83 Mon Sep 17 00:00:00 2001 From: Joftal <42807405+Joftal@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:24:50 +0800 Subject: [PATCH 1/2] Update ffmpeg.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 规避潜在的 panic 错误 --- src/pkg/parser/ffmpeg/ffmpeg.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pkg/parser/ffmpeg/ffmpeg.go b/src/pkg/parser/ffmpeg/ffmpeg.go index d1be074f..f020f66d 100644 --- a/src/pkg/parser/ffmpeg/ffmpeg.go +++ b/src/pkg/parser/ffmpeg/ffmpeg.go @@ -189,8 +189,10 @@ func (p *Parser) ParseLiveStream(ctx context.Context, url *url.URL, live live.Li func (p *Parser) Stop() error { p.closeOnce.Do(func() { - if p.cmd.ProcessState == nil { - p.cmdStdIn.Write([]byte("q")) + if p.cmdStdIn != nil && p.cmd.Process != nil && p.cmd.ProcessState == nil { + if _, err := p.cmdStdIn.Write([]byte("q")); err != nil { + log.Printf("Error sending stop command to ffmpeg: %v", err) + } } }) return nil From 9bef94a8fdb61ae54ffc1a98806e8fe1d65a676d Mon Sep 17 00:00:00 2001 From: kira1928 Date: Mon, 12 Feb 2024 20:12:22 +0900 Subject: [PATCH 2/2] log refine --- src/pkg/parser/ffmpeg/ffmpeg.go | 19 +++++++++++++------ src/recorders/recorder.go | 8 ++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/pkg/parser/ffmpeg/ffmpeg.go b/src/pkg/parser/ffmpeg/ffmpeg.go index f020f66d..7147ba83 100644 --- a/src/pkg/parser/ffmpeg/ffmpeg.go +++ b/src/pkg/parser/ffmpeg/ffmpeg.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "fmt" "io" "net/url" "os" @@ -187,13 +188,19 @@ func (p *Parser) ParseLiveStream(ctx context.Context, url *url.URL, live live.Li return nil } -func (p *Parser) Stop() error { +func (p *Parser) Stop() (err error) { p.closeOnce.Do(func() { - if p.cmdStdIn != nil && p.cmd.Process != nil && p.cmd.ProcessState == nil { - if _, err := p.cmdStdIn.Write([]byte("q")); err != nil { - log.Printf("Error sending stop command to ffmpeg: %v", err) - } + if p.cmd.ProcessState == nil { + if p.cmdStdIn != nil && p.cmd.Process != nil { + if _, err = p.cmdStdIn.Write([]byte("q")); err != nil { + err = fmt.Errorf("error sending stop command to ffmpeg: %v", err) + } + } else if p.cmdStdIn == nil { + err = fmt.Errorf("p.cmdStdIn == nil") + } else if p.cmd.Process == nil { + err = fmt.Errorf("p.cmd.Process == nil") + } } }) - return nil + return err } diff --git a/src/recorders/recorder.go b/src/recorders/recorder.go index 0198578f..d7910a92 100644 --- a/src/recorders/recorder.go +++ b/src/recorders/recorder.go @@ -253,7 +253,9 @@ func (r *recorder) setAndCloseParser(p parser.Parser) { r.parserLock.Lock() defer r.parserLock.Unlock() if r.parser != nil { - r.parser.Stop() + if err := r.parser.Stop(); err != nil { + r.getLogger().WithError(err).Warn("failed to end recorder") + } } r.parser = p } @@ -279,7 +281,9 @@ func (r *recorder) Close() { } close(r.stop) if p := r.getParser(); p != nil { - p.Stop() + if err := p.Stop(); err != nil { + r.getLogger().WithError(err).Warn("failed to end recorder") + } } r.getLogger().Info("Record End") r.ed.DispatchEvent(events.NewEvent(RecorderStop, r.Live))