rtc-link
Version:
A library for managing WebRTC connections with multiplexed streams, anti-glare handling, and streamlined data channel setup.
45 lines (42 loc) • 1.63 kB
JavaScript
import {catchError, concatMap, EMPTY, filter, fromEvent, map, ReplaySubject, takeUntil} from "rxjs";
import {connectAndSend, listenAndConnectionAndRead$, withEncoding} from "rxprotoplex";
import {CHANNEL} from "./constants.js";
import {filterNil} from "rx-filternil";
import {error$} from "./error-catch.js";
export const iceCandidateHandling = (plex, rtc, close$) => {
const ices$ = new ReplaySubject(undefined, 10000);
fromEvent(rtc, "signalingstatechange").pipe(
takeUntil(close$),
filter(() => rtc.signalingState === "have-remote-offer" || rtc.signalingState === "have-remote-pranswer"),
concatMap(() => ices$),
filter(o => !!o.candidate),
concatMap((ice) => rtc.addIceCandidate(new RTCIceCandidate(ice)))
).subscribe();
listenAndConnectionAndRead$(
plex,
CHANNEL.WEBRTC_ICE,
withEncoding("json")
)
.pipe(
takeUntil(close$),
filterNil(),
catchError(error => {
error$.next(error); // Report error without stopping everything
return EMPTY; // Continue the observable chain with an empty value
})
)
.subscribe(({data: ice}) => {
ices$.next(ice);
});
fromEvent(rtc, "icecandidate")
.pipe(
takeUntil(close$),
map(o => o.candidate),
filterNil(),
catchError(error => {
error$.next(error);
return EMPTY;
})
)
.subscribe(connectAndSend(plex, CHANNEL.WEBRTC_ICE, withEncoding("json", {unique: false})));
};