Commit graph

43 commits

Author SHA1 Message Date
Sergey Stepanov
9caf45af78
Reset fail timer on success 2024-12-01 18:20:54 +03:00
Sergey Stepanov
88a0911f93
Avoid segfault with nil error handlers in ws 2024-11-17 22:27:34 +03:00
Sergey Stepanov
7b57f73b26 Send worker lib 2024-11-17 12:59:43 +03:00
Sergey Stepanov
ddb16f899f
Update dependencies 2024-08-17 18:13:46 +03:00
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
3e0fcfbfcf
Enable SCTP zero checksums 2024-03-31 21:31:18 +03:00
Sergey Stepanov
3459c7e8d6 Add VP9 encoder option 2024-02-15 14:06:28 +03:00
Sergey Stepanov
6258f9a5e4
Add RTCP packet reader for output streams
Default interceptors need those.
2024-02-13 21:17:27 +03:00
Sergey Stepanov
4fbfa1d4e3
Fix possible NPEs 2023-11-26 22:39:46 +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
a8d47fd1bf
Don't nil peerconnection while receiving ICE 2023-10-18 20:42:46 +03:00
Sergey Stepanov
196930281b Add the initial caged apps abstraction
In the current version of the application, we have strictly hardcoded the captured runtime application (FFI Libretro frontend) as well as the streaming transport (WebRTC). This commit makes it possible to choose these components at runtime.

In this commit, we no longer manage initially connected users separately from the rooms, and instead, we treat all users as abstract app sessions, rather than hardcoded WebRTC connections. These sessions may contain all the transport specifics, such as WebRTC and so on.

