blob: fce1be5f0faa5714d4f96fee46efc4ad01410847 [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 {
niklase@google.com470e71d2011-07-07 08:21:25 +000020
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000021MediaOptimization::MediaOptimization(Clock* clock)
kthelgasond701dfd2017-03-27 07:24:57 -070022 : clock_(clock),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000023 max_bit_rate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000024 user_frame_rate_(0),
henrik.lundin@webrtc.orgb426c462013-09-24 07:41:53 +000025 frame_dropper_(new FrameDropper),
pbos73674632015-10-29 15:45:00 -070026 video_target_bitrate_(0),
Åsa Persson0122e842017-10-16 12:19:23 +020027 incoming_frame_rate_(0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000028 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
niklase@google.com470e71d2011-07-07 08:21:25 +000029}
30
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000031MediaOptimization::~MediaOptimization(void) {
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000034void MediaOptimization::Reset() {
kthelgasond701dfd2017-03-27 07:24:57 -070035 rtc::CritScope lock(&crit_sect_);
asapersson60dfbdb2017-08-07 00:03:03 -070036 SetEncodingDataInternal(0, 0, 0);
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);
pbos73674632015-10-29 15:45:00 -070041 video_target_bitrate_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000042 user_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,
asapersson60dfbdb2017-08-07 00:03:03 -070047 uint32_t frame_rate) {
kthelgasond701dfd2017-03-27 07:24:57 -070048 rtc::CritScope lock(&crit_sect_);
asapersson60dfbdb2017-08-07 00:03:03 -070049 SetEncodingDataInternal(max_bit_rate, frame_rate, target_bitrate);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000050}
51
Per69b332d2016-06-02 15:45:42 +020052void MediaOptimization::SetEncodingDataInternal(int32_t max_bit_rate,
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000053 uint32_t frame_rate,
asapersson60dfbdb2017-08-07 00:03:03 -070054 uint32_t target_bitrate) {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000055 // Everything codec specific should be reset here since this means the codec
Peter Boströmad6fc5a2016-05-12 03:01:31 +020056 // has changed.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000057 max_bit_rate_ = max_bit_rate;
pbos73674632015-10-29 15:45:00 -070058 video_target_bitrate_ = target_bitrate;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000059 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000060 frame_dropper_->Reset();
61 frame_dropper_->SetRates(target_bitrate_kbps, static_cast<float>(frame_rate));
62 user_frame_rate_ = static_cast<float>(frame_rate);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000063}
64
asapersson9abd2752016-12-08 02:19:40 -080065uint32_t MediaOptimization::SetTargetRates(uint32_t target_bitrate) {
kthelgasond701dfd2017-03-27 07:24:57 -070066 rtc::CritScope lock(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
Per69b332d2016-06-02 15:45:42 +020068 video_target_bitrate_ = target_bitrate;
pbos73674632015-10-29 15:45:00 -070069
70 // Cap target video bitrate to codec maximum.
71 if (max_bit_rate_ > 0 && video_target_bitrate_ > max_bit_rate_) {
72 video_target_bitrate_ = max_bit_rate_;
73 }
niklase@google.com470e71d2011-07-07 08:21:25 +000074
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000075 // Update encoding rates following protection settings.
76 float target_video_bitrate_kbps =
pbos73674632015-10-29 15:45:00 -070077 static_cast<float>(video_target_bitrate_) / 1000.0f;
sprang4847ae62017-06-27 07:06:52 -070078 float framerate = incoming_frame_rate_;
79 if (framerate == 0.0) {
80 // No framerate estimate available, use configured max framerate instead.
81 framerate = user_frame_rate_;
82 }
83
84 frame_dropper_->SetRates(target_video_bitrate_kbps, framerate);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000085
pbos73674632015-10-29 15:45:00 -070086 return video_target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000089uint32_t MediaOptimization::InputFrameRate() {
kthelgasond701dfd2017-03-27 07:24:57 -070090 rtc::CritScope lock(&crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000091 return InputFrameRateInternal();
92}
93
94uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000095 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
Stefan Holmer144475b2017-03-10 15:08:26 +010096 uint32_t framerate = static_cast<uint32_t>(std::min<float>(
97 std::numeric_limits<uint32_t>::max(), incoming_frame_rate_ + 0.5f));
98 return framerate;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000099}
100
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000101int32_t MediaOptimization::UpdateWithEncodedData(
102 const EncodedImage& encoded_image) {
103 size_t encoded_length = encoded_image._length;
kthelgasond701dfd2017-03-27 07:24:57 -0700104 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000105 if (encoded_length > 0) {
Peter Boström49e196a2015-10-23 15:58:18 +0200106 const bool delta_frame = encoded_image._frameType != kVideoFrameKey;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000107 frame_dropper_->Fill(encoded_length, delta_frame);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000108 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000109 return VCM_OK;
110}
111
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000112void MediaOptimization::EnableFrameDropper(bool enable) {
kthelgasond701dfd2017-03-27 07:24:57 -0700113 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000114 frame_dropper_->Enable(enable);
115}
116
117bool MediaOptimization::DropFrame() {
kthelgasond701dfd2017-03-27 07:24:57 -0700118 rtc::CritScope lock(&crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000119 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000120 // Leak appropriate number of bytes.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000121 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000122 return frame_dropper_->DropFrame();
123}
124
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000125void MediaOptimization::UpdateIncomingFrameRate() {
126 int64_t now = clock_->TimeInMilliseconds();
127 if (incoming_frame_times_[0] == 0) {
128 // No shifting if this is the first time.
129 } else {
130 // Shift all times one step.
131 for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) {
132 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
133 }
134 }
135 incoming_frame_times_[0] = now;
136 ProcessIncomingFrameRate(now);
137}
138
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000139// Allowing VCM to keep track of incoming frame rate.
140void MediaOptimization::ProcessIncomingFrameRate(int64_t now) {
141 int32_t num = 0;
142 int32_t nr_of_frames = 0;
143 for (num = 1; num < (kFrameCountHistorySize - 1); ++num) {
144 if (incoming_frame_times_[num] <= 0 ||
asapersson60dfbdb2017-08-07 00:03:03 -0700145 // Don't use data older than 2 s.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000146 now - incoming_frame_times_[num] > kFrameHistoryWinMs) {
147 break;
148 } else {
149 nr_of_frames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000151 }
152 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200153 const int64_t diff =
154 incoming_frame_times_[0] - incoming_frame_times_[num - 1];
155 incoming_frame_rate_ = 0.0; // No frame rate estimate available.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000156 if (diff > 0) {
157 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000159 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000160}
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000161} // namespace media_optimization
162} // namespace webrtc