forked from wulkano/aperture-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
213 lines (175 loc) · 5.27 KB
/
index.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
207
208
209
210
211
212
213
"use strict";
const os = require("os");
const util = require("util");
const path = require("path");
const execa = require("execa");
const tempy = require("tempy");
const macosVersion = require("macos-version");
const fileUrl = require("file-url");
const electronUtil = require("electron-util/node");
const debuglog = util.debuglog("aperture");
// Workaround for https://github.com/electron/electron/issues/9459
const BIN = path.join(electronUtil.fixPathForAsarUnpack(__dirname), "aperture");
const supportsHevcHardwareEncoding = (() => {
if (!macosVersion.isGreaterThanOrEqualTo("10.13")) {
return false;
}
// Get the Intel Core generation, the `4` in `Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz`
// More info: https://www.intel.com/content/www/us/en/processors/processor-numbers.html
const result = /Intel.*Core.*i(?:7|5)-(\d)/.exec(os.cpus()[0].model);
// Intel Core generation 6 or higher supports HEVC hardware encoding
return result && Number(result[1]) >= 6;
})();
class Aperture {
constructor() {
macosVersion.assertGreaterThanOrEqualTo("10.12");
}
recorderTimeout = null;
recorder = null;
startRecording({
fps = 30,
cropArea = undefined,
showCursor = true,
highlightClicks = false,
screenId = 0,
audioDeviceId = undefined,
videoCodec = undefined,
scaleFactor = 1,
} = {}) {
return new Promise((resolve, reject) => {
if (this.recorder !== null) {
reject(new Error("Call `.stopRecording()` first"));
return;
}
this.tmpPath = tempy.file({ extension: "mp4" });
if (highlightClicks === true) {
showCursor = true;
}
if (typeof cropArea === "object") {
if (
typeof cropArea.x !== "number" ||
typeof cropArea.y !== "number" ||
typeof cropArea.width !== "number" ||
typeof cropArea.height !== "number"
) {
reject(new Error("Invalid `cropArea` option object"));
return;
}
}
const recorderOpts = {
destination: fileUrl(this.tmpPath),
framesPerSecond: fps,
showCursor,
highlightClicks,
screenId,
audioDeviceId,
scaleFactor,
};
if (cropArea) {
recorderOpts.cropRect = [
[cropArea.x, cropArea.y],
[cropArea.width, cropArea.height],
];
}
if (videoCodec) {
const codecMap = new Map([
["h264", "avc1"],
["hevc", "hvc1"],
["proRes422", "apcn"],
["proRes4444", "ap4h"],
]);
if (!supportsHevcHardwareEncoding) {
codecMap.delete("hevc");
}
if (!codecMap.has(videoCodec)) {
throw new Error(`Unsupported video codec specified: ${videoCodec}`);
}
recorderOpts.videoCodec = codecMap.get(videoCodec);
}
console.log(':: Aperture Options :: ')
console.log(recorderOpts)
this.recorder = execa(BIN, [JSON.stringify(recorderOpts)]);
this.recorderTimeout = setTimeout(() => {
// `.stopRecording()` was called already
if (this.recorder === null) {
return;
}
const err = new Error("Could not start recording within 5 seconds");
err.code = "RECORDER_TIMEOUT";
this.recorder.kill();
this.recorder = null;
reject(err);
}, 10000);
this.recorder.catch((error) => {
clearTimeout(this.recorderTimeout);
this.recorder = null;
reject(error);
});
this.recorder.stdout.setEncoding("utf8");
this.recorder.stdout.on("data", (data) => {
debuglog(data);
if (data.trim() === "R") {
// `R` is printed by Swift when the recording **actually** starts
clearTimeout(this.recorderTimeout);
resolve(this.tmpPath);
}
});
});
}
async stopRecording() {
console.log("APERTURE :: Stopping Recording ::");
if (this.recorder === null) {
throw new Error("Call `.startRecording()` first");
}
this.recorder.kill();
await this.recorder;
this.recorder = null;
return this.tmpPath;
}
async cancelRecording() {
if (this.recoder === null) {
console.log("APERTURE :: Nothing to cancel", this.recorder);
return;
}
console.log("APERTURE :: Cancelling Recording ::");
// Duplicate check because, sentry keeps reporting errors
if (this.recorder) {
this.recorder.kill();
this.recorderTimeout && clearTimeout(this.recorderTimeout);
await this.recorder
}
this.recorder = null;
return;
}
}
module.exports = () => new Aperture();
module.exports.screens = async () => {
const stderr = await execa.stderr(BIN, ["list-screens"]);
try {
return JSON.parse(stderr);
} catch (_) {
return stderr;
}
};
module.exports.audioDevices = async () => {
const stderr = await execa.stderr(BIN, ["list-audio-devices"]);
try {
return JSON.parse(stderr);
} catch (_) {
return stderr;
}
};
Object.defineProperty(module.exports, "videoCodecs", {
get() {
const codecs = new Map([
["h264", "H264"],
["hevc", "HEVC"],
["proRes422", "Apple ProRes 422"],
["proRes4444", "Apple ProRes 4444"],
]);
if (!supportsHevcHardwareEncoding) {
codecs.delete("hevc");
}
return codecs;
},
});