scottlamb / retina

High-level RTSP multimedia streaming library, in Rust
https://crates.io/crates/retina
Apache License 2.0
220 stars 46 forks source link

UDP reorder buffer #40

Open scottlamb opened 2 years ago

scottlamb commented 2 years ago

I just saw a case for the first time where having a UDP reorder buffer would really help. For some reason, I'm fairly regularly getting packets out of order from my Reolink camera today, eg:

W20211021 11:12:36.908 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:36
I20211021 11:12:39.976 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=6a93 when expecting ssrc=Some(2a0a5257) seq=Some(6a95)
W20211021 11:12:40.127 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:40
I20211021 11:12:40.245 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=6ac0 when expecting ssrc=Some(2a0a5257) seq=Some(6ac2)
W20211021 11:12:40.250 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:40
I20211021 11:12:42.445 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=6bc2 when expecting ssrc=Some(2a0a5257) seq=Some(6bc4)
W20211021 11:12:42.495 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:42
I20211021 11:12:43.310 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=6c25 when expecting ssrc=Some(2a0a5257) seq=Some(6c27)
W20211021 11:12:43.360 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:43
I20211021 11:12:45.604 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=6d50 when expecting ssrc=Some(2a0a5257) seq=Some(6d52)
W20211021 11:12:45.753 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:45
I20211021 11:12:45.996 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=0ad9 when expecting ssrc=Some(1c9421e0) seq=Some(0adb)
W20211021 11:12:46.095 tokio-runtime-worker moonfire_nvr::stream] reolink-sub: lost 1 RTP packets @ 192.168.5.225:6970->192.168.1.121:21924@2021-10-21T11:12:46
I20211021 11:12:46.232 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=6d9f when expecting ssrc=Some(2a0a5257) seq=Some(6da1)
W20211021 11:12:46.241 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:46
I20211021 11:12:46.464 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=6db8 when expecting ssrc=Some(2a0a5257) seq=Some(6dbb)
W20211021 11:12:46.519 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:46
I20211021 11:12:49.625 tokio-runtime-worker retina::client::rtp] Skipping out-of-order seq=6f4b when expecting ssrc=Some(2a0a5257) seq=Some(6f4d)
W20211021 11:12:49.663 tokio-runtime-worker moonfire_nvr::stream] reolink-main: lost 1 RTP packets @ 192.168.5.225:6974->192.168.1.121:45572@2021-10-21T11:12:49

This is on my development workstation, which is on a different network segment than the camera in question. My best guess is that my router is reordering the packets for some reason (rather than the camera or switch or workstation doing so). In any case, it's not that weird of a scenario, so it'd be nice to handle it better.

I think we should have a reorder buffer with parameters for maximum delay (eg 500 ms) and maximum amount to buffer (specified in packets and/or bytes).