X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=kaeru.cpp;h=23b3c8786ce4ae0336d60f3cdcd85305d4b6a788;hb=9672da324b767feb716fe32c1ef961881aa97cfd;hp=a82ee51494fa9c29d45d548f9c01cf4dd2a4c132;hpb=b0ce4383b7d64760bbfccf4e0e769b293f0db0cd;p=nageru diff --git a/kaeru.cpp b/kaeru.cpp index a82ee51..23b3c87 100644 --- 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,15 +12,20 @@ #include #include +#include #include +#include #include using namespace bmusb; using namespace movit; using namespace std; +using namespace std::chrono; 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 +65,8 @@ unique_ptr create_mux(HTTPD *httpd, AVOutputFormat *oformat, X264Encoder *x unique_ptr 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; } @@ -72,9 +78,12 @@ void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, Audio FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format) { if (video_pts >= 0 && video_frame.len > 0) { + ReceivedTimestamps ts; + 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; - x264_encoder->add_frame(video_pts, frame_duration, video->get_current_frame_ycbcr_format().luma_coefficients, video_frame.data + video_offset, ReceivedTimestamps()); + x264_encoder->add_frame(video_pts, frame_duration, video->get_current_frame_ycbcr_format().luma_coefficients, video_frame.data + video_offset, ts); } if (audio_frame.len > 0) { // FFmpegCapture takes care of this for us. @@ -117,6 +126,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); @@ -124,6 +159,7 @@ int main(int argc, char *argv[]) usage(PROGRAM_KAERU); exit(1); } + global_flags.num_cards = 1; // For latency metrics. av_register_all(); avformat_network_init(); @@ -148,6 +184,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 +198,9 @@ int main(int argc, char *argv[]) httpd.start(9095); + signal(SIGUSR1, adjust_bitrate); + signal(SIGUSR2, adjust_bitrate); + for ( ;; ) { sleep(3600); }