2024-10-22 16:39:10 -04:00
|
|
|
package streamer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-10-23 03:40:10 -04:00
|
|
|
"context"
|
2024-10-23 10:47:37 -04:00
|
|
|
"errors"
|
2024-10-22 16:39:10 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"live-streamer/config"
|
2024-10-23 14:35:37 -04:00
|
|
|
"log"
|
2024-10-23 17:06:19 -04:00
|
|
|
"math"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Streamer struct {
|
2024-10-23 03:40:10 -04:00
|
|
|
videoList []config.InputItem
|
2024-10-22 16:39:10 -04:00
|
|
|
currentVideoIndex int
|
|
|
|
cmd *exec.Cmd
|
2024-10-23 03:40:10 -04:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2024-10-23 17:06:19 -04:00
|
|
|
output strings.Builder
|
|
|
|
manualControl bool
|
2024-10-23 04:37:43 -04:00
|
|
|
mu sync.Mutex
|
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-23 03:40:10 -04:00
|
|
|
videoList: videoList,
|
2024-10-22 16:39:10 -04:00
|
|
|
currentVideoIndex: 0,
|
|
|
|
cmd: nil,
|
2024-10-23 03:40:10 -04:00
|
|
|
ctx: nil,
|
2024-10-23 10:47:37 -04:00
|
|
|
}
|
|
|
|
return GlobalStreamer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streamer) start() {
|
2024-10-23 17:06:19 -04:00
|
|
|
s.mu.Lock()
|
2024-10-23 10:47:37 -04:00
|
|
|
s.ctx, s.cancel = context.WithCancel(context.Background())
|
|
|
|
currentVideo := s.videoList[s.currentVideoIndex]
|
|
|
|
videoPath := currentVideo.Path
|
|
|
|
s.cmd = exec.CommandContext(s.ctx, "ffmpeg", s.buildFFmpegArgs(currentVideo)...)
|
|
|
|
s.mu.Unlock()
|
2024-10-23 17:06:19 -04:00
|
|
|
s.writeOutput(fmt.Sprintln("start stream: ", videoPath))
|
2024-10-23 10:47:37 -04:00
|
|
|
pipe, err := s.cmd.StderrPipe()
|
|
|
|
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)
|
|
|
|
|
|
|
|
if err := s.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)
|
|
|
|
|
|
|
|
<-s.ctx.Done()
|
2024-10-23 17:06:19 -04:00
|
|
|
s.writeOutput(fmt.Sprintf("stop stream: %s\n", videoPath))
|
|
|
|
|
|
|
|
if s.manualControl {
|
|
|
|
s.manualControl = false
|
|
|
|
} else {
|
|
|
|
// stream next video
|
|
|
|
s.currentVideoIndex++
|
|
|
|
if s.currentVideoIndex >= len(s.videoList) {
|
|
|
|
s.currentVideoIndex = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-23 17:06:19 -04:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2024-10-23 10:47:37 -04:00
|
|
|
if s.cancel != nil {
|
|
|
|
stopped := make(chan error)
|
|
|
|
go func() {
|
2024-10-23 17:06:19 -04:00
|
|
|
if s.cmd != nil {
|
|
|
|
stopped <- s.cmd.Wait()
|
|
|
|
}
|
2024-10-23 10:47:37 -04:00
|
|
|
}()
|
|
|
|
s.cancel()
|
|
|
|
if s.cmd != nil && s.cmd.Process != nil {
|
|
|
|
select {
|
|
|
|
case <-stopped:
|
|
|
|
break
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
_ = s.cmd.Process.Kill()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s.cmd = nil
|
|
|
|
}
|
2024-10-22 16:39:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 17:06:19 -04:00
|
|
|
func (s *Streamer) writeOutput(str string) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.output.WriteString(str)
|
|
|
|
}
|
|
|
|
|
2024-10-22 16:39:10 -04:00
|
|
|
func (s *Streamer) Add(videoPath string) {
|
2024-10-23 17:06:19 -04:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.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-23 17:06:19 -04:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
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-23 03:40:10 -04:00
|
|
|
s.videoList = append(s.videoList[:i], s.videoList[i+1:]...)
|
|
|
|
if s.currentVideoIndex >= len(s.videoList) {
|
2024-10-22 16:39:10 -04:00
|
|
|
s.currentVideoIndex = 0
|
|
|
|
}
|
|
|
|
if s.currentVideoIndex == i {
|
2024-10-23 04:37:43 -04:00
|
|
|
s.Stop()
|
2024-10-22 16:39:10 -04:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streamer) Prev() {
|
2024-10-23 17:06:19 -04:00
|
|
|
s.mu.Lock()
|
|
|
|
s.manualControl = true
|
2024-10-22 16:39:10 -04:00
|
|
|
s.currentVideoIndex--
|
|
|
|
if s.currentVideoIndex < 0 {
|
2024-10-23 03:40:10 -04:00
|
|
|
s.currentVideoIndex = len(s.videoList) - 1
|
2024-10-22 16:39:10 -04:00
|
|
|
}
|
2024-10-23 17:06:19 -04:00
|
|
|
s.mu.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-23 17:06:19 -04:00
|
|
|
s.mu.Lock()
|
|
|
|
s.manualControl = true
|
2024-10-22 16:39:10 -04:00
|
|
|
s.currentVideoIndex++
|
2024-10-23 03:40:10 -04:00
|
|
|
if s.currentVideoIndex >= len(s.videoList) {
|
2024-10-22 16:39:10 -04:00
|
|
|
s.currentVideoIndex = 0
|
|
|
|
}
|
2024-10-23 17:06:19 -04:00
|
|
|
s.mu.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) {
|
|
|
|
select {
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if !config.GlobalConfig.Log.PlayState {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
n, err := reader.Read(buf)
|
|
|
|
if n > 0 {
|
|
|
|
videoPath, _ := s.GetCurrentVideoPath()
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 17:06:19 -04:00
|
|
|
func (s *Streamer) GetCurrentVideoPath() (string, error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if len(s.videoList) == 0 {
|
|
|
|
return "", errors.New("no video streaming")
|
|
|
|
}
|
|
|
|
return s.videoList[s.currentVideoIndex].Path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streamer) GetVideoList() []config.InputItem {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.videoList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streamer) GetVideoListPath() []string {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
var videoList []string
|
|
|
|
for _, item := range s.videoList {
|
|
|
|
videoList = append(videoList, item.Path)
|
|
|
|
}
|
|
|
|
return videoList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streamer) GetCurrentIndex() int {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.currentVideoIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streamer) GetOutput() string {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.output.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streamer) TruncateOutput() int {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
currentSize := s.output.Len()
|
|
|
|
if currentSize > math.MaxInt {
|
|
|
|
newStart := currentSize - math.MaxInt
|
|
|
|
trimmedOutput := s.output.String()[newStart:]
|
|
|
|
s.output.Reset()
|
|
|
|
s.output.WriteString(trimmedOutput)
|
|
|
|
}
|
|
|
|
return currentSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streamer) Close() {
|
|
|
|
s.Stop()
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, "-i", videoPath)
|
|
|
|
|
|
|
|
if videoItem.End != "" {
|
|
|
|
args = append(args, "-to", videoItem.End)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args,
|
|
|
|
"-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,
|
|
|
|
"-stats", "-loglevel", "info",
|
|
|
|
)
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
2024-10-23 10:47:37 -04:00
|
|
|
// logger.GlobalLogger.Println("ffmpeg args: ", args)
|
2024-10-22 16:39:10 -04:00
|
|
|
|
|
|
|
return args
|
|
|
|
}
|