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