How to convert RTSP to WebRTC using Docker

RTSP to WebRTC Docker

This guide delves into the process of transcoding an RTSP stream into WebRTC format utilizing Docker. We begin with an overview of both the RTSP and WebRTC protocols, understanding their functionality. Subsequently, we embark on a practical chapter, demonstrating the conversion of an RTSP stream into WebRTC through Docker.

What is RTSP?

The Real Time Streaming Protocol (RTSP) is a network command protocol tailored for managing streaming media servers within entertainment and communications systems. It was developed by RealNetworks, Netscape, and Columbia University around 1996 and is standardized by RFC 2326. RTSP is used to establish and control media sessions between endpoints. Clients can remotely control a video stream from a server, having the ability to play, pause, or stop the media file, much like a remote control.

History of RTSP

Since its inception in the mid-1990s, RTSP has seen widespread adoption in the broadcasting and surveillance industry. As streaming technology has evolved, RTSP has remained a staple protocol due to its robustness and the control it offers over media streaming.

Advantages of RTSP

RTSP offers several benefits:

  • Compatibility with Existing Equipment: Many surveillance and security cameras support RTSP, making it easy to integrate into existing systems.

  • Control Over the Stream: RTSP allows users to have precise control over playback operations, such as play, pause, and seek.

  • Use of Standard Transport Protocols: RTSP can be paired with a variety of transport protocols, such as RTP and UDP, for flexible streaming options.

Disadvantages of RTSP

Despite its advantages, RTSP has some drawbacks:

  • Firewall Issues: RTSP can encounter difficulties with firewall traversal, which can complicate setup and connectivity.

  • Latency: Depending on the network and transport protocols used, RTSP streams can experience higher latency than some modern streaming protocols.

  • Limited Support for Web Integration: RTSP is not natively supported by web browsers, requiring additional software or plugins for playback.
    For further details on embedding an IP camera in a web browser, please refer here.

Applications of RTSP

RTSP is widely used in applications such as IP camera streaming, video surveillance systems, and serving streaming media to clients. It is particularly prevalent in scenarios where control over the stream is required, such as in educational broadcasting or live event streaming.

What is WebRTC

WebRTC is a standard that implements high-speed data transmission through a web browser. In particular, it is used for video chat and web conferencing because it can send and receive large amounts of data, such as audio and video, in real-time. The project was started in 2011 to implement further real-time communications through APIs.

The method of communication of WebRTC is P2P. P2P stands for "Peer to Peer" and means that terminals communicate directly with each other without the need for a server. However, in web conferences with many participants, P2P increases overhead and reduces quality; for this reason, a client/server method is used that solves line problems by sending and receiving data through the server. This seems to have been done.

Advantages of WebRTC

WebRTC has a number of significant advantages:

  • No need to purchase additional hardware. A computer or smartphone is sufficient to use WebRTC. WebRTC is a technology developed by combining existing Internet technologies, so there is no need to purchase new hardware.

  • No other applications are required. WebRTC is a technology that can be implemented in a browser. It works well in all major browsers, such as Chrome, Firefox, Safari and Edge.

  • Minimal latency. WebRTC is the fastest technology available. A delay of less than 0.5 seconds is achieved thanks to the ability to transmit data without going through a server. Normal unidirectional communication uses the TCP protocol. In this protocol, data is transmitted through the server: the flow goes from the client to the server -> the server confirms that the other party has received it. Data is transferred by constantly repeating these steps. WebRTC, on the other hand, uses the UDP protocol. Unlike TCP, it sends data directly from client to client and does not require any processing to start or end communication.

  • High level of security: all connections are protected and encrypted according to DTLS and SRTP protocols. WebRTC only works on HTTPS and the site that uses this technology must be signed with a certificate.

  • Open source code - can be implemented in your product or service.

Disadvantages of WebRTC

Despite the numerous benefits associated with WebRTC, there are instances where employing TCP communication might be more favorable. Let's explore these scenarios:

  • WebRTC uses UDP as a protocol. This simplifies communication, but reduces security. As already mentioned, the TCP protocol involves a series of steps from the time the client starts connecting to the server until the other party confirms that the signal has been received. In other words, availability is guaranteed by the ability to confirm each time whether the other party has received the data. UDP, on the other hand, which sends data continuously, cannot confirm whether the other party has actually received it.

  • All WebRTC solutions are incompatible with each other, as the standard only describes how to transmit video and audio, leaving the developer to implement ways to address subscribers, track their availability, messaging and file sharing, scheduling and more. In other words, it is not possible to make calls from one WebRTC application to another.

  • WebRTC identifies the real IP addresses of users. Neither proxies nor using the Tor network help maintain anonymity. It is possible to hide your IP address using various VPN services and even a TURN server.

