]> git.sesse.net Git - bmusb/blob - bmusb.h
Never send in a frame rate of 0/0.
[bmusb] / bmusb.h
1 #ifndef _BMUSB_H
2 #define _BMUSB_H
3
4 #include <stdint.h>
5 #include <atomic>
6 #include <condition_variable>
7 #include <deque>
8 #include <functional>
9 #include <mutex>
10 #include <stack>
11 #include <string>
12 #include <thread>
13 #include <vector>
14
15 struct libusb_transfer;
16
17 // An interface for frame allocators; if you do not specify one
18 // (using set_video_frame_allocator), a default one that pre-allocates
19 // a freelist of eight frames using new[] will be used. Specifying
20 // your own can be useful if you have special demands for where you want the
21 // frame to end up and don't want to spend the extra copy to get it there, for
22 // instance GPU memory.
23 class FrameAllocator {
24  public:
25         struct Frame {
26                 uint8_t *data = nullptr;
27                 uint8_t *data2 = nullptr;  // Only if interleaved == true.
28                 size_t len = 0;  // Number of bytes we actually have.
29                 size_t size = 0;  // Number of bytes we have room for.
30                 size_t overflow = 0;
31                 void *userdata = nullptr;
32                 FrameAllocator *owner = nullptr;
33
34                 // If set to true, every other byte will go to data and to data2.
35                 // If so, <len> and <size> are still about the number of total bytes
36                 // so if size == 1024, there's 512 bytes in data and 512 in data2.
37                 bool interleaved = false;
38         };
39
40         virtual ~FrameAllocator();
41
42         // Request a video frame. Note that this is called from the
43         // USB thread, which runs with realtime priority and is
44         // very sensitive to delays. Thus, you should not do anything
45         // here that might sleep, including calling malloc().
46         // (Taking a mutex is borderline.)
47         //
48         // The Frame object will be given to the frame callback,
49         // which is responsible for releasing the video frame back
50         // once it is usable for new frames (ie., it will no longer
51         // be read from). You can use the "userdata" pointer for
52         // whatever you want to identify this frame if you need to.
53         //
54         // Returning a Frame with data==nullptr is allowed;
55         // if so, the frame in progress will be dropped.
56         virtual Frame alloc_frame() = 0;
57
58         virtual void release_frame(Frame frame) = 0;
59 };
60
61 // Audio is more important than video, and also much cheaper.
62 // By having many more audio frames available, hopefully if something
63 // starts to drop, we'll have CPU load go down (from not having to
64 // process as much video) before we have to drop audio.
65 #define NUM_QUEUED_VIDEO_FRAMES 16
66 #define NUM_QUEUED_AUDIO_FRAMES 64
67
68 class MallocFrameAllocator : public FrameAllocator {
69 public:
70         MallocFrameAllocator(size_t frame_size, size_t num_queued_frames);
71         Frame alloc_frame() override;
72         void release_frame(Frame frame) override;
73
74 private:
75         size_t frame_size;
76
77         std::mutex freelist_mutex;
78         std::stack<std::unique_ptr<uint8_t[]>> freelist;  // All of size <frame_size>.
79 };
80
81 struct VideoFormat {
82         uint16_t id = 0;  // For debugging/logging only.
83         unsigned width = 0, height = 0, second_field_start = 0;
84         unsigned extra_lines_top = 0, extra_lines_bottom = 0;
85         unsigned frame_rate_nom = 0, frame_rate_den = 0;
86         bool interlaced = false;
87         bool has_signal = false;
88 };
89
90 struct AudioFormat {
91         uint16_t id = 0;  // For debugging/logging only.
92         unsigned bits_per_sample = 0;
93         unsigned num_channels = 0;
94 };
95
96 typedef std::function<void(uint16_t timecode,
97                            FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
98                            FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format)>
99         frame_callback_t;
100
101 class CaptureInterface {
102  public:
103         virtual ~CaptureInterface() {}
104
105         // Does not take ownership.
106         virtual void set_video_frame_allocator(FrameAllocator *allocator) = 0;
107
108         virtual FrameAllocator *get_video_frame_allocator() = 0;
109
110         // Does not take ownership.
111         virtual void set_audio_frame_allocator(FrameAllocator *allocator) = 0;
112
113         virtual FrameAllocator *get_audio_frame_allocator() = 0;
114
115         virtual void set_frame_callback(frame_callback_t callback) = 0;
116
117         // Needs to be run before configure_card().
118         virtual void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) = 0;
119
120         // Only valid after configure_card().
121         virtual std::string get_description() const = 0;
122
123         virtual void configure_card() = 0;
124
125         virtual void start_bm_capture() = 0;
126
127         virtual void stop_dequeue_thread() = 0;
128 };
129
130 // The actual capturing class, representing capture from a single card.
131 class BMUSBCapture : public CaptureInterface {
132  public:
133         BMUSBCapture(int card_index)
134                 : card_index(card_index)
135         {
136         }
137
138         ~BMUSBCapture() {}
139
140         // Does not take ownership.
141         void set_video_frame_allocator(FrameAllocator *allocator) override
142         {
143                 video_frame_allocator = allocator;
144         }
145
146         FrameAllocator *get_video_frame_allocator() override
147         {
148                 return video_frame_allocator;
149         }
150
151         // Does not take ownership.
152         void set_audio_frame_allocator(FrameAllocator *allocator) override
153         {
154                 audio_frame_allocator = allocator;
155         }
156
157         FrameAllocator *get_audio_frame_allocator() override
158         {
159                 return audio_frame_allocator;
160         }
161
162         void set_frame_callback(frame_callback_t callback) override
163         {
164                 frame_callback = callback;
165         }
166
167         // Needs to be run before configure_card().
168         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
169         {
170                 dequeue_init_callback = init;
171                 dequeue_cleanup_callback = cleanup;
172                 has_dequeue_callbacks = true;
173         }
174
175         // Only valid after configure_card().
176         std::string get_description() const override {
177                 return description;
178         }
179
180         void configure_card() override;
181         void start_bm_capture() override;
182         void stop_dequeue_thread() override;
183
184         // TODO: It's rather messy to have these outside the interface.
185         static void start_bm_thread();
186         static void stop_bm_thread();
187
188  private:
189         struct QueuedFrame {
190                 uint16_t timecode;
191                 uint16_t format;
192                 FrameAllocator::Frame frame;
193         };
194
195         void start_new_audio_block(const uint8_t *start);
196         void start_new_frame(const uint8_t *start);
197
198         void queue_frame(uint16_t format, uint16_t timecode, FrameAllocator::Frame frame, std::deque<QueuedFrame> *q);
199         void dequeue_thread_func();
200
201         static void usb_thread_func();
202         static void cb_xfr(struct libusb_transfer *xfr);
203
204         std::string description;
205
206         FrameAllocator::Frame current_video_frame;
207         FrameAllocator::Frame current_audio_frame;
208
209         std::mutex queue_lock;
210         std::condition_variable queues_not_empty;
211         std::deque<QueuedFrame> pending_video_frames;
212         std::deque<QueuedFrame> pending_audio_frames;
213
214         FrameAllocator *video_frame_allocator = nullptr;
215         FrameAllocator *audio_frame_allocator = nullptr;
216         frame_callback_t frame_callback = nullptr;
217
218         std::thread dequeue_thread;
219         std::atomic<bool> dequeue_thread_should_quit;
220         bool has_dequeue_callbacks = false;
221         std::function<void()> dequeue_init_callback = nullptr;
222         std::function<void()> dequeue_cleanup_callback = nullptr;
223
224         int current_register = 0;
225
226         static constexpr int NUM_BMUSB_REGISTERS = 60;
227         uint8_t register_file[NUM_BMUSB_REGISTERS];
228
229         int card_index;
230         std::vector<libusb_transfer *> iso_xfrs;
231         int assumed_frame_width = 1280;
232 };
233
234 // Get details for the given video format; returns false if detection was incomplete.
235 // Note: Frame rate is _frame_ rate, not field rate. So 1080i60 gets 30/1, _not_ 60/1.
236 // "second_field_start" is only valid for interlaced modes; it signifies
237 // how many lines from the very top of the frame there are before the second field
238 // starts (so it will always be >= height/2 + extra_lines_top).
239 bool decode_video_format(uint16_t video_format, VideoFormat *decoded_video_format);
240
241 #endif