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.
4 #include "fake_capture.h"
14 #include <immintrin.h>
18 #include "bmusb/bmusb.h"
21 #define FRAME_SIZE (8 << 20) // 8 MB.
22 #define FAKE_FPS 25 // Must be an integer.
24 // Pure-color inputs: Red, green, blue, white.
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 };
34 void memset2(uint8_t *s, const uint8_t c[2], size_t n)
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]
42 __m128i cc = *(__m128i *)c_expanded;
43 __m128i *out = (__m128i *)s;
45 for ( ; i < (n & ~15); i += 16) {
46 _mm_storeu_si128(out++, cc);
47 _mm_storeu_si128(out++, cc);
58 void memset4(uint8_t *s, const uint8_t c[4], size_t n)
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]
66 __m128i cc = *(__m128i *)c_expanded;
67 __m128i *out = (__m128i *)s;
69 for ( ; i < (n & ~7); i += 8) {
70 _mm_storeu_si128(out++, cc);
71 _mm_storeu_si128(out++, cc);
86 FakeCapture::FakeCapture(int card_index)
89 snprintf(buf, sizeof(buf), "Fake card %d", card_index + 1);
92 y = ys[card_index % NUM_COLORS];
93 cb = cbs[card_index % NUM_COLORS];
94 cr = crs[card_index % NUM_COLORS];
97 FakeCapture::~FakeCapture()
99 if (has_dequeue_callbacks) {
100 dequeue_cleanup_callback();
104 void FakeCapture::configure_card()
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());
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());
116 void FakeCapture::start_bm_capture()
118 producer_thread_should_quit = false;
119 producer_thread = thread(&FakeCapture::producer_thread_func, this);
122 void FakeCapture::stop_dequeue_thread()
124 producer_thread_should_quit = true;
125 producer_thread.join();
128 std::map<uint32_t, VideoMode> FakeCapture::get_available_video_modes() const
133 snprintf(buf, sizeof(buf), "%dx%d", WIDTH, HEIGHT);
136 mode.autodetect = false;
138 mode.height = HEIGHT;
139 mode.frame_rate_num = FAKE_FPS;
140 mode.frame_rate_den = 1;
141 mode.interlaced = false;
143 return {{ 0, mode }};
146 std::map<uint32_t, std::string> FakeCapture::get_available_video_inputs() const
148 return {{ 0, "Fake video input (single color)" }};
151 std::map<uint32_t, std::string> FakeCapture::get_available_audio_inputs() const
153 return {{ 0, "Fake audio input (silence)" }};
156 void FakeCapture::set_video_mode(uint32_t video_mode_id)
158 assert(video_mode_id == 0);
161 void FakeCapture::set_video_input(uint32_t video_input_id)
163 assert(video_input_id == 0);
166 void FakeCapture::set_audio_input(uint32_t audio_input_id)
168 assert(audio_input_id == 0);
173 void add_time(double t, timespec *ts)
175 ts->tv_nsec += lrint(t * 1e9);
176 ts->tv_sec += ts->tv_nsec / 1000000000;
177 ts->tv_nsec %= 1000000000;
180 bool timespec_less_than(const timespec &a, const timespec &b)
182 return make_pair(a.tv_sec, a.tv_nsec) < make_pair(b.tv_sec, b.tv_nsec);
187 void FakeCapture::producer_thread_func()
189 uint16_t timecode = 0;
191 if (has_dequeue_callbacks) {
192 dequeue_init_callback();
196 clock_gettime(CLOCK_MONOTONIC, &next_frame);
197 add_time(1.0 / FAKE_FPS, &next_frame);
199 while (!producer_thread_should_quit) {
201 clock_gettime(CLOCK_MONOTONIC, &now);
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");
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;
216 if (!timespec_less_than(now, limit)) {
217 fprintf(stderr, "More than one second of missed fake frames; resetting clock.\n");
222 // Figure out when the next frame is to be, then compute the current one.
223 add_time(1.0 / FAKE_FPS, &next_frame);
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;
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);
241 uint8_t ycbcr[] = { y, cb, y, cr };
242 memset4(video_frame.data, ycbcr, WIDTH * HEIGHT / 2);
244 video_frame.len = WIDTH * HEIGHT * 2;
247 AudioFormat audio_format;
248 audio_format.bits_per_sample = 32;
249 audio_format.num_channels = 2;
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);
258 frame_callback(timecode++,
259 video_frame, 0, video_format,
260 audio_frame, 0, audio_format);
262 if (has_dequeue_callbacks) {
263 dequeue_cleanup_callback();