Rooms, instead of having the hardcoded emulator app and WebRTC media encoders, now have these components decoupled. In theory, it is possible to add new transports (e.g., WebTransport) and streaming apps (e.g., wrapped into an ffmpeg desktop app).
2023-09-16 20:12:24 +03:00
Sergey Stepanov
cccb3dce84 Fix some test on Arch 2023-09-16 20:12:24 +03:00
Sergey Stepanov
d7e7112e25 Update dependencies 2023-09-16 20:12:24 +03:00
Sergey Stepanov
3b06905e15 Skip peer connection state error due to DTLS spam 2023-09-16 20:12:24 +03:00
Sergey Stepanov
7c0a2051d4
Disable mDNS for ICE 2023-08-01 22:26:37 +03:00
Sergey Stepanov
d7e8ca5ace
Remove SSL from the monitoring routes 2023-07-07 15:28:50 +03:00
Sergey Stepanov
a4f47396e5
Remove unused 404 handler 2023-05-21 18:45:46 +03:00
Sergey Stepanov
624eecd4e8
Show errors when ICE fails 2023-05-17 08:57:16 +03:00
Sergey Stepanov
3815e18027
Remove the hack involving the /static route 2023-05-03 11:11:08 +03:00
Sergey Stepanov
8893e1e5bf Update config manager 2023-04-22 17:05:28 +03:00
Sergey Stepanov
f6e86a465f
Increase ws write timeout for slow IO 2023-04-07 22:24:35 +03:00
Sergey Stepanov
83b40e10e5
Increase default socket timeouts 2023-03-29 13:37:01 +03:00
sergystepanov
3bb54fdad4
Clean API (#391)
Remove hard coupling between api (all the API data structures) and com (app clients communication protocol and logic).
2023-03-18 20:24:06 +03:00
Sergey Stepanov
cd056ee976
Bump to v3 2023-03-16 23:46:53 +03:00
Sergey Stepanov
21a2680027
Fix websocket tests 2023-02-09 14:35:56 +03:00
Sergey Stepanov
652ffe37cc
Do not track DTLS errors (client disconnect) on WebRTC close 2023-02-04 20:32:16 +03:00
sergystepanov
2b81c3fb87
Some optimizations (#387)
- Fixed broken image cache for the first stage RGBA frames. It was not a thread-safe one, which led to image tearing (parts of old images in multiple consecutive frames).
- 180 flip function for the OpenGL coordinate system has been moved into the rotation part.
- Optimized YUV converter.
- Optimized color converters:
  - Use __restrict pointers.
  - Draw image pixels with the faster bitwise operators and pointer arithmetic as 32/16bit LE numbers (may break on ARM devices like RPi). 
  - Pass uints for less num conversions.
  - Much faster XRGB -> RGBA conversion with Go's stdlib 32bit flip.
- Wrapped RGBA images into a custom struct in order to bypass opacity tests for the standard Go png functions, which needed for the PNG file export. Before that we set RGBx opacity byte explicitly during the pixel format conversions (much slower).
- Made Libretro core shutdown more deterministic. When we run a C core separately from the main Go process we have to make sure that the C core is not doing anything in its syscall while we stopping the emulator. Basically, a blocking call may be suspended on the Go's side while the other goroutines have no knowledge of that.
- Less info level logs.
- Added recording user label.
- Enabled RTCP sender reports by default, which may help with A/V sync.
- Check onMessage webrtc handler if it's set. May crash the program if not.
- Fixed some make dirs permissions.
- Enabled console colors (since MS has finally fixed their bloody Terminal).
- Disable log in some tests.
- Updated deps.
2023-01-31 22:22:03 +03:00
Sergey Stepanov
c641065564
Enable RTCP sender reports by default 2023-01-15 01:07:53 +03:00
Sergey Stepanov
2df0135604
Merge remote-tracking branch 'origin/master' 2023-01-15 01:07:04 +03:00
Sergey Stepanov
cd90bfe58d
Enable RTCP sender reports by default 2023-01-15 01:06:50 +03:00
Sergey Stepanov
d12218e9db
Enable RTCP sender reports by default 2023-01-15 01:05:08 +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
ad822a624d
Add optional Origin handling for Websockets 2021-12-15 17:58:49 +03:00
sergystepanov
9acbecb813
WebRTC single port support (#354)
Add the new `webrtc.singlePort` config option which forces the WebRTC server to listen on this port only.
2021-11-02 17:05:31 +03:00
Sergey Stepanov
904b0ae1bb
Set coordinator server address to :http for cloudretro 2021-08-26 14:39:17 +03:00
Sergey Stepanov
25483b27b3
Enable http-01 challenge 2021-08-26 11:58:26 +03:00
Sergey Stepanov
5f941bf593
Use HTTPS redirect with HTTPS domain 2021-08-26 11:20:12 +03:00
Sergey Stepanov
048617373a
Use empty auto cert http handler 2021-08-25 23:13:27 +03:00
Sergey Stepanov
cd41759265
Try without auto cert wrapper redirect 2021-08-25 22:31:37 +03:00
sergystepanov
fb8ee791df
Update the prod deployment script (#341)
Test new deployment workflow.
2021-08-24 21:54:32 +03:00
Sergey Stepanov
c7b259f63d
Use zone prefix in the autocert whitelist 2021-08-16 23:34:41 +03:00
sergystepanov
431d215eee
Update server HTTPS configuration (#337)
* Merge HTTP and HTTPS routes builder in coordinator

* Extract HTTP/S routes

* Rename config in coordinator

* Generalize child services

* Extract games library helper function

* Use string address instead of port for HTTP/S

* Use a dedicated port extractor function

* Add missing GA tag templating

* Rename shared server address and port params

* Introduce TLS config parameters

* Simplify HTTP/S server constructors

* Update server auto port roll

* Extract init function in worker

* Reorder return params of address type port fn

* Refactor config handler names

* Update TLS default config params

* Extract HTTP to HTTPS redirect function

* Use httpx in monitoring

* Don't log echo requests

* Remove error return from the abstract server

* Add WSS option to the worker-coordinator connection

* Change default worker config

* Make worker send its internal connection params

* Decouple gamelib from the coordinator

* Expose HTTP/S listener address

* Keep original HTTP/S addresses

* Remove no config handler in worker

* Use HTTP-HTTPS redirection

* Wrap net.Listener into a struct

* Clean http server creation fn

* Redirect to https with a generated address

* Use URL in the redirector

* Use zone address param in worker

* Make use of actual addr and port in the monitoring servers

* Use auto-justified monitoring addresses info

* Add the non-HTTPS worker to HTTPS coordinator connection warning

* Embed TLS struct

* Move connection API struct into cws package
2021-08-09 10:42:06 +03:00