2020-05-08 - RTSP: TCP vs. UDP

https://upload.wikimedia.org/wikipedia/commons/5/5c/Axis_214_PTZ_Camera.jpg

what would you recommend for video streaming: TCP or UDP? most ppl would say: UDP! i used to be on that side as well. how i changed?

for some time now i use a couple RTSP-based cameras to monitor different things. i did over time noticed that they loose the ability to connect to their streams every now and then. at first i was convinced this is just a bug of the camera and just reset it from time to time – it always helped. one day however i've noticed that it does happen when i connect and disconnect often from my PC, but not if i do this from my tablet. this made me thinking… and experimenting…

it turned out that the… hmmm… “problem” (?) was my firewall. i tend to keep rules very restrictive. when package is unexpected/unwanted/malformed – it is simply dropped. there is no return message to the sender. now what happens with RTSP stream in such a case? camera just keeps sending data to the endpoint, that's firewall is already discarding all of it. socket is closed, but in case of UDP it does not effect in sending out any messages and since firewall makes port silent, there is basically no feedback to the camera, that other end is no longer listening. cameras themselves have limits, too – they cannot send infinite number of packages. so there is a cutoff point – after N “silently dead” clients are “connected”, camera stops accepting new requests.

tablet did not have this “problem”, because firewall was far more permissive (absent?) there, and UDP to a closed port immediately resulted in a “destination unreachable” ICMP so the camera knew connection was dropped and it can forget about this client. thus the pool was never exhausted.

i did not considered making firewall permissive to be an option. it however turned out, that switching to TCP stream was a perfect choice here – closing TCP socket does send out FIN package, thus other end is informed. whatever “magical” happens so that FIN never reaches its destination, it is still not a problem, as TCP packages requires ACKs and there are timeouts – if there is no ACK within a given time, after some number of retransmissions, connection will be dropped locally, on the camera, effectively freeing up resources as well.

what are the caveats? well – this whole thing i wrote is happening on my LAN. transmitting streams this way over a slow and/or lossy network would be a no-go. so, as usual – there is no golden hammer here. just pick the right tool for the job!

blog/2020/05/08/2020-05-08_-_rtsp_tcp_vs._udp.txt · Last modified: 2021/06/15 20:09 by 127.0.0.1
Back to top
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0