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
Sign up for a Vultr account: Visit https://www.vultr.com/ and create an account.
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.
Wait for the server to be provisioned: This usually takes a few minutes.
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
(replaceyour_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:
Update the package list:
Bash
Install MediaMTX:
Bash
If you're on a different architecture, like ARM, check the Mediamtx releases for the correct package.
Run MediaMTX:
Bash
To configure Mediamtx to auto-start on boot, you can setup a service.
Bash
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.
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.
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 likemedia.yourdomain.com
) to your Vultr server's IP address. If you're using IPv6, create anAAAA
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
Create a Cloudflare account: Go to https://www.cloudflare.com/ and sign up for a free account.
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.
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.
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.
Enable
proxyProtocol
under thehttp
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.
Install Certbot and Nginx plugin:
Bash
Obtain and install a certificate:
Bash
Replace
yourdomain.com
andwww.yourdomain.com
with your actual domain name. Follow the prompts to configure HTTPS.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
If using iptables
instead, you can use these commands:
Bash
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
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
).Extract the archive: Extract the downloaded ZIP file to a folder of your choice (e.g.,
C:\mediamtx
).Run MediaMTX: Open a Command Prompt or PowerShell window, navigate to the extracted folder, and run:
Bash
Opening Firewall Ports (Windows)
You'll need to allow MediaMTX through the Windows Firewall:
Open Windows Defender Firewall: Search for "Windows Defender Firewall" in the Start menu and open it.
Click on "Advanced settings" on the left sidebar.
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".
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
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
).Extract the archive: Open a Terminal window and use the
tar
command to extract the archive:Bash
(Replace
mediamtx_vX.Y.Z_darwin_amd64.tar.gz
with the actual filename).Move MediaMTX (Optional): You can move the
mediamtx
executable to a more convenient location, like/usr/local/bin/
:Bash
Run MediaMTX: Open a Terminal window and run:
Bash
Opening Firewall Ports (macOS)
macOS has a built-in firewall called pf
. Here's how to open ports:
Edit the
pf
configuration file:Bash
Add the following lines to the end of the file:
Save the file and exit the editor (Ctrl+X, then Y, then Enter).
Load the new
pf
rules:Bash
Enable
pf
:Bash
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:
&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.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.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.
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
&mediamtx
Parameter in VDO.Ninja: A Simple GuideThe &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:
Basic Domain Name:
Format:
&mediamtx=yourdomain.com
What it does: If you provide just a domain name (without
http://
orhttps://
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 tohttps://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).
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 tohttps://stream.live:8889/
.
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 tohttps://yourdomain.com:8890/
.
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 tohttp://yourdomain.com:8889/
.Recommendation: Always prefer HTTPS for security.
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 tohttps://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.
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 tohttp://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
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:
Guest URL: A guest joining a VDO.Ninja room would use a URL like this:
or
Replace
YourRoomName
with your actual room name,yourdomain.com
with your domain name, oryour_server_ip
with your server's IP address. If you haven't set up a domain, just use the IP.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.
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:
or
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 settingsession.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