Switching some interfaces to use std::unique_ptr<>.

This helps show where ownership is transfered between objects.

Specifically, this CL wraps cricket::VideoCapturer, MediaEngineInterface
and DataEngineInterface in unique_ptr.

BUG=None
TBR=magjed@webrtc.org

Review-Url: https://codereview.webrtc.org/2685093002
Cr-Commit-Position: refs/heads/master@{#16548}
diff --git a/webrtc/api/peerconnectionfactoryproxy.h b/webrtc/api/peerconnectionfactoryproxy.h
index e829cca..dd784f5 100644
--- a/webrtc/api/peerconnectionfactoryproxy.h
+++ b/webrtc/api/peerconnectionfactoryproxy.h
@@ -25,6 +25,11 @@
 // are called on is an implementation detail.
 BEGIN_SIGNALING_PROXY_MAP(PeerConnectionFactory)
   PROXY_SIGNALING_THREAD_DESTRUCTOR()
+  // Use the overloads of CreateVideoSource that take raw VideoCapturer
+  // pointers from PeerConnectionFactoryInterface.
+  // TODO(deadbeef): Remove this using statement once those overloads are
+  // removed.
+  using PeerConnectionFactoryInterface::CreateVideoSource;
   PROXY_METHOD1(void, SetOptions, const Options&)
   PROXY_METHOD5(rtc::scoped_refptr<PeerConnectionInterface>,
                 CreatePeerConnection,
@@ -48,11 +53,11 @@
                 const cricket::AudioOptions&)
   PROXY_METHOD2(rtc::scoped_refptr<VideoTrackSourceInterface>,
                 CreateVideoSource,
-                cricket::VideoCapturer*,
+                std::unique_ptr<cricket::VideoCapturer>,
                 const MediaConstraintsInterface*)
   PROXY_METHOD1(rtc::scoped_refptr<VideoTrackSourceInterface>,
                 CreateVideoSource,
-                cricket::VideoCapturer*)
+                std::unique_ptr<cricket::VideoCapturer>)
   PROXY_METHOD2(rtc::scoped_refptr<VideoTrackInterface>,
                 CreateVideoTrack,
                 const std::string&,
diff --git a/webrtc/api/peerconnectioninterface.h b/webrtc/api/peerconnectioninterface.h
index 2a22258..0c9198c 100644
--- a/webrtc/api/peerconnectioninterface.h
+++ b/webrtc/api/peerconnectioninterface.h
@@ -90,6 +90,7 @@
 #include "webrtc/base/socketaddress.h"
 #include "webrtc/base/sslstreamadapter.h"
 #include "webrtc/media/base/mediachannel.h"
+#include "webrtc/media/base/videocapturer.h"
 #include "webrtc/p2p/base/portallocator.h"
 
 namespace rtc {
@@ -966,16 +967,37 @@
 
   // Creates a VideoTrackSourceInterface. The new source takes ownership of
   // |capturer|.
-  // TODO(deadbeef): Switch to std::unique_ptr<>, to make this transfership of
-  // ownership more clear.
+  // TODO(deadbeef): Make pure virtual once downstream mock PC factory classes
+  // are updated.
   virtual rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
-      cricket::VideoCapturer* capturer) = 0;
+      std::unique_ptr<cricket::VideoCapturer> capturer) {
+    return nullptr;
+  }
+
   // A video source creator that allows selection of resolution and frame rate.
   // |constraints| decides video resolution and frame rate but can be NULL.
   // In the NULL case, use the version above.
+  //
+  // |constraints| is only used for the invocation of this method, and can
+  // safely be destroyed afterwards.
+  virtual rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
+      std::unique_ptr<cricket::VideoCapturer> capturer,
+      const MediaConstraintsInterface* constraints) {
+    return nullptr;
+  }
+
+  // Deprecated; please use the versions that take unique_ptrs above.
+  // TODO(deadbeef): Remove these once safe to do so.
+  virtual rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
+      cricket::VideoCapturer* capturer) {
+    return CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(capturer));
+  }
   virtual rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
       cricket::VideoCapturer* capturer,
-      const MediaConstraintsInterface* constraints) = 0;
+      const MediaConstraintsInterface* constraints) {
+    return CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(capturer),
+                             constraints);
+  }
 
   // Creates a new local VideoTrack. The same |source| can be used in several
   // tracks.
diff --git a/webrtc/examples/peerconnection/client/conductor.cc b/webrtc/examples/peerconnection/client/conductor.cc
index 8c288eb..7c41027 100644
--- a/webrtc/examples/peerconnection/client/conductor.cc
+++ b/webrtc/examples/peerconnection/client/conductor.cc
@@ -366,7 +366,7 @@
   }
 }
 
