Skip to content

feat: process frames on onNewFrame #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
.vscode/settings.json

# Flutter/Dart/Pub related
**/doc/api/
Expand Down
26 changes: 12 additions & 14 deletions lib/src/exporter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ import 'package:screen_recorder/src/frame.dart';

class Exporter {
final List<Frame> _frames = [];
Map<int, RawFrame> framesMap = {};
void onNewFrame(Frame frame) {
_frames.add(frame);
onNewFrameAsync(frame, _frames.length - 1);
}

Future<void> onNewFrameAsync(Frame frame, int index) async {
final bytes = await frame.image.toByteData(format: ui.ImageByteFormat.png);
if (bytes != null) {
framesMap[index] = RawFrame(16, bytes);
} else {
print('Skipped frame while enconding');
}
}

void clear() {
Expand All @@ -18,20 +29,7 @@ class Exporter {
bool get hasFrames => _frames.isNotEmpty;

Future<List<RawFrame>?> exportFrames() async {
if (_frames.isEmpty) {
return null;
}
final bytesImages = <RawFrame>[];
for (final frame in _frames) {
final bytesImage =
await frame.image.toByteData(format: ui.ImageByteFormat.png);
if (bytesImage != null) {
bytesImages.add(RawFrame(16, bytesImage));
} else {
print('Skipped frame while enconding');
}
}
return bytesImages;
return framesMap.values.toList();
}

Future<List<int>?> exportGif() async {
Expand Down
7 changes: 1 addition & 6 deletions test/screen_recorder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:screen_recorder/screen_recorder.dart';

class CustomExporter extends Exporter {
@override
Future<List<int>?> export() {
throw UnimplementedError();
}

@override
void onNewFrame(Frame frame) {}
}
Expand All @@ -20,6 +15,6 @@ void main() {
final exporter = CustomExporter();
final scrennRecorder = ScreenRecorderController(exporter: exporter);

expect(scrennRecorder._exporter, equals(exporter));
expect(scrennRecorder.exporter, equals(exporter));
});
}