Per | 69b332d | 2016-06-02 15:45:42 +0200 | [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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <stdlib.h> |
Ying Wang | 68b2df7 | 2018-10-22 16:50:43 +0200 | [diff] [blame] | 12 | #include <algorithm> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string> |
Ying Wang | 68b2df7 | 2018-10-22 16:50:43 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include "modules/include/module_fec_types.h" |
Ying Wang | 68b2df7 | 2018-10-22 16:50:43 +0200 | [diff] [blame] | 16 | #include "modules/video_coding/fec_controller_default.h" // NOLINT |
| 17 | #include "rtc_base/logging.h" |
| 18 | #include "system_wrappers/include/field_trial.h" |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 21 | using rtc::CritScope; |
Ying Wang | 68b2df7 | 2018-10-22 16:50:43 +0200 | [diff] [blame] | 22 | |
| 23 | const float kProtectionOverheadRateThreshold = 0.5; |
| 24 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 25 | FecControllerDefault::FecControllerDefault( |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 26 | Clock* clock, |
| 27 | VCMProtectionCallback* protection_callback) |
| 28 | : clock_(clock), |
| 29 | protection_callback_(protection_callback), |
| 30 | loss_prot_logic_(new media_optimization::VCMLossProtectionLogic( |
| 31 | clock_->TimeInMilliseconds())), |
Ying Wang | 68b2df7 | 2018-10-22 16:50:43 +0200 | [diff] [blame] | 32 | max_payload_size_(1460), |
| 33 | overhead_threshold_(GetProtectionOverheadRateThreshold()) {} |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 34 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 35 | FecControllerDefault::FecControllerDefault(Clock* clock) |
| 36 | : clock_(clock), |
| 37 | loss_prot_logic_(new media_optimization::VCMLossProtectionLogic( |
| 38 | clock_->TimeInMilliseconds())), |
Ying Wang | 68b2df7 | 2018-10-22 16:50:43 +0200 | [diff] [blame] | 39 | max_payload_size_(1460), |
| 40 | overhead_threshold_(GetProtectionOverheadRateThreshold()) {} |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 41 | |
| 42 | FecControllerDefault::~FecControllerDefault(void) { |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 43 | loss_prot_logic_->Release(); |
| 44 | } |
| 45 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 46 | void FecControllerDefault::SetProtectionCallback( |
| 47 | VCMProtectionCallback* protection_callback) { |
| 48 | protection_callback_ = protection_callback; |
| 49 | } |
| 50 | |
| 51 | void FecControllerDefault::SetEncodingData(size_t width, |
| 52 | size_t height, |
| 53 | size_t num_temporal_layers, |
| 54 | size_t max_payload_size) { |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 55 | CritScope lock(&crit_sect_); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 56 | loss_prot_logic_->UpdateFrameSize(width, height); |
| 57 | loss_prot_logic_->UpdateNumLayers(num_temporal_layers); |
| 58 | max_payload_size_ = max_payload_size; |
| 59 | } |
| 60 | |
Ying Wang | 68b2df7 | 2018-10-22 16:50:43 +0200 | [diff] [blame] | 61 | float FecControllerDefault::GetProtectionOverheadRateThreshold() { |
| 62 | float overhead_threshold = |
| 63 | strtof(webrtc::field_trial::FindFullName( |
| 64 | "WebRTC-ProtectionOverheadRateThreshold") |
| 65 | .c_str(), |
| 66 | nullptr); |
| 67 | if (overhead_threshold > 0 && overhead_threshold <= 1) { |
| 68 | RTC_LOG(LS_INFO) << "ProtectionOverheadRateThreshold is set to " |
| 69 | << overhead_threshold; |
| 70 | return overhead_threshold; |
| 71 | } else if (overhead_threshold < 0 || overhead_threshold > 1) { |
| 72 | RTC_LOG(WARNING) << "ProtectionOverheadRateThreshold field trial is set to " |
| 73 | "an invalid value, expecting a value between (0, 1]."; |
| 74 | } |
| 75 | // WebRTC-ProtectionOverheadRateThreshold field trial string is not found, use |
| 76 | // the default value. |
| 77 | return kProtectionOverheadRateThreshold; |
| 78 | } |
| 79 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 80 | uint32_t FecControllerDefault::UpdateFecRates( |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 81 | uint32_t estimated_bitrate_bps, |
| 82 | int actual_framerate_fps, |
| 83 | uint8_t fraction_lost, |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 84 | std::vector<bool> loss_mask_vector, |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 85 | int64_t round_trip_time_ms) { |
| 86 | float target_bitrate_kbps = |
| 87 | static_cast<float>(estimated_bitrate_bps) / 1000.0f; |
| 88 | // Sanity check. |
| 89 | if (actual_framerate_fps < 1.0) { |
| 90 | actual_framerate_fps = 1.0; |
| 91 | } |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 92 | FecProtectionParams delta_fec_params; |
| 93 | FecProtectionParams key_fec_params; |
| 94 | { |
| 95 | CritScope lock(&crit_sect_); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 96 | loss_prot_logic_->UpdateBitRate(target_bitrate_kbps); |
| 97 | loss_prot_logic_->UpdateRtt(round_trip_time_ms); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 98 | // Update frame rate for the loss protection logic class: frame rate should |
| 99 | // be the actual/sent rate. |
| 100 | loss_prot_logic_->UpdateFrameRate(actual_framerate_fps); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 101 | // Returns the filtered packet loss, used for the protection setting. |
| 102 | // The filtered loss may be the received loss (no filter), or some |
| 103 | // filtered value (average or max window filter). |
| 104 | // Use max window filter for now. |
| 105 | media_optimization::FilterPacketLossMode filter_mode = |
| 106 | media_optimization::kMaxFilter; |
| 107 | uint8_t packet_loss_enc = loss_prot_logic_->FilteredLoss( |
| 108 | clock_->TimeInMilliseconds(), filter_mode, fraction_lost); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 109 | // For now use the filtered loss for computing the robustness settings. |
| 110 | loss_prot_logic_->UpdateFilteredLossPr(packet_loss_enc); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 111 | if (loss_prot_logic_->SelectedType() == media_optimization::kNone) { |
| 112 | return estimated_bitrate_bps; |
| 113 | } |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 114 | // Update method will compute the robustness settings for the given |
| 115 | // protection method and the overhead cost |
| 116 | // the protection method is set by the user via SetVideoProtection. |
| 117 | loss_prot_logic_->UpdateMethod(); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 118 | // Get the bit cost of protection method, based on the amount of |
| 119 | // overhead data actually transmitted (including headers) the last |
| 120 | // second. |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 121 | // Get the FEC code rate for Key frames (set to 0 when NA). |
| 122 | key_fec_params.fec_rate = |
| 123 | loss_prot_logic_->SelectedMethod()->RequiredProtectionFactorK(); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 124 | // Get the FEC code rate for Delta frames (set to 0 when NA). |
| 125 | delta_fec_params.fec_rate = |
| 126 | loss_prot_logic_->SelectedMethod()->RequiredProtectionFactorD(); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 127 | // The RTP module currently requires the same |max_fec_frames| for both |
| 128 | // key and delta frames. |
| 129 | delta_fec_params.max_fec_frames = |
| 130 | loss_prot_logic_->SelectedMethod()->MaxFramesFec(); |
| 131 | key_fec_params.max_fec_frames = |
| 132 | loss_prot_logic_->SelectedMethod()->MaxFramesFec(); |
| 133 | } |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 134 | // Set the FEC packet mask type. |kFecMaskBursty| is more effective for |
| 135 | // consecutive losses and little/no packet re-ordering. As we currently |
| 136 | // do not have feedback data on the degree of correlated losses and packet |
| 137 | // re-ordering, we keep default setting to |kFecMaskRandom| for now. |
| 138 | delta_fec_params.fec_mask_type = kFecMaskRandom; |
| 139 | key_fec_params.fec_mask_type = kFecMaskRandom; |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 140 | // Update protection callback with protection settings. |
| 141 | uint32_t sent_video_rate_bps = 0; |
| 142 | uint32_t sent_nack_rate_bps = 0; |
| 143 | uint32_t sent_fec_rate_bps = 0; |
| 144 | // Rate cost of the protection methods. |
| 145 | float protection_overhead_rate = 0.0f; |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 146 | // TODO(Marco): Pass FEC protection values per layer. |
| 147 | protection_callback_->ProtectionRequest( |
| 148 | &delta_fec_params, &key_fec_params, &sent_video_rate_bps, |
| 149 | &sent_nack_rate_bps, &sent_fec_rate_bps); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 150 | uint32_t sent_total_rate_bps = |
| 151 | sent_video_rate_bps + sent_nack_rate_bps + sent_fec_rate_bps; |
| 152 | // Estimate the overhead costs of the next second as staying the same |
| 153 | // wrt the source bitrate. |
| 154 | if (sent_total_rate_bps > 0) { |
| 155 | protection_overhead_rate = |
| 156 | static_cast<float>(sent_nack_rate_bps + sent_fec_rate_bps) / |
| 157 | sent_total_rate_bps; |
| 158 | } |
Ying Wang | 68b2df7 | 2018-10-22 16:50:43 +0200 | [diff] [blame] | 159 | // Cap the overhead estimate to a threshold, default is 50%. |
| 160 | protection_overhead_rate = |
| 161 | std::min(protection_overhead_rate, overhead_threshold_); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 162 | // Source coding rate: total rate - protection overhead. |
| 163 | return estimated_bitrate_bps * (1.0 - protection_overhead_rate); |
| 164 | } |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 165 | void FecControllerDefault::SetProtectionMethod(bool enable_fec, |
| 166 | bool enable_nack) { |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 167 | media_optimization::VCMProtectionMethodEnum method(media_optimization::kNone); |
| 168 | if (enable_fec && enable_nack) { |
| 169 | method = media_optimization::kNackFec; |
| 170 | } else if (enable_nack) { |
| 171 | method = media_optimization::kNack; |
| 172 | } else if (enable_fec) { |
| 173 | method = media_optimization::kFec; |
| 174 | } |
| 175 | CritScope lock(&crit_sect_); |
| 176 | loss_prot_logic_->SetMethod(method); |
| 177 | } |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 178 | void FecControllerDefault::UpdateWithEncodedData( |
Ying Wang | 0dd1b0a | 2018-02-20 12:50:27 +0100 | [diff] [blame] | 179 | const size_t encoded_image_length, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 180 | const VideoFrameType encoded_image_frametype) { |
Ying Wang | 0dd1b0a | 2018-02-20 12:50:27 +0100 | [diff] [blame] | 181 | const size_t encoded_length = encoded_image_length; |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 182 | CritScope lock(&crit_sect_); |
| 183 | if (encoded_length > 0) { |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame^] | 184 | const bool delta_frame = |
| 185 | encoded_image_frametype != VideoFrameType::kVideoFrameKey; |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 186 | if (max_payload_size_ > 0 && encoded_length > 0) { |
| 187 | const float min_packets_per_frame = |
| 188 | encoded_length / static_cast<float>(max_payload_size_); |
| 189 | if (delta_frame) { |
| 190 | loss_prot_logic_->UpdatePacketsPerFrame(min_packets_per_frame, |
| 191 | clock_->TimeInMilliseconds()); |
| 192 | } else { |
| 193 | loss_prot_logic_->UpdatePacketsPerFrameKey( |
| 194 | min_packets_per_frame, clock_->TimeInMilliseconds()); |
| 195 | } |
| 196 | } |
| 197 | if (!delta_frame && encoded_length > 0) { |
| 198 | loss_prot_logic_->UpdateKeyFrameSize(static_cast<float>(encoded_length)); |
| 199 | } |
| 200 | } |
| 201 | } |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 202 | |
| 203 | bool FecControllerDefault::UseLossVectorMask() { |
| 204 | return false; |
| 205 | } |
| 206 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 207 | } // namespace webrtc |