@amatiasq/peer-connection
v4.1.0
Published
WebRTC without the ceremony: one class that owns the `RTCPeerConnection`, the ICE gathering and the local/remote descriptions. You still have to carry the `offer` and the `answer` between the two peers yourself — that is what a signalling server is for.
Readme
@amatiasq/peer-connection
WebRTC without the ceremony: one class that owns the RTCPeerConnection, the
ICE gathering and the local/remote descriptions. You still have to carry the
offer and the answer between the two peers yourself — that is what a
signalling server is for.
npm i --save @amatiasq/peer-connectionUsage
Add your tracks before creating the offer or the answer; they are what the description advertises.
import { PeerConnection } from '@amatiasq/peer-connection';
const conn = new PeerConnection();
conn.onTrackReceived.subscribe(event => play(event.streams[0]));
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
conn.addStream(stream);
// Caller
const offer = await conn.createOffer({ offerToReceiveVideo: true });
conn.acceptAnswer(await sendToPeerSomehow(offer));
// Callee
const answer = await conn.acceptOffer(offerFromPeer, {
offerToReceiveVideo: true,
});
await sendToPeerSomehow(answer);createOffer and acceptOffer resolve once ICE gathering has finished, so what
they hand you is a complete description, ready to send in one message. Gathering
waits for every ICE server it was given, so it is capped: a server that stops
answering delays a call by three seconds instead of hanging it forever.
Your own ICE servers
The default is a pair of public STUN servers, enough to cross a home NAT and
nothing more. Anything else — your own STUN, a TURN server for the networks STUN
cannot cross — goes in the constructor, which takes a plain RTCConfiguration:
const conn = new PeerConnection({
iceServers: [{ urls: 'turn:turn.example.com', username: 'me', credential: 'x' }],
});Changing the streams mid-call
There is no way to renegotiate here: conn.reset() closes the connection and
leaves the instance ready for a fresh handshake, which you start again from
scratch.
