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