startProcessing method
Start processing frames from the given media stream
This mirrors React's approach:
- Get camera stream
- Start frame capture loop
- Run segmentation on each frame
- 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();
}