1// Copyright (c) 2012 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// Tracks information about an FEC group, including the packets
6// that have been seen, and the running parity.  Provided the ability
7// to revive a dropped packet.
8
9#ifndef NET_QUIC_QUIC_FEC_GROUP_H_
10#define NET_QUIC_QUIC_FEC_GROUP_H_
11
12#include "base/strings/string_piece.h"
13#include "net/quic/quic_protocol.h"
14
15namespace net {
16
17class NET_EXPORT_PRIVATE QuicFecGroup {
18 public:
19  QuicFecGroup();
20  ~QuicFecGroup();
21
22  // Updates the FEC group based on the delivery of a data packet decrypted at
23  // |encryption_level|. Returns false if this packet has already been seen,
24  // true otherwise.
25  bool Update(EncryptionLevel encryption_level,
26              const QuicPacketHeader& header,
27              base::StringPiece decrypted_payload);
28
29  // Updates the FEC group based on the delivery of an FEC packet decrypted at
30  // |encryption_level|. Returns false if this packet has already been seen or
31  // if it does not claim to protect all the packets previously seen in this
32  // group.
33  bool UpdateFec(EncryptionLevel encryption_level,
34                 QuicPacketSequenceNumber fec_packet_sequence_number,
35                 const QuicFecData& fec);
36
37  // Returns true if a packet can be revived from this FEC group.
38  bool CanRevive() const;
39
40  // Returns true if all packets (FEC and data) from this FEC group have been
41  // seen or revived
42  bool IsFinished() const;
43
44  // Revives the missing packet from this FEC group.  This may return a packet
45  // that is null padded to a greater length than the original packet, but
46  // the framer will handle it correctly.  Returns the length of the data
47  // written to |decrypted_payload|, or 0 if the packet could not be revived.
48  size_t Revive(QuicPacketHeader* header,
49                char* decrypted_payload,
50                size_t decrypted_payload_len);
51
52  // Returns true of this FEC group protects any packets with sequence
53  // numbers less than |num|.
54  bool ProtectsPacketsBefore(QuicPacketSequenceNumber num) const;
55
56  const base::StringPiece payload_parity() const {
57    return base::StringPiece(payload_parity_, payload_parity_len_);
58  }
59
60  QuicPacketSequenceNumber min_protected_packet() const {
61    return min_protected_packet_;
62  }
63
64  size_t NumReceivedPackets() const {
65    return received_packets_.size();
66  }
67
68  // Returns the effective encryption level of the FEC group.
69  EncryptionLevel effective_encryption_level() const {
70    return effective_encryption_level_;
71  }
72
73 private:
74  bool UpdateParity(base::StringPiece payload);
75  // Returns the number of missing packets, or size_t max if the number
76  // of missing packets is not known.
77  size_t NumMissingPackets() const;
78
79  // Set of packets that we have recevied.
80  SequenceNumberSet received_packets_;
81  // Sequence number of the first protected packet in this group (the one
82  // with the lowest packet sequence number).  Will only be set once the FEC
83  // packet has been seen.
84  QuicPacketSequenceNumber min_protected_packet_;
85  // Sequence number of the last protected packet in this group (the one
86  // with the highest packet sequence number).  Will only be set once the FEC
87  // packet has been seen.
88  QuicPacketSequenceNumber max_protected_packet_;
89  // The cumulative parity calculation of all received packets.
90  char payload_parity_[kMaxPacketSize];
91  size_t payload_parity_len_;
92  // The effective encryption level, which is the lowest encryption level of
93  // the data and FEC in the group.
94  EncryptionLevel effective_encryption_level_;
95
96  DISALLOW_COPY_AND_ASSIGN(QuicFecGroup);
97};
98
99}  // namespace net
100
101#endif  // NET_QUIC_QUIC_FEC_GROUP_H_
102