rtc-link
Version:
A library for managing WebRTC connections with multiplexed streams, anti-glare handling, and streamlined data channel setup.
33 lines (28 loc) • 1.2 kB
JavaScript
import {extractFingerprint} from "./extractFingerprint.js";
import {connectAndSend, withEncoding} from "rxprotoplex";
import {CHANNEL} from "./constants.js";
import {waitForAnswer} from "./waitForAnswer.js";
import {error$} from "./error-catch.js";
export const makeOffer = async (plex, rtc, close$) => {
try {
rtc.makingOffer = true;
const offer = await rtc.createOffer();
rtc.localFingerprint = extractFingerprint(offer.sdp);
await rtc.setLocalDescription(offer);
connectAndSend(plex, CHANNEL.WEBRTC_OFFER, withEncoding("json", {unique: false}))(offer);
const answer = await waitForAnswer(plex, close$);
if (answer && rtc.signalingState !== "closed") {
await rtc.setRemoteDescription(new RTCSessionDescription(answer));
}
} catch (error) {
if (error.name === "InvalidStateError") {
console.warn("makeOffer: Anti-Glare engaged. Both you and other peer were initiators and created an offer.");
} else {
console.error("Error in makeOffer:", error);
close$.next();
error$.next(error);
}
} finally {
rtc.makingOffer = false;
}
};