summaryrefslogtreecommitdiff
path: root/chromium/net/quic/quic_fec_group.h
blob: d905d03236ee299e475f8e80dce124c061d6f838 (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
// Copyright (c) 2012 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.
//
// Tracks information about an FEC group, including the packets
// that have been seen, and the running parity.  Provided the ability
// to revive a dropped packet.

#ifndef NET_QUIC_QUIC_FEC_GROUP_H_
#define NET_QUIC_QUIC_FEC_GROUP_H_

#include <set>

#include "base/strings/string_piece.h"
#include "net/quic/quic_protocol.h"

namespace net {

class NET_EXPORT_PRIVATE QuicFecGroup {
 public:
  QuicFecGroup();
  ~QuicFecGroup();

  // Updates the FEC group based on the delivery of a data packet.
  // Returns false if this packet has already been seen, true otherwise.
  bool Update(const QuicPacketHeader& header,
              base::StringPiece decrypted_payload);

  // Updates the FEC group based on the delivery of an FEC packet.
  // Returns false if this packet has already been seen or if it does
  // not claim to protect all the packets previously seen in this group.
  //   |fec_packet_entropy|: XOR of entropy of all packets in the fec group.
  bool UpdateFec(QuicPacketSequenceNumber fec_packet_sequence_number,
                 bool fec_packet_entropy,
                 const QuicFecData& fec);

  // Returns true if a packet can be revived from this FEC group.
  bool CanRevive() const;

  // Returns true if all packets (FEC and data) from this FEC group have been
  // seen or revived
  bool IsFinished() const;

  // Revives the missing packet from this FEC group.  This may return a packet
  // that is null padded to a greater length than the original packet, but
  // the framer will handle it correctly.  Returns the length of the data
  // written to |decrypted_payload|, or 0 if the packet could not be revived.
  size_t Revive(QuicPacketHeader* header,
                char* decrypted_payload,
                size_t decrypted_payload_len);

  // Returns true of this FEC group protects any packets with sequence
  // numbers less than |num|.
  bool ProtectsPacketsBefore(QuicPacketSequenceNumber num) const;

  const base::StringPiece payload_parity() const {
    return base::StringPiece(payload_parity_, payload_parity_len_);
  }

  bool entropy_parity() const {
    return entropy_parity_;
  }

  QuicPacketSequenceNumber min_protected_packet() const {
    return min_protected_packet_;
  }

  size_t NumReceivedPackets() const {
    return received_packets_.size();
  }

 private:
  bool UpdateParity(base::StringPiece payload, bool entropy);
  // Returns the number of missing packets, or size_t max if the number
  // of missing packets is not known.
  size_t NumMissingPackets() const;

  // Set of packets that we have recevied.
  SequenceNumberSet received_packets_;
  // Sequence number of the first protected packet in this group (the one
  // with the lowest packet sequence number).  Will only be set once the FEC
  // packet has been seen.
  QuicPacketSequenceNumber min_protected_packet_;
  // Sequence number of the last protected packet in this group (the one
  // with the highest packet sequence number).  Will only be set once the FEC
  // packet has been seen.
  QuicPacketSequenceNumber max_protected_packet_;
  // The cumulative parity calculation of all received packets.
  char payload_parity_[kMaxPacketSize];
  size_t payload_parity_len_;
  bool entropy_parity_;

  DISALLOW_COPY_AND_ASSIGN(QuicFecGroup);
};

}  // namespace net

#endif  // NET_QUIC_QUIC_FEC_GROUP_H_