Skip to content

Commit

Permalink
fix ffmpeg race panic
Browse files Browse the repository at this point in the history
  • Loading branch information
kira1928 committed Jul 9, 2024
1 parent 29e25fd commit dcfe9b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ Bililive-go是一个支持多种直播平台的直播录制工具
<td>滋瓷</td>
<td></td>
</tr>
<tr align="center">
<td>twitch</td>
<td>www.twitch.tv</td>
<td>TODO</td>
<td></td>
</tr>
<tr align="center">
<td>OPENREC</td>
<td>www.openrec.tv</td>
Expand Down Expand Up @@ -120,12 +114,6 @@ Bililive-go是一个支持多种直播平台的直播录制工具
<td>滋瓷</td>
<td></td>
</tr>
<tr align="center">
<td>快手</td>
<td>live.kuaishou.com</td>
<td>滋瓷(不稳定)</td>
<td>滋瓷</td>
</tr>
<tr align="center">
<td>YY直播</td>
<td>www.yy.com</td>
Expand Down
37 changes: 25 additions & 12 deletions src/pkg/parser/ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Parser struct {

statusReq chan struct{}
statusResp chan map[string]string
cmdLock sync.Mutex
}

func (p *Parser) scanFFmpegStatus() <-chan []byte {
Expand Down Expand Up @@ -166,20 +167,30 @@ func (p *Parser) ParseLiveStream(ctx context.Context, url *url.URL, live live.Li
}

args = append(args, file)
p.cmd = exec.Command(ffmpegPath, args...)
if p.cmdStdIn, err = p.cmd.StdinPipe(); err != nil {
return err
}
if p.cmdStdout, err = p.cmd.StdoutPipe(); err != nil {
return err
}
if p.debug {
p.cmd.Stderr = os.Stderr
}
if err = p.cmd.Start(); err != nil {
p.cmd.Process.Kill()

// p.cmd operations need p.cmdLock
func() {
p.cmdLock.Lock()
defer p.cmdLock.Unlock()
p.cmd = exec.Command(ffmpegPath, args...)
if p.cmdStdIn, err = p.cmd.StdinPipe(); err != nil {
return
}
if p.cmdStdout, err = p.cmd.StdoutPipe(); err != nil {
return
}
if p.debug {
p.cmd.Stderr = os.Stderr
}
if err = p.cmd.Start(); err != nil {
p.cmd.Process.Kill()
return
}
}()
if err != nil {
return err
}

go p.scheduler()
err = p.cmd.Wait()
if err != nil {
Expand All @@ -190,6 +201,8 @@ func (p *Parser) ParseLiveStream(ctx context.Context, url *url.URL, live live.Li

func (p *Parser) Stop() (err error) {
p.closeOnce.Do(func() {
p.cmdLock.Lock()
defer p.cmdLock.Unlock()
if p.cmd.ProcessState == nil {
if p.cmdStdIn != nil && p.cmd.Process != nil {
if _, err = p.cmdStdIn.Write([]byte("q")); err != nil {
Expand Down

0 comments on commit dcfe9b4

Please sign in to comment.