bluenviron / mediamtx

Ready-to-use SRT / WebRTC / RTSP / RTMP / LL-HLS media server and media proxy that allows to read, publish, proxy, record and playback video and audio streams.
MIT License
10.79k stars 1.4k forks source link

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

Open Dylan-Ying opened 5 months ago

Dylan-Ying commented 5 months ago

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

aler9 commented 2 months ago

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:

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:

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.