versatica / mediasoup

Cutting Edge WebRTC Video Conferencing
https://mediasoup.org
ISC License
6.27k stars 1.13k forks source link

Use uint64_t for containers with RTP seq number as key #1370

Open ibc opened 7 months ago

ibc commented 7 months ago

As discussed in issue https://github.com/versatica/mediasoup/issues/1366 (this comment by @penguinol: https://github.com/versatica/mediasoup/issues/1366#issuecomment-2045075046) we may use uint64_t in C++ maps and sets in which the key is a RTP seq number or similar. This would allow us to use our SeqManager compare functions into those containers without violating transitivity, i.e. comp(a,b) && comp(b,c) -> comp(a,c), which currently we are violating (see the referenced issue for details).

libwebrtc uses uint64_t for seq nums, it takes more memory and cpu usage, but it's easier to understand and more reliable. uint64_t is big enougth to avoid wrap around. When receiving a packet, unwrap uint16_t seq num to uint64_t by roc * 65535 + seq (where roc is the cycle number), and then store and use the uint64_t value as in https://webrtc.googlesource.com/src/+/refs/heads/main/rtc_base/numerics/sequence_number_unwrapper.h. libwebrtc creates a SeqNumUnwrapper object for every stream which stores the uint64_t value of the last packet. When a new packet arrives, it calculates the distance between the seq of the packet and the last seq in SeqNumUnwrapper by something like our SeqManager::SeqLowerThan to determine which cycle does the packet belongs to.