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