6 #include <condition_variable>
16 struct libusb_device_handle;
17 struct libusb_transfer;
19 // An interface for frame allocators; if you do not specify one
20 // (using set_video_frame_allocator), a default one that pre-allocates
21 // a freelist of eight frames using new[] will be used. Specifying
22 // your own can be useful if you have special demands for where you want the
23 // frame to end up and don't want to spend the extra copy to get it there, for
24 // instance GPU memory.
25 class FrameAllocator {
28 uint8_t *data = nullptr;
29 uint8_t *data2 = nullptr; // Only if interleaved == true.
30 size_t len = 0; // Number of bytes we actually have.
31 size_t size = 0; // Number of bytes we have room for.
33 void *userdata = nullptr;
34 FrameAllocator *owner = nullptr;
36 // If set to true, every other byte will go to data and to data2.
37 // If so, <len> and <size> are still about the number of total bytes
38 // so if size == 1024, there's 512 bytes in data and 512 in data2.
39 bool interleaved = false;
42 virtual ~FrameAllocator();
44 // Request a video frame. Note that this is called from the
45 // USB thread, which runs with realtime priority and is
46 // very sensitive to delays. Thus, you should not do anything
47 // here that might sleep, including calling malloc().
48 // (Taking a mutex is borderline.)
50 // The Frame object will be given to the frame callback,
51 // which is responsible for releasing the video frame back
52 // once it is usable for new frames (ie., it will no longer
53 // be read from). You can use the "userdata" pointer for
54 // whatever you want to identify this frame if you need to.
56 // Returning a Frame with data==nullptr is allowed;
57 // if so, the frame in progress will be dropped.
58 virtual Frame alloc_frame() = 0;
60 virtual void release_frame(Frame frame) = 0;
63 // Audio is more important than video, and also much cheaper.
64 // By having many more audio frames available, hopefully if something
65 // starts to drop, we'll have CPU load go down (from not having to
66 // process as much video) before we have to drop audio.
67 #define NUM_QUEUED_VIDEO_FRAMES 16
68 #define NUM_QUEUED_AUDIO_FRAMES 64
70 class MallocFrameAllocator : public FrameAllocator {
72 MallocFrameAllocator(size_t frame_size, size_t num_queued_frames);
73 Frame alloc_frame() override;
74 void release_frame(Frame frame) override;
79 std::mutex freelist_mutex;
80 std::stack<std::unique_ptr<uint8_t[]>> freelist; // All of size <frame_size>.
83 // Represents an input mode you can tune a card to.
86 bool autodetect = false; // If true, all the remaining fields are irrelevant.
87 unsigned width = 0, height = 0;
88 unsigned frame_rate_num = 0, frame_rate_den = 0;
89 bool interlaced = false;
92 // Represents the format of an actual frame coming in.
94 uint16_t id = 0; // For debugging/logging only.
95 unsigned width = 0, height = 0, second_field_start = 0;
96 unsigned extra_lines_top = 0, extra_lines_bottom = 0;
97 unsigned frame_rate_nom = 0, frame_rate_den = 0;
98 bool interlaced = false;
99 bool has_signal = false;
103 uint16_t id = 0; // For debugging/logging only.
104 unsigned bits_per_sample = 0;
105 unsigned num_channels = 0;
108 typedef std::function<void(uint16_t timecode,
109 FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
110 FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format)>
113 class CaptureInterface {
115 virtual ~CaptureInterface() {}
117 virtual std::map<uint32_t, VideoMode> get_available_video_modes() const = 0;
118 virtual uint32_t get_current_video_mode() const = 0;
119 virtual void set_video_mode(uint32_t video_mode_id) = 0;
121 virtual std::map<uint32_t, std::string> get_available_video_inputs() const = 0;
122 virtual void set_video_input(uint32_t video_input_id) = 0;
123 virtual uint32_t get_current_video_input() const = 0;
125 virtual std::map<uint32_t, std::string> get_available_audio_inputs() const = 0;
126 virtual void set_audio_input(uint32_t audio_input_id) = 0;
127 virtual uint32_t get_current_audio_input() const = 0;
129 // Does not take ownership.
130 virtual void set_video_frame_allocator(FrameAllocator *allocator) = 0;
132 virtual FrameAllocator *get_video_frame_allocator() = 0;
134 // Does not take ownership.
135 virtual void set_audio_frame_allocator(FrameAllocator *allocator) = 0;
137 virtual FrameAllocator *get_audio_frame_allocator() = 0;
139 virtual void set_frame_callback(frame_callback_t callback) = 0;
141 // Needs to be run before configure_card().
142 virtual void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) = 0;
144 // Only valid after configure_card().
145 virtual std::string get_description() const = 0;
147 virtual void configure_card() = 0;
149 virtual void start_bm_capture() = 0;
151 virtual void stop_dequeue_thread() = 0;
154 // The actual capturing class, representing capture from a single card.
155 class BMUSBCapture : public CaptureInterface {
157 BMUSBCapture(int card_index)
158 : card_index(card_index)
164 std::map<uint32_t, VideoMode> get_available_video_modes() const override;
165 uint32_t get_current_video_mode() const override;
166 void set_video_mode(uint32_t video_mode_id) override;
168 virtual std::map<uint32_t, std::string> get_available_video_inputs() const override;
169 virtual void set_video_input(uint32_t video_input_id) override;
170 virtual uint32_t get_current_video_input() const override { return current_video_input; }
172 virtual std::map<uint32_t, std::string> get_available_audio_inputs() const override;
173 virtual void set_audio_input(uint32_t audio_input_id) override;
174 virtual uint32_t get_current_audio_input() const override { return current_audio_input; }
176 // Does not take ownership.
177 void set_video_frame_allocator(FrameAllocator *allocator) override
179 video_frame_allocator = allocator;
182 FrameAllocator *get_video_frame_allocator() override
184 return video_frame_allocator;
187 // Does not take ownership.
188 void set_audio_frame_allocator(FrameAllocator *allocator) override
190 audio_frame_allocator = allocator;
193 FrameAllocator *get_audio_frame_allocator() override
195 return audio_frame_allocator;
198 void set_frame_callback(frame_callback_t callback) override
200 frame_callback = callback;
203 // Needs to be run before configure_card().
204 void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
206 dequeue_init_callback = init;
207 dequeue_cleanup_callback = cleanup;
208 has_dequeue_callbacks = true;
211 // Only valid after configure_card().
212 std::string get_description() const override {
216 void configure_card() override;
217 void start_bm_capture() override;
218 void stop_dequeue_thread() override;
220 // TODO: It's rather messy to have these outside the interface.
221 static void start_bm_thread();
222 static void stop_bm_thread();
228 FrameAllocator::Frame frame;
231 void start_new_audio_block(const uint8_t *start);
232 void start_new_frame(const uint8_t *start);
234 void queue_frame(uint16_t format, uint16_t timecode, FrameAllocator::Frame frame, std::deque<QueuedFrame> *q);
235 void dequeue_thread_func();
237 static void usb_thread_func();
238 static void cb_xfr(struct libusb_transfer *xfr);
240 void update_capture_mode();
242 std::string description;
244 FrameAllocator::Frame current_video_frame;
245 FrameAllocator::Frame current_audio_frame;
247 std::mutex queue_lock;
248 std::condition_variable queues_not_empty;
249 std::deque<QueuedFrame> pending_video_frames;
250 std::deque<QueuedFrame> pending_audio_frames;
252 FrameAllocator *video_frame_allocator = nullptr;
253 FrameAllocator *audio_frame_allocator = nullptr;
254 frame_callback_t frame_callback = nullptr;
256 std::thread dequeue_thread;
257 std::atomic<bool> dequeue_thread_should_quit;
258 bool has_dequeue_callbacks = false;
259 std::function<void()> dequeue_init_callback = nullptr;
260 std::function<void()> dequeue_cleanup_callback = nullptr;
262 int current_register = 0;
264 static constexpr int NUM_BMUSB_REGISTERS = 60;
265 uint8_t register_file[NUM_BMUSB_REGISTERS];
268 std::vector<libusb_transfer *> iso_xfrs;
269 int assumed_frame_width = 1280;
271 libusb_device_handle *devh = nullptr;
272 uint32_t current_video_input = 0x00000000; // HDMI/SDI.
273 uint32_t current_audio_input = 0x00000000; // Embedded.
276 // Get details for the given video format; returns false if detection was incomplete.
277 // Note: Frame rate is _frame_ rate, not field rate. So 1080i60 gets 30/1, _not_ 60/1.
278 // "second_field_start" is only valid for interlaced modes; it signifies
279 // how many lines from the very top of the frame there are before the second field
280 // starts (so it will always be >= height/2 + extra_lines_top).
281 bool decode_video_format(uint16_t video_format, VideoFormat *decoded_video_format);