I'm building a web application, where music/audio is playing from an object using new Audio(). On another tab I'd like to listen to the music through WebRTC. I already have the WebRTC established, and I can confirm it works with microphones on both sides.
When I try to change the "music" tab's microphone to a mediastream of a "new Audio()", it no longer works and the listeners-tab is silent.
I'm trying it like this:
audio = new Audio();
audio.src = someArray['audiosrc'];
mediaStream = audio.captureStream()
audio.play();
webRTC.addTrack(mediaStreams.getAudioTracks()[0]);
I have no clue why the listeners-tab is completely silent... Is there anything wrong with the code? I don't see any errors in the console, I don't see any errors on the receiving side, but it still does not want to "stream" the audio.
When you already have a WebRTC connection established and you want to add a new track to it, renegotiation needs to be done so the browser knows how to encode the track.
Renegotiation is pretty much creating an SDP offer and receiving an answer just like during the first connection setup.
See the note on top of the page: https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addTrack