niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 8fe03af | 2012-01-23 14:56:14 +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 | #ifndef WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_ |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 15 | #include <memory> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
Henrik Kjellander | dca1e09 | 2017-07-01 16:42:22 +0200 | [diff] [blame] | 17 | #include "webrtc/base/criticalsection.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/include/module_common_types.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 19 | #include "webrtc/modules/video_coding/include/video_coding.h" |
| 20 | #include "webrtc/modules/video_coding/media_opt_util.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 22 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 24 | // Forward declarations. |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 25 | class Clock; |
stefan@webrtc.org | eb91792 | 2013-02-18 14:40:18 +0000 | [diff] [blame] | 26 | class FrameDropper; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | class VCMContentMetricsProcessing; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 29 | namespace media_optimization { |
| 30 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 31 | class MediaOptimization { |
| 32 | public: |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 33 | explicit MediaOptimization(Clock* clock); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 34 | ~MediaOptimization(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 36 | // TODO(andresp): Can Reset and SetEncodingData be done at construction time |
| 37 | // only? |
| 38 | void Reset(); |
| 39 | |
| 40 | // Informs media optimization of initial encoding state. |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 41 | // TODO(perkj): Deprecate SetEncodingData once its not used for stats in |
| 42 | // VieEncoder. |
| 43 | void SetEncodingData(int32_t max_bit_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 44 | uint32_t bit_rate, |
| 45 | uint16_t width, |
| 46 | uint16_t height, |
Peter Boström | df66453 | 2015-05-12 12:22:14 +0200 | [diff] [blame] | 47 | uint32_t frame_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 48 | int num_temporal_layers, |
| 49 | int32_t mtu); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 51 | // Sets target rates for the encoder given the channel parameters. |
asapersson | 9abd275 | 2016-12-08 02:19:40 -0800 | [diff] [blame] | 52 | // Input: target bitrate - the encoder target bitrate in bits/s. |
| 53 | uint32_t SetTargetRates(uint32_t target_bitrate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 55 | void EnableFrameDropper(bool enable); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 56 | bool DropFrame(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 58 | // Informs Media Optimization of encoded output. |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 59 | // TODO(perkj): Deprecate SetEncodingData once its not used for stats in |
| 60 | // VieEncoder. |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 61 | int32_t UpdateWithEncodedData(const EncodedImage& encoded_image); |
mikhal@google.com | b29d940 | 2011-08-01 16:39:20 +0000 | [diff] [blame] | 62 | |
Erik Språng | 66a641a | 2015-06-11 14:20:07 +0200 | [diff] [blame] | 63 | // InputFrameRate 0 = no frame rate estimate available. |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 64 | uint32_t InputFrameRate(); |
| 65 | uint32_t SentFrameRate(); |
| 66 | uint32_t SentBitRate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 68 | private: |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 69 | enum { kFrameCountHistorySize = 90 }; |
| 70 | enum { kFrameHistoryWinMs = 2000 }; |
| 71 | enum { kBitrateAverageWinMs = 1000 }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 73 | struct EncodedFrameSample; |
| 74 | typedef std::list<EncodedFrameSample> FrameSampleList; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 76 | void UpdateIncomingFrameRate() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
Sebastian Jansson | 9e3f1e4 | 2017-07-06 16:52:09 +0200 | [diff] [blame^] | 77 | void PurgeOldFrameSamples(int64_t threshold_ms) |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 78 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
Sebastian Jansson | 9e3f1e4 | 2017-07-06 16:52:09 +0200 | [diff] [blame^] | 79 | // Updates the sent bitrate field, a call to PurgeOldFrameSamples must preceed |
| 80 | int GetSentBitrate(int64_t now_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 81 | void UpdateSentFramerate() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 83 | void ProcessIncomingFrameRate(int64_t now) |
| 84 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 86 | // Checks conditions for suspending the video. The method compares |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 87 | // |video_target_bitrate_| with the threshold values for suspension, and |
| 88 | // changes the state of |video_suspended_| accordingly. |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 89 | void CheckSuspendConditions() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 90 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 91 | void SetEncodingDataInternal(int32_t max_bit_rate, |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 92 | uint32_t frame_rate, |
| 93 | uint32_t bit_rate, |
| 94 | uint16_t width, |
| 95 | uint16_t height, |
| 96 | int num_temporal_layers, |
| 97 | int32_t mtu) |
| 98 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 99 | |
| 100 | uint32_t InputFrameRateInternal() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 101 | |
| 102 | uint32_t SentFrameRateInternal() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 103 | |
| 104 | // Protect all members. |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 105 | rtc::CriticalSection crit_sect_; |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 106 | |
| 107 | Clock* clock_ GUARDED_BY(crit_sect_); |
| 108 | int32_t max_bit_rate_ GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 109 | uint16_t codec_width_ GUARDED_BY(crit_sect_); |
| 110 | uint16_t codec_height_ GUARDED_BY(crit_sect_); |
| 111 | float user_frame_rate_ GUARDED_BY(crit_sect_); |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 112 | std::unique_ptr<FrameDropper> frame_dropper_ GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 113 | uint32_t send_statistics_[4] GUARDED_BY(crit_sect_); |
| 114 | uint32_t send_statistics_zero_encode_ GUARDED_BY(crit_sect_); |
| 115 | int32_t max_payload_size_ GUARDED_BY(crit_sect_); |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 116 | int video_target_bitrate_ GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 117 | float incoming_frame_rate_ GUARDED_BY(crit_sect_); |
| 118 | int64_t incoming_frame_times_[kFrameCountHistorySize] GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 119 | std::list<EncodedFrameSample> encoded_frame_samples_ GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 120 | uint32_t avg_sent_framerate_ GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 121 | int num_layers_ GUARDED_BY(crit_sect_); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 122 | }; |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 123 | } // namespace media_optimization |
| 124 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 126 | #endif // WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_ |