niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +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_optimization.h" |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 12 | |
pbos | 854e84c | 2015-11-16 16:39:06 -0800 | [diff] [blame] | 13 | #include "webrtc/base/logging.h" |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 14 | #include "webrtc/modules/video_coding/utility/frame_dropper.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 18 | namespace media_optimization { |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 19 | |
| 20 | struct MediaOptimization::EncodedFrameSample { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 21 | EncodedFrameSample(size_t size_bytes, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 22 | uint32_t timestamp, |
| 23 | int64_t time_complete_ms) |
| 24 | : size_bytes(size_bytes), |
| 25 | timestamp(timestamp), |
| 26 | time_complete_ms(time_complete_ms) {} |
| 27 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 28 | size_t size_bytes; |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 29 | uint32_t timestamp; |
| 30 | int64_t time_complete_ms; |
| 31 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 33 | MediaOptimization::MediaOptimization(Clock* clock) |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 34 | : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 35 | clock_(clock), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 36 | max_bit_rate_(0), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 37 | codec_width_(0), |
| 38 | codec_height_(0), |
| 39 | user_frame_rate_(0), |
henrik.lundin@webrtc.org | b426c46 | 2013-09-24 07:41:53 +0000 | [diff] [blame] | 40 | frame_dropper_(new FrameDropper), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 41 | fraction_lost_(0), |
| 42 | send_statistics_zero_encode_(0), |
| 43 | max_payload_size_(1460), |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 44 | video_target_bitrate_(0), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 45 | incoming_frame_rate_(0), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 46 | encoded_frame_samples_(), |
| 47 | avg_sent_bit_rate_bps_(0), |
| 48 | avg_sent_framerate_(0), |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 49 | num_layers_(0), |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 50 | suspension_enabled_(false), |
| 51 | video_suspended_(false), |
| 52 | suspension_threshold_bps_(0), |
| 53 | suspension_window_bps_(0) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 54 | memset(send_statistics_, 0, sizeof(send_statistics_)); |
| 55 | memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 58 | MediaOptimization::~MediaOptimization(void) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 61 | void MediaOptimization::Reset() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 62 | CriticalSectionScoped lock(crit_sect_.get()); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 63 | SetEncodingDataInternal(0, 0, 0, 0, 0, 0, max_payload_size_); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 64 | memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_)); |
| 65 | incoming_frame_rate_ = 0.0; |
| 66 | frame_dropper_->Reset(); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 67 | frame_dropper_->SetRates(0, 0); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 68 | send_statistics_zero_encode_ = 0; |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 69 | video_target_bitrate_ = 0; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 70 | codec_width_ = 0; |
| 71 | codec_height_ = 0; |
| 72 | user_frame_rate_ = 0; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 73 | encoded_frame_samples_.clear(); |
| 74 | avg_sent_bit_rate_bps_ = 0; |
| 75 | num_layers_ = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 78 | void MediaOptimization::SetEncodingData(int32_t max_bit_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 79 | uint32_t target_bitrate, |
| 80 | uint16_t width, |
| 81 | uint16_t height, |
Peter Boström | df66453 | 2015-05-12 12:22:14 +0200 | [diff] [blame] | 82 | uint32_t frame_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 83 | int num_layers, |
| 84 | int32_t mtu) { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 85 | CriticalSectionScoped lock(crit_sect_.get()); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 86 | SetEncodingDataInternal(max_bit_rate, frame_rate, target_bitrate, width, |
| 87 | height, num_layers, mtu); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 90 | void MediaOptimization::SetEncodingDataInternal(int32_t max_bit_rate, |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 91 | uint32_t frame_rate, |
| 92 | uint32_t target_bitrate, |
| 93 | uint16_t width, |
| 94 | uint16_t height, |
| 95 | int num_layers, |
| 96 | int32_t mtu) { |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 97 | // Everything codec specific should be reset here since this means the codec |
Peter Boström | ad6fc5a | 2016-05-12 03:01:31 +0200 | [diff] [blame] | 98 | // has changed. |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 99 | |
| 100 | max_bit_rate_ = max_bit_rate; |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 101 | video_target_bitrate_ = target_bitrate; |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 102 | float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f; |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 103 | frame_dropper_->Reset(); |
| 104 | frame_dropper_->SetRates(target_bitrate_kbps, static_cast<float>(frame_rate)); |
| 105 | user_frame_rate_ = static_cast<float>(frame_rate); |
| 106 | codec_width_ = width; |
| 107 | codec_height_ = height; |
| 108 | num_layers_ = (num_layers <= 1) ? 1 : num_layers; // Can also be zero. |
| 109 | max_payload_size_ = mtu; |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 112 | uint32_t MediaOptimization::SetTargetRates(uint32_t target_bitrate, |
| 113 | uint8_t fraction_lost, |
| 114 | int64_t round_trip_time_ms) { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 115 | CriticalSectionScoped lock(crit_sect_.get()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 117 | // Get frame rate for encoder: this is the actual/sent frame rate. |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 118 | float actual_frame_rate = SentFrameRateInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 120 | // Sanity check. |
| 121 | if (actual_frame_rate < 1.0) { |
| 122 | actual_frame_rate = 1.0; |
| 123 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 125 | fraction_lost_ = fraction_lost; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 127 | video_target_bitrate_ = target_bitrate; |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 128 | |
| 129 | // Cap target video bitrate to codec maximum. |
| 130 | if (max_bit_rate_ > 0 && video_target_bitrate_ > max_bit_rate_) { |
| 131 | video_target_bitrate_ = max_bit_rate_; |
| 132 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 134 | // Update encoding rates following protection settings. |
| 135 | float target_video_bitrate_kbps = |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 136 | static_cast<float>(video_target_bitrate_) / 1000.0f; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 137 | frame_dropper_->SetRates(target_video_bitrate_kbps, incoming_frame_rate_); |
| 138 | |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 139 | CheckSuspendConditions(); |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 140 | |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 141 | return video_target_bitrate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 144 | uint32_t MediaOptimization::InputFrameRate() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 145 | CriticalSectionScoped lock(crit_sect_.get()); |
| 146 | return InputFrameRateInternal(); |
| 147 | } |
| 148 | |
| 149 | uint32_t MediaOptimization::InputFrameRateInternal() { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 150 | ProcessIncomingFrameRate(clock_->TimeInMilliseconds()); |
| 151 | return uint32_t(incoming_frame_rate_ + 0.5f); |
| 152 | } |
| 153 | |
| 154 | uint32_t MediaOptimization::SentFrameRate() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 155 | CriticalSectionScoped lock(crit_sect_.get()); |
| 156 | return SentFrameRateInternal(); |
| 157 | } |
| 158 | |
| 159 | uint32_t MediaOptimization::SentFrameRateInternal() { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 160 | PurgeOldFrameSamples(clock_->TimeInMilliseconds()); |
| 161 | UpdateSentFramerate(); |
| 162 | return avg_sent_framerate_; |
| 163 | } |
| 164 | |
| 165 | uint32_t MediaOptimization::SentBitRate() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 166 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 167 | const int64_t now_ms = clock_->TimeInMilliseconds(); |
| 168 | PurgeOldFrameSamples(now_ms); |
| 169 | UpdateSentBitrate(now_ms); |
| 170 | return avg_sent_bit_rate_bps_; |
| 171 | } |
| 172 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 173 | int32_t MediaOptimization::UpdateWithEncodedData( |
| 174 | const EncodedImage& encoded_image) { |
| 175 | size_t encoded_length = encoded_image._length; |
| 176 | uint32_t timestamp = encoded_image._timeStamp; |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 177 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 178 | const int64_t now_ms = clock_->TimeInMilliseconds(); |
| 179 | PurgeOldFrameSamples(now_ms); |
| 180 | if (encoded_frame_samples_.size() > 0 && |
| 181 | encoded_frame_samples_.back().timestamp == timestamp) { |
| 182 | // Frames having the same timestamp are generated from the same input |
| 183 | // frame. We don't want to double count them, but only increment the |
| 184 | // size_bytes. |
| 185 | encoded_frame_samples_.back().size_bytes += encoded_length; |
| 186 | encoded_frame_samples_.back().time_complete_ms = now_ms; |
| 187 | } else { |
| 188 | encoded_frame_samples_.push_back( |
| 189 | EncodedFrameSample(encoded_length, timestamp, now_ms)); |
| 190 | } |
| 191 | UpdateSentBitrate(now_ms); |
| 192 | UpdateSentFramerate(); |
| 193 | if (encoded_length > 0) { |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 194 | const bool delta_frame = encoded_image._frameType != kVideoFrameKey; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 195 | frame_dropper_->Fill(encoded_length, delta_frame); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return VCM_OK; |
| 199 | } |
| 200 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 201 | void MediaOptimization::EnableFrameDropper(bool enable) { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 202 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 203 | frame_dropper_->Enable(enable); |
| 204 | } |
| 205 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 206 | void MediaOptimization::SuspendBelowMinBitrate(int threshold_bps, |
| 207 | int window_bps) { |
| 208 | CriticalSectionScoped lock(crit_sect_.get()); |
| 209 | assert(threshold_bps > 0 && window_bps >= 0); |
| 210 | suspension_threshold_bps_ = threshold_bps; |
| 211 | suspension_window_bps_ = window_bps; |
| 212 | suspension_enabled_ = true; |
| 213 | video_suspended_ = false; |
| 214 | } |
| 215 | |
| 216 | bool MediaOptimization::IsVideoSuspended() const { |
| 217 | CriticalSectionScoped lock(crit_sect_.get()); |
| 218 | return video_suspended_; |
| 219 | } |
| 220 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 221 | bool MediaOptimization::DropFrame() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 222 | CriticalSectionScoped lock(crit_sect_.get()); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 223 | UpdateIncomingFrameRate(); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 224 | // Leak appropriate number of bytes. |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 225 | frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f)); |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 226 | if (video_suspended_) { |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 227 | return true; // Drop all frames when muted. |
| 228 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 229 | return frame_dropper_->DropFrame(); |
| 230 | } |
| 231 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 232 | void MediaOptimization::UpdateIncomingFrameRate() { |
| 233 | int64_t now = clock_->TimeInMilliseconds(); |
| 234 | if (incoming_frame_times_[0] == 0) { |
| 235 | // No shifting if this is the first time. |
| 236 | } else { |
| 237 | // Shift all times one step. |
| 238 | for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) { |
| 239 | incoming_frame_times_[i + 1] = incoming_frame_times_[i]; |
| 240 | } |
| 241 | } |
| 242 | incoming_frame_times_[0] = now; |
| 243 | ProcessIncomingFrameRate(now); |
| 244 | } |
| 245 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 246 | void MediaOptimization::PurgeOldFrameSamples(int64_t now_ms) { |
| 247 | while (!encoded_frame_samples_.empty()) { |
| 248 | if (now_ms - encoded_frame_samples_.front().time_complete_ms > |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 249 | kBitrateAverageWinMs) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 250 | encoded_frame_samples_.pop_front(); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 251 | } else { |
| 252 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | } |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 254 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 255 | } |
| 256 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 257 | void MediaOptimization::UpdateSentBitrate(int64_t now_ms) { |
| 258 | if (encoded_frame_samples_.empty()) { |
| 259 | avg_sent_bit_rate_bps_ = 0; |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 260 | return; |
| 261 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 262 | size_t framesize_sum = 0; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 263 | for (FrameSampleList::iterator it = encoded_frame_samples_.begin(); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 264 | it != encoded_frame_samples_.end(); ++it) { |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 265 | framesize_sum += it->size_bytes; |
| 266 | } |
| 267 | float denom = static_cast<float>( |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 268 | now_ms - encoded_frame_samples_.front().time_complete_ms); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 269 | if (denom >= 1.0f) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 270 | avg_sent_bit_rate_bps_ = |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 271 | static_cast<uint32_t>(framesize_sum * 8.0f * 1000.0f / denom + 0.5f); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 272 | } else { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 273 | avg_sent_bit_rate_bps_ = framesize_sum * 8; |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 277 | void MediaOptimization::UpdateSentFramerate() { |
| 278 | if (encoded_frame_samples_.size() <= 1) { |
| 279 | avg_sent_framerate_ = encoded_frame_samples_.size(); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 280 | return; |
| 281 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 282 | int denom = encoded_frame_samples_.back().timestamp - |
| 283 | encoded_frame_samples_.front().timestamp; |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 284 | if (denom > 0) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 285 | avg_sent_framerate_ = |
| 286 | (90000 * (encoded_frame_samples_.size() - 1) + denom / 2) / denom; |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 287 | } else { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 288 | avg_sent_framerate_ = encoded_frame_samples_.size(); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 291 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 292 | // Allowing VCM to keep track of incoming frame rate. |
| 293 | void MediaOptimization::ProcessIncomingFrameRate(int64_t now) { |
| 294 | int32_t num = 0; |
| 295 | int32_t nr_of_frames = 0; |
| 296 | for (num = 1; num < (kFrameCountHistorySize - 1); ++num) { |
| 297 | if (incoming_frame_times_[num] <= 0 || |
| 298 | // don't use data older than 2 s |
| 299 | now - incoming_frame_times_[num] > kFrameHistoryWinMs) { |
| 300 | break; |
| 301 | } else { |
| 302 | nr_of_frames++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 303 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 304 | } |
| 305 | if (num > 1) { |
Erik Språng | 66a641a | 2015-06-11 14:20:07 +0200 | [diff] [blame] | 306 | const int64_t diff = |
| 307 | incoming_frame_times_[0] - incoming_frame_times_[num - 1]; |
| 308 | incoming_frame_rate_ = 0.0; // No frame rate estimate available. |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 309 | if (diff > 0) { |
| 310 | incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 311 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 312 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | } |
| 314 | |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 315 | void MediaOptimization::CheckSuspendConditions() { |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 316 | // Check conditions for SuspendBelowMinBitrate. |video_target_bitrate_| is in |
| 317 | // bps. |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 318 | if (suspension_enabled_) { |
| 319 | if (!video_suspended_) { |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 320 | // Check if we just went below the threshold. |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 321 | if (video_target_bitrate_ < suspension_threshold_bps_) { |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 322 | video_suspended_ = true; |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 323 | } |
| 324 | } else { |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 325 | // Video is already suspended. Check if we just went over the threshold |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 326 | // with a margin. |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 327 | if (video_target_bitrate_ > |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 328 | suspension_threshold_bps_ + suspension_window_bps_) { |
| 329 | video_suspended_ = false; |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 335 | } // namespace media_optimization |
| 336 | } // namespace webrtc |