[Adaptation] Extract adaptation constraints to their own files

Bug: None
Change-Id: I55e72a725015d45608cffa46aedaa60d12bb3705
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/181582
Commit-Queue: Evan Shrubsole <eshr@google.com>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31927}
This commit is contained in:
Evan Shrubsole 2020-08-13 13:46:06 +02:00 committed by Commit Bot
parent b93ad75c94
commit 00ea6284a6
7 changed files with 257 additions and 181 deletions

View File

@ -10,6 +10,10 @@ import("../../webrtc.gni")
rtc_library("video_adaptation") {
sources = [
"balanced_constraint.cc",
"balanced_constraint.h",
"bitrate_constraint.cc",
"bitrate_constraint.h",
"encode_usage_resource.cc",
"encode_usage_resource.h",
"overuse_frame_detector.cc",

View File

@ -0,0 +1,65 @@
/*
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <string>
#include "rtc_base/task_utils/to_queued_task.h"
#include "video/adaptation/balanced_constraint.h"
namespace webrtc {
BalancedConstraint::BalancedConstraint(
DegradationPreferenceProvider* degradation_preference_provider)
: resource_adaptation_queue_(nullptr),
encoder_target_bitrate_bps_(absl::nullopt),
degradation_preference_provider_(degradation_preference_provider) {
RTC_DCHECK(degradation_preference_provider_);
}
void BalancedConstraint::SetAdaptationQueue(
TaskQueueBase* resource_adaptation_queue) {
resource_adaptation_queue_ = resource_adaptation_queue;
}
void BalancedConstraint::OnEncoderTargetBitrateUpdated(
absl::optional<uint32_t> encoder_target_bitrate_bps) {
resource_adaptation_queue_->PostTask(
ToQueuedTask([this_ref = rtc::scoped_refptr<BalancedConstraint>(this),
encoder_target_bitrate_bps] {
RTC_DCHECK_RUN_ON(this_ref->resource_adaptation_queue_);
this_ref->encoder_target_bitrate_bps_ = encoder_target_bitrate_bps;
}));
}
bool BalancedConstraint::IsAdaptationUpAllowed(
const VideoStreamInputState& input_state,
const VideoSourceRestrictions& restrictions_before,
const VideoSourceRestrictions& restrictions_after) const {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
// Don't adapt if BalancedDegradationSettings applies and determines this will
// exceed bitrate constraints.
if (degradation_preference_provider_->degradation_preference() ==
DegradationPreference::BALANCED &&
!balanced_settings_.CanAdaptUp(input_state.video_codec_type(),
input_state.frame_size_pixels().value(),
encoder_target_bitrate_bps_.value_or(0))) {
return false;
}
if (DidIncreaseResolution(restrictions_before, restrictions_after) &&
!balanced_settings_.CanAdaptUpResolution(
input_state.video_codec_type(),
input_state.frame_size_pixels().value(),
encoder_target_bitrate_bps_.value_or(0))) {
return false;
}
return true;
}
} // namespace webrtc

View File

@ -0,0 +1,52 @@
/*
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef VIDEO_ADAPTATION_BALANCED_CONSTRAINT_H_
#define VIDEO_ADAPTATION_BALANCED_CONSTRAINT_H_
#include <string>
#include "absl/types/optional.h"
#include "api/task_queue/task_queue_base.h"
#include "call/adaptation/adaptation_constraint.h"
#include "call/adaptation/degradation_preference_provider.h"
#include "rtc_base/experiments/balanced_degradation_settings.h"
namespace webrtc {
class BalancedConstraint : public rtc::RefCountInterface,
public AdaptationConstraint {
public:
explicit BalancedConstraint(
DegradationPreferenceProvider* degradation_preference_provider);
~BalancedConstraint() override = default;
void SetAdaptationQueue(TaskQueueBase* resource_adaptation_queue);
void OnEncoderTargetBitrateUpdated(
absl::optional<uint32_t> encoder_target_bitrate_bps);
// AdaptationConstraint implementation.
std::string Name() const override { return "BalancedConstraint"; }
bool IsAdaptationUpAllowed(
const VideoStreamInputState& input_state,
const VideoSourceRestrictions& restrictions_before,
const VideoSourceRestrictions& restrictions_after) const override;
private:
TaskQueueBase* resource_adaptation_queue_;
absl::optional<uint32_t> encoder_target_bitrate_bps_
RTC_GUARDED_BY(resource_adaptation_queue_);
BalancedDegradationSettings balanced_settings_;
DegradationPreferenceProvider* degradation_preference_provider_;
};
} // namespace webrtc
#endif // VIDEO_ADAPTATION_BALANCED_CONSTRAINT_H_

View File

@ -0,0 +1,77 @@
/*
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <string>
#include <utility>
#include "call/adaptation/video_stream_adapter.h"
#include "rtc_base/task_utils/to_queued_task.h"
#include "video/adaptation/bitrate_constraint.h"
namespace webrtc {
BitrateConstraint::BitrateConstraint()
: resource_adaptation_queue_(nullptr),
encoder_settings_(absl::nullopt),
encoder_target_bitrate_bps_(absl::nullopt) {}
void BitrateConstraint::SetAdaptationQueue(
TaskQueueBase* resource_adaptation_queue) {
resource_adaptation_queue_ = resource_adaptation_queue;
}
void BitrateConstraint::OnEncoderSettingsUpdated(
absl::optional<EncoderSettings> encoder_settings) {
resource_adaptation_queue_->PostTask(
ToQueuedTask([this_ref = rtc::scoped_refptr<BitrateConstraint>(this),
encoder_settings] {
RTC_DCHECK_RUN_ON(this_ref->resource_adaptation_queue_);
this_ref->encoder_settings_ = std::move(encoder_settings);
}));
}
void BitrateConstraint::OnEncoderTargetBitrateUpdated(
absl::optional<uint32_t> encoder_target_bitrate_bps) {
resource_adaptation_queue_->PostTask(
ToQueuedTask([this_ref = rtc::scoped_refptr<BitrateConstraint>(this),
encoder_target_bitrate_bps] {
RTC_DCHECK_RUN_ON(this_ref->resource_adaptation_queue_);
this_ref->encoder_target_bitrate_bps_ = encoder_target_bitrate_bps;
}));
}
bool BitrateConstraint::IsAdaptationUpAllowed(
const VideoStreamInputState& input_state,
const VideoSourceRestrictions& restrictions_before,
const VideoSourceRestrictions& restrictions_after) const {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
// Make sure bitrate limits are not violated.
if (DidIncreaseResolution(restrictions_before, restrictions_after)) {
uint32_t bitrate_bps = encoder_target_bitrate_bps_.value_or(0);
absl::optional<VideoEncoder::ResolutionBitrateLimits> bitrate_limits =
encoder_settings_.has_value()
? encoder_settings_->encoder_info()
.GetEncoderBitrateLimitsForResolution(
// Need some sort of expected resulting pixels to be used
// instead of unrestricted.
GetHigherResolutionThan(
input_state.frame_size_pixels().value()))
: absl::nullopt;
if (bitrate_limits.has_value() && bitrate_bps != 0) {
RTC_DCHECK_GE(bitrate_limits->frame_size_pixels,
input_state.frame_size_pixels().value());
return bitrate_bps >=
static_cast<uint32_t>(bitrate_limits->min_start_bitrate_bps);
}
}
return true;
}
} // namespace webrtc

View File

@ -0,0 +1,56 @@
/*
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef VIDEO_ADAPTATION_BITRATE_CONSTRAINT_H_
#define VIDEO_ADAPTATION_BITRATE_CONSTRAINT_H_
#include <string>
#include "absl/types/optional.h"
#include "api/task_queue/task_queue_base.h"
#include "call/adaptation/adaptation_constraint.h"
#include "call/adaptation/encoder_settings.h"
#include "call/adaptation/video_source_restrictions.h"
#include "call/adaptation/video_stream_input_state.h"
namespace webrtc {
class BitrateConstraint : public rtc::RefCountInterface,
public AdaptationConstraint {
public:
BitrateConstraint();
~BitrateConstraint() override = default;
void SetAdaptationQueue(TaskQueueBase* resource_adaptation_queue);
void OnEncoderSettingsUpdated(
absl::optional<EncoderSettings> encoder_settings);
void OnEncoderTargetBitrateUpdated(
absl::optional<uint32_t> encoder_target_bitrate_bps);
// AdaptationConstraint implementation.
std::string Name() const override { return "BitrateConstraint"; }
bool IsAdaptationUpAllowed(
const VideoStreamInputState& input_state,
const VideoSourceRestrictions& restrictions_before,
const VideoSourceRestrictions& restrictions_after) const override;
private:
// The |manager_| must be alive as long as this resource is added to the
// ResourceAdaptationProcessor, i.e. when IsAdaptationUpAllowed() is called.
TaskQueueBase* resource_adaptation_queue_;
absl::optional<EncoderSettings> encoder_settings_
RTC_GUARDED_BY(resource_adaptation_queue_);
absl::optional<uint32_t> encoder_target_bitrate_bps_
RTC_GUARDED_BY(resource_adaptation_queue_);
};
} // namespace webrtc
#endif // VIDEO_ADAPTATION_BITRATE_CONSTRAINT_H_

View File

@ -131,123 +131,6 @@ class VideoStreamEncoderResourceManager::InitialFrameDropper {
int initial_framedrop_;
};
VideoStreamEncoderResourceManager::BitrateConstraint::BitrateConstraint(
VideoStreamEncoderResourceManager* manager)
: manager_(manager),
resource_adaptation_queue_(nullptr),
encoder_settings_(absl::nullopt),
encoder_target_bitrate_bps_(absl::nullopt) {}
void VideoStreamEncoderResourceManager::BitrateConstraint::SetAdaptationQueue(
TaskQueueBase* resource_adaptation_queue) {
resource_adaptation_queue_ = resource_adaptation_queue;
}
void VideoStreamEncoderResourceManager::BitrateConstraint::
OnEncoderSettingsUpdated(absl::optional<EncoderSettings> encoder_settings) {
RTC_DCHECK_RUN_ON(manager_->encoder_queue_);
resource_adaptation_queue_->PostTask(
ToQueuedTask([this_ref = rtc::scoped_refptr<BitrateConstraint>(this),
encoder_settings] {
RTC_DCHECK_RUN_ON(this_ref->resource_adaptation_queue_);
this_ref->encoder_settings_ = std::move(encoder_settings);
}));
}
void VideoStreamEncoderResourceManager::BitrateConstraint::
OnEncoderTargetBitrateUpdated(
absl::optional<uint32_t> encoder_target_bitrate_bps) {
RTC_DCHECK_RUN_ON(manager_->encoder_queue_);
resource_adaptation_queue_->PostTask(
ToQueuedTask([this_ref = rtc::scoped_refptr<BitrateConstraint>(this),
encoder_target_bitrate_bps] {
RTC_DCHECK_RUN_ON(this_ref->resource_adaptation_queue_);
this_ref->encoder_target_bitrate_bps_ = encoder_target_bitrate_bps;
}));
}
bool VideoStreamEncoderResourceManager::BitrateConstraint::
IsAdaptationUpAllowed(
const VideoStreamInputState& input_state,
const VideoSourceRestrictions& restrictions_before,
const VideoSourceRestrictions& restrictions_after) const {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
// Make sure bitrate limits are not violated.
if (DidIncreaseResolution(restrictions_before, restrictions_after)) {
uint32_t bitrate_bps = encoder_target_bitrate_bps_.value_or(0);
absl::optional<VideoEncoder::ResolutionBitrateLimits> bitrate_limits =
encoder_settings_.has_value()
? encoder_settings_->encoder_info()
.GetEncoderBitrateLimitsForResolution(
// Need some sort of expected resulting pixels to be used
// instead of unrestricted.
GetHigherResolutionThan(
input_state.frame_size_pixels().value()))
: absl::nullopt;
if (bitrate_limits.has_value() && bitrate_bps != 0) {
RTC_DCHECK_GE(bitrate_limits->frame_size_pixels,
input_state.frame_size_pixels().value());
return bitrate_bps >=
static_cast<uint32_t>(bitrate_limits->min_start_bitrate_bps);
}
}
return true;
}
VideoStreamEncoderResourceManager::BalancedConstraint::BalancedConstraint(
VideoStreamEncoderResourceManager* manager,
DegradationPreferenceProvider* degradation_preference_provider)
: manager_(manager),
resource_adaptation_queue_(nullptr),
encoder_target_bitrate_bps_(absl::nullopt),
degradation_preference_provider_(degradation_preference_provider) {
RTC_DCHECK(manager_);
RTC_DCHECK(degradation_preference_provider_);
}
void VideoStreamEncoderResourceManager::BalancedConstraint::SetAdaptationQueue(
TaskQueueBase* resource_adaptation_queue) {
resource_adaptation_queue_ = resource_adaptation_queue;
}
void VideoStreamEncoderResourceManager::BalancedConstraint::
OnEncoderTargetBitrateUpdated(
absl::optional<uint32_t> encoder_target_bitrate_bps) {
RTC_DCHECK_RUN_ON(manager_->encoder_queue_);
resource_adaptation_queue_->PostTask(
ToQueuedTask([this_ref = rtc::scoped_refptr<BalancedConstraint>(this),
encoder_target_bitrate_bps] {
RTC_DCHECK_RUN_ON(this_ref->resource_adaptation_queue_);
this_ref->encoder_target_bitrate_bps_ = encoder_target_bitrate_bps;
}));
}
bool VideoStreamEncoderResourceManager::BalancedConstraint::
IsAdaptationUpAllowed(
const VideoStreamInputState& input_state,
const VideoSourceRestrictions& restrictions_before,
const VideoSourceRestrictions& restrictions_after) const {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
// Don't adapt if BalancedDegradationSettings applies and determines this will
// exceed bitrate constraints.
if (degradation_preference_provider_->degradation_preference() ==
DegradationPreference::BALANCED &&
!manager_->balanced_settings_.CanAdaptUp(
input_state.video_codec_type(),
input_state.frame_size_pixels().value(),
encoder_target_bitrate_bps_.value_or(0))) {
return false;
}
if (DidIncreaseResolution(restrictions_before, restrictions_after) &&
!manager_->balanced_settings_.CanAdaptUpResolution(
input_state.video_codec_type(),
input_state.frame_size_pixels().value(),
encoder_target_bitrate_bps_.value_or(0))) {
return false;
}
return true;
}
VideoStreamEncoderResourceManager::VideoStreamEncoderResourceManager(
VideoStreamInputStateProvider* input_state_provider,
VideoStreamEncoderObserver* encoder_stats_observer,
@ -256,9 +139,8 @@ VideoStreamEncoderResourceManager::VideoStreamEncoderResourceManager(
std::unique_ptr<OveruseFrameDetector> overuse_detector,
DegradationPreferenceProvider* degradation_preference_provider)
: degradation_preference_provider_(degradation_preference_provider),
bitrate_constraint_(new rtc::RefCountedObject<BitrateConstraint>(this)),
bitrate_constraint_(new rtc::RefCountedObject<BitrateConstraint>()),
balanced_constraint_(new rtc::RefCountedObject<BalancedConstraint>(
this,
degradation_preference_provider_)),
encode_usage_resource_(
EncodeUsageResource::Create(std::move(overuse_detector))),

View File

@ -42,6 +42,8 @@
#include "rtc_base/task_queue.h"
#include "rtc_base/thread_annotations.h"
#include "system_wrappers/include/clock.h"
#include "video/adaptation/balanced_constraint.h"
#include "video/adaptation/bitrate_constraint.h"
#include "video/adaptation/encode_usage_resource.h"
#include "video/adaptation/overuse_frame_detector.h"
#include "video/adaptation/quality_rampup_experiment_helper.h"
@ -162,68 +164,6 @@ class VideoStreamEncoderResourceManager
const std::map<VideoAdaptationReason, VideoAdaptationCounters>&
active_counts);
// TODO(hbos): Add tests for manager's constraints.
// Does not trigger adaptations, only prevents adapting up resolution.
class BitrateConstraint : public rtc::RefCountInterface,
public AdaptationConstraint {
public:
explicit BitrateConstraint(VideoStreamEncoderResourceManager* manager);
~BitrateConstraint() override = default;
void SetAdaptationQueue(TaskQueueBase* resource_adaptation_queue);
void OnEncoderSettingsUpdated(
absl::optional<EncoderSettings> encoder_settings);
void OnEncoderTargetBitrateUpdated(
absl::optional<uint32_t> encoder_target_bitrate_bps);
// AdaptationConstraint implementation.
std::string Name() const override { return "BitrateConstraint"; }
bool IsAdaptationUpAllowed(
const VideoStreamInputState& input_state,
const VideoSourceRestrictions& restrictions_before,
const VideoSourceRestrictions& restrictions_after) const override;
private:
// The |manager_| must be alive as long as this resource is added to the
// ResourceAdaptationProcessor, i.e. when IsAdaptationUpAllowed() is called.
VideoStreamEncoderResourceManager* const manager_;
TaskQueueBase* resource_adaptation_queue_;
absl::optional<EncoderSettings> encoder_settings_
RTC_GUARDED_BY(resource_adaptation_queue_);
absl::optional<uint32_t> encoder_target_bitrate_bps_
RTC_GUARDED_BY(resource_adaptation_queue_);
};
// Does not trigger adaptations, only prevents adapting up in BALANCED.
class BalancedConstraint : public rtc::RefCountInterface,
public AdaptationConstraint {
public:
BalancedConstraint(
VideoStreamEncoderResourceManager* manager,
DegradationPreferenceProvider* degradation_preference_provider);
~BalancedConstraint() override = default;
void SetAdaptationQueue(TaskQueueBase* resource_adaptation_queue);
void OnEncoderTargetBitrateUpdated(
absl::optional<uint32_t> encoder_target_bitrate_bps);
// AdaptationConstraint implementation.
std::string Name() const override { return "BalancedConstraint"; }
bool IsAdaptationUpAllowed(
const VideoStreamInputState& input_state,
const VideoSourceRestrictions& restrictions_before,
const VideoSourceRestrictions& restrictions_after) const override;
private:
// The |manager_| must be alive as long as this resource is added to the
// ResourceAdaptationProcessor, i.e. when IsAdaptationUpAllowed() is called.
VideoStreamEncoderResourceManager* const manager_;
TaskQueueBase* resource_adaptation_queue_;
absl::optional<uint32_t> encoder_target_bitrate_bps_
RTC_GUARDED_BY(resource_adaptation_queue_);
DegradationPreferenceProvider* degradation_preference_provider_;
};
DegradationPreferenceProvider* const degradation_preference_provider_;
const rtc::scoped_refptr<BitrateConstraint> bitrate_constraint_;
const rtc::scoped_refptr<BalancedConstraint> balanced_constraint_;