feross / simple-peer

📡 Simple WebRTC video, voice, and data channels
MIT License
7.44k stars 975 forks source link

replaceTraks only works for the second/third participant #864

Open ArgentumToivo opened 2 years ago

ArgentumToivo commented 2 years ago

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