getFrames method Null safety
- {required int frames,
- required int frameDelay,
- required Stream dataStream,
- required StreamController controller}
receive a data stream from the camera parse the individual JPEG frames add each stream to the controller stream
Implementation
static void getFrames(
{required int frames,
required int frameDelay,
required Stream dataStream,
required StreamController controller}) {
List<int> buffer = [];
int startIndex = -1;
int endIndex = -1;
int frameCount = 0;
// frame delay useful for testing SC2. milliseconds
Stopwatch frameTimer = Stopwatch();
frameTimer.start();
StreamSubscription? subscription;
subscription = dataStream.listen((chunkOfStream) {
if (frameCount > frames && frames != -1 && keepRunning) {
if (subscription != null) {
subscription.cancel();
controller.close();
}
}
if (keepRunning) {
buffer.addAll(chunkOfStream);
// print('current chunk of stream is ${chunkOfStream.length} bytes long');
for (var i = 1; i < chunkOfStream.length; i++) {
if (chunkOfStream[i - 1] == 0xff && chunkOfStream[i] == 0xd8) {
startIndex = i - 1;
}
if (chunkOfStream[i - 1] == 0xff && chunkOfStream[i] == 0xd9) {
endIndex = buffer.length;
}
if (startIndex != -1 && endIndex != -1) {
var frame = buffer.sublist(startIndex, endIndex);
if (frameTimer.elapsedMilliseconds > frameDelay) {
if (frameCount > 0) {
controller.add(frame);
print('framecount $frameCount');
frameTimer.reset();
}
frameCount++;
}
// print(frame);
startIndex = -1;
endIndex = -1;
buffer = [];
}
}
} // end keepRunning
});
}