]> git.sesse.net Git - nageru/blob - nageru/decklink_capture.h
Fix DeckLink capture using the 11.7 or newer drivers.
[nageru] / nageru / decklink_capture.h
1 #ifndef _DECKLINK_CAPTURE_H
2 #define _DECKLINK_CAPTURE_H 1
3
4 #include <DeckLinkAPI.h>
5 #include <stdint.h>
6 #include <atomic>
7 #include <functional>
8 #include <map>
9 #include <memory>
10 #include <set>
11 #include <string>
12
13 #include "DeckLinkAPIModes.h"
14 #include "DeckLinkAPITypes.h"
15 #include "LinuxCOM.h"
16 #include "bmusb/bmusb.h"
17
18 class IDeckLink;
19 class IDeckLinkConfiguration;
20
21 // TODO: Adjust CaptureInterface to be a little less bmusb-centric.
22 // There are too many member functions here that don't really do anything.
23 class DeckLinkCapture : public bmusb::CaptureInterface, IDeckLinkInputCallback
24 {
25 public:
26         DeckLinkCapture(IDeckLink *card, int card_index);  // Takes ownership of <card>.
27         ~DeckLinkCapture();
28
29         IDeckLinkInput *get_input() const { return input; }
30
31         // IDeckLinkInputCallback.
32         HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, LPVOID *) override;
33         ULONG STDMETHODCALLTYPE AddRef() override;
34         ULONG STDMETHODCALLTYPE Release() override;
35         HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(
36                 BMDVideoInputFormatChangedEvents,
37                 IDeckLinkDisplayMode*,
38                 BMDDetectedVideoInputFormatFlags) override;
39         HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(
40                 IDeckLinkVideoInputFrame *video_frame,
41                 IDeckLinkAudioInputPacket *audio_frame) override;
42
43         // CaptureInterface.
44         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
45         {
46                 video_frame_allocator = allocator;
47                 if (owned_video_frame_allocator.get() != allocator) {
48                         owned_video_frame_allocator.reset();
49                 }
50         }
51
52         bmusb::FrameAllocator *get_video_frame_allocator() override
53         {
54                 return video_frame_allocator;
55         }
56
57         // Does not take ownership.
58         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
59         {
60                 audio_frame_allocator = allocator;
61                 if (owned_audio_frame_allocator.get() != allocator) {
62                         owned_audio_frame_allocator.reset();
63                 }
64         }
65
66         bmusb::FrameAllocator *get_audio_frame_allocator() override
67         {
68                 return audio_frame_allocator;
69         }
70
71         void set_frame_callback(bmusb::frame_callback_t callback) override
72         {
73                 frame_callback = callback;
74         }
75
76         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
77         {
78                 dequeue_init_callback = init;
79                 dequeue_cleanup_callback = cleanup;
80                 has_dequeue_callbacks = true;
81         }
82
83         std::string get_description() const override
84         {
85                 return description;
86         }
87
88         void configure_card() override;
89         void start_bm_capture() override;
90         void stop_dequeue_thread() override;
91
92         // TODO: Can the API communicate this to us somehow, for e.g. Thunderbolt cards?
93         bool get_disconnected() const override { return false; }
94
95         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const override { return video_modes; }
96         void set_video_mode(uint32_t video_mode_id) override;
97         uint32_t get_current_video_mode() const override { return current_video_mode; }
98
99         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
100                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitYCbCr, bmusb::PixelFormat_10BitYCbCr };
101         }
102         void set_pixel_format(bmusb::PixelFormat pixel_format) override;
103         bmusb::PixelFormat get_current_pixel_format() const override {
104                 return current_pixel_format;
105         }
106
107         std::map<uint32_t, std::string> get_available_video_inputs() const override { return video_inputs; }
108         void set_video_input(uint32_t video_input_id) override;
109         uint32_t get_current_video_input() const override { return current_video_input; }
110
111         std::map<uint32_t, std::string> get_available_audio_inputs() const override { return audio_inputs; }
112         void set_audio_input(uint32_t audio_input_id) override;
113         uint32_t get_current_audio_input() const override { return current_audio_input; }
114
115 private:
116         void set_video_mode_no_restart(uint32_t video_mode_id);
117
118         std::atomic<int> refcount{1};
119         bool done_init = false;
120         std::string description;
121         uint16_t timecode = 0;
122         int card_index;
123
124         bool has_dequeue_callbacks = false;
125         std::function<void()> dequeue_init_callback = nullptr;
126         std::function<void()> dequeue_cleanup_callback = nullptr;
127
128         bmusb::FrameAllocator *video_frame_allocator = nullptr;
129         bmusb::FrameAllocator *audio_frame_allocator = nullptr;
130         std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
131         std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
132         bmusb::frame_callback_t frame_callback = nullptr;
133
134         IDeckLinkConfiguration *config = nullptr;
135
136         IDeckLink *card = nullptr;
137         IDeckLinkInput *input = nullptr;
138         BMDTimeValue frame_duration;
139         BMDTimeScale time_scale;
140         BMDFieldDominance field_dominance;
141         bool running = false;
142         bool supports_autodetect = false;
143
144         std::map<uint32_t, bmusb::VideoMode> video_modes;
145         BMDDisplayMode current_video_mode;
146         bmusb::PixelFormat current_pixel_format = bmusb::PixelFormat_8BitYCbCr;
147
148         std::map<uint32_t, std::string> video_inputs;
149         BMDVideoConnection current_video_input;
150
151         std::map<uint32_t, std::string> audio_inputs;
152         BMDAudioConnection current_audio_input;
153 };
154
155 #endif  // !defined(_DECKLINK_CAPTURE_H)