]> git.sesse.net Git - nageru/blob - video_encoder.h
Make the API for begin_frame()/end_frame() in VideoEncoder a bit more sensible.
[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 <epoxy/gl.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <atomic>
12 #include <memory>
13 #include <mutex>
14 #include <string>
15 #include <vector>
16
17 extern "C" {
18 #include <libavformat/avformat.h>
19 #include <libavformat/avio.h>
20 }
21
22 #include "ref_counted_gl_sync.h"
23
24 class AudioEncoder;
25 class DiskSpaceEstimator;
26 class HTTPD;
27 class Mux;
28 class QSurface;
29 class QuickSyncEncoder;
30 class RefCountedFrame;
31 class X264Encoder;
32
33 namespace movit {
34 class ResourcePool;
35 }  // namespace movit
36
37 class VideoEncoder {
38 public:
39         VideoEncoder(movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator);
40         ~VideoEncoder();
41
42         void add_audio(int64_t pts, std::vector<float> audio);
43
44         // Allocate a frame to render into. The returned two textures
45         // are yours to render into (build them into an FBO).
46         // Call end_frame() when you're done.
47         bool begin_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames, GLuint *y_tex, GLuint *cbcr_tex);
48
49         // Call after you are done rendering into the frame; at this point,
50         // y_tex and cbcr_tex will be assumed done, and handed over to the
51         // encoder. The returned fence is purely a convenience; you do not
52         // need to use it for anything, but it's useful if you wanted to set
53         // one anyway.
54         RefCountedGLsync end_frame();
55
56         // Does a cut of the disk stream immediately ("frame" is used for the filename only).
57         void do_cut(int frame);
58
59         void change_x264_bitrate(unsigned rate_kbit);
60
61 private:
62         void open_output_stream();
63         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
64         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
65
66         AVOutputFormat *oformat;
67         std::mutex qs_mu;
68         std::unique_ptr<QuickSyncEncoder> quicksync_encoder;  // Under <qs_mu>.
69         movit::ResourcePool *resource_pool;
70         QSurface *surface;
71         std::string va_display;
72         int width, height;
73         HTTPD *httpd;
74         DiskSpaceEstimator *disk_space_estimator;
75
76         bool seen_sync_markers = false;
77
78         std::unique_ptr<Mux> stream_mux;  // To HTTP.
79         std::unique_ptr<AudioEncoder> stream_audio_encoder;
80         std::unique_ptr<X264Encoder> x264_encoder;  // nullptr if not using x264.
81
82         std::string stream_mux_header;
83
84         std::atomic<int> quicksync_encoders_in_shutdown{0};
85
86         // Encoders that are shutdown, but need to call release_gl_resources()
87         // (or be deleted) from some thread with an OpenGL context.
88         std::vector<std::unique_ptr<QuickSyncEncoder>> qs_needing_cleanup;  // Under <qs_mu>.
89 };
90
91 #endif