blob: febc29c9bb1dc84051e886607a0f4b7e1e671428 [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
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +010014#include <string>
Henrik Boström48258ac2020-02-06 12:49:57 +010015#include <vector>
16
17#include "absl/types/optional.h"
Henrik Boströmc55516d2020-05-11 16:29:22 +020018#include "api/scoped_refptr.h"
Henrik Boström5cc28b02020-06-01 17:59:05 +020019#include "api/task_queue/task_queue_base.h"
Henrik Boströmb613e3a2020-04-17 13:48:21 +020020#include "call/adaptation/video_source_restrictions.h"
21#include "call/adaptation/video_stream_input_state.h"
Henrik Boströmc55516d2020-05-11 16:29:22 +020022#include "rtc_base/ref_count.h"
Henrik Boströmb04b2a12019-12-10 14:14:09 +010023
24namespace webrtc {
25
Henrik Boström48258ac2020-02-06 12:49:57 +010026class Resource;
27
Henrik Boströmb04b2a12019-12-10 14:14:09 +010028enum class ResourceUsageState {
29 // Action is needed to minimze the load on this resource.
30 kOveruse,
Henrik Boströmdc4f75f2020-04-20 12:04:12 +020031 // Increasing the load on this resource is desired, if possible.
Henrik Boströmb04b2a12019-12-10 14:14:09 +010032 kUnderuse,
33};
34
Henrik Boströmf0eef122020-05-28 16:22:42 +020035const char* ResourceUsageStateToString(ResourceUsageState usage_state);
36
Henrik Boström48258ac2020-02-06 12:49:57 +010037class ResourceListener {
38 public:
39 virtual ~ResourceListener();
40
41 // Informs the listener of a new measurement of resource usage. This means
Henrik Boströmc55516d2020-05-11 16:29:22 +020042 // that |resource->usage_state()| is now up-to-date.
43 virtual void OnResourceUsageStateMeasured(
44 rtc::scoped_refptr<Resource> resource) = 0;
Henrik Boström48258ac2020-02-06 12:49:57 +010045};
46
Henrik Boström5cc28b02020-06-01 17:59:05 +020047// 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ömc55516d2020-05-11 16:29:22 +020058class Resource : public rtc::RefCountInterface {
Henrik Boströmb04b2a12019-12-10 14:14:09 +010059 public:
Henrik Boström48258ac2020-02-06 12:49:57 +010060 Resource();
Henrik Boström5cc28b02020-06-01 17:59:05 +020061 // Destruction may happen on any task queue.
Henrik Boströmc55516d2020-05-11 16:29:22 +020062 ~Resource() override;
Henrik Boströmb04b2a12019-12-10 14:14:09 +010063
Henrik Boström5cc28b02020-06-01 17:59:05 +020064 // 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öm381d1092020-05-12 18:49:07 +020074
Henrik Boström5cc28b02020-06-01 17:59:05 +020075 // The listeners MUST be informed any time UsageState() changes.
76 virtual void SetResourceListener(ResourceListener* listener) = 0;
Henrik Boströmb04b2a12019-12-10 14:14:09 +010077
Henrik Boström5cc28b02020-06-01 17:59:05 +020078 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ömdc4f75f2020-04-20 12:04:12 +020089
Henrik Boströmb613e3a2020-04-17 13:48:21 +020090 // This method allows the Resource to reject a proposed adaptation in the "up"
Henrik Boström5cc28b02020-06-01 17:59:05 +020091 // direction if it predicts this would cause overuse of this resource.
Henrik Boströmb613e3a2020-04-17 13:48:21 +020092 virtual bool IsAdaptationUpAllowed(
93 const VideoStreamInputState& input_state,
94 const VideoSourceRestrictions& restrictions_before,
95 const VideoSourceRestrictions& restrictions_after,
Henrik Boström5cc28b02020-06-01 17:59:05 +020096 rtc::scoped_refptr<Resource> reason_resource) const = 0;
97
Henrik Boström91aa7322020-04-28 12:24:33 +020098 virtual void OnAdaptationApplied(
99 const VideoStreamInputState& input_state,
100 const VideoSourceRestrictions& restrictions_before,
101 const VideoSourceRestrictions& restrictions_after,
Henrik Boström5cc28b02020-06-01 17:59:05 +0200102 rtc::scoped_refptr<Resource> reason_resource) = 0;
Henrik Boströmb04b2a12019-12-10 14:14:09 +0100103};
104
105} // namespace webrtc
106
107#endif // CALL_ADAPTATION_RESOURCE_H_