Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 | #include "modules/video_coding/loss_notification_controller.h" |
| 12 | |
| 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/logging.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | namespace { |
| 18 | // Keep a container's size no higher than |max_allowed_size|, by paring its size |
| 19 | // down to |target_size| whenever it has more than |max_allowed_size| elements. |
| 20 | template <typename Container> |
| 21 | void PareDown(Container* container, |
| 22 | size_t max_allowed_size, |
| 23 | size_t target_size) { |
| 24 | if (container->size() > max_allowed_size) { |
| 25 | const size_t entries_to_delete = container->size() - target_size; |
| 26 | auto erase_to = container->begin(); |
| 27 | std::advance(erase_to, entries_to_delete); |
| 28 | container->erase(container->begin(), erase_to); |
| 29 | RTC_DCHECK_EQ(container->size(), target_size); |
| 30 | } |
| 31 | } |
| 32 | } // namespace |
| 33 | |
| 34 | LossNotificationController::LossNotificationController( |
| 35 | KeyFrameRequestSender* key_frame_request_sender, |
| 36 | LossNotificationSender* loss_notification_sender) |
| 37 | : key_frame_request_sender_(key_frame_request_sender), |
| 38 | loss_notification_sender_(loss_notification_sender), |
| 39 | current_frame_potentially_decodable_(true) { |
| 40 | RTC_DCHECK(key_frame_request_sender_); |
| 41 | RTC_DCHECK(loss_notification_sender_); |
| 42 | } |
| 43 | |
| 44 | LossNotificationController::~LossNotificationController() = default; |
| 45 | |
| 46 | void LossNotificationController::OnReceivedPacket(const VCMPacket& packet) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame^] | 47 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 48 | |
| 49 | if (!packet.generic_descriptor) { |
| 50 | RTC_LOG(LS_WARNING) << "Generic frame descriptor missing. Buggy remote? " |
| 51 | "Misconfigured local?"; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Ignore repeated or reordered packets. |
| 56 | // TODO(bugs.webrtc.org/10336): Handle packet reordering. |
| 57 | if (last_received_seq_num_ && |
| 58 | !AheadOf(packet.seqNum, *last_received_seq_num_)) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | DiscardOldInformation(); // Prevent memory overconsumption. |
| 63 | |
| 64 | const bool seq_num_gap = |
| 65 | last_received_seq_num_ && |
| 66 | packet.seqNum != static_cast<uint16_t>(*last_received_seq_num_ + 1u); |
| 67 | |
| 68 | last_received_seq_num_ = packet.seqNum; |
| 69 | |
| 70 | if (packet.generic_descriptor->FirstPacketInSubFrame()) { |
| 71 | const uint16_t frame_id = packet.generic_descriptor->FrameId(); |
Philip Eliasson | 1f850a6 | 2019-03-19 12:15:00 +0000 | [diff] [blame] | 72 | const int64_t unwrapped_frame_id = frame_id_unwrapper_.Unwrap(frame_id); |
Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 73 | |
| 74 | // Ignore repeated or reordered frames. |
| 75 | // TODO(TODO(bugs.webrtc.org/10336): Handle frame reordering. |
| 76 | if (last_received_unwrapped_frame_id_ && |
| 77 | unwrapped_frame_id <= *last_received_unwrapped_frame_id_) { |
| 78 | RTC_LOG(LS_WARNING) << "Repeated or reordered frame ID (" << frame_id |
| 79 | << ")."; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | last_received_unwrapped_frame_id_ = unwrapped_frame_id; |
| 84 | |
| 85 | const bool intra_frame = |
| 86 | packet.generic_descriptor->FrameDependenciesDiffs().empty(); |
| 87 | // Generic Frame Descriptor does not current allow us to distinguish |
| 88 | // whether an intra frame is a key frame. |
| 89 | // We therefore assume all intra frames are key frames. |
| 90 | const bool key_frame = intra_frame; |
| 91 | if (key_frame) { |
| 92 | // Subsequent frames may not rely on frames before the key frame. |
| 93 | decodable_unwrapped_frame_ids_.clear(); |
| 94 | current_frame_potentially_decodable_ = true; |
| 95 | } else { |
| 96 | const bool all_dependencies_decodable = AllDependenciesDecodable( |
| 97 | unwrapped_frame_id, |
| 98 | packet.generic_descriptor->FrameDependenciesDiffs()); |
| 99 | current_frame_potentially_decodable_ = all_dependencies_decodable; |
| 100 | if (seq_num_gap || !current_frame_potentially_decodable_) { |
| 101 | HandleLoss(packet.seqNum, current_frame_potentially_decodable_); |
| 102 | } |
| 103 | } |
| 104 | } else if (seq_num_gap || !current_frame_potentially_decodable_) { |
| 105 | current_frame_potentially_decodable_ = false; |
| 106 | // We allow sending multiple loss notifications for a single frame |
| 107 | // even if only one of its packets is lost. We do this because the bigger |
| 108 | // the frame, the more likely it is to be non-discardable, and therefore |
| 109 | // the more robust we wish to be to loss of the feedback messages. |
| 110 | HandleLoss(packet.seqNum, false); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void LossNotificationController::OnAssembledFrame( |
| 115 | uint16_t first_seq_num, |
| 116 | uint16_t frame_id, |
| 117 | bool discardable, |
| 118 | rtc::ArrayView<const uint16_t> frame_dependency_diffs) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame^] | 119 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 120 | |
| 121 | DiscardOldInformation(); // Prevent memory overconsumption. |
| 122 | |
| 123 | if (discardable) { |
| 124 | return; |
| 125 | } |
| 126 | |
Philip Eliasson | 1f850a6 | 2019-03-19 12:15:00 +0000 | [diff] [blame] | 127 | const int64_t unwrapped_frame_id = frame_id_unwrapper_.Unwrap(frame_id); |
Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 128 | if (!AllDependenciesDecodable(unwrapped_frame_id, frame_dependency_diffs)) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | last_decodable_non_discardable_.emplace(first_seq_num); |
| 133 | const auto it = decodable_unwrapped_frame_ids_.insert(unwrapped_frame_id); |
| 134 | RTC_DCHECK(it.second); |
| 135 | } |
| 136 | |
| 137 | void LossNotificationController::DiscardOldInformation() { |
| 138 | constexpr size_t kExpectedKeyFrameIntervalFrames = 3000; |
| 139 | constexpr size_t kMaxSize = 2 * kExpectedKeyFrameIntervalFrames; |
| 140 | constexpr size_t kTargetSize = kExpectedKeyFrameIntervalFrames; |
| 141 | PareDown(&decodable_unwrapped_frame_ids_, kMaxSize, kTargetSize); |
| 142 | } |
| 143 | |
| 144 | bool LossNotificationController::AllDependenciesDecodable( |
Philip Eliasson | 1f850a6 | 2019-03-19 12:15:00 +0000 | [diff] [blame] | 145 | int64_t unwrapped_frame_id, |
Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 146 | rtc::ArrayView<const uint16_t> frame_dependency_diffs) const { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame^] | 147 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 148 | |
| 149 | // Due to packet reordering, frame buffering and asynchronous decoders, it is |
| 150 | // infeasible to make reliable conclusions on the decodability of a frame |
| 151 | // immediately when it arrives. We use the following assumptions: |
| 152 | // * Intra frames are decodable. |
| 153 | // * Inter frames are decodable if all of their references were decodable. |
| 154 | // One possibility that is ignored, is that the packet may be corrupt. |
| 155 | |
| 156 | for (uint16_t frame_dependency_diff : frame_dependency_diffs) { |
Philip Eliasson | 1f850a6 | 2019-03-19 12:15:00 +0000 | [diff] [blame] | 157 | const int64_t unwrapped_ref_frame_id = |
Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 158 | unwrapped_frame_id - frame_dependency_diff; |
| 159 | |
| 160 | const auto ref_frame_it = |
| 161 | decodable_unwrapped_frame_ids_.find(unwrapped_ref_frame_id); |
| 162 | if (ref_frame_it == decodable_unwrapped_frame_ids_.end()) { |
| 163 | // Reference frame not decodable. |
| 164 | return false; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | void LossNotificationController::HandleLoss(uint16_t last_received_seq_num, |
| 172 | bool decodability_flag) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame^] | 173 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Elad Alon | 10874b2 | 2019-02-21 16:25:40 +0100 | [diff] [blame] | 174 | |
| 175 | if (last_decodable_non_discardable_) { |
| 176 | RTC_DCHECK(AheadOf(last_received_seq_num, |
| 177 | last_decodable_non_discardable_->first_seq_num)); |
| 178 | loss_notification_sender_->SendLossNotification( |
| 179 | last_decodable_non_discardable_->first_seq_num, last_received_seq_num, |
| 180 | decodability_flag); |
| 181 | } else { |
| 182 | key_frame_request_sender_->RequestKeyFrame(); |
| 183 | } |
| 184 | } |
| 185 | } // namespace webrtc |