Refactor FrameBuffer to store decoded frames history separately

This will allow to increase the stored decoded frames history size and
optimize it to reduce memory consumption.

Bug: webrtc:9710
Change-Id: I82be0eb376c5d0b61ad5d754e6a37d606b4df29d
Reviewed-on: https://webrtc-review.googlesource.com/c/116686
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26200}
diff --git a/modules/video_coding/frame_buffer2.h b/modules/video_coding/frame_buffer2.h
index 8d5416d..d132bec 100644
--- a/modules/video_coding/frame_buffer2.h
+++ b/modules/video_coding/frame_buffer2.h
@@ -14,6 +14,7 @@
 #include <array>
 #include <map>
 #include <memory>
+#include <set>
 #include <utility>
 #include <vector>
 
@@ -131,8 +132,7 @@
   void PropagateDecodability(const FrameInfo& info)
       RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
 
-  // Advances |last_decoded_frame_it_| to |decoded| and removes old
-  // frame info.
+  // Removes undecodable frames and moves decoded frame to the history.
   void AdvanceLastDecodedFrame(FrameMap::iterator decoded)
       RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
 
@@ -159,7 +159,9 @@
   EncodedFrame* CombineAndDeleteFrames(
       const std::vector<EncodedFrame*>& frames) const;
 
+  // Stores only undecoded frames.
   FrameMap frames_ RTC_GUARDED_BY(crit_);
+  std::set<VideoLayerFrameId> decoded_frames_history_ RTC_GUARDED_BY(crit_);
 
   rtc::CriticalSection crit_;
   Clock* const clock_;
@@ -168,11 +170,10 @@
   VCMTiming* const timing_ RTC_GUARDED_BY(crit_);
   VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(crit_);
   absl::optional<uint32_t> last_decoded_frame_timestamp_ RTC_GUARDED_BY(crit_);
-  FrameMap::iterator last_decoded_frame_it_ RTC_GUARDED_BY(crit_);
-  FrameMap::iterator last_continuous_frame_it_ RTC_GUARDED_BY(crit_);
+  absl::optional<VideoLayerFrameId> last_decoded_frame_ RTC_GUARDED_BY(crit_);
+  absl::optional<VideoLayerFrameId> last_continuous_frame_
+      RTC_GUARDED_BY(crit_);
   std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(crit_);
-  int num_frames_history_ RTC_GUARDED_BY(crit_);
-  int num_frames_buffered_ RTC_GUARDED_BY(crit_);
   bool stopped_ RTC_GUARDED_BY(crit_);
   VCMVideoProtection protection_mode_ RTC_GUARDED_BY(crit_);
   VCMReceiveStatisticsCallback* const stats_callback_;