]> git.sesse.net Git - nageru/blob - video_encoder.cpp
Move X264Encoder out of QuickSyncEncoder. This completes independent restart of the...
[nageru] / video_encoder.cpp
1 #include "video_encoder.h"
2
3 #include <assert.h>
4
5 #include <string>
6
7 #include "defs.h"
8 #include "flags.h"
9 #include "httpd.h"
10 #include "timebase.h"
11 #include "quicksync_encoder.h"
12 #include "x264_encoder.h"
13
14 using namespace std;
15
16 namespace {
17
18 string generate_local_dump_filename(int frame)
19 {
20         time_t now = time(NULL);
21         tm now_tm;
22         localtime_r(&now, &now_tm);
23
24         char timestamp[256];
25         strftime(timestamp, sizeof(timestamp), "%F-%T%z", &now_tm);
26
27         // Use the frame number to disambiguate between two cuts starting
28         // on the same second.
29         char filename[256];
30         snprintf(filename, sizeof(filename), "%s%s-f%02d%s",
31                 LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX);
32         return filename;
33 }
34
35 }  // namespace
36
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)
39 {
40         open_output_stream();
41
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));
44         } else {
45                 stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate));
46         }
47         stream_audio_encoder->add_mux(stream_mux.get());
48
49         if (global_flags.x264_video_to_http) {
50                 x264_encoder.reset(new X264Encoder(stream_mux.get()));
51         }
52
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()));
55 }
56
57 VideoEncoder::~VideoEncoder()
58 {
59         quicksync_encoder.reset(nullptr);
60         close_output_stream();
61 }
62
63 void VideoEncoder::do_cut(int frame)
64 {
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()));
69 }
70
71 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
72 {
73         quicksync_encoder->add_audio(pts, audio);
74 }
75
76 bool VideoEncoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
77 {
78         return quicksync_encoder->begin_frame(y_tex, cbcr_tex);
79 }
80
81 RefCountedGLsync VideoEncoder::end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames)
82 {
83         return quicksync_encoder->end_frame(pts, duration, input_frames);
84 }
85
86 void VideoEncoder::open_output_stream()
87 {
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;
92
93         string codec_name;
94         int bit_rate;
95
96         if (global_flags.stream_audio_codec_name.empty()) {
97                 codec_name = AUDIO_OUTPUT_CODEC_NAME;
98                 bit_rate = DEFAULT_AUDIO_OUTPUT_BIT_RATE;
99         } else {
100                 codec_name = global_flags.stream_audio_codec_name;
101                 bit_rate = global_flags.stream_audio_codec_bitrate;
102         }
103
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);
106
107         Mux::Codec video_codec;
108         if (global_flags.uncompressed_video_to_http) {
109                 video_codec = Mux::CODEC_NV12;
110         } else {
111                 video_codec = Mux::CODEC_H264;
112         }
113
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());
118                 exit(1);
119         }
120
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();
127 }
128
129 void VideoEncoder::close_output_stream()
130 {
131         stream_mux.reset();
132 }
133
134 int VideoEncoder::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
135 {
136         VideoEncoder *video_encoder = (VideoEncoder *)opaque;
137         return video_encoder->write_packet(buf, buf_size);
138 }
139
140 int VideoEncoder::write_packet(uint8_t *buf, int buf_size)
141 {
142         if (stream_mux_writing_header) {
143                 stream_mux_header.append((char *)buf, buf_size);
144         } else {
145                 httpd->add_data((char *)buf, buf_size, stream_mux_writing_keyframes);
146                 stream_mux_writing_keyframes = false;
147         }
148         return buf_size;
149 }
150