load_module "/usr/lib/nginx/modules/ngx_rtmp_module.so";

worker_processes  auto;
#error_log  logs/error.log;

events {
    worker_connections  1024;
}

# RTMP configuration
rtmp {
    server {
		listen 1935; # Listen on standard RTMP port
		chunk_size 4000; 
		# ping 30s;
		# notify_method get;

		# This application is to accept incoming stream
		application live {
			live on; # Allows live input
			push rtmp://localhost:1935/show;
		}

		# This is the HLS application
		application show {
			live on; # Allows live input from above application
            deny play all; # disable consuming the stream from nginx as rtmp

			hls on; # Enable HTTP Live Streaming
			hls_fragment 3;
			hls_playlist_length 10;
			hls_path /mnt/hls/;  # hls fragments path
						
			# MPEG-DASH
            dash on;
            dash_path /mnt/dash/;  # dash fragments path
			dash_fragment 3;
			dash_playlist_length 10;			
		}
	}
}


http {
	include /etc/nginx/sites-enabled/*.conf;
	sendfile off;
	tcp_nopush on;
	directio 512;
	# aio on;
	
	# HTTP server required to serve the player and HLS fragments
	server {
		listen 8080;
		
		# Serve HLS fragments
		location /hls {
			types {
				application/vnd.apple.mpegurl m3u8;
				video/mp2t ts;
			}
			
			root /mnt;

            add_header Cache-Control no-cache; # Disable 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;
			}
		}
		
        # Serve DASH fragments
        location /dash {
            types {
                application/dash+xml mpd;
                video/mp4 mp4;
            }

			root /mnt;
			
			add_header Cache-Control no-cache; # Disable 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;
            }
        }		
		
		# This URL provides RTMP statistics in XML
		location /stat {
			rtmp_stat all;
			rtmp_stat_stylesheet stat.xsl; # Use stat.xsl stylesheet 
		}

		location /stat.xsl {
			# XML stylesheet to view RTMP stats.
			root /usr/share/nginx/html;
		}

	}
}