-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
342 lines (292 loc) · 9.45 KB
/
server.js
File metadata and controls
342 lines (292 loc) · 9.45 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const auth = require('basic-auth');
const dotenv = require('dotenv');
const { spawn, exec } = require('child_process');
const fs = require('fs');
const path = require('path');
dotenv.config();
const app = express();
const server = http.createServer(app);
const io = new Server(server);
// Paths to local scrcpy binaries
const SCRCPY_PATH = path.join(__dirname, 'scrcpy/build-auto/app/scrcpy');
const SCRCPY_SERVER_PATH = path.join(__dirname, 'scrcpy/build-auto/server/scrcpy-server');
// Basic Auth Middleware
const basicAuth = (req, res, next) => {
const user = auth(req);
const envUser = process.env.BASIC_AUTH_USER;
const envPass = process.env.BASIC_AUTH_PASS;
if (!user || user.name !== envUser || user.pass !== envPass) {
res.set('WWW-Authenticate', 'Basic realm="Scrcpy Web"');
return res.status(401).send('Authentication required.');
}
next();
};
app.use(basicAuth);
app.use(express.static('public'));
// Serve jmuxer from node_modules
app.use('/libs', express.static(path.join(__dirname, 'node_modules')));
// Helper: Get connected devices from ADB
const getAdbDevices = () => {
return new Promise((resolve, reject) => {
exec('adb devices', (err, stdout) => {
if (err) {
console.error("Error running adb devices:", err);
return resolve([]);
}
const devices = [];
const lines = stdout.split('\n');
// Skip first line "List of devices attached"
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
const parts = line.split(/\s+/);
if (parts.length >= 2 && parts[1] === 'device') {
devices.push({
serial: parts[0],
name: `Device ${parts[0].slice(-4)}` // Default name
});
}
}
resolve(devices);
});
});
};
// Helper: Get phones (from file or auto-fill)
const getPhones = async () => {
const filePath = path.join(__dirname, 'phones.json');
let devices = [];
if (fs.existsSync(filePath)) {
try {
const data = fs.readFileSync(filePath, 'utf8');
devices = JSON.parse(data);
} catch (e) {
console.error("Error reading phones.json:", e);
}
}
// File doesn't exist or failed to read (or empty), try auto-fill
if (devices.length === 0) {
devices = await getAdbDevices();
if (devices.length > 0) {
try {
fs.writeFileSync(filePath, JSON.stringify(devices, null, '\t'));
console.log("Created phones.json with detected devices.");
} catch (e) {
console.error("Error creating phones.json:", e);
}
}
}
// Always ensure Test Device is present
if (!devices.find(d => d.serial === 'test-device')) {
devices.push({ serial: 'test-device', name: 'Test Pattern (Source)' });
}
return devices;
};
// API to get list of phones
app.get('/api/phones', async (req, res) => {
const phones = await getPhones();
res.json(phones);
});
// Socket.io connection
io.on('connection', (socket) => {
console.log('Client connected');
let streamProcess = null;
let currentResolution = { width: 0, height: 0 };
socket.on('start-stream', async (serial, options = {}) => {
if (streamProcess) {
console.log('Killing previous stream');
streamProcess.kill('SIGTERM');
streamProcess = null;
}
if (!serial) return;
console.log(`Starting stream for ${serial} with options:`, options);
// Test Stream Logic
if (serial === 'test-device') {
currentResolution = { width: 1280, height: 720 };
console.log("Starting Test Stream (ffmpeg)");
const args = [
'-re',
'-i', 'test.h264',
'-c', 'copy',
'-f', 'h264',
'-'
];
streamProcess = spawn('ffmpeg', args);
streamProcess.stdout.on('data', (data) => {
socket.emit('video-data', data);
});
streamProcess.on('close', (code) => {
console.log(`Test stream exited with code ${code}`);
socket.emit('stream-stopped', { code });
});
return;
}
// Auto-wake the device
exec(`adb -s ${serial} shell input keyevent 224`);
// Fetch device resolution (Promise wrapper)
const fetchResolution = () => new Promise((resolve) => {
exec(`adb -s ${serial} shell wm size`, (err, stdout) => {
if (!err && stdout) {
const match = stdout.match(/Physical size: (\d+)x(\d+)/);
if (match) {
resolve({ width: parseInt(match[1]), height: parseInt(match[2]) });
return;
}
}
resolve(null);
});
});
const resolution = await fetchResolution();
if (resolution) {
currentResolution = resolution;
console.log(`Device ${serial} resolution: ${currentResolution.width}x${currentResolution.height}`);
}
try {
// Use local 'scrcpy' -> FIFO -> 'ffmpeg' pipeline
// This avoids stdout pollution from scrcpy/adb which corrupts the stream if piped directly
const bitrate = options.bitrate || 2000000;
const maxSize = options.maxSize || 0;
// Create FIFO with unique ID to avoid collisions
const sessionId = Date.now().toString().slice(-6);
const fifoPath = path.join('/tmp', `scrcpy_${serial}_${sessionId}.mkv`);
try {
if (fs.existsSync(fifoPath)) fs.unlinkSync(fifoPath);
require('child_process').execSync(`mkfifo ${fifoPath}`);
} catch (e) {
console.error(`Failed to create FIFO:`, e);
return;
}
const scrcpyArgs = [
'-s', serial,
'--no-audio',
'--no-window',
`--record=${fifoPath}`,
'--record-format=mkv',
'--video-bit-rate', String(bitrate),
'--video-codec-options=profile=1' // Force Baseline profile for browser compatibility
];
if (maxSize > 0) {
scrcpyArgs.push('--max-size', String(maxSize));
}
console.log(`Spawning scrcpy at ${SCRCPY_PATH} with args: ${scrcpyArgs.join(' ')}`);
const scrcpyProcess = spawn(SCRCPY_PATH, scrcpyArgs, {
env: {
...process.env,
SCRCPY_SERVER_PATH: SCRCPY_SERVER_PATH,
XDG_RUNTIME_DIR: process.env.XDG_RUNTIME_DIR || '/tmp'
}
});
const ffmpegArgs = [
'-i', fifoPath,
'-c:v', 'copy',
'-bsf:v', 'h264_mp4toannexb', // Convert to Annex B for jmuxer
'-f', 'h264',
'-'
];
console.log(`Spawning ffmpeg pipeline...`);
const ffmpegProcess = spawn('ffmpeg', ffmpegArgs);
// Read ffmpeg stdout -> send to socket
ffmpegProcess.stdout.on('data', (data) => {
socket.emit('video-data', data);
});
// Error handling
scrcpyProcess.stderr.on('data', (data) => {
console.error(`[scrcpy stderr]:`, data.toString().trim());
});
// Cleanup function
const cleanup = () => {
console.log(`Cleaning up stream processes...`);
try {
scrcpyProcess.kill('SIGTERM');
ffmpegProcess.kill('SIGTERM');
} catch (e) { /* ignore */ }
// Remove FIFO
try {
if (fs.existsSync(fifoPath)) fs.unlinkSync(fifoPath);
} catch (e) { console.error(`Error removing FIFO:`, e); }
};
scrcpyProcess.on('close', (code) => {
console.log(`scrcpy process exited with code ${code}`);
cleanup();
socket.emit('stream-stopped', { code });
});
ffmpegProcess.on('close', (code) => {
console.log(`ffmpeg process exited with code ${code}`);
if (code !== 0 && code !== null) cleanup();
});
streamProcess = {
kill: (signal) => {
cleanup();
}
};
} catch (e) {
console.error("Error spawning stream:", e);
}
});
socket.on('control-key', (data) => {
const { serial, keycode } = data;
if (serial && keycode) {
console.log(`Sending key ${keycode} to ${serial}`);
exec(`adb -s ${serial} shell input keyevent ${keycode}`);
}
});
socket.on('touch-tap', (data) => {
const { serial, x, y } = data; // x, y are normalized 0.0-1.0
if (serial && currentResolution.width > 0) {
const realX = Math.round(x * currentResolution.width);
const realY = Math.round(y * currentResolution.height);
exec(`adb -s ${serial} shell input tap ${realX} ${realY}`);
}
});
socket.on('control-scroll', (data) => {
const { serial, direction } = data;
if (serial) {
const width = currentResolution.width || 1080;
const height = currentResolution.height || 1920;
const x = Math.round(width / 2);
const yCenter = Math.round(height / 2);
const scrollDist = Math.round(height * 0.2); // 20% screen height
let yStart, yEnd;
if (direction === 'down') {
// Scroll down -> Swipe Up (drag content up)
yStart = yCenter + Math.round(scrollDist / 2);
yEnd = yCenter - Math.round(scrollDist / 2);
} else {
// Scroll up -> Swipe Down (drag content down)
yStart = yCenter - Math.round(scrollDist / 2);
yEnd = yCenter + Math.round(scrollDist / 2);
}
// Fast swipe for scroll (100ms)
exec(`adb -s ${serial} shell input swipe ${x} ${yStart} ${x} ${yEnd} 100`);
}
});
socket.on('control-swipe-up', (data) => {
const { serial } = data;
if (serial) {
const width = currentResolution.width || 1080;
const height = currentResolution.height || 1920;
const x = Math.round(width / 2);
// Taller swipe: 90% -> 10%
const yStart = Math.round(height * 0.9);
const yEnd = Math.round(height * 0.1);
console.log(`Swiping up on ${serial}: ${x},${yStart} -> ${x},${yEnd}`);
// Slower duration: 1000ms
exec(`adb -s ${serial} shell input swipe ${x} ${yStart} ${x} ${yEnd} 1000`);
}
});
socket.on('disconnect', () => {
console.log('Client disconnected');
if (streamProcess) {
streamProcess.kill('SIGTERM');
}
});
});
const PORT = process.env.PORT || 8558;
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
getAdbDevices().then(devices => {
console.log("Currently connected ADB devices:", devices.map(d => d.serial).join(', '));
});
});