Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 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 | |
| 13 | #include <functional> |
| 14 | #include <memory> |
| 15 | |
| 16 | #include "absl/strings/string_view.h" |
| 17 | #include "api/task_queue/task_queue_base.h" |
| 18 | #include "api/task_queue/task_queue_factory.h" |
| 19 | #include "api/test/time_controller.h" |
| 20 | #include "api/units/time_delta.h" |
| 21 | #include "api/units/timestamp.h" |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 22 | #include "system_wrappers/include/clock.h" |
| 23 | #include "test/time_controller/simulated_time_controller.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | // TimeController implementation built on an external controlled alarm. |
| 28 | // This implementation is used to delegate scheduling and execution to an |
| 29 | // external run loop. |
| 30 | class ExternalTimeController : public TimeController, public TaskQueueFactory { |
| 31 | public: |
| 32 | explicit ExternalTimeController(ControlledAlarmClock* alarm); |
| 33 | |
| 34 | // Implementation of TimeController. |
| 35 | Clock* GetClock() override; |
| 36 | TaskQueueFactory* GetTaskQueueFactory() override; |
Markus Handell | 486cc55 | 2019-12-03 14:37:28 +0100 | [diff] [blame] | 37 | void AdvanceTime(TimeDelta duration) override; |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 38 | std::unique_ptr<rtc::Thread> CreateThread( |
| 39 | const std::string& name, |
| 40 | std::unique_ptr<rtc::SocketServer> socket_server) override; |
| 41 | rtc::Thread* GetMainThread() override; |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 42 | |
| 43 | // Implementation of TaskQueueFactory. |
| 44 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue( |
| 45 | absl::string_view name, |
| 46 | TaskQueueFactory::Priority priority) const override; |
| 47 | |
| 48 | private: |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 49 | class TaskQueueWrapper; |
| 50 | |
| 51 | // Executes any tasks scheduled at or before the current time. May call |
Artem Titov | 1ee563d | 2021-07-27 12:46:29 +0200 | [diff] [blame] | 52 | // `ScheduleNext` to schedule the next call to `Run`. |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 53 | void Run(); |
| 54 | |
| 55 | void UpdateTime(); |
| 56 | void ScheduleNext(); |
| 57 | |
| 58 | ControlledAlarmClock* alarm_; |
| 59 | sim_time_impl::SimulatedTimeControllerImpl impl_; |
Sebastian Jansson | 340af97 | 2019-12-04 10:07:48 +0100 | [diff] [blame] | 60 | rtc::ScopedYieldPolicy yield_policy_; |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 61 | |
| 62 | // Overrides the global rtc::Clock to ensure that it reports the same times as |
| 63 | // the time controller. |
| 64 | rtc::ScopedBaseFakeClock global_clock_; |
| 65 | }; |
| 66 | |
| 67 | } // namespace webrtc |
| 68 | |
| 69 | #endif // TEST_TIME_CONTROLLER_EXTERNAL_TIME_CONTROLLER_H_ |