summaryrefslogtreecommitdiff
path: root/media/cast/rtp_sender/packet_storage/packet_storage.cc
blob: 96e2d275d6f9f827aea5a4c4b3a670914d24c48e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/cast/rtp_sender/packet_storage/packet_storage.h"

#include <string>

#include "base/logging.h"
#include "media/cast/cast_defines.h"

namespace media {
namespace cast {

// Limit the max time delay to avoid frame id wrap around; 256 / 60 fps.
const int kMaxAllowedTimeStoredMs = 4000;

typedef PacketMap::iterator PacketMapIterator;
typedef TimeToPacketMap::iterator TimeToPacketIterator;

class StoredPacket {
 public:
  StoredPacket() {
    packet_.reserve(kIpPacketSize);
  }

  void Save(const Packet* packet) {
    DCHECK_LT(packet->size(), kIpPacketSize) << "Invalid argument";
    packet_.clear();
    packet_.insert(packet_.begin(), packet->begin(), packet->end());
  }

  void GetCopy(PacketList* packets) {
    packets->push_back(Packet(packet_.begin(), packet_.end()));
  }

 private:
  Packet packet_;
};

PacketStorage::PacketStorage(base::TickClock* clock,
                             int max_time_stored_ms)
    : clock_(clock) {
  max_time_stored_ = base::TimeDelta::FromMilliseconds(max_time_stored_ms);
  DCHECK_LE(max_time_stored_ms, kMaxAllowedTimeStoredMs) << "Invalid argument";
}

PacketStorage::~PacketStorage() {
  time_to_packet_map_.clear();

  PacketMapIterator store_it = stored_packets_.begin();
  for (; store_it != stored_packets_.end();
      store_it = stored_packets_.begin()) {
    stored_packets_.erase(store_it);
  }
  while (!free_packets_.empty()) {
    free_packets_.pop_front();
  }
}

void PacketStorage::CleanupOldPackets(base::TimeTicks now) {
  TimeToPacketIterator time_it = time_to_packet_map_.begin();

  // Check max size.
  while (time_to_packet_map_.size() >= kMaxStoredPackets) {
    PacketMapIterator store_it = stored_packets_.find(time_it->second);

    // We should always find the packet.
    DCHECK(store_it != stored_packets_.end()) << "Invalid state";
    time_to_packet_map_.erase(time_it);
    // Save the pointer.
    linked_ptr<StoredPacket> storted_packet = store_it->second;
    stored_packets_.erase(store_it);
    // Add this packet to the free list for later re-use.
    free_packets_.push_back(storted_packet);
    time_it = time_to_packet_map_.begin();
  }

  // Time out old packets.
  while (time_it != time_to_packet_map_.end()) {
    if (now < time_it->first + max_time_stored_) {
      break;
    }
    // Packet too old.
    PacketMapIterator store_it = stored_packets_.find(time_it->second);

    // We should always find the packet.
    DCHECK(store_it != stored_packets_.end()) << "Invalid state";
    time_to_packet_map_.erase(time_it);
    // Save the pointer.
    linked_ptr<StoredPacket> storted_packet = store_it->second;
    stored_packets_.erase(store_it);
    // Add this packet to the free list for later re-use.
    free_packets_.push_back(storted_packet);
    time_it = time_to_packet_map_.begin();
  }
}

void PacketStorage::StorePacket(uint32 frame_id, uint16 packet_id,
                                const Packet* packet) {
  base::TimeTicks now = clock_->NowTicks();
  CleanupOldPackets(now);

  // Internally we only use the 8 LSB of the frame id.
  uint32 index = ((0xff & frame_id) << 16) + packet_id;
  PacketMapIterator it = stored_packets_.find(index);
  if (it != stored_packets_.end()) {
    // We have already saved this.
    DCHECK(false) << "Invalid state";
    return;
  }
  linked_ptr<StoredPacket> stored_packet;
  if (free_packets_.empty()) {
    // No previous allocated packets allocate one.
    stored_packet.reset(new StoredPacket());
  } else {
    // Re-use previous allocated packet.
    stored_packet = free_packets_.front();
    free_packets_.pop_front();
  }
  stored_packet->Save(packet);
  stored_packets_[index] = stored_packet;
  time_to_packet_map_.insert(std::make_pair(now, index));
}

PacketList PacketStorage::GetPackets(
    const MissingFramesAndPacketsMap& missing_frames_and_packets) {
  PacketList packets_to_resend;

  // Iterate over all frames in the list.
  for (MissingFramesAndPacketsMap::const_iterator it =
       missing_frames_and_packets.begin();
       it != missing_frames_and_packets.end(); ++it) {
    uint8 frame_id = it->first;
    const PacketIdSet& packets_set = it->second;
    bool success = false;

    if (packets_set.empty()) {
      VLOG(1) << "Missing all packets in frame " << static_cast<int>(frame_id);

      uint16 packet_id = 0;
      do {
        // Get packet from storage.
        success = GetPacket(frame_id, packet_id, &packets_to_resend);
        ++packet_id;
      } while (success);
    } else {
      // Iterate over all of the packets in the frame.
      for (PacketIdSet::const_iterator set_it = packets_set.begin();
          set_it != packets_set.end(); ++set_it) {
        GetPacket(frame_id, *set_it, &packets_to_resend);
      }
    }
  }
  return packets_to_resend;
}

bool PacketStorage::GetPacket(uint8 frame_id,
                              uint16 packet_id,
                              PacketList* packets) {
  // Internally we only use the 8 LSB of the frame id.
  uint32 index = (static_cast<uint32>(frame_id) << 16) + packet_id;
  PacketMapIterator it = stored_packets_.find(index);
  if (it == stored_packets_.end()) {
    return false;
  }
  it->second->GetCopy(packets);
  VLOG(1) << "Resend " << static_cast<int>(frame_id)
          << ":" << packet_id;
  return true;
}

}  // namespace cast
}  // namespace media