- int latest_frame_id_to_reference = -1;
- if (acked_frame_buffers_[kAltRefBuffer]) {
- latest_frame_id_to_reference = used_buffers_frame_id_[kAltRefBuffer];
- }
- if (acked_frame_buffers_[kGoldenBuffer]) {
- if (latest_frame_id_to_reference == -1) {
- latest_frame_id_to_reference = used_buffers_frame_id_[kGoldenBuffer];
- } else {
- if (IsNewerFrameId(used_buffers_frame_id_[kGoldenBuffer],
- latest_frame_id_to_reference)) {
- latest_frame_id_to_reference = used_buffers_frame_id_[kGoldenBuffer];
- }
+ // The goal here is to make sure that we always keep one ACKed
+ // buffer while trying to get an ACK for a newer buffer as we go.
+ // Here are the rules for which buffer to select for update:
+ // 1. If there is a buffer in state kStartState, use it.
+ // 2. If there is a buffer other than the oldest buffer
+ // which is Acked, use the oldest buffer.
+ // 3. If there are Sent buffers which are older than
+ // latest_acked_frame_, use the oldest one.
+ // 4. If all else fails, just overwrite the newest buffer,
+ // but no more than 3 times in a row.
+ // TODO(hubbe): Figure out if 3 is optimal.
+ // Note, rule 1-3 describe cases where there is a "free" buffer
+ // that we can use. Rule 4 describes what happens when there is
+ // no free buffer available.
+
+ // Buffers, sorted from oldest frame to newest.
+ Vp8Encoder::Vp8Buffers buffers[kNumberOfVp8VideoBuffers];
+
+ for (int i = 0; i < kNumberOfVp8VideoBuffers; ++i) {
+ Vp8Encoder::Vp8Buffers buffer = static_cast<Vp8Encoder::Vp8Buffers>(i);
+
+ // Rule 1
+ if (buffer_state_[buffer].state == kBufferStartState) {
+ undroppable_frames_ = 0;
+ return buffer;