-cricket::VideoCapturer* Conductor::OpenVideoCaptureDevice() {
+std::unique_ptr<cricket::VideoCapturer> Conductor::OpenVideoCaptureDevice() {
   std::vector<std::string> device_names;
   {
     std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(
@@ -386,7 +386,7 @@
   }
 
   cricket::WebRtcVideoDeviceCapturerFactory factory;
-  cricket::VideoCapturer* capturer = nullptr;
+  std::unique_ptr<cricket::VideoCapturer> capturer;
   for (const auto& name : device_names) {
     capturer = factory.Create(cricket::Device(name, 0));
     if (capturer) {
diff --git a/webrtc/examples/peerconnection/client/conductor.h b/webrtc/examples/peerconnection/client/conductor.h
index ba3e271..726534b 100644
--- a/webrtc/examples/peerconnection/client/conductor.h
+++ b/webrtc/examples/peerconnection/client/conductor.h
@@ -14,6 +14,7 @@
 
 #include <deque>
 #include <map>
+#include <memory>
 #include <set>
 #include <string>
 
@@ -58,7 +59,7 @@
   void DeletePeerConnection();
   void EnsureStreamingUI();
   void AddStreams();
-  cricket::VideoCapturer* OpenVideoCaptureDevice();
+  std::unique_ptr<cricket::VideoCapturer> OpenVideoCaptureDevice();
 
   //
   // PeerConnectionObserver implementation.
diff --git a/webrtc/media/base/videocapturerfactory.h b/webrtc/media/base/videocapturerfactory.h
index 012c4a4..b6f886b 100644
--- a/webrtc/media/base/videocapturerfactory.h
+++ b/webrtc/media/base/videocapturerfactory.h
@@ -11,6 +11,8 @@
 #ifndef WEBRTC_MEDIA_BASE_VIDEOCAPTURERFACTORY_H_
 #define WEBRTC_MEDIA_BASE_VIDEOCAPTURERFACTORY_H_
 
+#include <memory>
+
 #include "webrtc/media/base/device.h"
 
 namespace cricket {
@@ -22,7 +24,7 @@
   VideoDeviceCapturerFactory() {}
   virtual ~VideoDeviceCapturerFactory() {}
 
-  virtual VideoCapturer* Create(const Device& device) = 0;
+  virtual std::unique_ptr<VideoCapturer> Create(const Device& device) = 0;
 };
 
 }  // namespace cricket
diff --git a/webrtc/media/engine/webrtcmediaengine.h b/webrtc/media/engine/webrtcmediaengine.h
index 17e79c6..b539b58 100644
--- a/webrtc/media/engine/webrtcmediaengine.h
+++ b/webrtc/media/engine/webrtcmediaengine.h
@@ -40,6 +40,8 @@
       WebRtcVideoEncoderFactory* video_encoder_factory,
       WebRtcVideoDecoderFactory* video_decoder_factory);
 
+  // TODO(deadbeef): Change these to return an std::unique_ptr<>, to indicate
+  // that the caller owns the returned object.
   static MediaEngineInterface* Create(
       webrtc::AudioDeviceModule* adm,
       const rtc::scoped_refptr<webrtc::AudioDecoderFactory>&
diff --git a/webrtc/media/engine/webrtcvideocapturerfactory.cc b/webrtc/media/engine/webrtcvideocapturerfactory.cc
index 3cf3609..6ee361a 100644
--- a/webrtc/media/engine/webrtcvideocapturerfactory.cc
+++ b/webrtc/media/engine/webrtcvideocapturerfactory.cc
@@ -15,16 +15,17 @@
 
 namespace cricket {
 
-VideoCapturer* WebRtcVideoDeviceCapturerFactory::Create(const Device& device) {
+std::unique_ptr<VideoCapturer> WebRtcVideoDeviceCapturerFactory::Create(
+    const Device& device) {
 #ifdef HAVE_WEBRTC_VIDEO
   std::unique_ptr<WebRtcVideoCapturer> capturer(
       new WebRtcVideoCapturer());
   if (!capturer->Init(device)) {
-    return nullptr;
+    return std::unique_ptr<VideoCapturer>();
   }
-  return capturer.release();
+  return std::move(capturer);
 #else
-  return nullptr;
+  return std::unique_ptr<VideoCapturer>();
 #endif
 }
 
diff --git a/webrtc/media/engine/webrtcvideocapturerfactory.h b/webrtc/media/engine/webrtcvideocapturerfactory.h
index 128ef9d..c1f91b7 100644
--- a/webrtc/media/engine/webrtcvideocapturerfactory.h
+++ b/webrtc/media/engine/webrtcvideocapturerfactory.h
@@ -19,7 +19,7 @@
 // Creates instances of cricket::WebRtcVideoCapturer.
 class WebRtcVideoDeviceCapturerFactory : public VideoDeviceCapturerFactory {
  public:
-  virtual VideoCapturer* Create(const Device& device);
+  std::unique_ptr<VideoCapturer> Create(const Device& device) override;
 };
 
 }  // namespace cricket
diff --git a/webrtc/pc/channelmanager.cc b/webrtc/pc/channelmanager.cc
index d0957a7..d8ecc9a 100644
--- a/webrtc/pc/channelmanager.cc
+++ b/webrtc/pc/channelmanager.cc
@@ -28,28 +28,26 @@
 
 using rtc::Bind;
 
-static DataEngineInterface* ConstructDataEngine() {
-  return new RtpDataEngine();
-}
-
-ChannelManager::ChannelManager(MediaEngineInterface* me,
-                               DataEngineInterface* dme,
+ChannelManager::ChannelManager(std::unique_ptr<MediaEngineInterface> me,
+                               std::unique_ptr<DataEngineInterface> dme,
                                rtc::Thread* thread) {
-  Construct(me, dme, thread, thread);
+  Construct(std::move(me), std::move(dme), thread, thread);
 }
 
-ChannelManager::ChannelManager(MediaEngineInterface* me,
+ChannelManager::ChannelManager(std::unique_ptr<MediaEngineInterface> me,
                                rtc::Thread* worker_thread,
                                rtc::Thread* network_thread) {
-  Construct(me, ConstructDataEngine(), worker_thread, network_thread);
+  Construct(std::move(me),
+            std::unique_ptr<DataEngineInterface>(new RtpDataEngine()),
+            worker_thread, network_thread);
 }
 
-void ChannelManager::Construct(MediaEngineInterface* me,
-                               DataEngineInterface* dme,
+void ChannelManager::Construct(std::unique_ptr<MediaEngineInterface> me,
+                               std::unique_ptr<DataEngineInterface> dme,
                                rtc::Thread* worker_thread,
                                rtc::Thread* network_thread) {
-  media_engine_.reset(me);
-  data_media_engine_.reset(dme);
+  media_engine_ = std::move(me);
+  data_media_engine_ = std::move(dme);
   initialized_ = false;
   main_thread_ = rtc::Thread::Current();
   worker_thread_ = worker_thread;
diff --git a/webrtc/pc/channelmanager.h b/webrtc/pc/channelmanager.h
index 879ea4d..052c363 100644
--- a/webrtc/pc/channelmanager.h
+++ b/webrtc/pc/channelmanager.h
@@ -38,13 +38,12 @@
 class ChannelManager {
  public:
   // For testing purposes. Allows the media engine and data media
-  // engine and dev manager to be mocks.  The ChannelManager takes
-  // ownership of these objects.
-  ChannelManager(MediaEngineInterface* me,
-                 DataEngineInterface* dme,
+  // engine and dev manager to be mocks.
+  ChannelManager(std::unique_ptr<MediaEngineInterface> me,
+                 std::unique_ptr<DataEngineInterface> dme,
                  rtc::Thread* worker_and_network);
   // Same as above, but gives an easier default DataEngine.
-  ChannelManager(MediaEngineInterface* me,
+  ChannelManager(std::unique_ptr<MediaEngineInterface> me,
                  rtc::Thread* worker,
                  rtc::Thread* network);
   ~ChannelManager();
@@ -157,8 +156,8 @@
   typedef std::vector<VideoChannel*> VideoChannels;
   typedef std::vector<RtpDataChannel*> RtpDataChannels;
 
-  void Construct(MediaEngineInterface* me,
-                 DataEngineInterface* dme,
+  void Construct(std::unique_ptr<MediaEngineInterface> me,
+                 std::unique_ptr<DataEngineInterface> dme,
                  rtc::Thread* worker_thread,
                  rtc::Thread* network_thread);
   bool InitMediaEngine_w();
diff --git a/webrtc/pc/channelmanager_unittest.cc b/webrtc/pc/channelmanager_unittest.cc
index 74c3590..deba6a1 100644
--- a/webrtc/pc/channelmanager_unittest.cc
+++ b/webrtc/pc/channelmanager_unittest.cc
@@ -8,6 +8,8 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
+#include <memory>
+
 #include "webrtc/base/gunit.h"
 #include "webrtc/base/logging.h"
 #include "webrtc/base/thread.h"
@@ -40,34 +42,28 @@
   ChannelManagerTest()
       : fme_(new cricket::FakeMediaEngine()),
         fdme_(new cricket::FakeDataEngine()),
-        cm_(new cricket::ChannelManager(fme_, fdme_, rtc::Thread::Current())),
+        cm_(new cricket::ChannelManager(
+            std::unique_ptr<MediaEngineInterface>(fme_),
+            std::unique_ptr<DataEngineInterface>(fdme_),
+            rtc::Thread::Current())),
         fake_call_(webrtc::Call::Config(&event_log_)),
-        fake_mc_(cm_, &fake_call_),
+        fake_mc_(cm_.get(), &fake_call_),
         transport_controller_(
-            new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {}
-
-  virtual void SetUp() {
+            new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {
     fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
     fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
   }
 
-  virtual void TearDown() {
-    delete transport_controller_;
-    delete cm_;
-    cm_ = NULL;
-    fdme_ = NULL;
-    fme_ = NULL;
-  }
-
   webrtc::RtcEventLogNullImpl event_log_;
   rtc::Thread network_;
   rtc::Thread worker_;
+  // |fme_| and |fdme_| are actually owned by |cm_|.
   cricket::FakeMediaEngine* fme_;
   cricket::FakeDataEngine* fdme_;
-  cricket::ChannelManager* cm_;
+  std::unique_ptr<cricket::ChannelManager> cm_;
   cricket::FakeCall fake_call_;
   cricket::FakeMediaController fake_mc_;
-  cricket::FakeTransportController* transport_controller_;
+  std::unique_ptr<cricket::FakeTransportController> transport_controller_;
 };
 
 // Test that we startup/shutdown properly.
@@ -133,9 +129,8 @@
   EXPECT_TRUE(cm_->set_worker_thread(&worker_));
   EXPECT_TRUE(cm_->set_network_thread(&network_));
   EXPECT_TRUE(cm_->Init());
-  delete transport_controller_;
-  transport_controller_ =
-      new cricket::FakeTransportController(&network_, ICEROLE_CONTROLLING);
+  transport_controller_.reset(
+      new cricket::FakeTransportController(&network_, ICEROLE_CONTROLLING));
   cricket::DtlsTransportInternal* rtp_transport =
       transport_controller_->CreateDtlsTransport(
           cricket::CN_AUDIO, cricket::ICE_CANDIDATE_COMPONENT_RTP);
diff --git a/webrtc/pc/peerconnection_unittest.cc b/webrtc/pc/peerconnection_unittest.cc
index b7ddc24..df87135 100644
--- a/webrtc/pc/peerconnection_unittest.cc
+++ b/webrtc/pc/peerconnection_unittest.cc
@@ -588,8 +588,9 @@
     fake_capturer->SetRotation(capture_rotation_);
     video_capturers_.push_back(fake_capturer);
     rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
-        peer_connection_factory_->CreateVideoSource(fake_capturer,
-                                                    &source_constraints);
+        peer_connection_factory_->CreateVideoSource(
+            std::unique_ptr<cricket::VideoCapturer>(fake_capturer),
+            &source_constraints);
     std::string label = stream_label + kVideoTrackLabelBase;
 
     rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
diff --git a/webrtc/pc/peerconnectionfactory.cc b/webrtc/pc/peerconnectionfactory.cc
index 4adee15..1de7284 100644
--- a/webrtc/pc/peerconnectionfactory.cc
+++ b/webrtc/pc/peerconnectionfactory.cc
@@ -211,13 +211,13 @@
 
   // TODO:  Need to make sure only one VoE is created inside
   // WebRtcMediaEngine.
-  cricket::MediaEngineInterface* media_engine =
-      worker_thread_->Invoke<cricket::MediaEngineInterface*>(
+  std::unique_ptr<cricket::MediaEngineInterface> media_engine =
+      worker_thread_->Invoke<std::unique_ptr<cricket::MediaEngineInterface>>(
           RTC_FROM_HERE,
           rtc::Bind(&PeerConnectionFactory::CreateMediaEngine_w, this));
 
   channel_manager_.reset(new cricket::ChannelManager(
-      media_engine, worker_thread_, network_thread_));
+      std::move(media_engine), worker_thread_, network_thread_));
 
   channel_manager_->SetVideoRtxEnabled(true);
   channel_manager_->SetCryptoOptions(options_.crypto_options);
@@ -254,21 +254,23 @@
 
 rtc::scoped_refptr<VideoTrackSourceInterface>
 PeerConnectionFactory::CreateVideoSource(
-    cricket::VideoCapturer* capturer,
+    std::unique_ptr<cricket::VideoCapturer> capturer,
     const MediaConstraintsInterface* constraints) {
   RTC_DCHECK(signaling_thread_->IsCurrent());
   rtc::scoped_refptr<VideoTrackSourceInterface> source(
-      VideoCapturerTrackSource::Create(worker_thread_, capturer, constraints,
-                                       false));
+      VideoCapturerTrackSource::Create(worker_thread_, std::move(capturer),
+                                       constraints, false));
   return VideoTrackSourceProxy::Create(signaling_thread_, worker_thread_,
                                        source);
 }
 
 rtc::scoped_refptr<VideoTrackSourceInterface>
-PeerConnectionFactory::CreateVideoSource(cricket::VideoCapturer* capturer) {
+PeerConnectionFactory::CreateVideoSource(
+    std::unique_ptr<cricket::VideoCapturer> capturer) {
   RTC_DCHECK(signaling_thread_->IsCurrent());
   rtc::scoped_refptr<VideoTrackSourceInterface> source(
-      VideoCapturerTrackSource::Create(worker_thread_, capturer, false));
+      VideoCapturerTrackSource::Create(worker_thread_, std::move(capturer),
+                                       false));
   return VideoTrackSourceProxy::Create(signaling_thread_, worker_thread_,
                                        source);
 }
@@ -389,11 +391,14 @@
   return network_thread_;
 }
 
-cricket::MediaEngineInterface* PeerConnectionFactory::CreateMediaEngine_w() {
+std::unique_ptr<cricket::MediaEngineInterface>
+PeerConnectionFactory::CreateMediaEngine_w() {
   RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
-  return cricket::WebRtcMediaEngineFactory::Create(
-      default_adm_.get(), audio_decoder_factory_, video_encoder_factory_.get(),
-      video_decoder_factory_.get(), external_audio_mixer_);
+  return std::unique_ptr<cricket::MediaEngineInterface>(
+      cricket::WebRtcMediaEngineFactory::Create(
+          default_adm_.get(), audio_decoder_factory_,
+          video_encoder_factory_.get(), video_decoder_factory_.get(),
+          external_audio_mixer_));
 }
 
 }  // namespace webrtc
diff --git a/webrtc/pc/peerconnectionfactory.h b/webrtc/pc/peerconnectionfactory.h
index 932821f..53c6745 100644
--- a/webrtc/pc/peerconnectionfactory.h
+++ b/webrtc/pc/peerconnectionfactory.h
@@ -33,6 +33,12 @@
 
 class PeerConnectionFactory : public PeerConnectionFactoryInterface {
  public:
+  // Use the overloads of CreateVideoSource that take raw VideoCapturer
+  // pointers from PeerConnectionFactoryInterface.
+  // TODO(deadbeef): Remove this using statement once those overloads are
+  // removed.
+  using PeerConnectionFactoryInterface::CreateVideoSource;
+
   void SetOptions(const Options& options) override;
 
   // Deprecated, use version without constraints.
@@ -61,13 +67,13 @@
       const MediaConstraintsInterface* constraints) override;
 
   virtual rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
-      cricket::VideoCapturer* capturer) override;
+      std::unique_ptr<cricket::VideoCapturer> capturer) override;
   // This version supports filtering on width, height and frame rate.
   // For the "constraints=null" case, use the version without constraints.
   // TODO(hta): Design a version without MediaConstraintsInterface.
   // https://bugs.chromium.org/p/webrtc/issues/detail?id=5617
   rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource(
-      cricket::VideoCapturer* capturer,
+      std::unique_ptr<cricket::VideoCapturer> capturer,
       const MediaConstraintsInterface* constraints) override;
 
   rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
@@ -118,7 +124,7 @@
   virtual ~PeerConnectionFactory();
 
  private:
-  cricket::MediaEngineInterface* CreateMediaEngine_w();
+  std::unique_ptr<cricket::MediaEngineInterface> CreateMediaEngine_w();
 
   bool owns_ptrs_;
   bool wraps_current_thread_;
diff --git a/webrtc/pc/peerconnectionfactory_unittest.cc b/webrtc/pc/peerconnectionfactory_unittest.cc
index 3fa523e..9a6e543 100644
--- a/webrtc/pc/peerconnectionfactory_unittest.cc
+++ b/webrtc/pc/peerconnectionfactory_unittest.cc
@@ -336,9 +336,11 @@
 // local video track.
 TEST_F(PeerConnectionFactoryTest, LocalRendering) {
   cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer();
-  // The source take ownership of |capturer|.
+  // The source takes ownership of |capturer|, but we keep a raw pointer to
+  // inject fake frames.
   rtc::scoped_refptr<VideoTrackSourceInterface> source(
-      factory_->CreateVideoSource(capturer, NULL));
+      factory_->CreateVideoSource(
+          std::unique_ptr<cricket::VideoCapturer>(capturer), NULL));
   ASSERT_TRUE(source.get() != NULL);
   rtc::scoped_refptr<VideoTrackInterface> track(
       factory_->CreateVideoTrack("testlabel", source));
diff --git a/webrtc/pc/peerconnectioninterface_unittest.cc b/webrtc/pc/peerconnectioninterface_unittest.cc
index 84bfa3a..7f73db8 100644
--- a/webrtc/pc/peerconnectioninterface_unittest.cc
+++ b/webrtc/pc/peerconnectioninterface_unittest.cc
@@ -801,7 +801,9 @@
     rtc::scoped_refptr<MediaStreamInterface> stream(
         pc_factory_->CreateLocalMediaStream(label));
     rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
-        pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
+        pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
+                                           new cricket::FakeVideoCapturer()),
+                                       NULL));
     rtc::scoped_refptr<VideoTrackInterface> video_track(
         pc_factory_->CreateVideoTrack(label + "v0", video_source));
     stream->AddTrack(video_track.get());
@@ -834,8 +836,9 @@
     stream->AddTrack(audio_track.get());
     rtc::scoped_refptr<VideoTrackInterface> video_track(
         pc_factory_->CreateVideoTrack(
-            video_track_label,
-            pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
+            video_track_label, pc_factory_->CreateVideoSource(
+                                   std::unique_ptr<cricket::VideoCapturer>(
+                                       new cricket::FakeVideoCapturer()))));
     stream->AddTrack(video_track.get());
     EXPECT_TRUE(pc_->AddStream(stream));
     EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
@@ -1385,8 +1388,9 @@
       pc_factory_->CreateAudioTrack("audio_track", nullptr));
   rtc::scoped_refptr<VideoTrackInterface> video_track(
       pc_factory_->CreateVideoTrack(
-          "video_track",
-          pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
+          "video_track", pc_factory_->CreateVideoSource(
+                             std::unique_ptr<cricket::VideoCapturer>(
+                                 new cricket::FakeVideoCapturer()))));
   auto audio_sender = pc_->AddTrack(audio_track, stream_list);
   auto video_sender = pc_->AddTrack(video_track, stream_list);
   EXPECT_EQ(1UL, audio_sender->stream_ids().size());
@@ -1456,8 +1460,9 @@
       pc_factory_->CreateAudioTrack("audio_track", nullptr));
   rtc::scoped_refptr<VideoTrackInterface> video_track(
       pc_factory_->CreateVideoTrack(
-          "video_track",
-          pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
+          "video_track", pc_factory_->CreateVideoSource(
+                             std::unique_ptr<cricket::VideoCapturer>(
+                                 new cricket::FakeVideoCapturer()))));
   auto audio_sender =
       pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
   auto video_sender =
@@ -1618,8 +1623,9 @@
   // Add video track to the audio-only stream.
   rtc::scoped_refptr<VideoTrackInterface> video_track(
       pc_factory_->CreateVideoTrack(
-          "video_label",
-          pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
+          "video_label", pc_factory_->CreateVideoSource(
+                             std::unique_ptr<cricket::VideoCapturer>(
+                                 new cricket::FakeVideoCapturer()))));
   stream->AddTrack(video_track.get());
 
   std::unique_ptr<SessionDescriptionInterface> offer;
diff --git a/webrtc/pc/rtcstatscollector_unittest.cc b/webrtc/pc/rtcstatscollector_unittest.cc
index 715da56..adace3f 100644
--- a/webrtc/pc/rtcstatscollector_unittest.cc
+++ b/webrtc/pc/rtcstatscollector_unittest.cc
@@ -277,9 +277,10 @@
         network_thread_(rtc::Thread::Current()),
         signaling_thread_(rtc::Thread::Current()),
         media_engine_(new cricket::FakeMediaEngine()),
-        channel_manager_(new cricket::ChannelManager(media_engine_,
-                                                     worker_thread_,
-                                                     network_thread_)),
+        channel_manager_(new cricket::ChannelManager(
+            std::unique_ptr<cricket::MediaEngineInterface>(media_engine_),
+            worker_thread_,
+            network_thread_)),
         media_controller_(
             MediaControllerInterface::Create(cricket::MediaConfig(),
                                              worker_thread_,
@@ -489,6 +490,7 @@
   rtc::Thread* const worker_thread_;
   rtc::Thread* const network_thread_;
   rtc::Thread* const signaling_thread_;
+  // |media_engine_| is actually owned by |channel_manager_|.
   cricket::FakeMediaEngine* media_engine_;
   std::unique_ptr<cricket::ChannelManager> channel_manager_;
   std::unique_ptr<MediaControllerInterface> media_controller_;
diff --git a/webrtc/pc/rtpsenderreceiver_unittest.cc b/webrtc/pc/rtpsenderreceiver_unittest.cc
index c02da58..99380e9 100644
--- a/webrtc/pc/rtpsenderreceiver_unittest.cc
+++ b/webrtc/pc/rtpsenderreceiver_unittest.cc
@@ -61,9 +61,10 @@
       :  // Create fake media engine/etc. so we can create channels to use to
         // test RtpSenders/RtpReceivers.
         media_engine_(new cricket::FakeMediaEngine()),
-        channel_manager_(media_engine_,
-                         rtc::Thread::Current(),
-                         rtc::Thread::Current()),
+        channel_manager_(
+            std::unique_ptr<cricket::MediaEngineInterface>(media_engine_),
+            rtc::Thread::Current(),
+            rtc::Thread::Current()),
         fake_call_(Call::Config(&event_log_)),
         fake_media_controller_(&channel_manager_, &fake_call_),
         stream_(MediaStream::Create(kStreamLabel1)) {
@@ -251,6 +252,7 @@
 
  protected:
   webrtc::RtcEventLogNullImpl event_log_;
+  // |media_engine_| is actually owned by |channel_manager_|.
   cricket::FakeMediaEngine* media_engine_;
   cricket::FakeTransportController fake_transport_controller_;
   cricket::ChannelManager channel_manager_;
diff --git a/webrtc/pc/statscollector_unittest.cc b/webrtc/pc/statscollector_unittest.cc
index 1bb6d1d..c9affa1 100644
--- a/webrtc/pc/statscollector_unittest.cc
+++ b/webrtc/pc/statscollector_unittest.cc
@@ -504,9 +504,10 @@
       : worker_thread_(rtc::Thread::Current()),
         network_thread_(rtc::Thread::Current()),
         media_engine_(new cricket::FakeMediaEngine()),
-        channel_manager_(new cricket::ChannelManager(media_engine_,
-                                                     worker_thread_,
-                                                     network_thread_)),
+        channel_manager_(new cricket::ChannelManager(
+            std::unique_ptr<cricket::MediaEngineInterface>(media_engine_),
+            worker_thread_,
+            network_thread_)),
         media_controller_(
             webrtc::MediaControllerInterface::Create(cricket::MediaConfig(),
                                                      worker_thread_,
@@ -780,6 +781,7 @@
   webrtc::RtcEventLogNullImpl event_log_;
   rtc::Thread* const worker_thread_;
   rtc::Thread* const network_thread_;
+  // |media_engine_| is actually owned by |channel_manager_|.
   cricket::FakeMediaEngine* media_engine_;
   std::unique_ptr<cricket::ChannelManager> channel_manager_;
   std::unique_ptr<webrtc::MediaControllerInterface> media_controller_;
diff --git a/webrtc/pc/test/peerconnectiontestwrapper.cc b/webrtc/pc/test/peerconnectiontestwrapper.cc
index c433699..e5ead48 100644
--- a/webrtc/pc/test/peerconnectiontestwrapper.cc
+++ b/webrtc/pc/test/peerconnectiontestwrapper.cc
@@ -269,7 +269,9 @@
 
     rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
         peer_connection_factory_->CreateVideoSource(
-            new webrtc::FakePeriodicVideoCapturer(), &constraints);
+            std::unique_ptr<cricket::VideoCapturer>(
+                new webrtc::FakePeriodicVideoCapturer()),
+            &constraints);
     std::string videotrack_label = label + kVideoTrackLabelBase;
     rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
         peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
diff --git a/webrtc/pc/videocapturertracksource.cc b/webrtc/pc/videocapturertracksource.cc
index 771429a..2bb29da 100644
--- a/webrtc/pc/videocapturertracksource.cc
+++ b/webrtc/pc/videocapturertracksource.cc
@@ -258,39 +258,39 @@
 
 rtc::scoped_refptr<VideoTrackSourceInterface> VideoCapturerTrackSource::Create(
     rtc::Thread* worker_thread,
-    cricket::VideoCapturer* capturer,
+    std::unique_ptr<cricket::VideoCapturer> capturer,
     const webrtc::MediaConstraintsInterface* constraints,
     bool remote) {
   RTC_DCHECK(worker_thread != NULL);
   RTC_DCHECK(capturer != NULL);
   rtc::scoped_refptr<VideoCapturerTrackSource> source(
-      new rtc::RefCountedObject<VideoCapturerTrackSource>(worker_thread,
-                                                          capturer, remote));
+      new rtc::RefCountedObject<VideoCapturerTrackSource>(
+          worker_thread, std::move(capturer), remote));
   source->Initialize(constraints);
   return source;
 }
 
 rtc::scoped_refptr<VideoTrackSourceInterface> VideoCapturerTrackSource::Create(
     rtc::Thread* worker_thread,
-    cricket::VideoCapturer* capturer,
+    std::unique_ptr<cricket::VideoCapturer> capturer,
     bool remote) {
   RTC_DCHECK(worker_thread != NULL);
   RTC_DCHECK(capturer != NULL);
   rtc::scoped_refptr<VideoCapturerTrackSource> source(
-      new rtc::RefCountedObject<VideoCapturerTrackSource>(worker_thread,
-                                                          capturer, remote));
+      new rtc::RefCountedObject<VideoCapturerTrackSource>(
+          worker_thread, std::move(capturer), remote));
   source->Initialize(nullptr);
   return source;
 }
 
 VideoCapturerTrackSource::VideoCapturerTrackSource(
     rtc::Thread* worker_thread,
-    cricket::VideoCapturer* capturer,
+    std::unique_ptr<cricket::VideoCapturer> capturer,
     bool remote)
-    : VideoTrackSource(capturer, remote),
+    : VideoTrackSource(capturer.get(), remote),
       signaling_thread_(rtc::Thread::Current()),
       worker_thread_(worker_thread),
-      video_capturer_(capturer),
+      video_capturer_(std::move(capturer)),
       started_(false) {
   video_capturer_->SignalStateChange.connect(
       this, &VideoCapturerTrackSource::OnStateChange);
diff --git a/webrtc/pc/videocapturertracksource.h b/webrtc/pc/videocapturertracksource.h
index 30991a1..af87cc9 100644
--- a/webrtc/pc/videocapturertracksource.h
+++ b/webrtc/pc/videocapturertracksource.h
@@ -34,18 +34,18 @@
                                  public sigslot::has_slots<> {
  public:
   // Creates an instance of VideoCapturerTrackSource.
-  // VideoCapturerTrackSource take ownership of |capturer|.
+  // VideoCapturerTrackSource takes ownership of |capturer|.
   // |constraints| can be NULL and in that case the camera is opened using a
   // default resolution.
   static rtc::scoped_refptr<VideoTrackSourceInterface> Create(
       rtc::Thread* worker_thread,
-      cricket::VideoCapturer* capturer,
+      std::unique_ptr<cricket::VideoCapturer> capturer,
       const webrtc::MediaConstraintsInterface* constraints,
       bool remote);
 
   static rtc::scoped_refptr<VideoTrackSourceInterface> Create(
       rtc::Thread* worker_thread,
-      cricket::VideoCapturer* capturer,
+      std::unique_ptr<cricket::VideoCapturer> capturer,
       bool remote);
 
   bool is_screencast() const override {
@@ -59,7 +59,7 @@
 
  protected:
   VideoCapturerTrackSource(rtc::Thread* worker_thread,
-                           cricket::VideoCapturer* capturer,
+                           std::unique_ptr<cricket::VideoCapturer> capturer,
                            bool remote);
   virtual ~VideoCapturerTrackSource();
   void Initialize(const webrtc::MediaConstraintsInterface* constraints);
diff --git a/webrtc/pc/videocapturertracksource_unittest.cc b/webrtc/pc/videocapturertracksource_unittest.cc
index 144d4b4..3fe726a 100644
--- a/webrtc/pc/videocapturertracksource_unittest.cc
+++ b/webrtc/pc/videocapturertracksource_unittest.cc
@@ -109,9 +109,8 @@
  protected:
   VideoCapturerTrackSourceTest() { InitCapturer(false); }
   void InitCapturer(bool is_screencast) {
-    capturer_cleanup_ = std::unique_ptr<TestVideoCapturer>(
-        new TestVideoCapturer(is_screencast));
-    capturer_ = capturer_cleanup_.get();
+    capturer_ = new TestVideoCapturer(is_screencast);
+    capturer_cleanup_.reset(capturer_);
   }
 
   void InitScreencast() { InitCapturer(true); }
@@ -120,9 +119,8 @@
 
   void CreateVideoCapturerSource(
       const webrtc::MediaConstraintsInterface* constraints) {
-    // VideoSource take ownership of |capturer_|
     source_ = VideoCapturerTrackSource::Create(rtc::Thread::Current(),
-                                               capturer_cleanup_.release(),
+                                               std::move(capturer_cleanup_),
                                                constraints, false);
 
     ASSERT_TRUE(source_.get() != NULL);
@@ -132,7 +130,7 @@
     source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants());
   }
 
-  std::unique_ptr<TestVideoCapturer> capturer_cleanup_;
+  std::unique_ptr<cricket::VideoCapturer> capturer_cleanup_;
   TestVideoCapturer* capturer_;
   cricket::FakeVideoRenderer renderer_;
   std::unique_ptr<StateObserver> state_observer_;
diff --git a/webrtc/pc/webrtcsession_unittest.cc b/webrtc/pc/webrtcsession_unittest.cc
index e9017d4..f785151 100644
--- a/webrtc/pc/webrtcsession_unittest.cc
+++ b/webrtc/pc/webrtcsession_unittest.cc
@@ -369,9 +369,10 @@
   WebRtcSessionTest()
       : media_engine_(new cricket::FakeMediaEngine()),
         data_engine_(new cricket::FakeDataEngine()),
-        channel_manager_(new cricket::ChannelManager(media_engine_,
-                                                     data_engine_,
-                                                     rtc::Thread::Current())),
+        channel_manager_(new cricket::ChannelManager(
+            std::unique_ptr<cricket::MediaEngineInterface>(media_engine_),
+            std::unique_ptr<cricket::DataEngineInterface>(data_engine_),
+            rtc::Thread::Current())),
         fake_call_(webrtc::Call::Config(&event_log_)),
         media_controller_(
             webrtc::MediaControllerInterface::Create(cricket::MediaConfig(),
@@ -1504,6 +1505,8 @@
   }
 
   webrtc::RtcEventLogNullImpl event_log_;
+  // |media_engine_| and |data_engine_| are actually owned by
+  // |channel_manager_|.
   cricket::FakeMediaEngine* media_engine_;
   cricket::FakeDataEngine* data_engine_;
   // Actually owned by session_.
diff --git a/webrtc/sdk/objc/Framework/Classes/RTCAVFoundationVideoSource.mm b/webrtc/sdk/objc/Framework/Classes/RTCAVFoundationVideoSource.mm
index 96c5c36..b004191 100644
--- a/webrtc/sdk/objc/Framework/Classes/RTCAVFoundationVideoSource.mm
+++ b/webrtc/sdk/objc/Framework/Classes/RTCAVFoundationVideoSource.mm
@@ -27,7 +27,8 @@
   _capturer = new webrtc::AVFoundationVideoCapturer();
   rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
       factory.nativeFactory->CreateVideoSource(
-          _capturer, constraints.nativeConstraints.get());
+          std::unique_ptr<cricket::VideoCapturer>(_capturer),
+          constraints.nativeConstraints.get());
 
   return [super initWithNativeVideoSource:source];
 }