Add method FakePeriodicVideoSource::Stop()

Fixes potential race at test shutdown, introduced in cl
https://webrtc-review.googlesource.com/49220.

Bug: webrtc:6353
Change-Id: Ifaf9e736681b87073a489d75bf1375aa95ee92bb
Reviewed-on: https://webrtc-review.googlesource.com/75124
Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23200}
diff --git a/ortc/ortcfactory_integrationtest.cc b/ortc/ortcfactory_integrationtest.cc
index 52dccdd..6a697cd 100644
--- a/ortc/ortcfactory_integrationtest.cc
+++ b/ortc/ortcfactory_integrationtest.cc
@@ -465,6 +465,7 @@
   // 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_sources_[0]->Stop();
   fake_video_track_sources_[0]->OnSourceDestroyed();
   fake_video_track_sources_[0] = nullptr;
   fake_video_sources_[0].reset();
diff --git a/pc/test/fakeperiodicvideosource.h b/pc/test/fakeperiodicvideosource.h
index 98ed4c2..fd5a3da 100644
--- a/pc/test/fakeperiodicvideosource.h
+++ b/pc/test/fakeperiodicvideosource.h
@@ -11,9 +11,12 @@
 #ifndef PC_TEST_FAKEPERIODICVIDEOSOURCE_H_
 #define PC_TEST_FAKEPERIODICVIDEOSOURCE_H_
 
+#include <memory>
+
 #include "api/videosourceinterface.h"
 #include "media/base/fakeframesource.h"
 #include "media/base/videobroadcaster.h"
+#include "rtc_base/ptr_util.h"
 #include "rtc_base/task_queue.h"
 
 namespace webrtc {
@@ -25,9 +28,11 @@
   static constexpr int kWidth = 640;
   static constexpr int kHeight = 480;
 
-  FakePeriodicVideoSource() {
+  FakePeriodicVideoSource()
+      : task_queue_(
+            rtc::MakeUnique<rtc::TaskQueue>("FakePeriodicVideoTrackSource")) {
     thread_checker_.DetachFromThread();
-    task_queue_.PostTask(rtc::MakeUnique<FrameTask>(&broadcaster_));
+    task_queue_->PostTask(rtc::MakeUnique<FrameTask>(&broadcaster_));
   }
 
   void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override {
@@ -41,6 +46,11 @@
     broadcaster_.AddOrUpdateSink(sink, wants);
   }
 
+  void Stop() {
+    RTC_DCHECK(task_queue_);
+    task_queue_.reset();
+  }
+
  private:
   class FrameTask : public rtc::QueuedTask {
    public:
@@ -65,8 +75,7 @@
 
   rtc::VideoBroadcaster broadcaster_;
 
-  // Last member, depend on detruction order.
-  rtc::TaskQueue task_queue_{"FakePeriodicVideoTrackSource"};
+  std::unique_ptr<rtc::TaskQueue> task_queue_;
 };
 
 }  // namespace webrtc