1 #include "video_encoder.h"
11 #include <libavutil/mem.h>
14 #include "audio_encoder.h"
16 #include "shared/ffmpeg_raii.h"
18 #include "shared/httpd.h"
19 #include "shared/mux.h"
20 #include "quicksync_encoder.h"
21 #include "shared/timebase.h"
22 #include "x264_encoder.h"
24 class RefCountedFrame;
27 using namespace movit;
31 string generate_local_dump_filename(int frame)
33 time_t now = time(NULL);
35 localtime_r(&now, &now_tm);
38 strftime(timestamp, sizeof(timestamp), "%F-%H%M%S%z", &now_tm);
40 // Use the frame number to disambiguate between two cuts starting
41 // on the same second.
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);
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)
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));
59 stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
61 if (global_flags.x264_video_to_http || global_flags.x264_video_to_disk) {
62 x264_encoder.reset(new X264Encoder(oformat));
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));
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());
76 VideoEncoder::~VideoEncoder()
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) {
87 void VideoEncoder::do_cut(int frame)
89 string filename = generate_local_dump_filename(frame);
90 printf("Starting new recording: %s\n", filename.c_str());
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.
99 lock(qs_mu, qs_audio_mu);
100 lock_guard<mutex> lock1(qs_mu, adopt_lock), lock2(qs_audio_mu, adopt_lock);
101 QuickSyncEncoder *old_encoder = quicksync_encoder.release(); // When we go C++14, we can use move capture instead.
102 X264Encoder *old_x264_encoder = nullptr;
103 if (global_flags.x264_video_to_disk) {
104 old_x264_encoder = x264_encoder.release();
106 thread([old_encoder, old_x264_encoder, this]{
107 old_encoder->shutdown();
108 delete old_x264_encoder;
109 old_encoder->close_file();
110 stream_mux->unplug();
112 // We cannot delete the encoder here, as this thread has no OpenGL context.
113 // We'll deal with it in begin_frame().
114 lock_guard<mutex> lock(qs_mu);
115 qs_needing_cleanup.emplace_back(old_encoder);
118 if (global_flags.x264_video_to_disk) {
119 x264_encoder.reset(new X264Encoder(oformat));
120 if (global_flags.x264_video_to_http) {
121 x264_encoder->add_mux(stream_mux.get());
123 if (overriding_bitrate != 0) {
124 x264_encoder->change_bitrate(overriding_bitrate);
128 quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, x264_encoder.get(), disk_space_estimator));
129 quicksync_encoder->set_stream_mux(stream_mux.get());
132 void VideoEncoder::change_x264_bitrate(unsigned rate_kbit)
134 overriding_bitrate = rate_kbit;
135 x264_encoder->change_bitrate(rate_kbit);
138 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
140 // Take only qs_audio_mu, since add_audio() is thread safe
141 // (we can only conflict with do_cut(), which takes qs_audio_mu)
142 // and we don't want to contend with begin_frame().
144 lock_guard<mutex> lock(qs_audio_mu);
145 quicksync_encoder->add_audio(pts, audio);
147 stream_audio_encoder->encode_audio(audio, pts + quicksync_encoder->global_delay());
150 bool VideoEncoder::is_zerocopy() const
152 // Explicitly do _not_ take qs_mu; this is called from the mixer,
153 // and qs_mu might be contended. is_zerocopy() is thread safe
154 // and never called in parallel with do_cut() (both happen only
155 // from the mixer thread).
156 return quicksync_encoder->is_zerocopy();
159 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)
161 lock_guard<mutex> lock(qs_mu);
162 qs_needing_cleanup.clear(); // Since we have an OpenGL context here, and are called regularly.
163 return quicksync_encoder->begin_frame(pts, duration, ycbcr_coefficients, input_frames, y_tex, cbcr_tex);
166 RefCountedGLsync VideoEncoder::end_frame()
168 lock_guard<mutex> lock(qs_mu);
169 return quicksync_encoder->end_frame();
172 void VideoEncoder::open_output_stream()
174 AVFormatContext *avctx = avformat_alloc_context();
175 avctx->oformat = oformat;
177 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
178 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, nullptr, nullptr);
179 avctx->pb->write_data_type = &VideoEncoder::write_packet2_thunk;
180 avctx->pb->ignore_boundary_point = 1;
182 Mux::Codec video_codec;
183 if (global_flags.uncompressed_video_to_http) {
184 video_codec = Mux::CODEC_NV12;
186 video_codec = Mux::CODEC_H264;
189 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
191 string video_extradata;
192 if (global_flags.x264_video_to_http || global_flags.x264_video_to_disk) {
193 video_extradata = x264_encoder->get_global_headers();
196 stream_mux.reset(new Mux(avctx, width, height, video_codec, video_extradata, stream_audio_encoder->get_codec_parameters().get(),
197 get_color_space(global_flags.ycbcr_rec709_coefficients), COARSE_TIMEBASE,
198 /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, { &stream_mux_metrics }));
199 stream_mux_metrics.init({{ "destination", "http" }});
202 int VideoEncoder::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
204 VideoEncoder *video_encoder = (VideoEncoder *)opaque;
205 return video_encoder->write_packet2(buf, buf_size, type, time);
208 int VideoEncoder::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
210 if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
211 seen_sync_markers = true;
212 } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
213 // We don't know if this is a keyframe or not (the muxer could
214 // avoid marking it), so we just have to make the best of it.
215 type = AVIO_DATA_MARKER_SYNC_POINT;
218 if (type == AVIO_DATA_MARKER_HEADER) {
219 stream_mux_header.append((char *)buf, buf_size);
220 httpd->set_header(HTTPD::StreamID{ HTTPD::MAIN_STREAM, 0 }, stream_mux_header);
222 httpd->add_data(HTTPD::StreamID{ HTTPD::MAIN_STREAM, 0 }, (char *)buf, buf_size, type == AVIO_DATA_MARKER_SYNC_POINT, time, AVRational{ AV_TIME_BASE, 1 });