]> git.sesse.net Git - bmusb/blob - bmusb.h
Send the audio format explicitly down, not just the ID.
[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 struct VideoFormat {
61         uint16_t id = 0;  // For debugging/logging only.
62         unsigned width = 0, height = 0, second_field_start = 0;
63         unsigned extra_lines_top = 0, extra_lines_bottom = 0;
64         unsigned frame_rate_nom = 0, frame_rate_den = 0;
65         bool interlaced = false;
66         bool has_signal = false;
67 };
68
69 struct AudioFormat {
70         uint16_t id = 0;  // For debugging/logging only.
71         unsigned bits_per_sample = 0;
72         unsigned num_channels = 0;
73 };
74
75 typedef std::function<void(uint16_t timecode,
76                            FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
77                            FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format)>
78         frame_callback_t;
79
80 class CaptureInterface {
81  public:
82         virtual ~CaptureInterface() {}
83
84         // Does not take ownership.
85         virtual void set_video_frame_allocator(FrameAllocator *allocator) = 0;
86
87         virtual FrameAllocator *get_video_frame_allocator() = 0;
88
89         // Does not take ownership.
90         virtual void set_audio_frame_allocator(FrameAllocator *allocator) = 0;
91
92         virtual FrameAllocator *get_audio_frame_allocator() = 0;
93
94         virtual void set_frame_callback(frame_callback_t callback) = 0;
95
96         // Needs to be run before configure_card().
97         virtual void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) = 0;
98
99         // Only valid after configure_card().
100         virtual std::string get_description() const = 0;
101
102         virtual void configure_card() = 0;
103
104         virtual void start_bm_capture() = 0;
105
106         virtual void stop_dequeue_thread() = 0;
107 };
108
109 // The actual capturing class, representing capture from a single card.
110 class BMUSBCapture : public CaptureInterface {
111  public:
112         BMUSBCapture(int card_index)
113                 : card_index(card_index)
114         {
115         }
116
117         ~BMUSBCapture() {}
118
119         // Does not take ownership.
120         void set_video_frame_allocator(FrameAllocator *allocator) override
121         {
122                 video_frame_allocator = allocator;
123         }
124
125         FrameAllocator *get_video_frame_allocator() override
126         {
127                 return video_frame_allocator;
128         }
129
130         // Does not take ownership.
131         void set_audio_frame_allocator(FrameAllocator *allocator) override
132         {
133                 audio_frame_allocator = allocator;
134         }
135
136         FrameAllocator *get_audio_frame_allocator() override
137         {
138                 return audio_frame_allocator;
139         }
140
141         void set_frame_callback(frame_callback_t callback) override
142         {
143                 frame_callback = callback;
144         }
145
146         // Needs to be run before configure_card().
147         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
148         {
149                 dequeue_init_callback = init;
150                 dequeue_cleanup_callback = cleanup;
151                 has_dequeue_callbacks = true;
152         }
153
154         // Only valid after configure_card().
155         std::string get_description() const override {
156                 return description;
157         }
158
159         void configure_card() override;
160         void start_bm_capture() override;
161         void stop_dequeue_thread() override;
162
163         // TODO: It's rather messy to have these outside the interface.
164         static void start_bm_thread();
165         static void stop_bm_thread();
166
167  private:
168         struct QueuedFrame {
169                 uint16_t timecode;
170                 uint16_t format;
171                 FrameAllocator::Frame frame;
172         };
173
174         void start_new_audio_block(const uint8_t *start);
175         void start_new_frame(const uint8_t *start);
176
177         void queue_frame(uint16_t format, uint16_t timecode, FrameAllocator::Frame frame, std::deque<QueuedFrame> *q);
178         void dequeue_thread_func();
179
180         static void usb_thread_func();
181         static void cb_xfr(struct libusb_transfer *xfr);
182
183         std::string description;
184
185         FrameAllocator::Frame current_video_frame;
186         FrameAllocator::Frame current_audio_frame;
187
188         std::mutex queue_lock;
189         std::condition_variable queues_not_empty;
190         std::deque<QueuedFrame> pending_video_frames;
191         std::deque<QueuedFrame> pending_audio_frames;
192
193         FrameAllocator *video_frame_allocator = nullptr;
194         FrameAllocator *audio_frame_allocator = nullptr;
195         frame_callback_t frame_callback = nullptr;
196
197         std::thread dequeue_thread;
198         std::atomic<bool> dequeue_thread_should_quit;
199         bool has_dequeue_callbacks = false;
200         std::function<void()> dequeue_init_callback = nullptr;
201         std::function<void()> dequeue_cleanup_callback = nullptr;
202
203         int current_register = 0;
204
205         static constexpr int NUM_BMUSB_REGISTERS = 60;
206         uint8_t register_file[NUM_BMUSB_REGISTERS];
207
208         int card_index;
209         std::vector<libusb_transfer *> iso_xfrs;
210         int assumed_frame_width = 1280;
211 };
212
213 // Get details for the given video format; returns false if detection was incomplete.
214 // Note: Frame rate is _frame_ rate, not field rate. So 1080i60 gets 30/1, _not_ 60/1.
215 // "second_field_start" is only valid for interlaced modes; it signifies
216 // how many lines from the very top of the frame there are before the second field
217 // starts (so it will always be >= height/2 + extra_lines_top).
218 bool decode_video_format(uint16_t video_format, VideoFormat *decoded_video_format);
219
220 #endif