]> git.sesse.net Git - nageru/blob - decklink_output.h
Rework the audio/video sync algorithm.
[nageru] / decklink_output.h
1 #ifndef _DECKLINK_OUTPUT_H
2 #define _DECKLINK_OUTPUT_H 1
3
4 #include <epoxy/gl.h>
5 #include <stdint.h>
6 #include <atomic>
7 #include <chrono>
8 #include <condition_variable>
9 #include <memory>
10 #include <mutex>
11 #include <queue>
12 #include <thread>
13 #include <vector>
14
15 #include "DeckLinkAPI.h"
16 #include "DeckLinkAPITypes.h"
17 #include "LinuxCOM.h"
18
19 #include "context.h"
20 #include "print_latency.h"
21 #include "ref_counted_frame.h"
22 #include "ref_counted_gl_sync.h"
23
24 namespace movit {
25
26 class ResourcePool;
27
28 }  // namespace movit
29
30 class ChromaSubsampler;
31 class IDeckLink;
32 class IDeckLinkOutput;
33 class QSurface;
34
35 class DeckLinkOutput : public IDeckLinkVideoOutputCallback {
36 public:
37         DeckLinkOutput(movit::ResourcePool *resource_pool, QSurface *surface, unsigned width, unsigned height, unsigned card_index);
38
39         void set_device(IDeckLink *output);
40         void start_output(uint32_t mode, int64_t base_pts);  // Mode comes from get_available_video_modes().
41         void end_output();
42
43         void send_frame(GLuint y_tex, GLuint cbcr_tex, const std::vector<RefCountedFrame> &input_frames, int64_t pts, int64_t duration);
44         void send_audio(int64_t pts, const std::vector<float> &samples);
45
46         // NOTE: The returned timestamp is undefined for preroll.
47         // Otherwise, it is the timestamp of the output frame as it should have been,
48         // even if we're overshooting. E.g. at 50 fps (0.02 spf), assuming the
49         // last frame was at t=0.980:
50         //
51         //   If we're at t=0.999, we wait until t=1.000 and return that.
52         //   If we're at t=1.001, we return t=1.000 immediately (small overshoot).
53         //   If we're at t=1.055, we drop two frames and return t=1.040 immediately.
54         void wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration, bool *is_preroll, std::chrono::steady_clock::time_point *frame_timestamp);
55
56         // Analogous to CaptureInterface. Will only return modes that have the right width/height.
57         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const { return video_modes; }
58
59         // IUnknown.
60         HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) override;
61         ULONG STDMETHODCALLTYPE AddRef() override;
62         ULONG STDMETHODCALLTYPE Release() override;
63
64         // IDeckLinkVideoOutputCallback.
65         HRESULT ScheduledFrameCompleted(/* in */ IDeckLinkVideoFrame *completedFrame, /* in */ BMDOutputFrameCompletionResult result) override;
66         HRESULT ScheduledPlaybackHasStopped() override;
67
68 private:
69         struct Frame : public IDeckLinkVideoFrame {
70         public:
71                 ~Frame();
72
73                 // IUnknown.
74                 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) override;
75                 ULONG STDMETHODCALLTYPE AddRef() override;
76                 ULONG STDMETHODCALLTYPE Release() override;
77
78                 // IDeckLinkVideoFrame.
79                 long GetWidth() override;
80                 long GetHeight() override;
81                 long GetRowBytes() override;
82                 BMDPixelFormat GetPixelFormat() override;
83                 BMDFrameFlags GetFlags() override;
84                 HRESULT GetBytes(/* out */ void **buffer) override;
85
86                 HRESULT GetTimecode(/* in */ BMDTimecodeFormat format, /* out */ IDeckLinkTimecode **timecode) override;
87                 HRESULT GetAncillaryData(/* out */ IDeckLinkVideoFrameAncillary **ancillary) override;
88
89         private:
90                 std::atomic<int> refcount{1};
91                 RefCountedGLsync fence;  // Needs to be waited on before uyvy_ptr can be read from.
92                 std::vector<RefCountedFrame> input_frames;  // Cannot be released before we are done rendering (ie., <fence> is asserted).
93                 ReceivedTimestamps received_ts;
94                 int64_t pts, duration;
95                 movit::ResourcePool *resource_pool;
96
97                 // These members are persistently allocated, and reused when the frame object is.
98                 GLuint uyvy_tex;  // Owned by <resource_pool>.
99                 GLuint pbo;
100                 uint8_t *uyvy_ptr;  // Persistent mapping into the PBO.
101
102                 // Current Blackmagic drivers (January 2017) have a bug where sending a PBO
103                 // pointer to the driver causes a kernel oops. Thus, we do an extra copy into
104                 // this pointer before giving the data to the driver. (We don't do a get
105                 // directly into this pointer, because e.g. Intel/Mesa hits a slow path when
106                 // you do readback into something that's not a PBO.) When Blackmagic fixes
107                 // the bug, we should drop this.
108                 std::unique_ptr<uint8_t[]> uyvy_ptr_local;
109
110                 friend class DeckLinkOutput;
111         };
112         std::unique_ptr<Frame> get_frame();
113         void create_uyvy(GLuint y_tex, GLuint cbcr_tex, GLuint dst_tex);
114
115         void present_thread_func();
116
117         std::atomic<int> refcount{1};
118
119         std::unique_ptr<ChromaSubsampler> chroma_subsampler;
120         std::map<uint32_t, bmusb::VideoMode> video_modes;
121
122         std::thread present_thread;
123         std::atomic<bool> should_quit{false};
124
125         std::mutex frame_queue_mutex;
126         std::queue<std::unique_ptr<Frame>> pending_video_frames;  // Under <frame_queue_mutex>.
127         std::queue<std::unique_ptr<Frame>> frame_freelist;  // Under <frame_queue_mutex>.
128         int num_frames_in_flight = 0;  // Number of frames allocated but not on the freelist. Under <frame_queue_mutex>.
129         std::condition_variable frame_queues_changed;
130         bool playback_initiated = false, playback_started = false;
131         int64_t base_pts, frame_duration;
132
133         movit::ResourcePool *resource_pool;
134         IDeckLinkOutput *output = nullptr;
135         BMDVideoConnection video_connection;
136         QSurface *surface;
137         unsigned width, height;
138         unsigned card_index;
139
140         GLuint uyvy_vbo;  // Holds position and texcoord data.
141         GLuint uyvy_program_num;  // Owned by <resource_pool>.
142         GLuint uyvy_position_attribute_index, uyvy_texcoord_attribute_index;
143 };
144
145 #endif  // !defined(_DECKLINK_OUTPUT_H)