OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-call / server / media.js

89 lines · javascript ref:
const { Readable: n } = require("streamx"),
  e = require("hypercore-id-encoding"),
  s = require("./webrtc"),
  { MEDIA_EVENTS: o } = require("../lib/common")
module.exports = class {
  constructor(n, s) {
    ;((this.server = n),
      (this.swarm = n.swarm),
      (this.onerror = n.onerror),
      (this.callId = s),
      (this.swarmId = e.encode(this.swarm.keyPair.publicKey)),
      (this._proxiesByConnection = new Map()),
      (this._sendingByConnection = new Map()),
      (this._pendingSignalsByConnection = new Map()),
      (this._signalSubscriptions = []),
      (this._proxySubscriptions = []),
      this.server.internalActivateCall(this))
    for (const n of this.swarm.connections)
      this.addConnection(n, this.server._rawMuxersByStream.get(n))
  }
  close() {
    for (const n of this._proxiesByConnection.keys()) this.removeConnection(n)
    this.server.internalDeactivateCall(this)
  }
  addConnection(n, e) {
    if (this._proxiesByConnection.has(n)) return
    const o = this.swarm.dht.udx.createSocket(),
      i = new s(this.callId, e, o, { onsignal: (e) => this._handleMediaSignal(n, e) })
    ;(i.on("channel-open", () => this._onProxyOpen(n, i)),
      i.on("channel-close", () => this._onProxyClose(n)),
      this._proxiesByConnection.set(n, i),
      this._sendingByConnection.set(n, new Set()))
  }
  removeConnection(n) {
    const s = e.encode(n.remotePublicKey),
      i = this._proxiesByConnection.get(n)
    if (i) {
      ;(i.close(),
        this._proxiesByConnection.delete(n),
        this._sendingByConnection.delete(n),
        this._pendingSignalsByConnection.delete(n))
      for (const n of this._proxySubscriptions) n.push({ type: o.PROXY_CLOSE, swarmId: s })
    }
  }
  _onProxyOpen(n, s) {
    const i = e.encode(n.remotePublicKey)
    for (const n of this._proxySubscriptions)
      n.push({ type: o.PROXY_OPEN, proxyPort: s.proxyPort, swarmId: i })
    const t = this._pendingSignalsByConnection.get(n)
    if (t) {
      for (; t.length; ) s.sendSignal(t.shift())
      this._pendingSignalsByConnection.delete(n)
    }
  }
  _onProxyClose(n) {
    const s = e.encode(n.remotePublicKey)
    for (const n of this._proxySubscriptions) n.push({ type: o.PROXY_CLOSE, swarmId: s })
  }
  _handleMediaSignal(n, s) {
    const o = e.encode(n.remotePublicKey)
    for (const n of this._signalSubscriptions) n.push({ swarmId: o, signal: s })
  }
  signal(n, e) {
    this._proxiesByConnection.get(n).sendSignal(e)
  }
  subscribeMediaSignals() {
    const e = new n({
      destroy: (n) => {
        const s = this._signalSubscriptions.indexOf(e)
        ;(-1 !== s && this._signalSubscriptions.splice(s, 1), n(null))
      },
    })
    return (this._signalSubscriptions.push(e), e)
  }
  subscribeProxyUpdates() {
    const s = new n({
      destroy: (n) => {
        const e = this._proxySubscriptions.indexOf(s)
        ;(-1 !== e && this._proxySubscriptions.splice(e, 1), n(null))
      },
    })
    this._proxySubscriptions.push(s)
    for (const [n, i] of this._proxiesByConnection)
      i.channelOpened &&
        s.push({ type: o.PROXY_OPEN, proxyPort: i.proxyPort, swarmId: e.encode(n.remotePublicKey) })
    return s
  }
}