blob: 1a5bdd4ae44a8d5a279663718f103237937050cc [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
Henrik Boströme2e8c172020-06-03 09:24:06 +020011#ifndef API_ADAPTATION_RESOURCE_H_
12#define API_ADAPTATION_RESOURCE_H_
Henrik Boströmb04b2a12019-12-10 14:14:09 +010013
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +010014#include <string>
Henrik Boström48258ac2020-02-06 12:49:57 +010015
Henrik Boströmc55516d2020-05-11 16:29:22 +020016#include "api/scoped_refptr.h"
Henrik Boströmc55516d2020-05-11 16:29:22 +020017#include "rtc_base/ref_count.h"
Henrik Boströmb04b2a12019-12-10 14:14:09 +010018
19namespace webrtc {
20
Henrik Boström48258ac2020-02-06 12:49:57 +010021class Resource;
22
Henrik Boströmb04b2a12019-12-10 14:14:09 +010023enum class ResourceUsageState {
24 // Action is needed to minimze the load on this resource.
25 kOveruse,
Henrik Boströmdc4f75f2020-04-20 12:04:12 +020026 // Increasing the load on this resource is desired, if possible.
Henrik Boströmb04b2a12019-12-10 14:14:09 +010027 kUnderuse,
28};
29
Henrik Boströmf0eef122020-05-28 16:22:42 +020030const char* ResourceUsageStateToString(ResourceUsageState usage_state);
31
Henrik Boström48258ac2020-02-06 12:49:57 +010032class ResourceListener {
33 public:
34 virtual ~ResourceListener();
35
Henrik Boströmc55516d2020-05-11 16:29:22 +020036 virtual void OnResourceUsageStateMeasured(
Henrik Boström39ab1b52020-06-03 09:21:34 +020037 rtc::scoped_refptr<Resource> resource,
38 ResourceUsageState usage_state) = 0;
Henrik Boström48258ac2020-02-06 12:49:57 +010039};
40
Henrik Boström39ab1b52020-06-03 09:21:34 +020041// A Resource monitors an implementation-specific resource. It may report
Henrik Boström5cc28b02020-06-01 17:59:05 +020042// kOveruse or kUnderuse when resource usage is high or low enough that we
43// should perform some sort of mitigation to fulfil the resource's constraints.
44//
Henrik Boström39ab1b52020-06-03 09:21:34 +020045// The methods on this interface are invoked on the adaptation task queue.
46// Resource usage measurements may be performed on an any task queue.
Henrik Boström5cc28b02020-06-01 17:59:05 +020047//
Henrik Boström5cc28b02020-06-01 17:59:05 +020048// The Resource is reference counted to prevent use-after-free when posting
49// between task queues. As such, the implementation MUST NOT make any
50// assumptions about which task queue Resource is destructed on.
Henrik Boströmc55516d2020-05-11 16:29:22 +020051class Resource : public rtc::RefCountInterface {
Henrik Boströmb04b2a12019-12-10 14:14:09 +010052 public:
Henrik Boström48258ac2020-02-06 12:49:57 +010053 Resource();
Henrik Boström5cc28b02020-06-01 17:59:05 +020054 // Destruction may happen on any task queue.
Henrik Boströmc55516d2020-05-11 16:29:22 +020055 ~Resource() override;
Henrik Boströmb04b2a12019-12-10 14:14:09 +010056
Henrik Boström5cc28b02020-06-01 17:59:05 +020057 virtual std::string Name() const = 0;
Henrik Boström39ab1b52020-06-03 09:21:34 +020058 // The |listener| may be informed of resource usage measurements on any task
59 // queue, but not after this method is invoked with the null argument.
Henrik Boström0f0aa9c2020-06-02 13:02:36 +020060 virtual void SetResourceListener(ResourceListener* listener) = 0;
Henrik Boströmb04b2a12019-12-10 14:14:09 +010061};
62
63} // namespace webrtc
64
Henrik Boströme2e8c172020-06-03 09:24:06 +020065#endif // API_ADAPTATION_RESOURCE_H_