Deploy your own Meshcast-like service

A detailed guide on how to deploy MediaMTX, a ready-to-use media server, that offers Meshcast-like functionality

Okay, here is a detailed guide on how to deploy MediaMTX, a ready-to-use SRT / WebRTC / RTSP / RTMP / LL-HLS media server, to use with VDO.Ninja's Meshcast-like functionality. This guide is written for beginners and advanced users alike, aiming to make the setup process as straightforward as possible.

Using MediaMTX with VDO.Ninja for Scalable Broadcasting

This guide will walk you through deploying MediaMTX (formerly rtsp-simple-server) as a self-hosted media server to enhance your VDO.Ninja experience, particularly for large audiences. By using MediaMTX, you can create a setup similar to VDO.Ninja's Meshcast feature, offloading the encoding and distribution of your streams to a dedicated server. This significantly reduces the load on your computer and network, enabling you to host more viewers and maintain a high-quality stream.

Why Use MediaMTX with VDO.Ninja?

VDO.Ninja, by default, uses peer-to-peer connections for video and audio transmission. While this works great for small groups, it can become a bottleneck when dealing with a larger audience. Each viewer adds to the upload bandwidth and encoding burden on the sender's machine.

Meshcast is a feature in VDO.Ninja designed to solve this scalability issue. It acts as an intermediary server (SFU or Selective Forwarding Unit) that receives a single stream from the broadcaster and then redistributes it to multiple viewers. This greatly reduces the load on the broadcaster.

MediaMTX allows you to replicate the functionality of Meshcast using your own server. By integrating MediaMTX with VDO.Ninja, you achieve:

  • Scalability: Handle a large number of viewers without overloading your computer or network.

  • Reduced Load: The broadcaster only needs to encode and upload the stream once to the MediaMTX server.

  • Improved Performance: Viewers receive a smoother, more stable stream as the server handles distribution.

  • Cost Savings: While Meshcast is a free service offered by VDO.Ninja, using your own MediaMTX server can be more cost-effective, especially if you already have a server or prefer using a VPS.

  • Control: You have full control over your media server, including its configuration and security.

  • Flexibility: MediaMTX supports various protocols beyond WebRTC (WHIP/WHEP), including SRT, RTSP, RTMP, and HLS, opening up possibilities for broader integration.

Prerequisites

Before we begin, make sure you have the following:

  • A server or computer: You'll need a machine to host the MediaMTX server. This can be:

    • Local machine (Windows, macOS, or Linux): Suitable for testing or small audiences, but you'll need to configure your router for port forwarding, which is not covered in this tutorial.

    • Virtual Private Server (VPS): Ideal for larger audiences and production environments. Recommended for best performance and reliability.

  • Domain name (Optional but Highly Recommended): While not strictly necessary, a domain name (e.g., yourdomain.com) simplifies configuration and makes your setup more user-friendly. It's also highly recommended for using free SSL certificates (for HTTPS). Using an IP address directly is not optimal, although you can still have VDO.Ninja generate SSL keys for you if you opt to use an IP address only.

  • Basic command-line knowledge: You'll need to be comfortable using the command line or terminal for your operating system.

  • VDO.Ninja: You should be familiar with the basic usage of VDO.Ninja.

Deployment Guide: MediaMTX

This guide will cover the deployment of MediaMTX on:

  • Linux Server (using a VPS like Vultr)

  • Windows

  • macOS

1. Deploying on a Linux Server (VPS)

Choosing a VPS Provider

Several VPS providers offer affordable and reliable services. Some popular options include:

  • Vultr: https://www.vultr.com/ - Offers a wide range of plans and locations, starting at around $2.50/month (IPv6 only) or $3.50/month (IPv4). Their High-Frequency Compute options are recommended for better performance.

  • DigitalOcean: https://www.digitalocean.com/ - Another popular choice with user-friendly interface and good performance.

  • Linode: https://www.linode.com/ - Known for its excellent customer support and robust infrastructure.

  • AWS Lightsail: https://aws.amazon.com/lightsail/ - Amazon's offering, integrating well with other AWS services.

