• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/logging.h"
8 #include "media/cast/cast_defines.h"
9 
10 namespace media {
11 namespace cast {
12 
PacketStorage()13 PacketStorage::PacketStorage()
14     : first_frame_id_in_list_(0),
15       zombie_count_(0) {
16 }
17 
~PacketStorage()18 PacketStorage::~PacketStorage() {
19 }
20 
GetNumberOfStoredFrames() const21 size_t PacketStorage::GetNumberOfStoredFrames() const {
22   return frames_.size() - zombie_count_;
23 }
24 
StoreFrame(uint32 frame_id,const SendPacketVector & packets)25 void PacketStorage::StoreFrame(uint32 frame_id,
26                                const SendPacketVector& packets) {
27   if (packets.empty()) {
28     NOTREACHED();
29     return;
30   }
31 
32   if (frames_.empty()) {
33     first_frame_id_in_list_ = frame_id;
34   } else {
35     // Make sure frame IDs are consecutive.
36     DCHECK_EQ(first_frame_id_in_list_ + static_cast<uint32>(frames_.size()),
37               frame_id);
38     // Make sure we aren't being asked to store more frames than the system's
39     // design limit.
40     DCHECK_LT(frames_.size(), static_cast<size_t>(kMaxUnackedFrames));
41   }
42 
43   // Save new frame to the end of the list.
44   frames_.push_back(packets);
45 }
46 
ReleaseFrame(uint32 frame_id)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()) {
51     return;
52   }
53 
54   frames_[offset].clear();
55   ++zombie_count_;
56 
57   while (!frames_.empty() && frames_.front().empty()) {
58     DCHECK_GT(zombie_count_, 0u);
59     --zombie_count_;
60     frames_.pop_front();
61     ++first_frame_id_in_list_;
62   }
63 }
64 
GetFrame8(uint8 frame_id_8bits) const65 const SendPacketVector* PacketStorage::GetFrame8(uint8 frame_id_8bits) const {
66   // The requested frame ID has only 8-bits so convert the first frame ID
67   // in list to match.
68   uint8 index_8bits = first_frame_id_in_list_ & 0xFF;
69   index_8bits = frame_id_8bits - index_8bits;
70   if (index_8bits >= frames_.size())
71     return NULL;
72   const SendPacketVector& packets = frames_[index_8bits];
73   return packets.empty() ? NULL : &packets;
74 }
75 
76 }  // namespace cast
77 }  // namespace media
78