]> git.sesse.net Git - nageru/blob - video_encoder.h
When doing a cut, do the shutdown in a separate thread.
[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 HTTPD;
20 class QSurface;
21 class QuickSyncEncoder;
22 class X264Encoder;
23
24 namespace movit {
25 class ResourcePool;
26 }  // namespace movit
27
28 class VideoEncoder : public KeyFrameSignalReceiver {
29 public:
30         VideoEncoder(movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd);
31         ~VideoEncoder();
32
33         void add_audio(int64_t pts, std::vector<float> audio);
34         bool begin_frame(GLuint *y_tex, GLuint *cbcr_tex);
35         RefCountedGLsync end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames);
36
37         // Does a cut of the disk stream immediately ("frame" is used for the filename only).
38         void do_cut(int frame);
39
40         virtual void signal_keyframe() override {
41                 stream_mux_writing_keyframes = true;
42         }
43
44 private:
45         void open_output_stream();
46         void close_output_stream();
47         static int write_packet_thunk(void *opaque, uint8_t *buf, int buf_size);
48         int write_packet(uint8_t *buf, int buf_size);
49
50         AVOutputFormat *oformat;
51         std::mutex qs_mu;
52         std::unique_ptr<QuickSyncEncoder> quicksync_encoder;  // Under <qs_mu>.
53         movit::ResourcePool *resource_pool;
54         QSurface *surface;
55         std::string va_display;
56         int width, height;
57         HTTPD *httpd;
58
59         std::unique_ptr<Mux> stream_mux;  // To HTTP.
60         std::unique_ptr<AudioEncoder> stream_audio_encoder;
61         std::unique_ptr<X264Encoder> x264_encoder;  // nullptr if not using x264.
62
63         // While Mux object is constructing, <stream_mux_writing_header> is true,
64         // and the header is being collected into stream_mux_header.
65         bool stream_mux_writing_header;
66         std::string stream_mux_header;
67
68         bool stream_mux_writing_keyframes = false;
69
70         std::atomic<int> quicksync_encoders_in_shutdown{0};
71
72         // Encoders that are shutdown, but need to call release_gl_resources()
73         // (or be deleted) from some thread with an OpenGL context.
74         std::vector<std::unique_ptr<QuickSyncEncoder>> qs_needing_cleanup;  // Under <qs_mu>.
75 };
76
77 #endif