blob: 9c74f4b84748dd864099a6a0dd71ff60106088d2 [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
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020015#include "webrtc/base/logging.h"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010016#include "webrtc/modules/video_coding/utility/frame_dropper.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 codec_width_(0),
41 codec_height_(0),
42 user_frame_rate_(0),
henrik.lundin@webrtc.orgb426c462013-09-24 07:41:53 +000043 frame_dropper_(new FrameDropper),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000044 send_statistics_zero_encode_(0),
45 max_payload_size_(1460),
pbos73674632015-10-29 15:45:00 -070046 video_target_bitrate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000047 incoming_frame_rate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000048 encoded_frame_samples_(),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000049 avg_sent_framerate_(0),
mflodmane15032c2016-07-01 09:00:03 +020050 num_layers_(0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000051 memset(send_statistics_, 0, sizeof(send_statistics_));
52 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
niklase@google.com470e71d2011-07-07 08:21:25 +000053}
54
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000055MediaOptimization::~MediaOptimization(void) {
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000058void MediaOptimization::Reset() {
kthelgasond701dfd2017-03-27 07:24:57 -070059 rtc::CritScope lock(&crit_sect_);
Per69b332d2016-06-02 15:45:42 +020060 SetEncodingDataInternal(0, 0, 0, 0, 0, 0, max_payload_size_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000061 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
62 incoming_frame_rate_ = 0.0;
63 frame_dropper_->Reset();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000064 frame_dropper_->SetRates(0, 0);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000065 send_statistics_zero_encode_ = 0;
pbos73674632015-10-29 15:45:00 -070066 video_target_bitrate_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000067 codec_width_ = 0;
68 codec_height_ = 0;
69 user_frame_rate_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000070 encoded_frame_samples_.clear();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000071 num_layers_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
Per69b332d2016-06-02 15:45:42 +020074void MediaOptimization::SetEncodingData(int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000075 uint32_t target_bitrate,
76 uint16_t width,
77 uint16_t height,
Peter Boströmdf664532015-05-12 12:22:14 +020078 uint32_t frame_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000079 int num_layers,
80 int32_t mtu) {
kthelgasond701dfd2017-03-27 07:24:57 -070081 rtc::CritScope lock(&crit_sect_);
Per69b332d2016-06-02 15:45:42 +020082 SetEncodingDataInternal(max_bit_rate, frame_rate, target_bitrate, width,
83 height, num_layers, mtu);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000084}
85
Per69b332d2016-06-02 15:45:42 +020086void MediaOptimization::SetEncodingDataInternal(int32_t max_bit_rate,
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000087 uint32_t frame_rate,
88 uint32_t target_bitrate,
89 uint16_t width,
90 uint16_t height,
91 int num_layers,
92 int32_t mtu) {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000093 // Everything codec specific should be reset here since this means the codec
Peter Boströmad6fc5a2016-05-12 03:01:31 +020094 // has changed.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000095
96 max_bit_rate_ = max_bit_rate;
pbos73674632015-10-29 15:45:00 -070097 video_target_bitrate_ = target_bitrate;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000098 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000099 frame_dropper_->Reset();
100 frame_dropper_->SetRates(target_bitrate_kbps, static_cast<float>(frame_rate));
101 user_frame_rate_ = static_cast<float>(frame_rate);
102 codec_width_ = width;
103 codec_height_ = height;
104 num_layers_ = (num_layers <= 1) ? 1 : num_layers; // Can also be zero.
105 max_payload_size_ = mtu;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000106}
107
asapersson9abd2752016-12-08 02:19:40 -0800108uint32_t MediaOptimization::SetTargetRates(uint32_t target_bitrate) {
kthelgasond701dfd2017-03-27 07:24:57 -0700109 rtc::CritScope lock(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
Per69b332d2016-06-02 15:45:42 +0200111 video_target_bitrate_ = target_bitrate;
pbos73674632015-10-29 15:45:00 -0700112
113 // Cap target video bitrate to codec maximum.
114 if (max_bit_rate_ > 0 && video_target_bitrate_ > max_bit_rate_) {
115 video_target_bitrate_ = max_bit_rate_;
116 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000118 // Update encoding rates following protection settings.
119 float target_video_bitrate_kbps =
pbos73674632015-10-29 15:45:00 -0700120 static_cast<float>(video_target_bitrate_) / 1000.0f;
sprang4847ae62017-06-27 07:06:52 -0700121 float framerate = incoming_frame_rate_;
122 if (framerate == 0.0) {
123 // No framerate estimate available, use configured max framerate instead.
124 framerate = user_frame_rate_;
125 }
126
127 frame_dropper_->SetRates(target_video_bitrate_kbps, framerate);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000128
pbos73674632015-10-29 15:45:00 -0700129 return video_target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130}
131
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000132uint32_t MediaOptimization::InputFrameRate() {
kthelgasond701dfd2017-03-27 07:24:57 -0700133 rtc::CritScope lock(&crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000134 return InputFrameRateInternal();
135}
136
137uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000138 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
Stefan Holmer144475b2017-03-10 15:08:26 +0100139 uint32_t framerate = static_cast<uint32_t>(std::min<float>(
140 std::numeric_limits<uint32_t>::max(), incoming_frame_rate_ + 0.5f));
141 return framerate;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000142}
143
144uint32_t MediaOptimization::SentFrameRate() {
kthelgasond701dfd2017-03-27 07:24:57 -0700145 rtc::CritScope lock(&crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000146 return SentFrameRateInternal();
147}
148
149uint32_t MediaOptimization::SentFrameRateInternal() {
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200150 PurgeOldFrameSamples(clock_->TimeInMilliseconds() - kBitrateAverageWinMs);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000151 UpdateSentFramerate();
152 return avg_sent_framerate_;
153}
154
155uint32_t MediaOptimization::SentBitRate() {
kthelgasond701dfd2017-03-27 07:24:57 -0700156 rtc::CritScope lock(&crit_sect_);
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200157 PurgeOldFrameSamples(clock_->TimeInMilliseconds() - kBitrateAverageWinMs);
158 size_t sent_bytes = 0;
159 for (auto& frame_sample : encoded_frame_samples_) {
160 sent_bytes += frame_sample.size_bytes;
161 }
162 return sent_bytes * kBitsPerByte * kMsPerSec / kBitrateAverageWinMs;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000163}
164
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000165int32_t MediaOptimization::UpdateWithEncodedData(
166 const EncodedImage& encoded_image) {
167 size_t encoded_length = encoded_image._length;
168 uint32_t timestamp = encoded_image._timeStamp;
kthelgasond701dfd2017-03-27 07:24:57 -0700169 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000170 const int64_t now_ms = clock_->TimeInMilliseconds();
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200171 PurgeOldFrameSamples(now_ms - kBitrateAverageWinMs);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000172 if (encoded_frame_samples_.size() > 0 &&
173 encoded_frame_samples_.back().timestamp == timestamp) {
174 // Frames having the same timestamp are generated from the same input
175 // frame. We don't want to double count them, but only increment the
176 // size_bytes.
177 encoded_frame_samples_.back().size_bytes += encoded_length;
178 encoded_frame_samples_.back().time_complete_ms = now_ms;
179 } else {
180 encoded_frame_samples_.push_back(
181 EncodedFrameSample(encoded_length, timestamp, now_ms));
182 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000183 UpdateSentFramerate();
184 if (encoded_length > 0) {
Peter Boström49e196a2015-10-23 15:58:18 +0200185 const bool delta_frame = encoded_image._frameType != kVideoFrameKey;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000186 frame_dropper_->Fill(encoded_length, delta_frame);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000187 }
188
189 return VCM_OK;
190}
191
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000192void MediaOptimization::EnableFrameDropper(bool enable) {
kthelgasond701dfd2017-03-27 07:24:57 -0700193 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000194 frame_dropper_->Enable(enable);
195}
196
197bool MediaOptimization::DropFrame() {
kthelgasond701dfd2017-03-27 07:24:57 -0700198 rtc::CritScope lock(&crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000199 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000200 // Leak appropriate number of bytes.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000201 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000202 return frame_dropper_->DropFrame();
203}
204
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000205void MediaOptimization::UpdateIncomingFrameRate() {
206 int64_t now = clock_->TimeInMilliseconds();
207 if (incoming_frame_times_[0] == 0) {
208 // No shifting if this is the first time.
209 } else {
210 // Shift all times one step.
211 for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) {
212 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
213 }
214 }
215 incoming_frame_times_[0] = now;
216 ProcessIncomingFrameRate(now);
217}
218
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200219void MediaOptimization::PurgeOldFrameSamples(int64_t threshold_ms) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000220 while (!encoded_frame_samples_.empty()) {
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +0200221 if (encoded_frame_samples_.front().time_complete_ms < threshold_ms) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000222 encoded_frame_samples_.pop_front();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000223 } else {
224 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 }
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000226 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000227}
228
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000229void MediaOptimization::UpdateSentFramerate() {
230 if (encoded_frame_samples_.size() <= 1) {
231 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000232 return;
233 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000234 int denom = encoded_frame_samples_.back().timestamp -
235 encoded_frame_samples_.front().timestamp;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000236 if (denom > 0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000237 avg_sent_framerate_ =
238 (90000 * (encoded_frame_samples_.size() - 1) + denom / 2) / denom;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000239 } else {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000240 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000241 }
242}
niklase@google.com470e71d2011-07-07 08:21:25 +0000243
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000244// Allowing VCM to keep track of incoming frame rate.
245void MediaOptimization::ProcessIncomingFrameRate(int64_t now) {
246 int32_t num = 0;
247 int32_t nr_of_frames = 0;
248 for (num = 1; num < (kFrameCountHistorySize - 1); ++num) {
249 if (incoming_frame_times_[num] <= 0 ||
250 // don't use data older than 2 s
251 now - incoming_frame_times_[num] > kFrameHistoryWinMs) {
252 break;
253 } else {
254 nr_of_frames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000256 }
257 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200258 const int64_t diff =
259 incoming_frame_times_[0] - incoming_frame_times_[num - 1];
260 incoming_frame_rate_ = 0.0; // No frame rate estimate available.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000261 if (diff > 0) {
262 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000263 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000264 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000265}
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000266} // namespace media_optimization
267} // namespace webrtc