]> git.sesse.net Git - nageru/blob - kaeru.cpp
Fix so that you can't right-click on non-signal channels anymore.
[nageru] / kaeru.cpp
1 // Kaeru (換える), a simple transcoder intended for use with Nageru.
2 // This is experimental code, not yet supported.
3
4 #include "audio_encoder.h"
5 #include "basic_stats.h"
6 #include "defs.h"
7 #include "flags.h"
8 #include "ffmpeg_capture.h"
9 #include "mixer.h"
10 #include "mux.h"
11 #include "quittable_sleeper.h"
12 #include "timebase.h"
13 #include "x264_encoder.h"
14
15 #include <assert.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <unistd.h>
19 #include <chrono>
20 #include <string>
21
22 using namespace bmusb;
23 using namespace movit;
24 using namespace std;
25 using namespace std::chrono;
26 using namespace std::placeholders;
27
28 Mixer *global_mixer = nullptr;
29 X264Encoder *global_x264_encoder = nullptr;
30 int frame_num = 0;
31 BasicStats *global_basic_stats = nullptr;
32 QuittableSleeper should_quit;
33 MuxMetrics stream_mux_metrics;
34
35 int write_packet(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
36 {
37         static bool seen_sync_markers = false;
38         static string stream_mux_header;
39         HTTPD *httpd = (HTTPD *)opaque;
40
41         if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
42                 seen_sync_markers = true;
43         } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
44                 // We don't know if this is a keyframe or not (the muxer could
45                 // avoid marking it), so we just have to make the best of it.
46                 type = AVIO_DATA_MARKER_SYNC_POINT;
47         }
48
49         if (type == AVIO_DATA_MARKER_HEADER) {
50                 stream_mux_header.append((char *)buf, buf_size);
51                 httpd->set_header(stream_mux_header);
52         } else {
53                 httpd->add_data((char *)buf, buf_size, type == AVIO_DATA_MARKER_SYNC_POINT);
54         }
55         return buf_size;
56 }
57
58 unique_ptr<Mux> create_mux(HTTPD *httpd, AVOutputFormat *oformat, X264Encoder *x264_encoder, AudioEncoder *audio_encoder)
59 {
60         AVFormatContext *avctx = avformat_alloc_context();
61         avctx->oformat = oformat;
62
63         uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
64         avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, httpd, nullptr, nullptr, nullptr);
65         avctx->pb->write_data_type = &write_packet;
66         avctx->pb->ignore_boundary_point = 1;
67         avctx->flags = AVFMT_FLAG_CUSTOM_IO;
68
69         string video_extradata = x264_encoder->get_global_headers();
70
71         unique_ptr<Mux> mux;
72         int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
73         mux.reset(new Mux(avctx, global_flags.width, global_flags.height, Mux::CODEC_H264, video_extradata, audio_encoder->get_codec_parameters().get(), time_base,
74                 /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, { &stream_mux_metrics }));
75         stream_mux_metrics.init({{ "destination", "http" }});
76         return mux;
77 }
78
79 void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, AudioEncoder *audio_encoder,
80                           int64_t video_pts, AVRational video_timebase,
81                           int64_t audio_pts, AVRational audio_timebase,
82                           uint16_t timecode,
83                           FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
84                           FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format)
85 {
86         if (video_pts >= 0 && video_frame.len > 0) {
87                 ReceivedTimestamps ts;
88                 ts.ts.push_back(steady_clock::now());
89
90                 video_pts = av_rescale_q(video_pts, video_timebase, AVRational{ 1, TIMEBASE });
91                 int64_t frame_duration = TIMEBASE * video_format.frame_rate_den / video_format.frame_rate_nom;
92                 x264_encoder->add_frame(video_pts, frame_duration, video->get_current_frame_ycbcr_format().luma_coefficients, video_frame.data + video_offset, ts);
93                 global_basic_stats->update(frame_num++, /*dropped_frames=*/0);
94         }
95         if (audio_frame.len > 0) {
96                 // FFmpegCapture takes care of this for us.
97                 assert(audio_format.num_channels == 2);
98                 assert(audio_format.sample_rate == OUTPUT_FREQUENCY);
99
100                 // TODO: Reduce some duplication against AudioMixer here.
101                 size_t num_samples = audio_frame.len / (audio_format.bits_per_sample / 8);
102                 vector<float> float_samples;
103                 float_samples.resize(num_samples);
104                 if (audio_format.bits_per_sample == 16) {
105                         const int16_t *src = (const int16_t *)audio_frame.data;
106                         float *dst = &float_samples[0];
107                         for (size_t i = 0; i < num_samples; ++i) {
108                                 *dst++ = le16toh(*src++) * (1.0f / 32768.0f);
109                         }
110                 } else if (audio_format.bits_per_sample == 32) {
111                         const int32_t *src = (const int32_t *)audio_frame.data;
112                         float *dst = &float_samples[0];
113                         for (size_t i = 0; i < num_samples; ++i) {
114                                 *dst++ = le32toh(*src++) * (1.0f / 2147483648.0f);
115                         }
116                 } else {
117                         assert(false);
118                 }
119                 audio_pts = av_rescale_q(audio_pts, audio_timebase, AVRational{ 1, TIMEBASE });
120                 audio_encoder->encode_audio(float_samples, audio_pts);
121         }
122
123         if (video_frame.owner) {
124                 video_frame.owner->release_frame(video_frame);
125         }
126         if (audio_frame.owner) {
127                 audio_frame.owner->release_frame(audio_frame);
128         }
129 }
130
131 void audio_frame_callback(Mux *mux, const AVPacket *pkt, AVRational timebase)
132 {
133         mux->add_packet(*pkt, pkt->pts, pkt->dts == AV_NOPTS_VALUE ? pkt->pts : pkt->dts, timebase);
134 }
135
136 void adjust_bitrate(int signal)
137 {
138         int new_bitrate = global_flags.x264_bitrate;
139         if (signal == SIGUSR1) {
140                 new_bitrate += 100;
141                 if (new_bitrate > 100000) {
142                         fprintf(stderr, "Ignoring SIGUSR1, can't increase bitrate below 100000 kbit/sec (currently at %d kbit/sec)\n",
143                                 global_flags.x264_bitrate);
144                 } else {
145                         fprintf(stderr, "Increasing bitrate to %d kbit/sec due to SIGUSR1.\n", new_bitrate);
146                         global_flags.x264_bitrate = new_bitrate;
147                         global_x264_encoder->change_bitrate(new_bitrate);
148                 }
149         } else if (signal == SIGUSR2) {
150                 new_bitrate -= 100;
151                 if (new_bitrate < 100) {
152                         fprintf(stderr, "Ignoring SIGUSR2, can't decrease bitrate below 100 kbit/sec (currently at %d kbit/sec)\n",
153                                 global_flags.x264_bitrate);
154                 } else {
155                         fprintf(stderr, "Decreasing bitrate to %d kbit/sec due to SIGUSR2.\n", new_bitrate);
156                         global_flags.x264_bitrate = new_bitrate;
157                         global_x264_encoder->change_bitrate(new_bitrate);
158                 }
159         }
160 }
161
162 void request_quit(int signal)
163 {
164         should_quit.quit();
165 }
166
167 int main(int argc, char *argv[])
168 {
169         parse_flags(PROGRAM_KAERU, argc, argv);
170         if (optind + 1 != argc) {
171                 usage(PROGRAM_KAERU);
172                 exit(1);
173         }
174         global_flags.num_cards = 1;  // For latency metrics.
175
176         av_register_all();
177         avformat_network_init();
178
179         HTTPD httpd;
180
181         AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
182         assert(oformat != nullptr);
183
184         unique_ptr<AudioEncoder> audio_encoder;
185         if (global_flags.stream_audio_codec_name.empty()) {
186                 audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, oformat));
187         } else {
188                 audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
189         }
190
191         unique_ptr<X264Encoder> x264_encoder(new X264Encoder(oformat));
192         unique_ptr<Mux> http_mux = create_mux(&httpd, oformat, x264_encoder.get(), audio_encoder.get());
193         if (global_flags.transcode_audio) {
194                 audio_encoder->add_mux(http_mux.get());
195         }
196         x264_encoder->add_mux(http_mux.get());
197         global_x264_encoder = x264_encoder.get();
198
199         FFmpegCapture video(argv[optind], global_flags.width, global_flags.height);
200         video.set_pixel_format(FFmpegCapture::PixelFormat_NV12);
201         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));
202         if (!global_flags.transcode_audio) {
203                 video.set_audio_callback(bind(audio_frame_callback, http_mux.get(), _1, _2));
204         }
205         video.configure_card();
206         video.start_bm_capture();
207         video.change_rate(2.0);  // Be sure never to really fall behind, but also don't dump huge amounts of stuff onto x264.
208
209         BasicStats basic_stats(/*verbose=*/false);
210         global_basic_stats = &basic_stats;
211         httpd.start(global_flags.http_port);
212
213         signal(SIGUSR1, adjust_bitrate);
214         signal(SIGUSR2, adjust_bitrate);
215         signal(SIGINT, request_quit);
216
217         while (!should_quit.should_quit()) {
218                 should_quit.sleep_for(hours(1000));
219         }
220
221         video.stop_dequeue_thread();
222         // Stop the x264 encoder before killing the mux it's writing to.
223         x264_encoder.reset();
224         return 0;
225 }