fix: 修复6个边缘侧问题 + 单元测试

1. 删除 ConfigAgent (与 MQTT 配置更新重叠且未启用)
2. OTA 完整安装流程: 解压tar.gz -> 校验ELF -> 替换二进制 -> systemctl重启
3. StreamManager stopAll 添加 Wait() 防止僵尸进程
4. InferClient socket 读写添加 timeout 防止永久阻塞
5. Heartbeat 集成 NPU 监控 (npu-smi + fallback 脚本)
6. 配置更新时动态重启 ffmpeg 以应用新 fps
This commit is contained in:
2026-05-08 23:58:08 +08:00
parent 88b2e71a77
commit 97a77d4745
11 changed files with 843 additions and 186 deletions

View File

@@ -1,59 +0,0 @@
package control
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"tianyan-edge/internal/config"
)
type ConfigAgent struct {
cfg *config.Config
}
func NewConfigAgent(cfg *config.Config) *ConfigAgent {
return &ConfigAgent{cfg: cfg}
}
func (a *ConfigAgent) Run(ctx context.Context) {
client := &http.Client{Timeout: 5 * time.Second}
ticker := time.NewTicker(60 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
a.pull(client)
}
}
}
func (a *ConfigAgent) pull(client *http.Client) {
url := fmt.Sprintf("%s/api/v1/edge/config?edge_id=%s", a.cfg.CloudURL, a.cfg.EdgeID)
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Authorization", "Bearer "+a.cfg.EdgeToken)
resp, err := client.Do(req)
if err != nil {
log.Printf("config pull: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return
}
var data map[string]any
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return
}
if v, ok := data["infer_fps"].(float64); ok && int(v) > 0 {
a.cfg.InferFPS = int(v)
}
if v, ok := data["conf_threshold"].(float64); ok {
a.cfg.ConfThreshold = v
}
}

View File

