LoadManager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef _LOADMANAGER_H_
  6. #define _LOADMANAGER_H_
  7. #include "LoadMonitor.h"
  8. #include "mozilla/StaticPtr.h"
  9. #include "mozilla/TimeStamp.h"
  10. #include "mozilla/Services.h"
  11. #include "nsTArray.h"
  12. #include "nsIObserver.h"
  13. #include "webrtc/common_types.h"
  14. #include "webrtc/video_engine/include/vie_base.h"
  15. extern mozilla::LazyLogModule gLoadManagerLog;
  16. namespace mozilla {
  17. class LoadManagerSingleton : public LoadNotificationCallback,
  18. public webrtc::CPULoadStateCallbackInvoker,
  19. public webrtc::CpuOveruseObserver,
  20. public nsIObserver
  21. {
  22. public:
  23. static LoadManagerSingleton* Get();
  24. NS_DECL_THREADSAFE_ISUPPORTS
  25. NS_DECL_NSIOBSERVER
  26. // LoadNotificationCallback interface
  27. void LoadChanged(float aSystemLoad, float aProcessLoad) override;
  28. // CpuOveruseObserver interface
  29. // Called as soon as an overuse is detected.
  30. void OveruseDetected() override;
  31. // Called periodically when the system is not overused any longer.
  32. void NormalUsage() override;
  33. // CPULoadStateCallbackInvoker interface
  34. void AddObserver(webrtc::CPULoadStateObserver * aObserver) override;
  35. void RemoveObserver(webrtc::CPULoadStateObserver * aObserver) override;
  36. private:
  37. LoadManagerSingleton(bool aEncoderOnly,
  38. int aLoadMeasurementInterval,
  39. int aAveragingMeasurements,
  40. float aHighLoadThreshold,
  41. float aLowLoadThreshold);
  42. ~LoadManagerSingleton();
  43. void LoadHasChanged(webrtc::CPULoadState aNewState);
  44. RefPtr<LoadMonitor> mLoadMonitor;
  45. // This protects access to the mObservers list, the current state, and
  46. // pretty much all the other members (below).
  47. Mutex mLock;
  48. nsTArray<webrtc::CPULoadStateObserver*> mObservers;
  49. webrtc::CPULoadState mCurrentState;
  50. TimeStamp mLastStateChange;
  51. float mTimeInState[static_cast<int>(webrtc::kLoadLast)];
  52. // Set when overuse was signaled to us, and hasn't been un-signaled yet.
  53. bool mOveruseActive;
  54. float mLoadSum;
  55. int mLoadSumMeasurements;
  56. // Load measurement settings
  57. int mLoadMeasurementInterval;
  58. int mAveragingMeasurements;
  59. float mHighLoadThreshold;
  60. float mLowLoadThreshold;
  61. static StaticRefPtr<LoadManagerSingleton> sSingleton;
  62. };
  63. class LoadManager final : public webrtc::CPULoadStateCallbackInvoker,
  64. public webrtc::CpuOveruseObserver
  65. {
  66. public:
  67. explicit LoadManager(LoadManagerSingleton* aManager)
  68. : mManager(aManager)
  69. {}
  70. ~LoadManager() {}
  71. void AddObserver(webrtc::CPULoadStateObserver * aObserver) override
  72. {
  73. mManager->AddObserver(aObserver);
  74. }
  75. void RemoveObserver(webrtc::CPULoadStateObserver * aObserver) override
  76. {
  77. mManager->RemoveObserver(aObserver);
  78. }
  79. void OveruseDetected() override
  80. {
  81. mManager->OveruseDetected();
  82. }
  83. void NormalUsage() override
  84. {
  85. mManager->NormalUsage();
  86. }
  87. private:
  88. RefPtr<LoadManagerSingleton> mManager;
  89. };
  90. } //namespace
  91. #endif /* _LOADMANAGER_H_ */