Reland In GenericEncoder enable timing frames for encoders with internal source

The original cl broke some downstream project because some internal source
encoders do not call OnBitrateChanged on GenericEncoder.

Bug: webrtc:9058
Change-Id: I7841c65059fb4fc9e1ab9754bb1d232ce660a990
Reviewed-on: https://webrtc-review.googlesource.com/66342
Reviewed-by: Erik Språng <sprang@webrtc.org>
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22733}
diff --git a/modules/video_coding/generic_encoder_unittest.cc b/modules/video_coding/generic_encoder_unittest.cc
index 2b788c5..b022525 100644
--- a/modules/video_coding/generic_encoder_unittest.cc
+++ b/modules/video_coding/generic_encoder_unittest.cc
@@ -13,6 +13,7 @@
 #include "modules/video_coding/encoded_frame.h"
 #include "modules/video_coding/generic_encoder.h"
 #include "modules/video_coding/include/video_coding_defines.h"
+#include "rtc_base/fakeclock.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -208,6 +209,46 @@
   EXPECT_FALSE(sink.WasTimingFrame());
 }
 
+TEST(TestVCMEncodedFrameCallback, AdjustsCaptureTimeForInternalSourceEncoder) {
+  rtc::ScopedFakeClock clock;
+  clock.SetTimeMicros(1234567);
+  EncodedImage image;
+  CodecSpecificInfo codec_specific;
+  const int64_t kEncodeStartDelayMs = 2;
+  const int64_t kEncodeFinishDelayMs = 10;
+  int64_t timestamp = 1;
+  image._length = 500;
+  image.capture_time_ms_ = timestamp;
+  image._timeStamp = static_cast<uint32_t>(timestamp * 90);
+  codec_specific.codecType = kVideoCodecGeneric;
+  codec_specific.codecSpecific.generic.simulcast_idx = 0;
+  FakeEncodedImageCallback sink;
+  VCMEncodedFrameCallback callback(&sink, nullptr);
+  callback.SetInternalSource(true);
+  VideoCodec::TimingFrameTriggerThresholds thresholds;
+  thresholds.delay_ms = 1;  // Make all frames timing frames.
+  callback.SetTimingFramesThresholds(thresholds);
+  callback.OnTargetBitrateChanged(500, 0);
+
+  // Verify a single frame without encode timestamps isn't a timing frame.
+  callback.OnEncodedImage(image, &codec_specific, nullptr);
+  EXPECT_FALSE(sink.WasTimingFrame());
+
+  // New frame, but this time with encode timestamps set in timing_.
+  // This should be a timing frame.
+  image.capture_time_ms_ = ++timestamp;
+  image._timeStamp = static_cast<uint32_t>(timestamp * 90);
+  image.timing_.encode_start_ms = timestamp + kEncodeStartDelayMs;
+  image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
+  callback.OnEncodedImage(image, &codec_specific, nullptr);
+  EXPECT_TRUE(sink.WasTimingFrame());
+  // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
+  // capture timestamp should be kEncodeFinishDelayMs in the past.
+  EXPECT_EQ(
+      sink.GetLastCaptureTimestamp(),
+      clock.TimeNanos() / rtc::kNumNanosecsPerMillisec - kEncodeFinishDelayMs);
+}
+
 TEST(TestVCMEncodedFrameCallback, NotifiesAboutDroppedFrames) {
   EncodedImage image;
   CodecSpecificInfo codec_specific;