- uint64_t nb_to_send = 0;
- int bytes_written;
- unsigned int msec_elapsed;
-
- while (thread_running) {
- if (limit_samples)
- nb_to_send = limit_samples - mydata->samples_counter;
- else
- nb_to_send = BUFSIZE; /* Continuous mode */
-
- if (limit_msec) {
- msec_elapsed = g_timer_elapsed(mydata->timer, NULL) * 1000;
- if (msec_elapsed > limit_msec)
- nb_to_send = 0;
- }
-
- if (nb_to_send == 0) {
- close(mydata->pipe_fds[1]);
- thread_running = 0;
- hw_stop_acquisition(mydata->device_index,
- mydata->session_device_id);
- } else if (nb_to_send > BUFSIZE) {
- nb_to_send = BUFSIZE;
- }
-
- samples_generator(buf, nb_to_send, data);
- mydata->samples_counter += nb_to_send;
-
- g_io_channel_write_chars(channels[1], (gchar *)&buf,
- nb_to_send, (gsize *)&bytes_written, NULL);
+ static uint64_t samples_to_send, expected_samplenum, sending_now;
+ int64_t time, elapsed;
+
+ (void)fd;
+ (void)revents;
+
+ /* How many "virtual" samples should we have collected by now? */
+ time = g_get_monotonic_time();
+ elapsed = time - devc->starttime;
+ expected_samplenum = elapsed * cur_samplerate / 1000000;
+ /* Of those, how many do we still have to send? */
+ samples_to_send = expected_samplenum - devc->samples_counter;
+
+ if (limit_samples) {
+ samples_to_send = MIN(samples_to_send,
+ limit_samples - devc->samples_counter);
+ }