/
aperture.js
206 lines (163 loc) · 4.88 KB
/
aperture.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
const {dialog} = require('electron');
const desktopIcons = require('hide-desktop-icons');
const dnd = require('@sindresorhus/do-not-disturb');
const createAperture = require('aperture');
const {openEditorWindow} = require('../editor');
const {closePrefsWindow} = require('../preferences');
const {setRecordingTray, disableTray, resetTray} = require('../tray');
const {disableCroppers, setRecordingCroppers, closeAllCroppers} = require('../cropper');
const {setCropperShortcutAction} = require('../global-accelerators');
// eslint-disable-next-line no-unused-vars
const {convertToH264} = require('../utils/encoding');
const {hasMicrophoneAccess} = require('./system-permissions');
const settings = require('./settings');
const {track} = require('./analytics');
const aperture = createAperture();
const {audioDevices, videoCodecs} = createAperture;
// eslint-disable-next-line no-unused-vars
const recordHevc = videoCodecs.has('hevc');
let wasDoNotDisturbAlreadyEnabled;
let lastUsedSettings;
let past;
const cleanup = () => {
const {
hideDesktopIcons,
doNotDisturb
} = lastUsedSettings;
closeAllCroppers();
resetTray();
if (hideDesktopIcons) {
desktopIcons.show();
}
if (doNotDisturb && !wasDoNotDisturbAlreadyEnabled) {
dnd.disable();
}
setCropperShortcutAction();
};
const startRecording = async options => {
if (past) {
return;
}
past = Date.now();
closePrefsWindow();
disableTray();
disableCroppers();
const {cropperBounds, screenBounds, displayId} = options;
cropperBounds.y = screenBounds.height - (cropperBounds.y + cropperBounds.height);
const {
record60fps,
showCursor,
highlightClicks,
recordAudio,
audioInputDeviceId,
hideDesktopIcons,
doNotDisturb
} = settings.store;
const apertureOpts = {
fps: record60fps ? 60 : 30,
cropArea: cropperBounds,
showCursor,
highlightClicks,
screenId: displayId
};
lastUsedSettings = {
recordedFps: apertureOpts.fps,
hideDesktopIcons,
doNotDisturb
};
if (recordAudio === true) {
// In case for some reason the default audio device is not set
// use the first available device for recording
if (audioInputDeviceId) {
apertureOpts.audioDeviceId = audioInputDeviceId;
} else {
const [defaultAudioDevice] = await audioDevices();
apertureOpts.audioDeviceId = defaultAudioDevice && defaultAudioDevice.id;
}
}
// TODO: figure out how to correctly process hevc videos with ffmpeg
// if (recordHevc) {
// apertureOpts.videoCodec = 'hevc';
// }
console.log(`Collected settings after ${(Date.now() - past) / 1000}s`);
if (hideDesktopIcons) {
await desktopIcons.hide();
}
console.log(`Hide desktop icons after ${(Date.now() - past) / 1000}s`);
if (doNotDisturb) {
wasDoNotDisturbAlreadyEnabled = await dnd.isEnabled();
if (!wasDoNotDisturbAlreadyEnabled) {
dnd.enable();
}
}
console.log(`Took care of DND after ${(Date.now() - past) / 1000}s`);
try {
await aperture.startRecording(apertureOpts);
} catch (error) {
track('recording/stopped/error');
dialog.showErrorBox('Recording error', error.message);
past = null;
return;
}
const startTime = (Date.now() - past) / 1000;
if (startTime > 3) {
track(`recording/started/${startTime}`);
} else {
track('recording/started');
}
console.log(`Started recording after ${startTime}s`);
setRecordingCroppers();
setRecordingTray(stopRecording);
setCropperShortcutAction(stopRecording);
past = Date.now();
// Track aperture errors after recording has started, to avoid kap freezing if something goes wrong
aperture.recorder.catch(error => {
// Make sure it doesn't catch the error of ending the recording
if (past) {
track('recording/stopped/error');
dialog.showErrorBox('Recording error', error.message);
past = null;
cleanup();
}
});
};
const stopRecording = async () => {
// Ensure we only stop recording once
if (!past) {
return;
}
console.log(`Stopped recording after ${(Date.now() - past) / 1000}s`);
past = null;
let filePath;
try {
filePath = await aperture.stopRecording();
} catch (error) {
track('recording/stopped/error');
dialog.showErrorBox('Recording error', error.message);
cleanup();
return;
}
const {recordedFps} = lastUsedSettings;
try {
cleanup();
} finally {
track('editor/opened/recording');
// TODO: bring this back when we figure out how to convert hevc files
// if (recordHevc) {
// openEditorWindow(await convertToH264(filePath), {recordedFps, isNewRecording: true, originalFilePath: filePath});
// } else {
openEditorWindow(filePath, {recordedFps, isNewRecording: true});
// }
}
};
module.exports = {
startRecording,
stopRecording,
getAudioDevices: () => {
if (hasMicrophoneAccess()) {
return audioDevices();
}
return [];
}
};