For this guide, we'll use Vultr as an example, but the steps are similar for other providers.

Setting up your Vultr VPS

  1. Sign up for a Vultr account: Visit https://www.vultr.com/ and create an account.

  2. Deploy a new server:

    • Choose Cloud Compute.

    • Select Optimized Cloud Compute for CPU & Storage Technology. High Frequency is the recommended option here.

    • Choose a server location that's geographically close to your target audience for optimal latency.

    • For Server Image, choose a Linux distribution. Ubuntu 22.04 LTS is a good option for its stability and long-term support. Debian 11 or 12 are also popular choices for use with Mediamtx.

    • Select a Server Size based on your expected audience size. For a small to medium-sized audience, the $6/month plan should be sufficient to start.

    • Enable IPv4 if needed, or leave it disabled for IPv6 only.

    • Give your server a Hostname (e.g., mediamtx-server).

    • Click Deploy Now.

  3. Wait for the server to be provisioned: This usually takes a few minutes.

  4. Connect to your server: Once the server is running, Vultr will display its IP address. You'll need to connect to it using SSH:

    • Linux/macOS: Open your terminal and run: ssh root@your_server_ip (replace your_server_ip with the actual IP address).

    • Windows: You can use an SSH client like PuTTY (https://www.putty.org/).

Installing MediaMTX

Once you're connected to your server via SSH, follow these steps:

  1. Update the package list:

    Bash

    sudo apt update
  2. Install MediaMTX:

    Bash

    wget https://github.com/bluenviron/mediamtx/releases/latest/download/mediamtx_linux_amd64.tar.gz
    tar -xf mediamtx_linux_amd64.tar.gz
    sudo mv mediamtx /usr/local/bin/
    sudo mv mediamtx.yml /usr/local/etc/

    If you're on a different architecture, like ARM, check the Mediamtx releases for the correct package.

  3. Run MediaMTX:

    Bash

    mediamtx

    To configure Mediamtx to auto-start on boot, you can setup a service.

    Bash

    sudo curl -s https://raw.githubusercontent.com/bluenviron/mediamtx/main/mediamtx.service --output /etc/systemd/system/mediamtx.service
    sudo systemctl daemon-reload
    sudo systemctl start mediamtx
    sudo systemctl enable mediamtx

Setting up a Domain Name (Optional but Recommended)

If you'd like to avoid using IP addresses directly and enable HTTPS, you'll want to use a domain name.

  1. Purchase a domain name: You can buy affordable domain names from registrars like:

    • Namecheap: https://www.namecheap.com/ - Often has great deals on domains, especially for the first year. You can get domains for as low as $1/year.

    • Porkbun: https://porkbun.com/ - Also offers competitive pricing and user-friendly interface.

  2. Configure DNS records:

    • After purchasing your domain, go to your domain registrar's DNS settings.

    • Create an A record that points your domain (or a subdomain like media.yourdomain.com) to your Vultr server's IP address. If you're using IPv6, create an AAAA record instead.

    • DNS changes can take some time to propagate (up to 48 hours, but usually much faster). You can use tools like https://www.whatsmydns.net/ to check the propagation status.

Enabling HTTPS (Optional but Recommended)

HTTPS is crucial for security and WebRTC compatibility in many browsers. If you only have an IP address available and no domain name, you can set up Mediamtx to generate and serve the SSL keys for you. This is outlined in the Mediamtx documentation.

If you have a domain name, here's how to set up free SSL certificates using Let's Encrypt with Cloudflare:

Using Cloudflare for Free SSL

  1. Create a Cloudflare account: Go to https://www.cloudflare.com/ and sign up for a free account.

  2. Add your domain to Cloudflare: Follow the instructions to add your website to Cloudflare. This usually involves changing your domain's nameservers to Cloudflare's nameservers. You will need to update your domain name's DNS records at your domain registrar, replacing them with the ones provided to you by Cloudflare.

  3. Enable SSL/TLS:

    • In your Cloudflare dashboard, go to the SSL/TLS tab.

    • Choose the Full (strict) encryption mode. This ensures secure communication between your server and Cloudflare, as well as between Cloudflare and your visitors. If this fails, you can try other SSL options available.

  4. Configure your Nginx configuration file to redirect HTTP traffic to HTTPS. Or, you can enable the "Always Use HTTPS" option under SSL/TLS -> Edge Certificates.

  5. Enable proxyProtocol under the http element of your Mediamtx.yml file.

Cloudflare will now automatically issue and renew SSL certificates for your domain.

If you are unable to use Cloudflare, you can also try using CertBot.

  1. Install Certbot and Nginx plugin:

    Bash

    sudo apt install certbot python3-certbot-nginx
  2. Obtain and install a certificate:

    Bash

    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

    Replace yourdomain.com and www.yourdomain.com with your actual domain name. Follow the prompts to configure HTTPS.

  3. Automatic renewal: Certbot automatically sets up a cron job or systemd timer to renew your certificates before they expire.

Opening Firewall Ports

Your VPS likely has a firewall enabled. You need to open the following ports for MediaMTX to work correctly:

  • 8889 (WHEP/WHIP): Used for WebRTC streaming.

  • 8554 (RTSP): Used for RTSP connections.

  • 1935 (RTMP): Used for RTMP connections.

  • 8888 (HLS): Used for HTTP Live Streaming.

  • 80 (HTTP) Used for Let's Encrypt renewals if not using Cloudflare or for general HTTP access.

  • 443 (HTTPS) Used for secure HTTPS access.

Here's how to open these ports using ufw (Uncomplicated Firewall), a common firewall management tool:

Bash

sudo ufw allow 8889/tcp
sudo ufw allow 8554/tcp
sudo ufw allow 1935/tcp
sudo ufw allow 8888/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

If using iptables instead, you can use these commands:

Bash

sudo iptables -I INPUT -p tcp --dport 8889 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 8554 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 1935 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 8888 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT
sudo netfilter-persistent save

If your VPS provider has a built-in firewall, you might need to open these ports in their control panel as well.

2. Deploying on Windows

Installing MediaMTX on Windows

  1. Download MediaMTX: Go to the MediaMTX releases page on GitHub (https://github.com/bluenviron/mediamtx/releases) and download the latest release for Windows (e.g., mediamtx_vX.Y.Z_windows_amd64.zip).

  2. Extract the archive: Extract the downloaded ZIP file to a folder of your choice (e.g., C:\mediamtx).

  3. Run MediaMTX: Open a Command Prompt or PowerShell window, navigate to the extracted folder, and run:

    Bash

    .\mediamtx.exe

Opening Firewall Ports (Windows)

You'll need to allow MediaMTX through the Windows Firewall:

  1. Open Windows Defender Firewall: Search for "Windows Defender Firewall" in the Start menu and open it.

  2. Click on "Advanced settings" on the left sidebar.

  3. Inbound Rules:

    • Click on "Inbound Rules" in the left pane.

    • Click on "New Rule..." in the right pane.

    • Select "Port" and click "Next".

    • Select "TCP" and enter the following ports in "Specific local ports": 8889, 8554, 1935, 8888, 80, 443 (you can add them as a comma-separated list). Click "Next".

    • Choose "Allow the connection" and click "Next".

    • Select the network profiles (Domain, Private, Public) for which you want to apply this rule. For home use, "Private" is usually sufficient. Click "Next".

    • Give the rule a name (e.g., "MediaMTX Ports") and click "Finish".

  4. Outbound Rules: You might also need to create outbound rules if you have a strict firewall configuration. Repeat the steps above, but select "Outbound Rules" instead of "Inbound Rules" in step 3.

Allowing connections through your router's firewall

You may need to manually forward ports 8889, 8554, 1935, 8888, 80, 443 to the IP address of your local PC. You will need to refer to your router's documentation on how to do this, as this process will vary based on make and model.

3. Deploying on macOS

Installing MediaMTX on macOS

  1. Download MediaMTX: Go to the MediaMTX releases page on GitHub (https://github.com/bluenviron/mediamtx/releases) and download the latest release for macOS (e.g., mediamtx_vX.Y.Z_darwin_amd64.tar.gz).

  2. Extract the archive: Open a Terminal window and use the tar command to extract the archive:

    Bash

    tar -xf mediamtx_vX.Y.Z_darwin_amd64.tar.gz

    (Replace mediamtx_vX.Y.Z_darwin_amd64.tar.gz with the actual filename).

  3. Move MediaMTX (Optional): You can move the mediamtx executable to a more convenient location, like /usr/local/bin/:

    Bash

    sudo mv mediamtx /usr/local/bin/
  4. Run MediaMTX: Open a Terminal window and run:

    Bash

    mediamtx

Opening Firewall Ports (macOS)

macOS has a built-in firewall called pf. Here's how to open ports:

  1. Edit the pf configuration file:

    Bash

    sudo nano /etc/pf.conf
  2. Add the following lines to the end of the file:

    pass in proto tcp from any to any port 8889
    pass in proto tcp from any to any port 8554
    pass in proto tcp from any to any port 1935
    pass in proto tcp from any to any port 8888
    pass in proto tcp from any to any port 80
    pass in proto tcp from any to any port 44
  3. Save the file and exit the editor (Ctrl+X, then Y, then Enter).

  4. Load the new pf rules:

    Bash

    sudo pfctl -f /etc/pf.conf
  5. Enable pf:

    Bash

    sudo pfctl -e

Allowing connections through your router's firewall

You may need to manually forward ports 8889, 8554, 1935, 8888, 80, 443 to the IP address of your local PC. You will need to refer to your router's documentation on how to do this, as this process will vary based on make and model.

Integrating MediaMTX with VDO.Ninja

Now that your MediaMTX server is up and running, it's time to integrate it with VDO.Ninja.

Understanding the Logic

VDO.Ninja uses the &mediamtx parameter to connect to your MediaMTX server. Here's how it works:

  1. &mediamtx Parameter: When a guest joins a VDO.Ninja room with the &mediamtx parameter in their URL, VDO.Ninja interprets it as the address of your MediaMTX server.

  2. WHIP Output: VDO.Ninja automatically constructs the WHIP (WebRTC-HTTP ingestion Protocol) endpoint based on the &mediamtx value and the stream ID. The guest's browser will publish their stream to this WHIP endpoint on your MediaMTX server.

  3. WHEP URL Sharing: The guest's browser shares the WHEP (WebRTC-HTTP Egress Protocol) URL of their stream (also hosted on your MediaMTX server) with other participants in the VDO.Ninja room via the data channel.

  4. WHEP Playback: Viewers in the VDO.Ninja room receive the WHEP URL and use it to play the stream from the MediaMTX server instead of directly from the guest via a peer-to-peer connection.

Using the &mediamtx Parameter in VDO.Ninja: A Simple Guide

The &mediamtx parameter is your key to connecting VDO.Ninja with your MediaMTX server, allowing you to create a scalable and efficient streaming setup. Here's how you use it and what you need to know:

What it Does

The &mediamtx parameter tells VDO.Ninja where your MediaMTX server is located. When a guest joins your VDO.Ninja room with this parameter, their browser will send their video and audio stream directly to your MediaMTX server instead of relying solely on peer-to-peer connections. Your MediaMTX server then handles distributing the stream to viewers.

How to Use It

You add the &mediamtx parameter to the end of the VDO.Ninja guest invite link, followed by the address of your MediaMTX server.

Formatting Options

Here's the breakdown of how to format the &mediamtx value:

  1. Basic Domain Name:

    • Format: &mediamtx=yourdomain.com

    • What it does: If you provide just a domain name (without http:// or https:// and without a port number), VDO.Ninja makes the following assumptions:

      • It assumes you want to use HTTPS (secure connection).

      • It assumes your MediaMTX server is running on the standard HTTPS port for WHIP/WHEP, which is 8889.

      • It assumes the top level domain is .com if you do not specify it. &mediamtx=mymediatxserver will be treated as &mediamtx=mymediatxserver.com.

    • Example: &mediamtx=mymediaserver.com will connect to https://mymediaserver.com:8889/.

    • Recommendation: This is the simplest and recommended way if you've set up your domain with HTTPS and are using the default WHIP/WHEP port (8889).

  2. Domain Name with Different Top-Level Domain:

    • Format: &mediamtx=yourdomain.xyz (or any other valid TLD)

    • What it does: Similar to the basic format, but allows you to specify a different top-level domain.

      • Assumes HTTPS.

      • Assumes the standard WHIP/WHEP port 8889.

    • Example: &mediamtx=stream.live will connect to https://stream.live:8889/.

  3. Specifying a Port:

    • Format: &mediamtx=yourdomain.com:port

    • What it does: If your MediaMTX server is running on a port other than 8889 for WHIP/WHEP, you need to specify it.

      • Assumes HTTPS if not specified.

    • Example: &mediamtx=yourdomain.com:8890 will connect to https://yourdomain.com:8890/.

  4. Specifying HTTP or HTTPS:

    • Format: &mediamtx=http://yourdomain.com or &mediamtx=https://yourdomain.com

    • What it does: You can explicitly specify whether to use HTTP or HTTPS.

    • Example: &mediamtx=http://yourdomain.com:8889 will connect to http://yourdomain.com:8889/.

    • Recommendation: Always prefer HTTPS for security.

  5. Using an IP Address:

    • Format: &mediamtx=123.45.67.89 or &mediamtx=123.45.67.89:port

    • What it does: You can use your server's IP address instead of a domain name.

      • Assumes HTTPS and port 8889 if not specified.

    • Example: &mediamtx=192.168.1.100:8890 will connect to https://192.168.1.100:8890/.

    • Note: Using IP addresses directly is generally not recommended for production because you cannot get a typically trusted SSL certificate without a domain name. If you do use an IP address, consider specifying the port, and configuring HTTPS via Mediamtx's automatic SSL generation.

  6. Using localhost (for local testing):

    • Format: &mediamtx=localhost or &mediamtx=localhost:port

    • What it does: If you're testing MediaMTX locally on your own computer, you can use localhost.

      • Assumes HTTP and port 8889 if not specified.

    • Example: &mediamtx=localhost:8890 will connect to http://localhost:8890/.

Important Notes

  • HTTPS is Highly Recommended: Always strive to use HTTPS for secure communication. WebRTC often requires HTTPS in many browsers.

  • Default Port: If you don't specify a port, VDO.Ninja assumes the default WHIP/WHEP port for MediaMTX, which is 8889.

  • Stream ID: VDO.Ninja automatically generates a unique stream ID for each guest. You don't need to specify this in the &mediamtx parameter.

  • Room Name: VDO.Ninja uses the room name as part of the path for WHIP/WHEP on the MediaMTX server.

Summary Table

Format
Assumed Protocol
Assumed Port
Notes

yourdomain.com

HTTPS

8889

Assumes .com TLD. Simplest and recommended if using HTTPS and default port.

yourdomain.xyz

HTTPS

8889

Allows for specifying a different TLD.

yourdomain.com:port

HTTPS

Specified

Use if your MediaMTX WHIP/WHEP server is on a non-default port.

http://yourdomain.com

HTTP

8889

Explicitly uses HTTP. Not recommended for production.

https://yourdomain.com

HTTPS

8889

Explicitly uses HTTPS. Recommended.

123.45.67.89

HTTPS

8889

Uses IP address. Assumes HTTPS and default port. Less ideal for production due to SSL limitations.

123.45.67.89:port

HTTPS

Specified

Uses IP address with a specific port. Consider specifying HTTPS if needed, and configuring SSL via Mediamtx's automatic SSL generation.

localhost

HTTP

8889

For local testing only. Assumes HTTP and default port.

localhost:port

HTTP

Specified

For local testing with a specific port.

By following these guidelines, you can correctly format the &mediamtx parameter and ensure that your VDO.Ninja guests connect seamlessly to your MediaMTX server. Remember that using a domain name with HTTPS is the most secure and user-friendly approach for most scenarios.

Usage Example

Here's a step-by-step example of how to use MediaMTX with VDO.Ninja:

  1. Guest URL: A guest joining a VDO.Ninja room would use a URL like this:

    https://vdo.ninja/?room=YourRoomName&mediamtx=yourdomain.com

    or

    https://vdo.ninja/?room=YourRoomName&mediamtx=your_server_ip:8889

    Replace YourRoomName with your actual room name, yourdomain.com with your domain name, or your_server_ip with your server's IP address. If you haven't set up a domain, just use the IP.

  2. Director's View: The director (or other viewers) can view the stream as usual in the VDO.Ninja room. They don't need to add any special parameters to their URL. VDO.Ninja will automatically handle the playback from the MediaMTX server.

  3. Direct WHEP Playback (Optional): If you want to play the stream directly from the MediaMTX server (outside of VDO.Ninja), you can use the WHEP URL. It will look like this:

    https://yourdomain.com:8889/YourRoomName/YourStreamID/whep

    or

    https://your_server_ip:8889/YourRoomName/YourStreamID/whep

    You can obtain YourStreamID from the VDO.Ninja interface or the URL of the guest's browser window.

    You may need to provide this URL manually if you want to play the stream in a separate player that supports WHEP.

Configuration Tips

  • Audio: If you want to ensure stereo audio, even when using MediaMTX, you might need to use the &stereo parameter in the guest's URL. The code you provided suggests that setting session.stereo=3 might force stereo, but you'll need to confirm this behavior.

  • Stream ID: The streamID in the WHIP/WHEP URLs is automatically generated by VDO.Ninja. It's usually a random string. You can find the stream ID in the guest's VDO.Ninja URL or in the VDO.Ninja interface.

  • Security:

    • HTTPS: Always use HTTPS (SSL) for your MediaMTX server to protect the stream and user data.

    • Authentication: Consider configuring authentication for your MediaMTX server if you need to restrict access to your streams. Refer to the MediaMTX documentation for details on how to set up authentication.

  • Performance:

    • Server Resources: Monitor your server's CPU, memory, and bandwidth usage to ensure it can handle the load. Upgrade your server if necessary.

    • Network Latency: Choose a server location that's geographically close to your audience to minimize latency.

    • Bitrate: Adjust the bitrate of your stream in VDO.Ninja based on your available bandwidth and the desired quality.

  • Troubleshooting:

    • Firewall: Double-check that your firewall (both on the server and your local machine if testing locally) is configured to allow the necessary ports.

    • Domain Propagation: If you're using a domain name, make sure the DNS records have fully propagated.

    • MediaMTX Logs: Check the MediaMTX logs for any errors or warnings.

    • Browser Console: Use your browser's developer console (usually by pressing F12) to look for any errors related to WebRTC or network requests.

Conclusion

Deploying MediaMTX and integrating it with VDO.Ninja provides a powerful and scalable solution for broadcasting to larger audiences. By offloading the stream distribution to a dedicated server, you can significantly improve the performance and stability of your VDO.Ninja streams.

This guide has covered the essential steps for setting up MediaMTX on Linux, Windows, and macOS, including VPS deployment, domain name configuration, HTTPS setup, and firewall configuration. It has also explained how to use the &mediamtx parameter in VDO.Ninja to connect to your MediaMTX server and leverage its capabilities.

Remember to tailor the configuration to your specific needs and environment. Monitor your server's performance and adjust settings as necessary to ensure a smooth and high-quality streaming experience for your viewers. With a little bit of setup, you can take your VDO.Ninja broadcasts to the next level with the power of MediaMTX!

Last updated