]> git.sesse.net Git - nageru/blobdiff - kaeru.cpp
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / kaeru.cpp
index 23b3c8786ce4ae0336d60f3cdcd85305d4b6a788..6d98cce1791992a47df17663b77d5159169061fd 100644 (file)
--- a/kaeru.cpp
+++ b/kaeru.cpp
@@ -2,11 +2,13 @@
 // This is experimental code, not yet supported.
 
 #include "audio_encoder.h"
+#include "basic_stats.h"
 #include "defs.h"
 #include "flags.h"
 #include "ffmpeg_capture.h"
 #include "mixer.h"
 #include "mux.h"
+#include "quittable_sleeper.h"
 #include "timebase.h"
 #include "x264_encoder.h"
 
@@ -25,6 +27,9 @@ using namespace std::placeholders;
 
 Mixer *global_mixer = nullptr;
 X264Encoder *global_x264_encoder = nullptr;
+int frame_num = 0;
+BasicStats *global_basic_stats = nullptr;
+QuittableSleeper should_quit;
 MuxMetrics stream_mux_metrics;
 
 int write_packet(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
@@ -59,6 +64,7 @@ unique_ptr<Mux> create_mux(HTTPD *httpd, AVOutputFormat *oformat, X264Encoder *x
        avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, httpd, nullptr, nullptr, nullptr);
        avctx->pb->write_data_type = &write_packet;
        avctx->pb->ignore_boundary_point = 1;
+       avctx->flags = AVFMT_FLAG_CUSTOM_IO;
 
        string video_extradata = x264_encoder->get_global_headers();
 
@@ -82,8 +88,9 @@ void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, Audio
                ts.ts.push_back(steady_clock::now());
 
                video_pts = av_rescale_q(video_pts, video_timebase, AVRational{ 1, TIMEBASE });
-               int64_t frame_duration = TIMEBASE * video_format.frame_rate_nom / video_format.frame_rate_den;
+               int64_t frame_duration = TIMEBASE * video_format.frame_rate_den / video_format.frame_rate_nom;
                x264_encoder->add_frame(video_pts, frame_duration, video->get_current_frame_ycbcr_format().luma_coefficients, video_frame.data + video_offset, ts);
+               global_basic_stats->update(frame_num++, /*dropped_frames=*/0);
        }
        if (audio_frame.len > 0) {
                // FFmpegCapture takes care of this for us.
@@ -142,16 +149,21 @@ void adjust_bitrate(int signal)
        } 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",
+                       fprintf(stderr, "Ignoring SIGUSR2, 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);
+                       fprintf(stderr, "Decreasing bitrate to %d kbit/sec due to SIGUSR2.\n", new_bitrate);
                        global_flags.x264_bitrate = new_bitrate;
                        global_x264_encoder->change_bitrate(new_bitrate);
                }
        }
 }
 
+void request_quit(int signal)
+{
+       should_quit.quit();
+}
+
 int main(int argc, char *argv[])
 {
        parse_flags(PROGRAM_KAERU, argc, argv);
@@ -170,25 +182,23 @@ int main(int argc, char *argv[])
        assert(oformat != nullptr);
 
        unique_ptr<AudioEncoder> audio_encoder;
-       if (global_flags.transcode_audio) {
-               if (global_flags.stream_audio_codec_name.empty()) {
-                       audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, oformat));
-               } else {
-                       audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
-               }
+       if (global_flags.stream_audio_codec_name.empty()) {
+               audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, oformat));
+       } else {
+               audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
        }
 
-       X264Encoder x264_encoder(oformat);
-       unique_ptr<Mux> http_mux = create_mux(&httpd, oformat, &x264_encoder, audio_encoder.get());
+       unique_ptr<X264Encoder> x264_encoder(new X264Encoder(oformat));
+       unique_ptr<Mux> http_mux = create_mux(&httpd, oformat, x264_encoder.get(), audio_encoder.get());
        if (global_flags.transcode_audio) {
                audio_encoder->add_mux(http_mux.get());
        }
-       x264_encoder.add_mux(http_mux.get());
-       global_x264_encoder = &x264_encoder;
+       x264_encoder->add_mux(http_mux.get());
+       global_x264_encoder = x264_encoder.get();
 
        FFmpegCapture video(argv[optind], global_flags.width, global_flags.height);
        video.set_pixel_format(FFmpegCapture::PixelFormat_NV12);
-       video.set_frame_callback(bind(video_frame_callback, &video, &x264_encoder, audio_encoder.get(), _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11));
+       video.set_frame_callback(bind(video_frame_callback, &video, x264_encoder.get(), audio_encoder.get(), _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11));
        if (!global_flags.transcode_audio) {
                video.set_audio_callback(bind(audio_frame_callback, http_mux.get(), _1, _2));
        }
@@ -196,12 +206,20 @@ int main(int argc, char *argv[])
        video.start_bm_capture();
        video.change_rate(2.0);  // Be sure never to really fall behind, but also don't dump huge amounts of stuff onto x264.
 
-       httpd.start(9095);
+       BasicStats basic_stats(/*verbose=*/false);
+       global_basic_stats = &basic_stats;
+       httpd.start(global_flags.http_port);
 
        signal(SIGUSR1, adjust_bitrate);
        signal(SIGUSR2, adjust_bitrate);
+       signal(SIGINT, request_quit);
 
-       for ( ;; ) {
-               sleep(3600);
+       while (!should_quit.should_quit()) {
+               should_quit.sleep_for(hours(1000));
        }
+
+       video.stop_dequeue_thread();
+       // Stop the x264 encoder before killing the mux it's writing to.
+       x264_encoder.reset();
+       return 0;
 }