aboutsummaryrefslogtreecommitdiff
path: root/call/adaptation/resource.h
blob: febc29c9bb1dc84051e886607a0f4b7e1e671428 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 *  Copyright 2019 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef CALL_ADAPTATION_RESOURCE_H_
#define CALL_ADAPTATION_RESOURCE_H_

#include <string>
#include <vector>

#include "absl/types/optional.h"
#include "api/scoped_refptr.h"
#include "api/task_queue/task_queue_base.h"
#include "call/adaptation/video_source_restrictions.h"
#include "call/adaptation/video_stream_input_state.h"
#include "rtc_base/ref_count.h"

namespace webrtc {

class Resource;

enum class ResourceUsageState {
  // Action is needed to minimze the load on this resource.
  kOveruse,
  // Increasing the load on this resource is desired, if possible.
  kUnderuse,
};

const char* ResourceUsageStateToString(ResourceUsageState usage_state);

class ResourceListener {
 public:
  virtual ~ResourceListener();

  // Informs the listener of a new measurement of resource usage. This means
  // that |resource->usage_state()| is now up-to-date.
  virtual void OnResourceUsageStateMeasured(
      rtc::scoped_refptr<Resource> resource) = 0;
};

// A Resource monitors an implementation-specific system resource. It may report
// kOveruse or kUnderuse when resource usage is high or low enough that we
// should perform some sort of mitigation to fulfil the resource's constraints.
//
// All methods defined in this interface, except RegisterAdaptationTaskQueue(),
// MUST be invoked on the resource adaptation task queue.
//
// Usage measurements may be performed on an implementation-specific task queue.
// The Resource is reference counted to prevent use-after-free when posting
// between task queues. As such, the implementation MUST NOT make any
// assumptions about which task queue Resource is destructed on.
class Resource : public rtc::RefCountInterface {
 public:
  Resource();
  // Destruction may happen on any task queue.
  ~Resource() override;

  // Provides a pointer to the adaptation task queue. After this call, all
  // methods defined in this interface, including
  // UnregisterAdaptationTaskQueue() MUST be invoked on the adaptation task
  // queue. Registering the adaptation task queue may, however, happen off the
  // adaptation task queue.
  virtual void RegisterAdaptationTaskQueue(
      TaskQueueBase* resource_adaptation_queue) = 0;
  // Signals that the adaptation task queue is no longer safe to use. No
  // assumptions must be made as to whether or not tasks in-flight will run.
  virtual void UnregisterAdaptationTaskQueue() = 0;

  // The listeners MUST be informed any time UsageState() changes.
  virtual void SetResourceListener(ResourceListener* listener) = 0;

  virtual std::string Name() const = 0;
  // Within a single task running on the adaptation task queue, UsageState()
  // MUST return the same value every time it is called.
  // TODO(https://crbug.com/webrtc/11618): Remove the UsageState() getter in
  // favor of passing the use usage state directly to the ResourceListener. This
  // gets rid of this strange requirement of having to return the same thing
  // every time.
  virtual absl::optional<ResourceUsageState> UsageState() const = 0;
  // Invalidates current usage measurements, i.e. in response to the system load
  // changing. Example: an adaptation was just applied.
  virtual void ClearUsageState() = 0;

  // This method allows the Resource to reject a proposed adaptation in the "up"
  // direction if it predicts this would cause overuse of this resource.
  virtual bool IsAdaptationUpAllowed(
      const VideoStreamInputState& input_state,
      const VideoSourceRestrictions& restrictions_before,
      const VideoSourceRestrictions& restrictions_after,
      rtc::scoped_refptr<Resource> reason_resource) const = 0;

  virtual void OnAdaptationApplied(
      const VideoStreamInputState& input_state,
      const VideoSourceRestrictions& restrictions_before,
      const VideoSourceRestrictions& restrictions_after,
      rtc::scoped_refptr<Resource> reason_resource) = 0;
};

}  // namespace webrtc

#endif  // CALL_ADAPTATION_RESOURCE_H_