]> git.sesse.net Git - nageru/blob - video_encoder.cpp
0df3b4ffe66c2bf8019be319233faa010c3796f8
[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
13 using namespace std;
14
15 namespace {
16
17 string generate_local_dump_filename(int frame)
18 {
19         time_t now = time(NULL);
20         tm now_tm;
21         localtime_r(&now, &now_tm);
22
23         char timestamp[256];
24         strftime(timestamp, sizeof(timestamp), "%F-%T%z", &now_tm);
25
26         // Use the frame number to disambiguate between two cuts starting
27         // on the same second.
28         char filename[256];
29         snprintf(filename, sizeof(filename), "%s%s-f%02d%s",
30                 LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX);
31         return filename;
32 }
33
34 }  // namespace
35
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)
38 {
39         open_output_stream();
40
41         if (global_flags.stream_audio_codec_name.empty()) {
42                 stream_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE));
43         } else {
44                 stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate));
45         }
46         stream_audio_encoder->add_mux(stream_mux.get());
47
48         string filename = generate_local_dump_filename(/*frame=*/0);
49         quicksync_encoder.reset(new QuickSyncEncoder(filename, surface, va_display, width, height, stream_mux.get(), stream_audio_encoder.get()));
50 }
51
52 VideoEncoder::~VideoEncoder()
53 {
54         quicksync_encoder.reset(nullptr);
55         close_output_stream();
56 }
57
58 void VideoEncoder::do_cut(int frame)
59 {
60         string filename = generate_local_dump_filename(frame);
61         printf("Starting new recording: %s\n", filename.c_str());
62         quicksync_encoder->shutdown();
63         quicksync_encoder.reset(new QuickSyncEncoder(filename, surface, va_display, width, height, stream_mux.get(), stream_audio_encoder.get()));
64 }
65
66 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
67 {
68         quicksync_encoder->add_audio(pts, audio);
69 }
70
71 bool VideoEncoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
72 {
73         return quicksync_encoder->begin_frame(y_tex, cbcr_tex);
74 }
75
76 RefCountedGLsync VideoEncoder::end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames)
77 {
78         return quicksync_encoder->end_frame(pts, duration, input_frames);
79 }
80
81 void VideoEncoder::open_output_stream()
82 {
83         AVFormatContext *avctx = avformat_alloc_context();
84         AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
85         assert(oformat != nullptr);
86         avctx->oformat = oformat;
87
88         string codec_name;
89         int bit_rate;
90
91         if (global_flags.stream_audio_codec_name.empty()) {
92                 codec_name = AUDIO_OUTPUT_CODEC_NAME;
93                 bit_rate = DEFAULT_AUDIO_OUTPUT_BIT_RATE;
94         } else {
95                 codec_name = global_flags.stream_audio_codec_name;
96                 bit_rate = global_flags.stream_audio_codec_bitrate;
97         }
98
99         uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
100         avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &VideoEncoder::write_packet_thunk, nullptr);
101
102         Mux::Codec video_codec;
103         if (global_flags.uncompressed_video_to_http) {
104                 video_codec = Mux::CODEC_NV12;
105         } else {
106                 video_codec = Mux::CODEC_H264;
107         }
108
109         avctx->flags = AVFMT_FLAG_CUSTOM_IO;
110         AVCodec *codec_audio = avcodec_find_encoder_by_name(codec_name.c_str());
111         if (codec_audio == nullptr) {
112                 fprintf(stderr, "ERROR: Could not find codec '%s'\n", codec_name.c_str());
113                 exit(1);
114         }
115
116         int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
117         stream_mux_writing_header = true;
118         stream_mux.reset(new Mux(avctx, width, height, video_codec, codec_audio, time_base, bit_rate, this));
119         stream_mux_writing_header = false;
120         httpd->set_header(stream_mux_header);
121         stream_mux_header.clear();
122 }
123
124 void VideoEncoder::close_output_stream()
125 {
126         stream_mux.reset();
127 }
128
129 int VideoEncoder::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
130 {
131         VideoEncoder *video_encoder = (VideoEncoder *)opaque;
132         return video_encoder->write_packet(buf, buf_size);
133 }
134
135 int VideoEncoder::write_packet(uint8_t *buf, int buf_size)
136 {
137         if (stream_mux_writing_header) {
138                 stream_mux_header.append((char *)buf, buf_size);
139         } else {
140                 httpd->add_data((char *)buf, buf_size, stream_mux_writing_keyframes);
141                 stream_mux_writing_keyframes = false;
142         }
143         return buf_size;
144 }
145