blob: ea70f3f47c5254a332a80974256f116854523e4a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/video_coding/utility/frame_dropper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
18namespace webrtc {
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000019namespace media_optimization {
Åsa Perssonb52a4d92017-11-08 11:33:37 +010020namespace {
21const int64_t kFrameHistoryWinMs = 2000;
22} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000024MediaOptimization::MediaOptimization(Clock* clock)
kthelgasond701dfd2017-03-27 07:24:57 -070025 : clock_(clock),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000026 max_bit_rate_(0),
Åsa Perssonb52a4d92017-11-08 11:33:37 +010027 max_frame_rate_(0),
henrik.lundin@webrtc.orgb426c462013-09-24 07:41:53 +000028 frame_dropper_(new FrameDropper),
Åsa Persson0122e842017-10-16 12:19:23 +020029 incoming_frame_rate_(0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000030 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
niklase@google.com470e71d2011-07-07 08:21:25 +000031}
32
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000033MediaOptimization::~MediaOptimization(void) {
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000036void MediaOptimization::Reset() {
kthelgasond701dfd2017-03-27 07:24:57 -070037 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000038 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
39 incoming_frame_rate_ = 0.0;
40 frame_dropper_->Reset();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000041 frame_dropper_->SetRates(0, 0);
Åsa Perssoneb450072017-11-13 10:24:22 +010042 max_bit_rate_ = 0;
Åsa Perssonb52a4d92017-11-08 11:33:37 +010043 max_frame_rate_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
Per69b332d2016-06-02 15:45:42 +020046void MediaOptimization::SetEncodingData(int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000047 uint32_t target_bitrate,
Åsa Perssonb52a4d92017-11-08 11:33:37 +010048 uint32_t max_frame_rate) {
kthelgasond701dfd2017-03-27 07:24:57 -070049 rtc::CritScope lock(&crit_sect_);
Åsa Perssoneb450072017-11-13 10:24:22 +010050 // Everything codec specific should be reset here since the codec has changed.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000051 max_bit_rate_ = max_bit_rate;
Åsa Perssonb52a4d92017-11-08 11:33:37 +010052 max_frame_rate_ = static_cast<float>(max_frame_rate);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000053 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000054 frame_dropper_->Reset();
Åsa Perssonb52a4d92017-11-08 11:33:37 +010055 frame_dropper_->SetRates(target_bitrate_kbps, max_frame_rate_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000056}
57
asapersson9abd2752016-12-08 02:19:40 -080058uint32_t MediaOptimization::SetTargetRates(uint32_t target_bitrate) {
kthelgasond701dfd2017-03-27 07:24:57 -070059 rtc::CritScope lock(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000060
pbos73674632015-10-29 15:45:00 -070061 // Cap target video bitrate to codec maximum.
Åsa Perssonb52a4d92017-11-08 11:33:37 +010062 int video_target_bitrate = target_bitrate;
63 if (max_bit_rate_ > 0 && video_target_bitrate > max_bit_rate_) {
64 video_target_bitrate = max_bit_rate_;
pbos73674632015-10-29 15:45:00 -070065 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000066 float target_video_bitrate_kbps =
Åsa Perssonb52a4d92017-11-08 11:33:37 +010067 static_cast<float>(video_target_bitrate) / 1000.0f;
Åsa Perssoneb450072017-11-13 10:24:22 +010068
sprang4847ae62017-06-27 07:06:52 -070069 float framerate = incoming_frame_rate_;
70 if (framerate == 0.0) {
71 // No framerate estimate available, use configured max framerate instead.
Åsa Perssonb52a4d92017-11-08 11:33:37 +010072 framerate = max_frame_rate_;
sprang4847ae62017-06-27 07:06:52 -070073 }
74
75 frame_dropper_->SetRates(target_video_bitrate_kbps, framerate);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000076
Åsa Perssonb52a4d92017-11-08 11:33:37 +010077 return video_target_bitrate;
niklase@google.com470e71d2011-07-07 08:21:25 +000078}
79
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000080uint32_t MediaOptimization::InputFrameRate() {
kthelgasond701dfd2017-03-27 07:24:57 -070081 rtc::CritScope lock(&crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000082 return InputFrameRateInternal();
83}
84
85uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000086 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
Stefan Holmer144475b2017-03-10 15:08:26 +010087 uint32_t framerate = static_cast<uint32_t>(std::min<float>(
88 std::numeric_limits<uint32_t>::max(), incoming_frame_rate_ + 0.5f));
89 return framerate;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000090}
91
Åsa Perssonb52a4d92017-11-08 11:33:37 +010092void MediaOptimization::UpdateWithEncodedData(
Ying Wang4f07bdb2018-02-15 17:06:32 +010093 const size_t encoded_image_length,
94 const FrameType encoded_image_frametype) {
95 size_t encoded_length = encoded_image_length;
kthelgasond701dfd2017-03-27 07:24:57 -070096 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000097 if (encoded_length > 0) {
Ying Wang4f07bdb2018-02-15 17:06:32 +010098 const bool delta_frame = encoded_image_frametype != kVideoFrameKey;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000099 frame_dropper_->Fill(encoded_length, delta_frame);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000100 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000101}
102
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000103void MediaOptimization::EnableFrameDropper(bool enable) {
kthelgasond701dfd2017-03-27 07:24:57 -0700104 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000105 frame_dropper_->Enable(enable);
106}
107
108bool MediaOptimization::DropFrame() {
kthelgasond701dfd2017-03-27 07:24:57 -0700109 rtc::CritScope lock(&crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000110 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000111 // Leak appropriate number of bytes.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000112 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000113 return frame_dropper_->DropFrame();
114}
115
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000116void MediaOptimization::UpdateIncomingFrameRate() {
117 int64_t now = clock_->TimeInMilliseconds();
118 if (incoming_frame_times_[0] == 0) {
119 // No shifting if this is the first time.
120 } else {
121 // Shift all times one step.
122 for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) {
123 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
124 }
125 }
126 incoming_frame_times_[0] = now;
127 ProcessIncomingFrameRate(now);
128}
129
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000130// Allowing VCM to keep track of incoming frame rate.
131void MediaOptimization::ProcessIncomingFrameRate(int64_t now) {
132 int32_t num = 0;
133 int32_t nr_of_frames = 0;
134 for (num = 1; num < (kFrameCountHistorySize - 1); ++num) {
135 if (incoming_frame_times_[num] <= 0 ||
asapersson60dfbdb2017-08-07 00:03:03 -0700136 // Don't use data older than 2 s.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000137 now - incoming_frame_times_[num] > kFrameHistoryWinMs) {
138 break;
139 } else {
140 nr_of_frames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000142 }
143 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200144 const int64_t diff =
145 incoming_frame_times_[0] - incoming_frame_times_[num - 1];
146 incoming_frame_rate_ = 0.0; // No frame rate estimate available.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000147 if (diff > 0) {
148 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000150 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000151}
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000152} // namespace media_optimization
153} // namespace webrtc