project structure refactoring
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
package hid
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
MouseUp = iota
|
||||
MouseDown
|
||||
MouseMoveAbsolute
|
||||
MouseMoveRelative
|
||||
MouseScroll
|
||||
)
|
||||
|
||||
const (
|
||||
MouseLeft = 1
|
||||
MouseRight = 2
|
||||
MouseWheel = 3
|
||||
)
|
||||
|
||||
const (
|
||||
HidMouseLeft byte = 0x01
|
||||
HidMouseRight byte = 0x02
|
||||
HidMouseWheel byte = 0x04
|
||||
)
|
||||
|
||||
const (
|
||||
ModifierLCtrl byte = 0x01
|
||||
ModifierLShift byte = 0x02
|
||||
ModifierLAlt byte = 0x04
|
||||
ModifierLGUI byte = 0x08
|
||||
ModifierRAlt byte = 0x40
|
||||
)
|
||||
|
||||
var (
|
||||
hid *Hid
|
||||
hidOnce sync.Once
|
||||
)
|
||||
|
||||
type Hid struct {
|
||||
g0 *os.File
|
||||
g1 *os.File
|
||||
g2 *os.File
|
||||
kbMutex sync.Mutex
|
||||
mouseMutex sync.Mutex
|
||||
}
|
||||
|
||||
func (h *Hid) Lock() {
|
||||
h.kbMutex.Lock()
|
||||
h.mouseMutex.Lock()
|
||||
}
|
||||
|
||||
func (h *Hid) Unlock() {
|
||||
h.kbMutex.Unlock()
|
||||
h.mouseMutex.Unlock()
|
||||
}
|
||||
|
||||
func GetHid() *Hid {
|
||||
hidOnce.Do(func() {
|
||||
hid = &Hid{}
|
||||
})
|
||||
return hid
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package hid
|
||||
|
||||
func (h *Hid) Keyboard(queue <-chan []int) {
|
||||
for event := range queue {
|
||||
h.kbMutex.Lock()
|
||||
h.writeKeyboard(event)
|
||||
h.kbMutex.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hid) writeKeyboard(event []int) {
|
||||
var data []byte
|
||||
|
||||
if event[0] > 0 {
|
||||
code := byte(event[0])
|
||||
modifier := getModifier(event)
|
||||
data = []byte{modifier, 0x00, code, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||
} else {
|
||||
data = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||
}
|
||||
|
||||
h.Write(h.g0, data)
|
||||
}
|
||||
|
||||
func getModifier(event []int) byte {
|
||||
var modifier byte = 0x00
|
||||
|
||||
if event[1] == 1 {
|
||||
modifier |= ModifierLCtrl
|
||||
}
|
||||
|
||||
if event[2] == 1 {
|
||||
modifier |= ModifierLShift
|
||||
}
|
||||
|
||||
if event[3] == 1 {
|
||||
modifier |= ModifierLAlt
|
||||
} else if event[3] == 2 {
|
||||
modifier |= ModifierRAlt
|
||||
}
|
||||
|
||||
if event[4] == 1 {
|
||||
modifier |= ModifierLGUI
|
||||
}
|
||||
|
||||
return modifier
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
package hid
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (h *Hid) Mouse(queue <-chan []int) {
|
||||
for event := range queue {
|
||||
h.mouseMutex.Lock()
|
||||
switch event[0] {
|
||||
case MouseDown:
|
||||
h.mouseDown(event)
|
||||
case MouseUp:
|
||||
h.mouseUp()
|
||||
case MouseMoveAbsolute:
|
||||
h.mouseMoveAbsolute(event)
|
||||
case MouseMoveRelative:
|
||||
h.mouseMoveRelative(event)
|
||||
case MouseScroll:
|
||||
h.scroll(event)
|
||||
default:
|
||||
log.Debugf("invalid mouse event: %+v", event)
|
||||
}
|
||||
h.mouseMutex.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hid) mouseDown(event []int) {
|
||||
var button byte
|
||||
|
||||
switch event[1] {
|
||||
case MouseLeft:
|
||||
button = HidMouseLeft
|
||||
case MouseRight:
|
||||
button = HidMouseRight
|
||||
case MouseWheel:
|
||||
button = HidMouseWheel
|
||||
default:
|
||||
log.Debugf("invalid mouse button: %+v", event)
|
||||
return
|
||||
}
|
||||
|
||||
data := []byte{button, 0, 0, 0}
|
||||
h.writeWithTimeout(h.g1, data)
|
||||
}
|
||||
|
||||
func (h *Hid) mouseUp() {
|
||||
data := []byte{0, 0, 0, 0}
|
||||
h.writeWithTimeout(h.g1, data)
|
||||
}
|
||||
|
||||
func (h *Hid) scroll(event []int) {
|
||||
direction := 0x01
|
||||
if event[3] > 0 {
|
||||
direction = -0x1
|
||||
}
|
||||
|
||||
data := []byte{0, 0, 0, byte(direction)}
|
||||
h.writeWithTimeout(h.g1, data)
|
||||
}
|
||||
|
||||
func (h *Hid) mouseMoveAbsolute(event []int) {
|
||||
x := make([]byte, 2)
|
||||
y := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(x, uint16(event[2]))
|
||||
binary.LittleEndian.PutUint16(y, uint16(event[3]))
|
||||
|
||||
data := []byte{0, x[0], x[1], y[0], y[1], 0}
|
||||
h.writeWithTimeout(h.g2, data)
|
||||
}
|
||||
|
||||
func (h *Hid) mouseMoveRelative(event []int) {
|
||||
data := []byte{byte(event[1]), byte(event[2]), byte(event[3]), 0}
|
||||
h.writeWithTimeout(h.g1, data)
|
||||
}
|
||||
|
||||
func (h *Hid) writeWithTimeout(file *os.File, data []byte) {
|
||||
deadline := time.Now().Add(8 * time.Millisecond)
|
||||
_ = file.SetWriteDeadline(deadline)
|
||||
|
||||
_, err := file.Write(data)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, os.ErrClosed):
|
||||
log.Tracef("hid already closed, reopen it...")
|
||||
h.OpenNoLock()
|
||||
case errors.Is(err, os.ErrDeadlineExceeded):
|
||||
log.Tracef("write to hid timeout")
|
||||
default:
|
||||
log.Errorf("write to hid failed: %s", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.Tracef("write to hid: %+v", data)
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package hid
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (h *Hid) OpenNoLock() {
|
||||
log.Debug("OpenNoLock hid")
|
||||
var err error
|
||||
h.CloseNoLock()
|
||||
|
||||
h.g0, err = os.OpenFile("/dev/hidg0", os.O_WRONLY, 0o666)
|
||||
if err != nil {
|
||||
log.Errorf("open /dev/hidg0 failed: %s", err)
|
||||
}
|
||||
|
||||
h.g1, err = os.OpenFile("/dev/hidg1", os.O_WRONLY, 0o666)
|
||||
if err != nil {
|
||||
log.Errorf("open /dev/hidg1 failed: %s", err)
|
||||
}
|
||||
|
||||
h.g2, err = os.OpenFile("/dev/hidg2", os.O_WRONLY, 0o666)
|
||||
if err != nil {
|
||||
log.Errorf("open /dev/hidg2 failed: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hid) Open() {
|
||||
h.kbMutex.Lock()
|
||||
defer h.kbMutex.Unlock()
|
||||
h.mouseMutex.Lock()
|
||||
defer h.mouseMutex.Unlock()
|
||||
|
||||
h.CloseNoLock()
|
||||
|
||||
h.OpenNoLock()
|
||||
}
|
||||
|
||||
func (h *Hid) CloseNoLock() {
|
||||
log.Debug("CloseNoLock")
|
||||
for _, file := range []*os.File{h.g0, h.g1, h.g2} {
|
||||
if file != nil {
|
||||
_ = file.Sync()
|
||||
_ = file.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hid) Close() {
|
||||
h.kbMutex.Lock()
|
||||
defer h.kbMutex.Unlock()
|
||||
h.mouseMutex.Lock()
|
||||
defer h.mouseMutex.Unlock()
|
||||
|
||||
h.CloseNoLock()
|
||||
}
|
||||
|
||||
func (h *Hid) Write(file *os.File, data []byte) {
|
||||
_, err := file.Write(data)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrClosed) {
|
||||
log.Debugf("hid already closed, reopen it...")
|
||||
h.OpenNoLock()
|
||||
} else {
|
||||
log.Errorf("write to hid failed: %s", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.Tracef("write to hid: %+v", data)
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ExtProcess struct {
|
||||
path string
|
||||
cmd *exec.Cmd
|
||||
mu sync.Mutex
|
||||
args []string
|
||||
running bool
|
||||
stopChan chan struct{}
|
||||
finished chan struct{}
|
||||
state State
|
||||
|
||||
stdin io.WriteCloser
|
||||
}
|
||||
|
||||
func Init(path string, args []string) *ExtProcess {
|
||||
return &ExtProcess{
|
||||
args: args,
|
||||
path: path,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *ExtProcess) Start() {
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
|
||||
if u.running {
|
||||
log.Debug("process is already running.")
|
||||
return
|
||||
}
|
||||
|
||||
u.stopChan = make(chan struct{})
|
||||
u.finished = make(chan struct{})
|
||||
|
||||
log.Debugf("Starting external process: %s %v", u.path, u.args)
|
||||
u.cmd = exec.Command(u.path, u.args...)
|
||||
u.cmd.Stdout = os.Stdout
|
||||
u.cmd.Stderr = os.Stderr
|
||||
stdin, err := u.cmd.StdinPipe()
|
||||
if err != nil {
|
||||
log.Errorf("Couldn't handle stdin pipe: %v", err)
|
||||
return
|
||||
}
|
||||
u.stdin = stdin
|
||||
|
||||
err = u.cmd.Start()
|
||||
if err != nil {
|
||||
log.Errorf("Failed to start process: %v", err)
|
||||
}
|
||||
u.running = true
|
||||
|
||||
go func() {
|
||||
err = u.cmd.Wait()
|
||||
u.running = false
|
||||
log.Errorf("process exited with error: %v", err)
|
||||
|
||||
// planned stop
|
||||
if err == nil {
|
||||
u.finished <- struct{}{}
|
||||
close(u.stopChan)
|
||||
return
|
||||
}
|
||||
|
||||
u.stopChan <- struct{}{}
|
||||
close(u.finished)
|
||||
}()
|
||||
}
|
||||
|
||||
// Stop the ustreamer application
|
||||
func (u *ExtProcess) Stop() {
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
|
||||
if !u.running {
|
||||
log.Debug("process is not running.")
|
||||
return
|
||||
}
|
||||
|
||||
log.Warn("Stopping process...")
|
||||
_, err := u.stdin.Write([]byte("q")) // TODO: works for ffmpeg, should be general or move into FFmpeg struct
|
||||
if err != nil {
|
||||
log.Errorf("Failed to stop process: %v", err)
|
||||
|
||||
if err := u.cmd.Process.Kill(); err != nil {
|
||||
log.Errorf("Failed to kill process: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := u.stdin.Close(); err != nil {
|
||||
log.Errorf("Failed to close stdin: %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-u.finished:
|
||||
log.Info("stopped as expected")
|
||||
case <-u.stopChan:
|
||||
log.Info("was killed")
|
||||
}
|
||||
|
||||
u.running = false
|
||||
}
|
||||
|
||||
func (u *ExtProcess) ChangeArgs(newArgs []string) {
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
|
||||
u.args = newArgs
|
||||
log.Printf("Updated process arguments: %v", u.args)
|
||||
}
|
||||
|
||||
func (u *ExtProcess) Watch() {
|
||||
for {
|
||||
select {
|
||||
case <-u.stopChan:
|
||||
log.Errorf("process stopped unexpectedly. Restarting...")
|
||||
u.Start()
|
||||
/*case <-time.After(1 * time.Second): // Adjust the monitoring interval as needed
|
||||
if !u.running {
|
||||
log.Errorf("process is not running. Restarting...")
|
||||
u.Start()
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"rkkvm/config"
|
||||
)
|
||||
|
||||
//h264
|
||||
//ffmpeg -re -i /dev/video0 -c:v h264_rkmpp -b:v 2000000 -bsf:v h264_mp4toannexb -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200
|
||||
|
||||
//h265
|
||||
//ffmpeg -re -i /dev/video0 -c:v hevc_rkmpp -b:v 2000000 -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200
|
||||
|
||||
// vp8
|
||||
//ffmpeg -re -i /dev/video0 -c:v libvpx -crf 10 -b:v 3M -deadline 1 -g 10 -error-resilient 1 -auto-alt-ref 1 -an -f rtp rtp://127.0.0.1:5004?pkt_size=1200
|
||||
|
||||
/*
|
||||
arecord -D hw:0,0 -f cd -r 44100 -c 2 | /app/ffmpeg -re -init_hw_device rkmpp=hw -filter_hw_device hw \
|
||||
-f wav -thread_queue_size 4096 -i pipe:0 -f v4l2 -i /dev/video0 \
|
||||
-vf hwupload,scale_rkrga=h=720:force_original_aspect_ratio=1 \
|
||||
-c:v h264_rkmpp -flags +low_delay -b:v 6000000 -framerate 60 -g 10 \
|
||||
-map 0:a -c:a aac -b:a 128k -ar 44100 -ac 2 -f rtp rtp://127.0.0.1:5006?pkt_size=1200 \
|
||||
-map 1:v -f rtp rtp://127.0.0.1:5004?pkt_size=1200
|
||||
|
||||
|
||||
arecord -D hw:0,0 -f cd -r 44100 -c 2 | /app/ffmpeg -re -f wav -i pipe:0 -c:a aac -b:a 128k -ar 44100 -ac 2 -f rtp rtp://127.0.0.1:5006?pkt_size=1200
|
||||
|
||||
arecord -D hw:0,0 -f dat -r 48000 -c 2 --buffer-size=60 | /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 rtp rtp://127.0.0.1:5006?pkt_size=1200 \
|
||||
-i /dev/video0 -map 1:v -vf hwupload,scale_rkrga=h=720:force_original_aspect_ratio=1 -c:v h264_rkmpp -flags +low_delay -b:v 6000000 -framerate 60 -g 10 \
|
||||
-f rtp rtp://127.0.0.1:5004?pkt_size=1200
|
||||
*/
|
||||
|
||||
// https://jsfiddle.net/z7ms3u5r/
|
||||
|
||||
var ffmpeg *FFmpeg
|
||||
|
||||
type FFmpeg struct {
|
||||
*PipedCmd
|
||||
config.FFmpeg
|
||||
}
|
||||
|
||||
func (f *FFmpeg) getCmd() string {
|
||||
return f.Commands[config.StreamInputVideoAudio]
|
||||
}
|
||||
|
||||
func (f *FFmpeg) SetBitrate(b int) {
|
||||
f.Bitrate = b
|
||||
if f.Bitrate < 0 {
|
||||
f.Bitrate = 6000
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FFmpeg) SetFPS(fps int) {
|
||||
f.FPS = fps
|
||||
if f.FPS < 0 {
|
||||
f.FPS = 30
|
||||
} else if f.FPS > 60 {
|
||||
f.FPS = 60
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FFmpeg) SetResolution(height int) {
|
||||
f.Height = height
|
||||
if f.Height <= 0 {
|
||||
f.Height = 1080
|
||||
} else if f.Height > 2060 {
|
||||
f.Height = 2060
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FFmpeg) SetGOP(gop int) {
|
||||
f.GOP = gop
|
||||
if f.GOP < 0 {
|
||||
f.GOP = 2
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FFmpeg) ApplyOptions() {
|
||||
f.ChangeCmd(f.FormatCmd(f.getCmd()))
|
||||
}
|
||||
|
||||
func InitFFmpeg() *FFmpeg {
|
||||
cfg := config.Get().Video
|
||||
cmd := cfg.Commands[config.StreamInputVideoAudio]
|
||||
|
||||
ffmpeg = &FFmpeg{
|
||||
PipedCmd: InitPipedCmd(cfg.FormatCmd(cmd)),
|
||||
FFmpeg: cfg,
|
||||
}
|
||||
return ffmpeg
|
||||
}
|
||||
|
||||
func GetFFmpeg() *FFmpeg {
|
||||
return ffmpeg
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var pipedCmd *PipedCmd
|
||||
var mu sync.Mutex
|
||||
|
||||
// PipedCmd struct manages a sequence of piped commands.
|
||||
type PipedCmd struct {
|
||||
cmds []*exec.Cmd
|
||||
running bool
|
||||
}
|
||||
|
||||
// InitPipedCmd initializes a PipedCmd instance with a sequence of commands.
|
||||
func InitPipedCmd(cmd string) *PipedCmd {
|
||||
pipedCmd = &PipedCmd{}
|
||||
pipedCmd.ChangeCmd(cmd)
|
||||
return pipedCmd
|
||||
}
|
||||
|
||||
func (p *PipedCmd) ChangeCmd(cmd string) {
|
||||
cmds := strings.Split(cmd, "|")
|
||||
for i, c := range cmds {
|
||||
cmds[i] = strings.TrimSpace(c)
|
||||
}
|
||||
|
||||
log.Debugf("Cmds: %+v", cmds)
|
||||
|
||||
pipedCmds := make([]*exec.Cmd, len(cmds))
|
||||
|
||||
// Initialize each command in the sequence
|
||||
for i, cmd := range cmds {
|
||||
if len(cmd) == 0 {
|
||||
continue
|
||||
}
|
||||
cmdArgs := strings.Split(cmd, " ")
|
||||
pipedCmds[i] = exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
||||
}
|
||||
|
||||
p.cmds = pipedCmds
|
||||
}
|
||||
|
||||
// Start begins execution of all commands in the piped sequence.
|
||||
func (p *PipedCmd) Start() error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if p.running {
|
||||
log.Debugf("Process is already running.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pipe each command's output to the next command's input
|
||||
for i := 0; i < len(p.cmds)-1; i++ {
|
||||
stdout, err := p.cmds[i].StdoutPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.cmds[i+1].Stdin = stdout
|
||||
}
|
||||
// pipe stdout and stderr of the last command into os
|
||||
p.cmds[len(p.cmds)-1].Stdout = os.Stdout
|
||||
p.cmds[len(p.cmds)-1].Stderr = os.Stderr
|
||||
|
||||
// Start each command in the sequence
|
||||
for _, cmd := range p.cmds {
|
||||
if err := cmd.Start(); err != nil {
|
||||
p.terminateAll()
|
||||
return err
|
||||
} else {
|
||||
log.Debugf("Started process: (%d) %v", cmd.Process.Pid, cmd.Args)
|
||||
}
|
||||
}
|
||||
|
||||
p.running = true
|
||||
|
||||
// Monitor commands in a goroutine to handle failures
|
||||
go p.monitorCommands()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// monitorCommands waits for each command to finish and checks for errors.
|
||||
func (p *PipedCmd) monitorCommands() {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(p.cmds))
|
||||
|
||||
for _, cmd := range p.cmds {
|
||||
go func(cmd *exec.Cmd) {
|
||||
defer wg.Done()
|
||||
err := cmd.Wait()
|
||||
if err != nil && err.Error() != "signal: terminated" {
|
||||
log.Debugf("Command failed: %v", err)
|
||||
p.terminateAll() // Terminate all if any command fails
|
||||
}
|
||||
}(cmd)
|
||||
}
|
||||
|
||||
// Wait for all commands to complete or terminate
|
||||
wg.Wait()
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
p.running = false
|
||||
}
|
||||
|
||||
// terminateAll sends a termination signal to all running commands.
|
||||
func (p *PipedCmd) terminateAll() {
|
||||
for _, cmd := range p.cmds {
|
||||
if cmd.Process != nil {
|
||||
log.Debugf("Sending SIGTERM to: (%d) %v", cmd.Process.Pid, cmd.Args)
|
||||
_ = cmd.Process.Signal(syscall.SIGTERM) // Send SIGTERM to allow graceful termination
|
||||
}
|
||||
}
|
||||
|
||||
p.running = false
|
||||
}
|
||||
|
||||
// Stop manually stops all commands in the sequence.
|
||||
func (p *PipedCmd) Stop() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if !p.running {
|
||||
log.Debug("Process is not running.")
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("Stopping process...")
|
||||
p.terminateAll()
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"rkkvm/config"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var ustreamer *UStreamer
|
||||
|
||||
type UStreamer struct {
|
||||
*ExtProcess
|
||||
config.UStreamer
|
||||
}
|
||||
|
||||
func InitUStreamer(path string, args []string) *UStreamer {
|
||||
ustreamer = &UStreamer{
|
||||
ExtProcess: Init(path, args),
|
||||
UStreamer: config.Get().UStreamer,
|
||||
}
|
||||
return ustreamer
|
||||
}
|
||||
|
||||
func GetUStreamer() *UStreamer {
|
||||
return ustreamer
|
||||
}
|
||||
|
||||
func (u *UStreamer) MonitorState() {
|
||||
ticker := time.NewTicker(1 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
{
|
||||
state, err := GetState()
|
||||
if err != nil {
|
||||
u.state = State{}
|
||||
log.Errorf("Failed to get process state: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
u.state = state
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UStreamer) GetState() State {
|
||||
return u.state
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"rkkvm/config"
|
||||
"rkkvm/http/hw/rtc"
|
||||
"rkkvm/http/reqrsp"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pion/webrtc/v4"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
/*
|
||||
{
|
||||
"ok": true,
|
||||
"result": {
|
||||
"instance_id": "",
|
||||
"encoder": {
|
||||
"type": "CPU",
|
||||
"quality": 80
|
||||
},
|
||||
"source": {
|
||||
"resolution": {
|
||||
"width": 1920,
|
||||
"height": 1080
|
||||
},
|
||||
"online": true,
|
||||
"desired_fps": 30,
|
||||
"captured_fps": 50
|
||||
},
|
||||
"stream": {
|
||||
"queued_fps": 26,
|
||||
"clients": 1,
|
||||
"clients_stat": {
|
||||
"2d90d210e837a19c": {
|
||||
"fps": 26,
|
||||
"extra_headers": false,
|
||||
"advance_headers": false,
|
||||
"dual_final_frames": false,
|
||||
"zero_data": false,
|
||||
"key": "0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
type State struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
InstanceID string `json:"instance_id"`
|
||||
Encoder struct {
|
||||
Type string `json:"type"`
|
||||
Quality int `json:"quality"`
|
||||
} `json:"encoder"`
|
||||
Source struct {
|
||||
Resolution struct {
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
} `json:"resolution"`
|
||||
Online bool `json:"online"`
|
||||
DesiredFPS int `json:"desired_fps"`
|
||||
CapturedFPS int `json:"captured_fps"`
|
||||
} `json:"source"`
|
||||
Stream struct {
|
||||
QueuedFPS int `json:"queued_fps"`
|
||||
Clients int `json:"clients"`
|
||||
ClientsStat map[string]struct {
|
||||
FPS int `json:"fps"`
|
||||
ExtraHeaders bool `json:"extra_headers"`
|
||||
AdvanceHeaders bool `json:"advance_headers"`
|
||||
DualFinalFrames bool `json:"dual_final_frames"`
|
||||
ZeroData bool `json:"zero_data"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
} `json:"stream"`
|
||||
} `json:"result"`
|
||||
}
|
||||
|
||||
func FormatHttpUrl() string {
|
||||
cfg := config.Get()
|
||||
return fmt.Sprintf("http://%s:%d", cfg.UStreamer.Host, cfg.UStreamer.Port)
|
||||
}
|
||||
|
||||
func GetState() (State, error) {
|
||||
url := FormatHttpUrl() + "/state"
|
||||
|
||||
rsp, err := http.Get(url)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to get state: %v", err)
|
||||
return State{}, err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
|
||||
if rsp.StatusCode != http.StatusOK {
|
||||
log.Errorf("Invalid status code: %d", rsp.StatusCode)
|
||||
return State{}, err
|
||||
}
|
||||
|
||||
var state State
|
||||
if err := json.NewDecoder(rsp.Body).Decode(&state); err != nil {
|
||||
log.Errorf("Failed to decode state: %v", err)
|
||||
return State{}, err
|
||||
}
|
||||
|
||||
return state, nil
|
||||
}
|
||||
|
||||
func MjpegHandler(c *gin.Context) {
|
||||
url := FormatHttpUrl() + "/stream"
|
||||
|
||||
rsp, err := http.Get(url)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Failed to get stream")
|
||||
log.Errorf("Failed to get stream: %v", err)
|
||||
return
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
|
||||
if rsp.StatusCode != http.StatusOK {
|
||||
c.String(rsp.StatusCode, "Invalid status code")
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("Content-Type", rsp.Header.Get("Content-Type"))
|
||||
buf := make([]byte, 1024*1024)
|
||||
_, err = io.CopyBuffer(c.Writer, rsp.Body, buf)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Failed to passthrough stream")
|
||||
log.Errorf("Failed to passthrough stream: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func WebRTCPeerConnect(c *gin.Context) {
|
||||
var offer webrtc.SessionDescription
|
||||
if err := c.ShouldBindJSON(&offer); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid offer"})
|
||||
return
|
||||
}
|
||||
|
||||
peer, err := rtc.NewPeer()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create peer connection"})
|
||||
return
|
||||
}
|
||||
|
||||
answer, err := rtc.Get().AddPeer(peer, offer)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to add peer"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, reqrsp.NanoKVMRsp{
|
||||
Msg: reqrsp.MsgSuccess,
|
||||
Data: answer,
|
||||
})
|
||||
}
|
||||
|
||||
func WebRTCSettings(c *gin.Context) {
|
||||
bitrateStr := c.Query("bitrate")
|
||||
fpsStr := c.Query("fps")
|
||||
|
||||
if fpsStr == "" && bitrateStr == "" {
|
||||
c.String(http.StatusBadGateway, "please specify bitrate or fps parameters")
|
||||
return
|
||||
}
|
||||
|
||||
ffmpeg := GetFFmpeg()
|
||||
|
||||
if len(bitrateStr) > 0 {
|
||||
bitrate, err := strconv.Atoi(bitrateStr)
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, "wrong bitrate value")
|
||||
return
|
||||
}
|
||||
ffmpeg.SetBitrate(bitrate)
|
||||
}
|
||||
|
||||
if len(fpsStr) > 0 {
|
||||
fps, err := strconv.Atoi(fpsStr)
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, "wrong fps value")
|
||||
return
|
||||
}
|
||||
ffmpeg.SetFPS(fps)
|
||||
}
|
||||
|
||||
ffmpeg.Stop()
|
||||
ffmpeg.Start()
|
||||
}
|
||||
@@ -6,11 +6,11 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"rkkvm/config"
|
||||
"rkkvm/http/hw/hid"
|
||||
"rkkvm/http/hw/stream"
|
||||
"rkkvm/hid"
|
||||
"rkkvm/http/middleware"
|
||||
"rkkvm/http/reqrsp"
|
||||
"rkkvm/http/ws"
|
||||
"rkkvm/stream"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -2,7 +2,7 @@ package route
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"rkkvm/http/hw/stream"
|
||||
"rkkvm/external/ffmpeg"
|
||||
"rkkvm/http/middleware"
|
||||
"rkkvm/http/reqrsp"
|
||||
"time"
|
||||
@@ -56,7 +56,7 @@ func SetScreen(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ffmpeg := stream.GetFFmpeg()
|
||||
ffmpeg := ffmpeg.GetFFmpeg()
|
||||
switch req.Type {
|
||||
case "fps":
|
||||
ffmpeg.SetFPS(req.Value)
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"rkkvm/config"
|
||||
"rkkvm/http/hw/hid"
|
||||
"rkkvm/hid"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
Reference in New Issue
Block a user