blob: 7cc750d6d49732c04e60b6b749da0462387da0be [file] [log] [blame]
Sebastian Jansson0d617cc2019-03-22 15:22:16 +01001/*
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 Chapovalov28de7cf2019-04-12 19:25:06 +020012#include "api/task_queue/default_task_queue_factory.h"
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010013#include "rtc_base/null_socket_server.h"
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010014
15namespace webrtc {
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010016namespace {
17class 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 Chapovalov28de7cf2019-04-12 19:25:06 +020033RealTimeController::RealTimeController()
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010034 : task_queue_factory_(CreateDefaultTaskQueueFactory()),
35 main_thread_(std::make_unique<MainThread>()) {
36 main_thread_->SetName("Main", this);
37}
Danil Chapovalov28de7cf2019-04-12 19:25:06 +020038
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010039Clock* RealTimeController::GetClock() {
40 return Clock::GetRealTimeClock();
41}
42
43TaskQueueFactory* RealTimeController::GetTaskQueueFactory() {
Danil Chapovalov28de7cf2019-04-12 19:25:06 +020044 return task_queue_factory_.get();
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010045}
46
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010047std::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
58rtc::Thread* RealTimeController::GetMainThread() {
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010059 return main_thread_.get();
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010060}
61
Markus Handell486cc552019-12-03 14:37:28 +010062void RealTimeController::AdvanceTime(TimeDelta duration) {
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010063 main_thread_->ProcessMessages(duration.ms());
Sebastian Jansson5d97f552019-04-15 14:43:03 +020064}
65
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010066} // namespace webrtc