2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPT_UTIL_H_
12 #define WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPT_UTIL_H_
17 #include "webrtc/modules/video_coding/main/source/internal_defines.h"
18 #include "webrtc/modules/video_coding/main/source/qm_select.h"
19 #include "webrtc/modules/video_coding/utility/include/exp_filter.h"
20 #include "webrtc/system_wrappers/interface/trace.h"
21 #include "webrtc/typedefs.h"
24 namespace media_optimization {
26 // Number of time periods used for (max) window filter for packet loss
27 // TODO (marpan): set reasonable window size for filtered packet loss,
28 // adjustment should be based on logged/real data of loss stats/correlation.
29 enum { kLossPrHistorySize = 10 };
31 // 1000 ms, total filter length is (kLossPrHistorySize * 1000) ms
32 enum { kLossPrShortFilterWinMs = 1000 };
34 // The type of filter used on the received packet loss reports.
35 enum FilterPacketLossMode {
36 kNoFilter, // No filtering on received loss.
37 kAvgFilter, // Recursive average filter.
38 kMaxFilter // Max-window filter, over the time interval of:
39 // (kLossPrHistorySize * kLossPrShortFilterWinMs) ms.
42 // Thresholds for hybrid NACK/FEC
43 // common to media optimization and the jitter buffer.
49 struct VCMProtectionParameters
51 VCMProtectionParameters() : rtt(0), lossPr(0.0f), bitRate(0.0f),
52 packetsPerFrame(0.0f), packetsPerFrameKey(0.0f), frameRate(0.0f),
53 keyFrameSize(0.0f), fecRateDelta(0), fecRateKey(0),
54 residualPacketLossFec(0.0f), codecWidth(0), codecHeight(0),
61 float packetsPerFrame;
62 float packetsPerFrameKey;
67 float residualPacketLossFec;
74 /******************************/
75 /* VCMProtectionMethod class */
76 /******************************/
78 enum VCMProtectionMethodEnum
86 class VCMLossProbabilitySample
89 VCMLossProbabilitySample() : lossPr255(0), timeMs(-1) {};
96 class VCMProtectionMethod
99 VCMProtectionMethod();
100 virtual ~VCMProtectionMethod();
102 // Updates the efficiency of the method using the parameters provided
105 // - parameters : Parameters used to calculate efficiency
107 // Return value : True if this method is recommended in
108 // the given conditions.
109 virtual bool UpdateParameters(const VCMProtectionParameters* parameters) = 0;
111 // Returns the protection type
113 // Return value : The protection type
114 enum VCMProtectionMethodEnum Type() const { return _type; }
116 // Returns the bit rate required by this protection method
117 // during these conditions.
119 // Return value : Required bit rate
120 virtual float RequiredBitRate() { return _efficiency; }
122 // Returns the effective packet loss for ER, required by this protection method
124 // Return value : Required effective packet loss
125 virtual uint8_t RequiredPacketLossER() { return _effectivePacketLoss; }
127 // Extracts the FEC protection factor for Key frame, required by this protection method
129 // Return value : Required protectionFactor for Key frame
130 virtual uint8_t RequiredProtectionFactorK() { return _protectionFactorK; }
132 // Extracts the FEC protection factor for Delta frame, required by this protection method
134 // Return value : Required protectionFactor for delta frame
135 virtual uint8_t RequiredProtectionFactorD() { return _protectionFactorD; }
137 // Extracts whether the FEC Unequal protection (UEP) is used for Key frame.
139 // Return value : Required Unequal protection on/off state.
140 virtual bool RequiredUepProtectionK() { return _useUepProtectionK; }
142 // Extracts whether the the FEC Unequal protection (UEP) is used for Delta frame.
144 // Return value : Required Unequal protection on/off state.
145 virtual bool RequiredUepProtectionD() { return _useUepProtectionD; }
147 virtual int MaxFramesFec() const { return 1; }
149 // Updates content metrics
150 void UpdateContentMetrics(const VideoContentMetrics* contentMetrics);
154 uint8_t _effectivePacketLoss;
155 uint8_t _protectionFactorK;
156 uint8_t _protectionFactorD;
157 // Estimation of residual loss after the FEC
158 float _residualPacketLossFec;
160 int32_t _maxPayloadSize;
162 VCMQmRobustness* _qmRobustness;
163 bool _useUepProtectionK;
164 bool _useUepProtectionD;
166 enum VCMProtectionMethodEnum _type;
170 class VCMNackMethod : public VCMProtectionMethod
174 virtual ~VCMNackMethod();
175 virtual bool UpdateParameters(const VCMProtectionParameters* parameters);
176 // Get the effective packet loss
177 bool EffectivePacketLoss(const VCMProtectionParameters* parameter);
180 class VCMFecMethod : public VCMProtectionMethod
184 virtual ~VCMFecMethod();
185 virtual bool UpdateParameters(const VCMProtectionParameters* parameters);
186 // Get the effective packet loss for ER
187 bool EffectivePacketLoss(const VCMProtectionParameters* parameters);
188 // Get the FEC protection factors
189 bool ProtectionFactor(const VCMProtectionParameters* parameters);
190 // Get the boost for key frame protection
191 uint8_t BoostCodeRateKey(uint8_t packetFrameDelta,
192 uint8_t packetFrameKey) const;
193 // Convert the rates: defined relative to total# packets or source# packets
194 uint8_t ConvertFECRate(uint8_t codeRate) const;
195 // Get the average effective recovery from FEC: for random loss model
196 float AvgRecoveryFEC(const VCMProtectionParameters* parameters) const;
197 // Update FEC with protectionFactorD
198 void UpdateProtectionFactorD(uint8_t protectionFactorD);
199 // Update FEC with protectionFactorK
200 void UpdateProtectionFactorK(uint8_t protectionFactorK);
201 // Compute the bits per frame. Account for temporal layers when applicable.
202 int BitsPerFrame(const VCMProtectionParameters* parameters);
205 enum { kUpperLimitFramesFec = 6 };
206 // Thresholds values for the bytes/frame and round trip time, below which we
207 // may turn off FEC, depending on |_numLayers| and |_maxFramesFec|.
208 // Max bytes/frame for VGA, corresponds to ~140k at 25fps.
209 enum { kMaxBytesPerFrameForFec = 700 };
210 // Max bytes/frame for CIF and lower: corresponds to ~80k at 25fps.
211 enum { kMaxBytesPerFrameForFecLow = 400 };
212 // Max bytes/frame for frame size larger than VGA, ~200k at 25fps.
213 enum { kMaxBytesPerFrameForFecHigh = 1000 };
214 // Max round trip time threshold in ms.
215 enum { kMaxRttTurnOffFec = 200 };
219 class VCMNackFecMethod : public VCMFecMethod
222 VCMNackFecMethod(int lowRttNackThresholdMs,
223 int highRttNackThresholdMs);
224 virtual ~VCMNackFecMethod();
225 virtual bool UpdateParameters(const VCMProtectionParameters* parameters);
226 // Get the effective packet loss for ER
227 bool EffectivePacketLoss(const VCMProtectionParameters* parameters);
228 // Get the protection factors
229 bool ProtectionFactor(const VCMProtectionParameters* parameters);
230 // Get the max number of frames the FEC is allowed to be based on.
231 int MaxFramesFec() const;
232 // Turn off the FEC based on low bitrate and other factors.
233 bool BitRateTooLowForFec(const VCMProtectionParameters* parameters);
235 int ComputeMaxFramesFec(const VCMProtectionParameters* parameters);
242 class VCMLossProtectionLogic
245 VCMLossProtectionLogic(int64_t nowMs);
246 ~VCMLossProtectionLogic();
248 // Set the protection method to be used
251 // - newMethodType : New requested protection method type. If one
252 // is already set, it will be deleted and replaced
253 // Return value: Returns true on update
254 bool SetMethod(VCMProtectionMethodEnum newMethodType);
256 // Remove requested protection method
258 // - method : method to be removed (if currently selected)
260 // Return value: Returns true on update
261 bool RemoveMethod(VCMProtectionMethodEnum method);
263 // Return required bit rate per selected protectin method
264 float RequiredBitRate() const;
266 // Update the round-trip time
269 // - rtt : Round-trip time in seconds.
270 void UpdateRtt(uint32_t rtt);
272 // Update residual packet loss
275 // - residualPacketLoss : residual packet loss:
276 // effective loss after FEC recovery
277 void UpdateResidualPacketLoss(float _residualPacketLoss);
279 // Update the filtered packet loss.
282 // - packetLossEnc : The reported packet loss filtered
283 // (max window or average)
284 void UpdateFilteredLossPr(uint8_t packetLossEnc);
286 // Update the current target bit rate.
289 // - bitRate : The current target bit rate in kbits/s
290 void UpdateBitRate(float bitRate);
292 // Update the number of packets per frame estimate, for delta frames
295 // - nPackets : Number of packets in the latest sent frame.
296 void UpdatePacketsPerFrame(float nPackets, int64_t nowMs);
298 // Update the number of packets per frame estimate, for key frames
301 // - nPackets : umber of packets in the latest sent frame.
302 void UpdatePacketsPerFrameKey(float nPackets, int64_t nowMs);
304 // Update the keyFrameSize estimate
307 // - keyFrameSize : The size of the latest sent key frame.
308 void UpdateKeyFrameSize(float keyFrameSize);
310 // Update the frame rate
313 // - frameRate : The current target frame rate.
314 void UpdateFrameRate(float frameRate) { _frameRate = frameRate; }
316 // Update the frame size
319 // - width : The codec frame width.
320 // - height : The codec frame height.
321 void UpdateFrameSize(uint16_t width, uint16_t height);
323 // Update the number of active layers
326 // - numLayers : Number of layers used.
327 void UpdateNumLayers(int numLayers);
329 // The amount of packet loss to cover for with FEC.
332 // - fecRateKey : Packet loss to cover for with FEC when
333 // sending key frames.
334 // - fecRateDelta : Packet loss to cover for with FEC when
335 // sending delta frames.
336 void UpdateFECRates(uint8_t fecRateKey, uint8_t fecRateDelta)
337 { _fecRateKey = fecRateKey;
338 _fecRateDelta = fecRateDelta; }
340 // Update the protection methods with the current VCMProtectionParameters
341 // and set the requested protection settings.
342 // Return value : Returns true on update
345 // Returns the method currently selected.
347 // Return value : The protection method currently selected.
348 VCMProtectionMethod* SelectedMethod() const;
350 // Return the protection type of the currently selected method
351 VCMProtectionMethodEnum SelectedType() const;
353 // Updates the filtered loss for the average and max window packet loss,
354 // and returns the filtered loss probability in the interval [0, 255].
355 // The returned filtered loss value depends on the parameter |filter_mode|.
356 // The input parameter |lossPr255| is the received packet loss.
358 // Return value : The filtered loss probability
359 uint8_t FilteredLoss(int64_t nowMs, FilterPacketLossMode filter_mode,
362 void Reset(int64_t nowMs);
367 // Sets the available loss protection methods.
368 void UpdateMaxLossHistory(uint8_t lossPr255, int64_t now);
369 uint8_t MaxFilteredLossPr(int64_t nowMs) const;
370 VCMProtectionMethod* _selectedMethod;
371 VCMProtectionParameters _currentParameters;
378 uint8_t _fecRateDelta;
379 int64_t _lastPrUpdateT;
380 int64_t _lastPacketPerFrameUpdateT;
381 int64_t _lastPacketPerFrameUpdateTKey;
382 VCMExpFilter _lossPr255;
383 VCMLossProbabilitySample _lossPrHistory[kLossPrHistorySize];
384 uint8_t _shortMaxLossPr255;
385 VCMExpFilter _packetsPerFrame;
386 VCMExpFilter _packetsPerFrameKey;
387 float _residualPacketLossFec;
388 uint16_t _codecWidth;
389 uint16_t _codecHeight;
393 } // namespace media_optimization
394 } // namespace webrtc
396 #endif // WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPT_UTIL_H_