]> git.sesse.net Git - bmusb/blobdiff - fake_capture.cpp
Give all of our threads meaningful names, to aid with debugging.
[bmusb] / fake_capture.cpp
index 961700eb54158a2b9245059d89f384620dac1f32..29b06b100d016d49543114c0056a6c31e4bf05dd 100644 (file)
@@ -4,6 +4,7 @@
 #include "bmusb/fake_capture.h"
 
 #include <assert.h>
+#include <pthread.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -13,6 +14,7 @@
 #if __SSE2__
 #include <immintrin.h>
 #endif
+#include <chrono>
 #include <cstddef>
 
 #include "bmusb/bmusb.h"
@@ -26,6 +28,7 @@ constexpr uint8_t cbs[NUM_COLORS] = { 90, 54, 240, 128 };
 constexpr uint8_t crs[NUM_COLORS] = { 240, 34, 110, 128 };
 
 using namespace std;
+using namespace std::chrono;
 
 namespace bmusb {
 namespace {
@@ -87,7 +90,7 @@ void memset4(uint8_t *s, const uint8_t c[4], size_t n)
 }  // namespace
 
 FakeCapture::FakeCapture(unsigned width, unsigned height, unsigned fps, unsigned audio_sample_frequency, int card_index, bool has_audio)
-       : width(width), height(height), fps(fps), audio_sample_frequency(audio_sample_frequency)
+       : width(width), height(height), fps(fps), audio_sample_frequency(audio_sample_frequency), card_index(card_index)
 {
        char buf[256];
        snprintf(buf, sizeof(buf), "Fake card %d", card_index + 1);
@@ -199,6 +202,10 @@ bool timespec_less_than(const timespec &a, const timespec &b)
 
 void FakeCapture::producer_thread_func()
 {
+       char thread_name[16];
+       snprintf(thread_name, sizeof(thread_name), "FakeCapture_%d", card_index);
+       pthread_setname_np(pthread_self(), thread_name);
+
        uint16_t timecode = 0;
 
        if (has_dequeue_callbacks) {
@@ -255,23 +262,25 @@ void FakeCapture::producer_thread_func()
                                memset4(video_frame.data, ycbcr, width * height / 2);
                        }
                        video_frame.len = width * height * 2;
+                       video_frame.received_timestamp = steady_clock::now();
                }
 
                AudioFormat audio_format;
                audio_format.bits_per_sample = 32;
-               audio_format.num_channels = 2;
+               audio_format.num_channels = 8;
 
                FrameAllocator::Frame audio_frame = audio_frame_allocator->alloc_frame();
                if (audio_frame.data != nullptr) {
                        const unsigned num_stereo_samples = audio_sample_frequency / fps;
-                       assert(audio_frame.size >= 2 * sizeof(int32_t) * num_stereo_samples);
-                       audio_frame.len = 2 * sizeof(int32_t) * num_stereo_samples;
+                       assert(audio_frame.size >= audio_format.num_channels * sizeof(int32_t) * num_stereo_samples);
+                       audio_frame.len = audio_format.num_channels * sizeof(int32_t) * num_stereo_samples;
+                       audio_frame.received_timestamp = steady_clock::now();
 
                        if (audio_sin == 0.0f) {
                                // Silence.
                                memset(audio_frame.data, 0, audio_frame.len);
                        } else {
-                               make_tone((int32_t *)audio_frame.data, num_stereo_samples);
+                               make_tone((int32_t *)audio_frame.data, num_stereo_samples, audio_format.num_channels);
                        }
                }
 
@@ -284,14 +293,15 @@ void FakeCapture::producer_thread_func()
        }
 }
 
-void FakeCapture::make_tone(int32_t *out, unsigned num_stereo_samples)
+void FakeCapture::make_tone(int32_t *out, unsigned num_stereo_samples, unsigned num_channels)
 {
        int32_t *ptr = out;
        float r = audio_real, i = audio_imag;
        for (unsigned sample_num = 0; sample_num < num_stereo_samples; ++sample_num) {
                int32_t s = lrintf(r);
-               *ptr++ = s;
-               *ptr++ = s;
+               for (unsigned i = 0; i < num_channels; ++i) {
+                       *ptr++ = s;
+               }
 
                // Rotate the phaser by one sample.
                float new_r = r * audio_cos - i * audio_sin;