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 };
35 void memset2(uint8_t *s, const uint8_t c[2], size_t n)
39 const uint8_t c_expanded[16] = {
40 c[0], c[1], c[0], c[1], c[0], c[1], c[0], c[1],
41 c[0], c[1], c[0], c[1], c[0], c[1], c[0], c[1]
43 __m128i cc = *(__m128i *)c_expanded;
44 __m128i *out = (__m128i *)s;
46 for ( ; i < (n & ~15); i += 16) {
47 _mm_storeu_si128(out++, cc);
48 _mm_storeu_si128(out++, cc);
59 void memset4(uint8_t *s, const uint8_t c[4], size_t n)
63 const uint8_t c_expanded[16] = {
64 c[0], c[1], c[2], c[3], c[0], c[1], c[2], c[3],
65 c[0], c[1], c[2], c[3], c[0], c[1], c[2], c[3]
67 __m128i cc = *(__m128i *)c_expanded;
68 __m128i *out = (__m128i *)s;
70 for ( ; i < (n & ~7); i += 8) {
71 _mm_storeu_si128(out++, cc);
72 _mm_storeu_si128(out++, cc);
87 FakeCapture::FakeCapture(int card_index)
90 snprintf(buf, sizeof(buf), "Fake card %d", card_index + 1);
93 y = ys[card_index % NUM_COLORS];
94 cb = cbs[card_index % NUM_COLORS];
95 cr = crs[card_index % NUM_COLORS];
98 FakeCapture::~FakeCapture()
100 if (has_dequeue_callbacks) {
101 dequeue_cleanup_callback();
105 void FakeCapture::configure_card()
107 if (video_frame_allocator == nullptr) {
108 owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
109 set_video_frame_allocator(owned_video_frame_allocator.get());
111 if (audio_frame_allocator == nullptr) {
112 owned_audio_frame_allocator.reset(new MallocFrameAllocator(65536, NUM_QUEUED_AUDIO_FRAMES));
113 set_audio_frame_allocator(owned_audio_frame_allocator.get());
117 void FakeCapture::start_bm_capture()
119 producer_thread_should_quit = false;
120 producer_thread = thread(&FakeCapture::producer_thread_func, this);
123 void FakeCapture::stop_dequeue_thread()
125 producer_thread_should_quit = true;
126 producer_thread.join();
129 std::map<uint32_t, VideoMode> FakeCapture::get_available_video_modes() const
134 snprintf(buf, sizeof(buf), "%dx%d", WIDTH, HEIGHT);
137 mode.autodetect = false;
139 mode.height = HEIGHT;
140 mode.frame_rate_num = FAKE_FPS;
141 mode.frame_rate_den = 1;
142 mode.interlaced = false;
144 return {{ 0, mode }};
147 std::map<uint32_t, std::string> FakeCapture::get_available_video_inputs() const
149 return {{ 0, "Fake video input (single color)" }};
152 std::map<uint32_t, std::string> FakeCapture::get_available_audio_inputs() const
154 return {{ 0, "Fake audio input (silence)" }};
157 void FakeCapture::set_video_mode(uint32_t video_mode_id)
159 assert(video_mode_id == 0);
162 void FakeCapture::set_video_input(uint32_t video_input_id)
164 assert(video_input_id == 0);
167 void FakeCapture::set_audio_input(uint32_t audio_input_id)
169 assert(audio_input_id == 0);
174 void add_time(double t, timespec *ts)
176 ts->tv_nsec += lrint(t * 1e9);
177 ts->tv_sec += ts->tv_nsec / 1000000000;
178 ts->tv_nsec %= 1000000000;
181 bool timespec_less_than(const timespec &a, const timespec &b)
183 return make_pair(a.tv_sec, a.tv_nsec) < make_pair(b.tv_sec, b.tv_nsec);
188 void FakeCapture::producer_thread_func()
190 uint16_t timecode = 0;
192 if (has_dequeue_callbacks) {
193 dequeue_init_callback();
197 clock_gettime(CLOCK_MONOTONIC, &next_frame);
198 add_time(1.0 / FAKE_FPS, &next_frame);
200 while (!producer_thread_should_quit) {
202 clock_gettime(CLOCK_MONOTONIC, &now);
204 if (timespec_less_than(now, next_frame)) {
205 // Wait until the next frame.
206 if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
207 &next_frame, nullptr) == -1) {
208 if (errno == EINTR) continue; // Re-check the flag and then sleep again.
209 perror("clock_nanosleep");
213 // We've seemingly missed a frame. If we're more than one second behind,
214 // reset the timer; otherwise, just keep going.
215 timespec limit = next_frame;
217 if (!timespec_less_than(now, limit)) {
218 fprintf(stderr, "More than one second of missed fake frames; resetting clock.\n");
223 // Figure out when the next frame is to be, then compute the current one.
224 add_time(1.0 / FAKE_FPS, &next_frame);
226 VideoFormat video_format;
227 video_format.width = WIDTH;
228 video_format.height = HEIGHT;
229 video_format.frame_rate_nom = FAKE_FPS;
230 video_format.frame_rate_den = 1;
231 video_format.has_signal = true;
232 video_format.is_connected = false;
234 FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
235 if (video_frame.data != nullptr) {
236 assert(video_frame.size >= WIDTH * HEIGHT * 2);
237 if (video_frame.interleaved) {
238 uint8_t cbcr[] = { cb, cr };
239 memset2(video_frame.data, cbcr, WIDTH * HEIGHT / 2);
240 memset(video_frame.data2, y, WIDTH * HEIGHT);
242 uint8_t ycbcr[] = { y, cb, y, cr };
243 memset4(video_frame.data, ycbcr, WIDTH * HEIGHT / 2);
245 video_frame.len = WIDTH * HEIGHT * 2;
248 AudioFormat audio_format;
249 audio_format.bits_per_sample = 32;
250 audio_format.num_channels = 2;
252 FrameAllocator::Frame audio_frame = audio_frame_allocator->alloc_frame();
253 if (audio_frame.data != nullptr) {
254 assert(audio_frame.size >= 2 * sizeof(uint32_t) * OUTPUT_FREQUENCY / FAKE_FPS);
255 audio_frame.len = 2 * sizeof(uint32_t) * OUTPUT_FREQUENCY / FAKE_FPS;
256 memset(audio_frame.data, 0, audio_frame.len);
259 frame_callback(timecode++,
260 video_frame, 0, video_format,
261 audio_frame, 0, audio_format);
263 if (has_dequeue_callbacks) {
264 dequeue_cleanup_callback();