Reland "Always use real VideoStreamsFactory in full stack tests"

Reland with fixes. Previous iteration affected media bitrate in bunch of tests.

Always use real VideoStreamsFactory in full stack tests

Because quality scaling is enabled now in full stack test, correct
factory should be used to compute actual resolution.

Also, since analyzed stream may be disabled completely now, change how
analyzer considers the test finished --- count captured frames and
stop if required amount of frames is captured and no new comparison were made.

Original Reviewed-on: https://webrtc-review.googlesource.com/c/118687

Bug: webrtc:10204
Change-Id: Id1d9066add185d56fe3cb6856b700d350576c6b2
Reviewed-on: https://webrtc-review.googlesource.com/c/119950
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Åsa Persson <asapersson@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26460}
diff --git a/video/video_analyzer.cc b/video/video_analyzer.cc
index c81c9b7..6535133 100644
--- a/video/video_analyzer.cc
+++ b/video/video_analyzer.cc
@@ -36,6 +36,12 @@
 namespace {
 constexpr int kSendStatsPollingIntervalMs = 1000;
 constexpr size_t kMaxComparisons = 10;
+// How often is keep alive message printed.
+constexpr int kKeepAliveIntervalSeconds = 30;
+// Interval between checking that the test is over.
+constexpr int kProbingIntervalMs = 500;
+constexpr int kKeepAliveIntervalIterations =
+    kKeepAliveIntervalSeconds * 1000 / kProbingIntervalMs;
 
 bool IsFlexfec(int payload_type) {
   return payload_type == test::CallTest::kFlexfecPayloadType;
@@ -63,7 +69,7 @@
       send_stream_(nullptr),
       receive_stream_(nullptr),
       audio_receive_stream_(nullptr),
-      captured_frame_forwarder_(this, clock),
+      captured_frame_forwarder_(this, clock, duration_frames),
       test_label_(test_label),
       graph_data_output_file_(graph_data_output_file),
       graph_title_(graph_title),
@@ -77,6 +83,7 @@
       frames_recorded_(0),
       frames_processed_(0),
       dropped_frames_(0),
+      captured_frames_(0),
       dropped_frames_before_first_encode_(0),
       dropped_frames_before_rendering_(0),
       last_render_time_(0),
@@ -333,12 +340,16 @@
   stats_polling_thread_.Start();
 
   int last_frames_processed = -1;
+  int last_frames_captured = -1;
   int iteration = 0;
-  while (!done_.Wait(test::CallTest::kDefaultTimeoutMs)) {
+
+  while (!done_.Wait(kProbingIntervalMs)) {
     int frames_processed;
+    int frames_captured;
     {
       rtc::CritScope crit(&comparison_lock_);
       frames_processed = frames_processed_;
+      frames_captured = captured_frames_;
     }
 
     // Print some output so test infrastructure won't think we've crashed.
@@ -346,24 +357,35 @@
         "Uh, I'm-I'm not quite dead, sir.",
         "Uh, I-I think uh, I could pull through, sir.",
         "Actually, I think I'm all right to come with you--"};
-    printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
+    if (++iteration % kKeepAliveIntervalIterations == 0) {
+      printf("- %s\n", kKeepAliveMessages[iteration % 3]);
+    }
 
     if (last_frames_processed == -1) {
       last_frames_processed = frames_processed;
+      last_frames_captured = frames_captured;
       continue;
     }
-    if (frames_processed == last_frames_processed) {
-      EXPECT_GT(frames_processed, last_frames_processed)
-          << "Analyzer stalled while waiting for test to finish.";
+    if (frames_processed == last_frames_processed &&
+        last_frames_captured == frames_captured) {
+      if (frames_captured < frames_to_process_) {
+        EXPECT_GT(frames_processed, last_frames_processed)
+            << "Analyzer stalled while waiting for test to finish.";
+      }
       done_.Set();
       break;
     }
     last_frames_processed = frames_processed;
+    last_frames_captured = frames_captured;
   }
 
   if (iteration > 0)
     printf("- Farewell, sweet Concorde!\n");
 
+  PrintResults();
+  if (graph_data_output_file_)
+    PrintSamplesToFile();
+
   stats_polling_thread_.Stop();
 }
 
@@ -516,9 +538,6 @@
   StopExcludingCpuThreadTime();
 
   if (FrameProcessed()) {
-    PrintResults();
-    if (graph_data_output_file_)
-      PrintSamplesToFile();
     done_.Set();
     comparison_available_event_.Set();
     return false;
@@ -564,6 +583,11 @@
 
 void VideoAnalyzer::PrintResults() {
   StopMeasuringCpuProcessTime();
+  int frames_left;
+  {
+    rtc::CritScope crit(&crit_);
+    frames_left = frames_.size();
+  }
   rtc::CritScope crit(&comparison_lock_);
   // Record the time from the last freeze until the last rendered frame to
   // ensure we cover the full timespan of the session. Otherwise the metric
@@ -592,7 +616,8 @@
   if (receive_stream_ != nullptr) {
     PrintResult("decode_time", decode_time_ms_, " ms");
   }
-
+  dropped_frames_ += dropped_frames_before_first_encode_ +
+                     dropped_frames_before_rendering_ + frames_left;
   test::PrintResult("dropped_frames", "", test_label_.c_str(), dropped_frames_,
                     "frames", false);
   test::PrintResult("cpu_usage", "", test_label_.c_str(), GetCpuUsagePercent(),
@@ -753,7 +778,10 @@
 void VideoAnalyzer::AddCapturedFrameForComparison(
     const VideoFrame& video_frame) {
   rtc::CritScope lock(&crit_);
-  frames_.push_back(video_frame);
+  if (captured_frames_ < frames_to_process_) {
+    ++captured_frames_;
+    frames_.push_back(video_frame);
+  }
 }
 
 void VideoAnalyzer::AddFrameComparison(const VideoFrame& reference,
@@ -844,11 +872,14 @@
 
 VideoAnalyzer::CapturedFrameForwarder::CapturedFrameForwarder(
     VideoAnalyzer* analyzer,
-    Clock* clock)
+    Clock* clock,
+    int frames_to_process)
     : analyzer_(analyzer),
       send_stream_input_(nullptr),
       video_source_(nullptr),
-      clock_(clock) {}
+      clock_(clock),
+      captured_frames_(0),
+      frames_to_process_(frames_to_process) {}
 
 void VideoAnalyzer::CapturedFrameForwarder::SetSource(
     VideoSourceInterface<VideoFrame>* video_source) {
@@ -866,7 +897,8 @@
   copy.set_timestamp(copy.ntp_time_ms() * 90);
   analyzer_->AddCapturedFrameForComparison(copy);
   rtc::CritScope lock(&crit_);
-  if (send_stream_input_)
+  ++captured_frames_;
+  if (send_stream_input_ && captured_frames_ <= frames_to_process_)
     send_stream_input_->OnFrame(copy);
 }