WebRTC using NGINX - nginx

I would like to show a RTSP stream in a Web application. I successfully streamed using HLS with the following configuration:
RTSP to RTMP:
ffmpeg -stream_loop -1 -re -i "C:\RA\test.m3u8" -acodec aac -vcodec libx264 -f flv rtmp:localhost/live/stream
Nginx (1.7 on windows) configuration
# RTMP configuration
rtmp {
server {
listen 1935;
application live {
live on;
interleave on;
hls on;
hls_path C:\RA\NGINX\hls;
hls_fragment 15s;
}
}
}
http {
sendfile off;
tcp_nopush on;
directio 512;
default_type application/octet-stream;
server {
listen 81;
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 C:\RA\NGINX;
}
}
}
I cannot find the documentation or examples related to the WebRTC configuration for NGINX
I tried with the socket connection but I don't know how to defined my input stream or what would be the URL to test this...
# RTMP configuration
rtmp {
server {
listen 1935;
application live {
live on;
}
}
}
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server localhost:8010;
}
server {
listen 8020;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}
}
}

Related

connect() failed (110: Connection timed out) while connecting to upstream

I have configured nginx setup like below
Nginx running as front end reverse proxy and routing live traffic to couple of upstream servers onprem and aws (Refer nginx site conf below). Here we trying to mirror the original On-Prem hosted application traffic to AWS hosted application which we using it as pre-production testing before completely migrate application to AWS cloud.
We noticed below things,
All original live traffic routed to onprem upstream server which is running on localhost:10200
We see few request failed with connection timeout while mirroring to aws upstream server which is running in AWS cloud
I am getting below error in nginx error log
connect() failed (110: Connection timed out) while connecting to
upstream
upstream onprem {
server localhost:10200;
keepalive 1024;
}
upstream aws {
server <myserver-name>:443;
keepalive 1024;
}
server {
server_name _;
listen 80 default_server;
client_header_buffer_size 16k;
client_header_timeout 900s;
client_max_body_size 100M;
client_body_buffer_size 100M;
client_body_timeout 900s;
proxy_connect_timeout 200s;
proxy_send_timeout 1200s;
proxy_read_timeout 1200s;
send_timeout 1200s;
proxy_buffers 16 128k;
proxy_buffer_size 16k;
proxy_redirect off;
proxy_busy_buffers_size 256k;
port_in_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
location / {
mirror /mirror;
proxy_pass http://onprem;
}
location = /mirror {
add_header 'Access-Control-Allow-Origin' 'https://myserver-name';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
internal;
set $delimiter "?";
if ($is_args) {
set $delimiter "&";
}
#log_subrequest on;
set $flag "${delimiter}useTest=true";
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'https://myserver-name';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
proxy_ssl_server_name on;
set $new_uri $request_uri;
if ($request_uri ~ "^/ingest(.*)$") {
set $new_uri /test$1;
}
proxy_pass https://aws$new_uri$flag;
}
}

Nginx-Rtmp not able to set latency

I'm using nginx-rtmp module to capture RTMP streams from my cameras and transmit them to further systems.
Generally my setup is working fine and generating .m3u8 files properly. For example:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:23
#EXT-X-TARGETDURATION:50
#EXT-X-DISCONTINUITY
#EXTINF:49.957,
verification-23.ts
But the fragments that are generated are always 50s as you can see on the above example.
This is causing more than 50s delay in playback that I would like to avoid.
I tried to decrease Nginx parameters to, for example 3s for fragment and 15s for playlist:
hls_fragment 3s;
hls_playlist_length 15s;
but it didn't work. .m3u8 files were still generated with #EXTINF:49.957.
Is it possible to decrease the delay with Nginx configuration? Or it might be some kind of additional configuration on the cameras?
My current nginx.conf file:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
rtmp {
server {
listen 1935;
chunk_size 2000;
application live {
live on;
hls on;
hls_path /tmp/hls;
hls_fragment 50s;
hls_playlist_length 60s;
deny play all;
}
}
}
http {
sendfile off;
tcp_nopush on;
directio 512;
default_type application/octet-stream;
server {
server_name stream.mydomain.pl;
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;
}
# Certbot configuration
# ...
}

Streaming via nginx and hls not working fully

