Always enter yield policy scope using simulated TimeControllers.

This makes the class easier to use at a minor cost of making it slightly
more magic.

Bug: webrtc:9883
Change-Id: If807cfbf046615333c3bcd3b58a001813102a9f9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/161231
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Reviewed-by: Markus Handell <handellm@webrtc.org>
Commit-Queue: Per Kjellander <perkj@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30008}
diff --git a/api/test/time_controller.h b/api/test/time_controller.h
index 616622b..70aabda 100644
--- a/api/test/time_controller.h
+++ b/api/test/time_controller.h
@@ -40,12 +40,6 @@
   // Allow task queues and process threads created by this instance to execute
   // for the given |duration|.
   virtual void AdvanceTime(TimeDelta duration) = 0;
-  // Execute closure in an implementation defined scope where rtc::Event::Wait
-  // might yield to execute other tasks. This allows doing blocking waits on
-  // tasks on other task queues froma a task queue without deadlocking.
-  virtual void InvokeWithControlledYield(std::function<void()> closure) = 0;
-  // Returns a YieldInterface which can be installed as a ScopedYieldPolicy.
-  virtual rtc::YieldInterface* YieldInterface() = 0;
 };
 
 // Interface for telling time, scheduling an event to fire at a particular time,
diff --git a/test/scenario/call_client.cc b/test/scenario/call_client.cc
index f89b13f..5823656 100644
--- a/test/scenario/call_client.cc
+++ b/test/scenario/call_client.cc
@@ -321,8 +321,7 @@
 }
 
 void CallClient::SendTask(std::function<void()> task) {
-  time_controller_->InvokeWithControlledYield(
-      [&] { task_queue_.SendTask(std::move(task), RTC_FROM_HERE); });
+  task_queue_.SendTask(std::move(task), RTC_FROM_HERE);
 }
 
 CallClientPair::~CallClientPair() = default;
diff --git a/test/time_controller/external_time_controller.cc b/test/time_controller/external_time_controller.cc
index 51e5641..bb60d89 100644
--- a/test/time_controller/external_time_controller.cc
+++ b/test/time_controller/external_time_controller.cc
@@ -159,7 +159,9 @@
 };
 
 ExternalTimeController::ExternalTimeController(ControlledAlarmClock* alarm)
-    : alarm_(alarm), impl_(alarm_->GetClock()->CurrentTime()) {
+    : alarm_(alarm),
+      impl_(alarm_->GetClock()->CurrentTime()),
+      yield_policy_(&impl_) {
   global_clock_.SetTime(alarm_->GetClock()->CurrentTime());
   alarm_->SetCallback([this] { Run(); });
 }
@@ -182,16 +184,6 @@
   alarm_->Sleep(duration);
 }
 
-void ExternalTimeController::InvokeWithControlledYield(
-    std::function<void()> closure) {
-  rtc::ScopedYieldPolicy policy(YieldInterface());
-  closure();
-}
-
-rtc::YieldInterface* ExternalTimeController::YieldInterface() {
-  return &impl_;
-}
-
 std::unique_ptr<TaskQueueBase, TaskQueueDeleter>
 ExternalTimeController::CreateTaskQueue(
     absl::string_view name,
diff --git a/test/time_controller/external_time_controller.h b/test/time_controller/external_time_controller.h
index 3ae302e..869a78f 100644
--- a/test/time_controller/external_time_controller.h
+++ b/test/time_controller/external_time_controller.h
@@ -38,8 +38,6 @@
   std::unique_ptr<ProcessThread> CreateProcessThread(
       const char* thread_name) override;
   void AdvanceTime(TimeDelta duration) override;
-  void InvokeWithControlledYield(std::function<void()> closure) override;
-  rtc::YieldInterface* YieldInterface() override;
 
   // Implementation of TaskQueueFactory.
   std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
@@ -59,6 +57,7 @@
 
   ControlledAlarmClock* alarm_;
   sim_time_impl::SimulatedTimeControllerImpl impl_;
+  rtc::ScopedYieldPolicy yield_policy_;
 
   // Overrides the global rtc::Clock to ensure that it reports the same times as
   // the time controller.
diff --git a/test/time_controller/external_time_controller_unittest.cc b/test/time_controller/external_time_controller_unittest.cc
index b0b09cb..d93b42a 100644
--- a/test/time_controller/external_time_controller_unittest.cc
+++ b/test/time_controller/external_time_controller_unittest.cc
@@ -138,11 +138,9 @@
       time_simulation.GetTaskQueueFactory()->CreateTaskQueue(
           "TestQueue", TaskQueueFactory::Priority::NORMAL));
 
