]> git.sesse.net Git - nageru/blobdiff - kaeru.cpp
Add mux metrics to Kaeru.
[nageru] / kaeru.cpp
index a82ee51494fa9c29d45d548f9c01cf4dd2a4c132..c41a4c2323dec4b21b8188575e9229f5d9bd8b31 100644 (file)
--- a/kaeru.cpp
+++ b/kaeru.cpp
@@ -1,4 +1,4 @@
-// Kaeru (える), a simple transcoder intended for use with Nageru.
+// Kaeru (える), a simple transcoder intended for use with Nageru.
 // This is experimental code, not yet supported.
 
 #include "audio_encoder.h"
@@ -12,6 +12,7 @@
 
 #include <assert.h>
 #include <fcntl.h>
+#include <signal.h>
 #include <unistd.h>
 #include <string>
 
@@ -21,6 +22,8 @@ using namespace std;
 using namespace std::placeholders;
 
 Mixer *global_mixer = nullptr;
+X264Encoder *global_x264_encoder = nullptr;
+MuxMetrics stream_mux_metrics;
 
 int write_packet(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
 {
@@ -60,7 +63,8 @@ unique_ptr<Mux> create_mux(HTTPD *httpd, AVOutputFormat *oformat, X264Encoder *x
        unique_ptr<Mux> mux;
        int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
        mux.reset(new Mux(avctx, global_flags.width, global_flags.height, Mux::CODEC_H264, video_extradata, audio_encoder->get_codec_parameters().get(), time_base,
-               /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, {}));
+               /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, { &stream_mux_metrics }));
+       stream_mux_metrics.init({{ "destination", "http" }});
        return mux;
 }
 
@@ -117,6 +121,32 @@ void audio_frame_callback(Mux *mux, const AVPacket *pkt, AVRational timebase)
        mux->add_packet(*pkt, pkt->pts, pkt->dts == AV_NOPTS_VALUE ? pkt->pts : pkt->dts, timebase);
 }
 
+void adjust_bitrate(int signal)
+{
+       int new_bitrate = global_flags.x264_bitrate;
+       if (signal == SIGUSR1) {
+               new_bitrate += 100;
+               if (new_bitrate > 100000) {
+                       fprintf(stderr, "Ignoring SIGUSR1, can't increase bitrate below 100000 kbit/sec (currently at %d kbit/sec)\n",
+                               global_flags.x264_bitrate);
+               } else {
+                       fprintf(stderr, "Increasing bitrate to %d kbit/sec due to SIGUSR1.\n", new_bitrate);
+                       global_flags.x264_bitrate = new_bitrate;
+                       global_x264_encoder->change_bitrate(new_bitrate);
+               }
+       } else if (signal == SIGUSR2) {
+               new_bitrate -= 100;
+               if (new_bitrate < 100) {
+                       fprintf(stderr, "Ignoring SIGUSR1, can't decrease bitrate below 100 kbit/sec (currently at %d kbit/sec)\n",
+                               global_flags.x264_bitrate);
+               } else {
+                       fprintf(stderr, "Decreasing bitrate to %d kbit/sec due to SIGUSR1.\n", new_bitrate);
+                       global_flags.x264_bitrate = new_bitrate;
+                       global_x264_encoder->change_bitrate(new_bitrate);
+               }
+       }
+}
+
 int main(int argc, char *argv[])
 {
        parse_flags(PROGRAM_KAERU, argc, argv);
@@ -148,6 +178,7 @@ int main(int argc, char *argv[])
                audio_encoder->add_mux(http_mux.get());
        }
        x264_encoder.add_mux(http_mux.get());
+       global_x264_encoder = &x264_encoder;
 
        FFmpegCapture video(argv[optind], global_flags.width, global_flags.height);
        video.set_pixel_format(FFmpegCapture::PixelFormat_NV12);
@@ -161,6 +192,9 @@ int main(int argc, char *argv[])
 
        httpd.start(9095);
 
+       signal(SIGUSR1, adjust_bitrate);
+       signal(SIGUSR2, adjust_bitrate);
+
        for ( ;; ) {
                sleep(3600);
        }