arut / nginx-rtmp-module

NGINX-based Media Streaming Server
http://nginx-rtmp.blogspot.com
BSD 2-Clause "Simplified" License
13.22k stars 3.49k forks source link

HLS_fragment and HLS_playlist not changing #EXT-X-TARGETDURATION #802

Open lightninglu10 opened 8 years ago

lightninglu10 commented 8 years ago

Hey guys, I'm running an adaptive transcoding setup here with HLS. It's weird because my _low, _mid, and _hi.m3u8 files all have a different #EXT-X-TARGETDURATION. For _hi, it's 8s, _mid is 17, and _low is 25. It seems like the HLS_fragment has no effect on this. Anyone have a clue about what's going on?

My config is below:

`worker_processes 4; # 2* # of CPUs

error_log logs/error.log debug;

events { worker_connections 1024; }

user ubuntu;

rtmp { server { listen 1935; allow play all;

    #creates our "live" full-resolution HLS videostream from our incoming encoder stream and tells where to put the HLS video manifest and video fragments
    application live {
        allow play all;
        live on;

        on_publish http://api.staging.internal/api/streaming/access/start_publish/;
        on_publish_done http://api.staging.internal/api/streaming/access/end_publish/;

        sync 300ms; # Max divergence between audio and video tracks
        hls on;
        hls_type live;
        hls_path /data/streams/source;

        hls_fragment_naming system;
        hls_fragment_naming_granularity 1000;
        #hls_fragment_slicing aligned;

        #-preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 256k -c:a copy -b:a 96k -r 10 -f flv rtmp://localhost/hls/$name_low
        #-preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 768k -c:a copy -b:a 128k -r 15 -f flv rtmp://localhost/hls/$name_mid
        #-preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 1024k -c:a copy -b:a copy -r 30 -f flv rtmp://localhost/hls/$name_hi
        #-preset veryfast -threads auto -tune zerolatency -loglevel info -c copy -b copy -f flv rtmp://localhost/hls/$name_source

        exec avconv -i rtmp://localhost/live/$name
    -preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 500k -c:a copy -b:a 96k -r 10 -s 512x288 -f flv rtmp://localhost/hls/$name_low
    -preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 900k -c:a copy -b:a 128k -r 15 -s 848x480 -f flv rtmp://localhost/hls/$name_mid
    -preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 1920k -c:a copy -b:a copy -r 30 -s 1280x720 -f flv rtmp://localhost/hls/$name_hi
    #-preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 4500k -c:a copy -b:a copy -r 30 -s 1920x1080 -f flv rtmp://localhost/hls$name_HD

    ;
    }

    application hls {
        live on;
        hls on;
        hls_type live;
        hls_path /data/streams/hls;
    hls_fragment 1s;
    hls_playlist_length 10s;
        hls_fragment_naming timestamp;

        hls_variant _low BANDWIDTH=460560 RESOLUTION=512x288;
        hls_variant _mid BANDWIDTH=836280 RESOLUTION=848x480;
        hls_variant _hi  BANDWIDTH=2149280 RESOLUTION=1280x720;
        #hls_variant _HD  BANDWIDTH=6221600 RESOLUTION=1920x1080;
}
}

}

http { root /data/streams/hls;

include       mime.types;
default_type  application/octet-stream;

server {
    listen *:80 default_server;

    # fix bug https://jeremyfelt.com/2013/01/08/clear-nginx-cache-in-vagrant/
    sendfile off;
tcp_nopush on;  

    charset     utf-8;
    client_max_body_size 75M;
    access_log   off;
    expires      1d;
    gzip  on;
    gzip_vary on;
    gzip_min_length 10240;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;

    access_log logs/access.log;
    error_log logs/error.log;

    location / {
        # Don't cache our index files
        location ~* \.m3u8$ {
            add_header Cache-Control no-cache;
        }
        add_header Access-Control-Allow-Origin *;
    }
}

}`

misiek08 commented 8 years ago

First disable multiple workers for rtmp. Run two nginx instances if you wonna serve http traffic using multiple cores.

Then (if it's possible) upload or give url of your playlists so we can see fragments. But first disable multiple workers, because rtmp module doesn't support it.

lightninglu10 commented 8 years ago

Oh, I didn't know that. Thanks for the help!

here are some playlists:

8a0acdf3c5b74c5ebd3c7cce8fc30820_hi.txt 8a0acdf3c5b74c5ebd3c7cce8fc30820_low.txt 8a0acdf3c5b74c5ebd3c7cce8fc30820_mid.txt 8a0acdf3c5b74c5ebd3c7cce8fc30820.txt

Do you also have an example of this? So are you saying if I want each transcoding to run on a separate core, then I need to start 3 different nginx configurations, one per core?

lightninglu10 commented 8 years ago

Okay guys, this issue is FIXED. Had nothing to do with # of workers, but I'm still not sure if multiple workers is supported or not.

The issue was that we didn't have a -g flag to signal when we should be grabbing keyframes. From the documentation, what made sense to me that your -g in the exec command HAS to be <= your hls_fragment.

`-preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 500k -c:a copy -b:a 96k -r 10 -s 512x288 -g 2-f flv rtmp://localhost/hls/$name_low

-preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 900k -c:a copy -b:a 128k -r 15 -s 848x480 -g 2 -f flv rtmp://localhost/hls/$name_mid

-preset veryfast -threads auto -tune zerolatency -loglevel info -c:v libx264 -b:v 1920k -c:a copy -b:a copy -r 30 -s 1280x720 -g 2 -f flv rtmp://localhost/hls/$name_hi `

with hls_fragment = 2 fixes it!

misiek08 commented 8 years ago

Multiple workers can seem working, but after a while something will crash and logs won't say much.

lightninglu10 commented 8 years ago

So if I want 4 different video bitrates, should I have 4 nginx configs, each one running one worker?

misiek08 commented 8 years ago

You can stream multiple channels with multiple bitrates on one nginx instance running one worker. Depending on your server, it's network and protocol you use it will handle up to 2500 live viewers easily. If you need more you can use pull (RTMP) or proxy_pass (HLS) and run another instance(s) to handle more. For HLS you can use one http instance with multiple workers, but rtmp requires one worker which will use only one CPU core.

marcodellemarche commented 6 years ago

Hi @lightninglu10 ! I'm sorry, what are the URL you use to access to your .m3u8 files? I'm using your code, but I'm not able to open these hls videos, thank you in advance!

bookercodes commented 5 years ago

@lightninglu10 Thanks for commenting your solution, you helped me a lot !