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