-  time_simulation.InvokeWithControlledYield([&] {
-    rtc::Event event;
-    task_queue.PostTask([&] { event.Set(); });
-    EXPECT_TRUE(event.Wait(200));
-  });
+  rtc::Event event;
+  task_queue.PostTask([&] { event.Set(); });
+  EXPECT_TRUE(event.Wait(200));
 }
 
 TEST(ExternalTimeControllerTest, TasksYieldToEachOther) {
diff --git a/test/time_controller/real_time_controller.cc b/test/time_controller/real_time_controller.cc
index 0494bc0..732f1bd 100644
--- a/test/time_controller/real_time_controller.cc
+++ b/test/time_controller/real_time_controller.cc
@@ -34,15 +34,6 @@
   SleepMs(duration.ms());
 }
 
-void RealTimeController::InvokeWithControlledYield(
-    std::function<void()> closure) {
-  closure();
-}
-
-rtc::YieldInterface* RealTimeController::YieldInterface() {
-  return nullptr;
-}
-
 RealTimeController* GlobalRealTimeController() {
   static RealTimeController* time_controller = new RealTimeController();
   return time_controller;
diff --git a/test/time_controller/real_time_controller.h b/test/time_controller/real_time_controller.h
index 58d7682..873ef90 100644
--- a/test/time_controller/real_time_controller.h
+++ b/test/time_controller/real_time_controller.h
@@ -29,8 +29,6 @@
   std::unique_ptr<ProcessThread> CreateProcessThread(
       const char* thread_name) override;
   void AdvanceTime(TimeDelta duration) override;
-  void InvokeWithControlledYield(std::function<void()> closure) override;
-  rtc::YieldInterface* YieldInterface() override;
 
  private:
   std::unique_ptr<TaskQueueFactory> task_queue_factory_;
diff --git a/test/time_controller/simulated_time_controller.cc b/test/time_controller/simulated_time_controller.cc
index c2c135a..4c8a1e1 100644
--- a/test/time_controller/simulated_time_controller.cc
+++ b/test/time_controller/simulated_time_controller.cc
@@ -399,7 +399,7 @@
 
 GlobalSimulatedTimeController::GlobalSimulatedTimeController(
     Timestamp start_time)
-    : sim_clock_(start_time.us()), impl_(start_time) {
+    : sim_clock_(start_time.us()), impl_(start_time), yield_policy_(&impl_) {
   global_clock_.SetTime(start_time);
 }
 
@@ -434,16 +434,4 @@
   }
 }
 
-void GlobalSimulatedTimeController::InvokeWithControlledYield(
-    std::function<void()> closure) {
-  rtc::ScopedYieldPolicy yield_policy(&impl_);
-  closure();
-}
-
-rtc::YieldInterface* GlobalSimulatedTimeController::YieldInterface() {
-  return &impl_;
-}
-
-// namespace sim_time_impl
-
 }  // namespace webrtc
diff --git a/test/time_controller/simulated_time_controller.h b/test/time_controller/simulated_time_controller.h
index 919b858..8725b06 100644
--- a/test/time_controller/simulated_time_controller.h
+++ b/test/time_controller/simulated_time_controller.h
@@ -91,14 +91,13 @@
   std::unique_ptr<ProcessThread> CreateProcessThread(
       const char* thread_name) override;
   void AdvanceTime(TimeDelta duration) override;
-  void InvokeWithControlledYield(std::function<void()> closure) override;
-  rtc::YieldInterface* YieldInterface() override;
 
  private:
   rtc::ScopedBaseFakeClock global_clock_;
   // Provides simulated CurrentNtpInMilliseconds()
   SimulatedClock sim_clock_;
   sim_time_impl::SimulatedTimeControllerImpl impl_;
+  rtc::ScopedYieldPolicy yield_policy_;
 };
 }  // namespace webrtc
 
diff --git a/video/video_receive_stream_unittest.cc b/video/video_receive_stream_unittest.cc
index b6c4200..503660e 100644
--- a/video/video_receive_stream_unittest.cc
+++ b/video/video_receive_stream_unittest.cc
@@ -493,26 +493,18 @@
                               &call_stats_,
                               time_controller_.GetClock(),
                               new VCMTiming(time_controller_.GetClock())) {
-    time_controller_.InvokeWithControlledYield(
-        [this] { video_receive_stream_.Start(); });
-  }
-
-  ~VideoReceiveStreamTestWithSimulatedClock() {
-    time_controller_.InvokeWithControlledYield(
-        [this] { video_receive_stream_.Stop(); });
+    video_receive_stream_.Start();
   }
 
   void OnFrameDecoded() { event_->Set(); }
 
   void PassEncodedFrameAndWait(
       std::unique_ptr<video_coding::EncodedFrame> frame) {
-    time_controller_.InvokeWithControlledYield([this, &frame] {
       event_ = std::make_unique<rtc::Event>();
       // This call will eventually end up in the Decoded method where the
       // event is set.
       video_receive_stream_.OnCompleteFrame(std::move(frame));
       event_->Wait(rtc::Event::kForever);
-    });
   }
 
  protected: