prevent exiting program when close streamer

This commit is contained in:
Nite07 2024-11-20 18:41:02 +08:00
parent 817bcdcefe
commit 8138c0df63
3 changed files with 16 additions and 12 deletions

View File

@ -77,6 +77,7 @@ func inputHandler() {
GlobalStreamer.Prev() GlobalStreamer.Prev()
case "quit": case "quit":
GlobalStreamer.Close() GlobalStreamer.Close()
os.Exit(0)
case "current": case "current":
fmt.Println(GlobalStreamer.GetCurrentVideoPath()) fmt.Println(GlobalStreamer.GetCurrentVideoPath())
} }

View File

@ -5,6 +5,7 @@ import (
"html/template" "html/template"
"live-streamer/streamer" "live-streamer/streamer"
"net/http" "net/http"
"os"
"sync" "sync"
"time" "time"
@ -180,5 +181,6 @@ func (s *Server) RequestHandler(reqType RequestType) {
s.streamer.Prev() s.streamer.Prev()
case TypeQuit: case TypeQuit:
s.streamer.Close() s.streamer.Close()
os.Exit(0)
} }
} }

View File

@ -9,7 +9,6 @@ import (
"live-streamer/model" "live-streamer/model"
"path/filepath" "path/filepath"
"os"
"os/exec" "os/exec"
"strings" "strings"
"sync" "sync"
@ -27,7 +26,8 @@ type Streamer struct {
outputReq chan chan string // address output concurrency security issue outputReq chan chan string // address output concurrency security issue
wg sync.WaitGroup // wait all handlers(except closehandler) to finish before closure wg sync.WaitGroup // wait all handlers(except closehandler) to finish before closure
close chan any close chan any // inform actorLoop() to return
closed chan any // inform Close() to return
log *zap.Logger log *zap.Logger
option *Option option *Option
@ -70,6 +70,7 @@ func NewStreamer(rtmpServer string, streamKey string, videoList []model.VideoIte
log: log, log: log,
rtmpServer: rtmpServer, rtmpServer: rtmpServer,
streamKey: streamKey, streamKey: streamKey,
closed: make(chan any),
} }
GlobalStreamer = s GlobalStreamer = s
go s.actorLoop() go s.actorLoop()
@ -237,10 +238,7 @@ func (s *Streamer) handleNext() {
} }
s.state.manualControl = true s.state.manualControl = true
s.state.currentVideoIndex++ s.state.currentVideoIndex = (s.state.currentVideoIndex + 1) % len(s.state.videoList)
if s.state.currentVideoIndex >= len(s.state.videoList) {
s.state.currentVideoIndex = 0
}
s.mailbox <- StopMessage{} s.mailbox <- StopMessage{}
s.mailbox <- StartMessage{} s.mailbox <- StartMessage{}
@ -252,10 +250,7 @@ func (s *Streamer) handlePrev() {
} }
s.state.manualControl = true s.state.manualControl = true
s.state.currentVideoIndex-- s.state.currentVideoIndex = (s.state.currentVideoIndex - 1 + len(s.state.videoList)) % len(s.state.videoList)
if s.state.currentVideoIndex < 0 {
s.state.currentVideoIndex = len(s.state.videoList) - 1
}
s.mailbox <- StopMessage{} s.mailbox <- StopMessage{}
s.mailbox <- StartMessage{} s.mailbox <- StartMessage{}
@ -289,7 +284,12 @@ func (s *Streamer) handleClose() {
close(s.close) close(s.close)
s.handleStop() s.handleStop()
s.wg.Wait() s.wg.Wait()
os.Exit(0) close(s.mailbox)
close(s.outputQueue)
close(s.outputReq)
s.output.Reset()
GlobalStreamer = nil
close(s.closed)
} }
// Public methods that send messages to the actor // Public methods that send messages to the actor
@ -343,6 +343,7 @@ func (s *Streamer) GetCurrentIndex() int {
func (s *Streamer) Close() { func (s *Streamer) Close() {
s.mailbox <- CloseMessage{} s.mailbox <- CloseMessage{}
<-s.closed
} }
func (s *Streamer) handleOutput() { func (s *Streamer) handleOutput() {