@@ -7,6 +7,9 @@ import (
"fmt"
"log"
"net/http"
"os/exec"
"strconv"
"strings"
"time"
"tianyan-edge/internal/config"
@@ -24,6 +27,10 @@ func (h *Heartbeat) Run(ctx context.Context) {
client := &http.Client{Timeout: 5 * time.Second}
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
// Send first heartbeat immediately
h.send(client)
for {
select {
case <-ctx.Done():
@@ -40,6 +47,12 @@ func (h *Heartbeat) send(client *http.Client) {
"version": h.cfg.Version,
"ts": float64(time.Now().UnixMilli()) / 1000.0,
}
// Collect NPU stats if available
if npu := collectNPUStats(); npu != nil {
payload["npu"] = npu
}
body, _ := json.Marshal(payload)
url := fmt.Sprintf("%s/api/v1/edge/heartbeat", h.cfg.CloudURL)
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
@@ -52,3 +65,113 @@ func (h *Heartbeat) send(client *http.Client) {
}
resp.Body.Close()
}
// NPUStats holds parsed NPU telemetry
type NPUStats struct {
TempC int `json:"temp_c"`
UtilPct int `json:"util_pct"`
MemUsedMB int `json:"mem_used_mb"`
MemTotalMB int `json:"mem_total_mb"`
MemPct int `json:"mem_pct"`
}
// collectNPUStats runs npu-smi and parses the output
func collectNPUStats() *NPUStats {
// Try npu-smi first (Ascend devices), fall back to the helper script
var commonOut, memOut []byte
var err error
commonOut, err = exec.Command("/usr/local/sbin/npu-smi", "info", "-t", "common", "-i", "0").CombinedOutput()
if err != nil {
// Fallback to shell script
commonOut, err = exec.Command("bash", "/opt/tianyan-edge/scripts/npu_info.sh").CombinedOutput()
if err != nil {
log.Printf("npu: monitoring unavailable: %v", err)
return nil
}
// If script succeeded, it outputs Influx line protocol — parse that
line := strings.TrimSpace(string(commonOut))
return parseInfluxLine(line)
}
memOut, _ = exec.Command("/usr/local/sbin/npu-smi", "info", "-t", "memory", "-i", "0").CombinedOutput()
return parseNpuSmiOutput(string(commonOut), string(memOut))
}
func parseNpuSmiOutput(common, memory string) *NPUStats {
stats := &NPUStats{}
for _, line := range strings.Split(common, "\n") {
line = strings.TrimSpace(line)
if strings.Contains(line, "Temperature") {
if v := extractLastNumber(line); v != 0 {
stats.TempC = v
}
}
if strings.Contains(line, "Aicore Usage Rate") {
if v := extractLastNumber(line); v != 0 {
stats.UtilPct = v
}
}
if strings.Contains(line, "Memory Usage Rate") {
if v := extractLastNumber(line); v != 0 {
stats.MemPct = v
}
}
}
for _, line := range strings.Split(memory, "\n") {
line = strings.TrimSpace(line)
if strings.Contains(line, "Capacity") {
if v := extractLastNumber(line); v != 0 {
stats.MemTotalMB = v
}
}
}
if stats.MemTotalMB > 0 && stats.MemPct > 0 {
stats.MemUsedMB = stats.MemTotalMB * stats.MemPct / 100
}
return stats
}
func parseInfluxLine(line string) *NPUStats {
// Format: npu_status,device=0 temp=X,utilization=Y,memory_used=Z,memory_total=W
stats := &NPUStats{}
parts := strings.Split(line, " ")
if len(parts) < 2 {
return nil
}
for _, field := range strings.Split(parts[1], ",") {
kv := strings.SplitN(field, "=", 2)
if len(kv) != 2 {
continue
}
v, _ := strconv.Atoi(kv[1])
switch kv[0] {
case "temp":
stats.TempC = v
case "utilization":
stats.UtilPct = v
case "memory_used":
stats.MemUsedMB = v
case "memory_total":
stats.MemTotalMB = v
}
}
return stats
}
func extractLastNumber(line string) int {
fields := strings.Fields(line)
if len(fields) == 0 {
return 0
}
// Last field might have units like "%" or "C"
raw := fields[len(fields)-1]
raw = strings.TrimRight(raw, "%C ")
v, _ := strconv.Atoi(raw)
return v
}

View File

@@ -0,0 +1,96 @@
package control
import (
"testing"
)
func TestParseNpuSmiOutput(t *testing.T) {
common := `Temperature(C) : 46
Aicore Usage Rate(%) : 25
Memory Usage Rate(%) : 86`
memory := `Capacity(MB) : 3513`
stats := parseNpuSmiOutput(common, memory)
if stats == nil {
t.Fatal("expected non-nil stats")
}
if stats.TempC != 46 {
t.Errorf("temp: got %d want 46", stats.TempC)
}
if stats.UtilPct != 25 {
t.Errorf("util: got %d want 25", stats.UtilPct)
}
if stats.MemTotalMB != 3513 {
t.Errorf("mem_total: got %d want 3513", stats.MemTotalMB)
}
if stats.MemPct != 86 {
t.Errorf("mem_pct: got %d want 86", stats.MemPct)
}
// 3513 * 86 / 100 = 3021
if stats.MemUsedMB != 3021 {
t.Errorf("mem_used: got %d want 3021", stats.MemUsedMB)
}
}
func TestParseInfluxLine(t *testing.T) {
line := "npu_status,device=0 temp=46,utilization=0,memory_used=3021,memory_total=3513"
stats := parseInfluxLine(line)
if stats == nil {
t.Fatal("expected non-nil stats")
}
if stats.TempC != 46 {
t.Errorf("temp: got %d want 46", stats.TempC)
}
if stats.UtilPct != 0 {
t.Errorf("util: got %d want 0", stats.UtilPct)
}
if stats.MemUsedMB != 3021 {
t.Errorf("mem_used: got %d want 3021", stats.MemUsedMB)
}
if stats.MemTotalMB != 3513 {
t.Errorf("mem_total: got %d want 3513", stats.MemTotalMB)
}
}
func TestParseInfluxLineInvalid(t *testing.T) {
// Input with no space-separated fields returns nil
stats := parseInfluxLine("garbage")
if stats != nil {
t.Error("expected nil stats for garbage input without fields")
}
// Input with fields but no valid data returns zero-valued stats
stats2 := parseInfluxLine("npu_status,device=0 ")
if stats2 == nil {
t.Fatal("expected non-nil stats for input with fields")
}
if stats2.TempC != 0 || stats2.UtilPct != 0 {
t.Errorf("expected zero values, got temp=%d util=%d", stats2.TempC, stats2.UtilPct)
}
}
func TestExtractLastNumber(t *testing.T) {
tests := []struct {
input string
want int
}{
{"Temperature(C) : 46", 46},
{"Aicore Usage Rate(%) : 25%", 25},
{" 86%", 86},
{"no numbers here", 0},
{"", 0},
{"value: 123C", 123},
}
for _, tt := range tests {
got := extractLastNumber(tt.input)
if got != tt.want {
t.Errorf("extractLastNumber(%q) = %d, want %d", tt.input, got, tt.want)
}
}
}
func TestInMaintenanceWindow(t *testing.T) {
// Can't test specific hours without mocking time.Time,
// but verify it doesn't panic
_ = inMaintenanceWindow()
}

View File

@@ -1,7 +1,9 @@
package control
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
"encoding/hex"
@@ -11,6 +13,7 @@ import (
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
@@ -29,6 +32,12 @@ func (o *OTAAgent) Run(ctx context.Context) {
client := &http.Client{Timeout: 30 * time.Second}
ticker := time.NewTicker(10 * time.Minute)
defer ticker.Stop()
// Check once on startup if in maintenance window
if inMaintenanceWindow() {
o.check(client)
}
for {
select {
case <-ctx.Done():
@@ -69,10 +78,16 @@ func (o *OTAAgent) check(client *http.Client) {
return
}
if err := o.downloadAndVerify(client, mf); err != nil {
log.Printf("ota: download/verify failed: %v", err)
o.report(client, target, "failed")
return
}
o.report(client, target, "verified")
if err := o.install(target); err != nil {
log.Printf("ota: install failed: %v", err)
o.report(client, target, "install_failed")
return
}
o.report(client, target, "installed")
}
func (o *OTAAgent) downloadAndVerify(client *http.Client, mf map[string]string) error {
@@ -93,13 +108,150 @@ func (o *OTAAgent) downloadAndVerify(client *http.Client, mf map[string]string)
sum := sha256.Sum256(data)
actual := hex.EncodeToString(sum[:])
if actual != expected {
return fmt.Errorf("checksum mismatch")
return fmt.Errorf("checksum mismatch: expected %s got %s", expected, actual)
}
_ = os.MkdirAll("/opt/tianyan-edge/staging", 0o755)
path := filepath.Join("/opt/tianyan-edge/staging", target+".tar.gz")
return os.WriteFile(path, data, 0o644)
}
func (o *OTAAgent) install(target string) error {
stagingPath := filepath.Join("/opt/tianyan-edge/staging", target+".tar.gz")
data, err := os.ReadFile(stagingPath)
if err != nil {
return fmt.Errorf("read staging file: %w", err)
}
// Extract tar.gz into a temp directory
tmpDir, err := os.MkdirTemp("/opt/tianyan-edge/staging", "ota-"+target)
if err != nil {
return fmt.Errorf("create temp dir: %w", err)
}
defer os.RemoveAll(tmpDir)
if err := extractTarGz(bytes.NewReader(data), tmpDir); err != nil {
return fmt.Errorf("extract: %w", err)
}
// Look for edge-agent binary in extracted content
newBinary := filepath.Join(tmpDir, "edge-agent")
if _, err := os.Stat(newBinary); os.IsNotExist(err) {
// Try finding it recursively
found := ""
var stopWalk = fmt.Errorf("found")
filepath.Walk(tmpDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.Name() == "edge-agent" && !info.IsDir() {
found = path
return stopWalk
}
return nil
})
if found == "" {
return fmt.Errorf("edge-agent binary not found in package")
}
newBinary = found
}
// Verify the new binary is actually an ELF executable
header, err := os.Open(newBinary)
if err != nil {
return fmt.Errorf("open new binary: %w", err)
}
magic := make([]byte, 4)
header.Read(magic)
header.Close()
if !bytes.Equal(magic, []byte{0x7f, 0x45, 0x4c, 0x46}) { // ELF magic
return fmt.Errorf("new binary is not a valid ELF executable")
}
// Backup current binary
backupPath := "/opt/tianyan-edge/edge-agent.bak"
if err := copyFile("/opt/tianyan-edge/edge-agent", backupPath); err != nil {
log.Printf("ota: backup failed (non-fatal): %v", err)
}
// Replace binary
if err := copyFile(newBinary, "/opt/tianyan-edge/edge-agent"); err != nil {
return fmt.Errorf("replace binary: %w", err)
}
if err := os.Chmod("/opt/tianyan-edge/edge-agent", 0755); err != nil {
return fmt.Errorf("chmod: %w", err)
}
// Update version in config
o.cfg.Version = target
_ = o.cfg.Save()
// Clean up staging
os.Remove(stagingPath)
// Restart service via systemd
log.Println("ota: binary replaced, restarting service...")
cmd := exec.Command("systemctl", "restart", "edge-agent")
if err := cmd.Run(); err != nil {
log.Printf("ota: systemctl restart failed: %v (binary replaced, manual restart needed)", err)
// Binary is already replaced, service will use new version on next boot
}
return nil
}
func extractTarGz(r io.Reader, dest string) error {
gzr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
target := filepath.Join(dest, hdr.Name)
switch hdr.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(target, os.FileMode(hdr.Mode)); err != nil {
return err
}
case tar.TypeReg:
os.MkdirAll(filepath.Dir(target), 0755)
f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, os.FileMode(hdr.Mode))
if err != nil {
return err
}
if _, err := io.Copy(f, tr); err != nil {
f.Close()
return err
}
f.Close()
}
}
return nil
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
func (o *OTAAgent) report(client *http.Client, version, status string) {
payload := map[string]any{"edge_id": o.cfg.EdgeID, "version": version,
"status": status, "ts": float64(time.Now().UnixMilli()) / 1000.0}

View File

@@ -0,0 +1,106 @@
package control
import (
"archive/tar"
"bytes"
"compress/gzip"
"os"
"path/filepath"
"testing"
)
func TestExtractTarGz(t *testing.T) {
// Create a test tar.gz with a text file and a binary-like file
var buf bytes.Buffer
gw := gzip.NewWriter(&buf)
tw := tar.NewWriter(gw)
// Add a directory
tw.WriteHeader(&tar.Header{
Name: "testdir/",
Typeflag: tar.TypeDir,
Mode: 0755,
})
// Add a text file
content := []byte("hello world")
tw.WriteHeader(&tar.Header{
Name: "testdir/hello.txt",
Size: int64(len(content)),
Mode: 0644,
})
tw.Write(content)
// Add a binary file (fake ELF header)
binContent := []byte{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x02}
tw.WriteHeader(&tar.Header{
Name: "testdir/edge-agent",
Size: int64(len(binContent)),
Mode: 0755,
})
tw.Write(binContent)
tw.Close()
gw.Close()
// Extract to temp dir
dest, _ := os.MkdirTemp("", "ota-test-*")
defer os.RemoveAll(dest)
if err := extractTarGz(bytes.NewReader(buf.Bytes()), dest); err != nil {
t.Fatalf("extractTarGz failed: %v", err)
}
// Verify extracted files
helloPath := filepath.Join(dest, "testdir", "hello.txt")
data, err := os.ReadFile(helloPath)
if err != nil {
t.Fatalf("hello.txt not found: %v", err)
}
if string(data) != "hello world" {
t.Errorf("hello.txt content: got %q want %q", string(data), "hello world")
}
binPath := filepath.Join(dest, "testdir", "edge-agent")
data, err = os.ReadFile(binPath)
if err != nil {
t.Fatalf("edge-agent not found: %v", err)
}
if !bytes.Equal(data, binContent) {
t.Errorf("edge-agent content mismatch")
}
}
func TestExtractTarGzInvalid(t *testing.T) {
dest, _ := os.MkdirTemp("", "ota-test-*")
defer os.RemoveAll(dest)
if err := extractTarGz(bytes.NewReader([]byte("not a gzip")), dest); err == nil {
t.Error("expected error for invalid gzip")
}
}
func TestCopyFile(t *testing.T) {
src, _ := os.CreateTemp("", "src-*")
src.Write([]byte("test data"))
src.Close()
defer os.Remove(src.Name())
dst := src.Name() + ".copy"
defer os.Remove(dst)
if err := copyFile(src.Name(), dst); err != nil {
t.Fatalf("copyFile failed: %v", err)
}
data, _ := os.ReadFile(dst)
if string(data) != "test data" {
t.Errorf("copy content: got %q want %q", string(data), "test data")
}
}
func TestCopyFileNotFound(t *testing.T) {
if err := copyFile("/nonexistent/file", "/tmp/dst"); err == nil {
t.Error("expected error for nonexistent source")
}
}

View File

@@ -0,0 +1,25 @@
package event
import (
"testing"
)
func TestBufferCap(t *testing.T) {
u := &Uploader{buf: []SuspectedEvent{}}
for i := 0; i < 600; i++ {
u.buffer(SuspectedEvent{EventID: "test"})
}
if len(u.buf) != maxBuffer {
t.Errorf("buffer len: got %d want %d", len(u.buf), maxBuffer)
}
}
func TestUploadError(t *testing.T) {
err := &UploadError{Fatal: true, Msg: "test error"}
if err.Error() != "test error" {
t.Errorf("Error(): got %q want %q", err.Error(), "test error")
}
if !err.Fatal {
t.Error("Fatal flag should be true")
}
}

View File

@@ -208,10 +208,17 @@ func (c *Client) send(conn net.Conn, f stream.Frame) error {
if err != nil {
return err
}
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
defer conn.SetWriteDeadline(time.Time{})
return writeMsg(conn, data)
}
func (c *Client) recv(conn net.Conn) (*resultMsg, error) {
// Set read deadline to prevent blocking forever if inference server hangs
// 30s timeout covers even slow NPU inference on large images
conn.SetReadDeadline(time.Now().Add(30 * time.Second))
defer conn.SetReadDeadline(time.Time{}) // reset after read
data, err := readMsg(conn)
if err != nil {
return nil, err

View File

@@ -0,0 +1,80 @@
package infer
import (
"testing"
"tianyan-edge/internal/config"
)
func TestIsDuplicate(t *testing.T) {
cfg := &config.Config{DedupWindowSec: 30}
c := &Client{
cfg: cfg,
dedup: &dedupState{seen: make(map[string]float64)},
}
// First detection should NOT be duplicate (uses default config dedup window of 30s)
// ts=100.0, key="cam-001|person" — first time, not duplicate
if c.isDuplicate("cam-001", "person", 100.0) {
t.Error("first detection should not be duplicate")
}
// Same device+class within window should BE duplicate
// ts=105.0, 105-100=5 < 30, duplicate
if !c.isDuplicate("cam-001", "person", 105.0) {
t.Error("same event within window should be duplicate")
}
// Same device+class outside window should NOT be duplicate
// ts=200.0, 200-105=95 > 30, not duplicate (updates the timestamp)
if c.isDuplicate("cam-001", "person", 200.0) {
t.Error("same event outside window should not be duplicate")
}
// Different class should NOT be duplicate
if c.isDuplicate("cam-001", "car", 105.0) {
t.Error("different class should not be duplicate")
}
// Different device should NOT be duplicate
if c.isDuplicate("cam-002", "person", 105.0) {
t.Error("different device should not be duplicate")
}
}
func TestDedupMapCleanup(t *testing.T) {
cfg := &config.Config{DedupWindowSec: 30}
c := &Client{
cfg: cfg,
dedup: &dedupState{seen: make(map[string]float64)},
}
// Fill up the map with old entries
for i := 0; i < 5001; i++ {
c.dedup.seen["cam-001|person"] = float64(i * 10)
}
// Trigger cleanup by adding a new entry
c.isDuplicate("cam-001", "person", 99999.0)
if len(c.dedup.seen) > 5000 {
t.Errorf("dedup map should be cleaned up, got %d entries", len(c.dedup.seen))
}
}
func TestMustUUID(t *testing.T) {
u1 := mustUUID()
u2 := mustUUID()
if u1 == u2 {
t.Error("UUIDs should be unique")
}
// Check format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (32 hex chars with dashes)
parts := 0
for _, c := range u1 {
if c == '-' {
parts++
}
}
if parts != 4 {
t.Errorf("UUID format: expected 4 dashes, got %d", parts)
}
}

View File

@@ -22,10 +22,15 @@ type Frame struct {
TS float64
}
type streamProcess struct {
cmd *exec.Cmd
fps int
}
type StreamManager struct {
cfg *config.Config
frames chan<- Frame
processes map[string]*exec.Cmd
processes map[string]*streamProcess // url -> {cmd, fps}
mu sync.Mutex
updates <-chan *config.Config
}
@@ -34,14 +39,14 @@ func NewStreamManager(cfg *config.Config, frames chan<- Frame, updates <-chan *c
return &StreamManager{
cfg: cfg,
frames: frames,
processes: make(map[string]*exec.Cmd),
processes: make(map[string]*streamProcess),
updates: updates,
}
}
func (sm *StreamManager) Run(ctx context.Context) {
// Initial start
sm.applyStreams(ctx, sm.cfg.RTSPURLs)
sm.applyStreams(ctx, sm.cfg.RTSPURLs, sm.cfg.InferFPS)
for {
select {
@@ -49,19 +54,19 @@ func (sm *StreamManager) Run(ctx context.Context) {
sm.stopAll()
return
case newCfg := <-sm.updates:
log.Printf("stream: applying new config, urls=%d", len(newCfg.RTSPURLs))
sm.applyStreams(ctx, newCfg.RTSPURLs)
log.Printf("stream: applying new config, urls=%d fps=%d", len(newCfg.RTSPURLs), newCfg.InferFPS)
sm.applyStreams(ctx, newCfg.RTSPURLs, newCfg.InferFPS)
}
}
}
func (sm *StreamManager) applyStreams(ctx context.Context, urls []string) {
func (sm *StreamManager) applyStreams(ctx context.Context, urls []string, fps int) {
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 {
toRemove := map[string]*streamProcess{}
for url, sp := range sm.processes {
found := false
for _, u := range urls {
if u == url {
@@ -70,27 +75,35 @@ func (sm *StreamManager) applyStreams(ctx context.Context, urls []string) {
}
}
if !found {
toRemove[url] = cmd
toRemove[url] = sp
}
}
// Stop removed streams
for url, cmd := range toRemove {
for url, sp := range toRemove {
log.Printf("stream: stopping %s", url)
cmd.Process.Kill()
cmd.Wait()
sm.stopProcess(sp)
delete(sm.processes, url)
}
// Start new streams
// Restart streams with changed fps
for url, sp := range sm.processes {
if sp.fps != fps {
log.Printf("stream: restarting %s (fps %d -> %d)", url, sp.fps, fps)
sm.stopProcess(sp)
delete(sm.processes, url)
}
}
// Start new / restarted streams
for i, url := range urls {
if _, exists := sm.processes[url]; !exists {
go sm.streamLoop(ctx, i, url)
go sm.streamLoop(ctx, i, url, fps)
}
}
}
func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string) {
func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string, fps int) {
deviceID := fmt.Sprintf("cam-%03d", idx)
for {
select {
@@ -99,9 +112,17 @@ func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string) {
default:
}
// Use current config fps (may have been updated)
currentFps := fps
sm.mu.Lock()
if sm.cfg != nil && sm.cfg.InferFPS > 0 {
currentFps = sm.cfg.InferFPS
}
sm.mu.Unlock()
cmd := exec.CommandContext(ctx, "ffmpeg",
"-i", url,
"-vf", fmt.Sprintf("fps=%d", sm.cfg.InferFPS),
"-vf", fmt.Sprintf("fps=%d", currentFps),
"-f", "image2pipe", "-vcodec", "mjpeg", "-q:v", "5", "-",
)
stdout, err := cmd.StdoutPipe()
@@ -111,20 +132,29 @@ func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string) {
continue
}
sp := &streamProcess{cmd: cmd, fps: currentFps}
sm.mu.Lock()
sm.processes[url] = cmd
sm.processes[url] = sp
sm.mu.Unlock()
if err := cmd.Start(); err != nil {
log.Printf("stream[%d] start failed: %v", idx, err)
sm.mu.Lock()
delete(sm.processes, url)
sm.mu.Unlock()
time.Sleep(5 * time.Second)
continue
}
log.Printf("stream[%d] connected url=%s", idx, url)
log.Printf("stream[%d] connected fps=%d url=%s", idx, currentFps, url)
sm.readFrames(ctx, stdout, idx, deviceID, url)
cmd.Wait()
// Clean up from map after exit
sm.mu.Lock()
delete(sm.processes, url)
sm.mu.Unlock()
select {
case <-ctx.Done():
return
@@ -175,12 +205,20 @@ func (sm *StreamManager) readFrames(ctx context.Context, r io.Reader, idx int, d
}
}
func (sm *StreamManager) stopProcess(sp *streamProcess) {
if sp == nil || sp.cmd == nil || sp.cmd.Process == nil {
return
}
sp.cmd.Process.Kill()
sp.cmd.Wait() // Prevent zombie processes
}
func (sm *StreamManager) stopAll() {
sm.mu.Lock()
defer sm.mu.Unlock()
for url, cmd := range sm.processes {
for url, sp := range sm.processes {
log.Printf("stream: stopping all %s", url)
cmd.Process.Kill()
sm.stopProcess(sp)
delete(sm.processes, url)
}
}