external_time_controller.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  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. #ifndef TEST_TIME_CONTROLLER_EXTERNAL_TIME_CONTROLLER_H_
  11. #define TEST_TIME_CONTROLLER_EXTERNAL_TIME_CONTROLLER_H_
  12. #include <functional>
  13. #include <memory>
  14. #include "absl/strings/string_view.h"
  15. #include "api/task_queue/task_queue_base.h"
  16. #include "api/task_queue/task_queue_factory.h"
  17. #include "api/test/time_controller.h"
  18. #include "api/units/time_delta.h"
  19. #include "api/units/timestamp.h"
  20. #include "modules/utility/include/process_thread.h"
  21. #include "system_wrappers/include/clock.h"
  22. #include "test/time_controller/simulated_time_controller.h"
  23. namespace webrtc {
  24. // TimeController implementation built on an external controlled alarm.
  25. // This implementation is used to delegate scheduling and execution to an
  26. // external run loop.
  27. class ExternalTimeController : public TimeController, public TaskQueueFactory {
  28. public:
  29. explicit ExternalTimeController(ControlledAlarmClock* alarm);
  30. // Implementation of TimeController.
  31. Clock* GetClock() override;
  32. TaskQueueFactory* GetTaskQueueFactory() override;
  33. std::unique_ptr<ProcessThread> CreateProcessThread(
  34. const char* thread_name) override;
  35. void AdvanceTime(TimeDelta duration) override;
  36. std::unique_ptr<rtc::Thread> CreateThread(
  37. const std::string& name,
  38. std::unique_ptr<rtc::SocketServer> socket_server) override;
  39. rtc::Thread* GetMainThread() override;
  40. // Implementation of TaskQueueFactory.
  41. std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
  42. absl::string_view name,
  43. TaskQueueFactory::Priority priority) const override;
  44. private:
  45. class ProcessThreadWrapper;
  46. class TaskQueueWrapper;
  47. // Executes any tasks scheduled at or before the current time. May call
  48. // |ScheduleNext| to schedule the next call to |Run|.
  49. void Run();
  50. void UpdateTime();
  51. void ScheduleNext();
  52. ControlledAlarmClock* alarm_;
  53. sim_time_impl::SimulatedTimeControllerImpl impl_;
  54. rtc::ScopedYieldPolicy yield_policy_;
  55. // Overrides the global rtc::Clock to ensure that it reports the same times as
  56. // the time controller.
  57. rtc::ScopedBaseFakeClock global_clock_;
  58. };
  59. } // namespace webrtc
  60. #endif // TEST_TIME_CONTROLLER_EXTERNAL_TIME_CONTROLLER_H_