rtci-ffi from JavaScript
rtci-ffi 0.1.0 is rtci-host as a dynamic library for JavaScript runtimes that load WebRTC through plain FFI (Deno.dlopen), with TypeScript bindings generated by dodo-bindgen 2.0. It is the native half; the shim that installs the W3C API over it lives with the runtime.
Get the library
Download the library for your target and the bindings from the v0.1.0 release and check them against its SHA256SUMS (Install & download has every file and checksum). Put the library beside the bindings it came with: the generated rtci_ffi.ts opens rtci_ffi.<target>.<so|dylib|dll> from its own directory.
The surface
One class, Engine, and buildId(out, cap). Every call, with its Rust documentation and its TypeScript signature, is in the rtci-ffi reference.
new Engine("{}")starts an engine thread of its own;Engine.shared("{}")is a handle on the process-wide engine, shared by every thread that asks (the main thread and its workers).- Each handle is a sink: its own event queue and waker (
id()), its own permission gate and the connections and networks made through it, which other handles cannot reach (-9). - Disposing of a handle (
using/[Symbol.dispose]()) closes what it made; the engine thread ends with its last handle. - Every method is synchronous except
generateCertificate,networkCreateandwait, which return a Promise.
Buffers, not pointers
So that a caller needs only Deno.dlopen and symbol calls, which --allow-ffi=<this library> permits:
- strings in: dodo-bindgen's
JsStr(a length-prefixedbuffer); - bytes in: a
Uint8Arrayand its length (dcSend(pc, ch, text, data, data.byteLength)); - results out: a
Uint8Arrayand its capacity,(out, cap)as the last two arguments; the call returns the result's lengthn. Ifn > capnothing was written and the result waits in the calling thread's slot:takeResult(out, cap)withcap >= ncopies it. The call never has to run twice.
Errors: a negative number is an error code; the error is then lastErrorCode() / lastError(out, cap) (per calling thread). Codes: -1 TypeError, -2 SyntaxError, -3 InvalidAccessError, -4 InvalidStateError, -5 OperationError, -6 InvalidModificationError, -7 NotSupportedError, -8 NotCapable, -9 BadResource, -10 engine poisoned, -11 internal error.
Events and waking
drainSize(maxBytes) is the size of the batch a drain takes now; drain(out, cap) takes the events that fit. Allocate drainSize(n) bytes and drain exactly that batch. To be woken: setWaker(threadSafeCallback.pointer), whose callback gets the engine's id() when events are queued (a Deno.UnsafeCallback needs a bare --allow-ffi); under a scoped grant, poll with wait(timeoutMs) and pending().
The events are rtci-host's JsEvent JSON: on handle 0 (the engine) gaterequest; on a connection's handle icecandidate, signalingstatechange, connectionstatechange, negotiationneeded, opdone, dcopen, dcmessage, rtp, encodedframe and the others the README lists, and null when its events end.
The permission gate
gateAttach()once the shim can answer. Until then every address is refused andpcCreatefails with InvalidStateError.- Each new question is a
gaterequestevent on handle 0, for example{"type":"gaterequest","ticket":7,"kind":"remoteCandidate","dir":"connect","proto":"udp","host":"192.0.2.7","port":4000,"resolvedFrom":null}. gateAnswer(ticket, allow, state): 0, or -9 when nothing waits for that ticket any more.
The engine waits for the answer: nothing is bound, resolved or sent to that address before an allow; an answer that does not come within 10 s is a refusal. --allow-ffi gives the library full native rights, scoped grant or not: its gate is cooperative, not a sandbox.
Calls that finish later
pcSetRemoteDescription and pcAddIceCandidate are two-phase: they return an operation id once their arguments are checked, and the outcome is the connection's opdone event ({"type":"opdone","op":3}, or with an error whose kind is a W3C name). networkCreate resolves to {"network":3} once a port network's sockets are bound.
A W3C API over it: the shim
rtci-ffi does not install RTCPeerConnection itself. The rtci-host repository at v0.2.0 carries a JavaScript shim, rtci_webrtc.js (in js/, not published as a package), that gives a Deno runtime without built-in WebRTC the W3C API over rtci-ffi; it checks the library's SHA-256 before dlopen and its buildId() after, and answers gaterequests from Deno.permissions:
import { install } from "./js/mod.ts";
const webrtc = await install(); // or install({ prompt: false, replace: true })
const pc = new RTCPeerConnection();
const channel = pc.createDataChannel("chat");
// ...
webrtc.dispose(); // closes every connection, removes the globals
From js/README.md, “rtci_webrtc.js: the W3C WebRTC API over rtci-ffi” at v0.2.0.
Building it yourself
See Install & download: dodo-bindgen-cli 2.0.0, and panic = "unwind" (the crate refuses to build under abort).