blob: d73fdfe1f09003b90938c716254ea757bb50fa37 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Kjellander2557b862015-11-18 22:00:21 +010011#include "webrtc/modules/video_coding/media_optimization.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000012
Stefan Holmer144475b2017-03-10 15:08:26 +010013#include <limits>
14
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010015#include "webrtc/modules/video_coding/utility/frame_dropper.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020016#include "webrtc/rtc_base/logging.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19namespace webrtc {
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000020namespace media_optimization {
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +020021const int kMsPerSec = 1000;
22const int kBitsPerByte = 8;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000023
24struct MediaOptimization::EncodedFrameSample {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000025 EncodedFrameSample(size_t size_bytes,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000026 uint32_t timestamp,
27 int64_t time_complete_ms)
28 : size_bytes(size_bytes),
29 timestamp(timestamp),
30 time_complete_ms(time_complete_ms) {}
31
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000032 size_t size_bytes;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000033 uint32_t timestamp;
34 int64_t time_complete_ms;
35};
niklase@google.com470e71d2011-07-07 08:21:25 +000036
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000037MediaOptimization::MediaOptimization(Clock* clock)
kthelgasond701dfd2017-03-27 07:24:57 -070038 : clock_(clock),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000039 max_bit_rate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000040 user_frame_rate_(0),
henrik.lundin@webrtc.orgb426c462013-09-24 07:41:53 +000041 frame_dropper_(new FrameDropper),
pbos73674632015-10-29 15:45:00 -070042 video_target_bitrate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000043 incoming_frame_rate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000044 encoded_frame_samples_(),
asapersson60dfbdb2017-08-07 00:03:03 -070045 avg_sent_framerate_(0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000046 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
niklase@google.com470e71d2011-07-07 08:21:25 +000047}
48
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000049MediaOptimization::~MediaOptimization(void) {
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000052void MediaOptimization::Reset() {
kthelgasond701dfd2017-03-27 07:24:57 -070053 rtc::CritScope lock(&crit_sect_);
asapersson60dfbdb2017-08-07 00:03:03 -070054 SetEncodingDataInternal(0, 0, 0);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000055 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
56 incoming_frame_rate_ = 0.0;
57 frame_dropper_->Reset();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000058 frame_dropper_->SetRates(0, 0);
pbos73674632015-10-29 15:45:00 -070059 video_target_bitrate_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000060 user_frame_rate_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000061 encoded_frame_samples_.clear();
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
Per69b332d2016-06-02 15:45:42 +020064void MediaOptimization::SetEncodingData(int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000065 uint32_t target_bitrate,
asapersson60dfbdb2017-08-07 00:03:03 -070066 uint32_t frame_rate) {
kthelgasond701dfd2017-03-27 07:24:57 -070067 rtc::CritScope lock(&crit_sect_);
asapersson60dfbdb2017-08-07 00:03:03 -070068 SetEncodingDataInternal(max_bit_rate, frame_rate, target_bitrate);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000069}
70
Per69b332d2016-06-02 15:45:42 +020071void MediaOptimization::SetEncodingDataInternal(int32_t max_bit_rate,
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000072 uint32_t frame_rate,
asapersson60dfbdb2017-08-07 00:03:03 -070073 uint32_t target_bitrate) {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000074 // Everything codec specific should be reset here since this means the codec
Peter Boströmad6fc5a2016-05-12 03:01:31 +020075 // has changed.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000076 max_bit_rate_ = max_bit_rate;
pbos73674632015-10-29 15:45:00 -070077 video_target_bitrate_ = target_bitrate;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000078 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000079 frame_dropper_->Reset();
80 frame_dropper_->SetRates(target_bitrate_kbps, static_cast<float>(frame_rate));
81 user_frame_rate_ = static_cast<float>(frame_rate);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000082}
83
asapersson9abd2752016-12-08 02:19:40 -080084uint32_t MediaOptimization::SetTargetRates(uint32_t target_bitrate) {
kthelgasond701dfd2017-03-27 07:24:57 -070085 rtc::CritScope lock(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
Per69b332d2016-06-02 15:45:42 +020087 video_target_bitrate_ = target_bitrate;
pbos73674632015-10-29 15:45:00 -070088
89 // Cap target video bitrate to codec maximum.
90 if (max_bit_rate_ > 0 && video_target_bitrate_ > max_bit_rate_) {
91 video_target_bitrate_ = max_bit_rate_;
92 }
niklase@google.com470e71d2011-07-07 08:21:25 +000093
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000094 // Update encoding rates following protection settings.
95 float target_video_bitrate_kbps =
pbos73674632015-10-29 15:45:00 -070096 static_cast<float>(video_target_bitrate_) / 1000.0f;
sprang4847ae62017-06-27 07:06:52 -070097 float framerate = incoming_frame_rate_;
98 if (framerate == 0.0) {
99 // No framerate estimate available, use configured max framerate instead.
100 framerate = user_frame_rate_;
101 }
102
103 frame_dropper_->SetRates(target_video_bitrate_kbps, framerate);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000104
pbos73674632015-10-29 15:45:00 -0700105 return video_target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106}
107
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000108uint32_t MediaOptimization::InputFrameRate() {
kthelgasond701dfd2017-03-27 07:24:57 -0700109 rtc::CritScope lock(&crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000110 return InputFrameRateInternal();
111}
112
113uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000114 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
Stefan Holmer144475b2017-03-10 15:08:26 +0100115 uint32_t framerate = static_cast<uint32_t>(std::min<float>(
116 std::numeric_limits<uint32_t>::max(), incoming_frame_rate_ + 0.5f));
117 return framerate;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000118}
119
120uint32_t MediaOptimization::SentFrameRate() {
kthelgasond701dfd2017-03-27 07:24:57 -0700121 rtc::CritScope lock(&crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000122 return SentFrameRateInternal();
123}
124
125uint32_t MediaOptimization::SentFrameRateInternal() {
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200126 PurgeOldFrameSamples(clock_->TimeInMilliseconds() - kBitrateAverageWinMs);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000127 UpdateSentFramerate();
128 return avg_sent_framerate_;
129}
130
131uint32_t MediaOptimization::SentBitRate() {
kthelgasond701dfd2017-03-27 07:24:57 -0700132 rtc::CritScope lock(&crit_sect_);
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200133 PurgeOldFrameSamples(clock_->TimeInMilliseconds() - kBitrateAverageWinMs);
134 size_t sent_bytes = 0;
135 for (auto& frame_sample : encoded_frame_samples_) {
136 sent_bytes += frame_sample.size_bytes;
137 }
138 return sent_bytes * kBitsPerByte * kMsPerSec / kBitrateAverageWinMs;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000139}
140
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000141int32_t MediaOptimization::UpdateWithEncodedData(
142 const EncodedImage& encoded_image) {
143 size_t encoded_length = encoded_image._length;
144 uint32_t timestamp = encoded_image._timeStamp;
kthelgasond701dfd2017-03-27 07:24:57 -0700145 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000146 const int64_t now_ms = clock_->TimeInMilliseconds();
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200147 PurgeOldFrameSamples(now_ms - kBitrateAverageWinMs);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000148 if (encoded_frame_samples_.size() > 0 &&
149 encoded_frame_samples_.back().timestamp == timestamp) {
150 // Frames having the same timestamp are generated from the same input
151 // frame. We don't want to double count them, but only increment the
152 // size_bytes.
153 encoded_frame_samples_.back().size_bytes += encoded_length;
154 encoded_frame_samples_.back().time_complete_ms = now_ms;
155 } else {
156 encoded_frame_samples_.push_back(
157 EncodedFrameSample(encoded_length, timestamp, now_ms));
158 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000159 UpdateSentFramerate();
160 if (encoded_length > 0) {
Peter Boström49e196a2015-10-23 15:58:18 +0200161 const bool delta_frame = encoded_image._frameType != kVideoFrameKey;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000162 frame_dropper_->Fill(encoded_length, delta_frame);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000163 }
164
165 return VCM_OK;
166}
167
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000168void MediaOptimization::EnableFrameDropper(bool enable) {
kthelgasond701dfd2017-03-27 07:24:57 -0700169 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000170 frame_dropper_->Enable(enable);
171}
172
173bool MediaOptimization::DropFrame() {
kthelgasond701dfd2017-03-27 07:24:57 -0700174 rtc::CritScope lock(&crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000175 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000176 // Leak appropriate number of bytes.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000177 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000178 return frame_dropper_->DropFrame();
179}
180
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000181void MediaOptimization::UpdateIncomingFrameRate() {
182 int64_t now = clock_->TimeInMilliseconds();
183 if (incoming_frame_times_[0] == 0) {
184 // No shifting if this is the first time.
185 } else {
186 // Shift all times one step.
187 for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) {
188 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
189 }
190 }
191 incoming_frame_times_[0] = now;
192 ProcessIncomingFrameRate(now);
193}
194
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200195void MediaOptimization::PurgeOldFrameSamples(int64_t threshold_ms) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000196 while (!encoded_frame_samples_.empty()) {
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200197 if (encoded_frame_samples_.front().time_complete_ms < threshold_ms) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000198 encoded_frame_samples_.pop_front();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000199 } else {
200 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201 }
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000202 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000203}
204
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000205void MediaOptimization::UpdateSentFramerate() {
206 if (encoded_frame_samples_.size() <= 1) {
207 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000208 return;
209 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000210 int denom = encoded_frame_samples_.back().timestamp -
211 encoded_frame_samples_.front().timestamp;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000212 if (denom > 0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000213 avg_sent_framerate_ =
214 (90000 * (encoded_frame_samples_.size() - 1) + denom / 2) / denom;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000215 } else {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000216 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000217 }
218}
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000220// Allowing VCM to keep track of incoming frame rate.
221void MediaOptimization::ProcessIncomingFrameRate(int64_t now) {
222 int32_t num = 0;
223 int32_t nr_of_frames = 0;
224 for (num = 1; num < (kFrameCountHistorySize - 1); ++num) {
225 if (incoming_frame_times_[num] <= 0 ||
asapersson60dfbdb2017-08-07 00:03:03 -0700226 // Don't use data older than 2 s.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000227 now - incoming_frame_times_[num] > kFrameHistoryWinMs) {
228 break;
229 } else {
230 nr_of_frames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000231 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000232 }
233 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200234 const int64_t diff =
235 incoming_frame_times_[0] - incoming_frame_times_[num - 1];
236 incoming_frame_rate_ = 0.0; // No frame rate estimate available.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000237 if (diff > 0) {
238 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000240 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000241}
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000242} // namespace media_optimization
243} // namespace webrtc