Persiapan Server Video Streaming
Dalam tutorial kali ini saya akan sedikit share mengenai tutorial membuat server video streaming berbasis NginX – RTMP
- Clone atau download nginx-rtmp module
- git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
- sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
- Download NginX
- wget http://nginx.org/download/nginx-1.10.1.tar.gz
- tar -xf nginx-1.10.1.tar.gz
- cd nginx-1.10.1
- Kompile NginX dengan moduk nginx-rtmp
- ./configure –with-http_ssl_module –add-module=../nginx-rtmp-module
- make -j 1
- sudo make install
- Optional, untuk -j 1 bisa di ganti dengan jumlah CPU untuk accelerate sewaktu melakukan compile.
Membuat file konfigurasi NginX agar bisa support modul rtmp.
rtmp module config
Basic untuk syantec nya : rtmp://nginx_host[:nginx_port]/app_name/stream_name , saya akan menggunakan stream untuk end point nya.
rtmp://localhost/show/stream dan http://localhost:8080/hls/stream.m3u8
Pengalaman saya, untuk HLS yang bagus, bisa menggunakan 3 detik fragment dengan 60 detik playlist.
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; } } }
Catatan, untuk /mnt/hls/ merupakan targt path untuk hls playlist dan file video, kamu bisa merubah ke direktori yang berbeda, tapi kamu harus memastikan bahwa Nginx memiliki akses write ke folder tsb.
http server config
HLS terdiri dari file static, http server bisa di set dengan 2 kondisi, MIME types dan CORS headers
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/; } }
Konvigurasi lengkap NginX
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 /mnt/; } } }
Menjalankan Service NginX
Test konfigurasi NginX
/usr/local/nginx/sbin/nginx -t
Start nginx di background
/usr/local/nginx/sbin/nginx
Start nginx di foreground
//usr/local/nginx/sbin/nginx -g 'daemon off;'