xref: /aosp_15_r20/external/webrtc/test/time_controller/real_time_controller.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
15 namespace webrtc {
16 namespace {
17 class MainThread : public rtc::Thread {
18  public:
MainThread()19   MainThread()
20       : Thread(std::make_unique<rtc::NullSocketServer>(), false),
21         current_setter_(this) {
22     DoInit();
23   }
~MainThread()24   ~MainThread() {
25     Stop();
26     DoDestroy();
27   }
28 
29  private:
30   CurrentThreadSetter current_setter_;
31 };
32 }  // namespace
RealTimeController()33 RealTimeController::RealTimeController()
34     : task_queue_factory_(CreateDefaultTaskQueueFactory()),
35       main_thread_(std::make_unique<MainThread>()) {
36   main_thread_->SetName("Main", this);
37 }
38 
GetClock()39 Clock* RealTimeController::GetClock() {
40   return Clock::GetRealTimeClock();
41 }
42 
GetTaskQueueFactory()43 TaskQueueFactory* RealTimeController::GetTaskQueueFactory() {
44   return task_queue_factory_.get();
45 }
46 
CreateThread(const std::string & name,std::unique_ptr<rtc::SocketServer> socket_server)47 std::unique_ptr<rtc::Thread> RealTimeController::CreateThread(
48     const std::string& name,
49     std::unique_ptr<rtc::SocketServer> socket_server) {
50   if (!socket_server)
51     socket_server = std::make_unique<rtc::NullSocketServer>();
52   auto res = std::make_unique<rtc::Thread>(std::move(socket_server));
53   res->SetName(name, nullptr);
54   res->Start();
55   return res;
56 }
57 
GetMainThread()58 rtc::Thread* RealTimeController::GetMainThread() {
59   return main_thread_.get();
60 }
61 
AdvanceTime(TimeDelta duration)62 void RealTimeController::AdvanceTime(TimeDelta duration) {
63   main_thread_->ProcessMessages(duration.ms());
64 }
65 
66 }  // namespace webrtc
67