From dd3f07d4ba7f648586541a9d2312bd103a2984fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Fri, 23 Oct 2020 01:49:33 +0200
Subject: [PATCH] Rizeni nahravani parametrem v rtmp push url

---
 lib/PiTube/Controller/Nginx.pm | 22 +++++++++++++-
 nginx.conf                     | 53 ++++++++++++++++------------------
 pi_tube.conf                   |  3 ++
 3 files changed, 49 insertions(+), 29 deletions(-)

diff --git a/lib/PiTube/Controller/Nginx.pm b/lib/PiTube/Controller/Nginx.pm
index ae5416b..7316223 100644
--- a/lib/PiTube/Controller/Nginx.pm
+++ b/lib/PiTube/Controller/Nginx.pm
@@ -1,5 +1,7 @@
 package PiTube::Controller::Nginx;
 use Mojo::Base 'Mojolicious::Controller';
+use Mojo::UserAgent;
+use Mojo::IOLoop;
 
 use constant HLS => qr/^\/hls\/([a-z0-9\-]+)(_\w+)?(\/\w+)?\.(m3u8|ts)$/i;
 
@@ -7,10 +9,11 @@ sub callback_rtmp {
     my $c = shift;
 
     if ( $c->param('call') =~ /publish/ ) {
+        my $name = $c->param('name');
 
         # stream
         my $stream = $c->schema->resultset('Stream')->find({
-            key => $c->param('name')
+            key => $name,
         });
         $c->render( status => 404, text => '' ), return if ! $stream;
 
@@ -20,15 +23,32 @@ sub callback_rtmp {
         });
         $c->render( status => 403, text => ''), return if ! $user;
 
+        # TODO: autorizace user/stream
+
         # aktualizace stavu streamu
         $stream->update({
             publish_last     => '\now()',
             publish_user_id  => $user->id,
             publish_time     => ( $c->param('time') // 0 ),
         });
+
+        # zapnuti nahravani
+        if ( $c->param('record') && $c->param('record') =~ /(all|audio)/) {
+            my $record = $1;
+            my $ua = Mojo::UserAgent->new;
+
+            Mojo::IOLoop->timer(1 => sub ($loop) {
+                my $res = $ua->get(
+                    $c->config->{nginx}{base_url}
+                    . "/control/record/start?app=stream&name=$name&rec=$record"
+                );
+            });
+        }
+
     }
 
     $c->render( status => 204, text => '' );
+
 }
 
 sub callback_hls {
diff --git a/nginx.conf b/nginx.conf
index 1f7fb81..96c5846 100644
--- a/nginx.conf
+++ b/nginx.conf
@@ -14,42 +14,35 @@ rtmp {
         application stream {
             live on;
 
-            on_publish http://pitube:3000/callback/rtmp;
             on_publish http://pitube:3000/callback/rtmp;
             on_publish_done http://pitube:3000/callback/rtmp;
             on_update http://pitube:3000/callback/rtmp;
             notify_update_timeout 15s;
 
-#              -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 1000k -f flv -g 30 -r 30 -s 854x480 -preset superfast -profile:v baseline
-#                    rtmp://localhost:1935/hls/$name_480p1128kbs
-#              -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 750k -f flv -g 30 -r 30 -s 640x360 -preset superfast -profile:v baseline
-#                    rtmp://localhost:1935/hls/$name_360p878kbs
-#              -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 400k -f flv -g 30 -r 30 -s 426x240 -preset superfast -profile:v baseline
-#                    rtmp://localhost:1935/hls/$name_240p528kbs
-
-            exec ffmpeg -i rtmp://localhost:1935/stream/$name
-              -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1280x720 -preset superfast -profile:v baseline
-                    rtmp://localhost:1935/hls/$name_720p2628kbs
-              -c:a libfdk_aac -b:a 64k -c:v libx264 -b:v 200k -f flv -g 15 -r 15 -s 426x240 -preset superfast -profile:v baseline
-                    rtmp://localhost:1935/hls/$name_240p264kbs;
-        }
-
-
-        application hls {
-            live on;
             hls on;
-            hls_fragment_naming system;
-            hls_fragment 5;
-            hls_playlist_length 10;
             hls_path /tmp/hls;
-            hls_nested on;
+            hls_fragment 15s;
+
+            recorder all {
+                record all manual;
+                record_suffix -%y%m%d-%h%m%s.flv;
+                record_path /tmp;
+                record_unique on;
+                record_max_size 4000m;
+            }
+
+            recorder audio {
+                record audio manual;
+                record_suffix -%y%m%d-%h%m%s.audio.flv;
+                record_path /tmp;
+                record_unique on;
+                record_max_size 500m;
+            }
+
 
-            hls_variant _720p2628kbs BANDWIDTH=2628000,RESOLUTION=1280x720;
-#            hls_variant _480p1128kbs BANDWIDTH=1128000,RESOLUTION=854x480;
-#            hls_variant _360p878kbs BANDWIDTH=878000,RESOLUTION=640x360;
-#            hls_variant _240p528kbs BANDWIDTH=528000,RESOLUTION=426x240;
-            hls_variant _240p264kbs BANDWIDTH=264000,RESOLUTION=426x240;
         }
+
+
     }
 }
 
@@ -64,8 +57,12 @@ http {
     server {
         listen ${HTTP_PORT};
 
+        location /control {
+            rtmp_control all;
+        }
+
         location /hls {
-            auth_request /callback/hls;
+#            auth_request /callback/hls;
             alias /tmp/hls;
             types {
                 application/vnd.apple.mpegurl m3u8;
diff --git a/pi_tube.conf b/pi_tube.conf
index e882205..9070c88 100644
--- a/pi_tube.conf
+++ b/pi_tube.conf
@@ -32,5 +32,8 @@
   },
   rtmp => {
     base_url => $ENV{RTMP_BASE_URL},
+  },
+  nginx => {
+    base_url => $ENV{NGINX_BASE_URL} // 'http://nginx-rtmp',
   }
 };
-- 
GitLab