What’s the deal with Streaming Protocols, and how to pick one for your use case?

Talha
May 31, 2024

Introduction

We deal with streaming media on a daily basis whether it’s a cat video on instagram, live stream of soccer match, latest taylor swift album or a video call to your family. They all use  a streaming protocol to bring the media to your personal devices. Some of content is viewed and discarded in real time (like a video call) and some can be played over and over again (like a YouTube video) Let’s dive into the protocols that made this content delivery possible  to keep us entertained and connected throughout the day.

So what is a streaming protocol?

Suppose you’re watching a movie online. A streaming protocol is like a traffic cop for all the tiny bits of video data that make up the movie. This cop breaks the movie down into smaller, easier to handle pieces and sends them over the internet to your device, in the right order and at the right speed, so you can see the movie smoothly.

Basically, streaming protocols make sure you don’t get a choppy, jerky mess when you watch videos online.

HLS (HTTP Live Streaming)

Imagine you order a giant pizza to share with friends. HLS streaming is like delivering that pizza in slices instead of the whole thing at once.

Here’s the breakdown:

  1. Slice it Up: The video is chopped into short, bite-sized chunks (think video clips).
  2. Label the Boxes: A list (playlist) is created that keeps track of all the slices (video chunks) and their order.
  3. Deliver on Demand: Your device (like a friend) only downloads the slices (video chunks) it needs as you watch.
  4. Adjust for Everyone: HLS can check your internet speed and deliver slices (video chunks) in the best quality depending on your connection (like sending smaller slices if someone has a slow internet).
Pros
  • Everyone speaks HLS: This means you can reach a huge audience without worrying if their device can play your stream.
  • Security matters: HLS keeps your streams safe and secure.
  • Quality you can see: HLS uses fancy tech to deliver smooth, high-quality video.
Cons
  • A bit behind the times: HLS can be laggy, which means there might be a slight delay between what happens on screen and what you actually see. This can be a problem for live events where real-time action is key.
  • Not the easiest to set up: Getting HLS streams going can be a bit trickier than some other options.

So, HLS is great for smooth video playback on (almost) any device, but it might not be the best choice for super live, real-time streaming.

Technically speaking you can use Nginx or Node Media Server to setup a HLS server.

MPEG-DASH

MPEG-DASH was introduced as an alternative to HLS but DASH streaming is like having a build-your-own-pizza bar.

  1. Pick Your Crust: DASH offers video content in various qualities, like different crust thicknesses (think high-resolution or low-resolution).
  2. Pile on the Toppings: Each quality option (crust type) has multiple chunks of video encoded at different bitrates (think bite-sized veggie or meat toppings).
  3. Build Your Slice: A special menu (manifest file) tells your device (like a friendly pizza chef) exactly what toppings are available on each quality crust.
  4. Fresh on Demand: Your device only downloads the specific chunks (toppings) it needs for the quality you choose (like building your pizza slice by slice).
  5.  Adapts to Your Taste: DASH can automatically adjust the quality (crust thickness) and toppings (video chunks) based on your internet speed, just like grabbing a thinner crust if you’re feeling indecisive.
Pros
  • Super Adaptable: Just like HLS, DASH can adjust the video quality based on your internet speed, but it also allows for more customization for different video and audio formats.
  • Open Source Power: Being open source means anyone can tinker with DASH to fit their specific needs.
Cons
  • Not Everyone’s Invited: DASH isn’t compatible with Apple devices, which means a big chunk of viewers might miss out.
  • A Bit Outdated: While once popular, DASH has been overshadowed by newer, more versatile protocols.

In short, MPEG-DASH is a flexible protocol for those who need customization, but its limited compatibility and age hold it back from the top spot.

Nginx and Node Media Server can also handle dash streaming.

RTSP (Real-Time Streaming Protocol)

RTSP is more like a live buffet. RTSP sets up a continuous connection between your device and the video source, like opening a live stream at a restaurant. You can tune in and watch whatever’s currently happening.

Unlike HLS with its bite-sized chunks, RTSP delivers the video continuously, so there’s almost no delay between what’s happening and what you see on your screen. Great for catching live action, like sports or security cameras. While RTSP is good for live stuff, it can be resource-intensive, kind of like a big buffet that might overwhelm some devices with not-so-great internet connections.

Pros
  • Live and in the moment: Perfect for catching real-time action without lag.
  • Keeps it simple: Easier to set up than some other streaming protocols.
Cons
  • Not for picky eaters (devices): Can be demanding on devices with slower connections.
  • Limited audience: Not all devices can play RTSP streams as readily as HLS.

So, RTSP shines for live, real-time streaming where a little technical hurdle is okay for the benefit of no delay.

RTSP stream can be set up using MediaMTX which is a zero-dependency media server.

RTMP (Real-Time Messaging Protocol)

Food analogies were fun while it lasted but let’s use a different analogy for this protocol. Imagine you’re at a live concert, the energy’s electric, and you want to share that experience with friends far away in real-time. RTMP is like having a dedicated courier who rushes those electrifying moments straight to their screens, with minimal delay.

