]> git.sesse.net Git - nageru/commitdiff
Make the resampling audio queue length adjustable with a flag.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 23 Dec 2016 22:03:11 +0000 (23:03 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 23 Dec 2016 22:03:18 +0000 (23:03 +0100)
audio_mixer.cpp
flags.cpp
flags.h
mixer.cpp
resampling_queue.cpp
resampling_queue.h

index 38d80c6bb4405e489e8e064ac49e749a19a9a0ba..01fed687b47a8d2ae77a752896f925d11c176c37 100644 (file)
@@ -227,7 +227,9 @@ void AudioMixer::reset_resampler_mutex_held(DeviceSpec device_spec)
        } else {
                // TODO: ResamplingQueue should probably take the full device spec.
                // (It's only used for console output, though.)
-               device->resampling_queue.reset(new ResamplingQueue(device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size()));
+               device->resampling_queue.reset(new ResamplingQueue(
+                       device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size(),
+                       global_flags.audio_queue_length_ms * 0.001));
        }
        device->next_local_pts = 0;
 }
index 239a688e428e4871296c8ed9644e8ddda817ffa7..1983c75e3d6f7b232ed95b3c9b937ca1841e61db 100644 (file)
--- a/flags.cpp
+++ b/flags.cpp
@@ -45,7 +45,8 @@ enum LongOption {
        OPTION_ENABLE_MAKEUP_GAIN_AUTO,
        OPTION_DISABLE_ALSA_OUTPUT,
        OPTION_NO_FLUSH_PBOS,
-       OPTION_PRINT_VIDEO_LATENCY
+       OPTION_PRINT_VIDEO_LATENCY,
+       OPTION_AUDIO_QUEUE_LENGTH_MS
 };
 
 void usage()
@@ -100,6 +101,7 @@ void usage()
        fprintf(stderr, "                                    (will give display corruption, but makes it\n");
        fprintf(stderr, "                                    possible to run with apitrace in real time)\n");
        fprintf(stderr, "      --print-video-latency       print out measurements of video latency on stdout\n");
+       fprintf(stderr, "      --audio-queue-length-ms     length of audio resampling queue (default 100.0)\n");
 }
 
 void parse_flags(int argc, char * const argv[])
@@ -146,6 +148,7 @@ void parse_flags(int argc, char * const argv[])
                { "disable-alsa-output", no_argument, 0, OPTION_DISABLE_ALSA_OUTPUT },
                { "no-flush-pbos", no_argument, 0, OPTION_NO_FLUSH_PBOS },
                { "print-video-latency", no_argument, 0, OPTION_PRINT_VIDEO_LATENCY },
+               { "audio-queue-length-ms", required_argument, 0, OPTION_AUDIO_QUEUE_LENGTH_MS },
                { 0, 0, 0, 0 }
        };
        vector<string> theme_dirs;
@@ -298,6 +301,9 @@ void parse_flags(int argc, char * const argv[])
                case OPTION_PRINT_VIDEO_LATENCY:
                        global_flags.print_video_latency = true;
                        break;
+               case OPTION_AUDIO_QUEUE_LENGTH_MS:
+                       global_flags.audio_queue_length_ms = atof(optarg);
+                       break;
                case OPTION_HELP:
                        usage();
                        exit(0);
diff --git a/flags.h b/flags.h
index 71495816d0d22734f8560c2d1e38aa79389e3a20..b3e676d09ade636e0cc98f8d0f1aec46c1b7357c 100644 (file)
--- a/flags.h
+++ b/flags.h
@@ -41,6 +41,7 @@ struct Flags {
        std::string input_mapping_filename;  // Empty for none.
        std::string midi_mapping_filename;  // Empty for none.
        bool print_video_latency = false;
+       double audio_queue_length_ms = 100.0;
 };
 extern Flags global_flags;
 
index a4ae1aab50c1a68baf623a03ce04b8990c158554..f7ef46e160d251e6ec89ee4dee611f86fac74262 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -870,7 +870,7 @@ void Mixer::render_one_frame(int64_t duration)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
-       const int64_t av_delay = TIMEBASE / 10;  // Corresponds to the fixed delay in resampling_queue.h. TODO: Make less hard-coded.
+       const int64_t av_delay = lrint(global_flags.audio_queue_length_ms * 0.001 * TIMEBASE);  // Corresponds to the delay in ResamplingQueue.
        RefCountedGLsync fence = video_encoder->end_frame(pts_int + av_delay, duration, theme_main_chain.input_frames);
 
        // The live frame just shows the RGBA texture we just rendered.
index 46fa91070e8d3e56354877ebf1786082f0c49ccd..025fa5041ab4950eaf854f66b0fca603161e5862 100644 (file)
@@ -29,9 +29,9 @@
 
 using namespace std;
 
-ResamplingQueue::ResamplingQueue(unsigned card_num, unsigned freq_in, unsigned freq_out, unsigned num_channels)
+ResamplingQueue::ResamplingQueue(unsigned card_num, unsigned freq_in, unsigned freq_out, unsigned num_channels, double expected_delay_seconds)
        : card_num(card_num), freq_in(freq_in), freq_out(freq_out), num_channels(num_channels),
-         ratio(double(freq_out) / double(freq_in))
+         ratio(double(freq_out) / double(freq_in)), expected_delay(expected_delay_seconds * OUTPUT_FREQUENCY)
 {
        vresampler.setup(ratio, num_channels, /*hlen=*/32);
 
index 8462152e0fe91bee15bc24474db7e18254b9f5f2..662a837262e8b84fcf3680ceae65bdf38f0e0871 100644 (file)
@@ -48,7 +48,7 @@
 class ResamplingQueue {
 public:
        // card_num is for debugging outputs only.
-       ResamplingQueue(unsigned card_num, unsigned freq_in, unsigned freq_out, unsigned num_channels = 2);
+       ResamplingQueue(unsigned card_num, unsigned freq_in, unsigned freq_out, unsigned num_channels, double expected_delay_seconds);
 
        // If policy is DO_NOT_ADJUST_RATE, the resampling rate will not be changed.
        // This is primarily useful if you have an extraordinary situation, such as
@@ -92,7 +92,7 @@ private:
        // How much delay we are expected to have, in input samples.
        // If actual delay drifts too much away from this, we will start
        // changing the resampling ratio to compensate.
-       double expected_delay = OUTPUT_FREQUENCY * 0.1;  // 100 ms.
+       double expected_delay;
 
        // Input samples not yet fed into the resampler.
        // TODO: Use a circular buffer instead, for efficiency.