blob: 7e6855f7e7fbd9f76efa96075936e5a52f083e26 [file] [log] [blame]
Henrik Boströmb04b2a12019-12-10 14:14:09 +01001/*
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öm48258ac2020-02-06 12:49:57 +010014#include <vector>
15
16#include "absl/types/optional.h"
Henrik Boströmb04b2a12019-12-10 14:14:09 +010017
18namespace webrtc {
19
Henrik Boström48258ac2020-02-06 12:49:57 +010020class Resource;
21
Henrik Boströmb04b2a12019-12-10 14:14:09 +010022enum 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öm48258ac2020-02-06 12:49:57 +010032enum 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
56class 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ömb04b2a12019-12-10 14:14:09 +010069// A Resource is something which can be measured as "overused", "stable" or
Henrik Boström48258ac2020-02-06 12:49:57 +010070// "underused". When the resource usage changes, listeners of the resource are
71// informed.
Henrik Boströmb04b2a12019-12-10 14:14:09 +010072//
Henrik Boström48258ac2020-02-06 12:49:57 +010073// Implementations of this interface are responsible for performing resource
74// usage measurements and invoking OnResourceUsageStateMeasured().
Henrik Boströmb04b2a12019-12-10 14:14:09 +010075class Resource {
76 public:
Henrik Boström48258ac2020-02-06 12:49:57 +010077 // By default, usage_state() is kStable until a measurement is made.
78 Resource();
Henrik Boströmb04b2a12019-12-10 14:14:09 +010079 virtual ~Resource();
80
Henrik Boström48258ac2020-02-06 12:49:57 +010081 // 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ömb04b2a12019-12-10 14:14:09 +010084
Henrik Boström48258ac2020-02-06 12:49:57 +010085 ResourceUsageState usage_state() const;
Henrik Boströmb04b2a12019-12-10 14:14:09 +010086
Henrik Boström48258ac2020-02-06 12:49:57 +010087 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ömb04b2a12019-12-10 14:14:09 +010097};
98
99} // namespace webrtc
100
101#endif // CALL_ADAPTATION_RESOURCE_H_