]> git.sesse.net Git - nageru/blob - video_stream.h
Move to RAII queue counting, to fix some underflows.
[nageru] / video_stream.h
1 #ifndef _VIDEO_STREAM_H
2 #define _VIDEO_STREAM_H 1
3
4 #include <epoxy/gl.h>
5 #include <stdint.h>
6
7 extern "C" {
8 #include <libavformat/avio.h>
9 }
10
11 #include "jpeg_frame_view.h"
12 #include "ref_counted_gl_sync.h"
13 #include "queue_spot_holder.h"
14
15 #include <chrono>
16 #include <condition_variable>
17 #include <deque>
18 #include <functional>
19 #include <movit/effect_chain.h>
20 #include <movit/mix_effect.h>
21 #include <movit/ycbcr_input.h>
22 #include <mutex>
23 #include <string>
24 #include <thread>
25
26 class ChromaSubsampler;
27 class DISComputeFlow;
28 class Interpolate;
29 class Mux;
30 class QSurface;
31 class QSurfaceFormat;
32 class YCbCrConverter;
33
34 class VideoStream {
35 public:
36         VideoStream();
37         ~VideoStream();
38         void start();
39         void stop();
40         void clear_queue();
41
42         // “display_func” is called after the frame has been calculated (if needed)
43         // and has gone out to the stream.
44         void schedule_original_frame(std::chrono::steady_clock::time_point,
45                                      int64_t output_pts, std::function<void()> &&display_func,
46                                      QueueSpotHolder &&queue_spot_holder,
47                                      unsigned stream_idx, int64_t input_pts);
48         void schedule_faded_frame(std::chrono::steady_clock::time_point, int64_t output_pts,
49                                   std::function<void()> &&display_func,
50                                   QueueSpotHolder &&queue_spot_holder,
51                                   unsigned stream_idx, int64_t input_pts, int secondary_stream_idx,
52                                   int64_t secondary_input_pts, float fade_alpha);
53         void schedule_interpolated_frame(std::chrono::steady_clock::time_point, int64_t output_pts,
54                                   std::function<void()> &&display_func,
55                                   QueueSpotHolder &&queue_spot_holder,
56                                   unsigned stream_idx, int64_t input_first_pts, int64_t input_second_pts,
57                                   float alpha, int secondary_stream_idx = -1, int64_t secondary_inputs_pts = -1,
58                                   float fade_alpha = 0.0f);  // -1 = no secondary frame.
59         void schedule_refresh_frame(std::chrono::steady_clock::time_point, int64_t output_pts,
60                                     std::function<void()> &&display_func,
61                                     QueueSpotHolder &&queue_spot_holder);
62
63 private:
64         void encode_thread_func();
65         std::thread encode_thread;
66
67         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
68         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
69
70         // Allocated at the very start; if we're empty, we start dropping frames
71         // (so that we don't build up an infinite interpolation backlog).
72         struct InterpolatedFrameResources {
73                 GLuint input_tex;  // Layered (contains both input frames), Y'CbCr.
74                 GLuint gray_tex;  // Same, but Y only.
75                 GLuint input_fbos[2];  // For rendering to the two layers of input_tex.
76
77                 // Destination textures and FBO if there is a fade.
78                 GLuint fade_y_output_tex, fade_cbcr_output_tex;
79                 GLuint fade_fbo;
80
81                 GLuint cb_tex, cr_tex;  // Subsampled, final output.
82
83                 GLuint pbo;  // For reading the data back.
84                 void *pbo_contents;  // Persistently mapped.
85         };
86         std::deque<InterpolatedFrameResources> interpolate_resources;  // Under <queue_lock>.
87         static constexpr size_t num_interpolate_slots = 15;  // Should be larger than Player::max_queued_frames, or we risk mass-dropping frames.
88
89         struct QueuedFrame {
90                 std::chrono::steady_clock::time_point local_pts;
91
92                 int64_t output_pts;
93                 enum Type { ORIGINAL, FADED, INTERPOLATED, FADED_INTERPOLATED, REFRESH } type;
94                 unsigned stream_idx;
95                 int64_t input_first_pts;  // The only pts for original frames.
96
97                 // For fades only (including fades against interpolated frames).
98                 int secondary_stream_idx = -1;
99                 int64_t secondary_input_pts;
100
101                 // For interpolated frames only.
102                 int64_t input_second_pts;
103                 float alpha;
104                 InterpolatedFrameResources resources;
105                 RefCountedGLsync fence;  // Set when the interpolated image is read back to the CPU.
106                 GLuint flow_tex, output_tex, cbcr_tex;  // Released in the receiving thread; not really used for anything else.
107                 JPEGID id;
108
109                 std::function<void()> display_func;  // Called when the image is done decoding.
110
111                 QueueSpotHolder queue_spot_holder;
112         };
113         std::deque<QueuedFrame> frame_queue;  // Under <queue_lock>.
114         std::mutex queue_lock;
115         std::condition_variable queue_changed;
116
117         std::unique_ptr<Mux> stream_mux;  // To HTTP.
118         std::string stream_mux_header;
119         bool seen_sync_markers = false;
120
121         std::unique_ptr<YCbCrConverter> ycbcr_converter;
122         std::unique_ptr<YCbCrConverter> ycbcr_semiplanar_converter;
123
124         // Frame interpolation.
125         std::unique_ptr<DISComputeFlow> compute_flow;
126         std::unique_ptr<Interpolate> interpolate, interpolate_no_split;
127         std::unique_ptr<ChromaSubsampler> chroma_subsampler;
128
129         std::vector<uint8_t> last_frame;
130 };
131
132 #endif  // !defined(_VIDEO_STREAM_H)