Moves ownership of time controller into NetworkEmulationManager.

This makes it easier to maintain consistency between real time
and simulated time modes.

The RealTimeController is updated to use an explicit main thread,
this ensures that pending destruction tasks are run as the network
emulator goes out of scope.

Bug: webrtc:11255
Change-Id: Ie73ab778c78a68d7c58c0f857f14a8d8ac027c67
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/166164
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30342}
diff --git a/api/test/create_network_emulation_manager.cc b/api/test/create_network_emulation_manager.cc
index 72efec2..089a2f8 100644
--- a/api/test/create_network_emulation_manager.cc
+++ b/api/test/create_network_emulation_manager.cc
@@ -17,8 +17,9 @@
 
 namespace webrtc {
 
-std::unique_ptr<NetworkEmulationManager> CreateNetworkEmulationManager() {
-  return std::make_unique<test::NetworkEmulationManagerImpl>();
+std::unique_ptr<NetworkEmulationManager> CreateNetworkEmulationManager(
+    TimeMode mode) {
+  return std::make_unique<test::NetworkEmulationManagerImpl>(mode);
 }
 
 }  // namespace webrtc
diff --git a/api/test/create_network_emulation_manager.h b/api/test/create_network_emulation_manager.h
index 747da1c..c57c348 100644
--- a/api/test/create_network_emulation_manager.h
+++ b/api/test/create_network_emulation_manager.h
@@ -18,7 +18,8 @@
 
 namespace webrtc {
 
-std::unique_ptr<NetworkEmulationManager> CreateNetworkEmulationManager();
+std::unique_ptr<NetworkEmulationManager> CreateNetworkEmulationManager(
+    TimeMode mode = TimeMode::kRealTime);
 
 }  // namespace webrtc
 
diff --git a/api/test/network_emulation_manager.h b/api/test/network_emulation_manager.h
index 4e5379f..a047670 100644
--- a/api/test/network_emulation_manager.h
+++ b/api/test/network_emulation_manager.h
@@ -16,6 +16,7 @@
 
 #include "api/test/network_emulation/network_emulation_interfaces.h"
 #include "api/test/simulated_network.h"
+#include "api/test/time_controller.h"
 
 #include "api/units/timestamp.h"
 #include "rtc_base/network.h"
@@ -69,6 +70,8 @@
       std::function<void(EmulatedNetworkStats)> stats_callback) const = 0;
 };
 
+enum class TimeMode { kRealTime, kSimulated };
+
 // Provides an API for creating and configuring emulated network layer.
 // All objects returned by this API are owned by NetworkEmulationManager itself
 // and will be deleted when manager will be deleted.
@@ -103,6 +106,8 @@
   };
   virtual ~NetworkEmulationManager() = default;
 
+  virtual TimeController* time_controller() = 0;
+
   // Creates an emulated network node, which represents single network in
   // the emulated network layer.
   virtual EmulatedNetworkNode* CreateEmulatedNode(
diff --git a/api/test/time_controller.cc b/api/test/time_controller.cc
new file mode 100644
index 0000000..26fe69c
--- /dev/null
+++ b/api/test/time_controller.cc
@@ -0,0 +1,27 @@
+/*
+ *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+#include "api/test/time_controller.h"
+
+namespace webrtc {
+bool TimeController::Wait(const std::function<bool()>& done,
+                          TimeDelta max_duration) {
+  // Step size is chosen to be short enough to not significantly affect latency
+  // in real time tests while being long enough to avoid adding too much load to
+  // the system.
+  const auto kStep = TimeDelta::ms(5);
+  for (auto elapsed = TimeDelta::Zero(); elapsed < max_duration;
+       elapsed += kStep) {
+    if (done())
+      return true;
+    AdvanceTime(kStep);
+  }
+  return done();
+}
+}  // namespace webrtc
diff --git a/api/test/time_controller.h b/api/test/time_controller.h
index 6c47e91..6d09481 100644
--- a/api/test/time_controller.h
+++ b/api/test/time_controller.h
@@ -23,7 +23,6 @@
 #include "system_wrappers/include/clock.h"
 
 namespace webrtc {
-
 // Interface for controlling time progress. This allows us to execute test code
 // in either real time or simulated time by using different implementation of
 // this interface.
@@ -51,6 +50,10 @@
   // Allow task queues and process threads created by this instance to execute
   // for the given |duration|.
   virtual void AdvanceTime(TimeDelta duration) = 0;
+
+  // Waits until done() == true, polling done() in small time intervals.
+  bool Wait(const std::function<bool()>& done,
+            TimeDelta max_duration = TimeDelta::seconds(5));
 };
 
 // Interface for telling time, scheduling an event to fire at a particular time,