1 #include "video_encoder.h"
11 #include "quicksync_encoder.h"
12 #include "x264_encoder.h"
18 string generate_local_dump_filename(int frame)
20 time_t now = time(NULL);
22 localtime_r(&now, &now_tm);
25 strftime(timestamp, sizeof(timestamp), "%F-%T%z", &now_tm);
27 // Use the frame number to disambiguate between two cuts starting
28 // on the same second.
30 snprintf(filename, sizeof(filename), "%s%s-f%02d%s",
31 LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX);
37 VideoEncoder::VideoEncoder(QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd)
38 : surface(surface), va_display(va_display), width(width), height(height), httpd(httpd)
42 if (global_flags.stream_audio_codec_name.empty()) {
43 stream_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE));
45 stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate));
47 stream_audio_encoder->add_mux(stream_mux.get());
49 if (global_flags.x264_video_to_http) {
50 x264_encoder.reset(new X264Encoder(stream_mux.get()));
53 string filename = generate_local_dump_filename(/*frame=*/0);
54 quicksync_encoder.reset(new QuickSyncEncoder(filename, surface, va_display, width, height, stream_mux.get(), stream_audio_encoder.get(), x264_encoder.get()));
57 VideoEncoder::~VideoEncoder()
59 quicksync_encoder.reset(nullptr);
60 close_output_stream();
63 void VideoEncoder::do_cut(int frame)
65 string filename = generate_local_dump_filename(frame);
66 printf("Starting new recording: %s\n", filename.c_str());
67 quicksync_encoder->shutdown();
68 quicksync_encoder.reset(new QuickSyncEncoder(filename, surface, va_display, width, height, stream_mux.get(), stream_audio_encoder.get(), x264_encoder.get()));
71 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
73 quicksync_encoder->add_audio(pts, audio);
76 bool VideoEncoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
78 return quicksync_encoder->begin_frame(y_tex, cbcr_tex);
81 RefCountedGLsync VideoEncoder::end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames)
83 return quicksync_encoder->end_frame(pts, duration, input_frames);
86 void VideoEncoder::open_output_stream()
88 AVFormatContext *avctx = avformat_alloc_context();
89 AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
90 assert(oformat != nullptr);
91 avctx->oformat = oformat;
96 if (global_flags.stream_audio_codec_name.empty()) {
97 codec_name = AUDIO_OUTPUT_CODEC_NAME;
98 bit_rate = DEFAULT_AUDIO_OUTPUT_BIT_RATE;
100 codec_name = global_flags.stream_audio_codec_name;
101 bit_rate = global_flags.stream_audio_codec_bitrate;
104 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
105 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &VideoEncoder::write_packet_thunk, nullptr);
107 Mux::Codec video_codec;
108 if (global_flags.uncompressed_video_to_http) {
109 video_codec = Mux::CODEC_NV12;
111 video_codec = Mux::CODEC_H264;
114 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
115 AVCodec *codec_audio = avcodec_find_encoder_by_name(codec_name.c_str());
116 if (codec_audio == nullptr) {
117 fprintf(stderr, "ERROR: Could not find codec '%s'\n", codec_name.c_str());
121 int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
122 stream_mux_writing_header = true;
123 stream_mux.reset(new Mux(avctx, width, height, video_codec, codec_audio, time_base, bit_rate, this));
124 stream_mux_writing_header = false;
125 httpd->set_header(stream_mux_header);
126 stream_mux_header.clear();
129 void VideoEncoder::close_output_stream()
134 int VideoEncoder::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
136 VideoEncoder *video_encoder = (VideoEncoder *)opaque;
137 return video_encoder->write_packet(buf, buf_size);
140 int VideoEncoder::write_packet(uint8_t *buf, int buf_size)
142 if (stream_mux_writing_header) {
143 stream_mux_header.append((char *)buf, buf_size);
145 httpd->add_data((char *)buf, buf_size, stream_mux_writing_keyframes);
146 stream_mux_writing_keyframes = false;