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 | |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | #include "absl/types/optional.h" |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 20 | class Resource; |
| 21 | |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 22 | enum class ResourceUsageState { |
| 23 | // Action is needed to minimze the load on this resource. |
| 24 | kOveruse, |
| 25 | // No action needed for this resource, increasing the load on this resource |
| 26 | // is not allowed. |
| 27 | kStable, |
| 28 | // Increasing the load on this resource is allowed. |
| 29 | kUnderuse, |
| 30 | }; |
| 31 | |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 32 | enum class ResourceListenerResponse { |
| 33 | kNothing, |
| 34 | // This response is only applicable to QualityScaler-based resources. |
| 35 | // It tells the QualityScaler to increase its QP measurement frequency. |
| 36 | // |
| 37 | // This is modelled after AdaptationObserverInterface::AdaptDown()'s return |
| 38 | // value. The method comment says "Returns false if a downgrade was requested |
| 39 | // but the request did not result in a new limiting resolution or fps." |
| 40 | // However the actual implementation seems to be: Return false if |
| 41 | // !has_input_video_ or if we use balanced degradation preference and we DID |
| 42 | // adapt frame rate but the difference between input frame rate and balanced |
| 43 | // settings' min fps is less than the balanced settings' min fps diff - in all |
| 44 | // other cases, return true whether or not adaptation happened. |
| 45 | // |
| 46 | // For QualityScaler-based resources, kQualityScalerShouldIncreaseFrequency |
| 47 | // maps to "return false" and kNothing maps to "return true". |
| 48 | // |
| 49 | // TODO(https://crbug.com/webrtc/11222): Remove this enum. Resource |
| 50 | // measurements and adaptation decisions need to be separated in order to |
| 51 | // support injectable adaptation modules, multi-stream aware adaptation and |
| 52 | // decision-making logic based on multiple resources. |
| 53 | kQualityScalerShouldIncreaseFrequency, |
| 54 | }; |
| 55 | |
| 56 | class ResourceListener { |
| 57 | public: |
| 58 | virtual ~ResourceListener(); |
| 59 | |
| 60 | // Informs the listener of a new measurement of resource usage. This means |
| 61 | // that |resource.usage_state()| is now up-to-date. |
| 62 | // |
| 63 | // The listener may influence the resource that signaled the measurement |
| 64 | // according to the returned ResourceListenerResponse enum. |
| 65 | virtual ResourceListenerResponse OnResourceUsageStateMeasured( |
| 66 | const Resource& resource) = 0; |
| 67 | }; |
| 68 | |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 69 | // A Resource is something which can be measured as "overused", "stable" or |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 70 | // "underused". When the resource usage changes, listeners of the resource are |
| 71 | // informed. |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 72 | // |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 73 | // Implementations of this interface are responsible for performing resource |
| 74 | // usage measurements and invoking OnResourceUsageStateMeasured(). |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 75 | class Resource { |
| 76 | public: |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 77 | // By default, usage_state() is kStable until a measurement is made. |
| 78 | Resource(); |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 79 | virtual ~Resource(); |
| 80 | |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 81 | // TODO(https://crbug.com/webrtc/11222): Make it possible to unregister |
| 82 | // listeners and DCHECK that they're all unregistered in the destructor. |
| 83 | void RegisterListener(ResourceListener* listener); |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 84 | |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 85 | ResourceUsageState usage_state() const; |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 86 | |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 87 | protected: |
| 88 | // Updates the usage state and informs all registered listeners. |
| 89 | // Returns the result of the last listener's OnResourceUsageStateMeasured() |
| 90 | // call that was not kNothing, else kNothing. |
| 91 | ResourceListenerResponse OnResourceUsageStateMeasured( |
| 92 | ResourceUsageState usage_state); |
| 93 | |
| 94 | private: |
| 95 | ResourceUsageState usage_state_; |
| 96 | std::vector<ResourceListener*> listeners_; |
Henrik Boström | b04b2a1 | 2019-12-10 14:14:09 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace webrtc |
| 100 | |
| 101 | #endif // CALL_ADAPTATION_RESOURCE_H_ |