OLD | NEW |
---|---|
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/packet_storage.h" | 5 #include "media/cast/net/rtp/packet_storage.h" |
6 | 6 |
7 #include <string> | |
8 | |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "media/cast/cast_defines.h" | 8 #include "media/cast/cast_defines.h" |
11 | 9 |
12 namespace media { | 10 namespace media { |
13 namespace cast { | 11 namespace cast { |
14 | 12 |
15 PacketStorage::PacketStorage(size_t stored_frames) | 13 PacketStorage::PacketStorage() |
16 : max_stored_frames_(stored_frames), | 14 : first_frame_id_in_list_(0), |
17 first_frame_id_in_list_(0), | 15 zombie_count_(0) { |
18 last_frame_id_in_list_(0) { | |
19 } | 16 } |
20 | 17 |
21 PacketStorage::~PacketStorage() { | 18 PacketStorage::~PacketStorage() { |
22 } | 19 } |
23 | 20 |
24 bool PacketStorage::IsValid() const { | |
25 return max_stored_frames_ > 0 && | |
26 static_cast<int>(max_stored_frames_) <= kMaxUnackedFrames; | |
27 } | |
28 | |
29 size_t PacketStorage::GetNumberOfStoredFrames() const { | 21 size_t PacketStorage::GetNumberOfStoredFrames() const { |
30 return frames_.size(); | 22 return frames_.size() - zombie_count_; |
31 } | 23 } |
32 | 24 |
33 void PacketStorage::StoreFrame(uint32 frame_id, | 25 void PacketStorage::StoreFrame(uint32 frame_id, |
34 const SendPacketVector& packets) { | 26 const SendPacketVector& packets) { |
27 if (packets.empty()) { | |
28 NOTREACHED(); | |
29 return; | |
30 } | |
31 | |
35 if (frames_.empty()) { | 32 if (frames_.empty()) { |
36 first_frame_id_in_list_ = frame_id; | 33 first_frame_id_in_list_ = frame_id; |
37 } else { | 34 } else { |
38 // Make sure frame IDs are consecutive. | 35 // Make sure frame IDs are consecutive. |
39 DCHECK_EQ(last_frame_id_in_list_ + 1, frame_id); | 36 DCHECK_EQ(first_frame_id_in_list_ + static_cast<uint32>(frames_.size()), |
37 frame_id); | |
38 | |
39 // Evict the oldest frame if the list will become too long. | |
40 if (frames_.size() == kMaxUnackedFrames) | |
hubbe
2014/09/11 23:05:05
This really should never happen, right?
LOG(WARNIN
miu
2014/09/18 01:10:52
Done. Made it a DCHECK.
Also, got rid of the uni
| |
41 ReleaseFrame(first_frame_id_in_list_); | |
40 } | 42 } |
41 | 43 |
42 // Save new frame to the end of the list. | 44 // Save new frame to the end of the list. |
43 last_frame_id_in_list_ = frame_id; | |
44 frames_.push_back(packets); | 45 frames_.push_back(packets); |
46 } | |
45 | 47 |
46 // Evict the oldest frame if the list is too long. | 48 void PacketStorage::ReleaseFrame(uint32 frame_id) { |
47 if (frames_.size() > max_stored_frames_) { | 49 const uint32 offset = frame_id - first_frame_id_in_list_; |
50 if (static_cast<int32>(offset) < 0 || offset >= frames_.size() || | |
51 frames_[offset].empty()) { | |
52 return; | |
53 } | |
54 | |
55 frames_[offset].clear(); | |
56 ++zombie_count_; | |
57 | |
58 while (!frames_.empty() && frames_.front().empty()) { | |
59 DCHECK_GT(zombie_count_, 0u); | |
60 --zombie_count_; | |
48 frames_.pop_front(); | 61 frames_.pop_front(); |
49 ++first_frame_id_in_list_; | 62 ++first_frame_id_in_list_; |
50 } | 63 } |
51 } | 64 } |
52 | 65 |
53 const SendPacketVector* PacketStorage::GetFrame8(uint8 frame_id_8bits) const { | 66 const SendPacketVector* PacketStorage::GetFrame8(uint8 frame_id_8bits) const { |
54 // The requested frame ID has only 8-bits so convert the first frame ID | 67 // The requested frame ID has only 8-bits so convert the first frame ID |
55 // in list to match. | 68 // in list to match. |
56 uint8 index_8bits = first_frame_id_in_list_ & 0xFF; | 69 uint8 index_8bits = first_frame_id_in_list_ & 0xFF; |
57 index_8bits = frame_id_8bits - index_8bits; | 70 index_8bits = frame_id_8bits - index_8bits; |
58 if (index_8bits >= frames_.size()) | 71 if (index_8bits >= frames_.size()) |
59 return NULL; | 72 return NULL; |
60 return &(frames_[index_8bits]); | 73 const SendPacketVector& packets = frames_[index_8bits]; |
74 return packets.empty() ? NULL : &packets; | |
61 } | 75 } |
62 | 76 |
63 } // namespace cast | 77 } // namespace cast |
64 } // namespace media | 78 } // namespace media |
OLD | NEW |