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
3 // found in the LICENSE file.
5 #include "media/cast/net/rtp/packet_storage.h"
7 #include "base/logging.h"
8 #include "media/cast/cast_defines.h"
13 PacketStorage::PacketStorage()
14 : first_frame_id_in_list_(0),
18 PacketStorage::~PacketStorage() {
21 size_t PacketStorage::GetNumberOfStoredFrames() const {
22 return frames_
.size() - zombie_count_
;
25 void PacketStorage::StoreFrame(uint32 frame_id
,
26 const SendPacketVector
& packets
) {
27 if (packets
.empty()) {
32 if (frames_
.empty()) {
33 first_frame_id_in_list_
= frame_id
;
35 // Make sure frame IDs are consecutive.
36 DCHECK_EQ(first_frame_id_in_list_
+ static_cast<uint32
>(frames_
.size()),
38 // Make sure we aren't being asked to store more frames than the system's
40 DCHECK_LT(frames_
.size(), static_cast<size_t>(kMaxUnackedFrames
));
43 // Save new frame to the end of the list.
44 frames_
.push_back(packets
);
47 void PacketStorage::ReleaseFrame(uint32 frame_id
) {
48 const uint32 offset
= frame_id
- first_frame_id_in_list_
;
49 if (static_cast<int32
>(offset
) < 0 || offset
>= frames_
.size() ||
50 frames_
[offset
].empty()) {
54 frames_
[offset
].clear();
57 while (!frames_
.empty() && frames_
.front().empty()) {
58 DCHECK_GT(zombie_count_
, 0u);
61 ++first_frame_id_in_list_
;
65 const SendPacketVector
* PacketStorage::GetFrame8(uint8 frame_id_8bits
) const {
66 // The requested frame ID has only 8-bits so convert the first frame ID
68 uint8 index_8bits
= first_frame_id_in_list_
& 0xFF;
69 index_8bits
= frame_id_8bits
- index_8bits
;
70 if (index_8bits
>= frames_
.size())
72 const SendPacketVector
& packets
= frames_
[index_8bits
];
73 return packets
.empty() ? NULL
: &packets
;