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"
9 #include "base/logging.h"
10 #include "media/cast/cast_defines.h"
15 PacketStorage::PacketStorage(size_t stored_frames)
16 : max_stored_frames_(stored_frames),
17 first_frame_id_in_list_(0),
18 last_frame_id_in_list_(0) {
21 PacketStorage::~PacketStorage() {
24 bool PacketStorage::IsValid() const {
25 return max_stored_frames_ > 0 &&
26 static_cast<int>(max_stored_frames_) <= kMaxUnackedFrames;
29 size_t PacketStorage::GetNumberOfStoredFrames() const {
30 return frames_.size();
33 void PacketStorage::StoreFrame(uint32 frame_id,
34 const SendPacketVector& packets) {
35 if (frames_.empty()) {
36 first_frame_id_in_list_ = frame_id;
38 // Make sure frame IDs are consecutive.
39 DCHECK_EQ(last_frame_id_in_list_ + 1, frame_id);
42 // Save new frame to the end of the list.
43 last_frame_id_in_list_ = frame_id;
44 frames_.push_back(packets);
46 // Evict the oldest frame if the list is too long.
47 if (frames_.size() > max_stored_frames_) {
49 ++first_frame_id_in_list_;
53 const SendPacketVector* PacketStorage::GetFrame8(uint8 frame_id_8bits) const {
54 // The requested frame ID has only 8-bits so convert the first frame ID
56 uint8 index_8bits = first_frame_id_in_list_ & 0xFF;
57 index_8bits = frame_id_8bits - index_8bits;
58 if (index_8bits >= frames_.size())
60 return &(frames_[index_8bits]);