]> git.sesse.net Git - bmusb/blob - bmusb.h
Initial checkin.
[bmusb] / bmusb.h
1 #ifndef _BMUSB_H
2 #define _BMUSB_H
3
4 #include <stdint.h>
5 #include <functional>
6
7 // An interface for frame allocators; if you do not specify one
8 // (using set_video_frame_allocator), a default one that pre-allocates
9 // a freelist of eight frames using new[] will be used. Specifying
10 // your own can be useful if you have special demands for where you want the
11 // frame to end up and don't want to spend the extra copy to get it there, for
12 // instance GPU memory.
13 class FrameAllocator {
14  public:
15         struct Frame {
16                 uint8_t *data = nullptr;
17                 size_t len = 0;  // Number of bytes we actually have.
18                 size_t size = 0;  // Number of bytes we have room for.
19                 void *userdata = nullptr;
20                 FrameAllocator *owner = nullptr;
21         };
22
23         virtual ~FrameAllocator();
24
25         // Request a video frame. Note that this is called from the
26         // USB thread, which runs with realtime priority and is
27         // very sensitive to delays. Thus, you should not do anything
28         // here that might sleep, including calling malloc().
29         // (Taking a mutex is borderline.)
30         //
31         // The Frame object will be given to the frame callback,
32         // which is responsible for releasing the video frame back
33         // once it is usable for new frames (ie., it will no longer
34         // be read from). You can use the "userdata" pointer for
35         // whatever you want to identify this frame if you need to.
36         //
37         // Returning a Frame with data==nullptr is allowed;
38         // if so, the frame in progress will be dropped.
39         virtual Frame alloc_frame() = 0;
40
41         virtual void release_frame(Frame frame) = 0;
42 };
43
44 typedef std::function<void(uint16_t timecode,
45                            FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
46                            FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)>
47         frame_callback_t;
48
49 void set_video_frame_allocator(FrameAllocator *allocator);  // Does not take ownership.
50 FrameAllocator *get_video_frame_allocator();
51
52 void set_audio_frame_allocator(FrameAllocator *allocator);  // Does not take ownership.
53 FrameAllocator *get_audio_frame_allocator();
54
55 void set_frame_callback(frame_callback_t callback);
56 void start_bm_capture();
57
58 #endif