adjust: code structure
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
package streamer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
c "live-streamer/config"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func buildFFmpegArgs(videoItem c.InputItem) []string {
|
||||
videoPath := videoItem.Path
|
||||
|
||||
args := []string{"-re"}
|
||||
if videoItem.Start != "" {
|
||||
args = append(args, "-ss", videoItem.Start)
|
||||
}
|
||||
|
||||
if videoItem.End != "" {
|
||||
args = append(args, "-to", videoItem.End)
|
||||
}
|
||||
|
||||
args = append(args,
|
||||
"-i", videoPath,
|
||||
)
|
||||
|
||||
for k, v := range config.Play {
|
||||
args = append(args, k)
|
||||
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.Output.RTMPServer, config.Output.StreamKey))
|
||||
|
||||
log.Debug("build ffmpeg", zap.Strings("args", args))
|
||||
|
||||
return args
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package streamer
|
||||
|
||||
import (
|
||||
c "live-streamer/config"
|
||||
)
|
||||
import "live-streamer/model"
|
||||
|
||||
type Message interface {
|
||||
messageType() string
|
||||
@@ -22,7 +20,7 @@ type GetCurrentVideoMessage struct {
|
||||
Response chan string
|
||||
}
|
||||
type GetVideoListMessage struct {
|
||||
Response chan []c.InputItem
|
||||
Response chan []model.VideoItem
|
||||
}
|
||||
type GetVideoListPathMessage struct {
|
||||
Response chan []string
|
||||
|
||||
@@ -3,10 +3,11 @@ package streamer
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
c "live-streamer/config"
|
||||
"live-streamer/logger"
|
||||
"live-streamer/model"
|
||||
"path/filepath"
|
||||
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -27,10 +28,21 @@ type Streamer struct {
|
||||
|
||||
wg sync.WaitGroup // wait all handlers(except closehandler) to finish before closure
|
||||
close chan any
|
||||
|
||||
log *zap.Logger
|
||||
option *Option
|
||||
|
||||
rtmpServer string
|
||||
streamKey string
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
FFmpegArgs map[string]any
|
||||
ShowFFmpegOutput bool
|
||||
}
|
||||
|
||||
type streamerState struct {
|
||||
videoList []c.InputItem
|
||||
videoList []model.VideoItem
|
||||
currentVideoIndex int
|
||||
manualControl bool
|
||||
cmd *exec.Cmd
|
||||
@@ -41,17 +53,10 @@ type streamerState struct {
|
||||
|
||||
var GlobalStreamer *Streamer
|
||||
|
||||
var (
|
||||
config *c.Config
|
||||
log *zap.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
config = c.GlobalConfig
|
||||
log = logger.GlobalLogger
|
||||
}
|
||||
|
||||
func NewStreamer(videoList []c.InputItem) *Streamer {
|
||||
func NewStreamer(rtmpServer string, streamKey string, videoList []model.VideoItem, log *zap.Logger, option *Option) (*Streamer, error) {
|
||||
if rtmpServer == "" || streamKey == "" {
|
||||
return nil, errors.New("lack of args")
|
||||
}
|
||||
s := &Streamer{
|
||||
mailbox: make(chan Message, 100),
|
||||
state: &streamerState{
|
||||
@@ -61,11 +66,15 @@ func NewStreamer(videoList []c.InputItem) *Streamer {
|
||||
outputQueue: make(chan string, 100),
|
||||
outputReq: make(chan chan string),
|
||||
close: make(chan any),
|
||||
option: option,
|
||||
log: log,
|
||||
rtmpServer: rtmpServer,
|
||||
streamKey: streamKey,
|
||||
}
|
||||
GlobalStreamer = s
|
||||
go s.actorLoop()
|
||||
go s.handleOutput()
|
||||
return s
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Streamer) actorLoop() {
|
||||
@@ -122,18 +131,18 @@ func (s *Streamer) handleStart() {
|
||||
s.state.ctx, s.state.cancel = context.WithCancel(context.Background())
|
||||
currentVideo := s.state.videoList[s.state.currentVideoIndex]
|
||||
videoPath := currentVideo.Path
|
||||
s.state.cmd = exec.CommandContext(s.state.ctx, "ffmpeg", buildFFmpegArgs(currentVideo)...)
|
||||
s.state.cmd = exec.CommandContext(s.state.ctx, "ffmpeg", s.buildFFmpegArgs(currentVideo)...)
|
||||
s.state.waitDone = make(chan any)
|
||||
|
||||
pipe, err := s.state.cmd.StderrPipe() // ffmpeg send all messages to stderr
|
||||
if err != nil {
|
||||
log.Error("failed to get pipe", zap.Error(err))
|
||||
s.log.Error("failed to get pipe", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(pipe)
|
||||
|
||||
log.Info("start stream", zap.String("path", videoPath))
|
||||
s.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))
|
||||
@@ -141,30 +150,30 @@ func (s *Streamer) handleStart() {
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Debug("wait stream end", zap.String("path", videoPath))
|
||||
s.log.Debug("wait stream end", zap.String("path", videoPath))
|
||||
_ = s.state.cmd.Wait()
|
||||
log.Debug("process stop", zap.String("path", videoPath))
|
||||
s.log.Debug("process stop", zap.String("path", videoPath))
|
||||
s.state.cancel()
|
||||
log.Debug("context cancel", zap.String("path", videoPath))
|
||||
s.log.Debug("context cancel", zap.String("path", videoPath))
|
||||
|
||||
s.writeOutput(fmt.Sprintf("stop stream: %s\n", videoPath))
|
||||
|
||||
if !s.state.manualControl {
|
||||
log.Debug("video end", zap.String("path", videoPath))
|
||||
s.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.Debug("manually end", zap.String("path", videoPath))
|
||||
s.log.Debug("manually end", zap.String("path", videoPath))
|
||||
s.state.manualControl = false
|
||||
}
|
||||
|
||||
close(s.state.waitDone)
|
||||
}()
|
||||
|
||||
go s.log(reader)
|
||||
go s.ffmpegLog(reader)
|
||||
}
|
||||
|
||||
func (s *Streamer) handleStop() {
|
||||
@@ -174,18 +183,18 @@ func (s *Streamer) handleStop() {
|
||||
|
||||
videoPath := s.state.videoList[s.state.currentVideoIndex].Path
|
||||
|
||||
log.Debug("wait context to be cancelled", zap.String("path", videoPath))
|
||||
s.log.Debug("wait context to be cancelled", zap.String("path", videoPath))
|
||||
s.state.cancel()
|
||||
log.Debug("context has been cancelled", zap.String("path", videoPath))
|
||||
s.log.Debug("context has been cancelled", zap.String("path", videoPath))
|
||||
|
||||
if s.state.cmd.Process != nil {
|
||||
log.Debug("wait ffmpeg process stop", zap.String("path", videoPath))
|
||||
s.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.Debug("ffmpeg process has stopped", zap.String("path", videoPath))
|
||||
s.log.Debug("ffmpeg process has stopped", zap.String("path", videoPath))
|
||||
}
|
||||
|
||||
s.state.cancel = nil
|
||||
@@ -193,7 +202,7 @@ func (s *Streamer) handleStop() {
|
||||
}
|
||||
|
||||
func (s *Streamer) handleAdd(path string) {
|
||||
s.state.videoList = append(s.state.videoList, c.InputItem{Path: path})
|
||||
s.state.videoList = append(s.state.videoList, model.VideoItem{Path: path})
|
||||
}
|
||||
|
||||
func (s *Streamer) handleRemove(path string) {
|
||||
@@ -260,7 +269,7 @@ func (s *Streamer) handleGetCurrentVideo(response chan string) {
|
||||
response <- s.state.videoList[s.state.currentVideoIndex].Path
|
||||
}
|
||||
|
||||
func (s *Streamer) handleGetVideoList(response chan []c.InputItem) {
|
||||
func (s *Streamer) handleGetVideoList(response chan []model.VideoItem) {
|
||||
response <- s.state.videoList
|
||||
}
|
||||
|
||||
@@ -314,8 +323,8 @@ func (s *Streamer) GetCurrentVideoPath() string {
|
||||
return <-response
|
||||
}
|
||||
|
||||
func (s *Streamer) GetVideoList() []c.InputItem {
|
||||
response := make(chan []c.InputItem)
|
||||
func (s *Streamer) GetVideoList() []model.VideoItem {
|
||||
response := make(chan []model.VideoItem)
|
||||
s.mailbox <- GetVideoListMessage{Response: response}
|
||||
return <-response
|
||||
}
|
||||
@@ -356,20 +365,21 @@ func (s *Streamer) writeOutput(str string) {
|
||||
s.outputQueue <- str
|
||||
}
|
||||
|
||||
func (s *Streamer) log(reader *bufio.Reader) {
|
||||
func (s *Streamer) ffmpegLog(reader *bufio.Reader) {
|
||||
select {
|
||||
case <-s.state.ctx.Done():
|
||||
return
|
||||
default:
|
||||
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 s.option.ShowFFmpegOutput {
|
||||
s.writeOutput(string(buf[:n]))
|
||||
}
|
||||
if s.log.Level() == zap.DebugLevel {
|
||||
fmt.Print(string(buf[:n]))
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
@@ -380,3 +390,38 @@ func (s *Streamer) log(reader *bufio.Reader) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Streamer) buildFFmpegArgs(videoItem model.VideoItem) []string {
|
||||
videoPath := videoItem.Path
|
||||
|
||||
args := []string{"-re"}
|
||||
if videoItem.Start != "" {
|
||||
args = append(args, "-ss", videoItem.Start)
|
||||
}
|
||||
|
||||
if videoItem.End != "" {
|
||||
args = append(args, "-to", videoItem.End)
|
||||
}
|
||||
|
||||
args = append(args,
|
||||
"-i", videoPath,
|
||||
)
|
||||
|
||||
for k, v := range s.option.FFmpegArgs {
|
||||
args = append(args, k)
|
||||
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", s.rtmpServer, s.streamKey))
|
||||
|
||||
s.log.Debug("build ffmpeg", zap.Strings("args", args))
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user