Nginx – How to reduce latency with Nginx RTMP streaming server

nginxrtmpubuntu-16.04

My Virtual Server is configured with 3GB memory, and 1 core.

I'm playing the following mp4 file Sample MP4 Video File through my NGINX RTMP server, as small.mp4. I'm experiencing a latency issue.

Here is my nginx.conf

rtmp {
    server {
        listen 1935;
        chunk_size 4000;

        # video on demand for flv files
        application live {
            play /usr/local/nginx/html;
        }

        # video on demand for mp4 files
        application live360 {
            play /usr/local/nginx/html;
        }
    }
}

# HTTP can be used for accessing RTMP stats
http {
    access_log /var/log/nginx/access-streaming.log;
    error_log /var/log/nginx/error-streaming.log;

    server {
        # in case we have another web server on port 80
        listen 8080;

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            # XML stylesheet to view RTMP stats.
            # Copy stat.xsl wherever you want
            # and put the full directory path here
            root /usr/local/nginx/html;
        }

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /tmp/app;
            expires -1;
        }
    }
}    

Best Answer

this is my config, i have a rtmp server and in the same server a nginx with video hls.js library i have a latency of 6 seconds with ffmpeg, i have a latency before of this of 15 seconds

my config

worker_processes  1;


rtmp {
    server {
    listen 1935;

        hls on;
        hls_path /tmp/hls;

        # Use HLS encryption
        #hls_keys on;

        # Store auto-generated keys in this location rather than hls_path
        #hls_key_path /tmp/keys;

        # Prepend key url with this value
        #hls_key_url https://example.com/keys/;

        # Change HLS key every 2 fragments
        #hls_fragments_per_key 2;
        application live {
            live on;
            # Turn on HLS
            hls on;
            hls_path /tmp/live/;
        # Use HLS encryption
        #hls_keys on;

        # Store auto-generated keys in this location rather than hls_path
        #hls_key_path /tmp/keys;

        # Prepend key url with this value
        #hls_key_url https://example.com/keys/;

        # Change HLS key every 2 fragments
        #hls_fragments_per_key 2;
        application live {
            live on;
            # Turn on HLS
            hls on;
            hls_path /tmp/live/;
            hls_fragment 1;
            hls_playlist_length 10;
            # disable consuming the stream from nginx as rtmp
            deny play all;
            hls_continuous on;
        }
    }
}
http {
    server {
    listen 8080;

        location / {
            # Disable cache
            add_header 'Cache-Control' 'no-cache';

            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';

            # allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            types {
                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /tmp/live/;
        }
    }
}

this is the code for ffmpeg

ffmpeg -re -i video.mp4 -c:v libx264 -preset veryfast -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv -maxrate 1.6M rtmp://192.168.1.27/live/key

and the library with the code

<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
  <div class="content">
    <div class="content-logo_ca"></div>
    <div class="content-video">
      <video autoplay="true" controls id="videohls"></video>

    </div>
  </div>
  <script>
    var video = document.getElementById('videohls');
    if(Hls.isSupported()) {
      var hls = new Hls({liveSyncDuration:3});
      hls.loadSource('http://192.168.1.27:8080/key.m3u8');
      hls.attachMedia(video);       
      hls.on(Hls.Events.MANIFEST_PARSED,function() {
       hls.startLoad();
       video.play();
      });
    }
    else if (video.canPlayType('application/vnd.apple.mpegurl')) {
      video.src = 'http://192.168.1.27:8080/key.m3u8';
      video.addEventListener('loadedmetadata',function() {
    hls.autoLevelEnabled = false;
    hls.loadLevel = 3;
        video.play();
      });
    }
    </script> 

and for me working very good