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_receiver/rtp_receiver_defines.h"
13 FrameInfo::FrameInfo(uint32 frame_id,
14 uint32 referenced_frame_id,
17 : is_key_frame_(key_frame),
19 referenced_frame_id_(referenced_frame_id),
20 max_received_packet_id_(0) {
21 // Create the set with all packets missing.
22 for (uint16 i = 0; i <= max_packet_id; i++) {
23 missing_packets_.insert(i);
27 FrameInfo::~FrameInfo() {}
29 PacketType FrameInfo::InsertPacket(uint16 packet_id) {
30 if (missing_packets_.find(packet_id) == missing_packets_.end()) {
31 return kDuplicatePacket;
33 // Update the last received packet id.
34 if (IsNewerPacketId(packet_id, max_received_packet_id_)) {
35 max_received_packet_id_ = packet_id;
37 missing_packets_.erase(packet_id);
38 return missing_packets_.empty() ? kNewPacketCompletingFrame : kNewPacket;
41 bool FrameInfo::Complete() const { return missing_packets_.empty(); }
43 void FrameInfo::GetMissingPackets(bool newest_frame,
44 PacketIdSet* missing_packets) const {
46 // Missing packets capped by max_received_packet_id_.
47 PacketIdSet::const_iterator it_after_last_received =
48 missing_packets_.lower_bound(max_received_packet_id_);
49 missing_packets->insert(missing_packets_.begin(), it_after_last_received);
51 missing_packets->insert(missing_packets_.begin(), missing_packets_.end());
55 FrameIdMap::FrameIdMap()
56 : waiting_for_key_(true),
57 last_released_frame_(kStartFrameId),
58 newest_frame_id_(kStartFrameId) {}
60 FrameIdMap::~FrameIdMap() {}
62 PacketType FrameIdMap::InsertPacket(const RtpCastHeader& rtp_header) {
63 uint32 frame_id = rtp_header.frame_id;
64 uint32 reference_frame_id;
65 if (rtp_header.is_reference) {
66 reference_frame_id = rtp_header.reference_frame_id;
68 reference_frame_id = static_cast<uint32>(frame_id - 1);
71 if (rtp_header.is_key_frame && waiting_for_key_) {
72 last_released_frame_ = static_cast<uint32>(frame_id - 1);
73 waiting_for_key_ = false;
76 VLOG(1) << "InsertPacket frame:" << frame_id
77 << " packet:" << static_cast<int>(rtp_header.packet_id)
78 << " max packet:" << static_cast<int>(rtp_header.max_packet_id);
80 if (IsOlderFrameId(frame_id, last_released_frame_) && !waiting_for_key_) {
84 // Update the last received frame id.
85 if (IsNewerFrameId(frame_id, newest_frame_id_)) {
86 newest_frame_id_ = frame_id;
89 // Does this packet belong to a new frame?
90 FrameMap::iterator it = frame_map_.find(frame_id);
91 PacketType packet_type;
92 if (it == frame_map_.end()) {
94 linked_ptr<FrameInfo> frame_info(new FrameInfo(frame_id,
96 rtp_header.max_packet_id,
97 rtp_header.is_key_frame));
98 std::pair<FrameMap::iterator, bool> retval =
99 frame_map_.insert(std::make_pair(frame_id, frame_info));
101 packet_type = retval.first->second->InsertPacket(rtp_header.packet_id);
103 // Insert packet to existing frame.
104 packet_type = it->second->InsertPacket(rtp_header.packet_id);
109 void FrameIdMap::RemoveOldFrames(uint32 frame_id) {
110 FrameMap::iterator it = frame_map_.begin();
112 while (it != frame_map_.end()) {
113 if (IsNewerFrameId(it->first, frame_id)) {
116 // Older or equal; erase.
117 frame_map_.erase(it++);
120 last_released_frame_ = frame_id;
123 void FrameIdMap::Clear() {
125 waiting_for_key_ = true;
126 last_released_frame_ = kStartFrameId;
127 newest_frame_id_ = kStartFrameId;
130 uint32 FrameIdMap::NewestFrameId() const { return newest_frame_id_; }
132 bool FrameIdMap::NextContinuousFrame(uint32* frame_id) const {
133 FrameMap::const_iterator it;
135 for (it = frame_map_.begin(); it != frame_map_.end(); ++it) {
136 if (it->second->Complete() && ContinuousFrame(it->second.get())) {
137 *frame_id = it->first;
144 uint32 FrameIdMap::LastContinuousFrame() const {
145 uint32 last_continuous_frame_id = last_released_frame_;
146 uint32 next_expected_frame = last_released_frame_;
148 FrameMap::const_iterator it;
151 next_expected_frame++;
152 it = frame_map_.find(next_expected_frame);
153 if (it == frame_map_.end())
155 if (!it->second->Complete())
158 // We found the next continuous frame.
159 last_continuous_frame_id = it->first;
160 } while (next_expected_frame != newest_frame_id_);
161 return last_continuous_frame_id;
164 bool FrameIdMap::NextAudioFrameAllowingMissingFrames(uint32* frame_id) const {
165 // First check if we have continuous frames.
166 if (NextContinuousFrame(frame_id))
169 // Find the oldest frame.
170 FrameMap::const_iterator it_best_match = frame_map_.end();
171 FrameMap::const_iterator it;
173 // Find first complete frame.
174 for (it = frame_map_.begin(); it != frame_map_.end(); ++it) {
175 if (it->second->Complete()) {
180 if (it_best_match == frame_map_.end())
181 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(uint32* 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())
206 *frame_id = it_best_match->first;
210 bool FrameIdMap::Empty() const { return frame_map_.empty(); }
212 int FrameIdMap::NumberOfCompleteFrames() const {
214 FrameMap::const_iterator it;
215 for (it = frame_map_.begin(); it != frame_map_.end(); ++it) {
216 if (it->second->Complete()) {
223 bool FrameIdMap::FrameExists(uint32 frame_id) const {
224 return frame_map_.end() != frame_map_.find(frame_id);
227 void FrameIdMap::GetMissingPackets(uint32 frame_id,
229 PacketIdSet* missing_packets) const {
230 FrameMap::const_iterator it = frame_map_.find(frame_id);
231 if (it == frame_map_.end())
234 it->second->GetMissingPackets(last_frame, missing_packets);
237 bool FrameIdMap::ContinuousFrame(FrameInfo* frame) const {
239 if (waiting_for_key_ && !frame->is_key_frame())
241 return static_cast<uint32>(last_released_frame_ + 1) == frame->frame_id();
244 bool FrameIdMap::DecodableVideoFrame(FrameInfo* frame) const {
245 if (frame->is_key_frame())
247 if (waiting_for_key_ && !frame->is_key_frame())
250 // Current frame is not necessarily referencing the last frame.
251 // Do we have the reference frame?
252 if (IsOlderFrameId(frame->referenced_frame_id(), last_released_frame_)) {
255 return frame->referenced_frame_id() == last_released_frame_;