]> git.sesse.net Git - nageru/blob - nageru/kaeru.cpp
Add CEF support to Kaeru.
[nageru] / nageru / kaeru.cpp
1 // Kaeru (換える), a simple transcoder intended for use with Nageru.
2
3 #include "audio_encoder.h"
4 #include "basic_stats.h"
5 #ifdef HAVE_CEF
6 #include "cef_capture.h"
7 #endif
8 #include "defs.h"
9 #include "flags.h"
10 #include "ffmpeg_capture.h"
11 #include "mixer.h"
12 #include "print_latency.h"
13 #include "shared/ffmpeg_raii.h"
14 #include "shared/httpd.h"
15 #include "shared/mux.h"
16 #include "quittable_sleeper.h"
17 #include "shared/shared_defs.h"
18 #include "shared/timebase.h"
19 #include "x264_encoder.h"
20
21 #include <assert.h>
22 #include <bmusb/bmusb.h>
23 #include <chrono>
24 #include <endian.h>
25 #include <errno.h>
26 #include <functional>
27 #include <memory>
28 #include <movit/image_format.h>
29 #include <signal.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <string>
36 #include <vector>
37
38 extern "C" {
39 #include <libavcodec/bsf.h>
40 #include <libavcodec/codec_par.h>
41 #include <libavcodec/packet.h>
42 #include <libavformat/avformat.h>
43 #include <libavformat/avio.h>
44 #include <libavformat/version.h>
45 #include <libavutil/avutil.h>
46 #include <libavutil/common.h>
47 #include <libavutil/error.h>
48 #include <libavutil/mathematics.h>
49 #include <libavutil/mem.h>
50 #include <libavutil/rational.h>
51 #include <libavutil/version.h>
52 }
53
54 #ifdef HAVE_CEF
55 #include "cef_encoder_adapter.h"
56 #include "nageru_cef_app.h"
57 CefRefPtr<NageruCefApp> cef_app;
58 #endif
59
60 using namespace bmusb;
61 using namespace movit;
62 using namespace std;
63 using namespace std::chrono;
64 using namespace std::placeholders;
65
66 Mixer *global_mixer = nullptr;
67 X264Encoder *global_x264_encoder = nullptr;
68 int frame_num = 0;
69 BasicStats *global_basic_stats = nullptr;
70 QuittableSleeper should_quit;
71 MuxMetrics stream_mux_metrics;
72
73 namespace {
74
75 int write_packet(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
76 {
77         static bool seen_sync_markers = false;
78         static string stream_mux_header;
79         HTTPD *httpd = (HTTPD *)opaque;
80
81         if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
82                 seen_sync_markers = true;
83         } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
84                 // We don't know if this is a keyframe or not (the muxer could
85                 // avoid marking it), so we just have to make the best of it.
86                 type = AVIO_DATA_MARKER_SYNC_POINT;
87         }
88
89         HTTPD::StreamID stream_id{ HTTPD::MAIN_STREAM, 0 };
90         if (type == AVIO_DATA_MARKER_HEADER) {
91                 stream_mux_header.append((char *)buf, buf_size);
92                 httpd->set_header(stream_id, stream_mux_header);
93         } else {
94                 httpd->add_data(stream_id, (char *)buf, buf_size, type == AVIO_DATA_MARKER_SYNC_POINT, time, AVRational{ AV_TIME_BASE, 1 });
95         }
96         return buf_size;
97 }
98
99 }  // namespace
100
101 unique_ptr<Mux> create_mux(HTTPD *httpd, const AVOutputFormat *oformat, X264Encoder *x264_encoder, AudioEncoder *audio_encoder)
102 {
103         AVFormatContext *avctx = avformat_alloc_context();
104         avctx->oformat = oformat;  // const_cast is a hack to work in FFmpeg both before and after 5.0.
105
106         uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
107         avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, httpd, nullptr, nullptr, nullptr);
108         avctx->pb->write_data_type = &write_packet;
109         avctx->pb->ignore_boundary_point = 1;
110         avctx->flags = AVFMT_FLAG_CUSTOM_IO;
111
112         string video_extradata = x264_encoder->get_global_headers();
113
114         // If audio is disabled (ie., we won't ever see any audio packets),
115         // set nullptr here to also not include the stream in the mux.
116         AVCodecParameters *audio_codecpar =
117                 global_flags.enable_audio ? audio_encoder->get_codec_parameters().release() : nullptr;
118
119         unique_ptr<Mux> mux;
120         mux.reset(new Mux(avctx, global_flags.width, global_flags.height, Mux::CODEC_H264, video_extradata, audio_codecpar,
121                 get_color_space(global_flags.ycbcr_rec709_coefficients), COARSE_TIMEBASE,
122                 /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, { &stream_mux_metrics }));
123         stream_mux_metrics.init({{ "destination", "http" }});
124         return mux;
125 }
126
127 // NOTE: If we start using the timecode for anything, CEFEncoderAdapter will need adjustment.
128 void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, AudioEncoder *audio_encoder,
129                           int64_t video_pts, AVRational video_timebase,
130                           int64_t audio_pts, AVRational audio_timebase,
131                           uint16_t timecode,
132                           FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
133                           FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format)
134 {
135         if (video_pts >= 0 && video_frame.len > 0) {
136                 ReceivedTimestamps ts;
137                 ts.ts.push_back(steady_clock::now());
138
139                 video_pts = av_rescale_q(video_pts, video_timebase, AVRational{ 1, TIMEBASE });
140                 int64_t frame_duration = int64_t(TIMEBASE) * video_format.frame_rate_den / video_format.frame_rate_nom;
141                 YCbCrLumaCoefficients luma_coefficients = video ? video->get_current_frame_ycbcr_format().luma_coefficients : YCBCR_REC_709;
142                 x264_encoder->add_frame(video_pts, frame_duration, luma_coefficients, video_frame.data + video_offset, ts);
143                 global_basic_stats->update(frame_num++, /*dropped_frames=*/0);
144         }
145         if (audio_frame.len > 0) {
146                 // FFmpegCapture takes care of this for us.
147                 assert(audio_format.num_channels == 2);
148                 assert(audio_format.sample_rate == OUTPUT_FREQUENCY);
149
150                 // TODO: Reduce some duplication against AudioMixer here.
151                 size_t num_samples = audio_frame.len / (audio_format.bits_per_sample / 8);
152                 vector<float> float_samples;
153                 float_samples.resize(num_samples);
154
155                 if (audio_format.bits_per_sample == 16) {
156                         const int16_t *src = (const int16_t *)audio_frame.data;
157                         float *dst = &float_samples[0];
158                         for (size_t i = 0; i < num_samples; ++i) {
159                                 *dst++ = int16_t(le16toh(*src++)) * (1.0f / 32768.0f);
160                         }
161                 } else if (audio_format.bits_per_sample == 32) {
162                         const int32_t *src = (const int32_t *)audio_frame.data;
163                         float *dst = &float_samples[0];
164                         for (size_t i = 0; i < num_samples; ++i) {
165                                 *dst++ = int32_t(le32toh(*src++)) * (1.0f / 2147483648.0f);
166                         }
167                 } else {
168                         assert(false);
169                 }
170                 audio_pts = av_rescale_q(audio_pts, audio_timebase, AVRational{ 1, TIMEBASE });
171                 audio_encoder->encode_audio(float_samples, audio_pts);
172         }
173
174         if (video_frame.owner) {
175                 video_frame.owner->release_frame(video_frame);
176         }
177         if (audio_frame.owner) {
178                 audio_frame.owner->release_frame(audio_frame);
179         }
180 }
181
182 void raw_packet_callback(Mux *mux, int stream_index, const AVPacket *pkt, AVRational timebase)
183 {
184         mux->add_packet(*pkt, pkt->pts, pkt->dts == AV_NOPTS_VALUE ? pkt->pts : pkt->dts, timebase, stream_index);
185 }
186
187 void filter_packet_callback(Mux *mux, int stream_index, AVBSFContext *bsfctx, const AVPacket *pkt, AVRational timebase)
188 {
189         if (pkt->size <= 2 || pkt->data[0] != 0xff || (pkt->data[1] & 0xf0) != 0xf0) {
190                 // Not ADTS data, so just pass it through.
191                 mux->add_packet(*pkt, pkt->pts, pkt->dts == AV_NOPTS_VALUE ? pkt->pts : pkt->dts, timebase, stream_index);
192                 return;
193         }
194
195         AVPacket *in_pkt = av_packet_clone(pkt);
196         unique_ptr<AVPacket, decltype(av_packet_unref) *> in_pkt_cleanup(in_pkt, av_packet_unref);
197         int err = av_bsf_send_packet(bsfctx, in_pkt);
198         if (err < 0) {
199                 fprintf(stderr, "av_bsf_send_packet() failed with %d, ignoring\n", err);
200         }
201         for ( ;; ) {
202                 AVPacketWithDeleter out_pkt = av_packet_alloc_unique();
203                 err = av_bsf_receive_packet(bsfctx, out_pkt.get());
204                 if (err == AVERROR(EAGAIN)) {
205                         break;
206                 }
207                 if (err < 0) {
208                         fprintf(stderr, "av_bsf_receive_packet() failed with %d, ignoring\n", err);
209                         return;
210                 }
211                 mux->add_packet(*out_pkt, out_pkt->pts, out_pkt->dts == AV_NOPTS_VALUE ? out_pkt->pts : out_pkt->dts, timebase, stream_index);
212         }
213 }
214
215 void adjust_bitrate(int signal)
216 {
217         int new_bitrate = global_flags.x264_bitrate;
218         if (signal == SIGUSR1) {
219                 new_bitrate += 100;
220                 if (new_bitrate > 100000) {
221                         fprintf(stderr, "Ignoring SIGUSR1, can't increase bitrate below 100000 kbit/sec (currently at %d kbit/sec)\n",
222                                 global_flags.x264_bitrate);
223                 } else {
224                         fprintf(stderr, "Increasing bitrate to %d kbit/sec due to SIGUSR1.\n", new_bitrate);
225                         global_flags.x264_bitrate = new_bitrate;
226                         global_x264_encoder->change_bitrate(new_bitrate);
227                 }
228         } else if (signal == SIGUSR2) {
229                 new_bitrate -= 100;
230                 if (new_bitrate < 100) {
231                         fprintf(stderr, "Ignoring SIGUSR2, can't decrease bitrate below 100 kbit/sec (currently at %d kbit/sec)\n",
232                                 global_flags.x264_bitrate);
233                 } else {
234                         fprintf(stderr, "Decreasing bitrate to %d kbit/sec due to SIGUSR2.\n", new_bitrate);
235                         global_flags.x264_bitrate = new_bitrate;
236                         global_x264_encoder->change_bitrate(new_bitrate);
237                 }
238         }
239 }
240
241 void request_quit(int signal)
242 {
243         should_quit.quit();
244 }
245
246 int main(int argc, char *argv[])
247 {
248 #ifdef HAVE_CEF
249         // Let CEF have first priority on parsing the command line, because we might be
250         // launched as a CEF sub-process.
251         CefMainArgs main_args(argc, argv);
252         cef_app = CefRefPtr<NageruCefApp>(new NageruCefApp());
253         int err = CefExecuteProcess(main_args, cef_app.get(), nullptr);
254         if (err >= 0) {
255                 return err;
256         }
257 #endif
258
259         parse_flags(PROGRAM_KAERU, argc, argv);
260         if (optind + 1 != argc) {
261                 usage(PROGRAM_KAERU);
262                 abort();
263         }
264         global_flags.max_num_cards = 1;  // For latency metrics.
265
266 #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
267         av_register_all();
268 #endif
269         avformat_network_init();
270
271         HTTPD httpd;
272
273         const AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
274         assert(oformat != nullptr);
275
276         unique_ptr<AudioEncoder> audio_encoder;
277         if (global_flags.stream_audio_codec_name.empty()) {
278                 audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, oformat));
279         } else {
280                 audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
281         }
282
283         unique_ptr<X264Encoder> x264_encoder(new X264Encoder(oformat, /*use_separate_disk_params=*/false));
284         unique_ptr<Mux> http_mux = create_mux(&httpd, oformat, x264_encoder.get(), audio_encoder.get());
285         if (global_flags.transcode_audio) {
286                 audio_encoder->add_mux(http_mux.get());
287         }
288         if (global_flags.transcode_video) {
289                 x264_encoder->add_mux(http_mux.get());
290         }
291         global_x264_encoder = x264_encoder.get();
292
293         CaptureInterface *video;
294         unique_ptr<FFmpegCapture> ffmpeg_video;
295 #ifdef HAVE_CEF
296         unique_ptr<CEFCapture> cef_video;
297         unique_ptr<CEFEncoderAdapter> cef_encoder_adapter;
298         if (global_flags.use_cef) {
299                 cef_encoder_adapter.reset(new CEFEncoderAdapter(global_flags.width, global_flags.height, x264_encoder.get(), audio_encoder.get()));
300                 cef_video.reset(new CEFCapture(argv[optind], global_flags.width, global_flags.height));
301                 cef_video->set_pixel_format(bmusb::PixelFormat_8BitBGRA);
302                 cef_video->set_frame_callback(bind(&CEFEncoderAdapter::video_frame_callback, cef_encoder_adapter.get(), _1, _2, _3, _4, _5, _6, _7));
303                 // NOTE: No CEF audio support yet.
304                 video = cef_video.get();
305         } else
306 #endif
307         {
308                ffmpeg_video.reset(new FFmpegCapture(argv[optind], global_flags.width, global_flags.height));
309                ffmpeg_video->set_pixel_format(FFmpegCapture::PixelFormat_NV12);
310                if (global_flags.transcode_video) {
311                        ffmpeg_video->set_frame_callback(bind(video_frame_callback, ffmpeg_video.get(), x264_encoder.get(), audio_encoder.get(), _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11));
312                } else {
313                        ffmpeg_video->set_video_callback(bind(raw_packet_callback, http_mux.get(), /*stream_index=*/0, _1, _2));
314                }
315                if (!global_flags.transcode_audio && global_flags.enable_audio) {
316                        AVBSFContext *bsfctx = nullptr;
317                        if (strcmp(oformat->name, "mp4") == 0 && strcmp(audio_encoder->get_codec()->name, "aac") == 0) {
318                                // We need to insert the aac_adtstoasc filter, seemingly (or we will get warnings to do so).
319                                const AVBitStreamFilter *filter = av_bsf_get_by_name("aac_adtstoasc");
320                                int err = av_bsf_alloc(filter, &bsfctx);
321                                if (err < 0) {
322                                        fprintf(stderr, "av_bsf_alloc() failed with %d\n", err);
323                                        exit(1);
324                                }
325                        }
326                        if (bsfctx == nullptr) {
327                                ffmpeg_video->set_audio_callback(bind(raw_packet_callback, http_mux.get(), /*stream_index=*/1, _1, _2));
328                        } else {
329                                ffmpeg_video->set_audio_callback(bind(filter_packet_callback, http_mux.get(), /*stream_index=*/1, bsfctx, _1, _2));
330                        }
331                }
332                ffmpeg_video->change_rate(10.0);  // Play as fast as possible.
333                video = ffmpeg_video.get();
334         }
335         video->configure_card();
336         video->start_bm_capture();
337
338         BasicStats basic_stats(/*verbose=*/false, /*use_opengl=*/false);
339         global_basic_stats = &basic_stats;
340         httpd.start(global_flags.http_port);
341
342         signal(SIGUSR1, adjust_bitrate);
343         signal(SIGUSR2, adjust_bitrate);
344         signal(SIGINT, request_quit);
345
346         while (!should_quit.should_quit()) {
347 #ifdef HAVE_CEF
348                 if (global_flags.use_cef) {
349                         cef_encoder_adapter->duplicate_frame_if_needed(&should_quit);
350                         continue;
351                 }
352 #endif
353                 should_quit.sleep_for(hours(1000));
354         }
355
356         video->stop_dequeue_thread();
357         // Stop the x264 encoder before killing the mux it's writing to.
358         global_x264_encoder = nullptr;
359         x264_encoder.reset();
360         return 0;
361 }