pythonwebrtcaiortc

python aiortc datachannel has a great latency


Demo

https://github.com/tsonglew/aiortc-datachannel-delay

run python main.py to start the server, then visit localhost:8080 with a web browser

Problem

I'm trying to process the video frames from video track, and send back the result with datachannel.

the result is replaced with time.time() in the above demo repo as following:

self.channel.send(
      json.dumps({"now": time.time() * 1000})
)

the <client receive from datachannel time> - <datachennl.send() time> is considered as delay, and are console.loged with code:

ch.addEventListener("message", function (evt) {
    console.log(Date.now() - JSON.parse(evt.data).now);
});

Output

as illustrated in the sreenshot, the delay is growing, but video and audio are fluent.

How can I get rid of the growing delay? Thx


Solution

  • Calling RTCDataChannel.send in aiortc do not send SCTP data chunks immediately. As a workaround, flushing outbound data queue and transmitting data explicitly may help.

    self.channel.send(json.dumps({"now": time.time() * 1000}))
    await self.channel._RTCDataChannel__transport._data_channel_flush()
    await self.channel._RTCDataChannel__transport._transmit()
    

    for more details: https://github.com/aiortc/aiortc/issues/547