]> git.sesse.net Git - bmusb/blob - bmusb/fake_capture.h
Release 0.7.8.
[bmusb] / bmusb / fake_capture.h
1 #ifndef _FAKE_CAPTURE_H
2 #define _FAKE_CAPTURE_H 1
3
4 #include <stdint.h>
5 #include <functional>
6 #include <string>
7
8 #include "bmusb/bmusb.h"
9
10 namespace bmusb {
11
12 class FakeCapture : public CaptureInterface
13 {
14 public:
15         FakeCapture(unsigned width, unsigned height, unsigned fps, unsigned audio_sample_frequency, int card_index, bool has_audio = false);
16         ~FakeCapture();
17
18         // CaptureInterface.
19         void set_video_frame_allocator(FrameAllocator *allocator) override
20         {
21                 video_frame_allocator = allocator;
22                 if (owned_video_frame_allocator.get() != allocator) {
23                         owned_video_frame_allocator.reset();
24                 }
25         }
26
27         FrameAllocator *get_video_frame_allocator() override
28         {
29                 return video_frame_allocator;
30         }
31
32         // Does not take ownership.
33         void set_audio_frame_allocator(FrameAllocator *allocator) override
34         {
35                 audio_frame_allocator = allocator;
36                 if (owned_audio_frame_allocator.get() != allocator) {
37                         owned_audio_frame_allocator.reset();
38                 }
39         }
40
41         FrameAllocator *get_audio_frame_allocator() override
42         {
43                 return audio_frame_allocator;
44         }
45
46         void set_frame_callback(frame_callback_t callback) override
47         {
48                 frame_callback = callback;
49         }
50
51         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
52         {
53                 dequeue_init_callback = init;
54                 dequeue_cleanup_callback = cleanup;
55                 has_dequeue_callbacks = true;
56         }
57
58         std::string get_description() const override
59         {
60                 return description;
61         }
62
63         void configure_card() override;
64         void start_bm_capture() override;
65         void stop_dequeue_thread() override;
66         bool get_disconnected() const override { return false; }
67
68         std::set<PixelFormat> get_available_pixel_formats() const override
69         {
70                 return std::set<PixelFormat>{ PixelFormat_8BitYCbCr, PixelFormat_10BitYCbCr };
71         }
72
73         void set_pixel_format(PixelFormat pixel_format) override
74         {
75                 current_pixel_format = pixel_format;
76         }
77
78         PixelFormat get_current_pixel_format() const
79         {
80                 return current_pixel_format;
81         }
82
83         std::map<uint32_t, VideoMode> get_available_video_modes() const override;
84         void set_video_mode(uint32_t video_mode_id) override;
85         uint32_t get_current_video_mode() const override { return 0; }
86
87         std::map<uint32_t, std::string> get_available_video_inputs() const override;
88         void set_video_input(uint32_t video_input_id) override;
89         uint32_t get_current_video_input() const override { return 0; }
90
91         std::map<uint32_t, std::string> get_available_audio_inputs() const override;
92         void set_audio_input(uint32_t audio_input_id) override;
93         uint32_t get_current_audio_input() const override { return 0; }
94
95 private:
96         void producer_thread_func();
97         void make_tone(int32_t *out, unsigned num_stereo_samples, unsigned num_channels);
98
99         unsigned width, height, fps, audio_sample_frequency;
100         PixelFormat current_pixel_format = PixelFormat_8BitYCbCr;
101         int card_index;
102         uint8_t y, cb, cr;
103
104         // sin(2 * pi * f / F) and similar for cos. Used for fast sine generation.
105         // Zero when no audio.
106         float audio_sin = 0.0f, audio_cos = 0.0f;
107         float audio_real = 0.0f, audio_imag = 0.0f;  // Current state of the audio phaser.
108         float audio_ref_level;
109
110         bool has_dequeue_callbacks = false;
111         std::function<void()> dequeue_init_callback = nullptr;
112         std::function<void()> dequeue_cleanup_callback = nullptr;
113
114         FrameAllocator *video_frame_allocator = nullptr;
115         FrameAllocator *audio_frame_allocator = nullptr;
116         std::unique_ptr<FrameAllocator> owned_video_frame_allocator;
117         std::unique_ptr<FrameAllocator> owned_audio_frame_allocator;
118         frame_callback_t frame_callback = nullptr;
119
120         std::string description;
121
122         std::atomic<bool> producer_thread_should_quit{false};
123         std::thread producer_thread;
124 };
125
126 }  // namespace bmusb
127
128 #endif  // !defined(_FAKE_CAPTURE_H)