Popularity

Today, WebRTC is the second most widespread protocol after the proprietary Zoom protocol, and precedes all other standard protocols (H.323 and SIP) and proprietary ones (Microsoft Teams and Cisco Webex).

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications within lightweight and portable containers. It was created by Solomon Hykes and released by Docker, Inc. in 2013. Docker containers encapsulate an application's software into a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries, and more. This ensures that the application will always run the same, regardless of the environment it is running in.

A Brief History of Docker

Since its introduction in 2013, Docker has become a fundamental tool in modern software development and operations. Its impact on the IT industry has led to widespread adoption in both small development teams and large-scale enterprise environments.

Advantages of Docker

  • Consistency Across Environments: Docker containers ensure that applications work consistently in different development, testing, and production environments.
  • Resource Efficiency: Containers share the host system's kernel and are not burdened with the overhead of associated operating systems, making them more efficient than traditional virtual machines.
  • Isolation: Docker containers can isolate applications, which improves security and reduces conflicts between apps.

Disadvantages of Docker

  • Steep Learning Curve: Docker technology can be complex and may require time to learn and understand properly.
  • Container Management: At scale, managing a large number of containers and ensuring they interact correctly can be challenging.
  • Limited Persistent Storage: Docker containers are ephemeral, which can pose challenges for persisting data across container lifecycles.

Applications of Docker

Docker is widely used for continuous integration/continuous deployment (CI/CD) pipelines, microservices architectures, and development platforms. It simplifies software delivery by allowing developers to create templates called images that can be used to create lightweight, reproducible, and portable application systems.

Setting Up Docker

Follow the instructions below to set up Docker on your system:

Windows Users:

  1. Obtain Docker Desktop for Windows from the Docker official site.
  2. Execute the downloaded installer and adhere to the setup guide.
  3. Open Docker Desktop via the Start menu once installed.

macOS Users:

  1. Acquire Docker Desktop for Mac from the Docker official site.
  2. Access the .dmg file and place the Docker app in the Applications directory.
  3. Initiate Docker from the Applications directory.

Ubuntu 20.04 Users:

  1. Update your system's packages and install curl with the following commands:
    $ sudo apt update & sudo apt upgrade 
    $ sudo apt install apt-transport-https curl
  2. Execute the command to install Docker:
    $ sudo apt install docker.io
  3. Enable and start the Docker service with:
    $ sudo systemctl enable docker 
    $ sudo systemctl start docker

Launch the Server

Launch the Server with Docker

The following runs a Docker container from the bluenviron/mediamtx:latest image. The --rm flag ensures the container is automatically removed after it stops. The--detach option runs the container in the background. Environment variables are set using -e, with MTX_PROTOCOLS set totcp. Two specific ports are exposed: port 8554 on the host is mapped to port 8554 in the container, and port 8889 on the host is mapped to port 8889 in the container.

docker run --rm --detach \
            -e MTX_PROTOCOLS=tcp \
            -p 8554:8554 \
            -p 8889:8889 \
            bluenviron/mediamtx:latest

Launch RTSP video with FFmpeg

The following command uses FFmpeg to stream a video in a loop over RTSP. The -re flag reads the input at the native frame rate. The -stream_loop -1 option loops the video indefinitely. The -i flag specifies the input file. The -c:v libx264 option sets the video codec to libx264, and -bf 0 disables B-frames for lower latency. The -f rtsp flag forces the format to RTSP, and the output is sent to the specified RTSP URL.

ffmpeg -re -stream_loop -1 \
            -i 60sec.mp4 \
            -c:v libx264 -bf 0 \
            -f rtsp rtsp://localhost:8554/stream

You can download a test video from here.

Voila! 🚀 We have finally converted RTSP to webRTC using Docker!
The server is now capturing frames from ffmpeg and can stream them to clients via WebRTC. To view the stream, navigate to http://localhost:8889/stream in your web browser.