DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. HLS Streaming of RTSP Stream by Nginx and Apache Tomcat

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!

sagar patel user avatar by
sagar patel
·
Sep. 28, 18 · Tutorial
Like (5)
Save
Tweet
Share
55.68K Views

Join the DZone community and get the full member experience.

Join For Free

Hello 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:

  1. RTSP to RTMP (configure nginx.conf for RTSP to RTMP conversion by ffmpeg).

  2. Make a pull request to stream RTMP.

  3. RTMP stream becomes an HLS stream (same name as we used in the pull request).

  4. HLS streams from RTSP are accessible in the browser or you can use it in a flash or HTM player.

  5. 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.

Image title
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.

Image title

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!

Stream (computing) Apache Tomcat

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Container Security: Don't Let Your Guard Down
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases
  • Real-Time Analytics for IoT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: