startProcessing method

Future<void> startProcessing(
  1. MediaStream stream, {
  2. int fps = 15,
})

Start processing frames from the given media stream

This mirrors React's approach:

  1. Get camera stream
  2. Start frame capture loop
  3. Run segmentation on each frame
  4. Call onFrameProcessed with results

stream - MediaStream from camera fps - Target frames per second (default: 15, max: 10 for stability)

Implementation

Future<void> startProcessing(MediaStream stream, {int fps = 15}) async {
  if (!_isInitialized) {
    throw StateError(
        'FrameProcessor not initialized. Call initialize() first.');
  }

  if (_isProcessing) {
    debugPrint('FrameProcessor: Already processing, stopping first');
    await stopProcessing();
  }

  _sourceStream = stream;
  // Limit FPS to avoid overwhelming ImageReader buffer
  _targetFps = fps.clamp(1, 10);
  _isProcessing = true;
  _frameCounter = 0;
  _consecutiveFailures = 0;

  // Get initial dimensions from track settings
  final videoTracks = stream.getVideoTracks();
  if (videoTracks.isNotEmpty) {
    final settings = videoTracks.first.getSettings();
    _lastWidth = settings['width'] as int? ?? 640;
    _lastHeight = settings['height'] as int? ?? 480;
  }

  debugPrint(
      'FrameProcessor: Starting processing at $_targetFps FPS (${_lastWidth}x$_lastHeight)');

  // Start the frame processing loop using sequential async calls
  // This ensures we wait for each capture to complete before starting the next
  _runFrameLoop();
}