Skip to content

Commit

Permalink
Merge pull request #663 from SAGE-3/dev-webview
Browse files Browse the repository at this point in the history
[App] Web streaming
  • Loading branch information
renambot authored Aug 3, 2023
2 parents a2ea611 + 1cbcf83 commit 5ac7d1f
Show file tree
Hide file tree
Showing 13 changed files with 372 additions and 140,070 deletions.
139,932 changes: 0 additions & 139,932 deletions sage_sbom.json

This file was deleted.

77 changes: 30 additions & 47 deletions webstack/apps/homebase/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,64 +189,47 @@ async function startServer() {
});

// Websocket API for WebRTC
const clients: Record<string, WebSocket> = {};
const clients: Map<string, WebSocket[]> = new Map();

function emitRTC(name: string, socket: WebSocket, data: any) {
for (const k in clients) {
const sock = clients[k];
if (sock !== socket) {
sock.send(JSON.stringify({ type: name, data: data }));
}
}
// Broadcast to all clients in the room
function emitRTC(room: string, type: string, params: any) {
const msg = JSON.stringify({ type, params });
clients.get(room)?.forEach((ws) => ws.send(msg));
}
async function sendRTC(name: string, socket: WebSocket, data: any) {
socket.send(JSON.stringify({ type: name, data: data }));
}

rtcWebSocketServer.on('connection', (socket: WebSocket, request: IncomingMessage) => {
console.log('WebRTC> connection', request.url);

if (request.url) {
const parts = request.url.split('/');
const roomID = parts[parts.length - 1];
console.log('WebRTC> roomID', roomID);
}

rtcWebSocketServer.on('connection', (socket: WebSocket, _request: IncomingMessage) => {
// new message
socket.on('message', (data) => {
const datastr = data.toString();
const msg = JSON.parse(datastr);
if (msg.type === 'join') {
clients[msg.user] = socket;
emitRTC('join', socket, msg);
console.log('WebRTC> connection #', Object.keys(clients).length);
sendRTC('clients', socket, Object.keys(clients));
} else if (msg.type === 'create') {
clients[msg.user] = socket;
console.log('WebRTC> new group for', msg.app);
} else if (msg.type === 'paint') {
emitRTC('paint', socket, msg.data);
switch (msg.type) {
case 'join':
if (!clients.has(msg.params.room)) {
clients.set(msg.params.room, []);
}
clients.get(msg.params.room)?.push(socket);
break;
case 'pixels':
// broadcast to all clients in the room
emitRTC(msg.params.room, 'data', msg.params);
break;
case 'leave':
clients.get(msg.params.room)?.splice(clients.get(msg.params.room)?.indexOf(socket) || 0, 1);
break;
}
});
socket.on('close', (_msg) => {
console.log('WebRTC> close');
// Delete the socket from the clients array
for (const [key, value] of Object.entries(clients)) {
if (value === socket) {
delete clients[key];
emitRTC('left', socket, key);
}
}
console.log('WebRTC> connection #', Object.keys(clients).length);
// close handler
socket.on('close', () => {
clients.forEach((sockets) => {
sockets.splice(sockets.indexOf(socket) || 0, 1);
});
});
// error handler
socket.on('error', (msg) => {
console.log('WebRTC> error', msg);
// Delete the socket from the clients array
for (const [key, value] of Object.entries(clients)) {
if (value === socket) {
delete clients[key];
}
}
console.log('WebRTC> connection #', Object.keys(clients).length);
clients.forEach((sockets) => {
sockets.splice(sockets.indexOf(socket) || 0, 1);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export function Background(props: BackgroundProps) {
const valid = isValidURL(pastedText);
if (valid) {
// Create a link app
createApp(setupApp('', 'WebpageLink', xdrop, ydrop, props.roomId, props.boardId, { w: 400, h: 400 }, { url: pastedText }));
createApp(setupApp('WebpageLink', 'WebpageLink', xdrop, ydrop, props.roomId, props.boardId, { w: 400, h: 400 }, { url: pastedText }));
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions webstack/clients/electron/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,27 +649,40 @@ function createWindow() {
// webPreferences.nodeIntegration = true;
// params.nodeIntegration = true;

ipcMain.on('streamview_stop', (e, args) => {
// Message for the webview pixel streaming
const viewContent = electron.webContents.fromId(args.id);
if (viewContent) viewContent.endFrameSubscription();
});
ipcMain.on('streamview', (e, args) => {
// console.log('Webview> IPC Message', evt.frameId, evt.processId, evt.reply);
// console.log('Webview> message', channel, args);
// console.log('Webview> IPC Message', args.id, args.width, args.height);

// Message for the webview pixel streaming
const viewContent = electron.webContents.fromId(args.id);
viewContent.beginFrameSubscription(true, (image, dirty) => {

viewContent.enableDeviceEmulation({
screenPosition: 'mobile',
screenSize: { width: args.width, height: args.height },
});

viewContent.beginFrameSubscription(false, (image, dirty) => {
let dataenc;
let neww, newh;
const devicePixelRatio = 2;
const quality = 50;
const quality = 60;
if (devicePixelRatio > 1) {
neww = dirty.width / devicePixelRatio;
newh = dirty.height / devicePixelRatio;
const resizedimage = image.resize({ width: neww, height: newh });
const resizedimage = image.resize({ width: neww, height: newh, quality: 'better' });
dataenc = resizedimage.toJPEG(quality);
} else {
dataenc = image.toJPEG(quality);
neww = dirty.width;
newh = dirty.height;
}
mainWindow.webContents.send('paint', {
id: args.id,
buf: dataenc.toString('base64'),
dirty: { ...dirty, width: neww, height: newh },
});
Expand Down
4 changes: 2 additions & 2 deletions webstack/clients/electron/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SAGE3",
"version": "1.0.5",
"version": "1.0.6",
"description": "Electron-based SAGE3 client",
"main": "electron.js",
"repository": {
Expand All @@ -18,7 +18,7 @@
"uuid": "8.3.0"
},
"devDependencies": {
"electron": "^25.3.0",
"electron": "^25.4.0",
"electron-notarize": "^1",
"electron-packager": "latest"
},
Expand Down
1 change: 1 addition & 0 deletions webstack/clients/electron/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const validChannels = [
'close-connect-page',
'take-screenshot',
'streamview',
'streamview_stop',
'paint',
'load-landing',
'store-interface',
Expand Down
8 changes: 4 additions & 4 deletions webstack/clients/electron/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,10 @@ electron-winstaller@latest:
lodash.template "^4.2.2"
temp "^0.9.0"

electron@^25.3.0:
version "25.3.0"
resolved "https://registry.yarnpkg.com/electron/-/electron-25.3.0.tgz#e818ab3ebd3e7a45f8fca0f47e607c9af2dc92c7"
integrity sha512-cyqotxN+AroP5h2IxUsJsmehYwP5LrFAOO7O7k9tILME3Sa1/POAg3shrhx4XEnaAMyMqMLxzGvkzCVxzEErnA==
electron@^25.4.0:
version "25.4.0"
resolved "https://registry.yarnpkg.com/electron/-/electron-25.4.0.tgz#d45b1cf3e4e96eb5bff5fee704d7aa13b532f3a5"
integrity sha512-VLTRxDhL4UvQbqM7pTNENnJo62cdAPZT92N+B7BZQ5Xfok1wuVPEewIjBot4K7U3EpLUuHn1veeLzho3ihiP+Q==
dependencies:
"@electron/get" "^2.0.0"
"@types/node" "^18.11.18"
Expand Down
Loading

0 comments on commit 5ac7d1f

Please sign in to comment.