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