]> git.sesse.net Git - nageru/blob - video_encoder.cpp
Update the queue length metric after trimming, not before.
[nageru] / video_encoder.cpp
1 #include "video_encoder.h"
2
3 #include <assert.h>
4 #include <stdio.h>
5 #include <time.h>
6 #include <unistd.h>
7 #include <string>
8 #include <thread>
9
10 extern "C" {
11 #include <libavutil/mem.h>
12 }
13
14 #include "audio_encoder.h"
15 #include "defs.h"
16 #include "ffmpeg_raii.h"
17 #include "flags.h"
18 #include "httpd.h"
19 #include "mux.h"
20 #include "quicksync_encoder.h"
21 #include "timebase.h"
22 #include "x264_encoder.h"
23
24 class RefCountedFrame;
25
26 using namespace std;
27 using namespace movit;
28
29 namespace {
30
31 string generate_local_dump_filename(int frame)
32 {
33         time_t now = time(NULL);
34         tm now_tm;
35         localtime_r(&now, &now_tm);
36
37         char timestamp[256];
38         strftime(timestamp, sizeof(timestamp), "%F-%T%z", &now_tm);
39
40         // Use the frame number to disambiguate between two cuts starting
41         // on the same second.
42         char filename[256];
43         snprintf(filename, sizeof(filename), "%s/%s%s-f%02d%s",
44                 global_flags.recording_dir.c_str(),
45                 LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX);
46         return filename;
47 }
48
49 }  // namespace
50
51 VideoEncoder::VideoEncoder(ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator)
52         : resource_pool(resource_pool), surface(surface), va_display(va_display), width(width), height(height), httpd(httpd), disk_space_estimator(disk_space_estimator)
53 {
54         oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
55         assert(oformat != nullptr);
56         if (global_flags.stream_audio_codec_name.empty()) {
57                 stream_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, oformat));
58         } else {
59                 stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
60         }
61         if (global_flags.x264_video_to_http || global_flags.x264_video_to_disk) {
62                 x264_encoder.reset(new X264Encoder(oformat));
63         }
64
65         string filename = generate_local_dump_filename(/*frame=*/0);
66         quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, x264_encoder.get(), disk_space_estimator));
67
68         open_output_stream();
69         stream_audio_encoder->add_mux(stream_mux.get());
70         quicksync_encoder->set_stream_mux(stream_mux.get());
71         if (global_flags.x264_video_to_http) {
72                 x264_encoder->add_mux(stream_mux.get());
73         }
74 }
75
76 VideoEncoder::~VideoEncoder()
77 {
78         quicksync_encoder->shutdown();
79         x264_encoder.reset(nullptr);
80         quicksync_encoder->close_file();
81         quicksync_encoder.reset(nullptr);
82         while (quicksync_encoders_in_shutdown.load() > 0) {
83                 usleep(10000);
84         }
85 }
86
87 void VideoEncoder::do_cut(int frame)
88 {
89         string filename = generate_local_dump_filename(frame);
90         printf("Starting new recording: %s\n", filename.c_str());
91
92         // Do the shutdown of the old encoder in a separate thread, since it can
93         // take some time (it needs to wait for all the frames in the queue to be
94         // done encoding, for one) and we are running on the main mixer thread.
95         // However, since this means both encoders could be sending packets at
96         // the same time, it means pts could come out of order to the stream mux,
97         // and we need to plug it until the shutdown is complete.
98         stream_mux->plug();
99         lock_guard<mutex> lock(qs_mu);
100         QuickSyncEncoder *old_encoder = quicksync_encoder.release();  // When we go C++14, we can use move capture instead.
101         X264Encoder *old_x264_encoder = nullptr;
102         if (global_flags.x264_video_to_disk) {
103                 old_x264_encoder = x264_encoder.release();
104         }
105         thread([old_encoder, old_x264_encoder, this]{
106                 old_encoder->shutdown();
107                 delete old_x264_encoder;
108                 old_encoder->close_file();
109                 stream_mux->unplug();
110
111                 // We cannot delete the encoder here, as this thread has no OpenGL context.
112                 // We'll deal with it in begin_frame().
113                 lock_guard<mutex> lock(qs_mu);
114                 qs_needing_cleanup.emplace_back(old_encoder);
115         }).detach();
116
117         if (global_flags.x264_video_to_disk) {
118                 x264_encoder.reset(new X264Encoder(oformat));
119                 if (global_flags.x264_video_to_http) {
120                         x264_encoder->add_mux(stream_mux.get());
121                 }
122                 if (overriding_bitrate != 0) {
123                         x264_encoder->change_bitrate(overriding_bitrate);
124                 }
125         }
126
127         quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, x264_encoder.get(), disk_space_estimator));
128         quicksync_encoder->set_stream_mux(stream_mux.get());
129 }
130
131 void VideoEncoder::change_x264_bitrate(unsigned rate_kbit)
132 {
133         overriding_bitrate = rate_kbit;
134         x264_encoder->change_bitrate(rate_kbit);
135 }
136
137 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
138 {
139         lock_guard<mutex> lock(qs_mu);
140         quicksync_encoder->add_audio(pts, audio);
141         stream_audio_encoder->encode_audio(audio, pts + quicksync_encoder->global_delay());
142 }
143
144 bool VideoEncoder::is_zerocopy() const
145 {
146         lock_guard<mutex> lock(qs_mu);
147         return quicksync_encoder->is_zerocopy();
148 }
149
150 bool VideoEncoder::begin_frame(int64_t pts, int64_t duration, movit::YCbCrLumaCoefficients ycbcr_coefficients, const std::vector<RefCountedFrame> &input_frames, GLuint *y_tex, GLuint *cbcr_tex)
151 {
152         lock_guard<mutex> lock(qs_mu);
153         qs_needing_cleanup.clear();  // Since we have an OpenGL context here, and are called regularly.
154         return quicksync_encoder->begin_frame(pts, duration, ycbcr_coefficients, input_frames, y_tex, cbcr_tex);
155 }
156
157 RefCountedGLsync VideoEncoder::end_frame()
158 {
159         lock_guard<mutex> lock(qs_mu);
160         return quicksync_encoder->end_frame();
161 }
162
163 void VideoEncoder::open_output_stream()
164 {
165         AVFormatContext *avctx = avformat_alloc_context();
166         avctx->oformat = oformat;
167
168         uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
169         avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, nullptr, nullptr);
170         avctx->pb->write_data_type = &VideoEncoder::write_packet2_thunk;
171         avctx->pb->ignore_boundary_point = 1;
172
173         Mux::Codec video_codec;
174         if (global_flags.uncompressed_video_to_http) {
175                 video_codec = Mux::CODEC_NV12;
176         } else {
177                 video_codec = Mux::CODEC_H264;
178         }
179
180         avctx->flags = AVFMT_FLAG_CUSTOM_IO;
181
182         string video_extradata;
183         if (global_flags.x264_video_to_http || global_flags.x264_video_to_disk) {
184                 video_extradata = x264_encoder->get_global_headers();
185         }
186
187         int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
188         stream_mux.reset(new Mux(avctx, width, height, video_codec, video_extradata, stream_audio_encoder->get_codec_parameters().get(), time_base,
189                 /*write_callback=*/nullptr, { &stream_mux_metrics }));
190         stream_mux_metrics.init({{ "destination", "http" }});
191 }
192
193 int VideoEncoder::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
194 {
195         VideoEncoder *video_encoder = (VideoEncoder *)opaque;
196         return video_encoder->write_packet2(buf, buf_size, type, time);
197 }
198
199 int VideoEncoder::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
200 {
201         if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
202                 seen_sync_markers = true;
203         } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
204                 // We don't know if this is a keyframe or not (the muxer could
205                 // avoid marking it), so we just have to make the best of it.
206                 type = AVIO_DATA_MARKER_SYNC_POINT;
207         }
208
209         if (type == AVIO_DATA_MARKER_HEADER) {
210                 stream_mux_header.append((char *)buf, buf_size);
211                 httpd->set_header(stream_mux_header);
212         } else {
213                 httpd->add_data((char *)buf, buf_size, type == AVIO_DATA_MARKER_SYNC_POINT);
214         }
215         return buf_size;
216 }
217