115 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			115 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
		
			Executable File
		
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"bufio"
 | |
| 	"fmt"
 | |
| 
 | |
| 	c "live-streamer/config"
 | |
| 	"live-streamer/constant"
 | |
| 	"live-streamer/logger"
 | |
| 	"live-streamer/server"
 | |
| 	"live-streamer/streamer"
 | |
| 	"live-streamer/utils"
 | |
| 	"live-streamer/websocket"
 | |
| 
 | |
| 	"os"
 | |
| 
 | |
| 	"github.com/fsnotify/fsnotify"
 | |
| 	"go.uber.org/zap"
 | |
| )
 | |
| 
 | |
| var (
 | |
| 	GlobalStreamer *streamer.Streamer
 | |
| 	log            *zap.Logger
 | |
| 	config         *c.Config
 | |
| )
 | |
| 
 | |
| func init() {
 | |
| 	config = c.GlobalConfig
 | |
| 	log = logger.GlobalLogger
 | |
| }
 | |
| 
 | |
| func main() {
 | |
| 	fmt.Println("Version: " + constant.Version)
 | |
| 
 | |
| 	if !utils.HasFFMPEG() {
 | |
| 		log.Fatal("ffmpeg not found")
 | |
| 	}
 | |
| 
 | |
| 	server.NewServer(config.Server.Addr, websocket.RequestHandler)
 | |
| 	server.GlobalServer.Run()
 | |
| 
 | |
| 	GlobalStreamer = streamer.NewStreamer(config.VideoList)
 | |
| 
 | |
| 	go startWatcher()
 | |
| 	go inputHandler()
 | |
| 
 | |
| 	GlobalStreamer.Start()
 | |
| 	select {}
 | |
| }
 | |
| 
 | |
| func inputHandler() {
 | |
| 	scanner := bufio.NewScanner(os.Stdin)
 | |
| 	for scanner.Scan() {
 | |
| 		line := scanner.Text()
 | |
| 		switch line {
 | |
| 		case "list":
 | |
| 			fmt.Println(GlobalStreamer.GetVideoListPath())
 | |
| 		case "index":
 | |
| 			fmt.Println(GlobalStreamer.GetCurrentIndex())
 | |
| 		case "next":
 | |
| 			GlobalStreamer.Next()
 | |
| 		case "prev":
 | |
| 			GlobalStreamer.Prev()
 | |
| 		case "quit":
 | |
| 			GlobalStreamer.Close()
 | |
| 		case "current":
 | |
| 			fmt.Println(GlobalStreamer.GetCurrentVideoPath())
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func startWatcher() {
 | |
| 	watcher, err := fsnotify.NewWatcher()
 | |
| 	if err != nil {
 | |
| 		log.Fatal("failed to create watcher", zap.Error(err))
 | |
| 	}
 | |
| 	defer watcher.Close()
 | |
| 	for _, item := range config.InputItems {
 | |
| 		if item.ItemType == "dir" {
 | |
| 			err = watcher.Add(item.Path)
 | |
| 			if err != nil {
 | |
| 				log.Fatal("failed to add dir to watcher", zap.Error(err))
 | |
| 			}
 | |
| 			log.Info("watching dir", zap.String("path", item.Path))
 | |
| 		}
 | |
| 	}
 | |
| 	if err != nil {
 | |
| 		log.Fatal("failed to start watcher", zap.Error(err))
 | |
| 	}
 | |
| 
 | |
| 	for {
 | |
| 		select {
 | |
| 		case event, ok := <-watcher.Events:
 | |
| 			if !ok {
 | |
| 				return
 | |
| 			}
 | |
| 			if event.Op&fsnotify.Create == fsnotify.Create {
 | |
| 				if utils.IsSupportedVideo(event.Name) {
 | |
| 					log.Info("new video added", zap.String("path", event.Name))
 | |
| 					GlobalStreamer.Add(event.Name)
 | |
| 				}
 | |
| 			}
 | |
| 			if event.Op&fsnotify.Remove == fsnotify.Remove {
 | |
| 				log.Info("video removed", zap.String("path", event.Name))
 | |
| 				GlobalStreamer.Remove(event.Name)
 | |
| 			}
 | |
| 		case err, ok := <-watcher.Errors:
 | |
| 			if !ok {
 | |
| 				return
 | |
| 			}
 | |
| 			log.Error("watcher error", zap.Error(err))
 | |
| 		}
 | |
| 	}
 | |
| }
 |