]> git.sesse.net Git - nageru/blob - nageru/video_encoder.h
Set CEF autoplay policy to be more lenient.
[nageru] / 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 <epoxy/gl_generated.h>
10 #include <movit/image_format.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <atomic>
14 #include <memory>
15 #include <mutex>
16 #include <string>
17 #include <vector>
18
19 extern "C" {
20 #include <libavformat/avformat.h>
21 #include <libavformat/avio.h>
22 }
23
24 #include <srt/srt.h>
25
26 #include "shared/mux.h"
27 #include "shared/ref_counted_gl_sync.h"
28 #include "srt_metrics.h"
29
30 class AudioEncoder;
31 class AV1Encoder;
32 class DiskSpaceEstimator;
33 class HTTPD;
34 class Mux;
35 class QSurface;
36 class QuickSyncEncoder;
37 class RefCountedFrame;
38 class X264Encoder;
39
40 namespace movit {
41 class ResourcePool;
42 }  // namespace movit
43
44 class VideoEncoder {
45 public:
46         VideoEncoder(movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator);
47         ~VideoEncoder();
48
49         void add_audio(int64_t pts, std::vector<float> audio);
50
51         bool is_zerocopy() const;
52
53         // Allocate a frame to render into. The returned two textures
54         // are yours to render into (build them into an FBO).
55         // Call end_frame() when you're done.
56         //
57         // The semantics of y_tex and cbcr_tex depend on is_zerocopy():
58         //
59         //   - If false, they are input parameters, ie., the caller
60         //     allocates textures. (The contents are not read before
61         //     end_frame() is called.)
62         //   - If true, they are output parameters, ie., VideoEncoder
63         //     allocates textures and borrow them to you for rendering.
64         //     In this case, after end_frame(), you are no longer allowed
65         //     to use the textures; they are torn down and given to the
66         //     H.264 encoder.
67         bool begin_frame(int64_t pts, int64_t duration, movit::YCbCrLumaCoefficients ycbcr_coefficients, const std::vector<RefCountedFrame> &input_frames, GLuint *y_tex, GLuint *cbcr_tex);
68
69         // Call after you are done rendering into the frame; at this point,
70         // y_tex and cbcr_tex will be assumed done, and handed over to the
71         // encoder. The returned fence is purely a convenience; you do not
72         // need to use it for anything, but it's useful if you wanted to set
73         // one anyway.
74         RefCountedGLsync end_frame();
75
76         // Does a cut of the disk stream immediately ("frame" is used for the filename only).
77         void do_cut(int frame);
78
79         void change_x264_bitrate(unsigned rate_kbit);
80
81 private:
82         void open_output_streams();
83         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
84         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
85
86         static int write_srt_packet_thunk(void *opaque, uint8_t *buf, int buf_size);
87         int write_srt_packet(uint8_t *buf, int buf_size);
88         int open_srt_socket();  // Returns -1 on error.
89         int connect_to_srt();  // Returns -1 on error.
90
91         const AVOutputFormat *oformat, *srt_oformat;
92         mutable std::mutex qs_mu, qs_audio_mu;
93         std::unique_ptr<QuickSyncEncoder> quicksync_encoder;  // Under <qs_mu> _and_ <qs_audio_mu>.
94         movit::ResourcePool *resource_pool;
95         QSurface *surface;
96         std::string va_display;
97         int width, height;
98         HTTPD *httpd;
99         DiskSpaceEstimator *disk_space_estimator;
100
101         bool seen_sync_markers = false;
102
103         std::unique_ptr<Mux> http_mux;  // To the HTTP server.
104         std::unique_ptr<Mux> srt_mux;  // To the SRT endpoint (if any).
105         std::unique_ptr<AudioEncoder> stream_audio_encoder;
106         std::unique_ptr<X264Encoder> x264_encoder;  // nullptr if not using x264.
107         std::unique_ptr<X264Encoder> x264_disk_encoder;  // nullptr if not using x264, or if not having separate disk encodes.
108 #ifdef HAVE_AV1
109         std::unique_ptr<AV1Encoder> av1_encoder;  // nullptr if not using SVT-AV1.
110 #endif
111
112         SRTSOCKET srt_sock = -1;
113
114         std::string http_mux_header;
115         MuxMetrics http_mux_metrics;
116         MuxMetrics srt_mux_metrics;
117         SRTMetrics srt_metrics;
118         std::atomic<int64_t> metric_srt_num_connection_attempts{0};
119         std::atomic<bool> want_srt_metric_update{true};  // Is nominally set every frame. Some racing is OK (this is mainly a rate-limiter).
120
121         std::atomic<int> quicksync_encoders_in_shutdown{0};
122         std::atomic<int> overriding_bitrate{0};
123         std::atomic<bool> should_quit{false};
124
125         // Encoders that are shutdown, but need to call release_gl_resources()
126         // (or be deleted) from some thread with an OpenGL context.
127         std::vector<std::unique_ptr<QuickSyncEncoder>> qs_needing_cleanup;  // Under <qs_mu>.
128 };
129
130 #endif