blob: 2c1e50e89b5a824e02f9714dae9366582557cab7 [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),
Rasmus Brandt3613fef2018-09-03 16:19:29 +020028 frame_dropper_(),
Å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_));
Rasmus Brandt3613fef2018-09-03 16:19:29 +020031 frame_dropper_.SetRates(0, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
Rasmus Brandt3613fef2018-09-03 16:19:29 +020034MediaOptimization::~MediaOptimization() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000035
Per69b332d2016-06-02 15:45:42 +020036void MediaOptimization::SetEncodingData(int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000037 uint32_t target_bitrate,
Åsa Perssonb52a4d92017-11-08 11:33:37 +010038 uint32_t max_frame_rate) {
kthelgasond701dfd2017-03-27 07:24:57 -070039 rtc::CritScope lock(&crit_sect_);
Åsa Perssoneb450072017-11-13 10:24:22 +010040 // Everything codec specific should be reset here since the codec has changed.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000041 max_bit_rate_ = max_bit_rate;
Åsa Perssonb52a4d92017-11-08 11:33:37 +010042 max_frame_rate_ = static_cast<float>(max_frame_rate);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000043 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
Rasmus Brandt3613fef2018-09-03 16:19:29 +020044 frame_dropper_.Reset();
45 frame_dropper_.SetRates(target_bitrate_kbps, max_frame_rate_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000046}
47
asapersson9abd2752016-12-08 02:19:40 -080048uint32_t MediaOptimization::SetTargetRates(uint32_t target_bitrate) {
kthelgasond701dfd2017-03-27 07:24:57 -070049 rtc::CritScope lock(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000050
pbos73674632015-10-29 15:45:00 -070051 // Cap target video bitrate to codec maximum.
Åsa Perssonb52a4d92017-11-08 11:33:37 +010052 int video_target_bitrate = target_bitrate;
53 if (max_bit_rate_ > 0 && video_target_bitrate > max_bit_rate_) {
54 video_target_bitrate = max_bit_rate_;
pbos73674632015-10-29 15:45:00 -070055 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000056 float target_video_bitrate_kbps =
Åsa Perssonb52a4d92017-11-08 11:33:37 +010057 static_cast<float>(video_target_bitrate) / 1000.0f;
Åsa Perssoneb450072017-11-13 10:24:22 +010058
sprang4847ae62017-06-27 07:06:52 -070059 float framerate = incoming_frame_rate_;
60 if (framerate == 0.0) {
61 // No framerate estimate available, use configured max framerate instead.
Åsa Perssonb52a4d92017-11-08 11:33:37 +010062 framerate = max_frame_rate_;
sprang4847ae62017-06-27 07:06:52 -070063 }
64
Rasmus Brandt3613fef2018-09-03 16:19:29 +020065 frame_dropper_.SetRates(target_video_bitrate_kbps, framerate);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000066
Åsa Perssonb52a4d92017-11-08 11:33:37 +010067 return video_target_bitrate;
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000070uint32_t MediaOptimization::InputFrameRate() {
kthelgasond701dfd2017-03-27 07:24:57 -070071 rtc::CritScope lock(&crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000072 return InputFrameRateInternal();
73}
74
75uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000076 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
Stefan Holmer144475b2017-03-10 15:08:26 +010077 uint32_t framerate = static_cast<uint32_t>(std::min<float>(
78 std::numeric_limits<uint32_t>::max(), incoming_frame_rate_ + 0.5f));
79 return framerate;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000080}
81
Åsa Perssonb52a4d92017-11-08 11:33:37 +010082void MediaOptimization::UpdateWithEncodedData(
Ying Wang0dd1b0a2018-02-20 12:50:27 +010083 const size_t encoded_image_length,
84 const FrameType encoded_image_frametype) {
85 size_t encoded_length = encoded_image_length;
kthelgasond701dfd2017-03-27 07:24:57 -070086 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000087 if (encoded_length > 0) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +010088 const bool delta_frame = encoded_image_frametype != kVideoFrameKey;
Rasmus Brandt3613fef2018-09-03 16:19:29 +020089 frame_dropper_.Fill(encoded_length, delta_frame);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000090 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000091}
92
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000093void MediaOptimization::EnableFrameDropper(bool enable) {
kthelgasond701dfd2017-03-27 07:24:57 -070094 rtc::CritScope lock(&crit_sect_);
Rasmus Brandt3613fef2018-09-03 16:19:29 +020095 frame_dropper_.Enable(enable);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000096}
97
98bool MediaOptimization::DropFrame() {
kthelgasond701dfd2017-03-27 07:24:57 -070099 rtc::CritScope lock(&crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000100 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000101 // Leak appropriate number of bytes.
Rasmus Brandt3613fef2018-09-03 16:19:29 +0200102 frame_dropper_.Leak(static_cast<uint32_t>(InputFrameRateInternal() + 0.5f));
103 return frame_dropper_.DropFrame();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000104}
105
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000106void MediaOptimization::UpdateIncomingFrameRate() {
107 int64_t now = clock_->TimeInMilliseconds();
108 if (incoming_frame_times_[0] == 0) {
109 // No shifting if this is the first time.
110 } else {
111 // Shift all times one step.
112 for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) {
113 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
114 }
115 }
116 incoming_frame_times_[0] = now;
117 ProcessIncomingFrameRate(now);
118}
119
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000120// Allowing VCM to keep track of incoming frame rate.
121void MediaOptimization::ProcessIncomingFrameRate(int64_t now) {
122 int32_t num = 0;
123 int32_t nr_of_frames = 0;
124 for (num = 1; num < (kFrameCountHistorySize - 1); ++num) {
125 if (incoming_frame_times_[num] <= 0 ||
asapersson60dfbdb2017-08-07 00:03:03 -0700126 // Don't use data older than 2 s.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000127 now - incoming_frame_times_[num] > kFrameHistoryWinMs) {
128 break;
129 } else {
130 nr_of_frames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000132 }
133 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200134 const int64_t diff =
135 incoming_frame_times_[0] - incoming_frame_times_[num - 1];
136 incoming_frame_rate_ = 0.0; // No frame rate estimate available.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000137 if (diff > 0) {
138 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000140 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000142} // namespace media_optimization
143} // namespace webrtc