Here’s how it works:

  • Direct Connection: Your encoder  grabs the audio and video and sets up a special, persistent connection with the streaming server, like a dedicated phone line.
  • Data Chunking: That massive stream of audio/video data gets chopped up into manageable packets, ensuring smooth delivery over the internet.
  • Channel Multiplexing: RTMP is like a skilled juggler, sending separate audio and video streams over the same connection but on different channels, so they arrive in perfect sync at your friends’ end.
  • Error Correction: Like a backup singer ready to jump in, RTMP has built-in mechanisms to catch and fix any errors that might occur during transmission, keeping the stream quality high.
  • Player Delivery: The streaming server acts as the stage manager, sending the received data to your friends’ media players, which then reassemble the packets and play the live stream for them to enjoy.
Pros
  • Low Latency: The focus is on real-time delivery, making it perfect for live streams where every second counts (think live gaming or breaking news).
  • Reliable Connections: Persistent connections and error correction keep the stream flowing smoothly.
  • Wide Compatibility: Many streaming platforms and encoders support RTMP, giving you flexibility in your setup.
Cons
  • Firewall Hurdles: RTMP typically uses a specific port (1935) that might be blocked by firewalls, requiring some configuration tweaks.
  • Security Concerns: The basic RTMP protocol itself isn’t encrypted, so for sensitive streams, consider RTMP Secure (RTMPS) for added protection.
  • Player Support: Modern web browsers often don’t have built-in RTMP playback, so additional plugins or player software might be needed.

RTMP remains a solid choice for live streaming, especially when minimal delay is crucial. However, newer protocols like HLS offer advantages in terms of firewall compatibility, built-in security, and broader player support.

Support for RTMP can be found in MediaMTX and Node Media Server.

WebRTC

Imagine WebRTC as a walkie-talkie app on your phone, but built directly into your web browser.

  • Direct communication:<span”> Instead of a central server acting as a middleman, WebRTC allows browsers to connect directly with each other, like two walkie-talkies establishing a connection.
  • Built-in tools: Just like a walkie-talkie uses your phone’s microphone and speaker, WebRTC leverages your device’s camera and microphone to capture and transmit audio and video. No downloads or plugins required.
  • Two-way interaction: WebRTC enables real-time, two-way communication between browsers. You can both see and hear each other, facilitating video conferencing, online gaming, or any situation where live interaction is key.
Pros
  • Low Latency: WebRTC prioritizes real-time delivery, making it ideal for applications like video conferencing, online gaming, and live streaming where even a slight delay can be disruptive.
  • Direct Browser Communication: Unlike some other protocols that rely on a central server, WebRTC allows browsers to connect directly with each other. This direct connection reduces latency and minimizes the need for additional infrastructure.
  • Built-in Functionality: WebRTC leverages your device’s camera and microphone, eliminating the need for separate downloads or plugins. This makes it user-friendly and convenient for quick video calls or chats.
Cons
  • Limited Audience Size: WebRTC is better suited for small group interactions or one-on-one communication. Broadcasting to a large audience can be challenging due to scalability limitations.
  • Bandwidth Demands:  Smooth operation requires good internet connections on both sides.  Devices with slow or unstable connections can experience lag or dropped calls.
  • Security Considerations:  While WebRTC offers secure connections with proper implementation, it’s important to ensure appropriate security measures are in place to safeguard data privacy.ke a walkie-talkie app embedded within your browser, promoting real-time, interactive communication directly between devices.

WebRTC shines in scenarios that demand real-time, interactive communication directly within the browser. Its low latency and built-in features make it ideal for video conferencing, online gaming, and similar applications. However, its limitations in scalability and bandwidth requirements make it less suitable for broadcasting to large audiences. For those situations, other protocols like HLS might offer a better fit.

As WebRTC is P2P protocol it’s doesn’t require a server but an extension called WHIP (WebRTC HTTP Ingest Protocol) can be used to publish stream to server and users can go to a link to view the published stream, this allow WebRTC to be used as general purpose streaming protocol.

MediaMTX supports WHIP and can act as a server for WebRTC streams.

Summary

Exploring an actual Use Case

Let’s say someone’s been drinking your orange juice while you are at work and no one is confessing to the crime. You want to catch the criminal red handed, you can just leave a recording device near the fridge and review it later to catch the criminal but that would have come at the cost of losing more orange juice, so you want the solution to give you real time (or almost real time time) feed before more orange juice is lost. According the comparison table  RTSP and WebRTC might be a good fit for this use case, but which one is better let’s find out
Setup
For this experiment we’ll use a USB camera capable of delivering 1080p video connected to a Raspberry PI 3 B+ which will be sent to a Linux (Ubuntu 20.04) web server having 4 cores and 4 GB RAM. Both the client and server have more than 10 Mbps downloading and uploading speed.
Experiment
There are multiple ways to publish the stream to a web server, for this experiment will be using FFMPEG to publish the stream for RTSP and Google Chrome to publish the stream for WebRTC. A stopwatch will be placed in front of the camera and the difference between the actual stopwatch reading and the stopwatch reading shown in the streamed video will be the metric of performance.
Result
In case of RSTP the delay was of 0.6 seconds. In case of WebRTC the video had delay of little over 2 seconds.

Conclusion

In this case RTSP seems to have an edge over WebRTC to save your precious orange juice but with few optimizations WebRTC could perform on par with RTSP  but will have the added benefit of supporting encryption.

One could argue using a video call service like Google Meet might seem like a handy way to grab a feed from your monitoring device without going into the hassle of setting up  the video streaming service yourself . But those platforms rely on WebRTC for their real-time video calls as well. So In conclusion, there’s always something new to learn when it comes to video streaming!