mediamtx icon indicating copy to clipboard operation
mediamtx copied to clipboard

Using net.Buffers.WriteTo send multiple RTP packets to reduce system calls to improve performance

Open Dylan-Ying opened this issue 1 year ago • 1 comments

Describe the feature

Description Each RTP packet sent invokes the io.Write function once, and a frame of video generates many RTP packets. From the pprof, it can be observed that RTP transmission consumes a significant amount of CPU resources.

pprof

Dylan-Ying avatar Jan 17 '24 09:01 Dylan-Ying

Hello, thank you very much for bringing to attention the existence of net.Buffers and the fact that multiple UDP or TCP buffers can be sent with a single call. Using net.Buffers.WriteTo() instead of net.Conn.Write() can surely improve performance in case all RTP packets of a video frames are available in advance, and this happens with the following cases:

  • RTMP to RTSP
  • HLS to RTSP
  • SRT/MPEG-TS to RTSP
  • RTSP to WebRTC (since RTP packets are decoded and re-encoded with a lower MTU)

However this approach won't bring any benefit when RTP packets are received singularly and sent out as soon as possible, and this happens with:

  • RTSP to RTSP
  • WebRTC to RTSP

Therefore: in the future we can add to gortsplib a new method, WritePacketsRTP(pkts []*rtp.Packet), that allows to send out multiple packets at once.

aler9 avatar Apr 17 '24 20:04 aler9