rob-revisited/extensions/webrtc/README.md

27 lines
2 KiB
Markdown

# jumphp/webrtc
**WebRTC for JumPHP games and apps**: peer-to-peer connections from PHP, the same code in the browser and on the desktop.
A JumPHP engine extension (docs/extensions.md): `composer require jumphp/webrtc`, nothing to wire.
```php
public function __construct(private WebRtc $webrtc) {} // a scene
$peer = $this->webrtc->peer();
$peer->on('signal', fn (array $s) => $net->sendJson(250, $s)); // offer / answer / ICE: send them to the other side, any way you like
// ... when the other side's signal arrives: $peer->signal($s);
$chat = $peer->channel('chat'); // reliable + ordered (a 'pos' channel: ordered: false, retransmits: 0 = like UDP)
$chat->on('message', fn (string $text) => $this->log[] = $text);
$chat->send('hello');
$peer->offer(); // one side offers, the other answers by itself
```
- **Browser**: the page's `RTCPeerConnection` (engine page module `jumphp-webrtc.js`, through the JsBridge).
- **Desktop**: a native peer, GStreamer `webrtcbin` driven as a sidecar process (`bin/webrtc-gst.py`); runs with the computer's
python3 + GStreamer, or in Docker (image built the first time). Linux for now.
- **Screen streaming** (desktop, Linux X11): `$peer->screen(['fps' => 60, 'kbps' => 8000])` sends the computer's screen as H.264
(GPU encoder: VA-API / NVENC, or x264 zero-latency) with an `input` data channel (mouse, wheel, keys, Unicode text, injected with XTest).
The JumPHP IDE's **Screen on a phone** is built on it, with the viewer **JumPHP Screen** (https://jumphp.net/jumphp-screen).
- `Signaling::overNet($net, $peer, $otherPlayer)`: the signals through the multiplayer relay of a LobbyScene room, then peer to peer.
Documentation: `doc/index.md`, the sidecar protocol `doc/sidecar.md`, a tutorial `doc/tutorial.md`, samples `doc/samples/`
(`LoopbackSample`: two peers in one game; `OnlineSample`: two players meet on the relay then talk peer to peer). Example game: `games/webrtc-demo`.