diff --git a/lib/src/isolates/child.dart b/lib/src/isolates/child.dart index 7b40f85..73d45b5 100644 --- a/lib/src/isolates/child.dart +++ b/lib/src/isolates/child.dart @@ -5,12 +5,13 @@ import "package:typed_isolate/typed_isolate.dart"; import "package:opencv_ffi/opencv_ffi.dart"; import "package:video/video.dart"; +import "fps_reporter.dart"; /// The maximum size of a UDP packet, in bytes (minus a few to be safe). const maxPacketLength = 60000; /// A child isolate that manages a single camera and streams frames from it. -abstract class CameraIsolate extends IsolateChild { +abstract class CameraIsolate extends IsolateChild with FpsReporter { /// Holds the current details of the camera. final CameraDetails details; /// A constructor with initial details. @@ -20,10 +21,6 @@ abstract class CameraIsolate extends IsolateChild Timer? statusTimer; /// A timer to read from the camera at an FPS given by [details]. Timer? frameTimer; - /// A timer to log out the [fpsCount] every 5 seconds using [sendLog]. - Timer? fpsTimer; - /// Records how many FPS this camera is actually running at. - int fpsCount = 0; /// Whether the camera is currently reading a frame. bool isReadingFrame = false; @@ -38,6 +35,7 @@ abstract class CameraIsolate extends IsolateChild /// /// Note: it is important to _not_ log this message directly in _this_ isolate, as it will /// not be configurable by the parent isolate and will not be sent to the Dashboard. + @override void sendLog(LogLevel level, String message) => send(LogPayload(level: level, message: message)); @override @@ -106,10 +104,7 @@ abstract class CameraIsolate extends IsolateChild sendLog(LogLevel.debug, "Starting camera $name. Status=${details.status}"); final interval = details.fps == 0 ? Duration.zero : Duration(milliseconds: 1000 ~/ details.fps); frameTimer = Timer.periodic(interval, _frameCallback); - fpsTimer = Timer.periodic(const Duration(seconds: 5), (_) { - sendLog(LogLevel.trace, "Camera $name sent ${fpsCount ~/ 5} frames"); - fpsCount = 0; - }); + startFps(); } Future _frameCallback(Timer timer) async { @@ -124,6 +119,6 @@ abstract class CameraIsolate extends IsolateChild sendLog(LogLevel.debug, "Stopping camera $name"); disposeCamera(); frameTimer?.cancel(); - fpsTimer?.cancel(); + stopFps(); } } diff --git a/lib/src/isolates/fps_reporter.dart b/lib/src/isolates/fps_reporter.dart new file mode 100644 index 0000000..ff9b48b --- /dev/null +++ b/lib/src/isolates/fps_reporter.dart @@ -0,0 +1,29 @@ +import "dart:async"; + +import "package:burt_network/burt_network.dart"; + +mixin FpsReporter { + CameraName get name; + void sendLog(Level level, String message); + + Timer? _fpsTimer; + + /// How many FPS this camera is actually running at. + int _fpsCount = 0; + + void startFps() { + _fpsTimer = Timer.periodic(const Duration(seconds: 5), _printFps); + } + + void stopFps() { + _fpsTimer?.cancel(); + + } + + void recordFrame() => _fpsCount++; + + void _printFps(Timer timer) { + sendLog(LogLevel.trace, "Camera $name sent ${_fpsCount ~/ 5} frames"); + _fpsCount = 0; + } +} diff --git a/lib/src/isolates/opencv.dart b/lib/src/isolates/opencv.dart index fe99326..ec0295a 100644 --- a/lib/src/isolates/opencv.dart +++ b/lib/src/isolates/opencv.dart @@ -59,6 +59,6 @@ class OpenCVCameraIsolate extends CameraIsolate { return; } sendFrame(frame); - fpsCount++; + recordFrame(); } } diff --git a/lib/src/isolates/realsense.dart b/lib/src/isolates/realsense.dart index 0f1cb26..32dd26f 100644 --- a/lib/src/isolates/realsense.dart +++ b/lib/src/isolates/realsense.dart @@ -77,8 +77,8 @@ class RealSenseIsolate extends CameraIsolate { } sendRgbFrame(frames.ref.rgb_data); + recordFrame(); - fpsCount++; // send(DepthFramePayload(frames.address)); // For autonomy nativeLib.Mat_destroy(colorizedMatrix); frames.dispose();