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 12 #include "api/task_queue/default_task_queue_factory.h" 13 #include "rtc_base/null_socket_server.h" 14 #include "system_wrappers/include/sleep.h" 15 16 namespace webrtc { 17 namespace { 18 class MainThread : public rtc::Thread { 19 public: MainThread()20 MainThread() 21 : Thread(std::make_unique<rtc::NullSocketServer>(), false), 22 current_setter_(this) { 23 DoInit(); 24 } ~MainThread()25 ~MainThread() { 26 Stop(); 27 DoDestroy(); 28 } 29 30 private: 31 CurrentThreadSetter current_setter_; 32 }; 33 } // namespace RealTimeController()34RealTimeController::RealTimeController() 35 : task_queue_factory_(CreateDefaultTaskQueueFactory()), 36 main_thread_(std::make_unique<MainThread>()) { 37 main_thread_->SetName("Main", this); 38 } 39 GetClock()40Clock* RealTimeController::GetClock() { 41 return Clock::GetRealTimeClock(); 42 } 43 GetTaskQueueFactory()44TaskQueueFactory* RealTimeController::GetTaskQueueFactory() { 45 return task_queue_factory_.get(); 46 } 47 CreateProcessThread(const char * thread_name)48std::unique_ptr<ProcessThread> RealTimeController::CreateProcessThread( 49 const char* thread_name) { 50 return ProcessThread::Create(thread_name); 51 } 52 CreateThread(const std::string & name,std::unique_ptr<rtc::SocketServer> socket_server)53std::unique_ptr<rtc::Thread> RealTimeController::CreateThread( 54 const std::string& name, 55 std::unique_ptr<rtc::SocketServer> socket_server) { 56 if (!socket_server) 57 socket_server = std::make_unique<rtc::NullSocketServer>(); 58 auto res = std::make_unique<rtc::Thread>(std::move(socket_server)); 59 res->SetName(name, nullptr); 60 res->Start(); 61 return res; 62 } 63 GetMainThread()64rtc::Thread* RealTimeController::GetMainThread() { 65 return main_thread_.get(); 66 } 67 AdvanceTime(TimeDelta duration)68void RealTimeController::AdvanceTime(TimeDelta duration) { 69 main_thread_->ProcessMessages(duration.ms()); 70 } 71 72 } // namespace webrtc 73