mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-23 18:17:32 +00:00
This PR contains refactored code.
**Changelog**
- Added new net code (the communication architecture was left intact).
- All network client IDs now have custom type `network.Uid` backed by github.com/rs/xid lib.
```
The string representation of a UUID takes 32 bytes, and the new type will take just 16.
Because of Golang JSON serialization problems with omitting zero-length empty slices (it can't)
and the need to use UID values as map keys (maps don't support slices as keys),
IDs are stored as strings (for now).
```
- A whole new WebSocket client/server implementation was added, as well as a new communication layer with synchronous and async call handlers.
- WebSocket connections now support dedicated Ping/Pong frames as opposed to original ping text messages.
- Used Gorilla WebSocket library doesn't allow concurrent (simultaneous) reads and writes, so this part was handled via send channel synchronization.
- New API structures can be found in the `pkg/api` folder.
- New communication protocol can be found in the `pkg/com/*` folder.
- Updated communication protocol is based on JSON-encoded messaging through WebSocket and has the following structure:
```
Packet
[id] string — a globally unique identification tag for the packet to track it trough a chain of requests.
t uint8 — contains packet type information (i.e. INIT_PACKET, SDP_OFFER_PACKET, ...).
[p] any — contains packet data (any type).
Each packet is a text message in JSON-serialized form (WebSocket control frames obviously not).
```
```
The main principle of this protocol and the duplex data exchange is:
the one who initializes connection is called a client, and
the one who is being connected to is called a server.
With the current architecture, the coordinator is the server, the user browsers and workers are the clients.
____ ____
↓ ↑ ↑ ↓
browser ⟶ coordinator ⟵ worker
(c) (s) (c)
One of the most crucial performance vise parts of these interactions is that
all the server-initiated calls to clients should be asynchronous!
```
- In order to track synchronous calls (packets) with an asynchronous protocol, such as WebSocket, each packet may have an `id` that should be copied in all subsequent requests/responses.
- The old `sessionID` param was replaced by `id` that should be stored inside the `p` (payload) part of the packet.
- It is possible to skip the default ping check for all connected workers on every user connection and just pick the first available with the new roundRobin param in the coordinator config file `coordinator.roundRobin: true/false`.
- Added a dedicated package for the system API (pkg/api/*).
- Added structured logging system (zerolog) for better logging and cloud services integration.
- Added a visual representation of the network message exchange in logs:
```
...
01:00:01.1078 3f98 INF w → c Handshake ws://localhost:8000/wso
01:00:01.1138 994 INF c ← w Handshake localhost:8000
01:00:01.1148 994 INF c ← w Connect cid=cep.hrg
01:00:01.1158 994 DBG c connection id has been changed to cepl7obdrc3jv66kp2ug cid=cep.hrg
01:00:01.1158 3f98 INF w → c Connect cid=cep.2ug
01:00:01.1158 994 INF c New worker / addr: localhost, ...
01:00:01.1158 3f98 INF w Connected to the coordinator localhost:8000 cid=cep.2ug
01:00:02.5834 994 INF c ← u Handshake localhost:8000
01:00:02.6175 994 INF c ← u Connect cid=cep.hs0
01:00:02.6209 994 INF c Search available workers cid=cep.hs0
01:00:02.6214 994 INF c Found next free worker cid=cep.hs0
01:00:02.6220 994 INF c → u InitSession cid=cep.hs0
01:00:02.6527 994 INF c ← u WebrtcInit cid=cep.hs0
01:00:02.6527 994 INF c → w ᵇWebrtcInit cid=cep.hrg
01:00:02.6537 3f98 INF w ← c WebrtcInit cid=cep.2ug
01:00:02.6537 3f98 INF w WebRTC start cid=cep.2ug
...
```
- Replaced a monstrous Prometheus metrics lib.
- Removed spflag dependency.
- Added new `version` config file param/constant for compatibility reasons.
- Bump the minimum required version for Go to 1.18 due to use of generics.
- Opus encoder now is cached and the default config is 96Kbps, complexity 5 (was 196Kbps, 8).
- Changed the default x264 quality parameters to `crf 23 / superfast / baseline` instead of `crf 17 / veryfast / main`.
- Added a separate WebRTC logging config param `webrtc.logLevel`.
- Worker now allocates much less memory.
- Optimized and fixed RGB to YUV converter.
- `--v=5` logging cmd flag was removed and replaced with the `debug` config parameter.
**Breaking changes (migration to v3)**
- Coordinator server API changes, see web/js/api/api.js.
- Coordinator client event API changes:
- c `GAME_PLAYER_IDX_CHANGE` (string) -> `GAME_PLAYER_IDX` (number)
- c `GAME_PLAYER_IDX` -> `GAME_PLAYER_IDX_SET`
- c `MEDIA_STREAM_INITIALIZED` -> `WEBRTC_NEW_CONNECTION`
- c `MEDIA_STREAM_SDP_AVAILABLE` -> `WEBRTC_SDP_OFFER`
- c `MEDIA_STREAM_CANDIDATE_ADD` -> `WEBRTC_ICE_CANDIDATE_RECEIVED`
- c `MEDIA_STREAM_CANDIDATE_FLUSH` -> `WEBRTC_ICE_CANDIDATES_FLUSH`
- x `MEDIA_STREAM_READY` -> **removed**
- c `CONNECTION_READY` -> `WEBRTC_CONNECTION_READY`
- c `CONNECTION_CLOSED` -> `WEBRTC_CONNECTION_CLOSED`
- c `GET_SERVER_LIST` -> `WORKER_LIST_FETCHED`
- x `KEY_STATE_UPDATED` -> **removed**
- n `WEBRTC_ICE_CANDIDATE_FOUND`
- n `WEBRTC_SDP_ANSWER`
- n `MESSAGE`
- `rtcp` module renamed to `webrtc`.
- Controller state equals Libretro controller state (changed order of bits), see: web/js/input/input.js.
- Added new `coordintaor.selector` config param that changes the selection algorithm for workers. By default it will select any free worker. Set this param to `ping` for the old behavior.
- Changed the name of the `webrtc.iceServers.url` config param to `webrtc.iceServers.urls`.
433 lines
12 KiB
JavaScript
433 lines
12 KiB
JavaScript
/**
|
|
* App statistics module.
|
|
*
|
|
* Events:
|
|
* <- STATS_TOGGLE
|
|
* <- HELP_OVERLAY_TOGGLED
|
|
*
|
|
* @version 1
|
|
*/
|
|
const stats = (() => {
|
|
const _modules = [];
|
|
let tempHide = false;
|
|
|
|
// internal rendering stuff
|
|
const fps = 30;
|
|
let time = 0;
|
|
let active = false;
|
|
|
|
// !to add connection drop notice
|
|
|
|
const statsOverlayEl = document.getElementById('stats-overlay');
|
|
|
|
/**
|
|
* The graph element.
|
|
*/
|
|
const graph = (parent, opts = {
|
|
historySize: 60,
|
|
width: 60 * 2 + 2,
|
|
height: 20,
|
|
pad: 4,
|
|
scale: 1,
|
|
style: {
|
|
barColor: '#9bd914',
|
|
barFallColor: '#c12604'
|
|
}
|
|
}) => {
|
|
const _canvas = document.createElement('canvas');
|
|
const _context = _canvas.getContext('2d');
|
|
|
|
let data = [];
|
|
|
|
_canvas.setAttribute('class', 'graph');
|
|
|
|
_canvas.width = opts.width * opts.scale;
|
|
_canvas.height = opts.height * opts.scale;
|
|
|
|
_context.scale(opts.scale, opts.scale);
|
|
_context.imageSmoothingEnabled = false;
|
|
_context.fillStyle = opts.fillStyle;
|
|
|
|
if (parent) parent.append(_canvas);
|
|
|
|
// bar size
|
|
const barWidth = Math.round(_canvas.width / opts.scale / opts.historySize);
|
|
const barHeight = Math.round(_canvas.height / opts.scale);
|
|
|
|
let maxN = 0,
|
|
minN = 0;
|
|
|
|
const max = () => maxN
|
|
|
|
const get = () => _canvas
|
|
|
|
const add = (value) => {
|
|
if (data.length > opts.historySize) data.shift();
|
|
data.push(value);
|
|
render();
|
|
}
|
|
|
|
/**
|
|
* Draws a bar graph on the canvas.
|
|
*/
|
|
const render = () => {
|
|
// 0,0 w,0 0,0 w,0 0,0 w,0
|
|
// +-------+ +-------+ +---------+
|
|
// | | |+---+ | |+---+ |
|
|
// | | |||||| | ||||||+---+
|
|
// | | |||||| | |||||||||||
|
|
// +-------+ +----+--+ +---------+
|
|
// 0,h w,h 0,h w,h 0,h w,h
|
|
// [] [3] [3, 2]
|
|
//
|
|
|
|
_context.clearRect(0, 0, _canvas.width, _canvas.height);
|
|
|
|
maxN = data[0] || 1;
|
|
minN = 0;
|
|
for (let k = 1; k < data.length; k++) {
|
|
if (data[k] > maxN) maxN = data[k];
|
|
if (data[k] < minN) minN = data[k];
|
|
}
|
|
|
|
for (let j = 0; j < data.length; j++) {
|
|
let x = j * barWidth,
|
|
y = (barHeight - opts.pad * 2) * (data[j] - minN) / (maxN - minN) + opts.pad;
|
|
|
|
const color = j > 0 && data[j] > data[j - 1] ? opts.style.barFallColor : opts.style.barColor;
|
|
|
|
drawRect(x, barHeight - Math.round(y), barWidth, barHeight, color);
|
|
}
|
|
}
|
|
|
|
const drawRect = (x, y, w, h, color = opts.style.barColor) => {
|
|
_context.fillStyle = color;
|
|
_context.fillRect(x, y, w, h);
|
|
}
|
|
|
|
return {add, get, max, render}
|
|
}
|
|
|
|
/**
|
|
* Get cached module UI.
|
|
*
|
|
* HTML:
|
|
* <div><div>LABEL</div><span>VALUE</span>[<span><canvas/><span>]</div>
|
|
*
|
|
* @param label The name of the stat to show.
|
|
* @param withGraph True if to draw a graph.
|
|
* @param postfix Supposed to be the name of the stat passed as a function.
|
|
* @returns {{el: HTMLDivElement, update: function}}
|
|
*/
|
|
const moduleUi = (label = '', withGraph = false, postfix = () => 'ms') => {
|
|
const ui = document.createElement('div'),
|
|
_label = document.createElement('div'),
|
|
_value = document.createElement('span');
|
|
ui.append(_label, _value);
|
|
|
|
let postfix_ = postfix;
|
|
|
|
let _graph;
|
|
if (withGraph) {
|
|
const _container = document.createElement('span');
|
|
ui.append(_container);
|
|
_graph = graph(_container);
|
|
}
|
|
|
|
_label.innerHTML = label;
|
|
|
|
const withPostfix = (value) => postfix_ = value;
|
|
|
|
const update = (value) => {
|
|
if (_graph) _graph.add(value);
|
|
// 203 (333) ms
|
|
_value.textContent = `${value < 1 ? '<1' : value} ${_graph ? `(${_graph.max()}) ` : ''}${postfix_(value)}`;
|
|
}
|
|
|
|
return {el: ui, update, withPostfix}
|
|
}
|
|
|
|
/**
|
|
* Latency stats submodule.
|
|
*
|
|
* Accumulates the simple rolling mean value
|
|
* between the next server request and following server response values.
|
|
*
|
|
* window
|
|
* _____________
|
|
* | |
|
|
* [1, 1, 3, 4, 1, 4, 3, 1, 2, 1, 1, 1, 2, ... n]
|
|
* |
|
|
* stats_snapshot_period
|
|
* mean = round(next - mean / length % window)
|
|
*
|
|
* Events:
|
|
* <- PING_RESPONSE
|
|
* <- PING_REQUEST
|
|
*
|
|
* ?Interface:
|
|
* HTMLElement get()
|
|
* void enable()
|
|
* void disable()
|
|
* void render()
|
|
*
|
|
* @version 1
|
|
*/
|
|
const latency = (() => {
|
|
let listeners = [];
|
|
|
|
let mean = 0;
|
|
let length = 0;
|
|
let previous = 0;
|
|
const window = 5;
|
|
|
|
const ui = moduleUi('Ping(c)', true);
|
|
|
|
const onPingRequest = (data) => previous = data.time;
|
|
|
|
const onPingResponse = () => {
|
|
length++;
|
|
const delta = Date.now() - previous;
|
|
mean += Math.round((delta - mean) / length);
|
|
|
|
if (length % window === 0) {
|
|
length = 1;
|
|
mean = delta;
|
|
}
|
|
}
|
|
|
|
const enable = () => {
|
|
listeners.push(
|
|
event.sub(PING_RESPONSE, onPingResponse),
|
|
event.sub(PING_REQUEST, onPingRequest)
|
|
);
|
|
}
|
|
|
|
const disable = () => {
|
|
while (listeners.length) listeners.shift().unsub();
|
|
}
|
|
|
|
const render = () => ui.update(mean);
|
|
|
|
const get = () => ui.el;
|
|
|
|
return {get, enable, disable, render}
|
|
})(event, moduleUi);
|
|
|
|
/**
|
|
* User agent memory stats.
|
|
*
|
|
* ?Interface:
|
|
* HTMLElement get()
|
|
* void enable()
|
|
* void disable()
|
|
* void render()
|
|
*
|
|
* @version 1
|
|
*/
|
|
const clientMemory = (() => {
|
|
let active = false;
|
|
|
|
const measures = ['B', 'KB', 'MB', 'GB'];
|
|
const precision = 1;
|
|
let mLog = 0;
|
|
|
|
const ui = moduleUi('Memory', false, (x) => (x > 0) ? measures[mLog] : '');
|
|
|
|
const get = () => ui.el;
|
|
|
|
const enable = () => {
|
|
active = true;
|
|
render();
|
|
}
|
|
|
|
const disable = () => active = false;
|
|
|
|
const render = () => {
|
|
if (!active) return;
|
|
|
|
const m = performance.memory.usedJSHeapSize;
|
|
let newValue = 'N/A';
|
|
|
|
if (m > 0) {
|
|
mLog = Math.floor(Math.log(m) / Math.log(1000));
|
|
newValue = Math.round(m * precision / Math.pow(1000, mLog)) / precision;
|
|
}
|
|
|
|
ui.update(newValue);
|
|
}
|
|
|
|
if (window.performance && !performance.memory) performance.memory = {usedJSHeapSize: 0, totalJSHeapSize: 0};
|
|
|
|
return {get, enable, disable, render}
|
|
})(moduleUi, performance, window);
|
|
|
|
|
|
const webRTCStats_ = (() => {
|
|
let interval = null
|
|
|
|
function getStats() {
|
|
if (!webrtc.isConnected()) return;
|
|
webrtc.getConnection().getStats(null).then(stats => {
|
|
let frameStatValue = '?';
|
|
stats.forEach(report => {
|
|
if (report["framesReceived"] !== undefined && report["framesDecoded"] !== undefined && report["framesDropped"] !== undefined) {
|
|
frameStatValue = report["framesReceived"] - report["framesDecoded"] - report["framesDropped"];
|
|
event.pub('STATS_WEBRTC_FRAME_STATS', frameStatValue)
|
|
} else if (report["framerateMean"] !== undefined) {
|
|
frameStatValue = Math.round(report["framerateMean"] * 100) / 100;
|
|
event.pub('STATS_WEBRTC_FRAME_STATS', frameStatValue)
|
|
}
|
|
|
|
if (report["nominated"] && report["currentRoundTripTime"] !== undefined) {
|
|
event.pub('STATS_WEBRTC_ICE_RTT', report["currentRoundTripTime"] * 1000);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
const enable = () => {
|
|
interval = window.setInterval(getStats, 1000);
|
|
}
|
|
|
|
const disable = () => window.clearInterval(interval);
|
|
|
|
return {enable, disable, internal: true}
|
|
})(event, webrtc, window);
|
|
|
|
/**
|
|
* User agent frame stats.
|
|
*
|
|
* ?Interface:
|
|
* HTMLElement get()
|
|
* void enable()
|
|
* void disable()
|
|
* void render()
|
|
*
|
|
* @version 1
|
|
*/
|
|
const webRTCFrameStats = (() => {
|
|
let value = 0;
|
|
let listener;
|
|
|
|
const label = env.getBrowser() === 'firefox' ? 'FramerateMean' : 'FrameDelay';
|
|
const ui = moduleUi(label, false, () => '');
|
|
|
|
const get = () => ui.el;
|
|
|
|
const enable = () => {
|
|
listener = event.sub('STATS_WEBRTC_FRAME_STATS', onStats);
|
|
}
|
|
|
|
const disable = () => {
|
|
value = 0;
|
|
if (listener) listener.unsub();
|
|
}
|
|
|
|
const render = () => ui.update(value);
|
|
|
|
function onStats(val) {
|
|
value = val;
|
|
}
|
|
|
|
return {get, enable, disable, render}
|
|
})(env, event, moduleUi);
|
|
|
|
const webRTCRttStats = (() => {
|
|
let value = 0;
|
|
let listener;
|
|
|
|
const ui = moduleUi('RTT', true, () => 'ms');
|
|
|
|
const get = () => ui.el;
|
|
|
|
const enable = () => {
|
|
listener = event.sub('STATS_WEBRTC_ICE_RTT', onStats);
|
|
}
|
|
|
|
const disable = () => {
|
|
value = 0;
|
|
if (listener) listener.unsub();
|
|
}
|
|
|
|
const render = () => ui.update(value);
|
|
|
|
function onStats(val) {
|
|
value = val;
|
|
}
|
|
|
|
return {get, enable, disable, render}
|
|
})(event, moduleUi);
|
|
|
|
const modules = (fn, force = true) => _modules.forEach(m => (force || !m.internal) && fn(m))
|
|
|
|
const enable = () => {
|
|
active = true;
|
|
modules(m => m.enable())
|
|
render();
|
|
draw();
|
|
_show();
|
|
};
|
|
|
|
function draw(timestamp) {
|
|
if (!active) return;
|
|
|
|
const time_ = time + 1000 / fps;
|
|
|
|
if (timestamp > time_) {
|
|
time = timestamp;
|
|
render();
|
|
}
|
|
|
|
requestAnimationFrame(draw);
|
|
}
|
|
|
|
const disable = () => {
|
|
active = false;
|
|
modules(m => m.disable());
|
|
_hide();
|
|
}
|
|
|
|
const _show = () => statsOverlayEl.style.visibility = 'visible';
|
|
const _hide = () => statsOverlayEl.style.visibility = 'hidden';
|
|
|
|
const onToggle = () => active ? disable() : enable();
|
|
|
|
/**
|
|
* Handles help overlay toggle event.
|
|
* Workaround for a not normal app layout layering.
|
|
*
|
|
* !to remove when app layering is fixed
|
|
*
|
|
* @param {Object} overlay Overlay data.
|
|
* @param {boolean} overlay.shown A flag if the overlay is being currently showed.
|
|
*/
|
|
const onHelpOverlayToggle = (overlay) => {
|
|
if (statsOverlayEl.style.visibility === 'visible' && overlay.shown && !tempHide) {
|
|
_hide();
|
|
tempHide = true;
|
|
} else {
|
|
if (tempHide) {
|
|
_show();
|
|
tempHide = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
const render = () => modules(m => m.render(), false);
|
|
|
|
// add submodules
|
|
_modules.push(
|
|
webRTCRttStats,
|
|
// latency,
|
|
clientMemory,
|
|
webRTCStats_,
|
|
webRTCFrameStats
|
|
);
|
|
modules(m => statsOverlayEl.append(m.get()), false);
|
|
|
|
event.sub(STATS_TOGGLE, onToggle);
|
|
event.sub(HELP_OVERLAY_TOGGLED, onHelpOverlayToggle)
|
|
|
|
return {enable, disable}
|
|
})(document, env, event, log, webrtc, window);
|