feat: add dynamic config reload, unique device identity (UUID), and MQTT support
- New StreamManager for dynamic RTSP/FLV stream lifecycle - Dynamic worker scaling for inference - Device UUID generation and persistence - Telegraf config and NPU monitoring scripts - .gitignore for build artifacts
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -10,6 +9,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"bytes"
|
||||
|
||||
"tianyan-edge/internal/config"
|
||||
)
|
||||
|
||||
@@ -21,63 +22,109 @@ type Frame struct {
|
||||
TS float64
|
||||
}
|
||||
|
||||
type Ingestor struct {
|
||||
cfg *config.Config
|
||||
frames chan<- Frame
|
||||
type StreamManager struct {
|
||||
cfg *config.Config
|
||||
frames chan<- Frame
|
||||
processes map[string]*exec.Cmd
|
||||
mu sync.Mutex
|
||||
updates <-chan *config.Config
|
||||
}
|
||||
|
||||
func NewIngestor(cfg *config.Config, frames chan<- Frame) *Ingestor {
|
||||
return &Ingestor{cfg: cfg, frames: frames}
|
||||
}
|
||||
|
||||
func (ing *Ingestor) Run(ctx context.Context) {
|
||||
if len(ing.cfg.RTSPURLs) == 0 {
|
||||
log.Println("stream: no RTSP URLs configured, idle")
|
||||
<-ctx.Done()
|
||||
return
|
||||
func NewStreamManager(cfg *config.Config, frames chan<- Frame, updates <-chan *config.Config) *StreamManager {
|
||||
return &StreamManager{
|
||||
cfg: cfg,
|
||||
frames: frames,
|
||||
processes: make(map[string]*exec.Cmd),
|
||||
updates: updates,
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for i, url := range ing.cfg.RTSPURLs {
|
||||
wg.Add(1)
|
||||
go func(idx int, u string) {
|
||||
defer wg.Done()
|
||||
ing.streamLoop(ctx, idx, u)
|
||||
}(i, url)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (ing *Ingestor) streamLoop(ctx context.Context, idx int, url string) {
|
||||
func (sm *StreamManager) Run(ctx context.Context) {
|
||||
// Initial start
|
||||
sm.applyStreams(ctx, sm.cfg.RTSPURLs)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
sm.stopAll()
|
||||
return
|
||||
case newCfg := <-sm.updates:
|
||||
log.Printf("stream: applying new config, urls=%d", len(newCfg.RTSPURLs))
|
||||
sm.applyStreams(ctx, newCfg.RTSPURLs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sm *StreamManager) applyStreams(ctx context.Context, urls []string) {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
|
||||
// Identify URLs to remove (present in sm.processes but not in new urls)
|
||||
toRemove := map[string]*exec.Cmd{}
|
||||
for url, cmd := range sm.processes {
|
||||
found := false
|
||||
for _, u := range urls {
|
||||
if u == url {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
toRemove[url] = cmd
|
||||
}
|
||||
}
|
||||
|
||||
// Stop removed streams
|
||||
for url, cmd := range toRemove {
|
||||
log.Printf("stream: stopping %s", url)
|
||||
cmd.Process.Kill()
|
||||
cmd.Wait()
|
||||
delete(sm.processes, url)
|
||||
}
|
||||
|
||||
// Start new streams
|
||||
for i, url := range urls {
|
||||
if _, exists := sm.processes[url]; !exists {
|
||||
go sm.streamLoop(ctx, i, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string) {
|
||||
deviceID := fmt.Sprintf("cam-%03d", idx)
|
||||
fps := ing.cfg.InferFPS
|
||||
if fps <= 0 {
|
||||
fps = 5
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, "ffmpeg",
|
||||
"-rtsp_transport", "tcp", "-i", url,
|
||||
"-vf", fmt.Sprintf("fps=%d", fps),
|
||||
"-i", url,
|
||||
"-vf", fmt.Sprintf("fps=%d", sm.cfg.InferFPS),
|
||||
"-f", "image2pipe", "-vcodec", "mjpeg", "-q:v", "5", "-",
|
||||
)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Printf("stream[%d] stdout pipe failed: %v, retry 10s", idx, err)
|
||||
time.Sleep(10 * time.Second)
|
||||
log.Printf("stream[%d] stdout pipe failed: %v", idx, err)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
sm.mu.Lock()
|
||||
sm.processes[url] = cmd
|
||||
sm.mu.Unlock()
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
log.Printf("stream[%d] ffmpeg start failed: %v, retry 10s", idx, err)
|
||||
time.Sleep(10 * time.Second)
|
||||
log.Printf("stream[%d] start failed: %v", idx, err)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("stream[%d] connected url=%s", idx, url)
|
||||
ing.readFrames(ctx, stdout, idx, deviceID, url)
|
||||
sm.readFrames(ctx, stdout, idx, deviceID, url)
|
||||
cmd.Wait()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
@@ -88,7 +135,7 @@ func (ing *Ingestor) streamLoop(ctx context.Context, idx int, url string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (ing *Ingestor) readFrames(ctx context.Context, r io.Reader, idx int, deviceID, url string) {
|
||||
func (sm *StreamManager) readFrames(ctx context.Context, r io.Reader, idx int, deviceID, url string) {
|
||||
soi, eoi := []byte{0xFF, 0xD8}, []byte{0xFF, 0xD9}
|
||||
buf, tmp := make([]byte, 0, 1<<20), make([]byte, 32768)
|
||||
for {
|
||||
@@ -116,7 +163,7 @@ func (ing *Ingestor) readFrames(ctx context.Context, r io.Reader, idx int, devic
|
||||
copy(frame, buf[:end])
|
||||
buf = buf[end:]
|
||||
select {
|
||||
case ing.frames <- Frame{StreamID: idx, DeviceID: deviceID, URL: url,
|
||||
case sm.frames <- Frame{StreamID: idx, DeviceID: deviceID, URL: url,
|
||||
JPEG: frame, TS: float64(time.Now().UnixMilli()) / 1000.0}:
|
||||
default:
|
||||
}
|
||||
@@ -127,3 +174,13 @@ func (ing *Ingestor) readFrames(ctx context.Context, r io.Reader, idx int, devic
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sm *StreamManager) stopAll() {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
for url, cmd := range sm.processes {
|
||||
log.Printf("stream: stopping all %s", url)
|
||||
cmd.Process.Kill()
|
||||
delete(sm.processes, url)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user