feross / simple-peer

📡 Simple WebRTC video, voice, and data channels

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

replaceTraks only works for the second/third participant

ArgentumToivo opened this issue · comments

In the room, one of the participants share the screen or changes the camera source, everything works if the second or third participant does it, but if the first one connected does it, then the video freezes for the other participants.
I use replaceTraks.

  replaceTracks(track: MediaStreamTrack) {
    this.meetingParticipants.forEach((participant) => {
      if (participant.connection) {
        participant.connection.replaceTrack(
          participant.connection.streams[0].getVideoTracks()[0],
          track,
          participant.connection.streams[0]
        )
      }
    })
  }

share screen function:

toogleScreenSharingHandler = () => {
    if (!this.isScreenSharing) {
      this.rootStore.meetingStore.enableVideo()
      navigator.mediaDevices.getDisplayMedia().then((stream) => {
        const videoTracks = this.stream.getVideoTracks()[0]
        if (videoTracks) {
          videoTracks.enabled = false
        }
        this.screenSharingstream = stream
        this.isScreenSharing = true
        this.camDisabled = true
        const screenTrack = stream.getTracks()[0]
        this.rootStore.meetingStore.replaceTracks(screenTrack)
        screenTrack.onended = () => {
          this.isScreenSharing = false
          const videoTracks = this.stream.getVideoTracks()[0]
          if (videoTracks) {
            videoTracks.enabled = false
            this.rootStore.meetingStore.disableVideo()
          }
        }
      })
      return
    }
    if (this.isScreenSharing) {
      this.isScreenSharing = false
      this.screenSharingstream.getVideoTracks().forEach((track) => track.stop())
      this.rootStore.meetingStore.disableVideo()
    }
  }

simple-peer v9.11.0, node v16.13.1, npm v8.3.0