philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_NACK_MODULE_H_ |
| 12 | #define MODULES_VIDEO_CODING_NACK_MODULE_H_ |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame^] | 14 | #include <stdint.h> |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 15 | #include <map> |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 16 | #include <set> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 17 | #include <vector> |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/include/module.h" |
Ilya Nikolaevskiy | 8643b78 | 2018-06-07 16:15:40 +0200 | [diff] [blame] | 20 | #include "modules/include/module_common_types.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/video_coding/histogram.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/criticalsection.h" |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 23 | #include "rtc_base/numerics/sequence_number_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/thread_annotations.h" |
| 25 | #include "system_wrappers/include/clock.h" |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | class NackModule : public Module { |
| 30 | public: |
| 31 | NackModule(Clock* clock, |
| 32 | NackSender* nack_sender, |
| 33 | KeyFrameRequestSender* keyframe_request_sender); |
| 34 | |
Niels Möller | bc01047 | 2018-03-23 13:22:29 +0100 | [diff] [blame] | 35 | int OnReceivedPacket(uint16_t seq_num, bool is_keyframe); |
Ying Wang | b32bb95 | 2018-10-31 10:12:27 +0100 | [diff] [blame] | 36 | int OnReceivedPacket(uint16_t seq_num, bool is_keyframe, bool is_recovered); |
| 37 | |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 38 | void ClearUpTo(uint16_t seq_num); |
| 39 | void UpdateRtt(int64_t rtt_ms); |
philipel | 83f831a | 2016-03-12 03:30:23 -0800 | [diff] [blame] | 40 | void Clear(); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 41 | |
| 42 | // Module implementation |
| 43 | int64_t TimeUntilNextProcess() override; |
| 44 | void Process() override; |
| 45 | |
| 46 | private: |
| 47 | // Which fields to consider when deciding which packet to nack in |
| 48 | // GetNackBatch. |
| 49 | enum NackFilterOptions { kSeqNumOnly, kTimeOnly, kSeqNumAndTime }; |
| 50 | |
| 51 | // This class holds the sequence number of the packet that is in the nack list |
| 52 | // as well as the meta data about when it should be nacked and how many times |
| 53 | // we have tried to nack this packet. |
| 54 | struct NackInfo { |
| 55 | NackInfo(); |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 56 | NackInfo(uint16_t seq_num, |
| 57 | uint16_t send_at_seq_num, |
| 58 | int64_t created_at_time); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 59 | |
| 60 | uint16_t seq_num; |
| 61 | uint16_t send_at_seq_num; |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 62 | int64_t created_at_time; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 63 | int64_t sent_at_time; |
| 64 | int retries; |
| 65 | }; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 66 | void AddPacketsToNack(uint16_t seq_num_start, uint16_t seq_num_end) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 67 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 68 | |
| 69 | // Removes packets from the nack list until the next keyframe. Returns true |
| 70 | // if packets were removed. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 71 | bool RemovePacketsUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 72 | std::vector<uint16_t> GetNackBatch(NackFilterOptions options) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 73 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 74 | |
| 75 | // Update the reordering distribution. |
| 76 | void UpdateReorderingStatistics(uint16_t seq_num) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 77 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 78 | |
| 79 | // Returns how many packets we have to wait in order to receive the packet |
| 80 | // with probability |probabilty| or higher. |
| 81 | int WaitNumberOfPackets(float probability) const |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 82 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 83 | |
| 84 | rtc::CriticalSection crit_; |
| 85 | Clock* const clock_; |
| 86 | NackSender* const nack_sender_; |
| 87 | KeyFrameRequestSender* const keyframe_request_sender_; |
| 88 | |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 89 | // TODO(philipel): Some of the variables below are consistently used on a |
| 90 | // known thread (e.g. see |initialized_|). Those probably do not need |
| 91 | // synchronized access. |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 92 | std::map<uint16_t, NackInfo, DescendingSeqNumComp<uint16_t>> nack_list_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 93 | RTC_GUARDED_BY(crit_); |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 94 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> keyframe_list_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 95 | RTC_GUARDED_BY(crit_); |
Ying Wang | b32bb95 | 2018-10-31 10:12:27 +0100 | [diff] [blame] | 96 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> recovered_list_ |
| 97 | RTC_GUARDED_BY(crit_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 98 | video_coding::Histogram reordering_histogram_ RTC_GUARDED_BY(crit_); |
| 99 | bool initialized_ RTC_GUARDED_BY(crit_); |
| 100 | int64_t rtt_ms_ RTC_GUARDED_BY(crit_); |
| 101 | uint16_t newest_seq_num_ RTC_GUARDED_BY(crit_); |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 102 | |
| 103 | // Only touched on the process thread. |
| 104 | int64_t next_process_time_ms_; |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 105 | |
| 106 | // Adds a delay before send nack on packet received. |
| 107 | const int64_t send_nack_delay_ms_; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace webrtc |
| 111 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 112 | #endif // MODULES_VIDEO_CODING_NACK_MODULE_H_ |