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.
5 #ifndef _VIDEO_ENCODER_H
6 #define _VIDEO_ENCODER_H
18 #include <libavformat/avformat.h>
19 #include <libavformat/avio.h>
22 #include "ref_counted_gl_sync.h"
25 class DiskSpaceEstimator;
29 class QuickSyncEncoder;
30 class RefCountedFrame;
39 VideoEncoder(movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator);
42 void add_audio(int64_t pts, std::vector<float> audio);
43 bool begin_frame(GLuint *y_tex, GLuint *cbcr_tex);
44 RefCountedGLsync end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames);
46 // Does a cut of the disk stream immediately ("frame" is used for the filename only).
47 void do_cut(int frame);
49 void change_x264_bitrate(unsigned rate_kbit);
52 void open_output_stream();
53 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
54 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
56 AVOutputFormat *oformat;
58 std::unique_ptr<QuickSyncEncoder> quicksync_encoder; // Under <qs_mu>.
59 movit::ResourcePool *resource_pool;
61 std::string va_display;
64 DiskSpaceEstimator *disk_space_estimator;
66 bool seen_sync_markers = false;
68 std::unique_ptr<Mux> stream_mux; // To HTTP.
69 std::unique_ptr<AudioEncoder> stream_audio_encoder;
70 std::unique_ptr<X264Encoder> x264_encoder; // nullptr if not using x264.
72 std::string stream_mux_header;
74 std::atomic<int> quicksync_encoders_in_shutdown{0};
76 // Encoders that are shutdown, but need to call release_gl_resources()
77 // (or be deleted) from some thread with an OpenGL context.
78 std::vector<std::unique_ptr<QuickSyncEncoder>> qs_needing_cleanup; // Under <qs_mu>.