]> git.sesse.net Git - nageru/blob - fake_capture.cpp
b767d8fd8fdf2770642487e82d9edea1af000778
[nageru] / fake_capture.cpp
1 // A fake capture device that sends single-color frames at a given rate.
2 // Mostly useful for testing themes without actually hooking up capture devices.
3
4 #include "fake_capture.h"
5
6 #include <assert.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <math.h>
12 #include <unistd.h>
13 #if __SSE2__
14 #include <immintrin.h>
15 #endif
16 #include <cstddef>
17
18 #include "bmusb/bmusb.h"
19 #include "defs.h"
20
21 #define FRAME_SIZE (8 << 20)  // 8 MB.
22 #define FAKE_FPS 25  // Must be an integer.
23
24 // Pure-color inputs: Red, green, blue, white.
25 #define NUM_COLORS 4
26 constexpr uint8_t ys[NUM_COLORS] = { 81, 145, 41, 235 };
27 constexpr uint8_t cbs[NUM_COLORS] = { 90, 54, 240, 128 };
28 constexpr uint8_t crs[NUM_COLORS] = { 240, 34, 110, 128 };
29
30 using namespace std;
31
32 namespace {
33
34 void memset2(uint8_t *s, const uint8_t c[2], size_t n)
35 {
36         size_t i = 0;
37 #if __SSE2__
38         const uint8_t c_expanded[16] = {
39                 c[0], c[1], c[0], c[1], c[0], c[1], c[0], c[1],
40                 c[0], c[1], c[0], c[1], c[0], c[1], c[0], c[1]
41         };
42         __m128i cc = *(__m128i *)c_expanded;
43         __m128i *out = (__m128i *)s;
44
45         for ( ; i < (n & ~15); i += 16) {
46                 _mm_storeu_si128(out++, cc);
47                 _mm_storeu_si128(out++, cc);
48         }
49
50         s = (uint8_t *)out;
51 #endif
52         for ( ; i < n; ++i) {
53                 *s++ = c[0];
54                 *s++ = c[1];
55         }
56 }
57
58 void memset4(uint8_t *s, const uint8_t c[4], size_t n)
59 {
60         size_t i = 0;
61 #if __SSE2__
62         const uint8_t c_expanded[16] = {
63                 c[0], c[1], c[2], c[3], c[0], c[1], c[2], c[3],
64                 c[0], c[1], c[2], c[3], c[0], c[1], c[2], c[3]
65         };
66         __m128i cc = *(__m128i *)c_expanded;
67         __m128i *out = (__m128i *)s;
68
69         for ( ; i < (n & ~7); i += 8) {
70                 _mm_storeu_si128(out++, cc);
71                 _mm_storeu_si128(out++, cc);
72         }
73
74         s = (uint8_t *)out;
75 #endif
76         for ( ; i < n; ++i) {
77                 *s++ = c[0];
78                 *s++ = c[1];
79                 *s++ = c[2];
80                 *s++ = c[3];
81         }
82 }
83
84 }  // namespace
85
86 FakeCapture::FakeCapture(int card_index)
87 {
88         char buf[256];
89         snprintf(buf, sizeof(buf), "Fake card %d", card_index + 1);
90         description = buf;
91
92         y = ys[card_index % NUM_COLORS];
93         cb = cbs[card_index % NUM_COLORS];
94         cr = crs[card_index % NUM_COLORS];
95 }
96
97 FakeCapture::~FakeCapture()
98 {
99         if (has_dequeue_callbacks) {
100                 dequeue_cleanup_callback();
101         }
102 }
103
104 void FakeCapture::configure_card()
105 {
106         if (video_frame_allocator == nullptr) {
107                 owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
108                 set_video_frame_allocator(owned_video_frame_allocator.get());
109         }
110         if (audio_frame_allocator == nullptr) {
111                 owned_audio_frame_allocator.reset(new MallocFrameAllocator(65536, NUM_QUEUED_AUDIO_FRAMES));
112                 set_audio_frame_allocator(owned_audio_frame_allocator.get());
113         }
114 }
115
116 void FakeCapture::start_bm_capture()
117 {
118         producer_thread_should_quit = false;
119         producer_thread = thread(&FakeCapture::producer_thread_func, this);
120 }
121
122 void FakeCapture::stop_dequeue_thread()
123 {
124         producer_thread_should_quit = true;
125         producer_thread.join();
126 }
127         
128 std::map<uint32_t, VideoMode> FakeCapture::get_available_video_modes() const
129 {
130         VideoMode mode;
131
132         char buf[256];
133         snprintf(buf, sizeof(buf), "%dx%d", WIDTH, HEIGHT);
134         mode.name = buf;
135         
136         mode.autodetect = false;
137         mode.width = WIDTH;
138         mode.height = HEIGHT;
139         mode.frame_rate_num = FAKE_FPS;
140         mode.frame_rate_den = 1;
141         mode.interlaced = false;
142
143         return {{ 0, mode }};
144 }
145
146 std::map<uint32_t, std::string> FakeCapture::get_available_video_inputs() const
147 {
148         return {{ 0, "Fake video input (single color)" }};
149 }
150
151 std::map<uint32_t, std::string> FakeCapture::get_available_audio_inputs() const
152 {
153         return {{ 0, "Fake audio input (silence)" }};
154 }
155
156 void FakeCapture::set_video_mode(uint32_t video_mode_id)
157 {
158         assert(video_mode_id == 0);
159 }
160
161 void FakeCapture::set_video_input(uint32_t video_input_id)
162 {
163         assert(video_input_id == 0);
164 }
165
166 void FakeCapture::set_audio_input(uint32_t audio_input_id)
167 {
168         assert(audio_input_id == 0);
169 }
170
171 namespace {
172
173 void add_time(double t, timespec *ts)
174 {
175         ts->tv_nsec += lrint(t * 1e9);
176         ts->tv_sec += ts->tv_nsec / 1000000000;
177         ts->tv_nsec %= 1000000000;
178 }
179
180 bool timespec_less_than(const timespec &a, const timespec &b)
181 {
182         return make_pair(a.tv_sec, a.tv_nsec) < make_pair(b.tv_sec, b.tv_nsec);
183 }
184
185 }  // namespace
186
187 void FakeCapture::producer_thread_func()
188 {
189         uint16_t timecode = 0;
190
191         if (has_dequeue_callbacks) {
192                 dequeue_init_callback();
193         }
194
195         timespec next_frame;
196         clock_gettime(CLOCK_MONOTONIC, &next_frame);
197         add_time(1.0 / FAKE_FPS, &next_frame);
198
199         while (!producer_thread_should_quit) {
200                 timespec now;
201                 clock_gettime(CLOCK_MONOTONIC, &now);
202
203                 if (timespec_less_than(now, next_frame)) {
204                         // Wait until the next frame.
205                         if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
206                                             &next_frame, nullptr) == -1) {
207                                 if (errno == EINTR) continue;  // Re-check the flag and then sleep again.
208                                 perror("clock_nanosleep");
209                                 exit(1);
210                         }
211                 } else {
212                         // We've seemingly missed a frame. If we're more than one second behind,
213                         // reset the timer; otherwise, just keep going.
214                         timespec limit = next_frame;
215                         ++limit.tv_sec;
216                         if (!timespec_less_than(now, limit)) {
217                                 fprintf(stderr, "More than one second of missed fake frames; resetting clock.\n");
218                                 next_frame = now;
219                         }
220                 }
221
222                 // Figure out when the next frame is to be, then compute the current one.
223                 add_time(1.0 / FAKE_FPS, &next_frame);
224
225                 VideoFormat video_format;
226                 video_format.width = WIDTH;
227                 video_format.height = HEIGHT;
228                 video_format.frame_rate_nom = FAKE_FPS;
229                 video_format.frame_rate_den = 1;
230                 video_format.has_signal = true;
231                 video_format.is_connected = false;
232
233                 FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
234                 if (video_frame.data != nullptr) {
235                         assert(video_frame.size >= WIDTH * HEIGHT * 2);
236                         if (video_frame.interleaved) {
237                                 uint8_t cbcr[] = { cb, cr };
238                                 memset2(video_frame.data, cbcr, WIDTH * HEIGHT / 2);
239                                 memset(video_frame.data2, y, WIDTH * HEIGHT);
240                         } else {
241                                 uint8_t ycbcr[] = { y, cb, y, cr };
242                                 memset4(video_frame.data, ycbcr, WIDTH * HEIGHT / 2);
243                         }
244                         video_frame.len = WIDTH * HEIGHT * 2;
245                 }
246
247                 AudioFormat audio_format;
248                 audio_format.bits_per_sample = 32;
249                 audio_format.num_channels = 2;
250
251                 FrameAllocator::Frame audio_frame = audio_frame_allocator->alloc_frame();
252                 if (audio_frame.data != nullptr) {
253                         assert(audio_frame.size >= 2 * sizeof(uint32_t) * OUTPUT_FREQUENCY / FAKE_FPS);
254                         audio_frame.len = 2 * sizeof(uint32_t) * OUTPUT_FREQUENCY / FAKE_FPS;
255                         memset(audio_frame.data, 0, audio_frame.len);
256                 }
257
258                 frame_callback(timecode++,
259                                video_frame, 0, video_format,
260                                audio_frame, 0, audio_format);
261         }
262         if (has_dequeue_callbacks) {
263                 dequeue_cleanup_callback();
264         }
265 }