blob: d704a05c11d29f1bde9967283ad7f83a36f65541 [file] [log] [blame]
philipel5ab4c6d2016-03-08 03:36:15 -08001/*
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
Mirko Bonadei9ca73652020-05-30 17:31:46 +020011#ifndef MODULES_VIDEO_CODING_DEPRECATED_NACK_MODULE_H_
12#define MODULES_VIDEO_CODING_DEPRECATED_NACK_MODULE_H_
philipel5ab4c6d2016-03-08 03:36:15 -080013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
philipel5ab4c6d2016-03-08 03:36:15 -080016#include <map>
philipel5ab4c6d2016-03-08 03:36:15 -080017#include <set>
Yves Gerey665174f2018-06-19 15:03:05 +020018#include <vector>
philipel5ab4c6d2016-03-08 03:36:15 -080019
Erik Språng3eae7e42019-10-25 09:24:45 +020020#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/include/module.h"
Ilya Nikolaevskiy8643b782018-06-07 16:15:40 +020022#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/video_coding/histogram.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/critical_section.h"
Mirko Bonadei9ca73652020-05-30 17:31:46 +020025#include "rtc_base/deprecation.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020026#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/thread_annotations.h"
28#include "system_wrappers/include/clock.h"
philipel5ab4c6d2016-03-08 03:36:15 -080029
30namespace webrtc {
31
Mirko Bonadei9ca73652020-05-30 17:31:46 +020032class DEPRECATED_NackModule : public Module {
philipel5ab4c6d2016-03-08 03:36:15 -080033 public:
Mirko Bonadei9ca73652020-05-30 17:31:46 +020034 DEPRECATED_NackModule(Clock* clock,
35 NackSender* nack_sender,
36 KeyFrameRequestSender* keyframe_request_sender);
philipel5ab4c6d2016-03-08 03:36:15 -080037
Niels Möllerbc010472018-03-23 13:22:29 +010038 int OnReceivedPacket(uint16_t seq_num, bool is_keyframe);
Ying Wangb32bb952018-10-31 10:12:27 +010039 int OnReceivedPacket(uint16_t seq_num, bool is_keyframe, bool is_recovered);
40
philipel5ab4c6d2016-03-08 03:36:15 -080041 void ClearUpTo(uint16_t seq_num);
42 void UpdateRtt(int64_t rtt_ms);
philipel83f831a2016-03-12 03:30:23 -080043 void Clear();
philipel5ab4c6d2016-03-08 03:36:15 -080044
45 // Module implementation
46 int64_t TimeUntilNextProcess() override;
47 void Process() override;
48
49 private:
50 // Which fields to consider when deciding which packet to nack in
51 // GetNackBatch.
52 enum NackFilterOptions { kSeqNumOnly, kTimeOnly, kSeqNumAndTime };
53
54 // This class holds the sequence number of the packet that is in the nack list
55 // as well as the meta data about when it should be nacked and how many times
56 // we have tried to nack this packet.
57 struct NackInfo {
58 NackInfo();
Ying Wang0367d1a2018-11-02 14:51:15 +010059 NackInfo(uint16_t seq_num,
60 uint16_t send_at_seq_num,
61 int64_t created_at_time);
philipel5ab4c6d2016-03-08 03:36:15 -080062
63 uint16_t seq_num;
64 uint16_t send_at_seq_num;
Ying Wang0367d1a2018-11-02 14:51:15 +010065 int64_t created_at_time;
philipel5ab4c6d2016-03-08 03:36:15 -080066 int64_t sent_at_time;
67 int retries;
68 };
Erik Språng3eae7e42019-10-25 09:24:45 +020069
70 struct BackoffSettings {
71 BackoffSettings(TimeDelta min_retry, TimeDelta max_rtt, double base);
72 static absl::optional<BackoffSettings> ParseFromFieldTrials();
73
74 // Min time between nacks.
75 const TimeDelta min_retry_interval;
76 // Upper bound on link-delay considered for exponential backoff.
77 const TimeDelta max_rtt;
78 // Base for the exponential backoff.
79 const double base;
80 };
81
philipel5ab4c6d2016-03-08 03:36:15 -080082 void AddPacketsToNack(uint16_t seq_num_start, uint16_t seq_num_end)
danilchap56359be2017-09-07 07:53:45 -070083 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080084
85 // Removes packets from the nack list until the next keyframe. Returns true
86 // if packets were removed.
danilchap56359be2017-09-07 07:53:45 -070087 bool RemovePacketsUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080088 std::vector<uint16_t> GetNackBatch(NackFilterOptions options)
danilchap56359be2017-09-07 07:53:45 -070089 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080090
91 // Update the reordering distribution.
92 void UpdateReorderingStatistics(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -070093 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080094
95 // Returns how many packets we have to wait in order to receive the packet
96 // with probability |probabilty| or higher.
97 int WaitNumberOfPackets(float probability) const
danilchap56359be2017-09-07 07:53:45 -070098 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080099
100 rtc::CriticalSection crit_;
101 Clock* const clock_;
102 NackSender* const nack_sender_;
103 KeyFrameRequestSender* const keyframe_request_sender_;
104
tommif284b7f2017-02-27 01:59:36 -0800105 // TODO(philipel): Some of the variables below are consistently used on a
106 // known thread (e.g. see |initialized_|). Those probably do not need
107 // synchronized access.
philipel1a830c22016-05-13 11:12:00 +0200108 std::map<uint16_t, NackInfo, DescendingSeqNumComp<uint16_t>> nack_list_
danilchap56359be2017-09-07 07:53:45 -0700109 RTC_GUARDED_BY(crit_);
philipel1a830c22016-05-13 11:12:00 +0200110 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> keyframe_list_
danilchap56359be2017-09-07 07:53:45 -0700111 RTC_GUARDED_BY(crit_);
Ying Wangb32bb952018-10-31 10:12:27 +0100112 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> recovered_list_
113 RTC_GUARDED_BY(crit_);
danilchap56359be2017-09-07 07:53:45 -0700114 video_coding::Histogram reordering_histogram_ RTC_GUARDED_BY(crit_);
115 bool initialized_ RTC_GUARDED_BY(crit_);
116 int64_t rtt_ms_ RTC_GUARDED_BY(crit_);
117 uint16_t newest_seq_num_ RTC_GUARDED_BY(crit_);
tommif284b7f2017-02-27 01:59:36 -0800118
119 // Only touched on the process thread.
120 int64_t next_process_time_ms_;
Ying Wang0367d1a2018-11-02 14:51:15 +0100121
122 // Adds a delay before send nack on packet received.
123 const int64_t send_nack_delay_ms_;
Erik Språng3eae7e42019-10-25 09:24:45 +0200124
125 const absl::optional<BackoffSettings> backoff_settings_;
philipel5ab4c6d2016-03-08 03:36:15 -0800126};
127
Mirko Bonadei9ca73652020-05-30 17:31:46 +0200128using NackModule = RTC_DEPRECATED DEPRECATED_NackModule;
129
philipel5ab4c6d2016-03-08 03:36:15 -0800130} // namespace webrtc
131
Mirko Bonadei9ca73652020-05-30 17:31:46 +0200132#endif // MODULES_VIDEO_CODING_DEPRECATED_NACK_MODULE_H_