blob: 2293cefed2e6489525ab4bf6034572ef51cf8eb0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010011#ifndef WEBRTC_MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_
12#define WEBRTC_MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000014#include <cstddef>
15
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000016#include "webrtc/base/exp_filter.h"
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000017#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010019namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000020
niklase@google.com470e71d2011-07-07 08:21:25 +000021// The Frame Dropper implements a variant of the leaky bucket algorithm
22// for keeping track of when to drop frames to avoid bit rate
23// over use when the encoder can't keep its bit rate.
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010024class FrameDropper {
25 public:
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000026 FrameDropper();
stefan@webrtc.org84cd8e32013-03-07 13:12:32 +000027 explicit FrameDropper(float max_time_drops);
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000028 virtual ~FrameDropper() {}
29
niklase@google.com470e71d2011-07-07 08:21:25 +000030 // Resets the FrameDropper to its initial state.
31 // This means that the frameRateWeight is set to its
32 // default value as well.
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000033 virtual void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000034
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000035 virtual void Enable(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000036 // Answers the question if it's time to drop a frame
37 // if we want to reach a given frame rate. Must be
38 // called for every frame.
39 //
40 // Return value : True if we should drop the current frame
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000041 virtual bool DropFrame();
niklase@google.com470e71d2011-07-07 08:21:25 +000042 // Updates the FrameDropper with the size of the latest encoded
43 // frame. The FrameDropper calculates a new drop ratio (can be
44 // seen as the probability to drop a frame) and updates its
45 // internal statistics.
46 //
47 // Input:
48 // - frameSizeBytes : The size of the latest frame
49 // returned from the encoder.
50 // - deltaFrame : True if the encoder returned
51 // a key frame.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000052 virtual void Fill(size_t frameSizeBytes, bool deltaFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +000053
pbos@webrtc.org034f0042013-04-08 11:13:29 +000054 virtual void Leak(uint32_t inputFrameRate);
niklase@google.com470e71d2011-07-07 08:21:25 +000055
pbos@webrtc.org034f0042013-04-08 11:13:29 +000056 void UpdateNack(uint32_t nackBytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
58 // Sets the target bit rate and the frame rate produced by
59 // the camera.
60 //
61 // Input:
62 // - bitRate : The target bit rate
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000063 virtual void SetRates(float bitRate, float incoming_frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +000064
65 // Return value : The current average frame rate produced
66 // if the DropFrame() function is used as
67 // instruction of when to drop frames.
pbos@webrtc.org034f0042013-04-08 11:13:29 +000068 virtual float ActualFrameRate(uint32_t inputFrameRate) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000069
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010070 private:
niklase@google.com470e71d2011-07-07 08:21:25 +000071 void FillBucket(float inKbits, float outKbits);
72 void UpdateRatio();
marpan@webrtc.org1dd8d4b2012-10-09 20:43:56 +000073 void CapAccumulator();
niklase@google.com470e71d2011-07-07 08:21:25 +000074
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000075 rtc::ExpFilter _keyFrameSizeAvgKbits;
76 rtc::ExpFilter _keyFrameRatio;
77 float _keyFrameSpreadFrames;
78 int32_t _keyFrameCount;
79 float _accumulator;
80 float _accumulatorMax;
81 float _targetBitRate;
82 bool _dropNext;
83 rtc::ExpFilter _dropRatio;
84 int32_t _dropCount;
85 float _windowSize;
86 float _incoming_frame_rate;
87 bool _wasBelowMax;
88 bool _enabled;
89 bool _fastMode;
90 float _cap_buffer_size;
91 float _max_time_drops;
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010092}; // end of VCMFrameDropper class
niklase@google.com470e71d2011-07-07 08:21:25 +000093
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000094} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000095
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010096#endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_