niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/media_opt_util.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <float.h> |
| 14 | #include <limits.h> |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 +0000 | [diff] [blame] | 15 | #include <math.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 17 | #include <algorithm> |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 18 | #include <limits> |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 19 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 20 | #include "webrtc/modules/include/module_common_types.h" |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 22 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
brandtr | 71b1c1f | 2017-01-12 06:16:24 -0800 | [diff] [blame] | 23 | #include "webrtc/modules/video_coding/fec_rate_table.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 24 | #include "webrtc/modules/video_coding/nack_fec_tables.h" |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 25 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | namespace webrtc { |
Peter Boström | 9cb1f30 | 2015-04-01 11:39:49 +0200 | [diff] [blame] | 27 | // Max value of loss rates in off-line model |
| 28 | static const int kPacketLossMax = 129; |
| 29 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 30 | namespace media_optimization { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | |
Peter Boström | 9cb1f30 | 2015-04-01 11:39:49 +0200 | [diff] [blame] | 32 | VCMProtectionMethod::VCMProtectionMethod() |
| 33 | : _effectivePacketLoss(0), |
| 34 | _protectionFactorK(0), |
| 35 | _protectionFactorD(0), |
| 36 | _scaleProtKey(2.0f), |
| 37 | _maxPayloadSize(1460), |
Peter Boström | 9cb1f30 | 2015-04-01 11:39:49 +0200 | [diff] [blame] | 38 | _corrFecCost(1.0), |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 39 | _type(kNone) {} |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 40 | |
pbos | c043052 | 2016-05-01 17:19:05 -0700 | [diff] [blame] | 41 | VCMProtectionMethod::~VCMProtectionMethod() {} |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 42 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 43 | VCMNackFecMethod::VCMNackFecMethod(int64_t lowRttNackThresholdMs, |
| 44 | int64_t highRttNackThresholdMs) |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 45 | : VCMFecMethod(), |
| 46 | _lowRttNackMs(lowRttNackThresholdMs), |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 47 | _highRttNackMs(highRttNackThresholdMs), |
| 48 | _maxFramesFec(1) { |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 49 | assert(lowRttNackThresholdMs >= -1 && highRttNackThresholdMs >= -1); |
| 50 | assert(highRttNackThresholdMs == -1 || |
| 51 | lowRttNackThresholdMs <= highRttNackThresholdMs); |
| 52 | assert(lowRttNackThresholdMs > -1 || highRttNackThresholdMs == -1); |
| 53 | _type = kNackFec; |
mikhal@google.com | 7740888 | 2011-07-22 22:05:25 +0000 | [diff] [blame] | 54 | } |
| 55 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 56 | VCMNackFecMethod::~VCMNackFecMethod() { |
| 57 | // |
mikhal@google.com | 7740888 | 2011-07-22 22:05:25 +0000 | [diff] [blame] | 58 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 59 | bool VCMNackFecMethod::ProtectionFactor( |
| 60 | const VCMProtectionParameters* parameters) { |
| 61 | // Hybrid Nack FEC has three operational modes: |
| 62 | // 1. Low RTT (below kLowRttNackMs) - Nack only: Set FEC rate |
| 63 | // (_protectionFactorD) to zero. -1 means no FEC. |
| 64 | // 2. High RTT (above _highRttNackMs) - FEC Only: Keep FEC factors. |
| 65 | // -1 means always allow NACK. |
| 66 | // 3. Medium RTT values - Hybrid mode: We will only nack the |
| 67 | // residual following the decoding of the FEC (refer to JB logic). FEC |
| 68 | // delta protection factor will be adjusted based on the RTT. |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 69 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 70 | // Otherwise: we count on FEC; if the RTT is below a threshold, then we |
| 71 | // nack the residual, based on a decision made in the JB. |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 72 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 73 | // Compute the protection factors |
| 74 | VCMFecMethod::ProtectionFactor(parameters); |
| 75 | if (_lowRttNackMs == -1 || parameters->rtt < _lowRttNackMs) { |
| 76 | _protectionFactorD = 0; |
| 77 | VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD); |
mikhal@google.com | 320813c | 2011-08-03 20:47:50 +0000 | [diff] [blame] | 78 | |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 79 | // When in Hybrid mode (RTT range), adjust FEC rates based on the |
| 80 | // RTT (NACK effectiveness) - adjustment factor is in the range [0,1]. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 81 | } else if (_highRttNackMs == -1 || parameters->rtt < _highRttNackMs) { |
| 82 | // TODO(mikhal): Disabling adjustment temporarily. |
| 83 | // uint16_t rttIndex = (uint16_t) parameters->rtt; |
| 84 | float adjustRtt = 1.0f; // (float)VCMNackFecTable[rttIndex] / 100.0f; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 86 | // Adjust FEC with NACK on (for delta frame only) |
| 87 | // table depends on RTT relative to rttMax (NACK Threshold) |
| 88 | _protectionFactorD = static_cast<uint8_t>( |
| 89 | adjustRtt * static_cast<float>(_protectionFactorD)); |
| 90 | // update FEC rates after applying adjustment |
| 91 | VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD); |
| 92 | } |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 93 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 94 | return true; |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 95 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 97 | int VCMNackFecMethod::ComputeMaxFramesFec( |
| 98 | const VCMProtectionParameters* parameters) { |
| 99 | if (parameters->numLayers > 2) { |
| 100 | // For more than 2 temporal layers we will only have FEC on the base layer, |
| 101 | // and the base layers will be pretty far apart. Therefore we force one |
| 102 | // frame FEC. |
| 103 | return 1; |
| 104 | } |
| 105 | // We set the max number of frames to base the FEC on so that on average |
| 106 | // we will have complete frames in one RTT. Note that this is an upper |
| 107 | // bound, and that the actual number of frames used for FEC is decided by the |
| 108 | // RTP module based on the actual number of packets and the protection factor. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 109 | float base_layer_framerate = |
| 110 | parameters->frameRate / |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 111 | static_cast<float>(1 << (parameters->numLayers - 1)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 112 | int max_frames_fec = std::max( |
| 113 | static_cast<int>(2.0f * base_layer_framerate * parameters->rtt / 1000.0f + |
| 114 | 0.5f), |
| 115 | 1); |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 116 | // |kUpperLimitFramesFec| is the upper limit on how many frames we |
| 117 | // allow any FEC to be based on. |
| 118 | if (max_frames_fec > kUpperLimitFramesFec) { |
| 119 | max_frames_fec = kUpperLimitFramesFec; |
| 120 | } |
| 121 | return max_frames_fec; |
| 122 | } |
| 123 | |
| 124 | int VCMNackFecMethod::MaxFramesFec() const { |
| 125 | return _maxFramesFec; |
| 126 | } |
| 127 | |
marpan@webrtc.org | 88ad06b | 2012-04-20 16:05:24 +0000 | [diff] [blame] | 128 | bool VCMNackFecMethod::BitRateTooLowForFec( |
| 129 | const VCMProtectionParameters* parameters) { |
| 130 | // Bitrate below which we turn off FEC, regardless of reported packet loss. |
| 131 | // The condition should depend on resolution and content. For now, use |
| 132 | // threshold on bytes per frame, with some effect for the frame size. |
| 133 | // The condition for turning off FEC is also based on other factors, |
| 134 | // such as |_numLayers|, |_maxFramesFec|, and |_rtt|. |
| 135 | int estimate_bytes_per_frame = 1000 * BitsPerFrame(parameters) / 8; |
| 136 | int max_bytes_per_frame = kMaxBytesPerFrameForFec; |
| 137 | int num_pixels = parameters->codecWidth * parameters->codecHeight; |
| 138 | if (num_pixels <= 352 * 288) { |
| 139 | max_bytes_per_frame = kMaxBytesPerFrameForFecLow; |
| 140 | } else if (num_pixels > 640 * 480) { |
| 141 | max_bytes_per_frame = kMaxBytesPerFrameForFecHigh; |
| 142 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 143 | // TODO(marpan): add condition based on maximum frames used for FEC, |
marpan@webrtc.org | 88ad06b | 2012-04-20 16:05:24 +0000 | [diff] [blame] | 144 | // and expand condition based on frame size. |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 145 | // Max round trip time threshold in ms. |
| 146 | const int64_t kMaxRttTurnOffFec = 200; |
marpan@webrtc.org | 88ad06b | 2012-04-20 16:05:24 +0000 | [diff] [blame] | 147 | if (estimate_bytes_per_frame < max_bytes_per_frame && |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 148 | parameters->numLayers < 3 && parameters->rtt < kMaxRttTurnOffFec) { |
marpan@webrtc.org | 88ad06b | 2012-04-20 16:05:24 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 154 | bool VCMNackFecMethod::EffectivePacketLoss( |
| 155 | const VCMProtectionParameters* parameters) { |
| 156 | // Set the effective packet loss for encoder (based on FEC code). |
| 157 | // Compute the effective packet loss and residual packet loss due to FEC. |
| 158 | VCMFecMethod::EffectivePacketLoss(parameters); |
| 159 | return true; |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 160 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 162 | bool VCMNackFecMethod::UpdateParameters( |
| 163 | const VCMProtectionParameters* parameters) { |
| 164 | ProtectionFactor(parameters); |
| 165 | EffectivePacketLoss(parameters); |
| 166 | _maxFramesFec = ComputeMaxFramesFec(parameters); |
| 167 | if (BitRateTooLowForFec(parameters)) { |
| 168 | _protectionFactorK = 0; |
| 169 | _protectionFactorD = 0; |
| 170 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 172 | // Protection/fec rates obtained above are defined relative to total number |
| 173 | // of packets (total rate: source + fec) FEC in RTP module assumes |
| 174 | // protection factor is defined relative to source number of packets so we |
| 175 | // should convert the factor to reduce mismatch between mediaOpt's rate and |
| 176 | // the actual one |
| 177 | _protectionFactorK = VCMFecMethod::ConvertFECRate(_protectionFactorK); |
| 178 | _protectionFactorD = VCMFecMethod::ConvertFECRate(_protectionFactorD); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 180 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 181 | } |
| 182 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 183 | VCMNackMethod::VCMNackMethod() : VCMProtectionMethod() { |
| 184 | _type = kNack; |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 185 | } |
| 186 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 187 | VCMNackMethod::~VCMNackMethod() { |
| 188 | // |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 189 | } |
| 190 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 191 | bool VCMNackMethod::EffectivePacketLoss( |
| 192 | const VCMProtectionParameters* parameter) { |
| 193 | // Effective Packet Loss, NA in current version. |
| 194 | _effectivePacketLoss = 0; |
| 195 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | } |
| 197 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 198 | bool VCMNackMethod::UpdateParameters( |
| 199 | const VCMProtectionParameters* parameters) { |
| 200 | // Compute the effective packet loss |
| 201 | EffectivePacketLoss(parameters); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 203 | // nackCost = (bitRate - nackCost) * (lossPr) |
| 204 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | } |
| 206 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 207 | VCMFecMethod::VCMFecMethod() : VCMProtectionMethod() { |
| 208 | _type = kFec; |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 209 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 210 | VCMFecMethod::~VCMFecMethod() { |
| 211 | // |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 212 | } |
| 213 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 214 | uint8_t VCMFecMethod::BoostCodeRateKey(uint8_t packetFrameDelta, |
| 215 | uint8_t packetFrameKey) const { |
| 216 | uint8_t boostRateKey = 2; |
| 217 | // Default: ratio scales the FEC protection up for I frames |
| 218 | uint8_t ratio = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 220 | if (packetFrameDelta > 0) { |
| 221 | ratio = (int8_t)(packetFrameKey / packetFrameDelta); |
| 222 | } |
| 223 | ratio = VCM_MAX(boostRateKey, ratio); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 225 | return ratio; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 228 | uint8_t VCMFecMethod::ConvertFECRate(uint8_t codeRateRTP) const { |
| 229 | return static_cast<uint8_t>(VCM_MIN( |
| 230 | 255, |
| 231 | (0.5 + 255.0 * codeRateRTP / static_cast<float>(255 - codeRateRTP)))); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | } |
| 233 | |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 234 | // Update FEC with protectionFactorD |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 235 | void VCMFecMethod::UpdateProtectionFactorD(uint8_t protectionFactorD) { |
| 236 | _protectionFactorD = protectionFactorD; |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 237 | } |
| 238 | |
mikhal@webrtc.org | d0752c3 | 2011-10-19 15:48:30 +0000 | [diff] [blame] | 239 | // Update FEC with protectionFactorK |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 240 | void VCMFecMethod::UpdateProtectionFactorK(uint8_t protectionFactorK) { |
| 241 | _protectionFactorK = protectionFactorK; |
mikhal@webrtc.org | d0752c3 | 2011-10-19 15:48:30 +0000 | [diff] [blame] | 242 | } |
| 243 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 244 | bool VCMFecMethod::ProtectionFactor(const VCMProtectionParameters* parameters) { |
| 245 | // FEC PROTECTION SETTINGS: varies with packet loss and bitrate |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 246 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 247 | // No protection if (filtered) packetLoss is 0 |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 248 | uint8_t packetLoss = static_cast<uint8_t>(255 * parameters->lossPr); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 249 | if (packetLoss == 0) { |
| 250 | _protectionFactorK = 0; |
| 251 | _protectionFactorD = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | return true; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Parameters for FEC setting: |
| 256 | // first partition size, thresholds, table pars, spatial resoln fac. |
| 257 | |
| 258 | // First partition protection: ~ 20% |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 259 | uint8_t firstPartitionProt = static_cast<uint8_t>(255 * 0.20); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 260 | |
| 261 | // Minimum protection level needed to generate one FEC packet for one |
| 262 | // source packet/frame (in RTP sender) |
| 263 | uint8_t minProtLevelFec = 85; |
| 264 | |
| 265 | // Threshold on packetLoss and bitRrate/frameRate (=average #packets), |
| 266 | // above which we allocate protection to cover at least first partition. |
| 267 | uint8_t lossThr = 0; |
| 268 | uint8_t packetNumThr = 1; |
| 269 | |
| 270 | // Parameters for range of rate index of table. |
| 271 | const uint8_t ratePar1 = 5; |
| 272 | const uint8_t ratePar2 = 49; |
| 273 | |
| 274 | // Spatial resolution size, relative to a reference size. |
| 275 | float spatialSizeToRef = |
| 276 | static_cast<float>(parameters->codecWidth * parameters->codecHeight) / |
| 277 | (static_cast<float>(704 * 576)); |
| 278 | // resolnFac: This parameter will generally increase/decrease the FEC rate |
| 279 | // (for fixed bitRate and packetLoss) based on system size. |
| 280 | // Use a smaller exponent (< 1) to control/soften system size effect. |
| 281 | const float resolnFac = 1.0 / powf(spatialSizeToRef, 0.3f); |
| 282 | |
| 283 | const int bitRatePerFrame = BitsPerFrame(parameters); |
| 284 | |
| 285 | // Average number of packets per frame (source and fec): |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 286 | const uint8_t avgTotPackets = static_cast<uint8_t>( |
| 287 | std::min(static_cast<float>(std::numeric_limits<uint8_t>::max()), |
| 288 | 1.5f + |
| 289 | static_cast<float>(bitRatePerFrame) * 1000.0f / |
| 290 | static_cast<float>(8.0 * _maxPayloadSize))); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 291 | |
| 292 | // FEC rate parameters: for P and I frame |
| 293 | uint8_t codeRateDelta = 0; |
| 294 | uint8_t codeRateKey = 0; |
| 295 | |
| 296 | // Get index for table: the FEC protection depends on an effective rate. |
| 297 | // The range on the rate index corresponds to rates (bps) |
| 298 | // from ~200k to ~8000k, for 30fps |
| 299 | const uint16_t effRateFecTable = |
| 300 | static_cast<uint16_t>(resolnFac * bitRatePerFrame); |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 301 | uint8_t rateIndexTable = static_cast<uint8_t>( |
| 302 | VCM_MAX(VCM_MIN((effRateFecTable - ratePar1) / ratePar1, ratePar2), 0)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 303 | |
| 304 | // Restrict packet loss range to 50: |
| 305 | // current tables defined only up to 50% |
| 306 | if (packetLoss >= kPacketLossMax) { |
| 307 | packetLoss = kPacketLossMax - 1; |
| 308 | } |
| 309 | uint16_t indexTable = rateIndexTable * kPacketLossMax + packetLoss; |
| 310 | |
| 311 | // Check on table index |
brandtr | 71b1c1f | 2017-01-12 06:16:24 -0800 | [diff] [blame] | 312 | RTC_DCHECK_LT(indexTable, kFecRateTableSize); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 313 | |
| 314 | // Protection factor for P frame |
brandtr | 71b1c1f | 2017-01-12 06:16:24 -0800 | [diff] [blame] | 315 | codeRateDelta = kFecRateTable[indexTable]; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 316 | |
| 317 | if (packetLoss > lossThr && avgTotPackets > packetNumThr) { |
| 318 | // Set a minimum based on first partition size. |
| 319 | if (codeRateDelta < firstPartitionProt) { |
| 320 | codeRateDelta = firstPartitionProt; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Check limit on amount of protection for P frame; 50% is max. |
| 325 | if (codeRateDelta >= kPacketLossMax) { |
| 326 | codeRateDelta = kPacketLossMax - 1; |
| 327 | } |
| 328 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 329 | // For Key frame: |
| 330 | // Effectively at a higher rate, so we scale/boost the rate |
| 331 | // The boost factor may depend on several factors: ratio of packet |
| 332 | // number of I to P frames, how much protection placed on P frames, etc. |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 333 | const uint8_t packetFrameDelta = |
| 334 | static_cast<uint8_t>(0.5 + parameters->packetsPerFrame); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 335 | const uint8_t packetFrameKey = |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 336 | static_cast<uint8_t>(0.5 + parameters->packetsPerFrameKey); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 337 | const uint8_t boostKey = BoostCodeRateKey(packetFrameDelta, packetFrameKey); |
| 338 | |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 339 | rateIndexTable = static_cast<uint8_t>(VCM_MAX( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 340 | VCM_MIN(1 + (boostKey * effRateFecTable - ratePar1) / ratePar1, ratePar2), |
brandtr | 05f845d | 2016-11-16 22:59:39 -0800 | [diff] [blame] | 341 | 0)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 342 | uint16_t indexTableKey = rateIndexTable * kPacketLossMax + packetLoss; |
| 343 | |
brandtr | 71b1c1f | 2017-01-12 06:16:24 -0800 | [diff] [blame] | 344 | indexTableKey = VCM_MIN(indexTableKey, kFecRateTableSize); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 345 | |
| 346 | // Check on table index |
brandtr | 71b1c1f | 2017-01-12 06:16:24 -0800 | [diff] [blame] | 347 | assert(indexTableKey < kFecRateTableSize); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 348 | |
| 349 | // Protection factor for I frame |
brandtr | 71b1c1f | 2017-01-12 06:16:24 -0800 | [diff] [blame] | 350 | codeRateKey = kFecRateTable[indexTableKey]; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 351 | |
| 352 | // Boosting for Key frame. |
| 353 | int boostKeyProt = _scaleProtKey * codeRateDelta; |
| 354 | if (boostKeyProt >= kPacketLossMax) { |
| 355 | boostKeyProt = kPacketLossMax - 1; |
| 356 | } |
| 357 | |
| 358 | // Make sure I frame protection is at least larger than P frame protection, |
| 359 | // and at least as high as filtered packet loss. |
| 360 | codeRateKey = static_cast<uint8_t>( |
| 361 | VCM_MAX(packetLoss, VCM_MAX(boostKeyProt, codeRateKey))); |
| 362 | |
| 363 | // Check limit on amount of protection for I frame: 50% is max. |
| 364 | if (codeRateKey >= kPacketLossMax) { |
| 365 | codeRateKey = kPacketLossMax - 1; |
| 366 | } |
| 367 | |
| 368 | _protectionFactorK = codeRateKey; |
| 369 | _protectionFactorD = codeRateDelta; |
| 370 | |
| 371 | // Generally there is a rate mis-match between the FEC cost estimated |
| 372 | // in mediaOpt and the actual FEC cost sent out in RTP module. |
| 373 | // This is more significant at low rates (small # of source packets), where |
| 374 | // the granularity of the FEC decreases. In this case, non-zero protection |
| 375 | // in mediaOpt may generate 0 FEC packets in RTP sender (since actual #FEC |
| 376 | // is based on rounding off protectionFactor on actual source packet number). |
| 377 | // The correction factor (_corrFecCost) attempts to corrects this, at least |
| 378 | // for cases of low rates (small #packets) and low protection levels. |
| 379 | |
| 380 | float numPacketsFl = 1.0f + (static_cast<float>(bitRatePerFrame) * 1000.0 / |
| 381 | static_cast<float>(8.0 * _maxPayloadSize) + |
| 382 | 0.5); |
| 383 | |
| 384 | const float estNumFecGen = |
| 385 | 0.5f + static_cast<float>(_protectionFactorD * numPacketsFl / 255.0f); |
| 386 | |
| 387 | // We reduce cost factor (which will reduce overhead for FEC and |
| 388 | // hybrid method) and not the protectionFactor. |
| 389 | _corrFecCost = 1.0f; |
| 390 | if (estNumFecGen < 1.1f && _protectionFactorD < minProtLevelFec) { |
| 391 | _corrFecCost = 0.5f; |
| 392 | } |
| 393 | if (estNumFecGen < 0.9f && _protectionFactorD < minProtLevelFec) { |
| 394 | _corrFecCost = 0.0f; |
| 395 | } |
| 396 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 397 | // DONE WITH FEC PROTECTION SETTINGS |
| 398 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | } |
| 400 | |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 401 | int VCMFecMethod::BitsPerFrame(const VCMProtectionParameters* parameters) { |
| 402 | // When temporal layers are available FEC will only be applied on the base |
| 403 | // layer. |
| 404 | const float bitRateRatio = |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 405 | kVp8LayerRateAlloction[parameters->numLayers - 1][0]; |
thakis@chromium.org | 65bc254 | 2012-08-13 19:26:12 +0000 | [diff] [blame] | 406 | float frameRateRatio = powf(1 / 2.0, parameters->numLayers - 1); |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 407 | float bitRate = parameters->bitRate * bitRateRatio; |
| 408 | float frameRate = parameters->frameRate * frameRateRatio; |
| 409 | |
| 410 | // TODO(mikhal): Update factor following testing. |
| 411 | float adjustmentFactor = 1; |
| 412 | |
Peter Boström | 5e8351b | 2016-01-28 23:55:29 +0100 | [diff] [blame] | 413 | if (frameRate < 1.0f) |
| 414 | frameRate = 1.0f; |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 415 | // Average bits per frame (units of kbits) |
| 416 | return static_cast<int>(adjustmentFactor * bitRate / frameRate); |
| 417 | } |
| 418 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 419 | bool VCMFecMethod::EffectivePacketLoss( |
| 420 | const VCMProtectionParameters* parameters) { |
| 421 | // Effective packet loss to encoder is based on RPL (residual packet loss) |
| 422 | // this is a soft setting based on degree of FEC protection |
| 423 | // RPL = received/input packet loss - average_FEC_recovery |
| 424 | // note: received/input packet loss may be filtered based on FilteredLoss |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 426 | // Effective Packet Loss, NA in current version. |
| 427 | _effectivePacketLoss = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 428 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 429 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 430 | } |
| 431 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 432 | bool VCMFecMethod::UpdateParameters(const VCMProtectionParameters* parameters) { |
| 433 | // Compute the protection factor |
| 434 | ProtectionFactor(parameters); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 435 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 436 | // Compute the effective packet loss |
| 437 | EffectivePacketLoss(parameters); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 438 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 439 | // Protection/fec rates obtained above is defined relative to total number |
| 440 | // of packets (total rate: source+fec) FEC in RTP module assumes protection |
| 441 | // factor is defined relative to source number of packets so we should |
| 442 | // convert the factor to reduce mismatch between mediaOpt suggested rate and |
| 443 | // the actual rate |
| 444 | _protectionFactorK = ConvertFECRate(_protectionFactorK); |
| 445 | _protectionFactorD = ConvertFECRate(_protectionFactorD); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 446 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 447 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 448 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 449 | VCMLossProtectionLogic::VCMLossProtectionLogic(int64_t nowMs) |
| 450 | : _currentParameters(), |
| 451 | _rtt(0), |
| 452 | _lossPr(0.0f), |
| 453 | _bitRate(0.0f), |
| 454 | _frameRate(0.0f), |
| 455 | _keyFrameSize(0.0f), |
| 456 | _fecRateKey(0), |
| 457 | _fecRateDelta(0), |
| 458 | _lastPrUpdateT(0), |
| 459 | _lossPr255(0.9999f), |
| 460 | _lossPrHistory(), |
| 461 | _shortMaxLossPr255(0), |
| 462 | _packetsPerFrame(0.9999f), |
| 463 | _packetsPerFrameKey(0.9999f), |
| 464 | _codecWidth(0), |
| 465 | _codecHeight(0), |
| 466 | _numLayers(1) { |
| 467 | Reset(nowMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | } |
| 469 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 470 | VCMLossProtectionLogic::~VCMLossProtectionLogic() { |
| 471 | Release(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 472 | } |
| 473 | |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 474 | void VCMLossProtectionLogic::SetMethod( |
| 475 | enum VCMProtectionMethodEnum newMethodType) { |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 476 | if (_selectedMethod && _selectedMethod->Type() == newMethodType) |
| 477 | return; |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 478 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 479 | switch (newMethodType) { |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 480 | case kNack: |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 481 | _selectedMethod.reset(new VCMNackMethod()); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 482 | break; |
| 483 | case kFec: |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 484 | _selectedMethod.reset(new VCMFecMethod()); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 485 | break; |
| 486 | case kNackFec: |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 487 | _selectedMethod.reset(new VCMNackFecMethod(kLowRttNackMs, -1)); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 488 | break; |
| 489 | case kNone: |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 490 | _selectedMethod.reset(); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 491 | break; |
| 492 | } |
| 493 | UpdateMethod(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 494 | } |
| 495 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 496 | void VCMLossProtectionLogic::UpdateRtt(int64_t rtt) { |
| 497 | _rtt = rtt; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 498 | } |
| 499 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 500 | void VCMLossProtectionLogic::UpdateMaxLossHistory(uint8_t lossPr255, |
| 501 | int64_t now) { |
| 502 | if (_lossPrHistory[0].timeMs >= 0 && |
| 503 | now - _lossPrHistory[0].timeMs < kLossPrShortFilterWinMs) { |
| 504 | if (lossPr255 > _shortMaxLossPr255) { |
| 505 | _shortMaxLossPr255 = lossPr255; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 506 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 507 | } else { |
| 508 | // Only add a new value to the history once a second |
| 509 | if (_lossPrHistory[0].timeMs == -1) { |
| 510 | // First, no shift |
| 511 | _shortMaxLossPr255 = lossPr255; |
| 512 | } else { |
| 513 | // Shift |
| 514 | for (int32_t i = (kLossPrHistorySize - 2); i >= 0; i--) { |
| 515 | _lossPrHistory[i + 1].lossPr255 = _lossPrHistory[i].lossPr255; |
| 516 | _lossPrHistory[i + 1].timeMs = _lossPrHistory[i].timeMs; |
| 517 | } |
| 518 | } |
| 519 | if (_shortMaxLossPr255 == 0) { |
| 520 | _shortMaxLossPr255 = lossPr255; |
| 521 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 522 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 523 | _lossPrHistory[0].lossPr255 = _shortMaxLossPr255; |
| 524 | _lossPrHistory[0].timeMs = now; |
| 525 | _shortMaxLossPr255 = 0; |
| 526 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 527 | } |
| 528 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 529 | uint8_t VCMLossProtectionLogic::MaxFilteredLossPr(int64_t nowMs) const { |
| 530 | uint8_t maxFound = _shortMaxLossPr255; |
| 531 | if (_lossPrHistory[0].timeMs == -1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 532 | return maxFound; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 533 | } |
| 534 | for (int32_t i = 0; i < kLossPrHistorySize; i++) { |
| 535 | if (_lossPrHistory[i].timeMs == -1) { |
| 536 | break; |
| 537 | } |
| 538 | if (nowMs - _lossPrHistory[i].timeMs > |
| 539 | kLossPrHistorySize * kLossPrShortFilterWinMs) { |
| 540 | // This sample (and all samples after this) is too old |
| 541 | break; |
| 542 | } |
| 543 | if (_lossPrHistory[i].lossPr255 > maxFound) { |
| 544 | // This sample is the largest one this far into the history |
| 545 | maxFound = _lossPrHistory[i].lossPr255; |
| 546 | } |
| 547 | } |
| 548 | return maxFound; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 549 | } |
| 550 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 551 | uint8_t VCMLossProtectionLogic::FilteredLoss(int64_t nowMs, |
| 552 | FilterPacketLossMode filter_mode, |
| 553 | uint8_t lossPr255) { |
marpan@webrtc.org | 2dad3fb | 2012-01-09 18:18:36 +0000 | [diff] [blame] | 554 | // Update the max window filter. |
| 555 | UpdateMaxLossHistory(lossPr255, nowMs); |
| 556 | |
| 557 | // Update the recursive average filter. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 558 | _lossPr255.Apply(static_cast<float>(nowMs - _lastPrUpdateT), |
| 559 | static_cast<float>(lossPr255)); |
marpan@webrtc.org | 2dad3fb | 2012-01-09 18:18:36 +0000 | [diff] [blame] | 560 | _lastPrUpdateT = nowMs; |
| 561 | |
| 562 | // Filtered loss: default is received loss (no filtering). |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 563 | uint8_t filtered_loss = lossPr255; |
marpan@webrtc.org | 2dad3fb | 2012-01-09 18:18:36 +0000 | [diff] [blame] | 564 | |
| 565 | switch (filter_mode) { |
| 566 | case kNoFilter: |
| 567 | break; |
| 568 | case kAvgFilter: |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 569 | filtered_loss = static_cast<uint8_t>(_lossPr255.filtered() + 0.5); |
marpan@webrtc.org | 2dad3fb | 2012-01-09 18:18:36 +0000 | [diff] [blame] | 570 | break; |
| 571 | case kMaxFilter: |
| 572 | filtered_loss = MaxFilteredLossPr(nowMs); |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | return filtered_loss; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 577 | } |
| 578 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 579 | void VCMLossProtectionLogic::UpdateFilteredLossPr(uint8_t packetLossEnc) { |
| 580 | _lossPr = static_cast<float>(packetLossEnc) / 255.0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 581 | } |
| 582 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 583 | void VCMLossProtectionLogic::UpdateBitRate(float bitRate) { |
| 584 | _bitRate = bitRate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 585 | } |
| 586 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 587 | void VCMLossProtectionLogic::UpdatePacketsPerFrame(float nPackets, |
| 588 | int64_t nowMs) { |
| 589 | _packetsPerFrame.Apply(static_cast<float>(nowMs - _lastPacketPerFrameUpdateT), |
| 590 | nPackets); |
| 591 | _lastPacketPerFrameUpdateT = nowMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 592 | } |
| 593 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 594 | void VCMLossProtectionLogic::UpdatePacketsPerFrameKey(float nPackets, |
| 595 | int64_t nowMs) { |
| 596 | _packetsPerFrameKey.Apply( |
| 597 | static_cast<float>(nowMs - _lastPacketPerFrameUpdateTKey), nPackets); |
| 598 | _lastPacketPerFrameUpdateTKey = nowMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 599 | } |
| 600 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 601 | void VCMLossProtectionLogic::UpdateKeyFrameSize(float keyFrameSize) { |
| 602 | _keyFrameSize = keyFrameSize; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 603 | } |
| 604 | |
perkj | c2c24f7 | 2016-07-11 01:47:32 -0700 | [diff] [blame] | 605 | void VCMLossProtectionLogic::UpdateFrameSize(size_t width, size_t height) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 606 | _codecWidth = width; |
| 607 | _codecHeight = height; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 608 | } |
| 609 | |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 610 | void VCMLossProtectionLogic::UpdateNumLayers(int numLayers) { |
| 611 | _numLayers = (numLayers == 0) ? 1 : numLayers; |
| 612 | } |
| 613 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 614 | bool VCMLossProtectionLogic::UpdateMethod() { |
| 615 | if (!_selectedMethod) |
| 616 | return false; |
| 617 | _currentParameters.rtt = _rtt; |
| 618 | _currentParameters.lossPr = _lossPr; |
| 619 | _currentParameters.bitRate = _bitRate; |
| 620 | _currentParameters.frameRate = _frameRate; // rename actual frame rate? |
| 621 | _currentParameters.keyFrameSize = _keyFrameSize; |
| 622 | _currentParameters.fecRateDelta = _fecRateDelta; |
| 623 | _currentParameters.fecRateKey = _fecRateKey; |
| 624 | _currentParameters.packetsPerFrame = _packetsPerFrame.filtered(); |
| 625 | _currentParameters.packetsPerFrameKey = _packetsPerFrameKey.filtered(); |
| 626 | _currentParameters.codecWidth = _codecWidth; |
| 627 | _currentParameters.codecHeight = _codecHeight; |
| 628 | _currentParameters.numLayers = _numLayers; |
| 629 | return _selectedMethod->UpdateParameters(&_currentParameters); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 630 | } |
| 631 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 632 | VCMProtectionMethod* VCMLossProtectionLogic::SelectedMethod() const { |
| 633 | return _selectedMethod.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 634 | } |
| 635 | |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 636 | VCMProtectionMethodEnum VCMLossProtectionLogic::SelectedType() const { |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 637 | return _selectedMethod ? _selectedMethod->Type() : kNone; |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 638 | } |
| 639 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 640 | void VCMLossProtectionLogic::Reset(int64_t nowMs) { |
| 641 | _lastPrUpdateT = nowMs; |
| 642 | _lastPacketPerFrameUpdateT = nowMs; |
| 643 | _lastPacketPerFrameUpdateTKey = nowMs; |
| 644 | _lossPr255.Reset(0.9999f); |
| 645 | _packetsPerFrame.Reset(0.9999f); |
| 646 | _fecRateDelta = _fecRateKey = 0; |
| 647 | for (int32_t i = 0; i < kLossPrHistorySize; i++) { |
| 648 | _lossPrHistory[i].lossPr255 = 0; |
| 649 | _lossPrHistory[i].timeMs = -1; |
| 650 | } |
| 651 | _shortMaxLossPr255 = 0; |
| 652 | Release(); |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 653 | } |
| 654 | |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 655 | void VCMLossProtectionLogic::Release() { |
| 656 | _selectedMethod.reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 657 | } |
| 658 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 659 | } // namespace media_optimization |
| 660 | } // namespace webrtc |