-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.ts
More file actions
162 lines (142 loc) · 4.34 KB
/
server.ts
File metadata and controls
162 lines (142 loc) · 4.34 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import express from "express";
import { WebSocketServer } from "ws";
import { spawn, ChildProcess } from "child_process";
import cors from "cors";
import { IncomingMessage, ServerResponse } from "http";
const app = express();
const HTTP_PORT = 3000;
const WS_PORT = 3001;
app.use(cors());
app.use(express.json());
const wss = new WebSocketServer({ port: WS_PORT });
console.log(`WebSocket server listening on ws://0.0.0.0:${WS_PORT}`);
let ffmpeg: ChildProcess | null = null;
let httpClients: ServerResponse[] = [];
function startFFmpeg() {
if (ffmpeg) return;
console.log("Starting ffmpeg process...");
ffmpeg = spawn("ffmpeg", [
"-f", "image2pipe",
"-r", "10",
"-i", "pipe:0",
"-vf", "scale=640:-2",
"-q:v", "5",
"-f", "mjpeg",
"pipe:1"
], { stdio: ["pipe", "pipe", "inherit"] });
ffmpeg.stdout?.on("data", (chunk: Buffer) => {
console.log(`ffmpeg output: ${chunk.length} bytes to ${httpClients.length} clients`);
httpClients.forEach(res => {
try {
res.write(chunk);
} catch (e) {
console.error("Error writing to client:", e);
}
});
});
ffmpeg.on("exit", (code) => {
console.log(`ffmpeg exited with code ${code}`);
ffmpeg = null;
});
ffmpeg.on("error", (err) => {
console.error("ffmpeg error:", err);
ffmpeg = null;
});
console.log("ffmpeg started successfully");
}
function stopFFmpeg() {
if (ffmpeg) {
console.log("Stopping ffmpeg...");
try {
if (ffmpeg.stdin && !ffmpeg.stdin.destroyed) {
ffmpeg.stdin.end();
}
ffmpeg.kill('SIGTERM');
} catch (err: any) {
console.log("Error stopping ffmpeg (this is normal):", err?.message || err);
}
ffmpeg = null;
}
}
// WebSocket connection handler
wss.on("connection", (ws) => {
console.log("Mobile client connected via WebSocket");
ws.on("message", (msg: Buffer) => {
try {
const msgStr = msg.toString();
const data = JSON.parse(msgStr);
if (data.type === "frame" && data.data) {
if (!ffmpeg) {
startFFmpeg();
}
const buffer = Buffer.from(data.data, "base64");
console.log(`Received frame: ${buffer.length} bytes`);
if (ffmpeg && ffmpeg.stdin && ffmpeg.stdin.writable && !ffmpeg.stdin.destroyed) {
try {
ffmpeg.stdin.write(buffer, (err) => {
if (err) {
console.error("Error writing to ffmpeg:", err.message);
}
});
} catch (err: any) {
console.error("Error writing frame to ffmpeg:", err?.message || err);
}
} else {
console.log("Cannot write to ffmpeg - stdin not available");
}
} else if (data.type === "stop") {
console.log("Stop signal received from client");
stopFFmpeg();
}
} catch (e) {
console.error("Error processing WebSocket message:", e);
}
});
ws.on("close", () => {
console.log("Mobile client disconnected");
});
ws.on("error", (err) => {
console.error("WebSocket error:", err);
});
});
app.get("/stream.mjpeg", (req: IncomingMessage, res: ServerResponse) => {
console.log("HTTP client connected to stream");
res.writeHead(200, {
"Content-Type": "multipart/x-mixed-replace; boundary=ffserver",
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
"Connection": "close",
"Access-Control-Allow-Origin": "*"
});
httpClients.push(res);
req.on("close", () => {
console.log("HTTP client disconnected from stream");
httpClients = httpClients.filter(r => r !== res);
});
});
app.get("/", (req, res) => {
res.json({
status: "ok",
message: "React Native Playground Backend",
streaming: ffmpeg !== null,
connectedClients: httpClients.length
});
});
app.get("/status", (req, res) => {
res.json({
ffmpegRunning: ffmpeg !== null,
httpClients: httpClients.length,
wsConnections: wss.clients.size
});
});
app.listen(HTTP_PORT, () => {
console.log(`HTTP server listening on http://0.0.0.0:${HTTP_PORT}`);
console.log(`MJPEG stream available at http://0.0.0.0:${HTTP_PORT}/stream.mjpeg`);
});
process.on("SIGINT", () => {
console.log("\nShutting down gracefully...");
stopFFmpeg();
wss.close();
process.exit(0);
});