Commit graph

36 commits

Author SHA1 Message Date
Sergey Stepanov
7ee98c1b03 Add keyboard and mouse support
Keyboard and mouse controls will now work if you use the kbMouseSupport parameter in the config for Libretro cores. Be aware that capturing mouse and keyboard controls properly is only possible in fullscreen mode.

Note: In the case of DOSBox, a virtual filesystem handler is not yet implemented, thus each game state will be shared between all rooms (DOS game instances) of CloudRetro.
2024-08-02 11:04:44 +03:00
Sergey Stepanov
a013192bf8 Clean webrtc.js 2024-04-22 11:47:46 +03:00
Sergey Stepanov
cebbcdf256 Refactor WebRTC stats 2024-04-02 21:10:05 +03:00
Sergey Stepanov
104498dec0
Fix wrong import order of some modules 2024-03-18 13:45:01 +03:00
Sergey Stepanov
2bc64a3be8 Migrate from IIFE to modern ES modules
These modules should be supported by all contemporary browsers, and this transition should resolve most issues related to the explicit import order of the .js files.
2024-03-17 22:09:43 +03:00
Sergey Stepanov
cdbb5e98f5
Clean 2024-03-02 16:46:08 +03:00
Sergey Stepanov
41bfe4f4d3
Fix WebRTC datachannels in FF 2024-02-20 21:29:57 +03:00
Sergey Stepanov
2e91feb861 Add initial automatic aspect ratio change
Depending on the configuration param coreAspectRatio, video streams may have automatic aspect ratio correction in the browser with the value provided by the cores themselves.
2023-11-03 01:12:22 +03:00
Sergey Stepanov
624eecd4e8
Show errors when ICE fails 2023-05-17 08:57:16 +03:00
Sergey Stepanov
229cd4044c
Fix broken warning in the wrtc js module 2023-04-24 14:16:25 +03:00
sergystepanov
3bd959b4ef
Refactored v3 (#350)
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`.
2023-01-09 23:20:22 +03:00
Sergey Stepanov
8025cc1d87
Make client ICE logging a bit clearer 2022-07-13 00:00:54 +03:00
Sergey Stepanov
c190177955
Show active workers in the debug mode 2022-04-08 20:11:28 +03:00
sergystepanov
9ad3c98a7d
Rework worker selection feature (#365)
We add a new option for manual worker or machine (server with multiple workers) select, depending on the new coordinator option `coordinator.debug` which by default allows machine selection.
2022-04-07 21:04:30 +03:00
Sergey Stepanov
24ff0f2ea2
Close WebRTC connections on disconnect
Not closing RTCPeerConnection and co will cause eventually high processor load in Chrome.
Also, Chrome has some GC issues related to WebRTC, see: https://bugs.chromium.org/p/chromium/issues/detail?id=825576.
2021-12-17 16:10:51 +03:00
sergystepanov
1271aa8438
Game recording support (#356)
This feature adds the ability to record game sessions as raw a/v media files.
2021-12-04 14:20:38 +03:00
Sergy Stepanov
5199d73caf Show errors when client WS connection fails 2021-07-31 13:05:48 +03:00
Sergy Stepanov
a3d2549028 Remove old is_mobile init param 2021-07-31 12:33:25 +03:00
sergystepanov
7ebd945344
Remove jQuery (#299)
* Remove jQuery

* Remove browser (vendor) prefixes for some CSS properties

* Use simple device orientation test

* Realign D-pad pointers

* Make GameBoy text unselectable

* Cleanup console.log

* Keep 90% size on mobile browsers due to gh-ribbon overlap

* Remove legacy `unselectable="on"` attributes

* Align UI buttons

* Remove not used UI elements

* Change Options button

* Don't show player change message when not in a game

* Add click/touch handler for circle-pad
2021-03-26 11:07:49 +03:00
sergystepanov
d5780acc75
Fix Pion WebRTC codecs (#289)
Since Pion's webrtc v3.0.13 and this fix if both peers have some fmtp set then codecs should match exactly. By default Pion defines a list of supported codecs with fmtp set but different browsers set their own fmtp values by default as well therefore they may not match.
To fix this we can define our own list of codecs on the server with empty fmtp so it will match any client codecs.

See: e5c8c659ca/rtpcodec.go (L99)
2021-03-08 17:22:43 +03:00
sergystepanov
b56069c96e
Fix stream freeze (#279)
* Fix D-pad key and overlay

* Add Video element error reporting

* Terminate clientside ws ping on the connection terminate
2021-02-20 18:45:43 +03:00
sergystepanov
980a97a526
Handle no config situation for workers (#253)
(experimental feature)

Before a worker can start, it should have a configuration file. In case if such a file is not found it may request configuration from the coordinator to which it connected.

Added example logic if a worker is needed to be blocked until a successful packet exchange with a coordinator is being made.

* Add error return for config loader

* Add config loaded flag to worker

* Add zone flag

* Add a custom mutex lock with timout

* Refactor worker runtime

* Refactor internal api

* Extract monitoring server config

* Extract worker HTTP(S) server

* Add generic sub-server interface

* Add internal coordinator API

* Add internal routes and handlers to worker

* Add internal worker API

* Refactor worker run

* Migrate serverId call to new API

* Add packet handler to cws

* Extract handlers for internal worker routes in coordinator

* Pass worker to the worker internal heandlers

* Cleanup worker handlers in coordinator

* Add closeRoom packet handler to the API

* Add GetRoom packet handler to the API

* Add RegisterRoom packet handler to the API

* Add IceCandidate packet handler to the API (internal and browser)

* Add Heartbeat packet handler to the API (internal and browser)

* Rename worker routes init function

* Extract worker/coordinator internal ws handlers

* Update timed locker

* Allow sequential timed locks

* Add config request from workers

* Add nil check for the route registration functions
2021-01-03 21:23:55 +03:00
88hcsif
f7f044d039 Add multitap support to SNES
Change SNES emulator to Snes9X
Add client control to toggle multitap
2020-06-28 23:35:07 +01:00
88hcsif
2aa1ae4ecb
Add frame delay or frame rate stats to stat view. (#206)
Unfortunately Firefox does not expose frame delay stat.
Unfortunately Chrome and Safari do not expose frame rate stat.
2020-06-26 02:39:39 +08:00
sergystepanov
43d5ca1737
Fix Chrome stereo issue (#202) 2020-06-18 18:56:00 +08:00
sergystepanov
27f8fdc117
Make it choose ws/wss protocol based on URL's protocol. (#196)
When you open the app with https: protocol it will chose wss secured socket connection instead of hardcoded addresses / domains.
2020-06-13 14:23:01 +08:00
giongto35
edf7c4b872
Enable voice communication in the same room (#182)
- All voice is fan-in and broadcasted.
- Currently, voice is sent in the same game track. TODO: send voice in different track (Having issue with dynamically addTrack after offer and answer)
2020-05-15 01:52:51 +08:00
trichimtrich
5dcf4ac95f
Add support firefox safari ios (#175)
* change rtc offer/answer direction

* remove overlay log

* merge browser+session, fix log, add browser map

* add candidate transfer on worker + web
2020-04-28 02:20:49 +08:00
sergystepanov
e05ae221e8
Add generic application statistics overlay (#168)
* Add stat module initial example

* Add stats module help overlay overlap handling

* Add generic graph canvas image builder

* Add more generic graphing module

* Clean up the code

* Some fixes

* Change graphing styles

* Clean some stats module code

* Move to RAF rendering to burn your CPU

* Add not standardized memory stats module

* Fix initial stats visibility flag handle
2020-04-26 22:19:00 +08:00
sergystepanov
b3cb95849d
Refactor input poll (#167)
* Refactor input poll to make it x2-3 faster (0.4ms+ -> 0.1ms)

* If Server is sendrecv transceiver, client needs to be sendrecv on Firefox.
c641b530a0

Co-authored-by: giongto35 <giongto35@yahoo.com>
2020-04-22 01:11:36 +08:00
giongto35
e52ba36759
Update concurrent input (#150) 2020-01-12 19:46:24 +08:00
giongto35
a1ac8f272a
Setup HTTPS using autocert (#149)
* Add HTTPS WSS

* Update https

* HTTPS

* Add https

* Add HTTPS worker

* Add HTTPS

* add https

* Add pingserver to worker

* add https

* Add public domain whitelist

* Not fatal when there existing echo server
2020-01-03 04:37:26 +08:00
giongto35
55815cb9ef
Add multiplayer player (#148)
* Add player index

* Add second player

* Add player index slider

* add multiplayer

* WIP

* Clean up

* Add multiplayer play
2019-12-14 12:32:31 +08:00
giongto35
e2d2436ceb Fix roomID correct 2019-12-12 02:30:14 +08:00
giongto35
1342fabe15
Give option to join by zone + update README (#146)
* Update DREADME CrowdPlay

* Update README

* Update README.md

* Add zone control

* add zone

* Update MISC

* Update go mod
2019-12-09 03:02:35 +08:00
sergystepanov
3d1f1ee587 Initial new frontend rewrite (#129)
* Initial new frontend rewrite

* Refactor some module deps

* Fix options ref in the ajax module

* Refactor

* Add controller state

* Refactor
2019-11-05 01:44:19 +07:00