2019-12-10 14:14:09 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright 2019 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/test/fake_resource.h"
|
|
|
|
|
|
2020-06-01 17:59:05 +02:00
|
|
|
#include <algorithm>
|
2019-12-10 14:14:09 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
2020-06-01 17:59:05 +02:00
|
|
|
#include "rtc_base/ref_counted_object.h"
|
|
|
|
|
#include "rtc_base/task_utils/to_queued_task.h"
|
|
|
|
|
|
2019-12-10 14:14:09 +01:00
|
|
|
namespace webrtc {
|
|
|
|
|
|
2020-06-01 17:59:05 +02:00
|
|
|
// static
|
|
|
|
|
rtc::scoped_refptr<FakeResource> FakeResource::Create(std::string name) {
|
|
|
|
|
return new rtc::RefCountedObject<FakeResource>(name);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 12:04:12 +02:00
|
|
|
FakeResource::FakeResource(std::string name)
|
2020-06-01 17:59:05 +02:00
|
|
|
: Resource(),
|
|
|
|
|
lock_(),
|
2020-04-29 16:46:30 +02:00
|
|
|
name_(std::move(name)),
|
2020-06-01 17:59:05 +02:00
|
|
|
resource_adaptation_queue_(nullptr),
|
2020-04-29 16:46:30 +02:00
|
|
|
is_adaptation_up_allowed_(true),
|
2020-06-01 17:59:05 +02:00
|
|
|
num_adaptations_applied_(0),
|
|
|
|
|
usage_state_(absl::nullopt),
|
|
|
|
|
listener_(nullptr) {}
|
2019-12-10 14:14:09 +01:00
|
|
|
|
2020-02-06 12:49:57 +01:00
|
|
|
FakeResource::~FakeResource() {}
|
2019-12-10 14:14:09 +01:00
|
|
|
|
2020-02-06 12:49:57 +01:00
|
|
|
void FakeResource::set_usage_state(ResourceUsageState usage_state) {
|
2020-06-01 17:59:05 +02:00
|
|
|
if (!resource_adaptation_queue_->IsCurrent()) {
|
|
|
|
|
resource_adaptation_queue_->PostTask(ToQueuedTask(
|
|
|
|
|
[this_ref = rtc::scoped_refptr<FakeResource>(this), usage_state] {
|
|
|
|
|
this_ref->set_usage_state(usage_state);
|
|
|
|
|
}));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
|
|
|
|
|
usage_state_ = usage_state;
|
|
|
|
|
if (listener_) {
|
|
|
|
|
listener_->OnResourceUsageStateMeasured(this);
|
|
|
|
|
}
|
2020-02-25 16:26:01 +01:00
|
|
|
}
|
2019-12-10 14:14:09 +01:00
|
|
|
|
2020-04-29 16:46:30 +02:00
|
|
|
void FakeResource::set_is_adaptation_up_allowed(bool is_adaptation_up_allowed) {
|
2020-06-01 17:59:05 +02:00
|
|
|
rtc::CritScope crit(&lock_);
|
2020-04-29 16:46:30 +02:00
|
|
|
is_adaptation_up_allowed_ = is_adaptation_up_allowed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t FakeResource::num_adaptations_applied() const {
|
2020-06-01 17:59:05 +02:00
|
|
|
rtc::CritScope crit(&lock_);
|
2020-04-29 16:46:30 +02:00
|
|
|
return num_adaptations_applied_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 17:59:05 +02:00
|
|
|
void FakeResource::RegisterAdaptationTaskQueue(
|
|
|
|
|
TaskQueueBase* resource_adaptation_queue) {
|
|
|
|
|
RTC_DCHECK(!resource_adaptation_queue_);
|
|
|
|
|
RTC_DCHECK(resource_adaptation_queue);
|
|
|
|
|
resource_adaptation_queue_ = resource_adaptation_queue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FakeResource::UnregisterAdaptationTaskQueue() {
|
|
|
|
|
RTC_DCHECK(resource_adaptation_queue_);
|
|
|
|
|
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
|
|
|
|
|
resource_adaptation_queue_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FakeResource::SetResourceListener(ResourceListener* listener) {
|
|
|
|
|
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
|
|
|
|
|
listener_ = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string FakeResource::Name() const {
|
|
|
|
|
return name_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
absl::optional<ResourceUsageState> FakeResource::UsageState() const {
|
|
|
|
|
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
|
|
|
|
|
return usage_state_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FakeResource::ClearUsageState() {
|
|
|
|
|
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
|
|
|
|
|
usage_state_ = absl::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 16:46:30 +02:00
|
|
|
bool FakeResource::IsAdaptationUpAllowed(
|
|
|
|
|
const VideoStreamInputState& input_state,
|
|
|
|
|
const VideoSourceRestrictions& restrictions_before,
|
|
|
|
|
const VideoSourceRestrictions& restrictions_after,
|
2020-05-11 16:29:22 +02:00
|
|
|
rtc::scoped_refptr<Resource> reason_resource) const {
|
2020-06-01 17:59:05 +02:00
|
|
|
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
|
|
|
|
|
rtc::CritScope crit(&lock_);
|
2020-04-29 16:46:30 +02:00
|
|
|
return is_adaptation_up_allowed_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FakeResource::OnAdaptationApplied(
|
|
|
|
|
const VideoStreamInputState& input_state,
|
|
|
|
|
const VideoSourceRestrictions& restrictions_before,
|
|
|
|
|
const VideoSourceRestrictions& restrictions_after,
|
2020-05-11 16:29:22 +02:00
|
|
|
rtc::scoped_refptr<Resource> reason_resource) {
|
2020-06-01 17:59:05 +02:00
|
|
|
RTC_DCHECK_RUN_ON(resource_adaptation_queue_);
|
|
|
|
|
rtc::CritScope crit(&lock_);
|
2020-04-29 16:46:30 +02:00
|
|
|
++num_adaptations_applied_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 14:14:09 +01:00
|
|
|
} // namespace webrtc
|