live-streamer/streamer/streamer.go

325 lines
6.7 KiB
Go
Raw Normal View History

2024-10-22 16:39:10 -04:00
package streamer
import (
"bufio"
2024-10-23 03:40:10 -04:00
"context"
2024-10-22 16:39:10 -04:00
"fmt"
"io"
"live-streamer/config"
2024-10-23 14:35:37 -04:00
"log"
"os"
2024-10-22 16:39:10 -04:00
"os/exec"
"strings"
2024-10-23 04:37:43 -04:00
"sync"
2024-10-22 16:39:10 -04:00
"time"
)
2024-10-24 04:21:35 -04:00
type playState struct {
2024-10-22 16:39:10 -04:00
currentVideoIndex int
2024-10-24 04:21:35 -04:00
manualControl bool
2024-10-22 16:39:10 -04:00
cmd *exec.Cmd
2024-10-23 03:40:10 -04:00
ctx context.Context
cancel context.CancelFunc
waitDone chan any
2024-10-24 04:21:35 -04:00
}
type Streamer struct {
playStateMu sync.RWMutex
playState playState
videoMu sync.RWMutex
videoList []config.InputItem
outputMu sync.RWMutex
output strings.Builder
2024-10-22 16:39:10 -04:00
}
2024-10-23 10:47:37 -04:00
var GlobalStreamer *Streamer
2024-10-23 17:06:19 -04:00
func NewStreamer(videoList []config.InputItem) *Streamer {
2024-10-23 10:47:37 -04:00
GlobalStreamer = &Streamer{
2024-10-24 04:21:35 -04:00
videoList: videoList,
playState: playState{},
output: strings.Builder{},
2024-10-23 10:47:37 -04:00
}
return GlobalStreamer
}
func (s *Streamer) start() {
2024-10-24 04:21:35 -04:00
s.playStateMu.Lock()
s.playState.ctx, s.playState.cancel = context.WithCancel(context.Background())
cancel := s.playState.cancel
2024-10-24 04:21:35 -04:00
currentVideo := s.videoList[s.playState.currentVideoIndex]
2024-10-23 10:47:37 -04:00
videoPath := currentVideo.Path
2024-10-24 04:21:35 -04:00
s.playState.cmd = exec.CommandContext(s.playState.ctx, "ffmpeg", s.buildFFmpegArgs(currentVideo)...)
s.playState.waitDone = make(chan any)
2024-10-24 04:21:35 -04:00
cmd := s.playState.cmd
s.playStateMu.Unlock()
2024-10-23 17:06:19 -04:00
s.writeOutput(fmt.Sprintln("start stream: ", videoPath))
2024-10-24 04:21:35 -04:00
pipe, err := cmd.StderrPipe()
2024-10-23 10:47:37 -04:00
if err != nil {
2024-10-23 14:35:37 -04:00
log.Printf("failed to get pipe: %v", err)
2024-10-23 10:47:37 -04:00
return
}
reader := bufio.NewReader(pipe)
2024-10-24 04:21:35 -04:00
if err := cmd.Start(); err != nil {
2024-10-23 17:06:19 -04:00
s.writeOutput(fmt.Sprintf("starting ffmpeg error: %v\n", err))
2024-10-23 10:47:37 -04:00
return
}
go s.log(reader)
_ = cmd.Wait()
cancel()
2024-10-23 17:06:19 -04:00
s.writeOutput(fmt.Sprintf("stop stream: %s\n", videoPath))
2024-10-24 04:21:35 -04:00
s.playStateMu.Lock()
if s.playState.manualControl {
// manualing change video, don't increase currentVideoIndex
s.playState.manualControl = false
2024-10-23 17:06:19 -04:00
} else {
2024-10-24 04:21:35 -04:00
s.playState.currentVideoIndex++
s.videoMu.RLock()
if s.playState.currentVideoIndex >= len(s.videoList) {
s.playState.currentVideoIndex = 0
2024-10-23 17:06:19 -04:00
}
2024-10-24 04:21:35 -04:00
s.videoMu.RUnlock()
2024-10-23 17:06:19 -04:00
}
close(s.playState.waitDone)
2024-10-24 04:21:35 -04:00
s.playStateMu.Unlock()
2024-10-23 17:06:19 -04:00
}
2024-10-23 10:47:37 -04:00
2024-10-23 17:06:19 -04:00
func (s *Streamer) Stream() {
for {
if len(s.videoList) == 0 {
time.Sleep(time.Second)
continue
}
s.start()
2024-10-23 10:47:37 -04:00
}
}
func (s *Streamer) Stop() {
2024-10-24 04:21:35 -04:00
s.playStateMu.Lock()
cancel := s.playState.cancel
s.playState.cancel = nil
cmd := s.playState.cmd
2024-10-24 04:21:35 -04:00
s.playState.cmd = nil
done := s.playState.waitDone
2024-10-24 04:21:35 -04:00
s.playStateMu.Unlock()
if cancel == nil || cmd == nil {
return
2024-10-22 16:39:10 -04:00
}
2024-10-24 04:21:35 -04:00
cancel()
if cmd.Process != nil {
select {
case <-done:
2024-10-24 04:21:35 -04:00
case <-time.After(3 * time.Second):
_ = cmd.Process.Kill()
}
}
2024-10-23 17:06:19 -04:00
}
2024-10-22 16:39:10 -04:00
func (s *Streamer) Add(videoPath string) {
2024-10-24 04:21:35 -04:00
s.videoMu.Lock()
defer s.videoMu.Unlock()
2024-10-23 03:40:10 -04:00
s.videoList = append(s.videoList, config.InputItem{Path: videoPath})
2024-10-22 16:39:10 -04:00
}
func (s *Streamer) Remove(videoPath string) {
2024-10-24 04:21:35 -04:00
var needStop bool // removed video is current playing
var removeIndex int = -1
s.videoMu.Lock()
2024-10-23 03:40:10 -04:00
for i, item := range s.videoList {
2024-10-22 16:39:10 -04:00
if item.Path == videoPath {
2024-10-24 04:21:35 -04:00
removeIndex = i
s.playStateMu.RLock()
needStop = (s.playState.currentVideoIndex == i)
s.playStateMu.RUnlock()
2024-10-22 16:39:10 -04:00
break
}
}
2024-10-24 04:21:35 -04:00
if removeIndex >= 0 && removeIndex < len(s.videoList) {
oldLen := len(s.videoList)
s.videoList = append(s.videoList[:removeIndex], s.videoList[removeIndex+1:]...)
s.playStateMu.Lock()
if s.playState.currentVideoIndex >= oldLen-1 {
s.playState.currentVideoIndex = 0
}
s.playStateMu.Unlock()
}
s.videoMu.Unlock()
if needStop {
s.Stop()
}
2024-10-22 16:39:10 -04:00
}
func (s *Streamer) Prev() {
2024-10-24 04:21:35 -04:00
s.videoMu.RLock()
videoLen := len(s.videoList)
if videoLen == 0 {
return
}
s.videoMu.RUnlock()
s.playStateMu.Lock()
s.playState.manualControl = true
s.playState.currentVideoIndex--
if s.playState.currentVideoIndex < 0 {
s.playState.currentVideoIndex = videoLen - 1
2024-10-22 16:39:10 -04:00
}
2024-10-24 04:21:35 -04:00
s.playStateMu.Unlock()
2024-10-23 04:37:43 -04:00
s.Stop()
2024-10-22 16:39:10 -04:00
}
func (s *Streamer) Next() {
2024-10-24 04:21:35 -04:00
s.videoMu.RLock()
videoLen := len(s.videoList)
if videoLen == 0 {
return
}
s.videoMu.RUnlock()
s.playStateMu.Lock()
s.playState.manualControl = true
s.playState.currentVideoIndex++
if s.playState.currentVideoIndex >= videoLen {
s.playState.currentVideoIndex = 0
2024-10-22 16:39:10 -04:00
}
2024-10-24 04:21:35 -04:00
s.playStateMu.Unlock()
2024-10-23 04:37:43 -04:00
s.Stop()
2024-10-22 16:39:10 -04:00
}
2024-10-23 10:47:37 -04:00
func (s *Streamer) log(reader *bufio.Reader) {
2024-10-24 04:21:35 -04:00
s.playStateMu.RLock()
ctx := s.playState.ctx
s.playStateMu.RUnlock()
2024-10-23 10:47:37 -04:00
select {
2024-10-24 04:21:35 -04:00
case <-ctx.Done():
2024-10-23 10:47:37 -04:00
return
default:
if !config.GlobalConfig.Log.PlayState {
return
}
buf := make([]byte, 1024)
for {
n, err := reader.Read(buf)
if n > 0 {
videoPath := s.GetCurrentVideoPath()
2024-10-23 10:47:37 -04:00
buf = append([]byte(videoPath), buf...)
2024-10-23 17:06:19 -04:00
s.writeOutput(string(buf[:n+len(videoPath)]))
2024-10-23 10:47:37 -04:00
}
if err != nil {
if err != io.EOF {
2024-10-23 17:06:19 -04:00
s.writeOutput(fmt.Sprintf("reading ffmpeg output error: %v\n", err))
2024-10-23 10:47:37 -04:00
}
break
}
2024-10-22 16:39:10 -04:00
}
}
}
func (s *Streamer) GetCurrentVideoPath() string {
2024-10-24 04:21:35 -04:00
s.videoMu.RLock()
defer s.videoMu.RUnlock()
2024-10-23 17:06:19 -04:00
if len(s.videoList) == 0 {
return ""
2024-10-23 17:06:19 -04:00
}
return s.videoList[s.GetCurrentIndex()].Path
2024-10-23 17:06:19 -04:00
}
func (s *Streamer) GetVideoList() []config.InputItem {
2024-10-24 04:21:35 -04:00
s.videoMu.RLock()
defer s.videoMu.RUnlock()
2024-10-23 17:06:19 -04:00
return s.videoList
}
func (s *Streamer) GetVideoListPath() []string {
2024-10-24 04:21:35 -04:00
s.videoMu.RLock()
defer s.videoMu.RUnlock()
2024-10-23 17:06:19 -04:00
var videoList []string
for _, item := range s.videoList {
videoList = append(videoList, item.Path)
}
return videoList
}
func (s *Streamer) GetCurrentIndex() int {
2024-10-24 04:21:35 -04:00
s.playStateMu.RLock()
defer s.playStateMu.RUnlock()
return s.playState.currentVideoIndex
2024-10-23 17:06:19 -04:00
}
2024-10-24 04:21:35 -04:00
func (s *Streamer) writeOutput(str string) {
s.outputMu.Lock()
defer s.outputMu.Unlock()
s.output.WriteString(str)
2024-10-23 17:06:19 -04:00
}
2024-10-24 04:21:35 -04:00
func (s *Streamer) GetOutput() string {
s.outputMu.RLock()
defer s.outputMu.RUnlock()
return s.output.String()
2024-10-23 17:06:19 -04:00
}
func (s *Streamer) Close() {
s.Stop()
os.Exit(0)
2024-10-23 17:06:19 -04:00
}
2024-10-22 16:39:10 -04:00
func (s *Streamer) buildFFmpegArgs(videoItem config.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,
2024-10-22 16:39:10 -04:00
"-c:v", config.GlobalConfig.Play.VideoCodec,
"-preset", config.GlobalConfig.Play.Preset,
"-crf", fmt.Sprintf("%d", config.GlobalConfig.Play.CRF),
"-maxrate", config.GlobalConfig.Play.MaxRate,
"-bufsize", config.GlobalConfig.Play.BufSize,
"-vf", fmt.Sprintf("scale=%s", config.GlobalConfig.Play.Scale),
"-r", fmt.Sprintf("%d", config.GlobalConfig.Play.FrameRate),
"-c:a", config.GlobalConfig.Play.AudioCodec,
"-b:a", config.GlobalConfig.Play.AudioBitrate,
"-ar", fmt.Sprintf("%d", config.GlobalConfig.Play.AudioSampleRate),
"-f", config.GlobalConfig.Play.OutputFormat,
)
if config.GlobalConfig.Play.CustomArgs != "" {
customArgs := strings.Fields(config.GlobalConfig.Play.CustomArgs)
args = append(args, customArgs...)
}
args = append(args, fmt.Sprintf("%s/%s", config.GlobalConfig.Output.RTMPServer, config.GlobalConfig.Output.StreamKey))
log.Println("ffmpeg args: ", args)
2024-10-22 16:39:10 -04:00
return args
}