'url', 'hdmi' => [ 'resolution' => '1920x1080', 'audio_source' => 'hw:1,0', 'framerate' => '30' ], 'url' => 'https://cdn.urmic.org/unavailable.mp4', 'rtmp' => [ 'mount' => 'channel_name', 'password' => 'live', ], 'srt' => [ 'stream_id_1' => 'forever', 'stream_id_2' => 'steaming', 'stream_id_3' => 'partner', ], 'udp' => 'udp://@224.1.1.1:8000', 'custom' => '', ]; $jsonFile = __DIR__ . '/input.json'; if (file_exists($jsonFile)) { $raw = file_get_contents($jsonFile); $data = json_decode($raw, true); if (!is_array($data)) $data = $defaults; } $input_source = $data['input']; $input_rtmp_mount = $data['rtmp']['mount']; $input_rtmp_pass = $data['rtmp']['password']; $srt_pass1 = $data['srt']['stream_id_1']; $srt_pass2 = $data['srt']['stream_id_2']; $srt_pass3 = $data['srt']['stream_id_3']; switch ($input_source) { case "hdmi": $input = "ffmpeg -hide_banner -f v4l2 -thread_queue_size 512 -input_format mjpeg -framerate " . $data['hdmi']['framerate'] . " -video_size " . $data['hdmi']['resolution'] . " -i /dev/video0 " . "-f alsa -i " . $data['hdmi']['audio_source']; break; case "url": $input .= "ffmpeg -hide_banner -stream_loop -1 -re -i " . $data['url']; break; case "udp": $input .= 'ffmpeg -hide_banner -stream_loop -1 -re -i "' . $data['udp']; break; case "rtmp": $input .= "ffmpeg -hide_banner -stream_loop -1 -re -i rtmp://127.0.0.1:1935/" . $$input_rtmp_mount . "/" . $input_rtmp_pass; break; case "srt": $input .= "ffmpeg -hide_banner -stream_loop -1 -re -i srt://127.0.0.1:1937/shree/bhatt/" . $srt_pass3; break; } $input .= " "; $jsonFile = __DIR__ . '/output.json'; $defaults = [ 'service_display' => 'disable', 'service_rtmp0_multiple' => 'disable', 'service_rtmp0_hls' => 'disable', 'service_rtmp0_dash' => 'disable', 'service_rtmp1_multiple' => 'disable', 'service_rtmp1_hls' => 'disable', 'service_rtmp1_dash' => 'disable', 'service_udp0' => 'disable', 'service_udp1' => 'disable', 'service_udp2' => 'disable', 'service_srt_multiple' => 'disable', 'service_custom' => 'disable', 'rtmp_multiple0' => [], 'rtmp_multiple1' => [], 'srt_multiple' => [], 'rtmp0' => [ 'resolution' => '1920x1080', 'data_rate' => '6M', 'framerate' => '30', 'gop' => '30', 'extra' => '', 'audio_data_rate' => '128k', 'audio_db_gain' => '0dB', 'audio_sample_rate' => '48000' ], 'rtmp1' => [ 'resolution' => '720x576', 'data_rate' => '1.5M', 'framerate' => '25', 'gop' => '25', 'extra' => '', 'audio_data_rate' => '96k', 'audio_db_gain' => '0dB', 'audio_sample_rate' => '48000' ], 'udp0' => [ 'udp' => '', 'formate' => '', 'resolution' => '', 'data_rate' => '', 'framerate' => '', 'gop' => '', 'extra' => '', 'audio_formate' => '', 'audio_data_rate' => '', 'audio_db_gain' => '', 'audio_sample_rate' => '' ], 'udp1' => [ 'udp' => '', 'formate' => '', 'resolution' => '', 'data_rate' => '', 'framerate' => '', 'gop' => '', 'extra' => '', 'audio_formate' => '', 'audio_data_rate' => '', 'audio_db_gain' => '', 'audio_sample_rate' => '' ], 'udp2' => [ 'udp' => '', 'formate' => '', 'resolution' => '', 'data_rate' => '', 'framerate' => '', 'gop' => '', 'extra' => '', 'audio_formate' => '', 'audio_data_rate' => '', 'audio_db_gain' => '', 'audio_sample_rate' => '' ], 'srt0' => [ 'formate' => '', 'resolution' => '', 'data_rate' => '', 'framerate' => '', 'gop' => '', 'extra' => '', 'audio_formate' => '', 'audio_data_rate' => '', 'audio_db_gain' => '', 'audio_sample_rate' => '' ], 'display' => '1920x1080@60.00', 'display_audio' => '0,3', 'custom_output' => '' ]; for ($i = 1; $i <= 11; $i++) { $defaults['rtmp_multiple0'][$i] = ['url' => '', 'name' => '', 'enabled' => false]; $defaults['rtmp_multiple1'][$i] = ['url' => '', 'name' => '', 'enabled' => false]; $defaults['srt_multiple'][$i] = ['url' => '', 'name' => '', 'enabled' => false]; } if (file_exists($jsonFile)) { $raw = file_get_contents($jsonFile); $data = json_decode($raw, true); if (!is_array($data)) $data = $defaults; $data = array_replace_recursive($defaults, $data); } else { $data = $defaults; } $service_display = $data['service_display']; $service_rtmp0_multiple = $data['service_rtmp0_multiple']; $service_rtmp0_hls = $data['service_rtmp0_hls']; $service_rtmp0_dash = $data['service_rtmp0_dash']; $service_rtmp1_multiple = $data['service_rtmp1_multiple']; $service_rtmp1_hls = $data['service_rtmp1_hls']; $service_rtmp1_dash = $data['service_rtmp1_dash']; $service_udp0 = $data['service_udp0']; $service_udp1 = $data['service_udp1']; $service_udp2 = $data['service_udp2']; $service_custom = $data['service_custom']; $service_srt_multiple = $data['service_srt_multiple']; $rtmp_multiple0 = $data['rtmp_multiple0']; $rtmp_multiple1 = $data['rtmp_multiple1']; $srt_multiple = $data['srt_multiple']; switch ($which_service) { case 'input': $input .= " -c:v copy -c:a copy -f mpegts udp://@239.255.254.254:39000?localaddr=127.0.0.1"; $service = $input; $file = "/var/www/encoder-main.sh"; if (file_put_contents($file, $service) !== false) { echo "File saved."; } else { echo "Error writing file."; } if ($service_display === "enable") { exec('sudo systemctl restart encoder-main'); } break; case 'display'; break; case 'rtmp0'; case 'rtmp1'; if ($service_rtmp0_hls === "enable") { $hls0 = " hls on; hls_path /var/www/html/hls/shree; hls_fragment 3; hls_playlist_length 60; "; } else { $hls0 = " "; } if ($service_rtmp0_dash === "enable") { $dash0 = " dash on; dash_path /var/www/html/dash/shree; "; } else { $dash0 = " "; } if ($service_rtmp1_hls === "enable") { $hls1 = " hls on; hls_path /var/www/html/hls/shreeshree; hls_fragment 3; hls_playlist_length 60; "; } else { $hls1 = " "; } if ($service_rtmp1_dash === "enable") { $dash1 = " dash on; dash_path /var/www/html/dash/shreeshree; "; } else { $dash1 = " "; } $rtmp_push0 = ""; for ($i = 1; $i <= 11; $i++) { if ($rtmp_multiple0[$i]['enabled'] == 1) { $rtmp_push0 .= " push " . $rtmp_multiple0[$i]['url'] . ";"; } } $rtmp_push1 = ""; for ($i = 1; $i <= 11; $i++) { if ($rtmp_multiple1[$i]['enabled'] == 1) { $rtmp_push0 .= " push " . $rtmp_multiple1[$i]['url'] . ";"; } } $nginx = " user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 2048; multi_accept on; } rtmp { server { listen 1935; chunk_size 4096; "; if ($input_source === "rtmp") { $nginx .= " application " . $input_rtmp_mount . " { live on; record off; meta off; wait_video on; } "; } $nginx .= " application shree { live on; record off; meta off; wait_video on; " . $hls0 . " " . $dash0 . " " . $rtmp_push0 . " } application shreeshree { live on; record off; meta off; wait_video on; " . $hls1 . " " . $dash1 . " " . $rtmp_push1 . " } } }"; $nginx .= " http { sendfile on; tcp_nopush on; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log warn; gzip on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } "; $file = "/var/www/nginx.conf"; file_put_contents($file, $nginx); exec('sudo cp /var/www/nginx.conf /etc/nginx/nginx.conf'); exec("sudo nginx -t 2>&1", $output, $status); if ($status === 0) { exec("sudo systemctl restart nginx 2>&1", $o, $s); } else { exec('sudo cp /var/www/default_nginx.conf.conf /etc/nginx/nginx.conf'); exec("sudo systemctl restart nginx"); } if ($service_rtmp0_multiple === "enable") { $rtmp = 'ffmpeg -hide_banner -fflags nobuffer -i "udp://@239.255.254.254:39000?fifo_size=5000000&overrun_nonfatal=1" ' .' -c:v h264 ' .' -vf "scale="'.str_replace("x",":",$data['rtmp0']['resolution']) .'" -b:v '.$data['rtmp0']['data_rate'] .' -maxrate '.$data['rtmp0']['data_rate'] .' -bufsize '.$data['rtmp0']['data_rate'] .' -r '.$data['rtmp0']['framerate'] .' -g '.$data['rtmp0']['gop'] .' -c:a aac -b:a '.$data['rtmp0']['audio_data_rate'] .' '.$data['rtmp0']['audio_db_gain'] .' -ar '.$data['rtmp0']['audio_sample_rate'] .' '.$data['rtmp0']['extra'] .' -f flv rtmp://127.0.0.1/shree/bhattji'; $file = "/var/www/encoder-rtmp0.sh"; file_put_contents($file, $rtmp); exec('sudo systemctl enable encoder-rtmp0'); exec('sudo systemctl restart encoder-rtmp0'); } else { exec('sudo systemctl stop encoder-rtmp0'); exec('sudo systemctl disable encoder-rtmp0'); } if ($service_rtmp0_multiple === "enable") { $rtmp = 'ffmpeg -hide_banner -fflags nobuffer -i "udp://@239.255.254.254:39000?fifo_size=5000000&overrun_nonfatal=1" ' .' -c:v h264_qsv ' .' -vf "scale="'.str_replace("x",":",$data['rtmp1']['resolution']) .'" -b:v '.$data['rtmp1']['data_rate'] .' -maxrate '.$data['rtmp1']['data_rate'] .' -bufsize '.$data['rtmp1']['data_rate'] .' -r '.$data['rtmp1']['framerate'] .' -g '.$data['rtmp1']['gop'] .' -c:a aac -b:a '.$data['rtmp1']['audio_data_rate'] .' '.$data['rtmp1']['audio_db_gain'] .' -ar '.$data['rtmp1']['audio_sample_rate'] .' '.$data['rtmp1']['extra'] .' -f flv rtmp://127.0.0.1/shreeshree/bhattji'; $file = "/var/www/encoder-rtmp1.sh"; file_put_contents($file, $rtmp); exec('sudo systemctl enable encoder-rtmp1'); exec('sudo systemctl restart encoder-rtmp1'); } else { exec('sudo systemctl stop encoder-rtmp1'); exec('sudo systemctl disable encoder-rtmp1'); } break; case "srt"; if ($service_srt_multiple) { $srt_push = ""; for ($i = 1; $i <= 11; $i++) { if ($srt_multiple[$i]['enabled'] == 1) { $srt_push .= " " . $srt_multiple[$i]['url']; } } $sls = " srt { worker_threads 64; worker_connections 500; log_file /tmp/logs/error.log ; log_level info; server { listen 1937; latency 1000; #ms domain_player shree; domain_publisher " . $srt_pass1 . " ; backlog 100; idle_streams_timeout 10; app { app_player bhatt ; app_publisher " . $srt_pass2 . " ; record_hls off; record_hls_segment_duration 10; relay { type push; mode all; #all; hash reconnect_interval 10; idle_streams_timeout -1; upstreams " . $srt_push . " ; } } } } "; $service = 'ffmpeg -fflags nobuffer -i udp://@239.255.254.254:39000 -c copy -f mpeg srt://127.0.0.1/' . $srt_pass1 . '/' . $srt_pass2 . '/ji'; $file = "/var/www/encoder-srt.sh"; file_put_contents($file, $service); $file = "/var/www/sls.conf"; file_put_contents($file, $sls); exec('sudo systemctl enable srt'); exec('sudo systemctl restart srt'); exec('sudo systemctl enable encoder-srt'); exec('sudo systemctl restart encoder-srt'); } else { exec('sudo systemctl disable srt'); exec('sudo systemctl stop srt'); exec('sudo systemctl disable encoder-srt'); exec('sudo systemctl stop encoder-srt'); } break; case "udp"; break; case "custom"; break; default: error_log("Error no input found"); break; } } function update_firewall() {} function update_network() {} function update_firmware() {}