Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / cast / net / rtp / packet_storage.cc
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.
4
5 #include "media/cast/net/rtp/packet_storage.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "media/cast/cast_defines.h"
11
12 namespace media {
13 namespace cast {
14
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) {
19 }
20
21 PacketStorage::~PacketStorage() {
22 }
23
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 {
30   return frames_.size();
31 }
32
33 void PacketStorage::StoreFrame(uint32 frame_id,
34                                const SendPacketVector& packets) {
35   if (frames_.empty()) {
36     first_frame_id_in_list_ = frame_id;
37   } else {
38     // Make sure frame IDs are consecutive.
39     DCHECK_EQ(last_frame_id_in_list_ + 1, frame_id);
40   }
41
42   // Save new frame to the end of the list.
43   last_frame_id_in_list_ = frame_id;
44   frames_.push_back(packets);
45
46   // Evict the oldest frame if the list is too long.
47   if (frames_.size() > max_stored_frames_) {
48     frames_.pop_front();
49     ++first_frame_id_in_list_;
50   }
51 }
52
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
55   // in list to match.
56   uint8 index_8bits = first_frame_id_in_list_ & 0xFF;
57   index_8bits = frame_id_8bits - index_8bits;
58   if (index_8bits >= frames_.size())
59     return NULL;
60   return &(frames_[index_8bits]);
61 }
62
63 }  // namespace cast
64 }  // namespace media