OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-call / client / local / tracks.js

168 lines · javascript ref:
const { EventEmitter: e } = require("events"),
  { Readable: t } = require("streamx"),
  a = require("debounceify"),
  r = require("fast-deep-equal"),
  { STREAM_TYPE: s, LOCAL_CHANGE_EVENTS: i, LOCAL_CLOSE_EVENTS: n } = require("./constants")
module.exports = class extends e {
  constructor() {
    ;(super(),
      (this.cameraStream = new MediaStream()),
      (this.videoTrack = null),
      (this.audioTrack = null),
      (this.cameraStreamOpened = !1),
      (this.cameraStreamConstraints = { video: !1, audio: !1 }),
      (this._cameraStreamConstraintsWanted = { ...this.cameraStreamConstraints }),
      (this._debouncedSetCameraTracks = a(this._setCameraTracks.bind(this))),
      (this.cameraStreamStatus = { videoMuted: !0, audioMuted: !0 }),
      (this._cameraChangesSubscriptions = new Set()),
      (this.screenStream = new MediaStream()),
      (this.screenTrack = null),
      (this.screenStreamOpened = !1),
      (this.screenStreamConstraints = { video: !1 }),
      (this._screenStreamConstraintsWanted = { ...this.screenStreamConstraints }),
      (this._debouncedSetScreenTracks = a(this._setScreenTracks.bind(this))),
      (this.screenStreamStatus = { videoMuted: !0, audioMuted: !0 }),
      (this._screenChangesSubscriptions = new Set()))
  }
  async _setCameraTracks() {
    const e = !r(this.cameraStreamConstraints.video, this._cameraStreamConstraintsWanted.video),
      t = !r(this.cameraStreamConstraints.audio, this._cameraStreamConstraintsWanted.audio)
    if (e)
      if (
        (this.cameraStream.getVideoTracks().forEach((e) => {
          ;(e.stop(), this.cameraStream.removeTrack(e))
        }),
        this._cameraStreamConstraintsWanted.video)
      ) {
        const e = await navigator.mediaDevices.getUserMedia({
          video: this._cameraStreamConstraintsWanted.video,
          audio: !1,
        })
        ;(e.getTracks().forEach((e) => {
          this.cameraStream.addTrack(e)
        }),
          (this.videoTrack = e.getVideoTracks()[0]))
      } else this.videoTrack = null
    if (t)
      if (
        (this.cameraStream.getAudioTracks().forEach((e) => {
          ;(e.stop(), this.cameraStream.removeTrack(e))
        }),
        this._cameraStreamConstraintsWanted.audio)
      ) {
        const e = await navigator.mediaDevices.getUserMedia({
          video: !1,
          audio: this._cameraStreamConstraintsWanted.audio,
        })
        ;(e.getTracks().forEach((e) => {
          this.cameraStream.addTrack(e)
        }),
          (this.audioTrack = e.getAudioTracks()[0]))
      } else this.audioTrack = null
    ;((this.cameraStreamConstraints.video = this._cameraStreamConstraintsWanted.video),
      (this.cameraStreamConstraints.audio = this._cameraStreamConstraintsWanted.audio),
      (this.cameraStreamStatus.videoMuted = null === this.videoTrack),
      (this.cameraStreamStatus.audioMuted = null === this.audioTrack))
    const a = { ...this.cameraStreamStatus }
    for (const e of this._cameraChangesSubscriptions.values()) e.push(a)
    this.emit(i.CAMERA, e, t, a)
  }
  async _setScreenTracks() {
    const e = !r(this.screenStreamConstraints.video, this._screenStreamConstraintsWanted.video)
    if (e)
      if (
        (this.screenStream.getVideoTracks().forEach((e) => {
          ;(e.stop(), this.screenStream.removeTrack(e))
        }),
        this._screenStreamConstraintsWanted.video)
      ) {
        const e = await navigator.mediaDevices.getUserMedia({
          video: this._screenStreamConstraintsWanted.video,
          audio: !1,
        })
        ;(e.getTracks().forEach((e) => {
          this.screenStream.addTrack(e)
        }),
          (this.screenTrack = e.getVideoTracks()[0]))
      } else this.screenTrack = null
    ;((this.screenStreamConstraints.video = this._screenStreamConstraintsWanted.video),
      (this.screenStreamStatus.videoMuted = null === this.screenTrack))
    const t = { ...this.screenStreamStatus }
    for (const e of this._screenChangesSubscriptions.values()) e.push(t)
    this.emit(i.SCREEN, e, t)
  }
  openStream(e) {
    if (e === s.CAMERA) {
      if (this.cameraStreamOpened) return
      return ((this.cameraStreamOpened = !0), this.cameraStream.id)
    }
    if (e === s.SCREEN) {
      if (this.screenStreamOpened) return
      return ((this.screenStreamOpened = !0), this.screenStream.id)
    }
    throw new Error(`openStream: wrong stream type (${e})`)
  }
  applyStreamConstraints(e, t) {
    return e === this.cameraStream.id
      ? ((this._cameraStreamConstraintsWanted.video = t.video),
        (this._cameraStreamConstraintsWanted.audio = t.audio),
        this._debouncedSetCameraTracks())
      : e === this.screenStream.id
        ? ((this._screenStreamConstraintsWanted.video = t.video), this._debouncedSetScreenTracks())
        : void console.warn(`applyStreamConstraints: wrong stream id (${e})`)
  }
  getSrcObject(e) {
    return e === this.cameraStream.id
      ? this.cameraStream
      : e === this.screenStream.id
        ? this.screenStream
        : null
  }
  closeStream(e) {
    if (e === this.cameraStream.id) {
      if (!this.cameraStreamOpened) return
      return (
        (this.cameraStreamOpened = !1),
        (this.cameraStreamConstraints = { video: !1, audio: !1 }),
        (this._cameraStreamConstraintsWanted.video = !1),
        (this._cameraStreamConstraintsWanted.audio = !1),
        this.cameraStream.getTracks().forEach((e) => {
          ;(e.stop(), this.cameraStream.removeTrack(e))
        }),
        (this.videoTrack = null),
        (this.audioTrack = null),
        void this.emit(n.CAMERA)
      )
    }
    if (e === this.screenStream.id) {
      if (!this.screenStreamOpened) return
      ;((this.screenStreamOpened = !1),
        (this.screenStreamConstraints = { video: !1 }),
        (this._screenStreamConstraintsWanted.video = !1),
        this.screenStream.getTracks().forEach((e) => {
          ;(e.stop(), this.screenStream.removeTrack(e))
        }),
        (this.screenTrack = null),
        this.emit(n.SCREEN))
    }
  }
  subscribeCameraChanges() {
    const e = new t({
        destroy: function (t) {
          ;(a._cameraChangesSubscriptions.delete(e), t(null))
        },
      }),
      a = this
    return (this._cameraChangesSubscriptions.add(e), e)
  }
  subscribeScreenChanges() {
    const e = new t({
        destroy: function (t) {
          ;(a._screenChangesSubscriptions.delete(e), t(null))
        },
      }),
      a = this
    return (this._screenChangesSubscriptions.add(e), e)
  }
}