1 /* 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_TRANSPORT_FEEDBACK_H_ 12 #define MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_TRANSPORT_FEEDBACK_H_ 13 14 #include <memory> 15 #include <vector> 16 17 #include "api/units/time_delta.h" 18 #include "modules/rtp_rtcp/source/rtcp_packet/rtpfb.h" 19 20 namespace webrtc { 21 namespace rtcp { 22 class CommonHeader; 23 24 class TransportFeedback : public Rtpfb { 25 public: 26 class ReceivedPacket { 27 public: ReceivedPacket(uint16_t sequence_number,int16_t delta_ticks)28 ReceivedPacket(uint16_t sequence_number, int16_t delta_ticks) 29 : sequence_number_(sequence_number), 30 delta_ticks_(delta_ticks), 31 received_(true) {} ReceivedPacket(uint16_t sequence_number)32 explicit ReceivedPacket(uint16_t sequence_number) 33 : sequence_number_(sequence_number), received_(false) {} 34 ReceivedPacket(const ReceivedPacket&) = default; 35 ReceivedPacket& operator=(const ReceivedPacket&) = default; 36 sequence_number()37 uint16_t sequence_number() const { return sequence_number_; } delta_ticks()38 int16_t delta_ticks() const { return delta_ticks_; } delta_us()39 int32_t delta_us() const { return delta_ticks_ * kDeltaScaleFactor; } delta()40 TimeDelta delta() const { return TimeDelta::Micros(delta_us()); } received()41 bool received() const { return received_; } 42 43 private: 44 uint16_t sequence_number_; 45 int16_t delta_ticks_; 46 bool received_; 47 }; 48 // TODO(sprang): IANA reg? 49 static constexpr uint8_t kFeedbackMessageType = 15; 50 // Convert to multiples of 0.25ms. 51 static constexpr int kDeltaScaleFactor = 250; 52 // Maximum number of packets (including missing) TransportFeedback can report. 53 static constexpr size_t kMaxReportedPackets = 0xffff; 54 55 TransportFeedback(); 56 57 // If |include_timestamps| is set to false, the created packet will not 58 // contain the receive delta block. 59 explicit TransportFeedback(bool include_timestamps, 60 bool include_lost = false); 61 TransportFeedback(const TransportFeedback&); 62 TransportFeedback(TransportFeedback&&); 63 64 ~TransportFeedback() override; 65 66 void SetBase(uint16_t base_sequence, // Seq# of first packet in this msg. 67 int64_t ref_timestamp_us); // Reference timestamp for this msg. 68 void SetFeedbackSequenceNumber(uint8_t feedback_sequence); 69 // NOTE: This method requires increasing sequence numbers (excepting wraps). 70 bool AddReceivedPacket(uint16_t sequence_number, int64_t timestamp_us); 71 const std::vector<ReceivedPacket>& GetReceivedPackets() const; 72 const std::vector<ReceivedPacket>& GetAllPackets() const; 73 74 uint16_t GetBaseSequence() const; 75 76 // Returns number of packets (including missing) this feedback describes. GetPacketStatusCount()77 size_t GetPacketStatusCount() const { return num_seq_no_; } 78 79 // Get the reference time in microseconds, including any precision loss. 80 int64_t GetBaseTimeUs() const; 81 TimeDelta GetBaseTime() const; 82 83 // Get the unwrapped delta between current base time and |prev_timestamp_us|. 84 int64_t GetBaseDeltaUs(int64_t prev_timestamp_us) const; 85 TimeDelta GetBaseDelta(TimeDelta prev_timestamp) const; 86 87 // Does the feedback packet contain timestamp information? IncludeTimestamps()88 bool IncludeTimestamps() const { return include_timestamps_; } 89 90 bool Parse(const CommonHeader& packet); 91 static std::unique_ptr<TransportFeedback> ParseFrom(const uint8_t* buffer, 92 size_t length); 93 // Pre and postcondition for all public methods. Should always return true. 94 // This function is for tests. 95 bool IsConsistent() const; 96 97 size_t BlockLength() const override; 98 size_t PaddingLength() const; 99 100 bool Create(uint8_t* packet, 101 size_t* position, 102 size_t max_length, 103 PacketReadyCallback callback) const override; 104 105 private: 106 // Size in bytes of a delta time in rtcp packet. 107 // Valid values are 0 (packet wasn't received), 1 or 2. 108 using DeltaSize = uint8_t; 109 // Keeps DeltaSizes that can be encoded into single chunk if it is last chunk. 110 class LastChunk { 111 public: 112 using DeltaSize = TransportFeedback::DeltaSize; 113 114 LastChunk(); 115 116 bool Empty() const; 117 void Clear(); 118 // Return if delta sizes still can be encoded into single chunk with added 119 // |delta_size|. 120 bool CanAdd(DeltaSize delta_size) const; 121 // Add |delta_size|, assumes |CanAdd(delta_size)|, 122 void Add(DeltaSize delta_size); 123 124 // Encode chunk as large as possible removing encoded delta sizes. 125 // Assume CanAdd() == false for some valid delta_size. 126 uint16_t Emit(); 127 // Encode all stored delta_sizes into single chunk, pad with 0s if needed. 128 uint16_t EncodeLast() const; 129 130 // Decode up to |max_size| delta sizes from |chunk|. 131 void Decode(uint16_t chunk, size_t max_size); 132 // Appends content of the Lastchunk to |deltas|. 133 void AppendTo(std::vector<DeltaSize>* deltas) const; 134 135 private: 136 static constexpr size_t kMaxRunLengthCapacity = 0x1fff; 137 static constexpr size_t kMaxOneBitCapacity = 14; 138 static constexpr size_t kMaxTwoBitCapacity = 7; 139 static constexpr size_t kMaxVectorCapacity = kMaxOneBitCapacity; 140 static constexpr DeltaSize kLarge = 2; 141 142 uint16_t EncodeOneBit() const; 143 void DecodeOneBit(uint16_t chunk, size_t max_size); 144 145 uint16_t EncodeTwoBit(size_t size) const; 146 void DecodeTwoBit(uint16_t chunk, size_t max_size); 147 148 uint16_t EncodeRunLength() const; 149 void DecodeRunLength(uint16_t chunk, size_t max_size); 150 151 DeltaSize delta_sizes_[kMaxVectorCapacity]; 152 size_t size_; 153 bool all_same_; 154 bool has_large_delta_; 155 }; 156 157 // Reset packet to consistent empty state. 158 void Clear(); 159 160 bool AddDeltaSize(DeltaSize delta_size); 161 162 const bool include_lost_; 163 uint16_t base_seq_no_; 164 uint16_t num_seq_no_; 165 int32_t base_time_ticks_; 166 uint8_t feedback_seq_; 167 bool include_timestamps_; 168 169 int64_t last_timestamp_us_; 170 std::vector<ReceivedPacket> received_packets_; 171 std::vector<ReceivedPacket> all_packets_; 172 // All but last encoded packet chunks. 173 std::vector<uint16_t> encoded_chunks_; 174 LastChunk last_chunk_; 175 size_t size_bytes_; 176 }; 177 178 } // namespace rtcp 179 } // namespace webrtc 180 #endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_TRANSPORT_FEEDBACK_H_ 181