-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (62 loc) · 1.9 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
const http = require("http");
const express = require("express");
const { Server } = require("socket.io");
const bodyParser = require("body-parser");
const path = require("path");
const { execSync, spawn } = require("child_process");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
let isLive = false;
app.use(bodyParser.json());
app.use("/", express.static(path.join(__dirname, "client")));
app.use("/live", express.static(path.join(__dirname, "stream")));
app.post("/api/live_start", async (req, res) => {
const { gain, shutter, bitrate, resolution, framerate } = req.body;
const g = gain ? `--gain ${gain}` : "";
const s = shutter ? `--shutter ${shutter}` : "";
try {
spawn(
`libcamera-vid -t 0 ${resolution} --framerate ${framerate} --codec h264 --bitrate ${bitrate} ${g} ${s} -o - | ffmpeg -i - -c copy -f hls -hls_time 4 -hls_list_size 5 -hls_flags delete_segments -hls_segment_filename './stream/segment_%03d.ts' ./stream/index.m3u8`,
{
shell: true,
detached: true,
stdio: "ignore",
}
).unref(); // streaming directly to static file via ffmpeg
setTimeout(() => {
isLive = true;
io.emit("live", { event: "start" });
}, 15000);
res.sendStatus(200);
} catch (e) {
console.log(e);
res.sendStatus(500);
}
});
app.post("/api/live_stop", async (req, res) => {
try {
execSync(`sudo pkill libcamera-vid`);
execSync(`sudo rm -f stream/*`);
isLive = false;
io.emit("live", { event: "stop" });
res.sendStatus(200);
} catch (e) {
console.log(e);
res.sendStatus(500);
}
});
app.get("/api/live_status", async (req, res) => {
try {
res.json({
isLive: isLive && !!execSync(`pgrep libcamera-vid`).toString(),
});
} catch (e) {
res.json({
isLive: false,
});
}
});
server.listen(7030, () => {
console.log("HTTP listen on port 7030");
});