All files / web/src/components/vision VisionCameraFeed.tsx

0% Statements 0/437
0% Branches 0/1
0% Functions 0/1
0% Lines 0/437

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
'use client'

import type { ReactNode } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { css } from '../../../styled-system/css'
import type { CalibrationGrid, QuadCorners } from '@/types/vision'
import { isOpenCVReady, loadOpenCV, rectifyQuadrilateral } from '@/lib/vision/perspectiveTransform'
import { CalibrationQuadEditor } from './CalibrationQuadEditor'

export interface VisionCameraFeedProps {
  /** Video stream to display */
  videoStream: MediaStream | null
  /** Whether camera is currently loading */
  isLoading?: boolean
  /** Calibration grid to visualize (optional) */
  calibration?: CalibrationGrid | null
  /** Whether to show the calibration grid overlay */
  showCalibrationGrid?: boolean
  /** Whether to show rectified (perspective-corrected) view */
  showRectifiedView?: boolean
  /** Video element ref callback for external access */
  videoRef?: (el: HTMLVideoElement | null) => void
  /** Rectified canvas ref callback for external access (only when showRectifiedView=true) */
  rectifiedCanvasRef?: (el: HTMLCanvasElement | null) => void
  /** Called when video metadata is loaded (provides dimensions) */
  onVideoReady?: (width: number, height: number) => void
  /** Children rendered over the video (e.g., CalibrationOverlay) */
  children?: ReactNode
  /** Number of abacus columns (used for fixed aspect ratio in rectified view) */
  columnCount?: number
}

/**
 * Get corners from calibration (prefer QuadCorners, fall back to ROI rectangle)
 */
function getCornersFromCalibration(calibration: CalibrationGrid): QuadCorners {
  if (calibration.corners) {
    return calibration.corners
  }
  const roi = calibration.roi
  return {
    topLeft: { x: roi.x, y: roi.y },
    topRight: { x: roi.x + roi.width, y: roi.y },
    bottomLeft: { x: roi.x, y: roi.y + roi.height },
    bottomRight: { x: roi.x + roi.width, y: roi.y + roi.height },
  }
}

/**
 * VisionCameraFeed - Displays camera video with optional calibration grid overlay
 *
 * Renders the video stream and optionally draws the calibration grid
 * showing the ROI and column dividers. Can also show a rectified
 * (perspective-corrected) view of the calibrated region.
 */
