Delete ortc methods using cricket::VideoCapturer.

For testing, introduce new class FakePeriodicVideoSource.

Bug: webrtc:6353
Change-Id: I234dbd4176ea23d21b8f876ee02d3662477f225e
Reviewed-on: https://webrtc-review.googlesource.com/49220
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22554}
diff --git a/api/ortc/ortcfactoryinterface.h b/api/ortc/ortcfactoryinterface.h
index d99fcd4..ea25c8a 100644
--- a/api/ortc/ortcfactoryinterface.h
+++ b/api/ortc/ortcfactoryinterface.h
@@ -215,20 +215,6 @@
     return CreateAudioSource(cricket::AudioOptions());
   }
 
-  // Creates a video source object wrapping and taking ownership of |capturer|.
-  //
-  // |constraints| can be used for selection of resolution and frame rate, and
-  // may be null if no constraints are desired.
-  virtual rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
-      std::unique_ptr<cricket::VideoCapturer> capturer,
-      const MediaConstraintsInterface* constraints) = 0;
-
-  // Version of the above method that omits |constraints|.
-  rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
-      std::unique_ptr<cricket::VideoCapturer> capturer) {
-    return CreateVideoSource(std::move(capturer), nullptr);
-  }
-
   // Creates a new local video track wrapping |source|. The same |source| can
   // be used in several tracks.
   virtual rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
diff --git a/ortc/BUILD.gn b/ortc/BUILD.gn
index bf66c44..d2c8cb2 100644
--- a/ortc/BUILD.gn
+++ b/ortc/BUILD.gn
@@ -87,6 +87,7 @@
       "../p2p:p2p_test_utils",
       "../p2p:rtc_p2p",
       "../pc:pc_test_utils",
+      "../pc:peerconnection",
       "../rtc_base:rtc_base",
       "../rtc_base:rtc_base_approved",
       "../rtc_base:rtc_base_tests_main",
diff --git a/ortc/ortcfactory.cc b/ortc/ortcfactory.cc
index f891561..506fc86 100644
--- a/ortc/ortcfactory.cc
+++ b/ortc/ortcfactory.cc
@@ -32,7 +32,6 @@
 #include "pc/audiotrack.h"
 #include "pc/channelmanager.h"
 #include "pc/localaudiosource.h"
-#include "pc/videocapturertracksource.h"
 #include "pc/videotrack.h"
 #include "rtc_base/asyncpacketsocket.h"
 #include "rtc_base/bind.h"
@@ -114,10 +113,6 @@
 PROXY_METHOD1(rtc::scoped_refptr<AudioSourceInterface>,
               CreateAudioSource,
               const cricket::AudioOptions&)
-PROXY_METHOD2(rtc::scoped_refptr<VideoTrackSourceInterface>,
-              CreateVideoSource,
-              std::unique_ptr<cricket::VideoCapturer>,
-              const MediaConstraintsInterface*)
 PROXY_METHOD2(rtc::scoped_refptr<VideoTrackInterface>,
               CreateVideoTrack,
               const std::string&,
@@ -480,17 +475,6 @@
       LocalAudioSource::Create(&options));
 }
 
-rtc::scoped_refptr<VideoTrackSourceInterface> OrtcFactory::CreateVideoSource(
-    std::unique_ptr<cricket::VideoCapturer> capturer,
-    const MediaConstraintsInterface* constraints) {
-  RTC_DCHECK_RUN_ON(signaling_thread_);
-  rtc::scoped_refptr<VideoTrackSourceInterface> source(
-      VideoCapturerTrackSource::Create(
-          worker_thread_.get(), std::move(capturer), constraints, false));
-  return VideoTrackSourceProxy::Create(signaling_thread_, worker_thread_.get(),
-                                       source);
-}
-
 rtc::scoped_refptr<VideoTrackInterface> OrtcFactory::CreateVideoTrack(
     const std::string& id,
     VideoTrackSourceInterface* source) {
diff --git a/ortc/ortcfactory.h b/ortc/ortcfactory.h
index 750fcf7..d72f612 100644
--- a/ortc/ortcfactory.h
+++ b/ortc/ortcfactory.h
@@ -81,10 +81,6 @@
   rtc::scoped_refptr<AudioSourceInterface> CreateAudioSource(
       const cricket::AudioOptions& options) override;
 
-  rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
-      std::unique_ptr<cricket::VideoCapturer> capturer,
-      const MediaConstraintsInterface* constraints) override;
-
   rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
       const std::string& id,
       VideoTrackSourceInterface* source) override;
diff --git a/ortc/ortcfactory_integrationtest.cc b/ortc/ortcfactory_integrationtest.cc
index def0e08..d556fb1 100644
--- a/ortc/ortcfactory_integrationtest.cc
+++ b/ortc/ortcfactory_integrationtest.cc
@@ -17,8 +17,9 @@
 #include "ortc/testrtpparameters.h"
 #include "p2p/base/udptransport.h"
 #include "pc/test/fakeaudiocapturemodule.h"
-#include "pc/test/fakeperiodicvideocapturer.h"
+#include "pc/test/fakeperiodicvideosource.h"
 #include "pc/test/fakevideotrackrenderer.h"
