1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/cast/framer/frame_id_map.h"
7 #include "base/logging.h"
8 #include "media/cast/rtp_common/rtp_defines.h"
13 FrameInfo::FrameInfo(uint8 frame_id,
14 uint8 referenced_frame_id,
17 : is_key_frame_(key_frame),
19 referenced_frame_id_(referenced_frame_id),
20 max_received_packet_id_(0) {
21 if (max_packet_id > 0) {
22 // Create the set with all packets missing.
23 for (uint16 i = 0; i <= max_packet_id; i++) {
24 missing_packets_.insert(i);
29 FrameInfo::~FrameInfo() {}
31 bool FrameInfo::InsertPacket(uint16 packet_id) {
32 // Update the last received packet id.
33 if (IsNewerPacketId(packet_id, max_received_packet_id_)) {
34 max_received_packet_id_ = packet_id;
36 missing_packets_.erase(packet_id);
37 return missing_packets_.empty();
40 bool FrameInfo::Complete() const {
41 return missing_packets_.empty();
44 void FrameInfo::GetMissingPackets(bool newest_frame,
45 PacketIdSet* missing_packets) const {
47 // Missing packets capped by max_received_packet_id_.
48 PacketIdSet::const_iterator it_after_last_received =
49 missing_packets_.lower_bound(max_received_packet_id_);
50 missing_packets->insert(missing_packets_.begin(), it_after_last_received);
52 missing_packets->insert(missing_packets_.begin(), missing_packets_.end());
57 FrameIdMap::FrameIdMap()
58 : waiting_for_key_(true),
59 last_released_frame_(kStartFrameId),
60 newest_frame_id_(kStartFrameId) {
63 FrameIdMap::~FrameIdMap() {}
65 bool FrameIdMap::InsertPacket(const RtpCastHeader& rtp_header, bool* complete) {
66 uint8 frame_id = rtp_header.frame_id;
67 uint8 reference_frame_id;
68 if (rtp_header.is_reference) {
69 reference_frame_id = rtp_header.reference_frame_id;
71 reference_frame_id = static_cast<uint8>(frame_id - 1);
74 if (rtp_header.is_key_frame && waiting_for_key_) {
75 last_released_frame_ = static_cast<uint8>(frame_id - 1);
76 waiting_for_key_ = false;
79 VLOG(1) << "InsertPacket frame:" << static_cast<int>(frame_id)
80 << " packet:" << static_cast<int>(rtp_header.packet_id)
81 << " max packet:" << static_cast<int>(rtp_header.max_packet_id);
83 if (IsOlderFrameId(frame_id, last_released_frame_) && !waiting_for_key_) {
87 // Update the last received frame id.
88 if (IsNewerFrameId(frame_id, newest_frame_id_)) {
89 newest_frame_id_ = frame_id;
92 // Does this packet belong to a new frame?
93 FrameMap::iterator it = frame_map_.find(frame_id);
94 if (it == frame_map_.end()) {
96 linked_ptr<FrameInfo> frame_info(new FrameInfo(frame_id,
98 rtp_header.max_packet_id,
99 rtp_header.is_key_frame));
100 std::pair<FrameMap::iterator, bool> retval =
101 frame_map_.insert(std::make_pair(frame_id, frame_info));
103 *complete = retval.first->second->InsertPacket(rtp_header.packet_id);
105 // Insert packet to existing frame.
106 *complete = it->second->InsertPacket(rtp_header.packet_id);
111 void FrameIdMap::RemoveOldFrames(uint8 frame_id) {
112 FrameMap::iterator it = frame_map_.begin();
114 while (it != frame_map_.end()) {
115 if (IsNewerFrameId(it->first, frame_id)) {
118 // Older or equal; erase.
119 frame_map_.erase(it++);
122 last_released_frame_ = frame_id;
125 void FrameIdMap::Clear() {
127 waiting_for_key_ = true;
128 last_released_frame_ = kStartFrameId;
129 newest_frame_id_ = kStartFrameId;
132 uint8 FrameIdMap::NewestFrameId() const {
133 return newest_frame_id_;
136 bool FrameIdMap::NextContinuousFrame(uint8* frame_id) const {
137 FrameMap::const_iterator it;
139 for (it = frame_map_.begin(); it != frame_map_.end(); ++it) {
140 if (it->second->Complete() && ContinuousFrame(it->second.get())) {
141 *frame_id = it->first;
148 uint8 FrameIdMap::LastContinuousFrame() const {
149 uint8 last_continuous_frame_id = last_released_frame_;
150 uint8 next_expected_frame = last_released_frame_;
152 FrameMap::const_iterator it;
155 next_expected_frame++;
156 it = frame_map_.find(next_expected_frame);
157 if (it == frame_map_.end()) break;
158 if (!it->second->Complete()) break;
160 // We found the next continuous frame.
161 last_continuous_frame_id = it->first;
162 } while (next_expected_frame != newest_frame_id_);
163 return last_continuous_frame_id;
166 bool FrameIdMap::NextAudioFrameAllowingMissingFrames(uint8* frame_id) const {
167 // First check if we have continuous frames.
168 if (NextContinuousFrame(frame_id)) return true;
170 // Find the oldest frame.
171 FrameMap::const_iterator it_best_match = frame_map_.end();
172 FrameMap::const_iterator it;
174 // Find first complete frame.
175 for (it = frame_map_.begin(); it != frame_map_.end(); ++it) {
176 if (it->second->Complete()) {
181 if (it_best_match == frame_map_.end()) return false; // No complete frame.
184 for (; it != frame_map_.end(); ++it) {
185 if (it->second->Complete() &&
186 IsOlderFrameId(it->first, it_best_match->first)) {
190 *frame_id = it_best_match->first;
194 bool FrameIdMap::NextVideoFrameAllowingSkippingFrames(uint8* frame_id) const {
195 // Find the oldest decodable frame.
196 FrameMap::const_iterator it_best_match = frame_map_.end();
197 FrameMap::const_iterator it;
198 for (it = frame_map_.begin(); it != frame_map_.end(); ++it) {
199 if (it->second->Complete() && DecodableVideoFrame(it->second.get())) {
203 if (it_best_match == frame_map_.end()) return false;
205 *frame_id = it_best_match->first;
209 bool FrameIdMap::Empty() const {
210 return frame_map_.empty();
213 int FrameIdMap::NumberOfCompleteFrames() const {
215 FrameMap::const_iterator it;
216 for (it = frame_map_.begin(); it != frame_map_.end(); ++it) {
217 if (it->second->Complete()) {
224 bool FrameIdMap::FrameExists(uint8 frame_id) const {
225 return frame_map_.end() != frame_map_.find(frame_id);
228 void FrameIdMap::GetMissingPackets(uint8 frame_id,
230 PacketIdSet* missing_packets) const {
231 FrameMap::const_iterator it = frame_map_.find(frame_id);
232 if (it == frame_map_.end()) return;
234 it->second->GetMissingPackets(last_frame, missing_packets);
237 bool FrameIdMap::ContinuousFrame(FrameInfo* frame) const {
239 if (waiting_for_key_ && !frame->is_key_frame()) return false;
240 return static_cast<uint8>(last_released_frame_ + 1) == frame->frame_id();
243 bool FrameIdMap::DecodableVideoFrame(FrameInfo* frame) const {
244 if (frame->is_key_frame()) return true;
245 if (waiting_for_key_ && !frame->is_key_frame()) return false;
247 // Current frame is not necessarily referencing the last frame.
248 // Do we have the reference frame?
249 if (IsOlderFrameId(frame->referenced_frame_id(), last_released_frame_)) {
252 return frame->referenced_frame_id() == last_released_frame_;