Webrtc aiortc - addTrack failing inside datachannel message receive handler

33 Views Asked by At

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):

  1. Browser only sends audio initially, and the track from remote peer (python server) is not added yet

  2. On "Stop" button, datachannel is used to send a message "trackended" to remote peer (server)

  3. On remote peer (server), the player is initialised and then track is added

  4. (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.

server.py server.py client.js client.js

1

There are 1 best solutions below

0
Ivan On

Adding a track requires a new SDP offer/answer exchange. So on the server-side you will need to do smth like this:

    pc.addTrack(player.audio)
    offer = await pc.createOffer()
    await pc.setLocalDescription(offer)
    # send the offer via datachannel or in some other way
    # .....
    # wait for the answer, then:
    await peer_connection.setRemoteDescription(answer)

The exchange is needed because peers need to negotiate codec parameters for the media.