Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [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 | #include "test/time_controller/real_time_controller.h" |
| 11 | |
Danil Chapovalov | 28de7cf | 2019-04-12 19:25:06 +0200 | [diff] [blame] | 12 | #include "api/task_queue/default_task_queue_factory.h" |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 13 | #include "rtc_base/null_socket_server.h" |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 16 | namespace { |
| 17 | class MainThread : public rtc::Thread { |
| 18 | public: |
| 19 | MainThread() |
| 20 | : Thread(std::make_unique<rtc::NullSocketServer>(), false), |
| 21 | current_setter_(this) { |
| 22 | DoInit(); |
| 23 | } |
| 24 | ~MainThread() { |
| 25 | Stop(); |
| 26 | DoDestroy(); |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | CurrentThreadSetter current_setter_; |
| 31 | }; |
| 32 | } // namespace |
Danil Chapovalov | 28de7cf | 2019-04-12 19:25:06 +0200 | [diff] [blame] | 33 | RealTimeController::RealTimeController() |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 34 | : task_queue_factory_(CreateDefaultTaskQueueFactory()), |
| 35 | main_thread_(std::make_unique<MainThread>()) { |
| 36 | main_thread_->SetName("Main", this); |
| 37 | } |
Danil Chapovalov | 28de7cf | 2019-04-12 19:25:06 +0200 | [diff] [blame] | 38 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 39 | Clock* RealTimeController::GetClock() { |
| 40 | return Clock::GetRealTimeClock(); |
| 41 | } |
| 42 | |
| 43 | TaskQueueFactory* RealTimeController::GetTaskQueueFactory() { |
Danil Chapovalov | 28de7cf | 2019-04-12 19:25:06 +0200 | [diff] [blame] | 44 | return task_queue_factory_.get(); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | std::unique_ptr<ProcessThread> RealTimeController::CreateProcessThread( |
| 48 | const char* thread_name) { |
| 49 | return ProcessThread::Create(thread_name); |
| 50 | } |
| 51 | |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 52 | std::unique_ptr<rtc::Thread> RealTimeController::CreateThread( |
| 53 | const std::string& name, |
| 54 | std::unique_ptr<rtc::SocketServer> socket_server) { |
| 55 | if (!socket_server) |
| 56 | socket_server = std::make_unique<rtc::NullSocketServer>(); |
| 57 | auto res = std::make_unique<rtc::Thread>(std::move(socket_server)); |
| 58 | res->SetName(name, nullptr); |
| 59 | res->Start(); |
| 60 | return res; |
| 61 | } |
| 62 | |
| 63 | rtc::Thread* RealTimeController::GetMainThread() { |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 64 | return main_thread_.get(); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Markus Handell | 486cc55 | 2019-12-03 14:37:28 +0100 | [diff] [blame] | 67 | void RealTimeController::AdvanceTime(TimeDelta duration) { |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 68 | main_thread_->ProcessMessages(duration.ms()); |
Sebastian Jansson | 5d97f55 | 2019-04-15 14:43:03 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 71 | } // namespace webrtc |