2026-05-06 21:49:04 +08:00
|
|
|
package stream
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-05-08 16:26:10 +08:00
|
|
|
"bytes"
|
|
|
|
|
|
2026-05-06 21:49:04 +08:00
|
|
|
"tianyan-edge/internal/config"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Frame struct {
|
|
|
|
|
StreamID int
|
|
|
|
|
DeviceID string
|
|
|
|
|
URL string
|
|
|
|
|
JPEG []byte
|
|
|
|
|
TS float64
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 16:26:10 +08:00
|
|
|
type StreamManager struct {
|
|
|
|
|
cfg *config.Config
|
|
|
|
|
frames chan<- Frame
|
|
|
|
|
processes map[string]*exec.Cmd
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
updates <-chan *config.Config
|
2026-05-06 21:49:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 16:26:10 +08:00
|
|
|
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,
|
|
|
|
|
}
|
2026-05-06 21:49:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 16:26:10 +08:00
|
|
|
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)
|
|
|
|
|
}
|
2026-05-06 21:49:04 +08:00
|
|
|
}
|
2026-05-08 16:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2026-05-06 21:49:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 16:26:10 +08:00
|
|
|
func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string) {
|
2026-05-06 21:49:04 +08:00
|
|
|
deviceID := fmt.Sprintf("cam-%03d", idx)
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
}
|
2026-05-08 16:26:10 +08:00
|
|
|
|
2026-05-06 21:49:04 +08:00
|
|
|
cmd := exec.CommandContext(ctx, "ffmpeg",
|
2026-05-08 16:26:10 +08:00
|
|
|
"-i", url,
|
|
|
|
|
"-vf", fmt.Sprintf("fps=%d", sm.cfg.InferFPS),
|
2026-05-06 21:49:04 +08:00
|
|
|
"-f", "image2pipe", "-vcodec", "mjpeg", "-q:v", "5", "-",
|
|
|
|
|
)
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
|
if err != nil {
|
2026-05-08 16:26:10 +08:00
|
|
|
log.Printf("stream[%d] stdout pipe failed: %v", idx, err)
|
|
|
|
|
time.Sleep(5 * time.Second)
|
2026-05-06 21:49:04 +08:00
|
|
|
continue
|
|
|
|
|
}
|
2026-05-08 16:26:10 +08:00
|
|
|
|
|
|
|
|
sm.mu.Lock()
|
|
|
|
|
sm.processes[url] = cmd
|
|
|
|
|
sm.mu.Unlock()
|
|
|
|
|
|
2026-05-06 21:49:04 +08:00
|
|
|
if err := cmd.Start(); err != nil {
|
2026-05-08 16:26:10 +08:00
|
|
|
log.Printf("stream[%d] start failed: %v", idx, err)
|
|
|
|
|
time.Sleep(5 * time.Second)
|
2026-05-06 21:49:04 +08:00
|
|
|
continue
|
|
|
|
|
}
|
2026-05-08 16:26:10 +08:00
|
|
|
|
2026-05-06 21:49:04 +08:00
|
|
|
log.Printf("stream[%d] connected url=%s", idx, url)
|
2026-05-08 16:26:10 +08:00
|
|
|
sm.readFrames(ctx, stdout, idx, deviceID, url)
|
2026-05-06 21:49:04 +08:00
|
|
|
cmd.Wait()
|
2026-05-08 16:26:10 +08:00
|
|
|
|
2026-05-06 21:49:04 +08:00
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
log.Printf("stream[%d] lost, retry 5s", idx)
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 16:26:10 +08:00
|
|
|
func (sm *StreamManager) readFrames(ctx context.Context, r io.Reader, idx int, deviceID, url string) {
|
2026-05-06 21:49:04 +08:00
|
|
|
soi, eoi := []byte{0xFF, 0xD8}, []byte{0xFF, 0xD9}
|
|
|
|
|
buf, tmp := make([]byte, 0, 1<<20), make([]byte, 32768)
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
n, err := r.Read(tmp)
|
|
|
|
|
if n > 0 {
|
|
|
|
|
buf = append(buf, tmp[:n]...)
|
|
|
|
|
for {
|
|
|
|
|
s := bytes.Index(buf, soi)
|
|
|
|
|
if s < 0 {
|
|
|
|
|
buf = buf[:0]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
buf = buf[s:]
|
|
|
|
|
e := bytes.Index(buf[2:], eoi)
|
|
|
|
|
if e < 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
end := e + 4
|
|
|
|
|
frame := make([]byte, end)
|
|
|
|
|
copy(frame, buf[:end])
|
|
|
|
|
buf = buf[end:]
|
|
|
|
|
select {
|
2026-05-08 16:26:10 +08:00
|
|
|
case sm.frames <- Frame{StreamID: idx, DeviceID: deviceID, URL: url,
|
2026-05-06 21:49:04 +08:00
|
|
|
JPEG: frame, TS: float64(time.Now().UnixMilli()) / 1000.0}:
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-08 16:26:10 +08:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|