+#include "pc/videotracksource.h"
 #include "rtc_base/criticalsection.h"
 #include "rtc_base/fakenetwork.h"
 #include "rtc_base/gunit.h"
@@ -28,6 +29,7 @@
 
 const int kDefaultTimeout = 10000;    // 10 seconds.
 const int kReceivingDuration = 1000;  // 1 second.
+
 // Default number of audio/video frames to wait for before considering a test a
 // success.
 const int kDefaultNumFrames = 3;
@@ -217,18 +219,18 @@
     return ortc_factory->CreateAudioTrack(id, source);
   }
 
-  // Stores created capturer in |fake_video_capturers_|.
+  // Stores created video source in |fake_video_sources_|.
   rtc::scoped_refptr<webrtc::VideoTrackInterface>
-  CreateLocalVideoTrackAndFakeCapturer(const std::string& id,
-                                       OrtcFactoryInterface* ortc_factory) {
-    webrtc::FakePeriodicVideoCapturer* fake_capturer =
-        new webrtc::FakePeriodicVideoCapturer();
-    fake_video_capturers_.push_back(fake_capturer);
-    rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
-        ortc_factory->CreateVideoSource(
-            std::unique_ptr<cricket::VideoCapturer>(fake_capturer));
-    return rtc::scoped_refptr<webrtc::VideoTrackInterface>(
-        ortc_factory->CreateVideoTrack(id, source));
+  CreateLocalVideoTrackAndFakeSource(const std::string& id,
+                                     OrtcFactoryInterface* ortc_factory) {
+    fake_video_sources_.emplace_back(
+        rtc::MakeUnique<FakePeriodicVideoSource>());
+    fake_video_track_sources_.emplace_back(
+        new rtc::RefCountedObject<VideoTrackSource>(
+            fake_video_sources_.back().get(), false /* remote */));
+    return rtc::scoped_refptr<VideoTrackInterface>(
+        ortc_factory->CreateVideoTrack(
+            id, fake_video_track_sources_.back()));
   }
 
   // Helper function used to test two way RTP senders and receivers with basic
@@ -283,13 +285,13 @@
         CreateLocalAudioTrack("audio", ortc_factory1_.get()));
     EXPECT_TRUE(error.ok());
     error = video_sender1->SetTrack(
-        CreateLocalVideoTrackAndFakeCapturer("video", ortc_factory1_.get()));
+        CreateLocalVideoTrackAndFakeSource("video", ortc_factory1_.get()));
     EXPECT_TRUE(error.ok());
     error = audio_sender2->SetTrack(
         CreateLocalAudioTrack("audio", ortc_factory2_.get()));
     EXPECT_TRUE(error.ok());
     error = video_sender2->SetTrack(
-        CreateLocalVideoTrackAndFakeCapturer("video", ortc_factory2_.get()));
+        CreateLocalVideoTrackAndFakeSource("video", ortc_factory2_.get()));
     EXPECT_TRUE(error.ok());
 
     // "sent_X_parameters1" are the parameters that endpoint 1 sends with and
@@ -350,8 +352,8 @@
   rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module2_;
   std::unique_ptr<OrtcFactoryInterface> ortc_factory1_;
   std::unique_ptr<OrtcFactoryInterface> ortc_factory2_;
-  // Actually owned by video tracks.
-  std::vector<webrtc::FakePeriodicVideoCapturer*> fake_video_capturers_;
+  std::vector<std::unique_ptr<FakePeriodicVideoSource>> fake_video_sources_;
+  std::vector<rtc::scoped_refptr<VideoTrackSource>> fake_video_track_sources_;
   int received_audio_frames1_ = 0;
   int received_audio_frames2_ = 0;
   int rendered_video_frames1_ = 0;
@@ -415,7 +417,7 @@
   auto receiver = receiver_result.MoveValue();
 
   RTCError error = sender->SetTrack(
-      CreateLocalVideoTrackAndFakeCapturer("video", ortc_factory1_.get()));
+      CreateLocalVideoTrackAndFakeSource("video", ortc_factory1_.get()));
   EXPECT_TRUE(error.ok());
 
   RtpParameters vp8_parameters = MakeMinimalVp8Parameters();
@@ -450,7 +452,7 @@
   auto receiver = receiver_result.MoveValue();
 
   RTCError error = sender->SetTrack(
-      CreateLocalVideoTrackAndFakeCapturer("video_1", ortc_factory1_.get()));
+      CreateLocalVideoTrackAndFakeSource("video_1", ortc_factory1_.get()));
   EXPECT_TRUE(error.ok());
   RtpParameters vp8_parameters = MakeMinimalVp8Parameters();
   EXPECT_TRUE(receiver->Receive(vp8_parameters).ok());
@@ -460,13 +462,15 @@
   // Expect for some initial number of frames to be received.
   EXPECT_TRUE_WAIT(fake_renderer.num_rendered_frames() > kDefaultNumFrames,
                    kDefaultTimeout);
