feat: log level config

feat: ffmpeg args placeholder
This commit is contained in:
2024-10-29 03:35:21 +08:00
parent 2afcac48dc
commit dfdb6003ea
23 changed files with 212 additions and 71 deletions

24
streamer/helper.go Normal file → Executable file
View File

@@ -2,11 +2,14 @@ package streamer
import (
"fmt"
"live-streamer/config"
"log"
c "live-streamer/config"
"path/filepath"
"strings"
"go.uber.org/zap"
)
func buildFFmpegArgs(videoItem config.InputItem) []string {
func buildFFmpegArgs(videoItem c.InputItem) []string {
videoPath := videoItem.Path
args := []string{"-re"}
@@ -22,14 +25,21 @@ func buildFFmpegArgs(videoItem config.InputItem) []string {
"-i", videoPath,
)
for k, v := range config.GlobalConfig.Play {
for k, v := range config.Play {
args = append(args, k)
args = append(args, fmt.Sprint(v))
if str, ok := v.(string); ok {
filename := strings.TrimSuffix(filepath.Base(videoPath), filepath.Ext(videoPath))
str = strings.ReplaceAll(str, "{{filepath}}", videoPath)
str = strings.ReplaceAll(str, "{{filename}}", filename)
args = append(args, str)
} else {
args = append(args, fmt.Sprint(v))
}
}
args = append(args, fmt.Sprintf("%s/%s", config.GlobalConfig.Output.RTMPServer, config.GlobalConfig.Output.StreamKey))
args = append(args, fmt.Sprintf("%s/%s", config.Output.RTMPServer, config.Output.StreamKey))
log.Println("ffmpeg args: ", args)
log.Debug("build ffmpeg", zap.Strings("args", args))
return args
}

6
streamer/message.go Normal file → Executable file
View File

@@ -1,6 +1,8 @@
package streamer
import "live-streamer/config"
import (
c "live-streamer/config"
)
type Message interface {
messageType() string
@@ -20,7 +22,7 @@ type GetCurrentVideoMessage struct {
Response chan string
}
type GetVideoListMessage struct {
Response chan []config.InputItem
Response chan []c.InputItem
}
type GetVideoListPathMessage struct {
Response chan []string

85
streamer/streamer.go Normal file → Executable file
View File

@@ -5,13 +5,16 @@ import (
"context"
"fmt"
"io"
"live-streamer/config"
"log"
c "live-streamer/config"
"live-streamer/logger"
"os"
"os/exec"
"strings"
"sync"
"time"
"go.uber.org/zap"
)
type Streamer struct {
@@ -22,11 +25,12 @@ type Streamer struct {
outputQueue chan string
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
}
type streamerState struct {
videoList []config.InputItem
videoList []c.InputItem
currentVideoIndex int
manualControl bool
cmd *exec.Cmd
@@ -37,7 +41,17 @@ type streamerState struct {
var GlobalStreamer *Streamer
func NewStreamer(videoList []config.InputItem) *Streamer {
var (
config *c.Config
log *zap.Logger
)
func init() {
config = c.GlobalConfig
log = logger.GlobalLogger
}
func NewStreamer(videoList []c.InputItem) *Streamer {
s := &Streamer{
mailbox: make(chan Message, 100),
state: &streamerState{
@@ -46,6 +60,7 @@ func NewStreamer(videoList []config.InputItem) *Streamer {
output: strings.Builder{},
outputQueue: make(chan string, 100),
outputReq: make(chan chan string),
close: make(chan any),
}
GlobalStreamer = s
go s.actorLoop()
@@ -54,13 +69,18 @@ func NewStreamer(videoList []config.InputItem) *Streamer {
}
func (s *Streamer) actorLoop() {
for msg := range s.mailbox {
if msg.messageType() != CloseMessage.messageType(CloseMessage{}) {
s.wg.Add(1)
s.handleMessage(msg)
s.wg.Done()
} else {
s.handleMessage(msg)
for {
select {
case <-s.close:
return
case msg := <-s.mailbox:
if _, ok := msg.(CloseMessage); !ok {
s.wg.Add(1)
s.handleMessage(msg)
s.wg.Done()
} else {
s.handleMessage(msg)
}
}
}
}
@@ -105,43 +125,46 @@ func (s *Streamer) handleStart() {
s.state.cmd = exec.CommandContext(s.state.ctx, "ffmpeg", buildFFmpegArgs(currentVideo)...)
s.state.waitDone = make(chan any)
s.writeOutput(fmt.Sprintln("start stream: ", videoPath))
pipe, err := s.state.cmd.StderrPipe() // ffmpeg send all messages to stderr
if err != nil {
log.Printf("failed to get pipe: %v", err)
log.Error("failed to get pipe", zap.Error(err))
return
}
reader := bufio.NewReader(pipe)
log.Info("start stream", zap.String("path", videoPath))
s.writeOutput(fmt.Sprintln("start stream: ", videoPath))
if err := s.state.cmd.Start(); err != nil {
s.writeOutput(fmt.Sprintf("starting ffmpeg error: %v\n", err))
return
}
go s.log(reader)
go func() {
log.Debug("wait stream end", zap.String("path", videoPath))
_ = s.state.cmd.Wait()
log.Debug("process stop", zap.String("path", videoPath))
s.state.cancel()
log.Debug("context cancel", zap.String("path", videoPath))
s.writeOutput(fmt.Sprintf("stop stream: %s\n", videoPath))
if !s.state.manualControl {
log.Println("ready to stream next video")
log.Debug("video end", zap.String("path", videoPath))
s.state.currentVideoIndex++
if s.state.currentVideoIndex >= len(s.state.videoList) {
s.state.currentVideoIndex = 0
}
s.mailbox <- StartMessage{}
} else {
log.Println("manually control")
log.Debug("manually end", zap.String("path", videoPath))
s.state.manualControl = false
}
close(s.state.waitDone)
}()
go s.log(reader)
}
func (s *Streamer) handleStop() {
@@ -149,18 +172,20 @@ func (s *Streamer) handleStop() {
return
}
log.Println("wait context to be cancelled")
videoPath := s.state.videoList[s.state.currentVideoIndex].Path
log.Debug("wait context to be cancelled", zap.String("path", videoPath))
s.state.cancel()
log.Println("context has been cancelled")
log.Debug("context has been cancelled", zap.String("path", videoPath))
if s.state.cmd.Process != nil {
log.Println("wait ffmpeg process stop")
log.Debug("wait ffmpeg process stop", zap.String("path", videoPath))
select {
case <-s.state.waitDone:
case <-time.After(3 * time.Second):
_ = s.state.cmd.Process.Kill()
}
log.Println("ffmpeg process has stopped")
log.Debug("ffmpeg process has stopped", zap.String("path", videoPath))
}
s.state.cancel = nil
@@ -168,7 +193,7 @@ func (s *Streamer) handleStop() {
}
func (s *Streamer) handleAdd(path string) {
s.state.videoList = append(s.state.videoList, config.InputItem{Path: path})
s.state.videoList = append(s.state.videoList, c.InputItem{Path: path})
}
func (s *Streamer) handleRemove(path string) {
@@ -235,7 +260,7 @@ func (s *Streamer) handleGetCurrentVideo(response chan string) {
response <- s.state.videoList[s.state.currentVideoIndex].Path
}
func (s *Streamer) handleGetVideoList(response chan []config.InputItem) {
func (s *Streamer) handleGetVideoList(response chan []c.InputItem) {
response <- s.state.videoList
}
@@ -252,6 +277,8 @@ func (s *Streamer) handleGetCurrentIndex(response chan int) {
}
func (s *Streamer) handleClose() {
close(s.close)
s.handleStop()
s.wg.Wait()
os.Exit(0)
}
@@ -287,8 +314,8 @@ func (s *Streamer) GetCurrentVideoPath() string {
return <-response
}
func (s *Streamer) GetVideoList() []config.InputItem {
response := make(chan []config.InputItem)
func (s *Streamer) GetVideoList() []c.InputItem {
response := make(chan []c.InputItem)
s.mailbox <- GetVideoListMessage{Response: response}
return <-response
}
@@ -306,7 +333,6 @@ func (s *Streamer) GetCurrentIndex() int {
}
func (s *Streamer) Close() {
s.mailbox <- StopMessage{}
s.mailbox <- CloseMessage{}
}
@@ -335,13 +361,14 @@ func (s *Streamer) log(reader *bufio.Reader) {
case <-s.state.ctx.Done():
return
default:
if !config.GlobalConfig.Log.PlayState {
if !config.Log.PlayState {
return
}
buf := make([]byte, 1024)
for {
n, err := reader.Read(buf)
if n > 0 {
log.Debug("ffmpeg output", zap.String("msg", strings.TrimSpace(string(buf[:n]))))
s.writeOutput(string(buf[:n]))
}
if err != nil {