Reduce time that video analyzer holds the frame in pc level framework

Bug: None
Change-Id: Ie669f3d8ff4f9f5b7900bcb11d13a5f7f579ce44
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/179526
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Commit-Queue: Andrey Logvin <landrey@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31765}
diff --git a/test/pc/e2e/analyzer/video/default_video_quality_analyzer.cc b/test/pc/e2e/analyzer/video/default_video_quality_analyzer.cc
index db86607..000d165 100644
--- a/test/pc/e2e/analyzer/video/default_video_quality_analyzer.cc
+++ b/test/pc/e2e/analyzer/video/default_video_quality_analyzer.cc
@@ -390,11 +390,11 @@
 
 void DefaultVideoQualityAnalyzer::OnFrameRendered(
     absl::string_view peer_name,
-    const webrtc::VideoFrame& raw_frame) {
+    const webrtc::VideoFrame& frame) {
   MutexLock lock(&lock_);
   size_t peer_index = peers_->index(peer_name);
 
-  auto frame_it = captured_frames_in_flight_.find(raw_frame.id());
+  auto frame_it = captured_frames_in_flight_.find(frame.id());
   if (frame_it == captured_frames_in_flight_.end() ||
       frame_it->second.HasRenderedTime(peer_index)) {
     // It means this frame was rendered before, so we can skip it. It may happen
@@ -405,12 +405,6 @@
     return;
   }
 
-  // Copy entire video frame including video buffer to ensure that analyzer
-  // won't hold any WebRTC internal buffers.
-  VideoFrame frame = raw_frame;
-  frame.set_video_frame_buffer(
-      I420Buffer::Copy(*raw_frame.video_frame_buffer()->ToI420()));
-
   // Find corresponding captured frame.
   FrameInFlight* frame_in_flight = &frame_it->second;
   absl::optional<VideoFrame> captured_frame = frame_in_flight->frame();
diff --git a/test/pc/e2e/analyzer/video/video_quality_analyzer_injection_helper.cc b/test/pc/e2e/analyzer/video/video_quality_analyzer_injection_helper.cc
index dfd6a8d..48e65ef 100644
--- a/test/pc/e2e/analyzer/video/video_quality_analyzer_injection_helper.cc
+++ b/test/pc/e2e/analyzer/video/video_quality_analyzer_injection_helper.cc
@@ -184,11 +184,18 @@
 
 void VideoQualityAnalyzerInjectionHelper::OnFrame(absl::string_view peer_name,
                                                   const VideoFrame& frame) {
-  if (IsDummyFrameBuffer(frame.video_frame_buffer()->ToI420())) {
+  rtc::scoped_refptr<I420BufferInterface> i420_buffer =
+      frame.video_frame_buffer()->ToI420();
+  if (IsDummyFrameBuffer(i420_buffer)) {
     // This is dummy frame, so we  don't need to process it further.
     return;
   }
-  analyzer_->OnFrameRendered(peer_name, frame);
+  // Copy entire video frame including video buffer to ensure that analyzer
+  // won't hold any WebRTC internal buffers.
+  VideoFrame frame_copy = frame;
+  frame_copy.set_video_frame_buffer(I420Buffer::Copy(*i420_buffer));
+  analyzer_->OnFrameRendered(peer_name, frame_copy);
+
   std::string stream_label = analyzer_->GetStreamLabel(frame.id());
   std::vector<std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>>>* sinks =
       PopulateSinks(stream_label);
diff --git a/test/pc/e2e/analyzer/video/video_quality_analyzer_injection_helper.h b/test/pc/e2e/analyzer/video/video_quality_analyzer_injection_helper.h
index 6139cfd..d741288 100644
--- a/test/pc/e2e/analyzer/video/video_quality_analyzer_injection_helper.h
+++ b/test/pc/e2e/analyzer/video/video_quality_analyzer_injection_helper.h
@@ -107,6 +107,8 @@
   test::VideoFrameWriter* MaybeCreateVideoWriter(
       absl::optional<std::string> file_name,
       const PeerConnectionE2EQualityTestFixture::VideoConfig& config);
+  // Creates a deep copy of the frame and passes it to the video analyzer, while
+  // passing real frame to the sinks
   void OnFrame(absl::string_view peer_name, const VideoFrame& frame);
   std::vector<std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>>>*
   PopulateSinks(const std::string& stream_label);