Skip to content

Commit

Permalink
[Adaptation] Move deg.pref. out of ResourceAdaptationProcessor
Browse files Browse the repository at this point in the history
This patch creates a new class which provides the DegradationPreference
thread safe to all classes that need if (BalancedConstraint and
QpScalerResource). It also broadcasts to all listeners when the
preferences are updated, so the ResourceAdaptationProcessor can update
the video if needed.

In future work, this could be used to remove the need for two task
queues for the VideoStreamEncoder resources.

Bug: webrtc:11700
Change-Id: I05480db8b7157b5643f6f86ec9c64850839b3e76
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/177522
Commit-Queue: Evan Shrubsole <[email protected]>
Reviewed-by: Ilya Nikolaevskiy <[email protected]>
Reviewed-by: Henrik Boström <[email protected]>
Cr-Commit-Position: refs/heads/master@{#31623}
  • Loading branch information
eshrubs authored and Commit Bot committed Jul 3, 2020
1 parent db1c81d commit 9492d50
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 160 deletions.
4 changes: 4 additions & 0 deletions call/adaptation/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ rtc_library("resource_adaptation") {
"adaptation_listener.h",
"broadcast_resource_listener.cc",
"broadcast_resource_listener.h",
"degradation_preference_listener.cc",
"degradation_preference_listener.h",
"degradation_preference_provider.cc",
"degradation_preference_provider.h",
"encoder_settings.cc",
"encoder_settings.h",
"resource_adaptation_processor.cc",
Expand Down
17 changes: 17 additions & 0 deletions call/adaptation/degradation_preference_listener.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 "call/adaptation/degradation_preference_listener.h"

namespace webrtc {

DegradationPreferenceListener::~DegradationPreferenceListener() = default;

} // namespace webrtc
28 changes: 28 additions & 0 deletions call/adaptation/degradation_preference_listener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 CALL_ADAPTATION_DEGRADATION_PREFERENCE_LISTENER_H_
#define CALL_ADAPTATION_DEGRADATION_PREFERENCE_LISTENER_H_

#include "api/rtp_parameters.h"

namespace webrtc {

class DegradationPreferenceListener {
public:
virtual ~DegradationPreferenceListener();

virtual void OnDegradationPreferenceUpdated(
DegradationPreference degradation_preference) = 0;
};

} // namespace webrtc

#endif // CALL_ADAPTATION_DEGRADATION_PREFERENCE_LISTENER_H_
14 changes: 14 additions & 0 deletions call/adaptation/degradation_preference_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 "call/adaptation/degradation_preference_provider.h"

webrtc::DegradationPreferenceProvider::~DegradationPreferenceProvider() =
default;
28 changes: 28 additions & 0 deletions call/adaptation/degradation_preference_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 CALL_ADAPTATION_DEGRADATION_PREFERENCE_PROVIDER_H_
#define CALL_ADAPTATION_DEGRADATION_PREFERENCE_PROVIDER_H_

#include "api/rtp_parameters.h"

namespace webrtc {

// Thread-safe retrieval of degradation preferences.
class DegradationPreferenceProvider {
public:
virtual ~DegradationPreferenceProvider();

virtual DegradationPreference degradation_preference() const = 0;
};

} // namespace webrtc

#endif // CALL_ADAPTATION_DEGRADATION_PREFERENCE_PROVIDER_H_
46 changes: 13 additions & 33 deletions call/adaptation/resource_adaptation_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ ResourceAdaptationProcessor::ResourceAdaptationProcessor(
new rtc::RefCountedObject<ResourceListenerDelegate>(this)),
encoder_stats_observer_(encoder_stats_observer),
resources_(),
degradation_preference_(DegradationPreference::DISABLED),
effective_degradation_preference_(DegradationPreference::DISABLED),
is_screenshare_(false),
stream_adapter_(stream_adapter),
last_reported_source_restrictions_(),
previous_mitigation_results_(),
Expand Down Expand Up @@ -112,18 +110,6 @@ void ResourceAdaptationProcessor::SetResourceAdaptationQueue(
stream_adapter_->AddRestrictionsListener(this);
}

DegradationPreference ResourceAdaptationProcessor::degradation_preference()
const {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
return degradation_preference_;
}

DegradationPreference
ResourceAdaptationProcessor::effective_degradation_preference() const {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
return effective_degradation_preference_;
}

void ResourceAdaptationProcessor::AddResourceLimitationsListener(
ResourceLimitationsListener* limitations_listener) {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
Expand Down Expand Up @@ -215,26 +201,20 @@ void ResourceAdaptationProcessor::RemoveAdaptationListener(
adaptation_listeners_.erase(it);
}

void ResourceAdaptationProcessor::SetDegradationPreference(
void ResourceAdaptationProcessor::OnDegradationPreferenceUpdated(
DegradationPreference degradation_preference) {
if (!resource_adaptation_queue_->IsCurrent()) {
resource_adaptation_queue_->PostTask(
ToQueuedTask([this, degradation_preference]() {
OnDegradationPreferenceUpdated(degradation_preference);
}));
return;
}
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
degradation_preference_ = degradation_preference;
MaybeUpdateEffectiveDegradationPreference();
}

void ResourceAdaptationProcessor::SetIsScreenshare(bool is_screenshare) {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
is_screenshare_ = is_screenshare;
MaybeUpdateEffectiveDegradationPreference();
}

void ResourceAdaptationProcessor::MaybeUpdateEffectiveDegradationPreference() {
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
effective_degradation_preference_ =
(is_screenshare_ &&
degradation_preference_ == DegradationPreference::BALANCED)
? DegradationPreference::MAINTAIN_RESOLUTION
: degradation_preference_;
if (degradation_preference == effective_degradation_preference_) {
return;
}
effective_degradation_preference_ = degradation_preference;
stream_adapter_->SetDegradationPreference(effective_degradation_preference_);
}

Expand Down Expand Up @@ -419,7 +399,7 @@ void ResourceAdaptationProcessor::TriggerAdaptationDueToFrameDroppedDueToSize(
VideoAdaptationCounters counters_before =
stream_adapter_->adaptation_counters();
OnResourceOveruse(reason_resource);
if (degradation_preference_ == DegradationPreference::BALANCED &&
if (effective_degradation_preference_ == DegradationPreference::BALANCED &&
stream_adapter_->adaptation_counters().fps_adaptations >
counters_before.fps_adaptations) {
// Oops, we adapted frame rate. Adapt again, maybe it will adapt resolution!
Expand Down
17 changes: 6 additions & 11 deletions call/adaptation/resource_adaptation_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "api/video/video_stream_encoder_observer.h"
#include "call/adaptation/adaptation_constraint.h"
#include "call/adaptation/adaptation_listener.h"
#include "call/adaptation/degradation_preference_listener.h"
#include "call/adaptation/resource_adaptation_processor_interface.h"
#include "call/adaptation/video_source_restrictions.h"
#include "call/adaptation/video_stream_adapter.h"
Expand All @@ -52,7 +53,8 @@ namespace webrtc {
// i.e. the "resource adaptation task queue".
class ResourceAdaptationProcessor : public ResourceAdaptationProcessorInterface,
public VideoSourceRestrictionsListener,
public ResourceListener {
public ResourceListener,
public DegradationPreferenceListener {
public:
ResourceAdaptationProcessor(
VideoStreamEncoderObserver* encoder_stats_observer,
Expand All @@ -63,9 +65,6 @@ class ResourceAdaptationProcessor : public ResourceAdaptationProcessorInterface,
TaskQueueBase* resource_adaptation_queue) override;

// ResourceAdaptationProcessorInterface implementation.
DegradationPreference degradation_preference() const override;
DegradationPreference effective_degradation_preference() const override;

void AddResourceLimitationsListener(
ResourceLimitationsListener* limitations_listener) override;
void RemoveResourceLimitationsListener(
Expand All @@ -81,10 +80,6 @@ class ResourceAdaptationProcessor : public ResourceAdaptationProcessorInterface,
void RemoveAdaptationListener(
AdaptationListener* adaptation_listener) override;

void SetDegradationPreference(
DegradationPreference degradation_preference) override;
void SetIsScreenshare(bool is_screenshare) override;

// ResourceListener implementation.
// Triggers OnResourceUnderuse() or OnResourceOveruse().
void OnResourceUsageStateMeasured(rtc::scoped_refptr<Resource> resource,
Expand All @@ -96,6 +91,9 @@ class ResourceAdaptationProcessor : public ResourceAdaptationProcessorInterface,
const VideoAdaptationCounters& adaptation_counters,
rtc::scoped_refptr<Resource> reason,
const VideoSourceRestrictions& unfiltered_restrictions) override;
// DegradationPreferenceListener implementation.
void OnDegradationPreferenceUpdated(
DegradationPreference degradation_preference) override;

// May trigger 1-2 adaptations. It is meant to reduce resolution but this is
// not guaranteed. It may adapt frame rate, which does not address the issue.
Expand Down Expand Up @@ -190,11 +188,8 @@ class ResourceAdaptationProcessor : public ResourceAdaptationProcessorInterface,
adaptation_limits_by_resources_
RTC_GUARDED_BY(resource_adaptation_queue_);
// Adaptation strategy settings.
DegradationPreference degradation_preference_
RTC_GUARDED_BY(resource_adaptation_queue_);
DegradationPreference effective_degradation_preference_
RTC_GUARDED_BY(resource_adaptation_queue_);
bool is_screenshare_ RTC_GUARDED_BY(resource_adaptation_queue_);
// Responsible for generating and applying possible adaptations.
VideoStreamAdapter* const stream_adapter_
RTC_GUARDED_BY(resource_adaptation_queue_);
Expand Down
11 changes: 0 additions & 11 deletions call/adaptation/resource_adaptation_processor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ class ResourceAdaptationProcessorInterface {
virtual void SetResourceAdaptationQueue(
TaskQueueBase* resource_adaptation_queue) = 0;

virtual DegradationPreference degradation_preference() const = 0;
// Reinterprets "balanced + screenshare" as "maintain-resolution".
// TODO(hbos): Don't do this. This is not what "balanced" means. If the
// application wants to maintain resolution it should set that degradation
// preference rather than depend on non-standard behaviors.
virtual DegradationPreference effective_degradation_preference() const = 0;

// Starts or stops listening to resources, effectively enabling or disabling
// processing.
// TODO(https://crbug.com/webrtc/11172): Automatically register and unregister
Expand All @@ -80,10 +73,6 @@ class ResourceAdaptationProcessorInterface {
virtual void RemoveAdaptationListener(
AdaptationListener* adaptation_listener) = 0;

virtual void SetDegradationPreference(
DegradationPreference degradation_preference) = 0;
virtual void SetIsScreenshare(bool is_screenshare) = 0;

// May trigger one or more adaptations. It is meant to reduce resolution -
// useful if a frame was dropped due to its size - however, the implementation
// may not guarantee this (see resource_adaptation_processor.h).
Expand Down
Loading

0 comments on commit 9492d50

Please sign in to comment.