blob: 5433edbab7742eafee0dd3c2066c56434997bdb5 [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
Yves Gerey665174f2018-06-19 15:03:05 +020033MediaOptimization::~MediaOptimization(void) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000034
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000035void MediaOptimization::Reset() {
kthelgasond701dfd2017-03-27 07:24:57 -070036 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000037 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
38 incoming_frame_rate_ = 0.0;
39 frame_dropper_->Reset();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000040 frame_dropper_->SetRates(0, 0);
Åsa Perssoneb450072017-11-13 10:24:22 +010041 max_bit_rate_ = 0;
Åsa Perssonb52a4d92017-11-08 11:33:37 +010042 max_frame_rate_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000043}
44
Per69b332d2016-06-02 15:45:42 +020045void MediaOptimization::SetEncodingData(int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000046 uint32_t target_bitrate,
Åsa Perssonb52a4d92017-11-08 11:33:37 +010047 uint32_t max_frame_rate) {
kthelgasond701dfd2017-03-27 07:24:57 -070048 rtc::CritScope lock(&crit_sect_);
Åsa Perssoneb450072017-11-13 10:24:22 +010049 // Everything codec specific should be reset here since the codec has changed.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000050 max_bit_rate_ = max_bit_rate;
Åsa Perssonb52a4d92017-11-08 11:33:37 +010051 max_frame_rate_ = static_cast<float>(max_frame_rate);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000052 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000053 frame_dropper_->Reset();
Åsa Perssonb52a4d92017-11-08 11:33:37 +010054 frame_dropper_->SetRates(target_bitrate_kbps, max_frame_rate_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000055}
56
asapersson9abd2752016-12-08 02:19:40 -080057uint32_t MediaOptimization::SetTargetRates(uint32_t target_bitrate) {
kthelgasond701dfd2017-03-27 07:24:57 -070058 rtc::CritScope lock(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000059
pbos73674632015-10-29 15:45:00 -070060 // Cap target video bitrate to codec maximum.
Åsa Perssonb52a4d92017-11-08 11:33:37 +010061 int video_target_bitrate = target_bitrate;
62 if (max_bit_rate_ > 0 && video_target_bitrate > max_bit_rate_) {
63 video_target_bitrate = max_bit_rate_;
pbos73674632015-10-29 15:45:00 -070064 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000065 float target_video_bitrate_kbps =
Åsa Perssonb52a4d92017-11-08 11:33:37 +010066 static_cast<float>(video_target_bitrate) / 1000.0f;
Åsa Perssoneb450072017-11-13 10:24:22 +010067
sprang4847ae62017-06-27 07:06:52 -070068 float framerate = incoming_frame_rate_;
69 if (framerate == 0.0) {
70 // No framerate estimate available, use configured max framerate instead.
Åsa Perssonb52a4d92017-11-08 11:33:37 +010071 framerate = max_frame_rate_;
sprang4847ae62017-06-27 07:06:52 -070072 }
73
74 frame_dropper_->SetRates(target_video_bitrate_kbps, framerate);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000075
Åsa Perssonb52a4d92017-11-08 11:33:37 +010076 return video_target_bitrate;
niklase@google.com470e71d2011-07-07 08:21:25 +000077}
78
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000079uint32_t MediaOptimization::InputFrameRate() {
kthelgasond701dfd2017-03-27 07:24:57 -070080 rtc::CritScope lock(&crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000081 return InputFrameRateInternal();
82}
83
84uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000085 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
Stefan Holmer144475b2017-03-10 15:08:26 +010086 uint32_t framerate = static_cast<uint32_t>(std::min<float>(
87 std::numeric_limits<uint32_t>::max(), incoming_frame_rate_ + 0.5f));
88 return framerate;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000089}
90
Åsa Perssonb52a4d92017-11-08 11:33:37 +010091void MediaOptimization::UpdateWithEncodedData(
Ying Wang0dd1b0a2018-02-20 12:50:27 +010092 const size_t encoded_image_length,
93 const FrameType encoded_image_frametype) {
94 size_t encoded_length = encoded_image_length;
kthelgasond701dfd2017-03-27 07:24:57 -070095 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000096 if (encoded_length > 0) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +010097 const bool delta_frame = encoded_image_frametype != kVideoFrameKey;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000098 frame_dropper_->Fill(encoded_length, delta_frame);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000099 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000100}
101
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000102void MediaOptimization::EnableFrameDropper(bool enable) {
kthelgasond701dfd2017-03-27 07:24:57 -0700103 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000104 frame_dropper_->Enable(enable);
105}
106
107bool MediaOptimization::DropFrame() {
kthelgasond701dfd2017-03-27 07:24:57 -0700108 rtc::CritScope lock(&crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000109 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000110 // Leak appropriate number of bytes.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000111 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000112 return frame_dropper_->DropFrame();
113}
114
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000115void MediaOptimization::UpdateIncomingFrameRate() {
116 int64_t now = clock_->TimeInMilliseconds();
117 if (incoming_frame_times_[0] == 0) {
118 // No shifting if this is the first time.
119 } else {
120 // Shift all times one step.
121 for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) {
122 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
123 }
124 }
125 incoming_frame_times_[0] = now;
126 ProcessIncomingFrameRate(now);
127}
128
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000129// Allowing VCM to keep track of incoming frame rate.
130void MediaOptimization::ProcessIncomingFrameRate(int64_t now) {
131 int32_t num = 0;
132 int32_t nr_of_frames = 0;
133 for (num = 1; num < (kFrameCountHistorySize - 1); ++num) {
134 if (incoming_frame_times_[num] <= 0 ||
asapersson60dfbdb2017-08-07 00:03:03 -0700135 // Don't use data older than 2 s.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000136 now - incoming_frame_times_[num] > kFrameHistoryWinMs) {
137 break;
138 } else {
139 nr_of_frames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000141 }
142 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200143 const int64_t diff =
144 incoming_frame_times_[0] - incoming_frame_times_[num - 1];
145 incoming_frame_rate_ = 0.0; // No frame rate estimate available.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000146 if (diff > 0) {
147 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000149 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000150}
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000151} // namespace media_optimization
152} // namespace webrtc