-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathscreen_recorder.dart
More file actions
139 lines (119 loc) · 3.95 KB
/
screen_recorder.dart
File metadata and controls
139 lines (119 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'dart:ui' as ui show Image;
import 'package:screen_recorder/src/exporter.dart';
import 'package:screen_recorder/src/frame.dart';
import 'package:screen_recorder/src/gif/gif_exporter.dart';
class ScreenRecorderController {
ScreenRecorderController({
Exporter? exporter,
this.pixelRatio = 0.5,
this.skipFramesBetweenCaptures = 2,
SchedulerBinding? binding,
}) : _containerKey = GlobalKey(),
_binding = binding ?? SchedulerBinding.instance,
exporter = exporter ?? GifExporter();
final GlobalKey _containerKey;
final SchedulerBinding _binding;
final Exporter exporter;
/// The pixelRatio describes the scale between the logical pixels and the size
/// of the output image. Specifying 1.0 will give you a 1:1 mapping between
/// logical pixels and the output pixels in the image. The default is a pixel
/// ration of 3 and a value below 1 is not recommended.
///
/// See [RenderRepaintBoundary](https://api.flutter.dev/flutter/rendering/RenderRepaintBoundary/toImage.html)
/// for the underlying implementation.
final double pixelRatio;
/// Describes how many frames are skipped between caputerd frames.
/// For example if it's `skipFramesBetweenCaptures = 2` screen_recorder
/// captures a frame, skips the next two frames and then captures the next
/// frame again.
final int skipFramesBetweenCaptures;
int skipped = 0;
bool _record = false;
void start() {
// only start a video, if no recording is in progress
if (_record == true) {
return;
}
_record = true;
_binding.addPostFrameCallback(postFrameCallback);
}
void stop() {
_record = false;
}
void postFrameCallback(Duration timestamp) async {
if (_record == false) {
return;
}
if (skipped > 0) {
// count down frames which should be skipped
skipped = skipped - 1;
// add a new PostFrameCallback to know about the next frame
_binding.addPostFrameCallback(postFrameCallback);
// but we do nothing, because we skip this frame
return;
}
if (skipped == 0) {
// reset skipped frame counter
skipped = skipped + skipFramesBetweenCaptures;
}
try {
final image = capture();
if (image == null) {
print('capture returned null');
return;
}
exporter.onNewFrame(Frame(timestamp, image));
} catch (e) {
print(e.toString());
}
_binding.addPostFrameCallback(postFrameCallback);
}
ui.Image? capture() {
final renderObject = _containerKey.currentContext!.findRenderObject()
as RenderRepaintBoundary;
return renderObject.toImageSync(pixelRatio: pixelRatio);
}
Future<List<int>?> export() => exporter.export();
}
class ScreenRecorder extends StatelessWidget {
ScreenRecorder({
Key? key,
required this.child,
required this.controller,
required this.width,
required this.height,
this.background = Colors.white,
}) : assert(background.alpha == 255,
'background color is not allowed to be transparent'),
super(key: key);
/// The child which should be recorded.
final Widget child;
/// This controller starts and stops the recording.
final ScreenRecorderController controller;
/// Width of the recording.
/// This should not change during recording as it could lead to
/// undefined behavior.
final double width;
/// Height of the recording
/// This should not change during recording as it could lead to
/// undefined behavior.
final double height;
/// The background color of the recording.
/// Transparency is currently not supported.
final Color background;
@override
Widget build(BuildContext context) {
return RepaintBoundary(
key: controller._containerKey,
child: Container(
width: width,
height: height,
color: background,
child: child,
),
);
}
}