1 #include "video_encoder.h"
11 #include "quicksync_encoder.h"
12 #include "x264_encoder.h"
15 using namespace movit;
19 string generate_local_dump_filename(int frame)
21 time_t now = time(NULL);
23 localtime_r(&now, &now_tm);
26 strftime(timestamp, sizeof(timestamp), "%F-%T%z", &now_tm);
28 // Use the frame number to disambiguate between two cuts starting
29 // on the same second.
31 snprintf(filename, sizeof(filename), "%s%s-f%02d%s",
32 LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX);
38 VideoEncoder::VideoEncoder(ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd)
39 : resource_pool(resource_pool), surface(surface), va_display(va_display), width(width), height(height), httpd(httpd)
41 oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
42 assert(oformat != nullptr);
43 if (global_flags.stream_audio_codec_name.empty()) {
44 stream_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, oformat));
46 stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
48 if (global_flags.x264_video_to_http) {
49 x264_encoder.reset(new X264Encoder(oformat));
52 string filename = generate_local_dump_filename(/*frame=*/0);
53 quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, stream_audio_encoder.get(), x264_encoder.get()));
56 stream_audio_encoder->add_mux(stream_mux.get());
57 quicksync_encoder->set_stream_mux(stream_mux.get());
58 x264_encoder->set_mux(stream_mux.get());
61 VideoEncoder::~VideoEncoder()
63 quicksync_encoder.reset(nullptr);
64 while (quicksync_encoders_in_shutdown.load() > 0) {
67 close_output_stream();
70 void VideoEncoder::do_cut(int frame)
72 string filename = generate_local_dump_filename(frame);
73 printf("Starting new recording: %s\n", filename.c_str());
75 // Do the shutdown of the old encoder in a separate thread, since it can
76 // take some time (it needs to wait for all the frames in the queue to be
77 // done encoding, for one) and we are running on the main mixer thread.
78 // However, since this means both encoders could be sending packets at
79 // the same time, it means pts could come out of order to the stream mux,
80 // and we need to plug it until the shutdown is complete.
82 lock_guard<mutex> lock(qs_mu);
83 QuickSyncEncoder *old_encoder = quicksync_encoder.release(); // When we go C++14, we can use move capture instead.
84 thread([old_encoder, this]{
85 old_encoder->shutdown();
88 // We cannot delete the encoder here, as this thread has no OpenGL context.
89 // We'll deal with it in begin_frame().
90 lock_guard<mutex> lock(qs_mu);
91 qs_needing_cleanup.emplace_back(old_encoder);
94 quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, stream_audio_encoder.get(), x264_encoder.get()));
95 quicksync_encoder->set_stream_mux(stream_mux.get());
98 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
100 lock_guard<mutex> lock(qs_mu);
101 quicksync_encoder->add_audio(pts, audio);
104 bool VideoEncoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
106 lock_guard<mutex> lock(qs_mu);
107 qs_needing_cleanup.clear(); // Since we have an OpenGL context here, and are called regularly.
108 return quicksync_encoder->begin_frame(y_tex, cbcr_tex);
111 RefCountedGLsync VideoEncoder::end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames)
113 lock_guard<mutex> lock(qs_mu);
114 return quicksync_encoder->end_frame(pts, duration, input_frames);
117 void VideoEncoder::open_output_stream()
119 AVFormatContext *avctx = avformat_alloc_context();
120 avctx->oformat = oformat;
122 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
123 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &VideoEncoder::write_packet_thunk, nullptr);
125 Mux::Codec video_codec;
126 if (global_flags.uncompressed_video_to_http) {
127 video_codec = Mux::CODEC_NV12;
129 video_codec = Mux::CODEC_H264;
132 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
134 string video_extradata;
135 if (global_flags.x264_video_to_http) {
136 video_extradata = x264_encoder->get_global_headers();
139 int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
140 stream_mux_writing_header = true;
141 stream_mux.reset(new Mux(avctx, width, height, video_codec, video_extradata, stream_audio_encoder->get_ctx(), time_base, this));
142 stream_mux_writing_header = false;
143 httpd->set_header(stream_mux_header);
144 stream_mux_header.clear();
147 void VideoEncoder::close_output_stream()
152 int VideoEncoder::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
154 VideoEncoder *video_encoder = (VideoEncoder *)opaque;
155 return video_encoder->write_packet(buf, buf_size);
158 int VideoEncoder::write_packet(uint8_t *buf, int buf_size)
160 if (stream_mux_writing_header) {
161 stream_mux_header.append((char *)buf, buf_size);
163 httpd->add_data((char *)buf, buf_size, stream_mux_writing_keyframes);
164 stream_mux_writing_keyframes = false;