1 #include "video_encoder.h"
11 #include "quicksync_encoder.h"
17 string generate_local_dump_filename(int frame)
19 time_t now = time(NULL);
21 localtime_r(&now, &now_tm);
24 strftime(timestamp, sizeof(timestamp), "%F-%T%z", &now_tm);
26 // Use the frame number to disambiguate between two cuts starting
27 // on the same second.
29 snprintf(filename, sizeof(filename), "%s%s-f%02d%s",
30 LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX);
36 VideoEncoder::VideoEncoder(QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd)
37 : surface(surface), va_display(va_display), width(width), height(height), httpd(httpd)
41 quicksync_encoder.reset(new QuickSyncEncoder(surface, va_display, width, height, stream_mux.get()));
42 quicksync_encoder->open_output_file(generate_local_dump_filename(/*frame=*/0).c_str());
45 VideoEncoder::~VideoEncoder()
47 quicksync_encoder.reset(nullptr);
48 close_output_stream();
51 void VideoEncoder::do_cut(int frame)
53 string filename = generate_local_dump_filename(frame);
54 printf("Starting new recording: %s\n", filename.c_str());
55 quicksync_encoder->close_output_file();
56 quicksync_encoder->shutdown();
57 quicksync_encoder.reset(new QuickSyncEncoder(surface, va_display, width, height, stream_mux.get()));
58 quicksync_encoder->open_output_file(filename.c_str());
61 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
63 quicksync_encoder->add_audio(pts, audio);
66 bool VideoEncoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
68 return quicksync_encoder->begin_frame(y_tex, cbcr_tex);
71 RefCountedGLsync VideoEncoder::end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames)
73 return quicksync_encoder->end_frame(pts, duration, input_frames);
76 void VideoEncoder::open_output_stream()
78 AVFormatContext *avctx = avformat_alloc_context();
79 AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
80 assert(oformat != nullptr);
81 avctx->oformat = oformat;
86 if (global_flags.stream_audio_codec_name.empty()) {
87 codec_name = AUDIO_OUTPUT_CODEC_NAME;
88 bit_rate = DEFAULT_AUDIO_OUTPUT_BIT_RATE;
90 codec_name = global_flags.stream_audio_codec_name;
91 bit_rate = global_flags.stream_audio_codec_bitrate;
94 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
95 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &VideoEncoder::write_packet_thunk, nullptr);
97 Mux::Codec video_codec;
98 if (global_flags.uncompressed_video_to_http) {
99 video_codec = Mux::CODEC_NV12;
101 video_codec = Mux::CODEC_H264;
104 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
105 AVCodec *codec_audio = avcodec_find_encoder_by_name(codec_name.c_str());
106 if (codec_audio == nullptr) {
107 fprintf(stderr, "ERROR: Could not find codec '%s'\n", codec_name.c_str());
111 int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
112 stream_mux_writing_header = true;
113 stream_mux.reset(new Mux(avctx, width, height, video_codec, codec_audio, time_base, bit_rate, this));
114 stream_mux_writing_header = false;
115 httpd->set_header(stream_mux_header);
116 stream_mux_header.clear();
119 void VideoEncoder::close_output_stream()
124 int VideoEncoder::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
126 VideoEncoder *video_encoder = (VideoEncoder *)opaque;
127 return video_encoder->write_packet(buf, buf_size);
130 int VideoEncoder::write_packet(uint8_t *buf, int buf_size)
132 if (stream_mux_writing_header) {
133 stream_mux_header.append((char *)buf, buf_size);
135 httpd->add_data((char *)buf, buf_size, stream_mux_writing_keyframes);
136 stream_mux_writing_keyframes = false;