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