How yuragb/front_cam_detect works
Purpose and Entry Points
cam_frontend is a browser frontend for live camera monitoring. It negotiates WebRTC with a C++ camera service through a Bun signaling server, renders H264 tracks, overlays YOLO detections, and shows latency/pipeline metrics. The client bootstraps through TanStack Start routing in src/router.tsx, generated route definitions in src/routeTree.gen.ts, and root layout/provider composition in src/routes/__root.tsx.
Routing, Access, and Shell
Public routes include the home page and auth API; authenticated application pages are grouped under a protected route layout. Route-level protection and session retrieval form the boundary before dashboard, profile, address, and video pages render.
• src/routes/(protected)/route.tsx
• src/routes/(protected)/video_stream/index.tsx
• src/lib/authBeforeLoad.ts
The reusable application shell combines header, sidebar, locale/theme controls, and responsive UI components.
• src/components/Header/Header.tsx
• src/modules/Sidebar/index.tsx
• src/components/SidebarWrapper.tsx
Authentication and Persistence
Better Auth serves through a catch-all API route and a server-side auth module. Auth configuration integrates with Drizzle-backed user/session schema and synchronization logic; the browser uses a dedicated Better Auth client and session hooks.
• src/routes/api/auth/$.ts
• src/server/modules/Auth/auth.ts
• src/server/modules/db/schema/auth.ts
Database initialization is centralized in Drizzle, with migrations applied through a server plugin.
• src/server/modules/db/drizzle/index.ts
• src/server/plugins/migrations/index.ts
• drizzle/0000_motionless_mandarin.sql
WebRTC Signaling and Session Control
The VideoStream module opens one WebSocket to the Bun signaling server, registers a frontend peer identity, joins the C++ camera peer, and exchanges SDP offers/answers plus ICE candidates. PeerConnection lifecycle, stream association, and signaling message handling are separated into hooks and connection utilities.
• src/modules/VideoStream/hooks/useWebsocket.ts
• src/modules/VideoStream/hooks/usePc.ts
• src/modules/VideoStream/lib/websocketConnection.ts
This is the primary external integration boundary: browser WebSocket signaling versus browser-native WebRTC media/DataChannel transport to the C++ camera service.
Stream Rendering and Detection Overlay
Received video tracks are managed per camera and rendered as native <video> media, with an adjacent canvas overlay. Track-map messages connect WebRTC media IDs to camera IDs; detection frames are parsed, retained, and painted without rebinding media srcObject.
• src/modules/VideoStream/hooks/useStreams.ts
• src/modules/VideoStream/components/CameraStreamView.tsx
• src/modules/VideoStream/lib/detections.ts
src/modules/VideoStream/index.tsx composes stream access, peer connection, detection, metric, and view state into the live-monitoring page.
Telemetry Pipeline
The C++ service sends detection, track-map, video-latency, and pipeline-metric payloads over the detectionStream DataChannel. Parsing routes payload types into specialized state. Latency uses service capture timestamps plus requestVideoFrameCallback() render timing, while pipeline metrics expose capture, encoding, inference, and dropped-frame health separately.
• src/modules/VideoStream/hooks/useVideoLatencyMetrics.ts
• src/modules/VideoStream/hooks/usePipelineMetrics.ts
• src/modules/VideoStream/components/PipelineMetricsPanel.tsx
Runtime and Deployment
The runtime is React with TanStack Start/Router, TanStack Query, Tailwind-based UI, Paraglide localization, Better Auth, and Drizzle. Health/readiness endpoints support orchestration.
• src/routes/(probes)/health.ts
• src/routes/(probes)/ready.ts
• src/integrations/tanstack-query/root-provider.tsx
Container deployment targets k3s with namespace, ConfigMap/secrets, persistent volume, Deployment, Service, and ingress manifests.
• Dockerfile
• k3s/frontend-app-deployment.yaml
• k3s/ingress.yaml
Main entry points: usePc.ts WebRTC peer lifecycle · route.tsx Protected routes · detections.ts Telemetry parsing · index.tsx Video stream page
Components 22
Browser client
React live-monitoring application.
- TanStack router · Bootstraps routes and generated route definitions.
src/router.tsx - Root providers · Composes global layout and providers.
src/routes/__root.tsx - Protected routes · Retrieves sessions before application pages render.
src/routes/(protected)/route.tsx - Session guard
src/lib/authBeforeLoad.ts - Application shell · Header, sidebar, locale, and theme controls.
src/components/SidebarWrapper.tsx - Video stream page · Composes monitoring state, controls, and views.
src/modules/VideoStream/index.tsx - Signaling session · Registers the frontend peer and exchanges signaling messages.
src/modules/VideoStream/hooks/useWebsocket.ts - WebRTC peer lifecycle · Manages media and DataChannel connections.
src/modules/VideoStream/hooks/usePc.ts - Camera stream state · Associates received media tracks with cameras.
src/modules/VideoStream/hooks/useStreams.ts - Camera video and overlay · Renders native video with adjacent detection canvas.
src/modules/VideoStream/components/CameraStreamView.tsx - Telemetry parsing · Routes detection and track-map payloads into state.
src/modules/VideoStream/lib/detections.ts - Video latency metrics · Combines capture timestamps with rendered-frame timing.
src/modules/VideoStream/hooks/useVideoLatencyMetrics.ts - Pipeline metrics · Tracks capture, encoding, inference, and dropped frames.
src/modules/VideoStream/hooks/usePipelineMetrics.ts - Metrics panel
src/modules/VideoStream/components/PipelineMetricsPanel.tsx
Application server
TanStack Start server routes and persistence.
- Better Auth API
src/routes/api/auth/$.ts - Auth service · Configures Better Auth and schema synchronization.
src/server/modules/Auth/auth.ts - User and session database · Auth schema with migration-managed storage.
src/server/modules/db/drizzle/index.ts - Health and readiness
src/routes/(probes)/health.ts
Camera integration
External Bun signaling and C++ camera service.
- Bun signaling server · Relays SDP and ICE negotiation messages.
- C++ camera service · Provides H264 media and detection telemetry.
Deployment
Container and k3s runtime resources.
- Frontend container
Dockerfile - k3s deployment · Deploys the frontend with service and ingress.
k3s/frontend-app-deployment.yaml
Connections 21
- TanStack router → Root providers: mounts
- Root providers → Protected routes: hosts
- Protected routes → Session guard: checks session
- Protected routes → Application shell: renders within
- Protected routes → Video stream page: allows access
- Video stream page → Signaling session: opens
- Video stream page → WebRTC peer lifecycle: controls
- Signaling session → Bun signaling server: WebSocket SDP and ICE
- WebRTC peer lifecycle → C++ camera service: WebRTC media and DataChannel
- Signaling session → WebRTC peer lifecycle: feeds negotiation
- WebRTC peer lifecycle → Camera stream state: received tracks
- Camera stream state → Camera video and overlay: camera media
- WebRTC peer lifecycle → Telemetry parsing: detectionStream payloads
- Telemetry parsing → Camera video and overlay: detection overlay
- Telemetry parsing → Video latency metrics: capture timestamps
- Telemetry parsing → Pipeline metrics: pipeline payloads
- Pipeline metrics → Metrics panel: health metrics
- Video latency metrics → Metrics panel: latency metric
- Better Auth API → Auth service: delegates
- Auth service → User and session database: users and sessions
- Frontend container → k3s deployment: deployed by