-  // Stop the old capturer, set a new track, and verify new frames are received
-  // from the new track. Stopping the old capturer ensures that we aren't
-  // actually still getting frames from it.
-  fake_video_capturers_[0]->StopFrameDelivery();
+  // Destroy old source, set a new track, and verify new frames are received
+  // from the new track. The VideoTrackSource is reference counted and may live
+  // a little longer, so tell it that its source is going away now.
+  fake_video_track_sources_[0]->OnSourceDestroyed();
+  fake_video_track_sources_[0] = nullptr;
+  fake_video_sources_[0].reset();
   int prev_num_frames = fake_renderer.num_rendered_frames();
   error = sender->SetTrack(
-      CreateLocalVideoTrackAndFakeCapturer("video_2", ortc_factory1_.get()));
+      CreateLocalVideoTrackAndFakeSource("video_2", ortc_factory1_.get()));
   EXPECT_TRUE(error.ok());
   EXPECT_TRUE_WAIT(
       fake_renderer.num_rendered_frames() > kDefaultNumFrames + prev_num_frames,
@@ -628,13 +632,13 @@
       CreateLocalAudioTrack("audio", ortc_factory1_.get()));
   EXPECT_TRUE(error.ok());
   error = video_sender1->SetTrack(
-      CreateLocalVideoTrackAndFakeCapturer("video", ortc_factory1_.get()));
+      CreateLocalVideoTrackAndFakeSource("video", ortc_factory1_.get()));
   EXPECT_TRUE(error.ok());
   error = audio_sender2->SetTrack(
       CreateLocalAudioTrack("audio", ortc_factory2_.get()));
   EXPECT_TRUE(error.ok());
   error = video_sender2->SetTrack(
-      CreateLocalVideoTrackAndFakeCapturer("video", ortc_factory2_.get()));
+      CreateLocalVideoTrackAndFakeSource("video", ortc_factory2_.get()));
   EXPECT_TRUE(error.ok());
 
   // Use different codecs in different directions for extra challenge.
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index cb167cd..907d199 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -347,6 +347,7 @@
       "test/fakepeerconnectionbase.h",
       "test/fakepeerconnectionforstats.h",
       "test/fakeperiodicvideocapturer.h",
+      "test/fakeperiodicvideosource.h",
       "test/fakertccertificategenerator.h",
       "test/fakesctptransport.h",
       "test/faketransportcontroller.h",
diff --git a/pc/test/fakeperiodicvideosource.h b/pc/test/fakeperiodicvideosource.h
new file mode 100644
index 0000000..98ed4c2
--- /dev/null
+++ b/pc/test/fakeperiodicvideosource.h
@@ -0,0 +1,74 @@
+/*
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef PC_TEST_FAKEPERIODICVIDEOSOURCE_H_
+#define PC_TEST_FAKEPERIODICVIDEOSOURCE_H_
+
+#include "api/videosourceinterface.h"
+#include "media/base/fakeframesource.h"
+#include "media/base/videobroadcaster.h"
+#include "rtc_base/task_queue.h"
+
+namespace webrtc {
+
+class FakePeriodicVideoSource final
+    : public rtc::VideoSourceInterface<VideoFrame> {
+ public:
+  static constexpr int kFrameIntervalMs = 33;
+  static constexpr int kWidth = 640;
+  static constexpr int kHeight = 480;
+
+  FakePeriodicVideoSource() {
+    thread_checker_.DetachFromThread();
+    task_queue_.PostTask(rtc::MakeUnique<FrameTask>(&broadcaster_));
+  }
+
+  void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override {
+    RTC_DCHECK(thread_checker_.CalledOnValidThread());
+    broadcaster_.RemoveSink(sink);
+  }
+
+  void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
+                       const rtc::VideoSinkWants& wants) override {
+    RTC_DCHECK(thread_checker_.CalledOnValidThread());
+    broadcaster_.AddOrUpdateSink(sink, wants);
+  }
+
+ private:
+  class FrameTask : public rtc::QueuedTask {
+   public:
+    explicit FrameTask(rtc::VideoSinkInterface<VideoFrame>* sink)
+        : frame_source_(kWidth,
+                        kHeight,
+                        kFrameIntervalMs * rtc::kNumMicrosecsPerMillisec),
+          sink_(sink) {}
+
+    bool Run() override {
+      sink_->OnFrame(frame_source_.GetFrame());
+      rtc::TaskQueue::Current()->PostDelayedTask(rtc::WrapUnique(this),
+                                                 kFrameIntervalMs);
+      return false;
+    }
+    cricket::FakeFrameSource frame_source_;
+    rtc::VideoSinkInterface<VideoFrame>* sink_;
+  };
+
+  void OnFrame(const webrtc::VideoFrame& frame) { broadcaster_.OnFrame(frame); }
+  rtc::ThreadChecker thread_checker_;
+
+  rtc::VideoBroadcaster broadcaster_;
+
+  // Last member, depend on detruction order.
+  rtc::TaskQueue task_queue_{"FakePeriodicVideoTrackSource"};
+};
+
+}  // namespace webrtc
+
+#endif  // PC_TEST_FAKEPERIODICVIDEOSOURCE_H_