Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 | |
| 11 | #ifndef CALL_ADAPTATION_RESOURCE_H_ |
| 12 | #define CALL_ADAPTATION_RESOURCE_H_ |
| 13 | |
Evan Shrubsole | aa6fbc1 | 2020-02-25 16:26:01 +0100 | [diff] [blame] | 14 | #include <string> |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
| 17 | #include "absl/types/optional.h" |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 18 | #include "api/scoped_refptr.h" |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 19 | #include "api/task_queue/task_queue_base.h" |
Henrik Boström | b613e3a | 2020-04-17 13:48:21 +0200 | [diff] [blame] | 20 | #include "call/adaptation/video_source_restrictions.h" |
| 21 | #include "call/adaptation/video_stream_input_state.h" |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 22 | #include "rtc_base/ref_count.h" |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 26 | class Resource; |
| 27 | |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 28 | enum class ResourceUsageState { |
| 29 | // Action is needed to minimze the load on this resource. |
| 30 | kOveruse, |
Henrik Boström | dc4f75f | 2020-04-20 12:04:12 +0200 | [diff] [blame] | 31 | // Increasing the load on this resource is desired, if possible. |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 32 | kUnderuse, |
| 33 | }; |
| 34 | |
Henrik Boström | f0eef12 | 2020-05-28 16:22:42 +0200 | [diff] [blame] | 35 | const char* ResourceUsageStateToString(ResourceUsageState usage_state); |
| 36 | |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 37 | class ResourceListener { |
| 38 | public: |
| 39 | virtual ~ResourceListener(); |
| 40 | |
| 41 | // Informs the listener of a new measurement of resource usage. This means |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 42 | // that |resource->usage_state()| is now up-to-date. |
| 43 | virtual void OnResourceUsageStateMeasured( |
| 44 | rtc::scoped_refptr<Resource> resource) = 0; |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 47 | // A Resource monitors an implementation-specific system resource. It may report |
| 48 | // kOveruse or kUnderuse when resource usage is high or low enough that we |
| 49 | // should perform some sort of mitigation to fulfil the resource's constraints. |
| 50 | // |
| 51 | // All methods defined in this interface, except RegisterAdaptationTaskQueue(), |
| 52 | // MUST be invoked on the resource adaptation task queue. |
| 53 | // |
| 54 | // Usage measurements may be performed on an implementation-specific task queue. |
| 55 | // The Resource is reference counted to prevent use-after-free when posting |
| 56 | // between task queues. As such, the implementation MUST NOT make any |
| 57 | // assumptions about which task queue Resource is destructed on. |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 58 | class Resource : public rtc::RefCountInterface { |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 59 | public: |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 60 | Resource(); |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 61 | // Destruction may happen on any task queue. |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 62 | ~Resource() override; |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 63 | |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 64 | // Provides a pointer to the adaptation task queue. After this call, all |
| 65 | // methods defined in this interface, including |
| 66 | // UnregisterAdaptationTaskQueue() MUST be invoked on the adaptation task |
| 67 | // queue. Registering the adaptation task queue may, however, happen off the |
| 68 | // adaptation task queue. |
| 69 | virtual void RegisterAdaptationTaskQueue( |
| 70 | TaskQueueBase* resource_adaptation_queue) = 0; |
| 71 | // Signals that the adaptation task queue is no longer safe to use. No |
| 72 | // assumptions must be made as to whether or not tasks in-flight will run. |
| 73 | virtual void UnregisterAdaptationTaskQueue() = 0; |
Henrik Boström | 381d109 | 2020-05-12 18:49:07 +0200 | [diff] [blame] | 74 | |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 75 | // The listeners MUST be informed any time UsageState() changes. |
| 76 | virtual void SetResourceListener(ResourceListener* listener) = 0; |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 77 | |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 78 | virtual std::string Name() const = 0; |
| 79 | // Within a single task running on the adaptation task queue, UsageState() |
| 80 | // MUST return the same value every time it is called. |
| 81 | // TODO(https://crbug.com/webrtc/11618): Remove the UsageState() getter in |
| 82 | // favor of passing the use usage state directly to the ResourceListener. This |
| 83 | // gets rid of this strange requirement of having to return the same thing |
| 84 | // every time. |
| 85 | virtual absl::optional<ResourceUsageState> UsageState() const = 0; |
| 86 | // Invalidates current usage measurements, i.e. in response to the system load |
| 87 | // changing. Example: an adaptation was just applied. |
| 88 | virtual void ClearUsageState() = 0; |
Henrik Boström | dc4f75f | 2020-04-20 12:04:12 +0200 | [diff] [blame] | 89 | |
Henrik Boström | b613e3a | 2020-04-17 13:48:21 +0200 | [diff] [blame] | 90 | // This method allows the Resource to reject a proposed adaptation in the "up" |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 91 | // direction if it predicts this would cause overuse of this resource. |
Henrik Boström | b613e3a | 2020-04-17 13:48:21 +0200 | [diff] [blame] | 92 | virtual bool IsAdaptationUpAllowed( |
| 93 | const VideoStreamInputState& input_state, |
| 94 | const VideoSourceRestrictions& restrictions_before, |
| 95 | const VideoSourceRestrictions& restrictions_after, |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 96 | rtc::scoped_refptr<Resource> reason_resource) const = 0; |
| 97 | |
Henrik Boström | 91aa732 | 2020-04-28 12:24:33 +0200 | [diff] [blame] | 98 | virtual void OnAdaptationApplied( |
| 99 | const VideoStreamInputState& input_state, |
| 100 | const VideoSourceRestrictions& restrictions_before, |
| 101 | const VideoSourceRestrictions& restrictions_after, |
Henrik Boström | 5cc28b0 | 2020-06-01 17:59:05 +0200 | [diff] [blame^] | 102 | rtc::scoped_refptr<Resource> reason_resource) = 0; |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace webrtc |
| 106 | |
| 107 | #endif // CALL_ADAPTATION_RESOURCE_H_ |