HLS Streaming of RTSP Stream by Nginx and Apache Tomcat
In this tutorial, we will learn how to set up Apache Tomcat and Nginx servers to support both HLS and RTSP streaming. Let's get started!
Join the DZone community and get the full member experience.
Join For FreeHello coders!
In my previous article, I tried to explain the basics of HLS.
Today, I am writing this article on how to configure Nginx for RTSP to HLS streams and both Apache 2 and Apache Tomcat for HLS streams.
We have many ways to convert RTSP streams to HLS streams. I have mentioned two ways to convert RTSP to HLS using the Nginx server and Apache server. We will discuss both ways, one-by-one. These two ways are easy and less time-consuming than setting up an environment to convert RTSP to HLS. There are just a few small steps required to configure Nginx and an Apache server.
1. Setting Up an Nginx Server for HLS Live Stream From an RTSP Stream
First of all, Nginx can't enable us to stream from RTSP to direct HLS. First, we need to convert RTSP to RTMP. RTMP streams can be converted into HLS by Nginx.
Required steps:
RTSP to RTMP (configure nginx.conf for RTSP to RTMP conversion by ffmpeg).
Make a pull request to stream RTMP.
RTMP stream becomes an HLS stream (same name as we used in the pull request).
HLS streams from RTSP are accessible in the browser or you can use it in a flash or HTM player.
Required ffmpeg binary.
NOTE: Requires a pull client (like VLC or any other custom player) for RTMP stream.
Example nginx.conf file
Find the RTMP tag in the file and configure that tag as shown below:
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
hls on;
hls_nested on;
hls_path /usr/local/nginx/html/hls/;
hls_fragment 3;
hls_playlist_length 60;
exec_pull /usr/bin/ffmpeg -i rtsp://10.103.0.77:8050/$name -vcodec copy -acodec copy -f flv rtmp://10.103.2.106:1935/live/$name;
}
}
}
Here:
The
ffmpeg
command is used to convert RTSP streams (for example, the local RTSP stream from a camera) to RTMP. The client is required to pull the RTMP stream.hls_path: The path where you want to store the HLS file (m3u8 file).
listen 1935: Listen to this port for the client. For example, you can pull an RTMP stream from VLC by using this port. This is configurable, so we can assign a different port.
hls_fragment: Each .ts file has a 3-second duration to update the m3u8 file for HLS.
Now, we can pull RTMP streams from the Nginx server via port 1935 (listening port).
The below image is the pull stream that streams data from VLC.
Select the network stream and enter the stream as shown below. The stream URL will be the same as that given in the configuration file.
Configure a Dynamic Name for the RTMP Stream
Here, I have used $name
for the dynamic name of the RTMP stream.
Now, you can see the RTMP stream in the VLC player; thus we can see the RTMP stream. The Nginx server starts to generate a m3u8 file on the given hls_path
.
To See the HLS Stream in the Browser
Now, in the bowser, we can see an HLS stream at url: http://server_ip/stream1.
Here, server_ip
is the Nginx server IP where Nginx is installed.
Limitations
The client is required to pull RTMP continuously, so once the client stops pulling, the HLS stream goes offline.
2. Setting Up an Apache 2 Server for HLS Live Stream From RTSP Stream
For Apache 2, only one step is required to set the path where your m3u8 file is generated. You can see an HLS stream via the server IP and the Apache port (given in the httpd.conf file).
In the browser, URL will look like this: http://server_ip:port/file_location/file.m3u8.
Example httpd.conf file
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen *:80
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/Library/WebServer/Documents"
Here DocumentRoot
will be your m3u8 file location.
For Linux, the same configuration will work. It's all about Apache 2 configuration.
2. Setting Up Apache Tomcat Server for HLS Live Stream From RTSP Stream.
In Tomcat 8, we are required to set the HLS path and folder location where m3u8 files will be stored.
The path will be like this: /tomcat8/webapps/ROOT/hls
Upload the HLS file in this location, and apply the filter shown below in the server.xml file. Tomcat will automatically recognize the .m3u8 file folder in the ROOT directory. In the browser, you can see the HLS stream via this URL: http://tomcat_serverIp:port/hls/filename.m3u8.
It's required to allow Apache Tomcat servers to serve cross-browser requests, so I have to use the below filter for HLS configurations called cross-browser filters.
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I hope I have covered all the information related to configuration you were hoping to learn. If you have any questions, please fill free to ask.
Previous article: https://dzone.com/articles/hls-streaming-protocol
Thanks!
Opinions expressed by DZone contributors are their own.
Comments