export function VisionCameraFeed({
  videoStream,
  isLoading = false,
  calibration,
  showCalibrationGrid = false,
  showRectifiedView = false,
  videoRef: externalVideoRef,
  rectifiedCanvasRef: externalCanvasRef,
  onVideoReady,
  children,
  columnCount,
}: VisionCameraFeedProps): ReactNode {
  const internalVideoRef = useRef<HTMLVideoElement>(null)
  const rectifiedCanvasRef = useRef<HTMLCanvasElement | null>(null)
  const containerRef = useRef<HTMLDivElement>(null)
  const animationFrameRef = useRef<number | null>(null)

  // Initialize opencvReady synchronously if already loaded (avoids flash on remount)
  const [opencvReady, setOpencvReady] = useState(() => isOpenCVReady())

  // Track when canvas is mounted (refs don't trigger re-renders, so we need state)
  const [canvasMounted, setCanvasMounted] = useState(false)

  // Track container and video dimensions for CalibrationQuadEditor
  const [containerDimensions, setContainerDimensions] = useState({ width: 0, height: 0 })
  const [videoDimensions, setVideoDimensions] = useState({ width: 0, height: 0 })

  // Load OpenCV when rectified view is needed
  useEffect(() => {
    if (showRectifiedView && !opencvReady) {
      loadOpenCV()
        .then(() => setOpencvReady(true))
        .catch((err) => console.error('[VisionCameraFeed] Failed to load OpenCV:', err))
    }
  }, [showRectifiedView, opencvReady])

  // Track container dimensions for CalibrationQuadEditor (SVG needs explicit size)
  // NOTE: videoStream in deps ensures this re-runs when switching from "no stream" to "has stream"
  // because the container element only renders when videoStream exists
  useEffect(() => {
    const container = containerRef.current
    if (!container) return

    const observer = new ResizeObserver((entries) => {
      for (const entry of entries) {
        const { width, height } = entry.contentRect
        setContainerDimensions({ width, height })
      }
    })

    observer.observe(container)
    // Get initial dimensions
    const rect = container.getBoundingClientRect()
    setContainerDimensions({ width: rect.width, height: rect.height })

    return () => observer.disconnect()
  }, [videoStream])

  // Set video ref for external access
  // IMPORTANT: Must re-run when videoStream changes because the video element
  // only renders when videoStream exists (see early return below)
  useEffect(() => {
    if (externalVideoRef) {
      externalVideoRef(internalVideoRef.current)
    }
  }, [externalVideoRef, videoStream])

  // Combined ref callback for rectified canvas - sets internal ref AND calls external callback
  // Also updates canvasMounted state to trigger effect re-run
  const handleRectifiedCanvasRef = useCallback(
    (el: HTMLCanvasElement | null) => {
      rectifiedCanvasRef.current = el
      setCanvasMounted(el !== null)
      if (externalCanvasRef) {
        externalCanvasRef(el)
      }
    },
    [externalCanvasRef]
  )

  // Attach stream to video element
  useEffect(() => {
    const video = internalVideoRef.current
    if (!video) return

    let isMounted = true

    if (videoStream) {
      video.srcObject = videoStream
      video.play().catch((err) => {
        // Ignore AbortError - happens when component unmounts during play()
        if (isMounted && err.name !== 'AbortError') {
          console.error('Failed to play video:', err)
        }
      })
    } else {
      video.srcObject = null
    }

    return () => {
      isMounted = false
    }
  }, [videoStream])

  // Handle video metadata loaded
  useEffect(() => {
    const video = internalVideoRef.current
    if (!video || !videoStream) return

    const handleLoadedMetadata = () => {
      if (video.videoWidth > 0 && video.videoHeight > 0) {
        setVideoDimensions({ width: video.videoWidth, height: video.videoHeight })
        if (onVideoReady) {
          onVideoReady(video.videoWidth, video.videoHeight)
        }
      }
    }

    // Check if already loaded (e.g., from cache or fast load)
    if (video.readyState >= 1 && video.videoWidth > 0) {
      handleLoadedMetadata()
    }

    video.addEventListener('loadedmetadata', handleLoadedMetadata)
    return () => {
      video.removeEventListener('loadedmetadata', handleLoadedMetadata)
    }
  }, [videoStream, onVideoReady])

  // Rectified view processing loop
  // NOTE: canvasMounted in deps ensures this re-runs when canvas becomes available after mount
  useEffect(() => {
    if (!showRectifiedView || !calibration || !opencvReady || !canvasMounted) {
      return
    }

    const video = internalVideoRef.current
    const canvas = rectifiedCanvasRef.current
    if (!video || !canvas) return

    const corners = getCornersFromCalibration(calibration)
    let running = true

    const processFrame = () => {
      if (!running) return

      if (video.readyState >= 2 && isOpenCVReady()) {
        rectifyQuadrilateral(video, corners, canvas, { columnCount })
      }

      animationFrameRef.current = requestAnimationFrame(processFrame)
    }

    processFrame()

    return () => {
      running = false
      if (animationFrameRef.current !== null) {
        cancelAnimationFrame(animationFrameRef.current)
        animationFrameRef.current = null
      }
    }
  }, [showRectifiedView, calibration, opencvReady, canvasMounted])

  // Draw column dividers on rectified view
  const drawRectifiedOverlay = useCallback(() => {
    if (!showRectifiedView || !calibration) return

    const canvas = rectifiedCanvasRef.current
    if (!canvas) return

    const ctx = canvas.getContext('2d')
    if (!ctx) return

    const width = canvas.width
    const height = canvas.height

    // Draw column dividers on rectified canvas
    ctx.strokeStyle = '#00ff00'
    ctx.lineWidth = 1
    ctx.setLineDash([])

    for (const divider of calibration.columnDividers) {
      const x = divider * width
      ctx.beginPath()
      ctx.moveTo(x, 0)
      ctx.lineTo(x, height)
      ctx.stroke()
    }

    // Draw beam line
    ctx.strokeStyle = '#ffff00'
    ctx.setLineDash([3, 3])
    const beamY = height * 0.2
    ctx.beginPath()
    ctx.moveTo(0, beamY)
    ctx.lineTo(width, beamY)
    ctx.stroke()
  }, [showRectifiedView, calibration])

  // Draw overlay after each rectification frame
  useEffect(() => {
    if (!showRectifiedView || !calibration || !opencvReady || !canvasMounted) return

    // Set up a loop to draw overlay after rectification
    let running = true
    const overlayLoop = () => {
      if (!running) return
      drawRectifiedOverlay()
      requestAnimationFrame(overlayLoop)
    }
    // Delay slightly to let rectification happen first
    const timeoutId = setTimeout(() => {
      overlayLoop()
    }, 100)

    return () => {
      running = false
      clearTimeout(timeoutId)
    }
  }, [showRectifiedView, calibration, opencvReady, canvasMounted, drawRectifiedOverlay])

  if (!videoStream) {
    return (
      <div
        data-component="vision-camera-feed"
        data-status={isLoading ? 'loading' : 'no-stream'}
        className={css({
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center',
          justifyContent: 'center',
          gap: 2,
          bg: 'gray.900',
          color: 'gray.400',
          borderRadius: 'lg',
          aspectRatio: '4/3',
          fontSize: 'sm',
        })}
      >
        {isLoading ? (
          <>
            <div
              className={css({
                width: '24px',
                height: '24px',
                border: '2px solid',
                borderColor: 'gray.600',
                borderTopColor: 'blue.400',
                borderRadius: 'full',
              })}
              style={{ animation: 'spin 1s linear infinite' }}
            />
            <style>{`@keyframes spin { to { transform: rotate(360deg); } }`}</style>
            <span>Requesting camera...</span>
          </>
        ) : (
          'No camera feed'
        )}
      </div>
    )
  }

  // Show loading indicator while keeping video element mounted (so stream stays attached)
  const isLoadingOpenCV = showRectifiedView && !opencvReady

  return (
    <div
      ref={containerRef}
      data-component="vision-camera-feed"
      data-status={showRectifiedView ? 'rectified' : 'active'}
      className={css({
        position: 'relative',
        borderRadius: 'lg',
        overflow: 'hidden',
        bg: 'black',
        height: '100%',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
      })}
    >
      {/* Video element - always mounted (may be hidden when showing rectified view) */}
      <video
        ref={internalVideoRef}
        autoPlay
        playsInline
        muted
        className={css({
          width: '100%',
          height: '100%',
          objectFit: 'contain',
          display: showRectifiedView && !isLoadingOpenCV ? 'none' : 'block',
        })}
      />

      {/* Loading OpenCV indicator (overlays video) */}
      {isLoadingOpenCV && (
        <div
          data-element="opencv-loading-overlay"
          className={css({
            position: 'absolute',
            inset: 0,
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            justifyContent: 'center',
            gap: 2,
            bg: 'rgba(0, 0, 0, 0.7)',
            color: 'gray.400',
            fontSize: 'sm',
          })}
        >
          <div
            className={css({
              width: '24px',
              height: '24px',
              border: '2px solid',
              borderColor: 'gray.600',
              borderTopColor: 'blue.400',
              borderRadius: 'full',
            })}
            style={{ animation: 'spin 1s linear infinite' }}
          />
          <style>{`@keyframes spin { to { transform: rotate(360deg); } }`}</style>
          <span>Loading vision processing...</span>
        </div>
      )}

      {/* Rectified view canvas */}
      {showRectifiedView && !isLoadingOpenCV && (
        <canvas
          ref={handleRectifiedCanvasRef}
          data-element="rectified-canvas"
          className={css({
            width: '100%',
            height: '100%',
            objectFit: 'contain',
            display: 'block',
          })}
        />
      )}

      {/* Calibration grid overlay (only when not in rectified mode) */}
      {/* Uses CalibrationQuadEditor for visual consistency with desktop, but non-interactive */}
      {showCalibrationGrid &&
        !showRectifiedView &&
        calibration &&
        videoDimensions.width > 0 &&
        containerDimensions.width > 0 && (
          <div
            data-element="quad-editor-wrapper"
            style={{
              pointerEvents: 'none',
              position: 'absolute',
              top: 0,
              left: 0,
              right: 0,
              bottom: 0,
            }}
          >
            <CalibrationQuadEditor
              columnCount={calibration.columnCount}
              corners={getCornersFromCalibration(calibration)}
              columnDividers={calibration.columnDividers}
              videoWidth={videoDimensions.width}
              videoHeight={videoDimensions.height}
              containerWidth={containerDimensions.width}
              containerHeight={containerDimensions.height}
              onCornersChange={() => {}}
              onDividersChange={() => {}}
            />
          </div>
        )}

      {/* Children (e.g., CalibrationOverlay during calibration) */}
      {children}
    </div>
  )
}

export default VisionCameraFeed