Remove ViECapture usage in VideoSendStream.

Instead a ViECapturer object is allocated and directly operated on. This
additionally exposes ViESharedData to Call to access the module
ProcessThread, moving towards Call ownership of shared resources.

BUG=1695
R=stefan@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/45339004

Cr-Commit-Position: refs/heads/master@{#9119}
diff --git a/talk/media/webrtc/fakewebrtcvideoengine.h b/talk/media/webrtc/fakewebrtcvideoengine.h
index 34d77b6..e3a2304 100644
--- a/talk/media/webrtc/fakewebrtcvideoengine.h
+++ b/talk/media/webrtc/fakewebrtcvideoengine.h
@@ -718,6 +718,7 @@
   }
   webrtc::ViEChannel* GetChannel(int channel_id) override { return nullptr; }
   webrtc::ViEEncoder* GetEncoder(int channel_id) override { return nullptr; }
+  webrtc::ViESharedData* shared_data() override { return nullptr; }
 
   WEBRTC_FUNC(CreateReceiveChannel, (int& channel, int original_channel)) {
     return CreateChannel(channel, original_channel);
diff --git a/webrtc/video/call.cc b/webrtc/video/call.cc
index 1c9624d..6104c24 100644
--- a/webrtc/video/call.cc
+++ b/webrtc/video/call.cc
@@ -32,6 +32,7 @@
 #include "webrtc/video/audio_receive_stream.h"
 #include "webrtc/video/video_receive_stream.h"
 #include "webrtc/video/video_send_stream.h"
+#include "webrtc/video_engine/vie_shared_data.h"
 #include "webrtc/video_engine/include/vie_base.h"
 #include "webrtc/video_engine/include/vie_codec.h"
 #include "webrtc/video_engine/include/vie_rtp_rtcp.h"
@@ -152,8 +153,8 @@
   VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
 
   VideoEngine* video_engine_;
+  ViESharedData* vie_shared_data_;
   ViERTP_RTCP* rtp_rtcp_;
-  ViECodec* codec_;
   ViERender* render_;
   ViEBase* base_;
   ViENetwork* network_;
@@ -209,9 +210,6 @@
   rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
   DCHECK(rtp_rtcp_ != nullptr);
 
-  codec_ = ViECodec::GetInterface(video_engine_);
-  DCHECK(codec_ != nullptr);
-
   network_ = ViENetwork::GetInterface(video_engine_);
 
   // As a workaround for non-existing calls in the old API, create a base
@@ -222,6 +220,7 @@
   base_->CreateChannel(base_channel_id_);
   DCHECK(base_channel_id_ != -1);
   channel_group_ = base_->GetChannelGroup(base_channel_id_);
+  vie_shared_data_ = base_->shared_data();
 
   network_->SetBitrateConfig(base_channel_id_,
                              config_.bitrate_config.min_bitrate_bps,
@@ -241,7 +240,6 @@
 
   base_->Release();
   network_->Release();
-  codec_->Release();
   render_->Release();
   rtp_rtcp_->Release();
   CHECK(webrtc::VideoEngine::Delete(video_engine_));
@@ -288,10 +286,10 @@
 
   // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
   // the call has already started.
-  VideoSendStream* send_stream =
-      new VideoSendStream(config_.send_transport, overuse_observer_proxy_.get(),
-                          video_engine_, channel_group_, config, encoder_config,
-                          suspended_video_send_ssrcs_, base_channel_id_);
+  VideoSendStream* send_stream = new VideoSendStream(
+      config_.send_transport, overuse_observer_proxy_.get(), video_engine_,
+      channel_group_, vie_shared_data_->module_process_thread(), config,
+      encoder_config, suspended_video_send_ssrcs_, base_channel_id_);
 
   // This needs to be taken before send_crit_ as both locks need to be held
   // while changing network state.
diff --git a/webrtc/video/video_send_stream.cc b/webrtc/video/video_send_stream.cc
index 7dc8816..adcd01c 100644
--- a/webrtc/video/video_send_stream.cc
+++ b/webrtc/video/video_send_stream.cc
@@ -25,6 +25,7 @@
 #include "webrtc/video_engine/include/vie_external_codec.h"
 #include "webrtc/video_engine/include/vie_image_process.h"
 #include "webrtc/video_engine/include/vie_network.h"
+#include "webrtc/video_engine/vie_capturer.h"
 #include "webrtc/video_engine/vie_channel.h"
 #include "webrtc/video_engine/vie_channel_group.h"
 #include "webrtc/video_engine/vie_encoder.h"
@@ -109,6 +110,7 @@
     CpuOveruseObserver* overuse_observer,
     webrtc::VideoEngine* video_engine,
     ChannelGroup* channel_group,
+    ProcessThread* module_process_thread,
     const VideoSendStream::Config& config,
     const VideoEncoderConfig& encoder_config,
     const std::map<uint32_t, RtpState>& suspended_ssrcs,
@@ -118,6 +120,7 @@
       config_(config),
       suspended_ssrcs_(suspended_ssrcs),
       channel_group_(channel_group),
+      module_process_thread_(module_process_thread),
       channel_(-1),
       use_config_bitrate_(true),
       stats_proxy_(Clock::GetRealTimeClock(), config) {
@@ -187,9 +190,8 @@
 
   vie_channel_->SetRTCPCName(rtcp_cname);
 
-  capture_ = ViECapture::GetInterface(video_engine);
-  capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
-  capture_->ConnectCaptureDevice(capture_id_, channel_);
+  vie_capturer_ = ViECapturer::CreateViECapturer(module_process_thread_);
+  CHECK_EQ(0, vie_capturer_->RegisterFrameCallback(channel_, vie_encoder_));
 
   vie_channel_->RegisterSendTransport(&transport_adapter_);
   // 28 to match packet overhead in ModuleRtpRtcpImpl.
@@ -207,11 +209,11 @@
   codec_ = ViECodec::GetInterface(video_engine);
   CHECK(ReconfigureVideoEncoder(encoder_config));
 
-  if (overuse_observer)
-    video_engine_base_->RegisterCpuOveruseObserver(channel_, overuse_observer);
+  if (overuse_observer) {
+    vie_capturer_->RegisterCpuOveruseObserver(overuse_observer);
+  }
   // Registered regardless of monitoring, used for stats.
-  video_engine_base_->RegisterCpuOveruseMetricsObserver(channel_,
-                                                        &stats_proxy_);
+  vie_capturer_->RegisterCpuOveruseMetricsObserver(&stats_proxy_);
 
   video_engine_base_->RegisterSendSideDelayObserver(channel_, &stats_proxy_);
   video_engine_base_->RegisterSendStatisticsProxy(channel_, &stats_proxy_);
@@ -233,7 +235,6 @@
 }
 
 VideoSendStream::~VideoSendStream() {
-  capture_->DeregisterObserver(capture_id_);
   codec_->DeregisterEncoderObserver(channel_);
 
   vie_channel_->RegisterSendFrameCountObserver(nullptr);
@@ -247,8 +248,9 @@
 
   vie_channel_->DeregisterSendTransport();
 
-  capture_->DisconnectCaptureDevice(channel_);
-  capture_->ReleaseCaptureDevice(capture_id_);
+  vie_capturer_->RegisterCpuOveruseObserver(nullptr);
+  vie_capturer_->DeregisterFrameCallback(vie_encoder_);
+  delete vie_capturer_;
 
   vie_encoder_->DeRegisterExternalEncoder(
       config_.encoder_settings.payload_type);
@@ -256,7 +258,6 @@
   video_engine_base_->DeleteChannel(channel_);
 
   video_engine_base_->Release();
-  capture_->Release();
   codec_->Release();
 }
 
@@ -266,7 +267,7 @@
     config_.local_renderer->RenderFrame(frame, 0);
 
   stats_proxy_.OnIncomingFrame();
-  external_capture_->IncomingFrame(frame);
+  vie_capturer_->IncomingFrame(frame);
 }
 
 VideoSendStreamInput* VideoSendStream::Input() { return this; }
diff --git a/webrtc/video/video_send_stream.h b/webrtc/video/video_send_stream.h
index bf34450..d564996 100644
--- a/webrtc/video/video_send_stream.h
+++ b/webrtc/video/video_send_stream.h
@@ -27,14 +27,14 @@
 namespace webrtc {
 
 class CpuOveruseObserver;
-class VideoEngine;
+class ProcessThread;
 class ViEBase;
-class ViECapture;
-class ViECodec;
+class ViECapturer;
 class ViEChannel;
+class ViECodec;
 class ViEEncoder;
-class ViEExternalCapture;
 class ViEExternalCodec;
+class VideoEngine;
 
 namespace internal {
 
@@ -45,6 +45,7 @@
                   CpuOveruseObserver* overuse_observer,
                   webrtc::VideoEngine* video_engine,
                   ChannelGroup* channel_group,
+                  ProcessThread* module_process_thread,
                   const VideoSendStream::Config& config,
                   const VideoEncoderConfig& encoder_config,
                   const std::map<uint32_t, RtpState>& suspended_ssrcs,
@@ -83,16 +84,15 @@
   std::map<uint32_t, RtpState> suspended_ssrcs_;
 
   ChannelGroup* const channel_group_;
+  ProcessThread* const module_process_thread_;
 
   ViEBase* video_engine_base_;
-  ViECapture* capture_;
   ViEChannel* vie_channel_;
   ViECodec* codec_;
   ViEEncoder* vie_encoder_;
-  ViEExternalCapture* external_capture_;
+  ViECapturer* vie_capturer_;
 
   int channel_;
-  int capture_id_;
 
   // Used as a workaround to indicate that we should be using the configured
   // start bitrate initially, instead of the one reported by VideoEngine (which
diff --git a/webrtc/video_engine/include/vie_base.h b/webrtc/video_engine/include/vie_base.h
index 3be2e43..f5bec89 100644
--- a/webrtc/video_engine/include/vie_base.h
+++ b/webrtc/video_engine/include/vie_base.h
@@ -34,7 +34,7 @@
 class ChannelGroup;
 class ViEChannel;
 class ViEEncoder;
-class ViERenderManager;
+class ViESharedData;
 
 // CpuOveruseObserver is called when a system overuse is detected and
 // VideoEngine cannot keep up the encoding frequency.
@@ -269,6 +269,8 @@
       int channel,
       ReceiveStatisticsProxy* receive_statistics_proxy) = 0;
 
+  virtual ViESharedData* shared_data() = 0;
+
  protected:
   ViEBase() {}
   virtual ~ViEBase() {}
diff --git a/webrtc/video_engine/vie_base_impl.h b/webrtc/video_engine/vie_base_impl.h
index fa1c250..82c4a75 100644
--- a/webrtc/video_engine/vie_base_impl.h
+++ b/webrtc/video_engine/vie_base_impl.h
@@ -67,12 +67,12 @@
   virtual int GetVersion(char version[1024]);
   virtual int LastError();
 
+  ViESharedData* shared_data() { return &shared_data_; }
+
  protected:
   explicit ViEBaseImpl(const Config& config);
   virtual ~ViEBaseImpl();
 
-  ViESharedData* shared_data() { return &shared_data_; }
-
  private:
   int CreateChannel(int& video_channel, int original_channel,  // NOLINT
                     bool sender, bool disable_default_encoder);
diff --git a/webrtc/video_engine/vie_capturer.cc b/webrtc/video_engine/vie_capturer.cc
index f148c92..aa38b9f 100644
--- a/webrtc/video_engine/vie_capturer.cc
+++ b/webrtc/video_engine/vie_capturer.cc
@@ -10,6 +10,7 @@
 
 #include "webrtc/video_engine/vie_capturer.h"
 
+#include "webrtc/base/checks.h"
 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
 #include "webrtc/modules/interface/module_common_types.h"
 #include "webrtc/modules/utility/interface/process_thread.h"
@@ -58,8 +59,7 @@
 
 ViECapturer::ViECapturer(int capture_id,
                          int engine_id,
-                         const Config& config,
-                         ProcessThread& module_process_thread)
+                         ProcessThread* module_process_thread)
     : ViEFrameProviderBase(capture_id, engine_id),
       capture_cs_(CriticalSectionWrapper::CreateCriticalSection()),
       effects_and_stats_cs_(CriticalSectionWrapper::CreateCriticalSection()),
@@ -92,11 +92,11 @@
                                    cpu_overuse_metrics_observer_.get())) {
   capture_thread_->Start();
   capture_thread_->SetPriority(kHighPriority);
-  module_process_thread_.RegisterModule(overuse_detector_.get());
+  module_process_thread_->RegisterModule(overuse_detector_.get());
 }
 
 ViECapturer::~ViECapturer() {
-  module_process_thread_.DeRegisterModule(overuse_detector_.get());
+  module_process_thread_->DeRegisterModule(overuse_detector_.get());
 
   // Stop the thread.
   rtc::AtomicOps::Increment(&stop_);
@@ -104,7 +104,7 @@
 
   // Stop the camera input.
   if (capture_module_) {
-    module_process_thread_.DeRegisterModule(capture_module_);
+    module_process_thread_->DeRegisterModule(capture_module_);
     capture_module_->DeRegisterCaptureDataCallback();
     capture_module_->Release();
     capture_module_ = NULL;
@@ -124,14 +124,22 @@
   delete brightness_frame_stats_;
 }
 
+ViECapturer* ViECapturer::CreateViECapturer(
+    ProcessThread* module_process_thread) {
+  ViECapturer* capturer = new ViECapturer(0, 0, module_process_thread);
+  // Init with nullptr, 0 will set capture as an external capturer.
+  CHECK_EQ(0, capturer->Init(nullptr, 0));
+  return capturer;
+}
+
 ViECapturer* ViECapturer::CreateViECapture(
     int capture_id,
     int engine_id,
     const Config& config,
     VideoCaptureModule* capture_module,
     ProcessThread& module_process_thread) {
-  ViECapturer* capture = new ViECapturer(capture_id, engine_id, config,
-                                         module_process_thread);
+  ViECapturer* capture = new ViECapturer(capture_id, engine_id,
+                                         &module_process_thread);
   if (!capture || capture->Init(capture_module) != 0) {
     delete capture;
     capture = NULL;
@@ -144,7 +152,7 @@
   capture_module_ = capture_module;
   capture_module_->RegisterCaptureDataCallback(*this);
   capture_module_->AddRef();
-  module_process_thread_.RegisterModule(capture_module_);
+  module_process_thread_->RegisterModule(capture_module_);
   return 0;
 }
 
@@ -155,8 +163,8 @@
     const char* device_unique_idUTF8,
     const uint32_t device_unique_idUTF8Length,
     ProcessThread& module_process_thread) {
-  ViECapturer* capture = new ViECapturer(capture_id, engine_id, config,
-                                         module_process_thread);
+  ViECapturer* capture = new ViECapturer(capture_id, engine_id,
+                                         &module_process_thread);
   if (!capture ||
       capture->Init(device_unique_idUTF8, device_unique_idUTF8Length) != 0) {
     delete capture;
@@ -180,7 +188,7 @@
   }
   capture_module_->AddRef();
   capture_module_->RegisterCaptureDataCallback(*this);
-  module_process_thread_.RegisterModule(capture_module_);
+  module_process_thread_->RegisterModule(capture_module_);
 
   return 0;
 }
diff --git a/webrtc/video_engine/vie_capturer.h b/webrtc/video_engine/vie_capturer.h
index 1ce6572..055406d 100644
--- a/webrtc/video_engine/vie_capturer.h
+++ b/webrtc/video_engine/vie_capturer.h
@@ -49,6 +49,12 @@
       protected VideoCaptureDataCallback,
       protected VideoCaptureFeedBack {
  public:
+  ViECapturer(int capture_id,
+              int engine_id,
+              ProcessThread* module_process_thread);
+
+  static ViECapturer* CreateViECapturer(ProcessThread* module_process_thread);
+
   static ViECapturer* CreateViECapture(int capture_id,
                                        int engine_id,
                                        const Config& config,
@@ -102,11 +108,6 @@
   void GetCpuOveruseMetrics(CpuOveruseMetrics* metrics) const;
 
  protected:
-  ViECapturer(int capture_id,
-              int engine_id,
-              const Config& config,
-              ProcessThread& module_process_thread);
-
   int32_t Init(VideoCaptureModule* capture_module);
   int32_t Init(const char* device_unique_idUTF8,
                uint32_t device_unique_idUTF8Length);
@@ -145,7 +146,7 @@
   rtc::scoped_ptr<CriticalSectionWrapper> effects_and_stats_cs_;
   VideoCaptureModule* capture_module_;
   bool use_external_capture_;
-  ProcessThread& module_process_thread_;
+  ProcessThread* const module_process_thread_;
   const int capture_id_;
 
   // Frame used in IncomingFrameI420.
diff --git a/webrtc/video_engine/vie_shared_data.h b/webrtc/video_engine/vie_shared_data.h
index 43b41d0..d7c365e 100644
--- a/webrtc/video_engine/vie_shared_data.h
+++ b/webrtc/video_engine/vie_shared_data.h
@@ -41,6 +41,9 @@
   ViEChannelManager* channel_manager() { return channel_manager_.get(); }
   ViEInputManager* input_manager() { return input_manager_.get(); }
   ViERenderManager* render_manager() { return render_manager_.get(); }
+  ProcessThread* module_process_thread() {
+    return module_process_thread_.get();
+  }
 
   std::map<int, CpuOveruseObserver*>* overuse_observers() {
     return &overuse_observers_; }