blob: fba55b1b161f3b4d015c084f158f22948828e29f [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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_NACK_MODULE_H_
12#define MODULES_VIDEO_CODING_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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/include/module.h"
Ilya Nikolaevskiy8643b782018-06-07 16:15:40 +020021#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/histogram.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/critical_section.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020024#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/thread_annotations.h"
26#include "system_wrappers/include/clock.h"
philipel5ab4c6d2016-03-08 03:36:15 -080027
28namespace webrtc {
29
30class NackModule : public Module {
31 public:
32 NackModule(Clock* clock,
33 NackSender* nack_sender,
34 KeyFrameRequestSender* keyframe_request_sender);
35
Niels Möllerbc010472018-03-23 13:22:29 +010036 int OnReceivedPacket(uint16_t seq_num, bool is_keyframe);
Ying Wangb32bb952018-10-31 10:12:27 +010037 int OnReceivedPacket(uint16_t seq_num, bool is_keyframe, bool is_recovered);
38
philipel5ab4c6d2016-03-08 03:36:15 -080039 void ClearUpTo(uint16_t seq_num);
40 void UpdateRtt(int64_t rtt_ms);
philipel83f831a2016-03-12 03:30:23 -080041 void Clear();
philipel5ab4c6d2016-03-08 03:36:15 -080042
43 // Module implementation
44 int64_t TimeUntilNextProcess() override;
45 void Process() override;
46
47 private:
48 // Which fields to consider when deciding which packet to nack in
49 // GetNackBatch.
50 enum NackFilterOptions { kSeqNumOnly, kTimeOnly, kSeqNumAndTime };
51
52 // This class holds the sequence number of the packet that is in the nack list
53 // as well as the meta data about when it should be nacked and how many times
54 // we have tried to nack this packet.
55 struct NackInfo {
56 NackInfo();
Ying Wang0367d1a2018-11-02 14:51:15 +010057 NackInfo(uint16_t seq_num,
58 uint16_t send_at_seq_num,
59 int64_t created_at_time);
philipel5ab4c6d2016-03-08 03:36:15 -080060
61 uint16_t seq_num;
62 uint16_t send_at_seq_num;
Ying Wang0367d1a2018-11-02 14:51:15 +010063 int64_t created_at_time;
philipel5ab4c6d2016-03-08 03:36:15 -080064 int64_t sent_at_time;
65 int retries;
66 };
philipel5ab4c6d2016-03-08 03:36:15 -080067 void AddPacketsToNack(uint16_t seq_num_start, uint16_t seq_num_end)
danilchap56359be2017-09-07 07:53:45 -070068 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080069
70 // Removes packets from the nack list until the next keyframe. Returns true
71 // if packets were removed.
danilchap56359be2017-09-07 07:53:45 -070072 bool RemovePacketsUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080073 std::vector<uint16_t> GetNackBatch(NackFilterOptions options)
danilchap56359be2017-09-07 07:53:45 -070074 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080075
76 // Update the reordering distribution.
77 void UpdateReorderingStatistics(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -070078 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080079
80 // Returns how many packets we have to wait in order to receive the packet
81 // with probability |probabilty| or higher.
82 int WaitNumberOfPackets(float probability) const
danilchap56359be2017-09-07 07:53:45 -070083 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080084
85 rtc::CriticalSection crit_;
86 Clock* const clock_;
87 NackSender* const nack_sender_;
88 KeyFrameRequestSender* const keyframe_request_sender_;
89
tommif284b7f2017-02-27 01:59:36 -080090 // TODO(philipel): Some of the variables below are consistently used on a
91 // known thread (e.g. see |initialized_|). Those probably do not need
92 // synchronized access.
philipel1a830c22016-05-13 11:12:00 +020093 std::map<uint16_t, NackInfo, DescendingSeqNumComp<uint16_t>> nack_list_
danilchap56359be2017-09-07 07:53:45 -070094 RTC_GUARDED_BY(crit_);
philipel1a830c22016-05-13 11:12:00 +020095 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> keyframe_list_
danilchap56359be2017-09-07 07:53:45 -070096 RTC_GUARDED_BY(crit_);
Ying Wangb32bb952018-10-31 10:12:27 +010097 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> recovered_list_
98 RTC_GUARDED_BY(crit_);
danilchap56359be2017-09-07 07:53:45 -070099 video_coding::Histogram reordering_histogram_ RTC_GUARDED_BY(crit_);
100 bool initialized_ RTC_GUARDED_BY(crit_);
101 int64_t rtt_ms_ RTC_GUARDED_BY(crit_);
102 uint16_t newest_seq_num_ RTC_GUARDED_BY(crit_);
tommif284b7f2017-02-27 01:59:36 -0800103
104 // Only touched on the process thread.
105 int64_t next_process_time_ms_;
Ying Wang0367d1a2018-11-02 14:51:15 +0100106
107 // Adds a delay before send nack on packet received.
108 const int64_t send_nack_delay_ms_;
philipel5ab4c6d2016-03-08 03:36:15 -0800109};
110
111} // namespace webrtc
112
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200113#endif // MODULES_VIDEO_CODING_NACK_MODULE_H_