fix: ffmpeg process not exit after program exit

This commit is contained in:
Nite07 2024-10-25 03:41:48 +08:00
parent 6093f5d36b
commit 1c1028d857
3 changed files with 43 additions and 9 deletions

33
.github/workflows/release.yml vendored Normal file
View File

@ -0,0 +1,33 @@
name: release
on:
push:
tags:
- "v*"
permissions:
contents: write
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
- name: Install UPX
uses: crazy-max/ghaction-upx@v3
with:
install-only: true
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@ -109,10 +109,9 @@ func (s *Server) handleWebSocket(c *gin.Context) {
go func() {
ticker := time.NewTicker(1 * time.Second)
for range ticker.C {
currentVideoPath, _ := streamer.GlobalStreamer.GetCurrentVideoPath()
s.Broadcast(mywebsocket.Date{
Timestamp: time.Now().UnixMilli(),
CurrentVideoPath: currentVideoPath,
CurrentVideoPath: streamer.GlobalStreamer.GetCurrentVideoPath(),
VideoList: streamer.GlobalStreamer.GetVideoListPath(),
Output: streamer.GlobalStreamer.GetOutput(),
})

View File

@ -3,7 +3,6 @@ package streamer
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"live-streamer/config"
@ -21,6 +20,7 @@ type playState struct {
cmd *exec.Cmd
ctx context.Context
cancel context.CancelFunc
waitDone chan any
}
type Streamer struct {
@ -52,6 +52,7 @@ func (s *Streamer) start() {
currentVideo := s.videoList[s.playState.currentVideoIndex]
videoPath := currentVideo.Path
s.playState.cmd = exec.CommandContext(s.playState.ctx, "ffmpeg", s.buildFFmpegArgs(currentVideo)...)
s.playState.waitDone = make(chan any)
cmd := s.playState.cmd
s.playStateMu.Unlock()
@ -89,6 +90,7 @@ func (s *Streamer) start() {
}
s.videoMu.RUnlock()
}
close(s.playState.waitDone)
s.playStateMu.Unlock()
}
@ -108,7 +110,7 @@ func (s *Streamer) Stop() {
s.playState.cancel = nil
cmd := s.playState.cmd
s.playState.cmd = nil
ctx := s.playState.ctx
done := s.playState.waitDone
s.playStateMu.Unlock()
if cancel == nil || cmd == nil {
@ -119,7 +121,7 @@ func (s *Streamer) Stop() {
if cmd.Process != nil {
select {
case <-ctx.Done():
case <-done:
case <-time.After(3 * time.Second):
_ = cmd.Process.Kill()
}
@ -220,7 +222,7 @@ func (s *Streamer) log(reader *bufio.Reader) {
for {
n, err := reader.Read(buf)
if n > 0 {
videoPath, _ := s.GetCurrentVideoPath()
videoPath := s.GetCurrentVideoPath()
buf = append([]byte(videoPath), buf...)
s.writeOutput(string(buf[:n+len(videoPath)]))
}
@ -234,13 +236,13 @@ func (s *Streamer) log(reader *bufio.Reader) {
}
}
func (s *Streamer) GetCurrentVideoPath() (string, error) {
func (s *Streamer) GetCurrentVideoPath() string {
s.videoMu.RLock()
defer s.videoMu.RUnlock()
if len(s.videoList) == 0 {
return "", errors.New("no video streaming")
return ""
}
return s.videoList[s.GetCurrentIndex()].Path, nil
return s.videoList[s.GetCurrentIndex()].Path
}
func (s *Streamer) GetVideoList() []config.InputItem {