+void Mixer::trim_queue(CaptureCard *card, unsigned card_index)
+{
+ // Count the number of frames in the queue, including any frames
+ // we dropped. It's hard to know exactly how we should deal with
+ // dropped (corrupted) input frames; they don't help our goal of
+ // avoiding starvation, but they still add to the problem of latency.
+ // Since dropped frames is going to mean a bump in the signal anyway,
+ // we err on the side of having more stable latency instead.
+ unsigned queue_length = 0;
+ for (const CaptureCard::NewFrame &frame : card->new_frames) {
+ queue_length += frame.dropped_frames + 1;
+ }
+ card->queue_length_policy.update_policy(queue_length);
+
+ // If needed, drop frames until the queue is below the safe limit.
+ // We prefer to drop from the head, because all else being equal,
+ // we'd like more recent frames (less latency).
+ unsigned dropped_frames = 0;
+ while (queue_length > card->queue_length_policy.get_safe_queue_length()) {
+ assert(!card->new_frames.empty());
+ assert(queue_length > card->new_frames.front().dropped_frames);
+ queue_length -= card->new_frames.front().dropped_frames;
+
+ if (queue_length <= card->queue_length_policy.get_safe_queue_length()) {
+ // No need to drop anything.
+ break;
+ }
+
+ card->new_frames.pop_front();
+ card->new_frames_changed.notify_all();
+ --queue_length;
+ ++dropped_frames;
+ }
+
+ if (dropped_frames > 0) {
+ fprintf(stderr, "Card %u dropped %u frame(s) to keep latency down.\n",
+ card_index, dropped_frames);
+ }
+}
+
+