blob: 7d7c70b3eba1b54159e88a10685f49ec7cf52209 [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öm4db954e2020-06-16 19:10:17 +020018#include "rtc_base/system/rtc_export.h"
Henrik Boströmb04b2a12019-12-10 14:14:09 +010019
20namespace webrtc {
21
Henrik Boström48258ac2020-02-06 12:49:57 +010022class Resource;
23
Henrik Boströmb04b2a12019-12-10 14:14:09 +010024enum class ResourceUsageState {
25 // Action is needed to minimze the load on this resource.
26 kOveruse,
Henrik Boströmdc4f75f2020-04-20 12:04:12 +020027 // Increasing the load on this resource is desired, if possible.
Henrik Boströmb04b2a12019-12-10 14:14:09 +010028 kUnderuse,
29};
30
Henrik Boström4db954e2020-06-16 19:10:17 +020031RTC_EXPORT const char* ResourceUsageStateToString(
32 ResourceUsageState usage_state);
Henrik Boströmf0eef122020-05-28 16:22:42 +020033
Henrik Boström4db954e2020-06-16 19:10:17 +020034class RTC_EXPORT ResourceListener {
Henrik Boström48258ac2020-02-06 12:49:57 +010035 public:
36 virtual ~ResourceListener();
37
Henrik Boströmc55516d2020-05-11 16:29:22 +020038 virtual void OnResourceUsageStateMeasured(
Henrik Boström39ab1b52020-06-03 09:21:34 +020039 rtc::scoped_refptr<Resource> resource,
40 ResourceUsageState usage_state) = 0;
Henrik Boström48258ac2020-02-06 12:49:57 +010041};
42
Henrik Boström39ab1b52020-06-03 09:21:34 +020043// A Resource monitors an implementation-specific resource. It may report
Henrik Boström5cc28b02020-06-01 17:59:05 +020044// kOveruse or kUnderuse when resource usage is high or low enough that we
45// should perform some sort of mitigation to fulfil the resource's constraints.
46//
Henrik Boström39ab1b52020-06-03 09:21:34 +020047// The methods on this interface are invoked on the adaptation task queue.
48// Resource usage measurements may be performed on an any task queue.
Henrik Boström5cc28b02020-06-01 17:59:05 +020049//
Henrik Boström5cc28b02020-06-01 17:59:05 +020050// The Resource is reference counted to prevent use-after-free when posting
51// between task queues. As such, the implementation MUST NOT make any
52// assumptions about which task queue Resource is destructed on.
Henrik Boström4db954e2020-06-16 19:10:17 +020053class RTC_EXPORT Resource : public rtc::RefCountInterface {
Henrik Boströmb04b2a12019-12-10 14:14:09 +010054 public:
Henrik Boström48258ac2020-02-06 12:49:57 +010055 Resource();
Henrik Boström5cc28b02020-06-01 17:59:05 +020056 // Destruction may happen on any task queue.
Henrik Boströmc55516d2020-05-11 16:29:22 +020057 ~Resource() override;
Henrik Boströmb04b2a12019-12-10 14:14:09 +010058
Henrik Boström5cc28b02020-06-01 17:59:05 +020059 virtual std::string Name() const = 0;
Artem Titov0e61fdd2021-07-25 21:50:14 +020060 // The `listener` may be informed of resource usage measurements on any task
Henrik Boström39ab1b52020-06-03 09:21:34 +020061 // queue, but not after this method is invoked with the null argument.
Henrik Boström0f0aa9c2020-06-02 13:02:36 +020062 virtual void SetResourceListener(ResourceListener* listener) = 0;
Henrik Boströmb04b2a12019-12-10 14:14:09 +010063};
64
65} // namespace webrtc
66
Henrik Boströme2e8c172020-06-03 09:24:06 +020067#endif // API_ADAPTATION_RESOURCE_H_