-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstopRecording.js
More file actions
93 lines (85 loc) · 2.88 KB
/
stopRecording.js
File metadata and controls
93 lines (85 loc) · 2.88 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
const { validate } = require("doc-detective-common");
const { log } = require("../utils");
const { exec } = require("child_process");
const path = require("path");
const fs = require("fs");
const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
exports.stopRecording = stopRecording;
async function stopRecording({ config, step, driver }) {
let result = {
status: "PASS",
description: "Stopped recording.",
};
// Validate step payload
const isValidStep = validate({ schemaKey: "step_v3", object: step });
if (!isValidStep.valid) {
result.status = "FAIL";
result.description = `Invalid step definition: ${isValidStep.errors}`;
return result;
}
// Accept coerced and defaulted values
step = isValidStep.object;
// Skip if recording is not started
if (!config.recording) {
result.status = "SKIPPED";
result.description = `Recording isn't started.`;
return result;
}
try {
if (config.recording.type === "MediaRecorder") {
// MediaRecorder
// Switch to recording tab
await driver.switchToWindow(config.recording.tab);
// Verify the recorder was properly initialized
const recorderExists = await driver.execute(() => {
return typeof window.recorder !== "undefined" && window.recorder !== null;
});
if (!recorderExists) {
result.status = "FAIL";
result.description =
"Recording was not properly started. The recorder object doesn't exist in the browser context.";
await driver.closeWindow();
config.recording = null;
return result;
}
// Stop recording
await driver.execute(() => {
window.recorder.stop();
});
// Wait for file to be in download path
while (!fs.existsSync(config.recording.downloadPath)) {
await new Promise((r) => setTimeout(r, 1000));
}
// Close recording tab
await driver.closeWindow();
// Convert the file into the target format/location
const targetPath = `${config.recording.targetPath}`;
const downloadPath = `${config.recording.downloadPath}`;
const endMessage = `Finished processing file: ${config.recording.targetPath}`;
const ffmpeg = exec(
`${ffmpegPath} -y -i ${downloadPath} -pix_fmt yuv420p ${
path.extname(targetPath) === ".gif"
? `-vf scale=iw:-1:flags=lanczos`
: ""
} ${targetPath}`
).on("close", () => {
if (targetPath !== downloadPath) {
// Delete the downloaded file
fs.unlinkSync(downloadPath);
log(config, "debug", endMessage);
}
});
config.recording = null;
} else {
// FFMPEG
// config.recording.stdin.write("q");
}
} catch (error) {
// Couldn't stop recording
result.status = "FAIL";
result.description = `Couldn't stop recording. ${error}`;
return result;
}
// PASS
return result;
}