]> git.sesse.net Git - nageru/blob - decklink_capture.h
Release Nageru 1.7.2.
[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         // IDeckLinkInputCallback.
30         HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, LPVOID *) override;
31         ULONG STDMETHODCALLTYPE AddRef() override;
32         ULONG STDMETHODCALLTYPE Release() override;
33         HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(
34                 BMDVideoInputFormatChangedEvents,
35                 IDeckLinkDisplayMode*,
36                 BMDDetectedVideoInputFormatFlags) override;
37         HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(
38                 IDeckLinkVideoInputFrame *video_frame,
39                 IDeckLinkAudioInputPacket *audio_frame) override;
40
41         // CaptureInterface.
42         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
43         {
44                 video_frame_allocator = allocator;
45                 if (owned_video_frame_allocator.get() != allocator) {
46                         owned_video_frame_allocator.reset();
47                 }
48         }
49
50         bmusb::FrameAllocator *get_video_frame_allocator() override
51         {
52                 return video_frame_allocator;
53         }
54
55         // Does not take ownership.
56         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
57         {
58                 audio_frame_allocator = allocator;
59                 if (owned_audio_frame_allocator.get() != allocator) {
60                         owned_audio_frame_allocator.reset();
61                 }
62         }
63
64         bmusb::FrameAllocator *get_audio_frame_allocator() override
65         {
66                 return audio_frame_allocator;
67         }
68
69         void set_frame_callback(bmusb::frame_callback_t callback) override
70         {
71                 frame_callback = callback;
72         }
73
74         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
75         {
76                 dequeue_init_callback = init;
77                 dequeue_cleanup_callback = cleanup;
78                 has_dequeue_callbacks = true;
79         }
80
81         std::string get_description() const override
82         {
83                 return description;
84         }
85
86         void configure_card() override;
87         void start_bm_capture() override;
88         void stop_dequeue_thread() override;
89
90         // TODO: Can the API communicate this to us somehow, for e.g. Thunderbolt cards?
91         bool get_disconnected() const override { return false; }
92
93         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const override { return video_modes; }
94         void set_video_mode(uint32_t video_mode_id) override;
95         uint32_t get_current_video_mode() const override { return current_video_mode; }
96
97         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
98                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitYCbCr, bmusb::PixelFormat_10BitYCbCr };
99         }
100         void set_pixel_format(bmusb::PixelFormat pixel_format) override;
101         bmusb::PixelFormat get_current_pixel_format() const override {
102                 return current_pixel_format;
103         }
104
105         std::map<uint32_t, std::string> get_available_video_inputs() const override { return video_inputs; }
106         void set_video_input(uint32_t video_input_id) override;
107         uint32_t get_current_video_input() const override { return current_video_input; }
108
109         std::map<uint32_t, std::string> get_available_audio_inputs() const override { return audio_inputs; }
110         void set_audio_input(uint32_t audio_input_id) override;
111         uint32_t get_current_audio_input() const override { return current_audio_input; }
112
113 private:
114         void set_video_mode_no_restart(uint32_t video_mode_id);
115
116         std::atomic<int> refcount{1};
117         bool done_init = false;
118         std::string description;
119         uint16_t timecode = 0;
120         int card_index;
121
122         bool has_dequeue_callbacks = false;
123         std::function<void()> dequeue_init_callback = nullptr;
124         std::function<void()> dequeue_cleanup_callback = nullptr;
125
126         bmusb::FrameAllocator *video_frame_allocator = nullptr;
127         bmusb::FrameAllocator *audio_frame_allocator = nullptr;
128         std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
129         std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
130         bmusb::frame_callback_t frame_callback = nullptr;
131
132         IDeckLinkConfiguration *config = nullptr;
133
134         IDeckLink *card = nullptr;
135         IDeckLinkInput *input = nullptr;
136         BMDTimeValue frame_duration;
137         BMDTimeScale time_scale;
138         BMDFieldDominance field_dominance;
139         bool running = false;
140         bool supports_autodetect = false;
141
142         std::map<uint32_t, bmusb::VideoMode> video_modes;
143         BMDDisplayMode current_video_mode;
144         bmusb::PixelFormat current_pixel_format = bmusb::PixelFormat_8BitYCbCr;
145
146         std::map<uint32_t, std::string> video_inputs;
147         BMDVideoConnection current_video_input;
148
149         std::map<uint32_t, std::string> audio_inputs;
150         BMDAudioConnection current_audio_input;
151 };
152
153 #endif  // !defined(_DECKLINK_CAPTURE_H)