resource_adaptation_processor.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2020 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. #ifndef CALL_ADAPTATION_RESOURCE_ADAPTATION_PROCESSOR_H_
  11. #define CALL_ADAPTATION_RESOURCE_ADAPTATION_PROCESSOR_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <utility>
  16. #include <vector>
  17. #include "absl/types/optional.h"
  18. #include "api/adaptation/resource.h"
  19. #include "api/rtp_parameters.h"
  20. #include "api/scoped_refptr.h"
  21. #include "api/task_queue/task_queue_base.h"
  22. #include "api/video/video_adaptation_counters.h"
  23. #include "api/video/video_frame.h"
  24. #include "api/video/video_stream_encoder_observer.h"
  25. #include "call/adaptation/resource_adaptation_processor_interface.h"
  26. #include "call/adaptation/video_source_restrictions.h"
  27. #include "call/adaptation/video_stream_adapter.h"
  28. #include "call/adaptation/video_stream_input_state.h"
  29. #include "call/adaptation/video_stream_input_state_provider.h"
  30. namespace webrtc {
  31. // The Resource Adaptation Processor is responsible for reacting to resource
  32. // usage measurements (e.g. overusing or underusing CPU). When a resource is
  33. // overused the Processor is responsible for performing mitigations in order to
  34. // consume less resources.
  35. //
  36. // Today we have one Processor per VideoStreamEncoder and the Processor is only
  37. // capable of restricting resolution or frame rate of the encoded stream. In the
  38. // future we should have a single Processor responsible for all encoded streams,
  39. // and it should be capable of reconfiguring other things than just
  40. // VideoSourceRestrictions (e.g. reduce render frame rate).
  41. // See Resource-Adaptation hotlist:
  42. // https://bugs.chromium.org/u/590058293/hotlists/Resource-Adaptation
  43. //
  44. // The ResourceAdaptationProcessor is single-threaded. It may be constructed on
  45. // any thread but MUST subsequently be used and destroyed on a single sequence,
  46. // i.e. the "resource adaptation task queue". Resources can be added and removed
  47. // from any thread.
  48. class ResourceAdaptationProcessor : public ResourceAdaptationProcessorInterface,
  49. public VideoSourceRestrictionsListener,
  50. public ResourceListener {
  51. public:
  52. explicit ResourceAdaptationProcessor(
  53. VideoStreamAdapter* video_stream_adapter);
  54. ~ResourceAdaptationProcessor() override;
  55. void SetTaskQueue(TaskQueueBase* task_queue) override;
  56. // ResourceAdaptationProcessorInterface implementation.
  57. void AddResourceLimitationsListener(
  58. ResourceLimitationsListener* limitations_listener) override;
  59. void RemoveResourceLimitationsListener(
  60. ResourceLimitationsListener* limitations_listener) override;
  61. void AddResource(rtc::scoped_refptr<Resource> resource) override;
  62. std::vector<rtc::scoped_refptr<Resource>> GetResources() const override;
  63. void RemoveResource(rtc::scoped_refptr<Resource> resource) override;
  64. // ResourceListener implementation.
  65. // Triggers OnResourceUnderuse() or OnResourceOveruse().
  66. void OnResourceUsageStateMeasured(rtc::scoped_refptr<Resource> resource,
  67. ResourceUsageState usage_state) override;
  68. // VideoSourceRestrictionsListener implementation.
  69. void OnVideoSourceRestrictionsUpdated(
  70. VideoSourceRestrictions restrictions,
  71. const VideoAdaptationCounters& adaptation_counters,
  72. rtc::scoped_refptr<Resource> reason,
  73. const VideoSourceRestrictions& unfiltered_restrictions) override;
  74. private:
  75. // If resource usage measurements happens off the adaptation task queue, this
  76. // class takes care of posting the measurement for the processor to handle it
  77. // on the adaptation task queue.
  78. class ResourceListenerDelegate : public rtc::RefCountInterface,
  79. public ResourceListener {
  80. public:
  81. explicit ResourceListenerDelegate(ResourceAdaptationProcessor* processor);
  82. void SetTaskQueue(TaskQueueBase* task_queue);
  83. void OnProcessorDestroyed();
  84. // ResourceListener implementation.
  85. void OnResourceUsageStateMeasured(rtc::scoped_refptr<Resource> resource,
  86. ResourceUsageState usage_state) override;
  87. private:
  88. TaskQueueBase* task_queue_;
  89. ResourceAdaptationProcessor* processor_ RTC_GUARDED_BY(task_queue_);
  90. };
  91. enum class MitigationResult {
  92. kNotMostLimitedResource,
  93. kSharedMostLimitedResource,
  94. kRejectedByAdapter,
  95. kAdaptationApplied,
  96. };
  97. struct MitigationResultAndLogMessage {
  98. MitigationResultAndLogMessage();
  99. MitigationResultAndLogMessage(MitigationResult result, std::string message);
  100. MitigationResult result;
  101. std::string message;
  102. };
  103. // Performs the adaptation by getting the next target, applying it and
  104. // informing listeners of the new VideoSourceRestriction and adaptation
  105. // counters.
  106. MitigationResultAndLogMessage OnResourceUnderuse(
  107. rtc::scoped_refptr<Resource> reason_resource);
  108. MitigationResultAndLogMessage OnResourceOveruse(
  109. rtc::scoped_refptr<Resource> reason_resource);
  110. void UpdateResourceLimitations(rtc::scoped_refptr<Resource> reason_resource,
  111. const VideoSourceRestrictions& restrictions,
  112. const VideoAdaptationCounters& counters)
  113. RTC_RUN_ON(task_queue_);
  114. // Searches |adaptation_limits_by_resources_| for each resource with the
  115. // highest total adaptation counts. Adaptation up may only occur if the
  116. // resource performing the adaptation is the only most limited resource. This
  117. // function returns the list of all most limited resources as well as the
  118. // corresponding adaptation of that resource.
  119. std::pair<std::vector<rtc::scoped_refptr<Resource>>,
  120. VideoStreamAdapter::RestrictionsWithCounters>
  121. FindMostLimitedResources() const RTC_RUN_ON(task_queue_);
  122. void RemoveLimitationsImposedByResource(
  123. rtc::scoped_refptr<Resource> resource);
  124. TaskQueueBase* task_queue_;
  125. rtc::scoped_refptr<ResourceListenerDelegate> resource_listener_delegate_;
  126. // Input and output.
  127. mutable Mutex resources_lock_;
  128. std::vector<rtc::scoped_refptr<Resource>> resources_
  129. RTC_GUARDED_BY(resources_lock_);
  130. std::vector<ResourceLimitationsListener*> resource_limitations_listeners_
  131. RTC_GUARDED_BY(task_queue_);
  132. // Purely used for statistics, does not ensure mapped resources stay alive.
  133. std::map<rtc::scoped_refptr<Resource>,
  134. VideoStreamAdapter::RestrictionsWithCounters>
  135. adaptation_limits_by_resources_ RTC_GUARDED_BY(task_queue_);
  136. // Responsible for generating and applying possible adaptations.
  137. VideoStreamAdapter* const stream_adapter_ RTC_GUARDED_BY(task_queue_);
  138. VideoSourceRestrictions last_reported_source_restrictions_
  139. RTC_GUARDED_BY(task_queue_);
  140. // Keeps track of previous mitigation results per resource since the last
  141. // successful adaptation. Used to avoid RTC_LOG spam.
  142. std::map<Resource*, MitigationResult> previous_mitigation_results_
  143. RTC_GUARDED_BY(task_queue_);
  144. };
  145. } // namespace webrtc
  146. #endif // CALL_ADAPTATION_RESOURCE_ADAPTATION_PROCESSOR_H_