]> git.sesse.net Git - nageru/blob - video_encoder.h
Make the UI show free disk space, and a rough estimation of for how much longer we...
[nageru] / video_encoder.h
1 // A class to orchestrate the concept of video encoding. Will keep track of
2 // the muxes to stream and disk, the QuickSyncEncoder, and also the X264Encoder
3 // (for the stream) if there is one.
4
5 #ifndef _VIDEO_ENCODER_H
6 #define _VIDEO_ENCODER_H
7
8 #include <stdint.h>
9 #include <memory>
10 #include <set>
11 #include <string>
12 #include <vector>
13
14 #include "audio_encoder.h"
15 #include "mux.h"
16 #include "ref_counted_frame.h"
17 #include "ref_counted_gl_sync.h"
18
19 class DiskSpaceEstimator;
20 class HTTPD;
21 class QSurface;
22 class QuickSyncEncoder;
23 class X264Encoder;
24
25 namespace movit {
26 class ResourcePool;
27 }  // namespace movit
28
29 class VideoEncoder {
30 public:
31         VideoEncoder(movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator);
32         ~VideoEncoder();
33
34         void add_audio(int64_t pts, std::vector<float> audio);
35         bool begin_frame(GLuint *y_tex, GLuint *cbcr_tex);
36         RefCountedGLsync end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames);
37
38         // Does a cut of the disk stream immediately ("frame" is used for the filename only).
39         void do_cut(int frame);
40
41         void change_x264_bitrate(unsigned rate_kbit);
42
43 private:
44         void open_output_stream();
45         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
46         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
47
48         AVOutputFormat *oformat;
49         std::mutex qs_mu;
50         std::unique_ptr<QuickSyncEncoder> quicksync_encoder;  // Under <qs_mu>.
51         movit::ResourcePool *resource_pool;
52         QSurface *surface;
53         std::string va_display;
54         int width, height;
55         HTTPD *httpd;
56         DiskSpaceEstimator *disk_space_estimator;
57
58         std::unique_ptr<Mux> stream_mux;  // To HTTP.
59         std::unique_ptr<AudioEncoder> stream_audio_encoder;
60         std::unique_ptr<X264Encoder> x264_encoder;  // nullptr if not using x264.
61
62         std::string stream_mux_header;
63
64         std::atomic<int> quicksync_encoders_in_shutdown{0};
65
66         // Encoders that are shutdown, but need to call release_gl_resources()
67         // (or be deleted) from some thread with an OpenGL context.
68         std::vector<std::unique_ptr<QuickSyncEncoder>> qs_needing_cleanup;  // Under <qs_mu>.
69 };
70
71 #endif