Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: media/cast/net/rtp/framer.cc

Issue 540713002: Cast: Get rid of frame_id_map (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: removed unused variable Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/cast/net/rtp/framer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/cast/net/rtp/framer.h" 5 #include "media/cast/net/rtp/framer.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 namespace media { 9 namespace media {
10 namespace cast { 10 namespace cast {
11 11
12 typedef FrameList::const_iterator ConstFrameIterator; 12 typedef FrameList::const_iterator ConstFrameIterator;
13 13
14 Framer::Framer(base::TickClock* clock, 14 Framer::Framer(base::TickClock* clock,
15 RtpPayloadFeedback* incoming_payload_feedback, 15 RtpPayloadFeedback* incoming_payload_feedback,
16 uint32 ssrc, 16 uint32 ssrc,
17 bool decoder_faster_than_max_frame_rate, 17 bool decoder_faster_than_max_frame_rate,
18 int max_unacked_frames) 18 int max_unacked_frames)
19 : decoder_faster_than_max_frame_rate_(decoder_faster_than_max_frame_rate), 19 : decoder_faster_than_max_frame_rate_(decoder_faster_than_max_frame_rate),
20 cast_msg_builder_( 20 cast_msg_builder_(
21 new CastMessageBuilder(clock, 21 new CastMessageBuilder(clock,
22 incoming_payload_feedback, 22 incoming_payload_feedback,
23 &frame_id_map_, 23 this,
24 ssrc, 24 ssrc,
25 decoder_faster_than_max_frame_rate, 25 decoder_faster_than_max_frame_rate,
26 max_unacked_frames)) { 26 max_unacked_frames)),
27 waiting_for_key_(true),
28 last_released_frame_(kStartFrameId),
29 newest_frame_id_(kStartFrameId) {
27 DCHECK(incoming_payload_feedback) << "Invalid argument"; 30 DCHECK(incoming_payload_feedback) << "Invalid argument";
28 } 31 }
29 32
30 Framer::~Framer() {} 33 Framer::~Framer() {}
31 34
32 bool Framer::InsertPacket(const uint8* payload_data, 35 bool Framer::InsertPacket(const uint8* payload_data,
33 size_t payload_size, 36 size_t payload_size,
34 const RtpCastHeader& rtp_header, 37 const RtpCastHeader& rtp_header,
35 bool* duplicate) { 38 bool* duplicate) {
36 *duplicate = false; 39 *duplicate = false;
37 PacketType packet_type = frame_id_map_.InsertPacket(rtp_header); 40 uint32 frame_id = rtp_header.frame_id;
38 if (packet_type == kTooOldPacket) { 41
42 if (rtp_header.is_key_frame && waiting_for_key_) {
43 last_released_frame_ = static_cast<uint32>(frame_id - 1);
44 waiting_for_key_ = false;
45 }
46
47 VLOG(0) << "InsertPacket frame:" << frame_id
48 << " packet:" << static_cast<int>(rtp_header.packet_id)
49 << " max packet:" << static_cast<int>(rtp_header.max_packet_id);
50
51 if (IsOlderFrameId(frame_id, last_released_frame_) && !waiting_for_key_) {
52 // Packet is too old.
39 return false; 53 return false;
40 } 54 }
41 if (packet_type == kDuplicatePacket) { 55
56 // Update the last received frame id.
57 if (IsNewerFrameId(frame_id, newest_frame_id_)) {
58 newest_frame_id_ = frame_id;
59 }
60
61 // Does this packet belong to a new frame?
62 FrameList::iterator it = frames_.find(frame_id);
63 if (it == frames_.end()) {
64 // New frame.
65 linked_ptr<FrameBuffer> frame_info(new FrameBuffer);
66 std::pair<FrameList::iterator, bool> retval =
67 frames_.insert(std::make_pair(frame_id, frame_info));
68 it = retval.first;
69 }
70
71 // Insert packet.
72 if (!it->second->InsertPacket(payload_data, payload_size, rtp_header)) {
42 VLOG(3) << "Packet already received, ignored: frame " 73 VLOG(3) << "Packet already received, ignored: frame "
43 << static_cast<int>(rtp_header.frame_id) << ", packet " 74 << static_cast<int>(rtp_header.frame_id) << ", packet "
44 << rtp_header.packet_id; 75 << rtp_header.packet_id;
45 *duplicate = true; 76 *duplicate = true;
46 return false; 77 return false;
47 } 78 }
48 79
49 // Does this packet belong to a new frame? 80 return it->second->Complete();
50 FrameList::iterator it = frames_.find(rtp_header.frame_id);
51 if (it == frames_.end()) {
52 // New frame.
53 linked_ptr<FrameBuffer> frame_buffer(new FrameBuffer());
54 frame_buffer->InsertPacket(payload_data, payload_size, rtp_header);
55 frames_.insert(std::make_pair(rtp_header.frame_id, frame_buffer));
56 } else {
57 // Insert packet to existing frame buffer.
58 it->second->InsertPacket(payload_data, payload_size, rtp_header);
59 }
60
61 return packet_type == kNewPacketCompletingFrame;
62 } 81 }
63 82
64 // This does not release the frame. 83 // This does not release the frame.
65 bool Framer::GetEncodedFrame(EncodedFrame* frame, 84 bool Framer::GetEncodedFrame(EncodedFrame* frame,
66 bool* next_frame, 85 bool* next_frame,
67 bool* have_multiple_decodable_frames) { 86 bool* have_multiple_decodable_frames) {
68 *have_multiple_decodable_frames = frame_id_map_.HaveMultipleDecodableFrames(); 87 *have_multiple_decodable_frames = HaveMultipleDecodableFrames();
69 88
70 uint32 frame_id; 89 uint32 frame_id;
71 // Find frame id. 90 // Find frame id.
72 if (frame_id_map_.NextContinuousFrame(&frame_id)) { 91 if (NextContinuousFrame(&frame_id)) {
73 // We have our next frame. 92 // We have our next frame.
74 *next_frame = true; 93 *next_frame = true;
75 } else { 94 } else {
76 // Check if we can skip frames when our decoder is too slow. 95 // Check if we can skip frames when our decoder is too slow.
77 if (!decoder_faster_than_max_frame_rate_) 96 if (!decoder_faster_than_max_frame_rate_)
78 return false; 97 return false;
79 98
80 if (!frame_id_map_.NextFrameAllowingSkippingFrames(&frame_id)) { 99 if (!NextFrameAllowingSkippingFrames(&frame_id)) {
81 return false; 100 return false;
82 } 101 }
83 *next_frame = false; 102 *next_frame = false;
84 } 103 }
85 104
86 ConstFrameIterator it = frames_.find(frame_id); 105 ConstFrameIterator it = frames_.find(frame_id);
87 DCHECK(it != frames_.end()); 106 DCHECK(it != frames_.end());
88 if (it == frames_.end()) 107 if (it == frames_.end())
89 return false; 108 return false;
90 109
91 return it->second->AssembleEncodedFrame(frame); 110 return it->second->AssembleEncodedFrame(frame);
92 } 111 }
93 112
94 void Framer::AckFrame(uint32 frame_id) { 113 void Framer::AckFrame(uint32 frame_id) {
95 VLOG(2) << "ACK frame " << frame_id; 114 VLOG(2) << "ACK frame " << frame_id;
96 cast_msg_builder_->CompleteFrameReceived(frame_id); 115 cast_msg_builder_->CompleteFrameReceived(frame_id);
97 } 116 }
98 117
99 void Framer::Reset() { 118 void Framer::Reset() {
100 frame_id_map_.Clear(); 119 waiting_for_key_ = true;
120 last_released_frame_ = kStartFrameId;
121 newest_frame_id_ = kStartFrameId;
101 frames_.clear(); 122 frames_.clear();
102 cast_msg_builder_->Reset(); 123 cast_msg_builder_->Reset();
103 } 124 }
104 125
105 void Framer::ReleaseFrame(uint32 frame_id) { 126 void Framer::ReleaseFrame(uint32 frame_id) {
106 frame_id_map_.RemoveOldFrames(frame_id); 127 RemoveOldFrames(frame_id);
107 frames_.erase(frame_id); 128 frames_.erase(frame_id);
108 129
109 // We have a frame - remove all frames with lower frame id. 130 // We have a frame - remove all frames with lower frame id.
110 bool skipped_old_frame = false; 131 bool skipped_old_frame = false;
111 FrameList::iterator it; 132 FrameList::iterator it;
112 for (it = frames_.begin(); it != frames_.end();) { 133 for (it = frames_.begin(); it != frames_.end();) {
113 if (IsOlderFrameId(it->first, frame_id)) { 134 if (IsOlderFrameId(it->first, frame_id)) {
114 frames_.erase(it++); 135 frames_.erase(it++);
115 skipped_old_frame = true; 136 skipped_old_frame = true;
116 } else { 137 } else {
117 ++it; 138 ++it;
118 } 139 }
119 } 140 }
120 if (skipped_old_frame) { 141 if (skipped_old_frame) {
121 cast_msg_builder_->UpdateCastMessage(); 142 cast_msg_builder_->UpdateCastMessage();
122 } 143 }
123 } 144 }
124 145
125 bool Framer::TimeToSendNextCastMessage(base::TimeTicks* time_to_send) { 146 bool Framer::TimeToSendNextCastMessage(base::TimeTicks* time_to_send) {
126 return cast_msg_builder_->TimeToSendNextCastMessage(time_to_send); 147 return cast_msg_builder_->TimeToSendNextCastMessage(time_to_send);
127 } 148 }
128 149
129 void Framer::SendCastMessage() { cast_msg_builder_->UpdateCastMessage(); } 150 void Framer::SendCastMessage() { cast_msg_builder_->UpdateCastMessage(); }
130 151
152 void Framer::RemoveOldFrames(uint32 frame_id) {
153 FrameList::iterator it = frames_.begin();
154
155 while (it != frames_.end()) {
156 if (IsNewerFrameId(it->first, frame_id)) {
157 ++it;
158 } else {
159 // Older or equal; erase.
160 frames_.erase(it++);
161 }
162 }
163 last_released_frame_ = frame_id;
164 }
165
166 uint32 Framer::NewestFrameId() const { return newest_frame_id_; }
167
168 bool Framer::NextContinuousFrame(uint32* frame_id) const {
169 FrameList::const_iterator it;
170
171 for (it = frames_.begin(); it != frames_.end(); ++it) {
172 if (it->second->Complete() && ContinuousFrame(it->second.get())) {
173 *frame_id = it->first;
174 return true;
175 }
176 }
177 return false;
178 }
179
180 bool Framer::HaveMultipleDecodableFrames() const {
181 // Find the oldest decodable frame.
182 FrameList::const_iterator it;
183 bool found_one = false;
184 for (it = frames_.begin(); it != frames_.end(); ++it) {
185 if (it->second->Complete() && DecodableFrame(it->second.get())) {
186 if (found_one) {
187 return true;
188 } else {
189 found_one = true;
190 }
191 }
192 }
193 return false;
194 }
195
196 uint32 Framer::LastContinuousFrame() const {
197 uint32 last_continuous_frame_id = last_released_frame_;
198 uint32 next_expected_frame = last_released_frame_;
199
200 FrameList::const_iterator it;
201
202 do {
203 next_expected_frame++;
204 it = frames_.find(next_expected_frame);
205 if (it == frames_.end())
206 break;
207 if (!it->second->Complete())
208 break;
209
210 // We found the next continuous frame.
211 last_continuous_frame_id = it->first;
212 } while (next_expected_frame != newest_frame_id_);
213 return last_continuous_frame_id;
214 }
215
216 bool Framer::NextFrameAllowingSkippingFrames(uint32* frame_id) const {
217 // Find the oldest decodable frame.
218 FrameList::const_iterator it_best_match = frames_.end();
219 FrameList::const_iterator it;
220 for (it = frames_.begin(); it != frames_.end(); ++it) {
221 if (it->second->Complete() && DecodableFrame(it->second.get())) {
222 if (it_best_match == frames_.end() ||
223 IsOlderFrameId(it->first, it_best_match->first)) {
224 it_best_match = it;
225 }
226 }
227 }
228 if (it_best_match == frames_.end())
229 return false;
230
231 *frame_id = it_best_match->first;
232 return true;
233 }
234
235 bool Framer::Empty() const { return frames_.empty(); }
236
237 int Framer::NumberOfCompleteFrames() const {
238 int count = 0;
239 FrameList::const_iterator it;
240 for (it = frames_.begin(); it != frames_.end(); ++it) {
241 if (it->second->Complete()) {
242 ++count;
243 }
244 }
245 return count;
246 }
247
248 bool Framer::FrameExists(uint32 frame_id) const {
249 return frames_.end() != frames_.find(frame_id);
250 }
251
252 void Framer::GetMissingPackets(uint32 frame_id,
253 bool last_frame,
254 PacketIdSet* missing_packets) const {
255 FrameList::const_iterator it = frames_.find(frame_id);
256 if (it == frames_.end())
257 return;
258
259 it->second->GetMissingPackets(last_frame, missing_packets);
260 }
261
262 bool Framer::ContinuousFrame(FrameBuffer* frame) const {
263 DCHECK(frame);
264 if (waiting_for_key_ && !frame->is_key_frame())
265 return false;
266 return static_cast<uint32>(last_released_frame_ + 1) == frame->frame_id();
267 }
268
269 bool Framer::DecodableFrame(FrameBuffer* frame) const {
270 if (frame->is_key_frame())
271 return true;
272 if (waiting_for_key_ && !frame->is_key_frame())
273 return false;
274 // Self-reference?
275 if (frame->last_referenced_frame_id() == frame->frame_id())
276 return true;
277
278 // Current frame is not necessarily referencing the last frame.
279 // Do we have the reference frame?
280 if (IsOlderFrameId(frame->last_referenced_frame_id(), last_released_frame_)) {
281 return true;
282 }
283 return frame->last_referenced_frame_id() == last_released_frame_;
284 }
285
286
131 } // namespace cast 287 } // namespace cast
132 } // namespace media 288 } // namespace media
OLDNEW
« no previous file with comments | « media/cast/net/rtp/framer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698