blob: c18c99fc3c4dfd19436afd2048e0e54c22a6ebd6 [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
14#include <map>
15#include <vector>
16#include <set>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/include/module.h"
19#include "modules/video_coding/histogram.h"
20#include "modules/video_coding/include/video_coding_defines.h"
21#include "modules/video_coding/packet.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/criticalsection.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020023#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread_annotations.h"
25#include "system_wrappers/include/clock.h"
philipel5ab4c6d2016-03-08 03:36:15 -080026
27namespace webrtc {
28
29class NackModule : public Module {
30 public:
31 NackModule(Clock* clock,
32 NackSender* nack_sender,
33 KeyFrameRequestSender* keyframe_request_sender);
34
Niels Möllerbc010472018-03-23 13:22:29 +010035 int OnReceivedPacket(uint16_t seq_num, bool is_keyframe);
philipel1a830c22016-05-13 11:12:00 +020036 int OnReceivedPacket(const VCMPacket& packet);
philipel5ab4c6d2016-03-08 03:36:15 -080037 void ClearUpTo(uint16_t seq_num);
38 void UpdateRtt(int64_t rtt_ms);
philipel83f831a2016-03-12 03:30:23 -080039 void Clear();
philipel5ab4c6d2016-03-08 03:36:15 -080040
41 // Module implementation
42 int64_t TimeUntilNextProcess() override;
43 void Process() override;
44
45 private:
46 // Which fields to consider when deciding which packet to nack in
47 // GetNackBatch.
48 enum NackFilterOptions { kSeqNumOnly, kTimeOnly, kSeqNumAndTime };
49
50 // This class holds the sequence number of the packet that is in the nack list
51 // as well as the meta data about when it should be nacked and how many times
52 // we have tried to nack this packet.
53 struct NackInfo {
54 NackInfo();
55 NackInfo(uint16_t seq_num, uint16_t send_at_seq_num);
56
57 uint16_t seq_num;
58 uint16_t send_at_seq_num;
59 int64_t sent_at_time;
60 int retries;
61 };
philipel5ab4c6d2016-03-08 03:36:15 -080062 void AddPacketsToNack(uint16_t seq_num_start, uint16_t seq_num_end)
danilchap56359be2017-09-07 07:53:45 -070063 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080064
65 // Removes packets from the nack list until the next keyframe. Returns true
66 // if packets were removed.
danilchap56359be2017-09-07 07:53:45 -070067 bool RemovePacketsUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080068 std::vector<uint16_t> GetNackBatch(NackFilterOptions options)
danilchap56359be2017-09-07 07:53:45 -070069 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080070
71 // Update the reordering distribution.
72 void UpdateReorderingStatistics(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -070073 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080074
75 // Returns how many packets we have to wait in order to receive the packet
76 // with probability |probabilty| or higher.
77 int WaitNumberOfPackets(float probability) const
danilchap56359be2017-09-07 07:53:45 -070078 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080079
80 rtc::CriticalSection crit_;
81 Clock* const clock_;
82 NackSender* const nack_sender_;
83 KeyFrameRequestSender* const keyframe_request_sender_;
84
tommif284b7f2017-02-27 01:59:36 -080085 // TODO(philipel): Some of the variables below are consistently used on a
86 // known thread (e.g. see |initialized_|). Those probably do not need
87 // synchronized access.
philipel1a830c22016-05-13 11:12:00 +020088 std::map<uint16_t, NackInfo, DescendingSeqNumComp<uint16_t>> nack_list_
danilchap56359be2017-09-07 07:53:45 -070089 RTC_GUARDED_BY(crit_);
philipel1a830c22016-05-13 11:12:00 +020090 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> keyframe_list_
danilchap56359be2017-09-07 07:53:45 -070091 RTC_GUARDED_BY(crit_);
92 video_coding::Histogram reordering_histogram_ RTC_GUARDED_BY(crit_);
93 bool initialized_ RTC_GUARDED_BY(crit_);
94 int64_t rtt_ms_ RTC_GUARDED_BY(crit_);
95 uint16_t newest_seq_num_ RTC_GUARDED_BY(crit_);
tommif284b7f2017-02-27 01:59:36 -080096
97 // Only touched on the process thread.
98 int64_t next_process_time_ms_;
philipel5ab4c6d2016-03-08 03:36:15 -080099};
100
101} // namespace webrtc
102
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200103#endif // MODULES_VIDEO_CODING_NACK_MODULE_H_