jim800121chen 3f0175f1a9 feat(local-agent): Phase 0.5 visionA Agent — Wails 桌面 + tunnel client + 配對 UI
從 local-tool 複製出獨立的「visionA Agent」桌面應用(A3 純橋樑:
tunnel client + 配對 UI + 設定,不開 HTTP port、不做本機裝置/推論 UI)。
Bundle ID 與 local-tool 不同(com.innovedus.visiona-agent vs visiona-local),
雙 app 可共存。fork 後不主動 sync,需要時手動 cherry-pick。

Backend / Wails Go(AB1-AB13):
- internal/tunnel:6 狀態機(Idle/Connecting/Connected/Reconnecting/Failed/Stopped)
  + Pair/Unpair/Reconnect/Disconnect binding + ClientHooks event
- internal/auth:encrypted file token store(AES-GCM + scrypt + machineID
  fallback salt + 13 tests)
- internal/config:YAML validation + atomic write + 11 tests
- internal/log:ring buffer + ExportLog 升級 zip
- visionA-backend /api/pairing/exchange:SessionTokenStore + 17 new tests
- 三平台 build 驗證(macOS DMG 160 MB / Windows EXE / Linux AppImage)
- end-to-end 5 milestone 全綠(pairing → tunnel → forward → reuse 防護
  → tunnel drop failover)

Frontend / Next.js(AF1-AF7,沿用 visionA-frontend 基礎):
- AppShell + Header + TabNav(StatusView / PairView / SettingsView 三 tab)
- ConnectionStatusBadge 5 種狀態
- TokenInput regex 驗證 + 7 種錯誤 + 0.5s auto-switch 到狀態頁
- 設定頁 4 區塊(含重新配對 AlertDialog)
- agent-api.ts 封裝 Wails bindings(mock/real 雙實作)+ 90 tests

Phase 0.7 review-driven fix(Round 2):
- A1 Session fixation 防護(RotateSessionID)
- A3 mock pairing 預設改 false(必須明確 opt-in)+ startup log
- A4 Pair 失敗後 state 清理矩陣(exchange/Save/Start fail 各自終態)
- A5 Pair/Unpair/Reconnect lifecycleMu + 50 goroutine race test
- F1 重新配對次按鈕 / F2 PairView Esc cancel / F3 Wails BrowserOpenURL
  / F4 Settings draft 持久 + 未儲存 badge

驗證:agent backend go test -race -count=3 ./... 4 packages 全綠 /
agent frontend pnpm test 119 tests 全綠

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 11:22:01 +08:00

135 lines
3.1 KiB
Go

package camera
import (
"fmt"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
)
// DetectFFmpeg checks if ffmpeg is available on the system.
func DetectFFmpeg() bool {
_, err := exec.LookPath("ffmpeg")
return err == nil
}
// ListFFmpegDevices detects available video devices using ffmpeg.
// Automatically selects the correct capture framework for the current OS:
// - macOS: AVFoundation
// - Windows: DirectShow (dshow)
func ListFFmpegDevices() []CameraInfo {
if !DetectFFmpeg() {
return nil
}
switch runtime.GOOS {
case "windows":
return listDShowDevices()
default:
return listAVFoundationDevices()
}
}
// --- macOS (AVFoundation) ---
func listAVFoundationDevices() []CameraInfo {
cmd := exec.Command("ffmpeg", "-f", "avfoundation", "-list_devices", "true", "-i", "")
output, _ := cmd.CombinedOutput()
return parseAVFoundationOutput(string(output))
}
// parseAVFoundationOutput parses ffmpeg AVFoundation device listing.
// Example:
//
// [AVFoundation indev @ 0x...] AVFoundation video devices:
// [AVFoundation indev @ 0x...] [0] FaceTime HD Camera
// [AVFoundation indev @ 0x...] [1] Capture screen 0
// [AVFoundation indev @ 0x...] AVFoundation audio devices:
func parseAVFoundationOutput(output string) []CameraInfo {
var cameras []CameraInfo
lines := strings.Split(output, "\n")
deviceRe := regexp.MustCompile(`\[AVFoundation[^\]]*\]\s*\[(\d+)\]\s*(.+)`)
inVideoSection := false
for _, line := range lines {
if strings.Contains(line, "AVFoundation video devices") {
inVideoSection = true
continue
}
if strings.Contains(line, "AVFoundation audio devices") {
break
}
if !inVideoSection {
continue
}
matches := deviceRe.FindStringSubmatch(line)
if len(matches) == 3 {
index, err := strconv.Atoi(matches[1])
if err != nil {
continue
}
name := strings.TrimSpace(matches[2])
// Skip screen capture devices
if strings.Contains(strings.ToLower(name), "capture screen") {
continue
}
cameras = append(cameras, CameraInfo{
ID: fmt.Sprintf("cam-%d", index),
Name: name,
Index: index,
Width: 640,
Height: 480,
})
}
}
return cameras
}
// --- Windows (DirectShow) ---
func listDShowDevices() []CameraInfo {
cmd := exec.Command("ffmpeg", "-f", "dshow", "-list_devices", "true", "-i", "dummy")
output, _ := cmd.CombinedOutput()
return parseDShowOutput(string(output))
}
// parseDShowOutput parses ffmpeg DirectShow device listing.
// Example:
//
// [dshow @ 0x...] "Integrated Camera" (video)
// [dshow @ 0x...] Alternative name "@device_pnp_..."
// [dshow @ 0x...] "Microphone" (audio)
func parseDShowOutput(output string) []CameraInfo {
var cameras []CameraInfo
lines := strings.Split(output, "\n")
// Match: [dshow @ 0x...] "Device Name" (video)
deviceRe := regexp.MustCompile(`\[dshow[^\]]*\]\s*"([^"]+)"\s*\(video\)`)
index := 0
for _, line := range lines {
matches := deviceRe.FindStringSubmatch(line)
if len(matches) == 2 {
name := strings.TrimSpace(matches[1])
cameras = append(cameras, CameraInfo{
ID: fmt.Sprintf("cam-%d", index),
Name: name,
Index: index,
Width: 640,
Height: 480,
})
index++
}
}
return cameras
}