]> git.sesse.net Git - bmusb/blob - bmusb.h
6fdddedeb6e1d5781d8a9128cf67b5525fd5ef33
[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 <string>
11 #include <thread>
12 #include <vector>
13
14 struct libusb_transfer;
15
16 // An interface for frame allocators; if you do not specify one
17 // (using set_video_frame_allocator), a default one that pre-allocates
18 // a freelist of eight frames using new[] will be used. Specifying
19 // your own can be useful if you have special demands for where you want the
20 // frame to end up and don't want to spend the extra copy to get it there, for
21 // instance GPU memory.
22 class FrameAllocator {
23  public:
24         struct Frame {
25                 uint8_t *data = nullptr;
26                 uint8_t *data2 = nullptr;  // Only if interleaved == true.
27                 size_t len = 0;  // Number of bytes we actually have.
28                 size_t size = 0;  // Number of bytes we have room for.
29                 size_t overflow = 0;
30                 void *userdata = nullptr;
31                 FrameAllocator *owner = nullptr;
32
33                 // If set to true, every other byte will go to data and to data2.
34                 // If so, <len> and <size> are still about the number of total bytes
35                 // so if size == 1024, there's 512 bytes in data and 512 in data2.
36                 bool interleaved = false;
37         };
38
39         virtual ~FrameAllocator();
40
41         // Request a video frame. Note that this is called from the
42         // USB thread, which runs with realtime priority and is
43         // very sensitive to delays. Thus, you should not do anything
44         // here that might sleep, including calling malloc().
45         // (Taking a mutex is borderline.)
46         //
47         // The Frame object will be given to the frame callback,
48         // which is responsible for releasing the video frame back
49         // once it is usable for new frames (ie., it will no longer
50         // be read from). You can use the "userdata" pointer for
51         // whatever you want to identify this frame if you need to.
52         //
53         // Returning a Frame with data==nullptr is allowed;
54         // if so, the frame in progress will be dropped.
55         virtual Frame alloc_frame() = 0;
56
57         virtual void release_frame(Frame frame) = 0;
58 };
59
60 typedef std::function<void(uint16_t timecode,
61                            FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
62                            FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)>
63         frame_callback_t;
64
65 // The actual capturing class, representing capture from a single card.
66 class BMUSBCapture {
67  public:
68         BMUSBCapture(int card_index)
69                 : card_index(card_index)
70         {
71         }
72
73         // Does not take ownership.
74         void set_video_frame_allocator(FrameAllocator *allocator)
75         {
76                 video_frame_allocator = allocator;
77         }
78
79         FrameAllocator *get_video_frame_allocator()
80         {
81                 return video_frame_allocator;
82         }
83
84         // Does not take ownership.
85         void set_audio_frame_allocator(FrameAllocator *allocator)
86         {
87                 audio_frame_allocator = allocator;
88         }
89
90         FrameAllocator *get_audio_frame_allocator()
91         {
92                 return audio_frame_allocator;
93         }
94
95         void set_frame_callback(frame_callback_t callback)
96         {
97                 frame_callback = callback;
98         }
99
100         // Needs to be run before configure_card().
101         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup)
102         {
103                 dequeue_init_callback = init;
104                 dequeue_cleanup_callback = cleanup;
105                 has_dequeue_callbacks = true;
106         }
107
108         // Only valid after configure_card().
109         std::string get_description() const {
110                 return description;
111         }
112
113         void configure_card();
114         void start_bm_capture();
115         void stop_dequeue_thread();
116
117         static void start_bm_thread();
118         static void stop_bm_thread();
119
120  private:
121         struct QueuedFrame {
122                 uint16_t timecode;
123                 uint16_t format;
124                 FrameAllocator::Frame frame;
125         };
126
127         void start_new_audio_block(const uint8_t *start);
128         void start_new_frame(const uint8_t *start);
129
130         void queue_frame(uint16_t format, uint16_t timecode, FrameAllocator::Frame frame, std::deque<QueuedFrame> *q);
131         void dequeue_thread_func();
132
133         static void usb_thread_func();
134         static void cb_xfr(struct libusb_transfer *xfr);
135
136         std::string description;
137
138         FrameAllocator::Frame current_video_frame;
139         FrameAllocator::Frame current_audio_frame;
140
141         std::mutex queue_lock;
142         std::condition_variable queues_not_empty;
143         std::deque<QueuedFrame> pending_video_frames;
144         std::deque<QueuedFrame> pending_audio_frames;
145
146         FrameAllocator *video_frame_allocator = nullptr;
147         FrameAllocator *audio_frame_allocator = nullptr;
148         frame_callback_t frame_callback = nullptr;
149
150         std::thread dequeue_thread;
151         std::atomic<bool> dequeue_thread_should_quit;
152         bool has_dequeue_callbacks = false;
153         std::function<void()> dequeue_init_callback = nullptr;
154         std::function<void()> dequeue_cleanup_callback = nullptr;
155
156         int current_register = 0;
157
158         static constexpr int NUM_BMUSB_REGISTERS = 60;
159         uint8_t register_file[NUM_BMUSB_REGISTERS];
160
161         int card_index;
162         std::vector<libusb_transfer *> iso_xfrs;
163         int assumed_frame_width = 1280;
164 };
165
166 // Get details for the given video format; returns false if detection was incomplete.
167 // Note: Frame rate is _frame_ rate, not field rate. So 1080i60 gets 30/1, _not_ 60/1.
168 // "second_field_start" is only valid for interlaced modes; it signifies
169 // how many lines from the very top of the frame there are before the second field
170 // starts (so it will always be >= height/2 + extra_lines_top).
171 bool decode_video_format(uint16_t video_format, unsigned *width, unsigned *height, unsigned *second_field_start,
172                          unsigned *extra_lines_top, unsigned *extra_lines_bottom,
173                          unsigned *frame_rate_nom, unsigned *frame_rate_den, bool *interlaced);
174
175 #endif