+void Framer::RemoveOldFrames(uint32 frame_id) {
+ FrameList::iterator it = frames_.begin();
+
+ while (it != frames_.end()) {
+ if (IsNewerFrameId(it->first, frame_id)) {
+ ++it;
+ } else {
+ // Older or equal; erase.
+ frames_.erase(it++);
+ }
+ }
+ last_released_frame_ = frame_id;
+}
+
+uint32 Framer::NewestFrameId() const { return newest_frame_id_; }
+
+bool Framer::NextContinuousFrame(uint32* frame_id) const {
+ FrameList::const_iterator it;
+
+ for (it = frames_.begin(); it != frames_.end(); ++it) {
+ if (it->second->Complete() && ContinuousFrame(it->second.get())) {
+ *frame_id = it->first;
+ return true;
+ }
+ }
+ return false;
+}
+
+bool Framer::HaveMultipleDecodableFrames() const {
+ // Find the oldest decodable frame.
+ FrameList::const_iterator it;
+ bool found_one = false;
+ for (it = frames_.begin(); it != frames_.end(); ++it) {
+ if (it->second->Complete() && DecodableFrame(it->second.get())) {
+ if (found_one) {
+ return true;
+ } else {
+ found_one = true;
+ }
+ }
+ }
+ return false;
+}
+
+uint32 Framer::LastContinuousFrame() const {
+ uint32 last_continuous_frame_id = last_released_frame_;
+ uint32 next_expected_frame = last_released_frame_;
+
+ FrameList::const_iterator it;
+
+ do {
+ next_expected_frame++;
+ it = frames_.find(next_expected_frame);
+ if (it == frames_.end())
+ break;
+ if (!it->second->Complete())
+ break;
+
+ // We found the next continuous frame.
+ last_continuous_frame_id = it->first;
+ } while (next_expected_frame != newest_frame_id_);
+ return last_continuous_frame_id;
+}
+
+bool Framer::NextFrameAllowingSkippingFrames(uint32* frame_id) const {
+ // Find the oldest decodable frame.
+ FrameList::const_iterator it_best_match = frames_.end();
+ FrameList::const_iterator it;
+ for (it = frames_.begin(); it != frames_.end(); ++it) {
+ if (it->second->Complete() && DecodableFrame(it->second.get())) {
+ if (it_best_match == frames_.end() ||
+ IsOlderFrameId(it->first, it_best_match->first)) {
+ it_best_match = it;
+ }
+ }
+ }
+ if (it_best_match == frames_.end())
+ return false;
+
+ *frame_id = it_best_match->first;
+ return true;
+}
+
+bool Framer::Empty() const { return frames_.empty(); }
+
+int Framer::NumberOfCompleteFrames() const {
+ int count = 0;
+ FrameList::const_iterator it;
+ for (it = frames_.begin(); it != frames_.end(); ++it) {
+ if (it->second->Complete()) {
+ ++count;
+ }
+ }
+ return count;
+}
+
+bool Framer::FrameExists(uint32 frame_id) const {
+ return frames_.end() != frames_.find(frame_id);
+}
+
+void Framer::GetMissingPackets(uint32 frame_id,
+ bool last_frame,
+ PacketIdSet* missing_packets) const {
+ FrameList::const_iterator it = frames_.find(frame_id);
+ if (it == frames_.end())
+ return;
+
+ it->second->GetMissingPackets(last_frame, missing_packets);
+}
+
+bool Framer::ContinuousFrame(FrameBuffer* frame) const {
+ DCHECK(frame);
+ if (waiting_for_key_ && !frame->is_key_frame())
+ return false;
+ return static_cast<uint32>(last_released_frame_ + 1) == frame->frame_id();
+}
+
+bool Framer::DecodableFrame(FrameBuffer* frame) const {
+ if (frame->is_key_frame())
+ return true;
+ if (waiting_for_key_ && !frame->is_key_frame())
+ return false;
+ // Self-reference?
+ if (frame->last_referenced_frame_id() == frame->frame_id())
+ return true;
+
+ // Current frame is not necessarily referencing the last frame.
+ // Do we have the reference frame?
+ if (IsOlderFrameId(frame->last_referenced_frame_id(), last_released_frame_)) {
+ return true;
+ }
+ return frame->last_referenced_frame_id() == last_released_frame_;
+}
+
+