- double rcorr = -1.0;
- if (rate_adjustment_policy == ADJUST_RATE) {
- double last_output_len;
- if (first_output) {
- // Synthesize a fake length.
- last_output_len = double(num_samples) / freq_out;
- } else {
- last_output_len = pts - last_output_pts;
- }
- last_output_pts = pts;
-
- // Using the time point since just before the last call to add_input_samples() as a base,
- // estimate actual delay based on activity since then, measured in number of input samples:
- double actual_delay = 0.0;
- assert(last_input_len != 0);
- actual_delay += (k_a1 - k_a0) * last_output_len / last_input_len; // Inserted samples since k_a0, rescaled for the different time periods.
- actual_delay += k_a0 - total_consumed_samples; // Samples inserted before k_a0 but not consumed yet.
- actual_delay += vresampler.inpdist(); // Delay in the resampler itself.
+ // This can happen when we get dropped frames on the master card.
+ if (duration<double>(ts.time_since_epoch()).count() <= 0.0) {
+ rate_adjustment_policy = DO_NOT_ADJUST_RATE;
+ }
+
+ if (rate_adjustment_policy == ADJUST_RATE && (a0.good_sample || a1.good_sample)) {
+ // Estimate the current number of input samples produced at
+ // this instant in time, by extrapolating from the last known
+ // good point. Note that we could be extrapolating backward or
+ // forward, depending on the timing of the calls.
+ const InputPoint &base_point = a1.good_sample ? a1 : a0;
+ assert(duration<double>(base_point.ts.time_since_epoch()).count() >= 0.0);
+
+ // NOTE: Due to extrapolation, input_samples_received can
+ // actually go negative here the few first calls (ie., we asked
+ // about a timestamp where we hadn't actually started producing
+ // samples yet), but that is harmless.
+ const double input_samples_received = base_point.input_samples_received +
+ current_estimated_freq_in * duration<double>(ts - base_point.ts).count();
+
+ // Estimate the number of input samples _consumed_ after we've run the resampler.
+ const double input_samples_consumed = total_consumed_samples +
+ num_samples / (ratio * rcorr);
+
+ double actual_delay = input_samples_received - input_samples_consumed;
+ actual_delay += vresampler.inpdist(); // Delay in the resampler itself.