Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Invite a friend to watch and play as player 2, with the page options in a menu

Samir Nuri79d8eab9b7f8
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d97d248..6765f07 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -103,7 +103,7 @@ if(PS2_GENERATED)
endif()
target_link_libraries(ps2web PRIVATE ps2game ps2runtime)
target_link_options(ps2web PRIVATE --profiling-funcs -sJSPI -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=268435456 -sPTHREAD_POOL_SIZE=2 -sALLOW_TABLE_GROWTH=1 -lidbfs.js -sSTACK_SIZE=16777216 -sENVIRONMENT=web
- -sEXPORTED_FUNCTIONS=_main,_ps2web_pad,_ps2web_pad_connected,_ps2web_resize,_ps2web_gpu_ms,_ps2web_frames,_ps2web_primitives,_ps2web_audio_ring,_ps2web_audio_queued_ms,_ps2web_idle_ms,_ps2web_vu_interpreted,_ps2web_quick_state,_ps2web_video_options
+ -sEXPORTED_FUNCTIONS=_main,_ps2web_pad,_ps2web_pad_connected,_ps2web_resize,_ps2web_gpu_ms,_ps2web_frames,_ps2web_primitives,_ps2web_audio_ring,_ps2web_audio_queued_ms,_ps2web_idle_ms,_ps2web_vu_interpreted,_ps2web_quick_state,_ps2web_video_options,_ps2web_stream
-sEXPORTED_RUNTIME_METHODS=callMain,addRunDependency,removeRunDependency,HEAPU8,ENV
--shell-file ${CMAKE_SOURCE_DIR}/web/shell.html --preload-file ${PS2_WEB_ELF}@/game/core.elf)
set_target_properties(ps2web PROPERTIES SUFFIX ".html" LINK_DEPENDS ${CMAKE_SOURCE_DIR}/web/shell.html)
diff --git a/README.md b/README.md
index 488f0f1..343b36b 100644
--- a/README.md
+++ b/README.md
@@ -73,7 +73,7 @@ Set your expectations accordingly: two games have been brought up so far. Anothe
The page needs WebGPU for the GPU renderer. Chrome and Edge have it on Windows and macOS. On Linux it is off by default in Chrome and Chromium: turn on `chrome://flags/#enable-unsafe-webgpu` and `chrome://flags/#enable-vulkan` and restart the browser, as the page tells you when it finds no usable adapter. Without WebGPU the software renderer takes over, which is fine for menus and far too slow for 3D. On laptops with two GPUs, a canvas drawn on the discrete GPU sometimes cannot be shown on the screen; the page tries both adapters and falls back to copying frames through a 2D canvas.
-The keyboard maps the face buttons to Z (cross), X (circle), A (square) and S (triangle), the shoulders to Q, W, 1 and 2, start and select to Enter and Backspace, the D-pad to the arrow keys and the left stick to I, J, K and L. Gamepads work through the browser's gamepad API. Their analog triggers reach the game as the DualShock 2's pressure-sensitive L2 and R2, and controllers with motors vibrate when the game asks for it, in browsers that expose vibration (Chrome and Edge do). Phones get on-screen controls, and F toggles fullscreen. The resolution selector reloads the page at the new size, and so does the 4:3/16:9 selector, which tells the game the TV is widescreen the way the PS2's system settings do and shows the picture at that shape. Most games need a widescreen patch or their own menu option to draw for it.
+The keyboard maps the face buttons to Z (cross), X (circle), A (square) and S (triangle), the shoulders to Q, W, 1 and 2, start and select to Enter and Backspace, the D-pad to the arrow keys and the left stick to I, J, K and L. Gamepads work through the browser's gamepad API. Their analog triggers reach the game as the DualShock 2's pressure-sensitive L2 and R2, and controllers with motors vibrate when the game asks for it, in browsers that expose vibration (Chrome and Edge do). Phones get on-screen controls, and F toggles fullscreen. The resolution selector reloads the page at the new size, and so does the 4:3/16:9 selector, which starts at 16:9 and tells the game the TV is widescreen the way the PS2's system settings do and shows the picture at that shape. Most games need a widescreen patch or their own menu option to draw for it.
## Playing natively
@@ -89,6 +89,10 @@ build/native-mygame/ps2run games/mygame/disc/SLUS_123.45 --disc games/mygame/dis
The page puts a memory card in slot 1 and keeps it in the browser's storage for the address you play from, so progress survives a reload. Games format and use it as they would a real card. **Export card** downloads it as a `.ps2` file in the layout PCSX2 uses, and **Import card** puts such a file back in the slot, so a card can travel between browsers or come from PCSX2. `?nocard` starts with the slot empty.
+## Playing together
+
+**Invite** makes a room on the server and copies a link to it. A friend who opens the link sees the game and hears it over WebRTC, and their keyboard, gamepad or touch controls become player 2. Their browser downloads neither the game nor the disc, so a phone can join a game a PC is running. The picture they get is the game's own resolution, compressed. Players on networks that cannot reach each other directly would need a TURN relay, which the page does not set up.
+
## Hosting
The page uses SharedArrayBuffer for its threads, so it has to be served cross-origin isolated, with `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`. `scripts/serve.py` sends both headers and supports the Range requests the disc streaming relies on. Anyone who can reach the server can load the game, so keep it to yourself.
diff --git a/host/web.cpp b/host/web.cpp
index 8889950..2417224 100644
--- a/host/web.cpp
+++ b/host/web.cpp
@@ -100,6 +100,10 @@ EM_JS(void, ps2web_frame_done, (), {
EM_ASYNC_JS(void, ps2web_wait_shown, (), {
if (Module.shownPending) await new Promise((resolve) => Module.shownWaiters.push(resolve));
});
+EM_JS(void, ps2web_stream_frame, (const std::uint32_t *rgba, int width, int height), {
+ if (Module.onStreamFrame) Module.onStreamFrame(HEAPU8.subarray(rgba, rgba + width * height * 4), width, height);
+});
+
EM_JS(void, ps2web_blit, (const std::uint32_t *rgba, int width, int height, int wide), {
const canvas = Module.canvas;
if (!Module.blitContext) Module.blitContext = canvas.getContext('2d');
@@ -351,6 +355,10 @@ extern "C" EMSCRIPTEN_KEEPALIVE int ps2web_quick_state(int loading) {
}) ? 1 : 0;
}
+std::atomic<bool> g_streaming{false};
+
+extern "C" EMSCRIPTEN_KEEPALIVE void ps2web_stream(int on) { g_streaming.store(on != 0); }
+
extern "C" EMSCRIPTEN_KEEPALIVE void ps2web_video_options(int flags) { ps2::set_video_options(static_cast<std::uint32_t>(flags)); }
extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_gpu_ms() { return ps2::gpu_busy_ms(); }
@@ -449,6 +457,10 @@ int main(int argc, char **argv) {
const std::function<void(const std::uint32_t *, std::uint32_t, std::uint32_t)> blit = [](const std::uint32_t *rgba, std::uint32_t width, std::uint32_t height) {
ps2web_blit(rgba, static_cast<int>(width), static_cast<int>(height), ps2::g_options.widescreen ? 1 : 0);
};
+ const std::function<void(const std::uint32_t *, std::uint32_t, std::uint32_t)> streamed = [copy, &blit](const std::uint32_t *rgba, std::uint32_t width, std::uint32_t height) {
+ if (copy) blit(rgba, width, height);
+ if (g_streaming.load()) ps2web_stream_frame(rgba, static_cast<int>(width), static_cast<int>(height));
+ };
const std::function<void(const std::uint32_t *, std::uint32_t, std::uint32_t)> none;
const std::function<void()> wait_shown = [] {
static int since_skip = 0;
@@ -469,7 +481,7 @@ int main(int argc, char **argv) {
};
for (;;) {
service_disc();
- const ps2::Rendered rendered = ps2::render_pending(surface, copy ? blit : none, wait_shown, 6.0);
+ const ps2::Rendered rendered = ps2::render_pending(surface, g_streaming.load() ? streamed : copy ? blit : none, wait_shown, 6.0);
if (rendered == ps2::Rendered::presented) ps2web_frame_done();
if (rendered != ps2::Rendered::idle) {
ps2::browser_yield();
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index c09f9c8..47d89be 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -543,7 +543,7 @@ Rendered render_pending(bool surface, const std::function<void(const std::uint32
if (before_present) before_present();
if (surface) {
gs::present(regs);
- return;
+ if (!copy) return;
}
std::uint32_t width = 0, height = 0;
if (copy && gs::copy_frame(regs, frame, width, height)) copy(frame.data(), width, height);
diff --git a/scripts/serve.py b/scripts/serve.py
index 1832b63..d9020a6 100755
--- a/scripts/serve.py
+++ b/scripts/serve.py
@@ -2,13 +2,20 @@
"""Serves a web build and a disc image with HTTP range requests.
usage: serve.py <build dir> <disc image> [port] [--host address] [--record <dir for unrecorded VU1 programs>]
+
+It also relays the messages that connect a host page to the guests it invites
+(/room), so a friend can play along from another browser.
"""
import http.server
+import json
import os
import re
+import secrets
import shutil
import sys
+import threading
+import time
RECORD = None
if '--record' in sys.argv:
@@ -23,6 +30,11 @@ if '--host' in sys.argv:
BUILD = os.path.abspath(sys.argv[1])
DISC = os.path.abspath(sys.argv[2])
PORT = int(sys.argv[3]) if len(sys.argv) > 3 else 8620
+ROOMS = {}
+ROOMS_CHANGED = threading.Condition()
+ROOM_PATH = re.compile(r'^/room(?:/([A-Z2-9]{6})/([a-z0-9]{1,16}))?$')
+ROOM_LETTERS = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
+ROOM_IDLE = 3600
TYPES = {'.html': 'text/html', '.js': 'text/javascript', '.wasm': 'application/wasm', '.data': 'application/octet-stream'}
@@ -54,6 +66,69 @@ class Handler(http.server.BaseHTTPRequestHandler):
self.send_header('Cross-Origin-Resource-Policy', 'same-origin')
self.end_headers()
+ def send_json(self, value, status=200):
+ body = json.dumps(value).encode()
+ self.send_response(status)
+ self.send_header('Content-Type', 'application/json')
+ self.send_header('Content-Length', str(len(body)))
+ self.send_header('Cache-Control', 'no-store')
+ self.end_headers()
+ self.wfile.write(body)
+
+ def room_get(self, code, me):
+ match = re.search(r'after=(\d+)', self.path)
+ after = int(match.group(1)) if match else 0
+ deadline = time.time() + 20
+ with ROOMS_CHANGED:
+ while True:
+ room = ROOMS.get(code)
+ if room is None:
+ self.send_json({'error': 'no such room'}, 404)
+ return
+ room['seen'] = time.time()
+ inbox = room['inboxes'].setdefault(me, [])
+ left = deadline - time.time()
+ if len(inbox) > after or left <= 0:
+ messages = inbox[after:]
+ break
+ ROOMS_CHANGED.wait(left)
+ self.send_json({'messages': messages, 'next': after + len(messages)})
+
+ def do_POST(self):
+ match = ROOM_PATH.match(self.path.split('?', 1)[0])
+ length = int(self.headers.get('Content-Length', '0'))
+ if not match or length > 65536:
+ self.send_error(404 if not match else 413)
+ return
+ body = self.rfile.read(length)
+ code, to = match.groups()
+ with ROOMS_CHANGED:
+ now = time.time()
+ for old in [c for c, r in ROOMS.items() if now - r['seen'] > ROOM_IDLE]:
+ del ROOMS[old]
+ if not code:
+ code = ''.join(secrets.choice(ROOM_LETTERS) for _ in range(6))
+ while code in ROOMS:
+ code = ''.join(secrets.choice(ROOM_LETTERS) for _ in range(6))
+ ROOMS[code] = {'inboxes': {}, 'seen': now}
+ result = {'room': code}
+ elif code in ROOMS:
+ try:
+ message = json.loads(body)
+ except ValueError:
+ message = None
+ if not isinstance(message, dict):
+ self.send_json({'error': 'bad message'}, 400)
+ return
+ ROOMS[code]['inboxes'].setdefault(to, []).append(message)
+ ROOMS[code]['seen'] = now
+ ROOMS_CHANGED.notify_all()
+ result = {}
+ else:
+ self.send_json({'error': 'no such room'}, 404)
+ return
+ self.send_json(result)
+
def do_HEAD(self):
path = self.target()
if not path:
@@ -63,6 +138,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
self.headers_for(path, os.path.getsize(path))
def do_GET(self):
+ match = ROOM_PATH.match(self.path.split('?', 1)[0])
+ if match and match.group(1):
+ self.room_get(*match.groups())
+ return
path = self.target()
if not path:
self.send_error(404)
diff --git a/web/shell.html b/web/shell.html
index 0f21c4c..b35d64e 100644
--- a/web/shell.html
+++ b/web/shell.html
@@ -10,12 +10,21 @@
body { display: flex; flex-direction: column; }
#stage { flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; }
canvas { width: 100%; height: 100%; display: block; background: #000; }
+ #remote { display: none; width: 100%; height: 100%; background: #000; object-fit: fill; }
+ body.guest #remote { display: block; }
+ body.guest.frames #remote { display: none; }
+ body.guest.frames #canvas { display: block; }
+ body.guest:not(.frames) #canvas, body.guest #bar .host-only, body.guest #menu .host-only { display: none; }
#bar { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; padding: 6px 12px; background: #141418; border-top: 1px solid #222; }
#status { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#stats { font-variant-numeric: tabular-nums; color: #9ad; }
select { background: #2a3a55; color: #fff; border: 0; padding: 5px 8px; border-radius: 4px; }
button { background: #2a3a55; color: #fff; border: 0; padding: 6px 14px; border-radius: 4px; cursor: pointer; }
- #aa-label { display: flex; align-items: center; gap: 6px; color: #9ad; white-space: nowrap; }
+ #aa-label, #stats-label { display: flex; align-items: center; gap: 6px; color: #9ad; white-space: nowrap; }
+ #menu { position: fixed; right: 8px; z-index: 4; display: flex; flex-direction: column; align-items: stretch; gap: 8px; padding: 10px;
+ background: #141418; border: 1px solid #2a2a33; border-radius: 8px; max-height: 70vh; overflow-y: auto; }
+ #menu[hidden] { display: none; }
+ body.touch #menu button, body.touch #menu select { padding: 8px 12px; font-size: 14px; }
#overlay { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.6); padding: 16px; }
#panel { display: flex; flex-direction: column; align-items: center; gap: 14px; max-width: 520px; text-align: center; }
#help { margin: 0; line-height: 1.5; color: #e8c37a; }
@@ -23,8 +32,9 @@
body.touch { overscroll-behavior: none; touch-action: none; -webkit-user-select: none; user-select: none; }
body.touch #bar { flex-wrap: nowrap; overflow-x: auto; gap: 8px; padding: 4px 8px calc(4px + env(safe-area-inset-bottom)); touch-action: pan-x; scrollbar-width: none; }
body.touch #bar::-webkit-scrollbar { display: none; }
- body.touch #bar > * { flex: none; }
- body.touch #status { max-width: 45vw; }
+ body.touch #bar > button { flex: none; }
+ body.touch #status, body.touch #stats { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+ body.touch #stats { flex: 0 1 auto; }
body.touch #bar button, body.touch #bar select { padding: 5px 10px; font-size: 12px; }
body.touch #stage { flex: none; width: 100%; aspect-ratio: 4 / 3; max-height: 60vh; }
body.touch.wide #stage { aspect-ratio: 16 / 9; }
@@ -88,7 +98,7 @@
</style>
</head>
<body>
-<div id="stage"><canvas id="canvas" tabindex="0"></canvas></div>
+<div id="stage"><canvas id="canvas" tabindex="0"></canvas><video id="remote" playsinline></video></div>
<div id="touch" class="controls" aria-hidden="true">
<div class="cluster left">
<div class="shoulders"><div class="ctl shoulder" data-btn="256">L2</div><div class="ctl shoulder" data-btn="1024">L1</div></div>
@@ -113,28 +123,34 @@
</div>
</div>
<div id="bar"><span id="status">Loading…</span><span id="stats"></span>
- <select id="scale" aria-label="Resolution" title="Internal resolution, the page reloads to apply it">
+ <button id="invite" class="host-only" type="button" disabled title="Make a room and copy a link that lets a friend watch and play as player 2">Invite</button>
+ <button id="menu-toggle" type="button" aria-expanded="false" aria-controls="menu">Menu ▴</button>
+ <button id="fullscreen" type="button" title="Fullscreen (F or double click)">Fullscreen</button></div>
+<div id="menu" hidden>
+ <select id="scale" class="host-only" aria-label="Resolution" title="Internal resolution, the page reloads to apply it">
<option value="1">640×512</option>
<option value="2">1280×1024</option>
<option value="3">1920×1536</option>
</select>
- <select id="aspect" aria-label="Aspect ratio" title="Screen shape the game is told about, the page reloads to apply it">
+ <select id="aspect" class="host-only" aria-label="Aspect ratio" title="Screen shape the game is told about, the page reloads to apply it">
<option value="4:3">4:3</option>
<option value="16:9">16:9</option>
</select>
<select id="pad" aria-label="Player 1 gamepad" title="Gamepad for player 1, the next connected one is player 2 (press a button on a pad if it is missing)">
<option value="">Pad: auto</option>
</select>
- <button id="save-state" type="button" disabled title="Save the game to this device">Save state</button>
- <button id="load-state" type="button" disabled title="Load the quick save from this device">Load state</button>
- <label id="aa-label" title="Smooth jagged edges, applied immediately"><input id="aa" type="checkbox" disabled> Smooth edges</label>
+ <button id="save-state" class="host-only" type="button" disabled title="Save the game to this device">Save state</button>
+ <button id="load-state" class="host-only" type="button" disabled title="Load the quick save from this device">Load state</button>
+ <label id="aa-label" class="host-only" title="Smooth jagged edges, applied immediately"><input id="aa" type="checkbox" disabled> Smooth edges</label>
+ <label id="stats-label"><input id="stats-toggle" type="checkbox"> Show stats</label>
<button id="controls-toggle" type="button" hidden>Hide controls</button>
- <button id="export-card" type="button" title="Download the memory card in slot 1 as a .ps2 file">Export card</button>
- <button id="import-card" type="button" title="Put a .ps2 memory card file in slot 1">Import card</button>
+ <button id="export-card" class="host-only" type="button" title="Download the memory card in slot 1 as a .ps2 file">Export card</button>
+ <button id="import-card" class="host-only" type="button" title="Put a .ps2 memory card file in slot 1">Import card</button>
<input id="import-file" type="file" accept=".ps2" hidden>
- <button id="fullscreen" type="button" title="Fullscreen (F or double click)">Fullscreen</button></div>
+</div>
<button id="exit-fullscreen" type="button">Exit fullscreen</button>
<div id="overlay"><div id="panel"><p id="help" hidden></p><button id="start" disabled>Loading…</button></div></div>
+<template id="engine">{{{ SCRIPT }}}</template>
<script>
const statusEl = document.getElementById('status');
const CARD_DIR = '/memcard', CARD_FILE = '/memcard/Mcd001.ps2', CARD_BYTES = 8650752;
@@ -200,8 +216,31 @@ importInput.addEventListener('change', async () => {
storeCard(() => location.reload());
});
const statsEl = document.getElementById('stats');
+const statsToggle = document.getElementById('stats-toggle');
+let showStats = !(matchMedia('(pointer: coarse)').matches || /[?&]touch\b/.test(location.search));
+try { const stored = localStorage.getItem('ps2web-stats'); if (stored) showStats = stored === '1'; } catch (e) {}
+statsToggle.checked = showStats;
+statsEl.hidden = !showStats;
+statsToggle.addEventListener('change', () => {
+ showStats = statsToggle.checked;
+ statsEl.hidden = !showStats;
+ try { localStorage.setItem('ps2web-stats', showStats ? '1' : '0'); } catch (e) {}
+});
+const menu = document.getElementById('menu'), menuToggle = document.getElementById('menu-toggle');
+function setMenu(open) {
+ if (open) menu.style.bottom = document.getElementById('bar').offsetHeight + 8 + 'px';
+ menu.hidden = !open;
+ menuToggle.setAttribute('aria-expanded', open ? 'true' : 'false');
+ menuToggle.textContent = open ? 'Menu ▾' : 'Menu ▴';
+}
+menuToggle.addEventListener('click', () => setMenu(menu.hidden));
+document.addEventListener('pointerdown', (e) => {
+ if (!menu.hidden && !menu.contains(e.target) && !menuToggle.contains(e.target)) setMenu(false);
+});
const canvas = document.getElementById('canvas');
const params = new URLSearchParams(location.search);
+const guestRoom = params.get('join');
+if (guestRoom) document.body.classList.add('guest');
function missingFeatures() {
const header = [0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 10];
@@ -220,7 +259,7 @@ function missingFeatures() {
if (!accepts([0, 0xFD, 0x0C].concat(new Array(16).fill(0), [0x1A, 0x0B]))) missing.push('WebAssembly SIMD');
return missing;
}
-const missing = missingFeatures();
+const missing = guestRoom ? [] : missingFeatures();
const touchRoot = document.getElementById('touch'), knob = document.getElementById('knob');
let touchPad = { buttons: 0, x: 0, y: 0, stick: false };
@@ -292,7 +331,7 @@ let aspect = params.get('aspect');
if (!aspect) {
try { aspect = localStorage.getItem('ps2web-aspect'); } catch (e) {}
}
-aspect = aspect === '16:9' ? '16:9' : '4:3';
+aspect = aspect === '4:3' ? '4:3' : '16:9';
aspectSelect.value = aspect;
document.body.classList.toggle('wide', aspect === '16:9');
aspectSelect.addEventListener('change', () => {
@@ -427,6 +466,7 @@ controlsButton.addEventListener('click', () => {
});
function fitCanvas() {
+ if (guestRoom) return;
const ratio = Math.min(window.devicePixelRatio || 1, 2);
const width = Math.max(1, Math.round(canvas.clientWidth * ratio));
const height = Math.max(1, Math.round(canvas.clientHeight * ratio));
@@ -617,12 +657,288 @@ padSelect.addEventListener('change', () => {
window.addEventListener('gamepadconnected', listPads);
window.addEventListener('gamepaddisconnected', listPads);
listPads();
-function pollInput() {
+function orderedPads() {
const pads = [...(navigator.getGamepads ? navigator.getGamepads() : [])].filter((pad) => pad);
const chosen = pads.findIndex((pad) => String(pad.index) === padChoice);
if (chosen > 0) pads.unshift(pads.splice(chosen, 1)[0]);
+ return pads;
+}
+function pollInput() {
+ const pads = orderedPads();
pollPlayer(0, pads[0] || null, true);
- pollPlayer(1, pads[1] || null, false);
+ if (remotePlayer && performance.now() - remoteSeen > 3000) (guestLeaves.get(remotePlayer) || releaseRemote)(remotePlayer);
+ if (!remotePlayer) pollPlayer(1, pads[1] || null, false);
+}
+
+const rtcConfig = { iceServers: [{ urls: ['stun:stun.cloudflare.com:3478', 'stun:stun.l.google.com:19302'] }] };
+const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
+async function roomSend(code, to, message) {
+ const r = await fetch('room/' + code + '/' + to, { method: 'POST', body: JSON.stringify(message) });
+ if (!r.ok) throw new Error(r.status === 404 ? 'Room ' + code + ' does not exist any more.' : 'The room server answered ' + r.status + '.');
+}
+async function* roomMessages(code, me) {
+ let after = 0;
+ for (;;) {
+ let r;
+ try {
+ r = await fetch('room/' + code + '/' + me + '?after=' + after, { cache: 'no-store' });
+ } catch (e) {
+ await sleep(2000);
+ continue;
+ }
+ if (r.status === 404) return;
+ if (!r.ok) {
+ await sleep(2000);
+ continue;
+ }
+ const data = await r.json();
+ after = data.next;
+ yield* data.messages;
+ }
+}
+function gathered(pc) {
+ if (pc.iceGatheringState === 'complete') return Promise.resolve();
+ return new Promise((resolve) => {
+ pc.addEventListener('icegatheringstatechange', () => { if (pc.iceGatheringState === 'complete') resolve(); });
+ setTimeout(resolve, 3000);
+ });
+}
+
+const inviteButton = document.getElementById('invite');
+let room = null, hostStream = null, remotePlayer = null, remoteSeen = 0;
+const guestLeaves = new Map();
+function inviteLink() {
+ const url = new URL(location.href);
+ url.search = '?join=' + room;
+ return url.href;
+}
+function copyInvite() {
+ const link = inviteLink();
+ const copied = navigator.clipboard ? navigator.clipboard.writeText(link) : Promise.reject(new Error('no clipboard'));
+ copied.then(() => note('Invite link copied: ' + link), () => note('Invite link: ' + link));
+}
+let streamShape = 4 / 3;
+function streamTrack() {
+ if (typeof MediaStreamTrackGenerator !== 'function' || typeof VideoFrame !== 'function') {
+ const track = canvas.captureStream(60).getVideoTracks()[0];
+ track.contentHint = 'motion';
+ streamShape = canvas.width / canvas.height;
+ return track;
+ }
+ const generator = new MediaStreamTrackGenerator({ kind: 'video' });
+ const writer = generator.writable.getWriter();
+ let busy = false;
+ Module.onStreamFrame = (pixels, width, height) => {
+ if (busy) return;
+ busy = true;
+ const frame = new VideoFrame(pixels.slice(), { format: 'RGBA', codedWidth: width, codedHeight: height, timestamp: Math.round(performance.now() * 1000) });
+ writer.write(frame).then(() => { busy = false; }, () => { busy = false; });
+ };
+ Module._ps2web_stream(1);
+ generator.contentHint = 'motion';
+ streamShape = aspect === '16:9' ? 16 / 9 : 4 / 3;
+ return generator;
+}
+inviteButton.addEventListener('click', async () => {
+ if (room) {
+ copyInvite();
+ return;
+ }
+ inviteButton.disabled = true;
+ try {
+ const r = await fetch('room', { method: 'POST' });
+ if (!r.ok) throw new Error('This server cannot make rooms. Serve the page with scripts/serve.py.');
+ room = (await r.json()).room;
+ const sound = audio.createMediaStreamDestination();
+ node.connect(sound);
+ hostStream = new MediaStream([streamTrack(), ...sound.stream.getAudioTracks()]);
+ inviteButton.textContent = 'Room ' + room;
+ inviteButton.title = 'Copy the invite link again';
+ copyInvite();
+ hostRoom(room);
+ } catch (error) {
+ room = null;
+ note(error.message);
+ }
+ inviteButton.disabled = false;
+});
+async function hostRoom(code) {
+ const guests = new Map();
+ for await (const m of roomMessages(code, 'host')) {
+ if (m.type === 'join' && typeof m.from === 'string' && !guests.has(m.from)) acceptGuest(code, m.from, guests).catch((e) => console.warn('[room] ' + e));
+ else if (m.type === 'answer' && guests.has(m.from)) guests.get(m.from).setRemoteDescription({ type: 'answer', sdp: m.sdp }).catch((e) => console.warn('[room] ' + e));
+ }
+ note('Room ' + code + ' has closed.');
+}
+async function acceptGuest(code, id, guests) {
+ const pc = new RTCPeerConnection(rtcConfig);
+ guests.set(id, pc);
+ for (const track of hostStream.getTracks()) pc.addTrack(track, hostStream);
+ const channel = pc.createDataChannel('pad', { ordered: false, maxRetransmits: 0 });
+ const leave = () => {
+ guests.delete(id);
+ guestLeaves.delete(id);
+ pc.close();
+ releaseRemote(id);
+ };
+ guestLeaves.set(id, leave);
+ channel.onmessage = (e) => remoteInput(id, e.data);
+ channel.onclose = leave;
+ pc.addEventListener('connectionstatechange', () => {
+ if (pc.connectionState === 'connected') {
+ for (const sender of pc.getSenders()) {
+ if (!sender.track || sender.track.kind !== 'video') continue;
+ const parameters = sender.getParameters();
+ if (!parameters.encodings || !parameters.encodings.length) parameters.encodings = [{}];
+ parameters.encodings[0].maxBitrate = 8000000;
+ parameters.encodings[0].maxFramerate = 60;
+ parameters.degradationPreference = 'maintain-resolution';
+ sender.setParameters(parameters).catch(() => {});
+ }
+ }
+ if (pc.connectionState === 'failed' || pc.connectionState === 'closed') leave();
+ });
+ await pc.setLocalDescription(await pc.createOffer());
+ await gathered(pc);
+ await roomSend(code, id, { type: 'offer', sdp: pc.localDescription.sdp, shape: streamShape });
+}
+function remoteInput(id, data) {
+ if (remotePlayer && remotePlayer !== id) return;
+ let m;
+ try {
+ m = JSON.parse(data);
+ } catch (e) {
+ return;
+ }
+ remoteSeen = performance.now();
+ if (!remotePlayer) {
+ remotePlayer = id;
+ players[1].plugged = true;
+ Module._ps2web_pad_connected(1, 1);
+ note('Player 2 joined');
+ }
+ Module._ps2web_pad(1, m.b, m.lx, m.ly, m.rx, m.ry);
+ if (Array.isArray(m.p)) Module._ps2web_pressure(1, m.p[0], m.p[1], m.p[2], m.p[3]);
+}
+function releaseRemote(id) {
+ if (remotePlayer !== id) return;
+ remotePlayer = null;
+ Object.assign(players[1], { plugged: false, pad: -1, pressure: '' });
+ Module._ps2web_pad_connected(1, 0);
+ note('Player 2 left');
+}
+
+let guestShape = 4 / 3;
+function fitRemote() {
+ const video = document.body.classList.contains('frames') ? canvas : document.getElementById('remote'), stage = document.getElementById('stage');
+ let width = stage.clientWidth, height = width / guestShape;
+ if (height > stage.clientHeight) {
+ height = stage.clientHeight;
+ width = height * guestShape;
+ }
+ video.style.width = Math.floor(width) + 'px';
+ video.style.height = Math.floor(height) + 'px';
+}
+async function drawFrames(track) {
+ document.body.classList.add('frames');
+ fitRemote();
+ const reader = new MediaStreamTrackProcessor({ track }).readable.getReader();
+ const context = canvas.getContext('2d', { alpha: false });
+ let image = null, copyFrames = true;
+ for (;;) {
+ const { value: frame, done } = await reader.read();
+ if (done) return;
+ try {
+ const width = frame.displayWidth, height = frame.displayHeight;
+ if (!image || image.width !== width || image.height !== height) {
+ image = new ImageData(width, height);
+ canvas.width = width;
+ canvas.height = height;
+ }
+ if (copyFrames) {
+ try {
+ await frame.copyTo(image.data, { format: 'RGBA', colorSpace: 'srgb' });
+ context.putImageData(image, 0, 0);
+ continue;
+ } catch (e) {
+ copyFrames = false;
+ }
+ }
+ context.drawImage(frame, 0, 0);
+ } catch (e) {
+ console.warn('[room] ' + e);
+ } finally {
+ frame.close();
+ }
+ }
+}
+async function startGuest() {
+ window.addEventListener('resize', fitRemote);
+ statusEl.textContent = 'Joining room ' + guestRoom + '…';
+ const id = Math.random().toString(36).slice(2, 12);
+ const video = document.getElementById('remote');
+ const pc = new RTCPeerConnection(rtcConfig);
+ let channel = null;
+ const sent = { b: 0, lx: 128, ly: 128, rx: 128, ry: 128, p: [-1, -1, -1, -1] };
+ Object.assign(Module, {
+ _ps2web_pad_connected: () => {},
+ _ps2web_pad: (port, b, lx, ly, rx, ry) => Object.assign(sent, { b, lx, ly, rx, ry }),
+ _ps2web_pressure: (port, a, b, c, d) => { sent.p = [a, b, c, d]; },
+ _ps2web_rumble: () => 0,
+ });
+ const sound = new Audio();
+ pc.ontrack = (e) => {
+ if (typeof MediaStreamTrackProcessor === 'function') {
+ if (e.track.kind === 'video') drawFrames(e.track);
+ else {
+ sound.srcObject = new MediaStream([e.track]);
+ sound.play().catch(() => {});
+ }
+ return;
+ }
+ if (video.srcObject === e.streams[0]) return;
+ video.muted = true;
+ video.srcObject = e.streams[0];
+ video.play().catch(() => {});
+ const unmute = () => {
+ video.muted = false;
+ video.play().catch(() => {});
+ window.removeEventListener('pointerdown', unmute, true);
+ window.removeEventListener('keydown', unmute, true);
+ };
+ window.addEventListener('pointerdown', unmute, true);
+ window.addEventListener('keydown', unmute, true);
+ };
+ pc.ondatachannel = (e) => { channel = e.channel; };
+ pc.addEventListener('connectionstatechange', () => {
+ if (pc.connectionState === 'connected') statusEl.textContent = 'Playing as player 2 in room ' + guestRoom;
+ else if (pc.connectionState === 'failed') statusEl.textContent = 'Could not reach the host. Your networks may need a relay (TURN) server between them.';
+ else if (pc.connectionState === 'closed' || pc.connectionState === 'disconnected') statusEl.textContent = 'Lost the host.';
+ });
+ const send = () => {
+ pollPlayer(0, orderedPads()[0] || null, true);
+ if (channel && channel.readyState === 'open') channel.send(JSON.stringify(sent));
+ requestAnimationFrame(send);
+ };
+ requestAnimationFrame(send);
+ try {
+ const messages = roomMessages(guestRoom, id);
+ await roomSend(guestRoom, 'host', { type: 'join', from: id });
+ for await (const m of messages) {
+ if (m.type !== 'offer') continue;
+ if (m.shape > 0) guestShape = m.shape;
+ document.body.classList.toggle('wide', guestShape > 1.5);
+ fitRemote();
+ await pc.setRemoteDescription({ type: 'offer', sdp: m.sdp });
+ await pc.setLocalDescription(await pc.createAnswer());
+ await gathered(pc);
+ await roomSend(guestRoom, 'host', { type: 'answer', from: id, sdp: pc.localDescription.sdp });
+ return;
+ }
+ statusEl.textContent = 'Room ' + guestRoom + ' does not exist any more.';
+ } catch (error) {
+ statusEl.textContent = error.message;
+ }
}
// some browsers cannot show a canvas drawn by another GPU (hybrid laptops on Linux), so each adapter draws a few test frames first
@@ -821,17 +1137,35 @@ if (missing.length) {
document.getElementById('start').textContent = 'Unsupported browser';
}
+if (guestRoom) {
+ const start = document.getElementById('start');
+ start.disabled = false;
+ start.textContent = 'Join room ' + guestRoom;
+ statusEl.textContent = 'Invited to room ' + guestRoom;
+}
+
document.getElementById('start').addEventListener('click', async () => {
document.getElementById('overlay').remove();
+ if (guestRoom) {
+ startGuest();
+ return;
+ }
canvas.focus();
fitCanvas();
await startAudio();
statusEl.textContent = 'Running, ' + Module.gpuInfo;
Module.running = true;
setStateBusy(false);
+ inviteButton.disabled = false;
Module.callMain(Module.arguments);
});
+
+if (!guestRoom) {
+ const engine = document.createElement('script');
+ engine.src = document.getElementById('engine').content.querySelector('script').getAttribute('src');
+ engine.async = true;
+ document.body.appendChild(engine);
+}
</script>
-{{{ SCRIPT }}}
</body>
</html>