All files / web/src/hooks usePassiveBoundaryCapture.ts

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

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                                                                                                                                                                                                                                                                                                                                                               
'use client'

import { useCallback, useRef } from 'react'
import type { QuadCorners } from '@/types/vision'
import { saveBoundarySample } from '@/lib/vision/saveBoundarySample'

/** Default capture interval: 200ms (5fps) to match phone sending rate */
const DEFAULT_CAPTURE_INTERVAL_MS = 200

/** Minimum capture interval: 200ms (5fps) - we need massive training data */
const MIN_CAPTURE_INTERVAL_MS = 200

interface PassiveBoundaryCaptureConfig {
  /** Whether passive capture is enabled (default: true) */
  enabled?: boolean
  /** Minimum interval between captures in ms (default: 200) */
  captureIntervalMs?: number
  /** Device ID for organizing captures (default: "passive-remote") */
  deviceId?: string
  /** Session ID for tracking which practice session captured the frame */
  sessionId?: string
  /** Player ID for tracking which student was practicing */
  playerId?: string
  /** Callback when capture succeeds */
  onCaptureSuccess?: () => void
  /** Callback when capture fails */
  onCaptureError?: (error: string) => void
}

interface UsePassiveBoundaryCaptureReturn {
  /**
   * Attempt to capture a boundary frame for training.
   * Call this whenever you have a frame with detected corners.
   * The hook will rate-limit captures automatically.
   *
   * @param imageData Base64 image data (JPEG or PNG, with or without data URL prefix)
   * @param corners Detected corner coordinates (in image pixel space)
   * @param frameWidth Width of the image in pixels
   * @param frameHeight Height of the image in pixels
   * @returns true if capture was attempted, false if rate-limited
   */
  maybeCapture: (
    imageData: string,
    corners: QuadCorners,
    frameWidth: number,
    frameHeight: number
  ) => boolean
  /** Number of successful captures this session */
  captureCount: number
  /** Timestamp of last capture (0 if none) */
  lastCaptureTime: number
  /** Whether a capture is currently in progress */
  isCapturing: boolean
}

/**
 * Hook for passive boundary frame capture during practice sessions.
 *
 * This hook allows automatic collection of boundary detector training data
 * when markers are detected during normal practice sessions. It rate-limits
 * captures to avoid flooding storage.
 *
 * Uses the shared saveBoundarySample utility (same as BoundaryDataCapture).
 *
 * Usage:
 * ```tsx
 * const { maybeCapture } = usePassiveBoundaryCapture({ enabled: true })
 *
 * // In your frame handler when you have detected corners:
 * if (frame.detectedCorners && frame.mode === 'raw') {
 *   maybeCapture(frame.imageData, frame.detectedCorners, width, height)
 * }
 * ```
 */
export function usePassiveBoundaryCapture(
  config: PassiveBoundaryCaptureConfig = {}
): UsePassiveBoundaryCaptureReturn {
  const {
    enabled = true,
    captureIntervalMs = DEFAULT_CAPTURE_INTERVAL_MS,
    deviceId = 'passive-remote',
    sessionId,
    playerId,
    onCaptureSuccess,
    onCaptureError,
  } = config

  // Use refs to avoid re-renders and stale closures
  const lastCaptureTimeRef = useRef(0)
  const captureCountRef = useRef(0)
  const isCapturingRef = useRef(false)

  const maybeCapture = useCallback(
    (imageData: string, corners: QuadCorners, frameWidth: number, frameHeight: number): boolean => {
      // Skip if disabled
      if (!enabled) {
        return false
      }

      // Skip if currently capturing
      if (isCapturingRef.current) {
        return false
      }

      // Rate limit: check if enough time has passed since last capture
      const now = Date.now()
      const effectiveInterval = Math.max(captureIntervalMs, MIN_CAPTURE_INTERVAL_MS)
      const timeSinceLastCapture = now - lastCaptureTimeRef.current

      if (timeSinceLastCapture < effectiveInterval) {
        return false
      }

      // Validate corners exist
      if (
        !corners?.topLeft ||
        !corners?.topRight ||
        !corners?.bottomLeft ||
        !corners?.bottomRight
      ) {
        return false
      }

      // Mark as capturing and update timestamp optimistically
      isCapturingRef.current = true
      lastCaptureTimeRef.current = now

      // Use shared saveBoundarySample utility (fire and forget, but track success/failure)
      saveBoundarySample({
        imageData,
        corners,
        frameWidth,
        frameHeight,
        deviceId,
        sessionId,
        playerId,
      })
        .then((result) => {
          if (result.success) {
            captureCountRef.current++
            console.log(
              `[PassiveBoundaryCapture] Captured frame #${captureCountRef.current} for training`
            )
            onCaptureSuccess?.()
          } else {
            console.warn('[PassiveBoundaryCapture] Capture failed:', result.error)
            onCaptureError?.(result.error || 'Unknown error')
          }
        })
        .catch((error) => {
          console.error('[PassiveBoundaryCapture] Unexpected error:', error)
          onCaptureError?.(error.message || 'Unexpected error')
        })
        .finally(() => {
          isCapturingRef.current = false
        })

      return true
    },
    [enabled, captureIntervalMs, deviceId, sessionId, playerId, onCaptureSuccess, onCaptureError]
  )

  return {
    maybeCapture,
    get captureCount() {
      return captureCountRef.current
    },
    get lastCaptureTime() {
      return lastCaptureTimeRef.current
    },
    get isCapturing() {
      return isCapturingRef.current
    },
  }
}