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