project structure refactoring

This commit is contained in:
Artem
2024-11-05 23:47:00 +01:00
parent 1f5a56cc5d
commit c228921237
16 changed files with 51 additions and 47 deletions

View File

@@ -3,9 +3,10 @@ package main
import ( import (
"fmt" "fmt"
"rkkvm/config" "rkkvm/config"
"rkkvm/http/hw/rtc" "rkkvm/external/ffmpeg"
"rkkvm/http/hw/stream" "rkkvm/external/process"
"rkkvm/http/route" "rkkvm/http/route"
"rkkvm/http/rtc"
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -30,18 +31,7 @@ func main() {
log.Println("Failed to parse log level, use default level: info") log.Println("Failed to parse log level, use default level: info")
} }
if cfg.Stream.Source == config.StreamSourceMjpeg { webrtc, err := rtc.InitListener(cfg.WebRtc.Host, cfg.WebRtc.VideoPort, 5006)
ustreamer := stream.Init(cfg.UStreamer.Path, strings.Split(cfg.UStreamer.Args, " "))
ustreamer.Start()
//go ustreamer.Watch()
} else if cfg.Stream.Source == config.StreamSourceH264 {
v := stream.InitFFmpeg()
v.Start()
} else {
log.Fatalf("unsupported stream source type: %v", cfg.Stream.Source)
}
webrtc, err := rtc.InitListener(cfg.WebRtc.Host, cfg.WebRtc.Port, 5006)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -49,6 +39,17 @@ func main() {
go webrtc.VideoListenerRead() go webrtc.VideoListenerRead()
go webrtc.AudioListenerRead() go webrtc.AudioListenerRead()
if cfg.Stream.Source == config.StreamSourceMjpeg {
ustreamer := process.Init(cfg.UStreamer.Path, strings.Split(cfg.UStreamer.Args, " "))
ustreamer.Start()
//go ustreamer.Watch()
} else if cfg.Stream.Source == config.StreamSourceH264 {
v := ffmpeg.InitFFmpeg()
v.Start()
} else {
log.Fatalf("unsupported stream source type: %v", cfg.Stream.Source)
}
r := gin.Default() r := gin.Default()
r.Use(gin.Recovery()) r.Use(gin.Recovery())
route.Api(r) route.Api(r)

View File

@@ -14,7 +14,6 @@ type Config struct {
Port int `yaml:"port"` Port int `yaml:"port"`
UStreamer UStreamer `yaml:"ustreamer"` UStreamer UStreamer `yaml:"ustreamer"`
Video FFmpeg `yaml:"video"` Video FFmpeg `yaml:"video"`
Audio []string `yaml:"audio"`
WebRtc WebRtc `yaml:"webrtc"` WebRtc WebRtc `yaml:"webrtc"`
Stream Stream `yaml:"stream"` Stream Stream `yaml:"stream"`
@@ -24,7 +23,8 @@ type Config struct {
type WebRtc struct { type WebRtc struct {
Host string `yaml:"host"` Host string `yaml:"host"`
Port int `yaml:"port"` VideoPort int `yaml:"video_port"`
AudioPort int `yaml:"audio_port"`
} }
type ExtProcess struct { type ExtProcess struct {
@@ -34,6 +34,7 @@ type ExtProcess struct {
type Stream struct { type Stream struct {
Source string `yaml:"source"` // mjpeg or h264 Source string `yaml:"source"` // mjpeg or h264
} }
const ( const (
@@ -89,12 +90,12 @@ func Init() {
}, },
Video: FFmpeg{ Video: FFmpeg{
Commands: map[StreamInput]string{ Commands: map[StreamInput]string{
StreamInputVideoAudio: "/usr/bin/arecord -D hw:0,0 -f dat -r 48000 -c 2 --buffer-size=150 | /app/ffmpeg -init_hw_device rkmpp=hw -filter_hw_device hw" + StreamInputVideoAudio: "/usr/bin/arecord -D hw:0,0 -f dat -r 48000 -c 2 --buffer-size=150 | /app/ffmpeg -re -init_hw_device rkmpp=hw -filter_hw_device hw" +
" -f wav -i pipe:0 -map 0:a -c:a libopus -b:a 48000 -sample_fmt s16 -ssrc 1 -payload_type 111 -f rtp -max_delay 0 -application lowdelay" + " -f wav -i pipe:0 -map 0:a -c:a libopus -b:a 48000 -sample_fmt s16 -ssrc 1 -payload_type 111 -f rtp -max_delay 0 -application lowdelay" +
" -f rtp rtp://127.0.0.1:5006?pkt_size=1200" + " -f rtp rtp://127.0.0.1:5006?pkt_size=1200" +
" -i /dev/video0 -map 1:v -vf hwupload,scale_rkrga=h=%d:force_original_aspect_ratio=1 -c:v %s_rkmpp -flags +low_delay -b:v %d -framerate %d -g %d" + " -i /dev/video0 -map 1:v -vf hwupload,scale_rkrga=h=%d:force_original_aspect_ratio=1 -c:v %s_rkmpp -flags +low_delay -b:v %d -framerate %d -g %d" +
" -f rtp rtp://127.0.0.1:5004?pkt_size=1200", " -f rtp rtp://127.0.0.1:5004?pkt_size=1200",
StreamInputVideo: "/app/ffmpeg -hide_banner -loglevel error -init_hw_device rkmpp=hw -filter_hw_device hw" + StreamInputVideo: "/app/ffmpeg -re -hide_banner -loglevel error -init_hw_device rkmpp=hw -filter_hw_device hw" +
" -i /dev/video0 -vf hwupload,scale_rkrga=h=%d:force_original_aspect_ratio=1 -c:v %s_rkmpp -flags +low_delay -b:v %d -framerate %d -g %d" + " -i /dev/video0 -vf hwupload,scale_rkrga=h=%d:force_original_aspect_ratio=1 -c:v %s_rkmpp -flags +low_delay -b:v %d -framerate %d -g %d" +
" -f rtp rtp://127.0.0.1:5004?pkt_size=1200", " -f rtp rtp://127.0.0.1:5004?pkt_size=1200",
}, },
@@ -102,11 +103,12 @@ func Init() {
Bitrate: 6000, Bitrate: 6000,
Height: 720, Height: 720,
GOP: 5, GOP: 5,
Codec: "h264", Codec: StreamSourceH264,
}, },
WebRtc: WebRtc{ WebRtc: WebRtc{
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 5004, VideoPort: 5004,
AudioPort: 5006,
}, },
Stream: Stream{ Stream: Stream{
Source: StreamSourceH264, Source: StreamSourceH264,

View File

@@ -1,7 +1,8 @@
package stream package ffmpeg
import ( import (
"rkkvm/config" "rkkvm/config"
"rkkvm/external/process"
) )
//h264 //h264
@@ -36,7 +37,7 @@ arecord -D hw:0,0 -f dat -r 48000 -c 2 --buffer-size=60 | /app/ffmpeg -re -init_
var ffmpeg *FFmpeg var ffmpeg *FFmpeg
type FFmpeg struct { type FFmpeg struct {
*PipedCmd *process.PipedCmd
config.FFmpeg config.FFmpeg
} }
@@ -85,7 +86,7 @@ func InitFFmpeg() *FFmpeg {
cmd := cfg.Commands[config.StreamInputVideoAudio] cmd := cfg.Commands[config.StreamInputVideoAudio]
ffmpeg = &FFmpeg{ ffmpeg = &FFmpeg{
PipedCmd: InitPipedCmd(cfg.FormatCmd(cmd)), PipedCmd: process.InitPipedCmd(cfg.FormatCmd(cmd)),
FFmpeg: cfg, FFmpeg: cfg,
} }
return ffmpeg return ffmpeg

View File

@@ -1,4 +1,4 @@
package stream package process
import ( import (
"io" "io"
@@ -17,7 +17,6 @@ type ExtProcess struct {
running bool running bool
stopChan chan struct{} stopChan chan struct{}
finished chan struct{} finished chan struct{}
state State
stdin io.WriteCloser stdin io.WriteCloser
} }

View File

@@ -1,4 +1,4 @@
package stream package process
import ( import (
"os" "os"
@@ -10,18 +10,16 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var pipedCmd *PipedCmd
var mu sync.Mutex
// PipedCmd struct manages a sequence of piped commands. // PipedCmd struct manages a sequence of piped commands.
type PipedCmd struct { type PipedCmd struct {
cmds []*exec.Cmd cmds []*exec.Cmd
mu sync.Mutex
running bool running bool
} }
// InitPipedCmd initializes a PipedCmd instance with a sequence of commands. // InitPipedCmd initializes a PipedCmd instance with a sequence of commands.
func InitPipedCmd(cmd string) *PipedCmd { func InitPipedCmd(cmd string) *PipedCmd {
pipedCmd = &PipedCmd{} pipedCmd := &PipedCmd{}
pipedCmd.ChangeCmd(cmd) pipedCmd.ChangeCmd(cmd)
return pipedCmd return pipedCmd
} }
@@ -50,8 +48,8 @@ func (p *PipedCmd) ChangeCmd(cmd string) {
// Start begins execution of all commands in the piped sequence. // Start begins execution of all commands in the piped sequence.
func (p *PipedCmd) Start() error { func (p *PipedCmd) Start() error {
mu.Lock() p.mu.Lock()
defer mu.Unlock() defer p.mu.Unlock()
if p.running { if p.running {
log.Debugf("Process is already running.") log.Debugf("Process is already running.")
@@ -106,8 +104,8 @@ func (p *PipedCmd) monitorCommands() {
// Wait for all commands to complete or terminate // Wait for all commands to complete or terminate
wg.Wait() wg.Wait()
mu.Lock() p.mu.Lock()
defer mu.Unlock() defer p.mu.Unlock()
p.running = false p.running = false
} }
@@ -125,8 +123,8 @@ func (p *PipedCmd) terminateAll() {
// Stop manually stops all commands in the sequence. // Stop manually stops all commands in the sequence.
func (p *PipedCmd) Stop() { func (p *PipedCmd) Stop() {
mu.Lock() p.mu.Lock()
defer mu.Unlock() defer p.mu.Unlock()
if !p.running { if !p.running {
log.Debug("Process is not running.") log.Debug("Process is not running.")

View File

@@ -6,11 +6,11 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"rkkvm/config" "rkkvm/config"
"rkkvm/http/hw/hid" "rkkvm/hid"
"rkkvm/http/hw/stream"
"rkkvm/http/middleware" "rkkvm/http/middleware"
"rkkvm/http/reqrsp" "rkkvm/http/reqrsp"
"rkkvm/http/ws" "rkkvm/http/ws"
"rkkvm/stream"
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"

View File

@@ -2,7 +2,7 @@ package route
import ( import (
"net/http" "net/http"
"rkkvm/http/hw/stream" "rkkvm/external/ffmpeg"
"rkkvm/http/middleware" "rkkvm/http/middleware"
"rkkvm/http/reqrsp" "rkkvm/http/reqrsp"
"time" "time"
@@ -56,7 +56,7 @@ func SetScreen(c *gin.Context) {
return return
} }
ffmpeg := stream.GetFFmpeg() ffmpeg := ffmpeg.GetFFmpeg()
switch req.Type { switch req.Type {
case "fps": case "fps":
ffmpeg.SetFPS(req.Value) ffmpeg.SetFPS(req.Value)

View File

@@ -5,7 +5,7 @@ import (
"net" "net"
"net/http" "net/http"
"rkkvm/config" "rkkvm/config"
"rkkvm/http/hw/hid" "rkkvm/hid"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"

View File

@@ -2,6 +2,7 @@ package stream
import ( import (
"rkkvm/config" "rkkvm/config"
"rkkvm/external/process"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -10,13 +11,14 @@ import (
var ustreamer *UStreamer var ustreamer *UStreamer
type UStreamer struct { type UStreamer struct {
*ExtProcess *process.ExtProcess
config.UStreamer config.UStreamer
state State
} }
func InitUStreamer(path string, args []string) *UStreamer { func InitUStreamer(path string, args []string) *UStreamer {
ustreamer = &UStreamer{ ustreamer = &UStreamer{
ExtProcess: Init(path, args), ExtProcess: process.Init(path, args),
UStreamer: config.Get().UStreamer, UStreamer: config.Get().UStreamer,
} }
return ustreamer return ustreamer

View File

@@ -6,8 +6,9 @@ import (
"io" "io"
"net/http" "net/http"
"rkkvm/config" "rkkvm/config"
"rkkvm/http/hw/rtc" "rkkvm/external/ffmpeg"
"rkkvm/http/reqrsp" "rkkvm/http/reqrsp"
"rkkvm/http/rtc"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -172,7 +173,7 @@ func WebRTCSettings(c *gin.Context) {
return return
} }
ffmpeg := GetFFmpeg() ffmpeg := ffmpeg.GetFFmpeg()
if len(bitrateStr) > 0 { if len(bitrateStr) > 0 {
bitrate, err := strconv.Atoi(bitrateStr) bitrate, err := strconv.Atoi(bitrateStr)