- /* But never more than the limit, if there is one. */
- if (!devc->continuous)
- expected_samplenum = MIN(expected_samplenum, devc->limit_samples);
+ /* What time span should we send samples for? */
+ elapsed_us = g_get_monotonic_time() - devc->start_us;
+ limit_us = 1000 * devc->limit_msec;
+ if (limit_us > 0 && limit_us < elapsed_us)
+ todo_us = MAX(0, limit_us - devc->spent_us);
+ else
+ todo_us = MAX(0, elapsed_us - devc->spent_us);
+
+ /* How many samples are outstanding since the last round? */
+ samples_todo = (todo_us * devc->cur_samplerate + G_USEC_PER_SEC - 1)
+ / G_USEC_PER_SEC;
+ if (devc->limit_samples > 0) {
+ if (devc->limit_samples < devc->sent_samples)
+ samples_todo = 0;
+ else if (devc->limit_samples - devc->sent_samples < samples_todo)
+ samples_todo = devc->limit_samples - devc->sent_samples;
+ }
+ /* Calculate the actual time covered by this run back from the sample
+ * count, rounded towards zero. This avoids getting stuck on a too-low
+ * time delta with no samples being sent due to round-off.
+ */
+ todo_us = samples_todo * G_USEC_PER_SEC / devc->cur_samplerate;