123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- const log$l = new Logger("rtcp");
- class Rtcp extends EventEmitter {
- constructor(e) {
- super();
- E(this, "connection", null);
- E(this, "inputChannel", null);
- E(this, "mediaStream");
- E(this, "socket");
- E(this, "connected", !1);
- E(this, "candidates", []);
- E(this, "isAnswered", !1);
- E(this, "isFlushing", !1);
- E(this, "inputReady", !1);
- E(this, "workers");
- E(this, "actived", !0);
- E(this, "heartbeat");
- E(this, "onIcecandidate", e=>{
- if (e.candidate != null) {
- const t = JSON.stringify(e.candidate);
- log$l.debug(`Got ice candidate: ${t}`),
- this.network.socket.send({
- id: "ice_candidate",
- data: btoa(t)
- })
- }
- }
- );
- E(this, "onIcecandidateerror", e=>{
- log$l.error("onicecandidateerror", e.errorCode, e.errorText, e)
- }
- );
- E(this, "onIceStateChange", e=>{
- switch (e.target.iceGatheringState) {
- case "gathering":
- log$l.info("ice gathering");
- break;
- case "complete":
- log$l.info("Ice gathering completed")
- }
- }
- );
- E(this, "onIceConnectionStateChange", ()=>{
- if (!!this.connection)
- switch (log$l.info(`iceConnectionState: ${this.connection.iceConnectionState}`),
- this.connection.iceConnectionState) {
- case "connected":
- {
- this.connected = !0;
- break
- }
- case "disconnected":
- {
- this.connected = !1,
- this.emit("rtcDisconnected");
- break
- }
- case "failed":
- {
- this.emit("rtcDisconnected"),
- this.connected = !1;
- break
- }
- }
- }
- );
- E(this, "setRemoteDescription", async(e,t)=>{
- var a, s, l;
- if (!this.connection)
- return;
- const r = JSON.parse(atob(e))
- , n = new RTCSessionDescription(r);
- await this.connection.setRemoteDescription(n);
- const o = await this.connection.createAnswer();
- if (o.sdp = (a = o.sdp) == null ? void 0 : a.replace(/(a=fmtp:111 .*)/g, "$1;stereo=1;sprop-stereo=1"),
- ((l = (s = o.sdp) == null ? void 0 : s.match(/a=mid:1/g)) == null ? void 0 : l.length) == 2) {
- const u = o.sdp.lastIndexOf("a=mid:1");
- o.sdp = o.sdp.slice(0, u) + "a=mid:2" + o.sdp.slice(u + 7)
- }
- try {
- await this.connection.setLocalDescription(o)
- } catch (u) {
- log$l.error("error", u)
- }
- this.isAnswered = !0,
- this.network.rtcp.flushCandidate(),
- this.network.socket.send({
- id: "answer",
- data: btoa(JSON.stringify(o))
- }),
- t.srcObject = this.mediaStream
- }
- );
- E(this, "flushCandidate", ()=>{
- this.isFlushing || !this.isAnswered || (this.isFlushing = !0,
- this.candidates.forEach(e=>{
- const t = atob(e)
- , r = JSON.parse(t);
- if (/172\./.test(r.candidate))
- return;
- const n = new RTCIceCandidate(r);
- this.connection && this.connection.addIceCandidate(n).then(()=>{}
- , o=>{
- log$l.info("add candidate failed", o)
- }
- )
- }
- ),
- this.isFlushing = !1)
- }
- );
- E(this, "input", e=>{
- var t;
- !this.actived || !this.inputChannel || this.inputChannel.readyState === "open" && ((t = this.inputChannel) == null || t.send(e))
- }
- );
- this.network = e,
- this.workers = new Workers(this,new Logger("decode")),
- this.workers.registerLogger(new Logger("decode")),
- this.workers.registerFunction("data", t=>{
- this.emit("data", t)
- }
- ),
- this.heartbeat = new Heartbeat({
- ping: t=>{
- e.room.actionsHandler.echo(t)
- }
- ,
- pong(t, r) {
- var n;
- r && t > 500 && log$l.warn(`high hb value ${t}, traceId:` + r),
- (n = e.room.stats) == null || n.assign({
- hb: t
- })
- }
- })
- }
- start() {
- this.connection = new RTCPeerConnection;
- const e = Date.now();
- this.connection.ondatachannel = t=>{
- log$l.info(`ondatachannel: ${t.channel.label}`),
- this.inputChannel = t.channel,
- this.inputChannel.onopen = ()=>{
- var r;
- log$l.info("The input channel has opened, id:", (r = this.inputChannel) == null ? void 0 : r.id),
- this.inputReady = !0,
- this.emit("rtcConnected"),
- this.network.room.currentNetworkOptions.reconnect || (log$l.infoAndReportMeasurement({
- metric: "datachannelOpenedAt",
- startTime: this.network.room._startTime,
- group: "joinRoom"
- }),
- log$l.infoAndReportMeasurement({
- metric: "datachannelOpenedCost",
- startTime: e,
- group: "joinRoom"
- }))
- }
- ,
- this.inputChannel.onclose = ()=>{
- var r;
- return log$l.info("The input channel has closed, id:", (r = this.inputChannel) == null ? void 0 : r.id)
- }
- ,
- this.inputChannel.onmessage = r=>{
- this.workers.dataHandle(r.data)
- }
- }
- ,
- this.connection.oniceconnectionstatechange = this.onIceConnectionStateChange,
- this.connection.onicegatheringstatechange = this.onIceStateChange,
- this.connection.onicecandidate = this.onIcecandidate,
- this.connection.onicecandidateerror = this.onIcecandidateerror,
- this.network.socket.send({
- id: "init_webrtc",
- data: JSON.stringify({
- is_mobile: !0
- })
- })
- }
- addCandidate(e) {
- e === "" ? this.network.rtcp.flushCandidate() : this.candidates.push(e)
- }
- disconnect() {
- var e, t, r;
- this.heartbeat.stop(),
- log$l.info("ready to close datachannel, id", (e = this.inputChannel) == null ? void 0 : e.id),
- (t = this.inputChannel) == null || t.close(),
- (r = this.connection) == null || r.close(),
- this.connection = null,
- this.inputChannel = null
- }
- sendStringData(e) {
- this.input(e)
- }
- sendData(e) {
- let t = "";
- try {
- t = JSON.stringify(e)
- } catch (r) {
- log$l.error(r);
- return
- }
- this.input(t)
- }
- }
|