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