Skip to content

Why onnegotiationneeded is triggered only once?

I am adding tracks to my peer and the first time onnegotiationneeded is triggered just fine. However, upon adding more tracks onnegotiationneeded is not triggered therefore the new tracks are not showing any effect. This is the main part of the code I am working with :

  const { id } = req.params
  peer[id] = new webrtc.RTCPeerConnection()
  const guest = { id, name: '', tracks: [], room }
  guest.tracks.forEach(t => peer[host.id].addTrack(t))

  const createNegotiation = async (peerID) => {
    peer[peerID].onnegotiationneeded = async () => {
      console.log('onnegotiationneededonnegotiationneeded')
      const offer = await peer[peerID].createOffer()
      await peer[peerID].setLocalDescription(offer)
      dc.send(JSON.stringify({ type: 'onnegotiationneeded', "sdp": peer[peerID].localDescription }))
    }
  }

Answer

It turns out that onnegotiationneeded only fires once at removeTrack or addTrack and is not able to detect further additional tracks. Therefore I ended up reestablishing the connection allover again through a

  const reNew = async (peerID) => {
      console.log('RENEW WITH PEER ID '  , peerID)
      const offer = await peer[peerID].createOffer()
      await peer[peerID].setLocalDescription(offer)
      peer[peerID].dcm.send(JSON.stringify({ type: 'RENEW', "sdp": peer[peerID].localDescription }))
  }