I have used the server folder in examples of aiortc repo as a base point. Then I made a few modification to achieve the following (I'm interested only in audio part):
Browser only sends audio initially, and the track from remote peer (python server) is not added yet
On "Stop" button, datachannel is used to send a message "trackended" to remote peer (server)
On remote peer (server), the player is initialised and then track is added
(Expected) Browser should see track event and then start playing audio sent over that track (wav)
// connect audio / video pc.addEventListener('track', (evt) => { if (evt.track.kind == 'video') document.getElementById('video').srcObject = evt.streams[0]; else document.getElementById('audio').srcObject = evt.streams[0]; });
Steps 1/2/3 work fine. However, step 4 doesn't happen. I've attached the diff here. I would want to know if this is a restriction?
NOTE: I had to resort to the above convoluted technique since @track.on("ended") is not invoked on closing the track from browser.


Adding a track requires a new SDP offer/answer exchange. So on the server-side you will need to do smth like this:
The exchange is needed because peers need to negotiate codec parameters for the media.