blob: 6518f32bb6f490b550d903479e6b9ce895ab56eb [file] [log] [blame]
Tommid3807da2020-05-22 17:36:36 +02001/*
2 * Copyright (c) 2016 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_VIDEO_CODING_NACK_MODULE2_H_
12#define MODULES_VIDEO_CODING_NACK_MODULE2_H_
13
14#include <stdint.h>
15
16#include <map>
17#include <set>
18#include <vector>
19
20#include "api/units/time_delta.h"
21#include "modules/include/module.h"
22#include "modules/include/module_common_types.h"
23#include "modules/video_coding/histogram.h"
24#include "rtc_base/critical_section.h"
25#include "rtc_base/numerics/sequence_number_util.h"
26#include "rtc_base/thread_annotations.h"
27#include "system_wrappers/include/clock.h"
28
29namespace webrtc {
30
31class NackModule2 final : public Module {
32 public:
33 NackModule2(Clock* clock,
34 NackSender* nack_sender,
35 KeyFrameRequestSender* keyframe_request_sender);
36
37 int OnReceivedPacket(uint16_t seq_num, bool is_keyframe);
38 int OnReceivedPacket(uint16_t seq_num, bool is_keyframe, bool is_recovered);
39
40 void ClearUpTo(uint16_t seq_num);
41 void UpdateRtt(int64_t rtt_ms);
42 void Clear();
43
44 // Module implementation
45 int64_t TimeUntilNextProcess() override;
46 void Process() override;
47
48 private:
49 // Which fields to consider when deciding which packet to nack in
50 // GetNackBatch.
51 enum NackFilterOptions { kSeqNumOnly, kTimeOnly, kSeqNumAndTime };
52
53 // This class holds the sequence number of the packet that is in the nack list
54 // as well as the meta data about when it should be nacked and how many times
55 // we have tried to nack this packet.
56 struct NackInfo {
57 NackInfo();
58 NackInfo(uint16_t seq_num,
59 uint16_t send_at_seq_num,
60 int64_t created_at_time);
61
62 uint16_t seq_num;
63 uint16_t send_at_seq_num;
64 int64_t created_at_time;
65 int64_t sent_at_time;
66 int retries;
67 };
68
69 struct BackoffSettings {
70 BackoffSettings(TimeDelta min_retry, TimeDelta max_rtt, double base);
71 static absl::optional<BackoffSettings> ParseFromFieldTrials();
72
73 // Min time between nacks.
74 const TimeDelta min_retry_interval;
75 // Upper bound on link-delay considered for exponential backoff.
76 const TimeDelta max_rtt;
77 // Base for the exponential backoff.
78 const double base;
79 };
80
81 void AddPacketsToNack(uint16_t seq_num_start, uint16_t seq_num_end)
82 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
83
84 // Removes packets from the nack list until the next keyframe. Returns true
85 // if packets were removed.
86 bool RemovePacketsUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
87 std::vector<uint16_t> GetNackBatch(NackFilterOptions options)
88 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
89
90 // Update the reordering distribution.
91 void UpdateReorderingStatistics(uint16_t seq_num)
92 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
93
94 // Returns how many packets we have to wait in order to receive the packet
95 // with probability |probabilty| or higher.
96 int WaitNumberOfPackets(float probability) const
97 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
98
99 rtc::CriticalSection crit_;
100 Clock* const clock_;
101 NackSender* const nack_sender_;
102 KeyFrameRequestSender* const keyframe_request_sender_;
103
104 // TODO(philipel): Some of the variables below are consistently used on a
105 // known thread (e.g. see |initialized_|). Those probably do not need
106 // synchronized access.
107 std::map<uint16_t, NackInfo, DescendingSeqNumComp<uint16_t>> nack_list_
108 RTC_GUARDED_BY(crit_);
109 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> keyframe_list_
110 RTC_GUARDED_BY(crit_);
111 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> recovered_list_
112 RTC_GUARDED_BY(crit_);
113 video_coding::Histogram reordering_histogram_ RTC_GUARDED_BY(crit_);
114 bool initialized_ RTC_GUARDED_BY(crit_);
115 int64_t rtt_ms_ RTC_GUARDED_BY(crit_);
116 uint16_t newest_seq_num_ RTC_GUARDED_BY(crit_);
117
118 // Only touched on the process thread.
119 int64_t next_process_time_ms_;
120
121 // Adds a delay before send nack on packet received.
122 const int64_t send_nack_delay_ms_;
123
124 const absl::optional<BackoffSettings> backoff_settings_;
125};
126
127} // namespace webrtc
128
129#endif // MODULES_VIDEO_CODING_NACK_MODULE2_H_