]> git.sesse.net Git - nageru/blob - video_encoder.cpp
Start pulling video orchestration logic into VideoEncoder.
[nageru] / video_encoder.cpp
1 #include "video_encoder.h"
2
3 #include <string>
4
5 #include "defs.h"
6 #include "quicksync_encoder.h"
7
8 using namespace std;
9
10 namespace {
11
12 string generate_local_dump_filename(int frame)
13 {
14         time_t now = time(NULL);
15         tm now_tm;
16         localtime_r(&now, &now_tm);
17
18         char timestamp[256];
19         strftime(timestamp, sizeof(timestamp), "%F-%T%z", &now_tm);
20
21         // Use the frame number to disambiguate between two cuts starting
22         // on the same second.
23         char filename[256];
24         snprintf(filename, sizeof(filename), "%s%s-f%02d%s",
25                 LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX);
26         return filename;
27 }
28
29 }  // namespace
30
31 VideoEncoder::VideoEncoder(QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd)
32         : surface(surface), va_display(va_display), width(width), height(height), httpd(httpd)
33 {
34         quicksync_encoder.reset(new QuickSyncEncoder(surface, va_display, width, height, httpd));
35         quicksync_encoder->open_output_file(generate_local_dump_filename(/*frame=*/0).c_str());
36 }
37
38 VideoEncoder::~VideoEncoder()
39 {
40         quicksync_encoder.reset(nullptr);
41 }
42
43 void VideoEncoder::do_cut(int frame)
44 {
45         string filename = generate_local_dump_filename(frame);
46         printf("Starting new recording: %s\n", filename.c_str());
47         quicksync_encoder->close_output_file();
48         quicksync_encoder->shutdown();
49         quicksync_encoder.reset(new QuickSyncEncoder(surface, va_display, width, height, httpd));
50         quicksync_encoder->open_output_file(filename.c_str());
51 }
52
53 void VideoEncoder::add_audio(int64_t pts, std::vector<float> audio)
54 {
55         quicksync_encoder->add_audio(pts, audio);
56 }
57
58 bool VideoEncoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
59 {
60         return quicksync_encoder->begin_frame(y_tex, cbcr_tex);
61 }
62
63 RefCountedGLsync VideoEncoder::end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames)
64 {
65         return quicksync_encoder->end_frame(pts, duration, input_frames);
66 }