Make LoadObserver settable per video send stream. Gives client flexibility and makes the implementation slightly simpler. See discussion in: https://codereview.webrtc.org/1269863005/

BUG=webrtc:4690

Review URL: https://codereview.webrtc.org/1325263002

Cr-Commit-Position: refs/heads/master@{#9891}
diff --git a/talk/media/webrtc/webrtcvideoengine2.cc b/talk/media/webrtc/webrtcvideoengine2.cc
index bc303cd..923fee6 100644
--- a/talk/media/webrtc/webrtcvideoengine2.cc
+++ b/talk/media/webrtc/webrtcvideoengine2.cc
@@ -803,7 +803,6 @@
   options_.SetAll(options);
   options_.cpu_overuse_detection.Get(&signal_cpu_adaptation_);
   webrtc::Call::Config config;
-  config.overuse_callback = this;
   if (voice_engine != NULL) {
     config.voice_engine = voice_engine->voe()->engine();
   }
@@ -1131,10 +1130,13 @@
   for (uint32 used_ssrc : sp.ssrcs)
     send_ssrcs_.insert(used_ssrc);
 
+  webrtc::VideoSendStream::Config config(this);
+  config.overuse_callback = this;
+
   WebRtcVideoSendStream* stream =
       new WebRtcVideoSendStream(call_.get(),
                                 sp,
-                                webrtc::VideoSendStream::Config(this),
+                                config,
                                 external_encoder_factory_,
                                 options_,
                                 bitrate_config_.max_bitrate_bps,
diff --git a/talk/media/webrtc/webrtcvideoengine2_unittest.cc b/talk/media/webrtc/webrtcvideoengine2_unittest.cc
index 257c548..e488ad7 100644
--- a/talk/media/webrtc/webrtcvideoengine2_unittest.cc
+++ b/talk/media/webrtc/webrtcvideoengine2_unittest.cc
@@ -1746,15 +1746,14 @@
   EXPECT_TRUE(channel_->SetSend(true));
 
   // Trigger overuse.
+  ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
+  FakeVideoSendStream* send_stream = fake_call_->GetVideoSendStreams().front();
   webrtc::LoadObserver* overuse_callback =
-      fake_call_->GetConfig().overuse_callback;
+      send_stream->GetConfig().overuse_callback;
   ASSERT_TRUE(overuse_callback != NULL);
   overuse_callback->OnLoadUpdate(webrtc::LoadObserver::kOveruse);
 
   EXPECT_TRUE(capturer.CaptureFrame());
-  ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
-  FakeVideoSendStream* send_stream = fake_call_->GetVideoSendStreams().front();
-
   EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
 
   if (enable_overuse && !is_screenshare) {
@@ -2343,9 +2342,12 @@
   cricket::VideoOptions options;
   options.cpu_overuse_detection.Set(true);
   EXPECT_TRUE(channel_->SetOptions(options));
+
   // Trigger overuse.
+  ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
   webrtc::LoadObserver* overuse_callback =
-      fake_call_->GetConfig().overuse_callback;
+      fake_call_->GetVideoSendStreams().front()->GetConfig().overuse_callback;
+  ASSERT_TRUE(overuse_callback != NULL);
   overuse_callback->OnLoadUpdate(webrtc::LoadObserver::kOveruse);
 
   // Capture format VGA -> adapt (OnCpuResolutionRequest downgrade) -> VGA/2.
diff --git a/webrtc/call.h b/webrtc/call.h
index 97226a0..160a918 100644
--- a/webrtc/call.h
+++ b/webrtc/call.h
@@ -74,10 +74,6 @@
     // VoiceEngine used for audio/video synchronization for this Call.
     VoiceEngine* voice_engine = nullptr;
 
-    // Callback for overuse and normal usage based on the jitter of incoming
-    // captured frames. 'nullptr' disables the callback.
-    LoadObserver* overuse_callback = nullptr;
-
     // Bitrate config used until valid bitrate estimates are calculated. Also
     // used to cap total bitrate used.
     struct BitrateConfig {
diff --git a/webrtc/video/call.cc b/webrtc/video/call.cc
index 928828f..6c58fc1 100644
--- a/webrtc/video/call.cc
+++ b/webrtc/video/call.cc
@@ -41,30 +41,6 @@
 
 namespace internal {
 
-class CpuOveruseObserverProxy : public webrtc::CpuOveruseObserver {
- public:
-  explicit CpuOveruseObserverProxy(LoadObserver* overuse_callback)
-      : overuse_callback_(overuse_callback) {
-    DCHECK(overuse_callback != nullptr);
-  }
-
-  virtual ~CpuOveruseObserverProxy() {}
-
-  void OveruseDetected() override {
-    rtc::CritScope lock(&crit_);
-    overuse_callback_->OnLoadUpdate(LoadObserver::kOveruse);
-  }
-
-  void NormalUsage() override {
-    rtc::CritScope lock(&crit_);
-    overuse_callback_->OnLoadUpdate(LoadObserver::kUnderuse);
-  }
-
- private:
-  rtc::CriticalSection crit_;
-  LoadObserver* overuse_callback_ GUARDED_BY(crit_);
-};
-
 class Call : public webrtc::Call, public PacketReceiver {
  public:
   explicit Call(const Call::Config& config);
@@ -138,8 +114,6 @@
   std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
   std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
 
-  rtc::scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
-
   VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
 
   DISALLOW_COPY_AND_ASSIGN(Call);
@@ -172,11 +146,6 @@
   Trace::CreateTrace();
   module_process_thread_->Start();
 
-  if (config.overuse_callback) {
-    overuse_observer_proxy_.reset(
-        new CpuOveruseObserverProxy(config.overuse_callback));
-  }
-
   SetBitrateControllerConfig(config_.bitrate_config);
 }
 
@@ -248,8 +217,7 @@
 
   // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
   // the call has already started.
-  VideoSendStream* send_stream = new VideoSendStream(
-      overuse_observer_proxy_.get(), num_cpu_cores_,
+  VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
       module_process_thread_.get(), channel_group_.get(),
       rtc::AtomicOps::Increment(&next_channel_id_), config, encoder_config,
       suspended_video_send_ssrcs_);
diff --git a/webrtc/video/call_perf_tests.cc b/webrtc/video/call_perf_tests.cc
index 481abb6..592b68e 100644
--- a/webrtc/video/call_perf_tests.cc
+++ b/webrtc/video/call_perf_tests.cc
@@ -488,15 +488,10 @@
         observation_complete_->Set();
     }
 
-    Call::Config GetSenderCallConfig() override {
-      Call::Config config;
-      config.overuse_callback = this;
-      return config;
-    }
-
     void ModifyConfigs(VideoSendStream::Config* send_config,
                        std::vector<VideoReceiveStream::Config>* receive_configs,
                        VideoEncoderConfig* encoder_config) override {
+      send_config->overuse_callback = this;
       send_config->encoder_settings.encoder = &encoder_;
     }
 
diff --git a/webrtc/video/video_send_stream.cc b/webrtc/video/video_send_stream.cc
index 659fecb..42ad774 100644
--- a/webrtc/video/video_send_stream.cc
+++ b/webrtc/video/video_send_stream.cc
@@ -101,7 +101,6 @@
 
 namespace internal {
 VideoSendStream::VideoSendStream(
-    CpuOveruseObserver* overuse_observer,
     int num_cpu_cores,
     ProcessThread* module_process_thread,
     ChannelGroup* channel_group,
@@ -162,7 +161,7 @@
 
   input_.reset(new internal::VideoCaptureInput(
       module_process_thread_, vie_encoder_, config_.local_renderer,
-      &stats_proxy_, overuse_observer));
+      &stats_proxy_, this));
 
   // 28 to match packet overhead in ModuleRtpRtcpImpl.
   DCHECK_LE(config_.rtp.max_packet_size, static_cast<size_t>(0xFFFF - 28));
@@ -390,6 +389,16 @@
   return stats_proxy_.GetStats();
 }
 
+void VideoSendStream::OveruseDetected() {
+  if (config_.overuse_callback)
+    config_.overuse_callback->OnLoadUpdate(LoadObserver::kOveruse);
+}
+
+void VideoSendStream::NormalUsage() {
+  if (config_.overuse_callback)
+    config_.overuse_callback->OnLoadUpdate(LoadObserver::kUnderuse);
+}
+
 void VideoSendStream::ConfigureSsrcs() {
   vie_channel_->SetSSRC(config_.rtp.ssrcs.front(), kViEStreamTypeNormal, 0);
   for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
diff --git a/webrtc/video/video_send_stream.h b/webrtc/video/video_send_stream.h
index 7114d4f..5859314 100644
--- a/webrtc/video/video_send_stream.h
+++ b/webrtc/video/video_send_stream.h
@@ -28,17 +28,16 @@
 namespace webrtc {
 
 class ChannelGroup;
-class CpuOveruseObserver;
 class ProcessThread;
 class ViEChannel;
 class ViEEncoder;
 
 namespace internal {
 
-class VideoSendStream : public webrtc::VideoSendStream {
+class VideoSendStream : public webrtc::VideoSendStream,
+                        public webrtc::CpuOveruseObserver {
  public:
-  VideoSendStream(CpuOveruseObserver* overuse_observer,
-                  int num_cpu_cores,
+  VideoSendStream(int num_cpu_cores,
                   ProcessThread* module_process_thread,
                   ChannelGroup* channel_group,
                   int channel_id,
@@ -59,6 +58,10 @@
   bool ReconfigureVideoEncoder(const VideoEncoderConfig& config) override;
   Stats GetStats() override;
 
+  // webrtc::CpuOveruseObserver implementation.
+  void OveruseDetected() override;
+  void NormalUsage() override;
+
   typedef std::map<uint32_t, RtpState> RtpStateMap;
   RtpStateMap GetRtpStates() const;
 
diff --git a/webrtc/video_send_stream.h b/webrtc/video_send_stream.h
index 18606de..1c37390 100644
--- a/webrtc/video_send_stream.h
+++ b/webrtc/video_send_stream.h
@@ -23,6 +23,7 @@
 
 namespace webrtc {
 
+class LoadObserver;
 class VideoEncoder;
 
 // Class to deliver captured frame to the video send stream.
@@ -122,6 +123,10 @@
     // Transport for outgoing packets.
     newapi::Transport* send_transport = nullptr;
 
+    // Callback for overuse and normal usage based on the jitter of incoming
+    // captured frames. 'nullptr' disables the callback.
+    LoadObserver* overuse_callback = nullptr;
+
     // Called for each I420 frame before encoding the frame. Can be used for
     // effects, snapshots etc. 'nullptr' disables the callback.
     I420FrameCallback* pre_encode_callback = nullptr;