blob: 80528e1403d63eaab9d46b226e4001f1175ad012 [file] [log] [blame]
Bjorn A Mellemc4f86542019-11-21 10:37:18 -08001/*
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"
22#include "modules/utility/include/process_thread.h"
23#include "system_wrappers/include/clock.h"
24#include "test/time_controller/simulated_time_controller.h"
25
26namespace webrtc {
27
28// TimeController implementation built on an external controlled alarm.
29// This implementation is used to delegate scheduling and execution to an
30// external run loop.
31class ExternalTimeController : public TimeController, public TaskQueueFactory {
32 public:
33 explicit ExternalTimeController(ControlledAlarmClock* alarm);
34
35 // Implementation of TimeController.
36 Clock* GetClock() override;
37 TaskQueueFactory* GetTaskQueueFactory() override;
38 std::unique_ptr<ProcessThread> CreateProcessThread(
39 const char* thread_name) override;
Markus Handell486cc552019-12-03 14:37:28 +010040 void AdvanceTime(TimeDelta duration) override;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010041 std::unique_ptr<rtc::Thread> CreateThread(
42 const std::string& name,
43 std::unique_ptr<rtc::SocketServer> socket_server) override;
44 rtc::Thread* GetMainThread() override;
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080045
46 // Implementation of TaskQueueFactory.
47 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
48 absl::string_view name,
49 TaskQueueFactory::Priority priority) const override;
50
51 private:
52 class ProcessThreadWrapper;
53 class TaskQueueWrapper;
54
55 // Executes any tasks scheduled at or before the current time. May call
Artem Titov1ee563d2021-07-27 12:46:29 +020056 // `ScheduleNext` to schedule the next call to `Run`.
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080057 void Run();
58
59 void UpdateTime();
60 void ScheduleNext();
61
62 ControlledAlarmClock* alarm_;
63 sim_time_impl::SimulatedTimeControllerImpl impl_;
Sebastian Jansson340af972019-12-04 10:07:48 +010064 rtc::ScopedYieldPolicy yield_policy_;
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080065
66 // Overrides the global rtc::Clock to ensure that it reports the same times as
67 // the time controller.
68 rtc::ScopedBaseFakeClock global_clock_;
69};
70
71} // namespace webrtc
72
73#endif // TEST_TIME_CONTROLLER_EXTERNAL_TIME_CONTROLLER_H_