Don't restart streams in scenario tests.

This CL changes the behavior for RunFor and RunUntil so they do not
anymore restart the underlying streams every time they are called.

This has a side effect on the semantics of the calls. Previously,
both RunUntil and RunFor would restart the session and run until the
given time had passed. Now RunFor will still run for the provided
duration, however, to make the name of RunUntil more correct, it
will run until the time since start is equal to the max_duration
parameter. An extra overload of RunUntil was added to allow using
this behavior without providing an ending condition.

Bug: webrtc:9510
Change-Id: I9fe56a44116907fba3d102894b5c96af2ba6cffb
Reviewed-on: https://webrtc-review.googlesource.com/c/111502
Reviewed-by: Christoffer Rodbro <crodbro@webrtc.org>
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25726}
diff --git a/test/scenario/audio_stream.cc b/test/scenario/audio_stream.cc
index 076b4ea..8359bd0 100644
--- a/test/scenario/audio_stream.cc
+++ b/test/scenario/audio_stream.cc
@@ -152,6 +152,7 @@
 
 void SendAudioStream::Start() {
   send_stream_->Start();
+  sender_->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
 }
 
 ColumnPrinter SendAudioStream::StatsPrinter() {
@@ -192,6 +193,11 @@
   receiver_->call_->DestroyAudioReceiveStream(receive_stream_);
 }
 
+void ReceiveAudioStream::Start() {
+  receive_stream_->Start();
+  receiver_->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
+}
+
 AudioStreamPair::~AudioStreamPair() = default;
 
 AudioStreamPair::AudioStreamPair(
diff --git a/test/scenario/audio_stream.h b/test/scenario/audio_stream.h
index 601a375..3ab0a1f 100644
--- a/test/scenario/audio_stream.h
+++ b/test/scenario/audio_stream.h
@@ -50,6 +50,7 @@
  public:
   RTC_DISALLOW_COPY_AND_ASSIGN(ReceiveAudioStream);
   ~ReceiveAudioStream();
+  void Start();
 
  private:
   friend class Scenario;
diff --git a/test/scenario/scenario.cc b/test/scenario/scenario.cc
index 9dcdcbe..7589878 100644
--- a/test/scenario/scenario.cc
+++ b/test/scenario/scenario.cc
@@ -75,6 +75,8 @@
 }
 
 Scenario::~Scenario() {
+  if (start_time_.IsFinite())
+    Stop();
   if (!real_time_mode_)
     rtc::SetClockForTesting(nullptr);
 }
@@ -305,35 +307,18 @@
 }
 
 void Scenario::RunFor(TimeDelta duration) {
-  RunUntil(duration, TimeDelta::PlusInfinity(), []() { return false; });
+  RunUntil(Duration() + duration);
+}
+
+void Scenario::RunUntil(TimeDelta max_duration) {
+  RunUntil(max_duration, TimeDelta::PlusInfinity(), []() { return false; });
 }
 
 void Scenario::RunUntil(TimeDelta max_duration,
                         TimeDelta poll_interval,
                         std::function<bool()> exit_function) {
-  start_time_ = Timestamp::us(clock_->TimeInMicroseconds());
-  for (auto& activity : repeated_activities_) {
-    activity->SetStartTime(start_time_);
-  }
-
-  for (auto& stream_pair : video_streams_)
-    stream_pair->receive()->receive_stream_->Start();
-  for (auto& stream_pair : audio_streams_)
-    stream_pair->receive()->receive_stream_->Start();
-  for (auto& stream_pair : video_streams_) {
-    if (stream_pair->config_.autostart) {
-      stream_pair->send()->Start();
-    }
-  }
-  for (auto& stream_pair : audio_streams_) {
-    if (stream_pair->config_.autostart) {
-      stream_pair->send()->Start();
-    }
-  }
-  for (auto& call : clients_) {
-    call->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
-    call->call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
-  }
+  if (start_time_.IsInfinite())
+    Start();
 
   rtc::Event done_;
   while (!exit_function() && Duration() < max_duration) {
@@ -363,6 +348,32 @@
                                            1000);
     }
   }
+}
+
+void Scenario::Start() {
+  start_time_ = Timestamp::us(clock_->TimeInMicroseconds());
+  for (auto& activity : repeated_activities_) {
+    activity->SetStartTime(start_time_);
+  }
+
+  for (auto& stream_pair : video_streams_)
+    stream_pair->receive()->Start();
+  for (auto& stream_pair : audio_streams_)
+    stream_pair->receive()->Start();
+  for (auto& stream_pair : video_streams_) {
+    if (stream_pair->config_.autostart) {
+      stream_pair->send()->Start();
+    }
+  }
+  for (auto& stream_pair : audio_streams_) {
+    if (stream_pair->config_.autostart) {
+      stream_pair->send()->Start();
+    }
+  }
+}
+
+void Scenario::Stop() {
+  RTC_DCHECK(start_time_.IsFinite());
   for (auto& stream_pair : video_streams_) {
     stream_pair->send()->video_capturer_->Stop();
     stream_pair->send()->send_stream_->Stop();
@@ -373,6 +384,7 @@
     stream_pair->receive()->receive_stream_->Stop();
   for (auto& stream_pair : audio_streams_)
     stream_pair->receive()->receive_stream_->Stop();
+  start_time_ = Timestamp::PlusInfinity();
 }
 
 Timestamp Scenario::Now() {
@@ -380,6 +392,8 @@
 }
 
 TimeDelta Scenario::Duration() {
+  if (start_time_.IsInfinite())
+    return TimeDelta::Zero();
   return Now() - start_time_;
 }
 
diff --git a/test/scenario/scenario.h b/test/scenario/scenario.h
index 5ee017c..38bef44 100644
--- a/test/scenario/scenario.h
+++ b/test/scenario/scenario.h
@@ -140,9 +140,12 @@
   // Runs the scenario for the given time or until the exit function returns
   // true.
   void RunFor(TimeDelta duration);
+  void RunUntil(TimeDelta max_duration);
   void RunUntil(TimeDelta max_duration,
                 TimeDelta probe_interval,
                 std::function<bool()> exit_function);
+  void Start();
+  void Stop();
 
   // Triggers sending of dummy packets over the given nodes.
   void TriggerPacketBurst(std::vector<NetworkNode*> over_nodes,
diff --git a/test/scenario/video_stream.cc b/test/scenario/video_stream.cc
index 528a9cc2..3639923 100644
--- a/test/scenario/video_stream.cc
+++ b/test/scenario/video_stream.cc
@@ -256,6 +256,7 @@
 void SendVideoStream::Start() {
   send_stream_->Start();
   video_capturer_->Start();
+  sender_->call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
 }
 
 void SendVideoStream::UpdateConfig(
@@ -375,6 +376,11 @@
     receiver_->call_->DestroyFlexfecReceiveStream(flecfec_stream_);
 }
 
+void ReceiveVideoStream::Start() {
+  receive_stream_->Start();
+  receiver_->call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
+}
+
 VideoStreamPair::~VideoStreamPair() = default;
 
 VideoStreamPair::VideoStreamPair(CallClient* sender,
diff --git a/test/scenario/video_stream.h b/test/scenario/video_stream.h
index a68dbf6..ebf9c95 100644
--- a/test/scenario/video_stream.h
+++ b/test/scenario/video_stream.h
@@ -65,6 +65,7 @@
  public:
   RTC_DISALLOW_COPY_AND_ASSIGN(ReceiveVideoStream);
   ~ReceiveVideoStream();
+  void Start();
 
  private:
   friend class Scenario;