Reland of Split IncomingVideoStream into two implementations, with smoothing and without. (patchset #1 id:1 of https://codereview.webrtc.org/2084873002/ )

Reason for revert:
Reverting the revert.  This change is not related to the failure on the Windows FYI bots.  The cause of the failure has been reverted in Chromium:
https://codereview.chromium.org/2081653004/

Original issue's description:
> Revert of Split IncomingVideoStream into two implementations, with smoothing and without. (patchset #5 id:340001 of https://codereview.webrtc.org/2078873002/ )
>
> Reason for revert:
> Breaks chromium.webrtc.fyi
>
> https://uberchromegw.corp.google.com/i/chromium.webrtc.fyi/builders/Win7%20Tester/builds/4719
> https://uberchromegw.corp.google.com/i/chromium.webrtc.fyi/builders/Win10%20Tester/builds/3120
>
> Original issue's description:
> > Reland of IncomingVideoStream refactoring.
> > This reland does not contain the non-smoothing part of the original implementation.  Instead, when smoothing is turned off, frame callbacks run on the decoder thread, as they did before.  This code path is used in Chrome.  As far as Chrome goes, the difference now is that there won't be an instance of IncomingVideoStream in between the decoder and the callback (i.e. fewer locks).  Other than that, no change for Chrome.
> >
> > Original issue's description (with non-smoothing references removed):
> >
> > Split IncomingVideoStream into two implementations, with smoothing and without.
> >
> > * Added TODOs and documentation for VideoReceiveStream::OnFrame, where we today grab 6 locks.
> >
> > * Removed the Start/Stop methods from the IncomingVideoStream implementations.  Now, when an instance is created, it should be considered to be "running" and when it is deleted, it's "not running".  This saves on resources and also reduces the amount of locking required and I could remove one critical section altogether.
> >
> > * Changed the VideoStreamDecoder class to not depend on IncomingVideoStream but rather use the generic rtc::VideoSinkInterface<VideoFrame> interface.  This means that any implementation of that interface can be used and the decoder can be made to  just use the 'renderer' from the config.  Once we do that, we can decouple the IncomingVideoStream implementations from the decoder and VideoReceiveStream implementations and leave it up to the application for how to do smoothing.  The app can choose to use the Incoming* classes or roll its own (which may be preferable since applications often have their own scheduling mechanisms).
> >
> > * The lifetime of the VideoStreamDecoder instance is now bound to Start/Stop in VideoReceiveStream and not all of the lifetime of VideoReceiveStream.
> >
> > * Fixed VideoStreamDecoder to unregister callbacks in the dtor that were registered in the ctor. (this was open to a use-after-free regression)
> >
> > * Delay and callback pointers are now passed via the ctors to the IncomingVideoStream classes.  The thread primitives in the IncomingVideoStream classes are also constructed/destructed at the same time as the owning object, which allowed me to remove one more lock.
> >
> > * Removed code in the VideoStreamDecoder that could overwrite the VideoReceiveStream render delay with a fixed value of 10ms on construction.  This wasn't a problem with the previous implementation (it would be now though) but seemed to me like the wrong place to be setting that value.
> >
> > * Made the render delay value in VideoRenderFrames, const.
> >
> > BUG=chromium:620232
> > R=mflodman@webrtc.org, nisse@webrtc.org
> >
> > Committed: https://crrev.com/884c336c345d988974c2a69cea402b0fb8b07a63
> > Cr-Commit-Position: refs/heads/master@{#13219}
>
> TBR=nisse@webrtc.org,philipel@webrtc.org,mflodman@webrtc.org,tommi@webrtc.org
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=chromium:620232
>
> Committed: https://crrev.com/a536bfe70de38fe877245317a7f0b00bcf69cbd0
> Cr-Commit-Position: refs/heads/master@{#13229}

TBR=nisse@webrtc.org,philipel@webrtc.org,mflodman@webrtc.org,sakal@webrtc.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=chromium:620232

Review-Url: https://codereview.webrtc.org/2089613002
Cr-Commit-Position: refs/heads/master@{#13230}
diff --git a/webrtc/common_video/incoming_video_stream.cc b/webrtc/common_video/incoming_video_stream.cc
index a5e7ba7..8deca0f 100644
--- a/webrtc/common_video/incoming_video_stream.cc
+++ b/webrtc/common_video/incoming_video_stream.cc
@@ -10,9 +10,6 @@
 
 #include "webrtc/common_video/include/incoming_video_stream.h"
 
-#include <assert.h>
-
-#include "webrtc/base/platform_thread.h"
 #include "webrtc/base/timeutils.h"
 #include "webrtc/common_video/video_render_frames.h"
 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
@@ -20,122 +17,65 @@
 
 namespace webrtc {
 
-IncomingVideoStream::IncomingVideoStream(bool disable_prerenderer_smoothing)
-    : disable_prerenderer_smoothing_(disable_prerenderer_smoothing),
-      incoming_render_thread_(),
+IncomingVideoStream::IncomingVideoStream(
+    int32_t delay_ms,
+    rtc::VideoSinkInterface<VideoFrame>* callback)
+    : incoming_render_thread_(&IncomingVideoStreamThreadFun,
+                              this,
+                              "IncomingVideoStreamThread"),
       deliver_buffer_event_(EventTimerWrapper::Create()),
-      running_(false),
-      external_callback_(nullptr),
-      render_buffers_(new VideoRenderFrames()) {}
+      external_callback_(callback),
+      render_buffers_(new VideoRenderFrames(delay_ms)) {
+  RTC_DCHECK(external_callback_);
+
+  render_thread_checker_.DetachFromThread();
+  decoder_thread_checker_.DetachFromThread();
+
+  incoming_render_thread_.Start();
+  incoming_render_thread_.SetPriority(rtc::kRealtimePriority);
+  deliver_buffer_event_->StartTimer(false, kEventStartupTimeMs);
+}
 
 IncomingVideoStream::~IncomingVideoStream() {
-  Stop();
+  RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
+
+  {
+    rtc::CritScope cs(&buffer_critsect_);
+    render_buffers_.reset();
+  }
+
+  deliver_buffer_event_->Set();
+  incoming_render_thread_.Stop();
+  deliver_buffer_event_->StopTimer();
 }
 
 void IncomingVideoStream::OnFrame(const VideoFrame& video_frame) {
-  rtc::CritScope csS(&stream_critsect_);
-
-  if (!running_) {
-    return;
-  }
+  RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
 
   // Hand over or insert frame.
-  if (disable_prerenderer_smoothing_) {
-    DeliverFrame(video_frame);
-  } else {
-    rtc::CritScope csB(&buffer_critsect_);
-    if (render_buffers_->AddFrame(video_frame) == 1) {
-      deliver_buffer_event_->Set();
-    }
+  rtc::CritScope csB(&buffer_critsect_);
+  if (render_buffers_->AddFrame(video_frame) == 1) {
+    deliver_buffer_event_->Set();
   }
 }
 
-int32_t IncomingVideoStream::SetExpectedRenderDelay(
-    int32_t delay_ms) {
-  rtc::CritScope csS(&stream_critsect_);
-  if (running_) {
-    return -1;
-  }
-  rtc::CritScope cs(&buffer_critsect_);
-  return render_buffers_->SetRenderDelay(delay_ms);
-}
-
-void IncomingVideoStream::SetExternalCallback(
-    rtc::VideoSinkInterface<VideoFrame>* external_callback) {
-  rtc::CritScope cs(&thread_critsect_);
-  external_callback_ = external_callback;
-}
-
-int32_t IncomingVideoStream::Start() {
-  rtc::CritScope csS(&stream_critsect_);
-  if (running_) {
-    return 0;
-  }
-
-  if (!disable_prerenderer_smoothing_) {
-    rtc::CritScope csT(&thread_critsect_);
-    assert(incoming_render_thread_ == NULL);
-
-    incoming_render_thread_.reset(new rtc::PlatformThread(
-        IncomingVideoStreamThreadFun, this, "IncomingVideoStreamThread"));
-    if (!incoming_render_thread_) {
-      return -1;
-    }
-
-    incoming_render_thread_->Start();
-    incoming_render_thread_->SetPriority(rtc::kRealtimePriority);
-    deliver_buffer_event_->StartTimer(false, kEventStartupTimeMs);
-  }
-
-  running_ = true;
-  return 0;
-}
-
-int32_t IncomingVideoStream::Stop() {
-  rtc::CritScope cs_stream(&stream_critsect_);
-
-  if (!running_) {
-    return 0;
-  }
-
-  rtc::PlatformThread* thread = NULL;
-  {
-    rtc::CritScope cs_thread(&thread_critsect_);
-    if (incoming_render_thread_) {
-      // Setting the incoming render thread to NULL marks that we're performing
-      // a shutdown and will make IncomingVideoStreamProcess abort after wakeup.
-      thread = incoming_render_thread_.release();
-      deliver_buffer_event_->StopTimer();
-      // Set the event to allow the thread to wake up and shut down without
-      // waiting for a timeout.
-      deliver_buffer_event_->Set();
-    }
-  }
-  if (thread) {
-    thread->Stop();
-    delete thread;
-  }
-  running_ = false;
-  return 0;
-}
-
 bool IncomingVideoStream::IncomingVideoStreamThreadFun(void* obj) {
   return static_cast<IncomingVideoStream*>(obj)->IncomingVideoStreamProcess();
 }
 
 bool IncomingVideoStream::IncomingVideoStreamProcess() {
-  if (kEventError != deliver_buffer_event_->Wait(kEventMaxWaitTimeMs)) {
-    rtc::CritScope cs(&thread_critsect_);
-    if (incoming_render_thread_ == NULL) {
-      // Terminating
-      return false;
-    }
+  RTC_DCHECK_RUN_ON(&render_thread_checker_);
 
+  if (kEventError != deliver_buffer_event_->Wait(kEventMaxWaitTimeMs)) {
     // Get a new frame to render and the time for the frame after this one.
     rtc::Optional<VideoFrame> frame_to_render;
     uint32_t wait_time;
     {
       rtc::CritScope cs(&buffer_critsect_);
+      if (!render_buffers_.get()) {
+        // Terminating
+        return false;
+      }
       frame_to_render = render_buffers_->FrameToRender();
       wait_time = render_buffers_->TimeToNextFrameRelease();
     }
@@ -144,21 +84,14 @@
     if (wait_time > kEventMaxWaitTimeMs) {
       wait_time = kEventMaxWaitTimeMs;
     }
+
     deliver_buffer_event_->StartTimer(false, wait_time);
 
-    if (frame_to_render)
-      DeliverFrame(*frame_to_render);
+    if (frame_to_render) {
+      external_callback_->OnFrame(*frame_to_render);
+    }
   }
   return true;
 }
 
-void IncomingVideoStream::DeliverFrame(const VideoFrame& video_frame) {
-  rtc::CritScope cs(&thread_critsect_);
-
-  // Send frame for rendering.
-  if (external_callback_) {
-    external_callback_->OnFrame(video_frame);
-  }
-}
-
 }  // namespace webrtc