jim800121chen c54f16fca0 Initial commit: visionA monorepo with local-tool subproject
local-tool/: visionA-local desktop app
- M1: Wails shell + Go server + Next.js UI + Mock mode (macOS dmg ready)
- M2: i18n (zh-TW/en) + Settings 4-tab refactor
- M3: Embedded Python 3.12 runtime (python-build-standalone) + KneronPLUS wheels
- M4: Windows Inno Setup script (build on Windows runner)
- M5: Linux AppImage script + udev rule (build on Linux runner)
- M6: ffmpeg (GPL, pending legal review) + yt-dlp bundled
- Lifecycle: watchServer health check, fatal native dialog,
            Wails IPC raise endpoint, stale process cleanup

.autoflow/: full PRD / Design Spec / Architecture / Testing docs
            (4 rounds tri-party discussion + cross review)
.github/workflows/: macOS / Windows / Linux build CI

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 22:10:38 +08:00

82 lines
1.5 KiB
Go

package camera
import (
"fmt"
"net/http"
"sync"
)
type MJPEGStreamer struct {
frameCh chan []byte
clients map[chan []byte]bool
mu sync.Mutex
}
func NewMJPEGStreamer() *MJPEGStreamer {
return &MJPEGStreamer{
frameCh: make(chan []byte, 3),
clients: make(map[chan []byte]bool),
}
}
func (s *MJPEGStreamer) FrameChannel() chan<- []byte {
return s.frameCh
}
func (s *MJPEGStreamer) Run() {
for frame := range s.frameCh {
s.mu.Lock()
for ch := range s.clients {
select {
case ch <- frame:
default:
// drop frame for slow client
}
}
s.mu.Unlock()
}
}
func (s *MJPEGStreamer) AddClient() chan []byte {
ch := make(chan []byte, 3)
s.mu.Lock()
s.clients[ch] = true
s.mu.Unlock()
return ch
}
func (s *MJPEGStreamer) RemoveClient(ch chan []byte) {
s.mu.Lock()
delete(s.clients, ch)
s.mu.Unlock()
}
func (s *MJPEGStreamer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary=frame")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming not supported", 500)
return
}
clientCh := s.AddClient()
defer s.RemoveClient(clientCh)
for {
select {
case <-r.Context().Done():
return
case frame := <-clientCh:
fmt.Fprintf(w, "--frame\r\n")
fmt.Fprintf(w, "Content-Type: image/jpeg\r\n")
fmt.Fprintf(w, "Content-Length: %d\r\n\r\n", len(frame))
w.Write(frame)
fmt.Fprintf(w, "\r\n")
flusher.Flush()
}
}
}