1 #include "video_encoder.h"
8 #include <sys/socket.h>
14 #include <libavutil/mem.h>
17 #include "audio_encoder.h"
19 #include "av1_encoder.h"
22 #include "shared/ffmpeg_raii.h"
24 #include "shared/httpd.h"
25 #include "shared/mux.h"
26 #include "quicksync_encoder.h"
27 #include "shared/timebase.h"
28 #include "x264_encoder.h"
30 class RefCountedFrame;
33 using namespace movit;
37 string generate_local_dump_filename(int frame)
39 time_t now = time(NULL);
41 localtime_r(&now, &now_tm);
44 strftime(timestamp, sizeof(timestamp), "%F-%H%M%S%z", &now_tm);
46 // Use the frame number to disambiguate between two cuts starting
47 // on the same second.
49 snprintf(filename, sizeof(filename), "%s/%s%s-f%02d%s",
50 global_flags.recording_dir.c_str(),
51 LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX);
57 VideoEncoder::VideoEncoder(ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator)
58 : resource_pool(resource_pool), surface(surface), va_display(va_display), width(width), height(height), httpd(httpd), disk_space_estimator(disk_space_estimator)
60 // TODO: If we're outputting AV1, we can't use MPEG-TS currently.
61 srt_oformat = av_guess_format("mpegts", nullptr, nullptr);
62 assert(srt_oformat != nullptr);
64 oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
65 assert(oformat != nullptr);
66 if (global_flags.stream_audio_codec_name.empty()) {
67 stream_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, oformat));
69 stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
71 if (global_flags.x264_video_to_http || global_flags.x264_video_to_disk) {
72 x264_encoder.reset(new X264Encoder(oformat, /*use_separate_disk_params=*/false));
74 VideoCodecInterface *http_encoder = x264_encoder.get();
75 VideoCodecInterface *disk_encoder = x264_encoder.get();
77 if (global_flags.av1_video_to_http) {
78 av1_encoder.reset(new AV1Encoder(oformat));
79 http_encoder = av1_encoder.get();
82 if (global_flags.x264_separate_disk_encode) {
83 x264_disk_encoder.reset(new X264Encoder(oformat, /*use_separate_disk_params=*/true));
84 disk_encoder = x264_disk_encoder.get();
87 string filename = generate_local_dump_filename(/*frame=*/0);
88 quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, http_encoder, disk_encoder, disk_space_estimator));
90 open_output_streams();
91 stream_audio_encoder->add_mux(http_mux.get());
92 stream_audio_encoder->add_mux(srt_mux.get());
93 quicksync_encoder->set_http_mux(http_mux.get());
94 quicksync_encoder->set_srt_mux(srt_mux.get());
95 if (global_flags.x264_video_to_http) {
96 x264_encoder->add_mux(http_mux.get());
97 x264_encoder->add_mux(srt_mux.get());
100 if (global_flags.av1_video_to_http) {
101 av1_encoder->add_mux(http_mux.get());
102 av1_encoder->add_mux(srt_mux.get());
107 VideoEncoder::~VideoEncoder()
109 quicksync_encoder->shutdown();
110 x264_encoder.reset(nullptr);
111 x264_disk_encoder.reset(nullptr);
112 quicksync_encoder->close_file();
113 quicksync_encoder.reset(nullptr);
114 while (quicksync_encoders_in_shutdown.load() > 0) {
119 void VideoEncoder::do_cut(int frame)
121 string filename = generate_local_dump_filename(frame);
122 printf("Starting new recording: %s\n", filename.c_str());
124 // Do the shutdown of the old encoder in a separate thread, since it can
125 // take some time (it needs to wait for all the frames in the queue to be
126 // done encoding, for one) and we are running on the main mixer thread.
127 // However, since this means both encoders could be sending packets at
128 // the same time, it means pts could come out of order to the stream mux,
129 // and we need to plug it until the shutdown is complete.
131 lock(qs_mu, qs_audio_mu);
132 lock_guard<mutex> lock1(qs_mu, adopt_lock), lock2(qs_audio_mu, adopt_lock);
133 QuickSyncEncoder *old_encoder = quicksync_encoder.release(); // When we go C++14, we can use move capture instead.
134 X264Encoder *old_x264_encoder = nullptr;
135 X264Encoder *old_x264_disk_encoder = nullptr;
136 if (global_flags.x264_video_to_disk) {
137 old_x264_encoder = x264_encoder.release();
139 if (global_flags.x264_separate_disk_encode) {
140 old_x264_disk_encoder = x264_disk_encoder.release();
142 thread([old_encoder, old_x264_encoder, old_x264_disk_encoder, this]{
143 old_encoder->shutdown();
144 delete old_x264_encoder;
145 delete old_x264_disk_encoder;
146 old_encoder->close_file();
149 // We cannot delete the encoder here, as this thread has no OpenGL context.
150 // We'll deal with it in begin_frame().
151 lock_guard<mutex> lock(qs_mu);
152 qs_needing_cleanup.emplace_back(old_encoder);
155 if (global_flags.x264_video_to_disk) {
156 x264_encoder.reset(new X264Encoder(oformat, /*use_separate_disk_params=*/false));
157 assert(global_flags.x264_video_to_http);
158 if (global_flags.x264_video_to_http) {
159 x264_encoder->add_mux(http_mux.get());
161 if (overriding_bitrate != 0) {
162 x264_encoder->change_bitrate(overriding_bitrate);
165 X264Encoder *http_encoder = x264_encoder.get();
166 X264Encoder *disk_encoder = x264_encoder.get();
167 if (global_flags.x264_separate_disk_encode) {
168 x264_disk_encoder.reset(new X264Encoder(oformat, /*use_separate_disk_params=*/true));
169 disk_encoder = x264_disk_encoder.get();
172 quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, http_encoder, disk_encoder, disk_space_estimator));
173 quicksync_encoder->set_http_mux(http_mux.get());
176 void VideoEncoder::change_x264_bitrate(unsigned rate_kbit)
178 overriding_bitrate = rate_kbit;
179 x264_encoder->change_bitrate(rate_kbit);
182 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
184 // Take only qs_audio_mu, since add_audio() is thread safe
185 // (we can only conflict with do_cut(), which takes qs_audio_mu)
186 // and we don't want to contend with begin_frame().
188 lock_guard<mutex> lock(qs_audio_mu);
189 quicksync_encoder->add_audio(pts, audio);
191 stream_audio_encoder->encode_audio(audio, pts + quicksync_encoder->global_delay());
194 bool VideoEncoder::is_zerocopy() const
196 // Explicitly do _not_ take qs_mu; this is called from the mixer,
197 // and qs_mu might be contended. is_zerocopy() is thread safe
198 // and never called in parallel with do_cut() (both happen only
199 // from the mixer thread).
200 return quicksync_encoder->is_zerocopy();
203 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)
205 lock_guard<mutex> lock(qs_mu);
206 qs_needing_cleanup.clear(); // Since we have an OpenGL context here, and are called regularly.
207 return quicksync_encoder->begin_frame(pts, duration, ycbcr_coefficients, input_frames, y_tex, cbcr_tex);
210 RefCountedGLsync VideoEncoder::end_frame()
212 want_srt_metric_update = true;
213 lock_guard<mutex> lock(qs_mu);
214 return quicksync_encoder->end_frame();
217 void VideoEncoder::open_output_streams()
219 for (bool is_srt : {false, true}) {
220 if (is_srt && global_flags.srt_destination_host.empty()) {
224 AVFormatContext *avctx = avformat_alloc_context();
225 avctx->oformat = is_srt ? srt_oformat : oformat;
227 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
228 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, nullptr, nullptr);
230 avctx->pb->write_packet = &VideoEncoder::write_srt_packet_thunk;
232 avctx->pb->write_data_type = &VideoEncoder::write_packet2_thunk;
233 avctx->pb->ignore_boundary_point = 1;
236 Mux::Codec video_codec;
237 if (global_flags.av1_video_to_http) {
238 video_codec = Mux::CODEC_AV1;
240 video_codec = Mux::CODEC_H264;
243 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
245 string video_extradata;
246 if (global_flags.x264_video_to_http) {
247 video_extradata = x264_encoder->get_global_headers();
249 } else if (global_flags.av1_video_to_http) {
250 video_extradata = av1_encoder->get_global_headers();
254 Mux *mux = new Mux(avctx, width, height, video_codec, video_extradata, stream_audio_encoder->get_codec_parameters().get(),
255 get_color_space(global_flags.ycbcr_rec709_coefficients), COARSE_TIMEBASE,
256 /*write_callback=*/nullptr, is_srt ? Mux::WRITE_BACKGROUND : Mux::WRITE_FOREGROUND, { is_srt ? &srt_mux_metrics : &http_mux_metrics });
259 srt_mux_metrics.init({{ "destination", "srt" }});
260 srt_metrics.init({{ "cardtype", "output" }});
261 global_metrics.add("srt_num_connection_attempts", {{ "cardtype", "output" }}, &metric_srt_num_connection_attempts);
264 http_mux_metrics.init({{ "destination", "http" }});
269 int VideoEncoder::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
271 VideoEncoder *video_encoder = (VideoEncoder *)opaque;
272 return video_encoder->write_packet2(buf, buf_size, type, time);
275 int VideoEncoder::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
277 if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
278 seen_sync_markers = true;
279 } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
280 // We don't know if this is a keyframe or not (the muxer could
281 // avoid marking it), so we just have to make the best of it.
282 type = AVIO_DATA_MARKER_SYNC_POINT;
285 if (type == AVIO_DATA_MARKER_HEADER) {
286 http_mux_header.append((char *)buf, buf_size);
287 httpd->set_header(HTTPD::StreamID{ HTTPD::MAIN_STREAM, 0 }, http_mux_header);
289 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 });
294 int VideoEncoder::write_srt_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
296 VideoEncoder *video_encoder = (VideoEncoder *)opaque;
297 return video_encoder->write_srt_packet(buf, buf_size);
300 static string print_addrinfo(const addrinfo *ai)
302 char hoststr[NI_MAXHOST], portstr[NI_MAXSERV];
303 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hoststr, sizeof(hoststr), portstr, sizeof(portstr), NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
304 return "<unknown address>"; // Should basically never happen, since we're not doing DNS lookups.
307 if (ai->ai_family == AF_INET6) {
308 return string("[") + hoststr + "]:" + portstr;
310 return string(hoststr) + ":" + portstr;
314 int VideoEncoder::open_srt_socket()
316 int sock = srt_create_socket();
318 fprintf(stderr, "srt_create_socket(): %s\n", srt_getlasterror_str());
322 SRT_TRANSTYPE live = SRTT_LIVE;
323 if (srt_setsockopt(sock, 0, SRTO_TRANSTYPE, &live, sizeof(live)) < 0) {
324 fprintf(stderr, "srt_setsockopt(SRTO_TRANSTYPE): %s\n", srt_getlasterror_str());
329 if (srt_setsockopt(sock, 0, SRTO_LATENCY, &global_flags.srt_output_latency, sizeof(global_flags.srt_output_latency)) < 0) {
330 fprintf(stderr, "srt_setsockopt(SRTO_LATENCY): %s\n", srt_getlasterror_str());
335 if (!global_flags.srt_streamid.empty()) {
336 if (srt_setsockopt(sock, 0, SRTO_STREAMID, global_flags.srt_streamid.data(), global_flags.srt_streamid.size()) < 0) {
337 fprintf(stderr, "srt_setsockopt(SRTO_STREAMID): %s\n", srt_getlasterror_str());
343 if (!global_flags.srt_passphrase.empty()) {
344 if (srt_setsockopt(sock, 0, SRTO_PASSPHRASE, global_flags.srt_passphrase.data(), global_flags.srt_passphrase.size()) < 0) {
345 fprintf(stderr, "srt_setsockopt(SRTO_PASSPHRASE): %s\n", srt_getlasterror_str());
354 int VideoEncoder::connect_to_srt()
356 // We need to specify SOCK_DGRAM as a hint, or we'll get all addresses
357 // three times (for each of TCP, UDP, raw).
359 memset(&hints, 0, sizeof(hints));
360 hints.ai_flags = AI_ADDRCONFIG;
361 hints.ai_socktype = SOCK_DGRAM;
364 int ret = getaddrinfo(global_flags.srt_destination_host.c_str(), global_flags.srt_destination_port.c_str(), &hints, &ai);
366 fprintf(stderr, "getaddrinfo(%s:%s): %s\n", global_flags.srt_destination_host.c_str(), global_flags.srt_destination_port.c_str(), gai_strerror(ret));
370 for (const addrinfo *cur = ai; cur != nullptr; cur = cur->ai_next) {
371 // Seemingly, srt_create_socket() isn't universal; once we try to connect,
372 // it gets locked to either IPv4 or IPv6. So we need to create a new one
373 // for every address we try.
374 int sock = open_srt_socket();
379 ++metric_srt_num_connection_attempts;
380 if (srt_connect(sock, cur->ai_addr, cur->ai_addrlen) < 0) {
381 fprintf(stderr, "srt_connect(%s): %s\n", print_addrinfo(cur).c_str(), srt_getlasterror_str());
385 fprintf(stderr, "Connected to destination SRT endpoint at %s.\n", print_addrinfo(cur).c_str());
390 // Out of candidates, so give up.
395 int VideoEncoder::write_srt_packet(uint8_t *buf, int buf_size)
397 if (want_srt_metric_update.exchange(false) && srt_sock != -1) {
398 srt_metrics.update_srt_stats(srt_sock);
400 while (buf_size > 0) {
401 if (srt_sock == -1) {
402 srt_sock = connect_to_srt();
403 if (srt_sock == -1) {
407 srt_metrics.update_srt_stats(srt_sock);
409 int to_send = min(buf_size, SRT_LIVE_DEF_PLSIZE);
410 int ret = srt_send(srt_sock, (char *)buf, to_send);
412 fprintf(stderr, "srt_send(): %s\n", srt_getlasterror_str());
414 srt_metrics.metric_srt_uptime_seconds = 0.0 / 0.0;
415 srt_sock = connect_to_srt();