r/howdidtheycodeit • u/nyc13f • Jul 20 '22
How are video messaging applications like FaceTime and Zoom coded?
Curious how video messaging apps are coded and how they are able to stream video in real time overcoming lag and latency.
62
Upvotes
63
u/Formal-Secret-294 Jul 20 '22
Lossy compression of video and audio separately, encoding them, and then transporting the data in small packets through RTP (or TCP) over a SIP connection.
It's a lot to explain really, you gotta dig in yourself. Compression, encoding, payload packaging and sequencing, setting up data streams, connections, handshakes, checksum validation, data loss recovery and compensation....
Computerphile has a few accessible videos to get a basic idea of how a few of those things work. (as I also only have a surface level understanding, but not actually deeply familiar with implementation, I just use libraries and services like everyone else)
https://www.youtube.com/playlist?list=PLzH6n4zXuckrc7uBWvIroTMwtcrk634Nu
https://www.youtube.com/playlist?list=PLzH6n4zXuckpKAj1_88VS-8Z6yn9zX_P6
14
u/nvec ProProgrammer Jul 20 '22
Video codecs can be designed with different objectives and here they're looking for low-latency, so fast encode/decode is most important (ideally with GPU acceleration for hardware which allows it, and a CPU fallback for lower end hardware) with bitrate close behind. Quality doesn't really matter as much. They also want variable bitrate so that on good quality connections it looks nice and clear, and when things slow down and get congested the image looks blurry and blocky but doesn't lag or buffer.
For networking go for UDP rather than TCP as it's faster (although less reliable) but doesn't have all of the error validation. That's fine, we can assemble the frame information ourselves and if a packet is lost and we can't show a frame it'll just get skipped. Again latency over quality.
With just this we can set up a good point-to-point video call, but for group chat they tend to go one step further. Each participant in a call sends their video to a central server as above but there it's either reassembled into a single data feed to send to all participants (similar to a Multiplex/Mux in broadcast TV), or actually all encoded as separate parts of a larger video which can then be broken apart again on the client. This means that you're sending only a single, albeit larger, stream of data to everyone, they'll all get everything in the same order, and if reencoding you can probably get better compression as you're able to fit the central servers with exactly the right type of hardware to accelerate the video you're using- whether GPU racks, or even custom FPGA chips dedicated to the codec you're using.
5
u/[deleted] Jul 20 '22
I know nothing about how facetime and/or zoom is actually implemented but one option and an alternative to having central infrastructure for dealing with all video streams it is also possible with some kind of peer to peer mechanic for sending the video streams direct between the meeting attendees instead and only having central infrastructure for initial handshake and "rooms" / config / auth / contacts / etc etc...
More info: WebRTC
4
u/[deleted] Jul 21 '22
Jitsi is open source zoom clone, you can examine how they did everything
1
u/technologyclassroom Jul 21 '22
Big Blue Button and Jitsi are similar and you can see exactly how they do it.
0
u/megablast Jul 20 '22
Access camera, send stream to server. Boom. Let TCP figure it out.
-2
u/TheChrish Jul 20 '22
So, technically speaking, it isn't something someone who doesn't work at these places knows for sure. If everyone knew, there'd be a lot more competition.
The gist is this: -record video frame on the senders side -compress the video frame to the max -send the compressed frame -the receiver gets the compressed frame and then decompresses it -the receiver then upscales the compressed frame to compensate for losses
The exact details are much more complex and varied. The compression technique can be very varied. Perhaps the frame data is converted to frequency domain, and high frequency data is removed or cut down on (this removes noise and small details). Ai upscaling has allowed for much less bandwidth to be used and with the advent of temporal upscaling (upscaling that uses previous frame data to influence current frame predictions), the need to send repeating data is severely diminished. With many video compression techniques, temporal data is built into the compression technique and an unchanging white background doesn't need to have new information every frame. Bringing this to real time video streaming is a pretty big deal. With these techniques being introduced to video streaming, low bandwidth mediums like cellular data have been able to stream real time video.