so I've configured my nginx server on azure to the point that i can in fact connect to it and stream but there is one issue i don't have the .m3u3 file in my hls directory, I'm streaming to this server via obs here is my nginx config
I found some old thread with this error but i've added user root; to file and still nothing
anyone know why it's not working
user root;
worker_processes auto; events {
worker_connections 1024;
}
# RTMP configuration
rtmp {
server {
listen 1935 ; # Listen on standard RTMP port
chunk_size 4000;
application show {
live on;
# Turn on HLS
hls on;
hls_path /mnt/hls/;
hls_fragment 3;
hls_playlist_length 60;
# disable consuming the stream from nginx as rtmp
deny play all;
}
}
}
http {
sendfile off;
tcp_nopush on;
# aio on;
directio 512;
default_type application/octet-stream;
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 /nginx/;
}
}
}
this is my nginx.conf using Ubuntu 18.04../usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
application live {
live on;
interleave on;
record off;
hls on;
hls_path /mnt/hls;
hls_fragment 6s;
hls_playlist_length 60s;
deny play all;
dash on;
dash_path /mnt/dash;
dash_fragment 3s;
}
}
}
http {
sendfile off;
tcp_nopush on;
directio 512;
default_type application/octet-stream;
server {
listen 8080;
location / {
add_header 'Cache-Control' 'no-cache';
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
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/vnd.apple.mpegurl m3u8;
video/mp2t ts;
text/html html;
application/dash+xml mpd;
}
root /mnt/;
}
}
}
OBS Stream setting :
server add : rtmp://[ip-addr]/live
stream key : stream
i've tried live stream using android (larix broadcaster app), with this connection setting,
server add : rtmp//{ip-addr]:1935/live/stream
stram key : stream
and at my website, i've added playerjs.js and use this inside index.html
<section class="wrapper style1">
<div class="inner">
<div class="video">
<div class="video-wrapper">
<iframe width="100%" height="100%" src="playerjs.html?file=http://[ip-addr]:8080/hls/stream.m3u8" type="text/html" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
</section>
i'm running my server with apache2 on port:80, php, mySql..while Nginx on port:8080 to serve livestream HLS
.m3u8 will generated automaticly inside /mnt/hls
Application name define inside rtmp ..>> application live (i named it "live"), you may named it you like..while stream key, you define by yourself via streamer (OBS, Larix etc...) >> rtmp:/{ip-addr]:1935/live/stream ( i named "stream"). nginx will pull that stream name and put inside mnt/hls/ and create .m3u8 file, devide clips into 60s playlist, and load every clips by 6s fragment..
thats why i like nginx :) and still going smooth .. hope this would help

Nginx RTMP Module is not saving live-streaming files

I have had no issues in live-streaming to the server, but I am trying to add VOD functionality with some difficulty. The record_path "/mnt/film/" is not being created or written to by the server, even if I create the directory before running the server.
I have tried adding a user with root permissions, but this did not seem to help. The weird thing is that error logs are not being created as well, but it writes to the "/mnt/hls" path with no problems whatsoever.
user nginx root;
worker_processes auto;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
# RTMP configuration
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
# Turn on HLS
hls on;
hls_path /mnt/hls/;
hls_fragment 3;
hls_playlist_length 60;
# record stream to folder /film/
record all;
record_path /mnt/film/;
# disable consuming the stream from nginx as rtmp
deny play all;
}
}
}
http {
sendfile off;
tcp_nopush on;
default_type application/octet-stream;
server {
listen 8080;
location /hls {
# 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/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /mnt/;
}
}
server {
listen 8384;
# vod caches
vod_metadata_cache metadata_cache 256m;
vod_response_cache response_cache 128m;
# vod settings
vod_mode local;
vod_segment_duration 2000; # 2s
vod_align_segments_to_key_frames on;
#file handle caching / aio
open_file_cache max=1000 inactive=5m;
open_file_cache_valid 2m;
open_file_cache_min_uses 1;
open_file_cache_errors on;
aio on;
location /vod/ {
alias mnt/film/;
vod hls;
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Expose-Headers' 'Server,range,Content-Length,Content-Range';
add_header 'Access-Control-Allow-Methods' 'GET, HEAD, OPTIONS';
add_header 'Access-Control-Allow-Origin' '*';
expires 100d;
}
}
}
I am just trying to get some files to show up in the "record_path" directory defined in the nginx.conf above.
Thanks
Have you checked hls_cleanup directive ? By default the feature is on. In this mode nginx cache manager process removes old HLS fragments and playlists from HLS directory.
https://github.com/arut/nginx-rtmp-module/wiki/Directives#hls_cleanup

How to make streaming MP4 from reverse proxy?

I try to make little youtube. I have two servers.
Backend server Ubuntu16. Server has application + mp4 files.
Django + uWSGI +
NGINX
user www-data;
worker_processes auto;
pid /etc/nginx/logs/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name XXX.XXX.XXX.191;
charset utf-8;
gzip on;
gzip_static on;
gzip_comp_level 5;
gzip_min_length 1024;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if_modified_since off;
add_header Accept-Ranges bytes;
location /static/ {
root /home/tube;
}
location / {
uwsgi_pass django;
include uwsgi_params;
}
}
}
When I load page with video from browser using http://www.backend/, I see:
1.mp4 - answer 206 - chunk 2mb. After I click to play, the video starts to play chunk by chunk with answer 206. Seems to be correct. This is backend server (it has public access )
Frontend server Ubuntu16. NGINX as reverse proxy.
user www-data;
worker_processes auto;
pid /etc/nginx/logs/nginx.pid;
events {worker_connections 1024;}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name XXX.XXX.XXX.92;
proxy_http_version 1.1;
proxy_pass_request_headers on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Range $http_range;
proxy_set_header If-Range $http_if_range;
proxy_no_cache $http_range $http_if_range;
location / {
proxy_pass http://XXX.XXX.XXX.191/;
}
}
}
When I load the same page with video from browser using http://www.frontend/
I see:
1.mp4 - answer 200 - preload starts until full lenght of video (~150mb), instead 2mb chunk. After preload finished, I can click play and video starts streaming, loading mp4 again (answer 200)
I want that frontend server streaming videos correctly. First chunk 2-4mb after I load html, the rest chunks after i click play.

Resources