Piping AutoMuter interface through to ViE API

This is a piece of the AutoMuter effort. A second CL will follow containing modifications to the new API, and tests.

BUG=2436
R=mflodman@webrtc.org

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@4899 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/video_engine/include/vie_codec.h b/video_engine/include/vie_codec.h
index 47bc686..017bedf 100644
--- a/video_engine/include/vie_codec.h
+++ b/video_engine/include/vie_codec.h
@@ -35,6 +35,12 @@
   virtual void OutgoingRate(const int video_channel,
                             const unsigned int framerate,
                             const unsigned int bitrate) = 0;
+
+  // This method is called whenever the state of the AutoMuter changes, i.e.,
+  // when |is_muted| toggles.
+  // TODO(hlundin): Remove the default implementation when possible.
+  virtual void VideoAutoMuted(bool is_muted) {};
+
  protected:
   virtual ~ViEEncoderObserver() {}
 };
@@ -177,6 +183,13 @@
   // Disables recording of debugging information.
   virtual int StopDebugRecording(int video_channel) = 0;
 
+  // Enables AutoMuter to turn off video when the rate drops below
+  // |threshold_bps|, and turns back on when the rate goes back up above
+  // |threshold_bps| + |window_bps|.
+  // This is under development; not tested.
+  virtual void EnableAutoMuting(int video_channel, int threshold_bps,
+                                int window_bps) = 0;
+
  protected:
   ViECodec() {}
   virtual ~ViECodec() {}
diff --git a/video_engine/test/auto_test/source/vie_autotest_codec.cc b/video_engine/test/auto_test/source/vie_autotest_codec.cc
index df950c6..d242831 100644
--- a/video_engine/test/auto_test/source/vie_autotest_codec.cc
+++ b/video_engine/test/auto_test/source/vie_autotest_codec.cc
@@ -44,6 +44,7 @@
   unsigned int last_outgoing_bitrate_;
   unsigned int last_incoming_framerate_;
   unsigned int last_incoming_bitrate_;
+  unsigned int video_auto_muted_called_;
 
   webrtc::VideoCodec incoming_codec_;
 
@@ -57,7 +58,8 @@
         last_outgoing_framerate_(0),
         last_outgoing_bitrate_(0),
         last_incoming_framerate_(0),
-        last_incoming_bitrate_(0) {
+        last_incoming_bitrate_(0),
+        video_auto_muted_called_(0) {
     memset(&incoming_codec_, 0, sizeof(incoming_codec_));
   }
   virtual void IncomingCodecChanged(const int video_channel,
@@ -86,6 +88,10 @@
     last_outgoing_bitrate_ += bitrate;
   }
 
+  virtual void VideoAutoMuted(bool is_muted) {
+    video_auto_muted_called_++;
+  }
+
   virtual void RequestNewKeyFrame(const int video_channel) {
   }
 };
diff --git a/video_engine/test/auto_test/source/vie_autotest_custom_call.cc b/video_engine/test/auto_test/source/vie_autotest_custom_call.cc
index cbccf6f..5d2ecfc 100644
--- a/video_engine/test/auto_test/source/vie_autotest_custom_call.cc
+++ b/video_engine/test/auto_test/source/vie_autotest_custom_call.cc
@@ -72,6 +72,10 @@
     std::cout << "Send FR: " << framerate
               << " BR: " << bitrate << std::endl;
   }
+
+  virtual void VideoAutoMuted(bool is_muted) {
+    std::cout << "VideoAutoMuted: " << is_muted << std::endl;
+  }
 };
 
 class ViEAutotestDecoderObserver : public webrtc::ViEDecoderObserver {
diff --git a/video_engine/vie_codec_impl.cc b/video_engine/vie_codec_impl.cc
index 61449db..bf845d9 100644
--- a/video_engine/vie_codec_impl.cc
+++ b/video_engine/vie_codec_impl.cc
@@ -715,6 +715,19 @@
   return vie_encoder->StopDebugRecording();
 }
 
+void ViECodecImpl::EnableAutoMuting(int video_channel, int threshold_bps,
+                                    int window_bps) {
+  ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
+  ViEEncoder* vie_encoder = cs.Encoder(video_channel);
+  if (!vie_encoder) {
+    WEBRTC_TRACE(kTraceError, kTraceVideo,
+                 ViEId(shared_data_->instance_id(), video_channel),
+                 "%s: No encoder %d", __FUNCTION__, video_channel);
+    return;
+  }
+  return vie_encoder->EnableAutoMuting(threshold_bps, window_bps);
+}
+
 bool ViECodecImpl::CodecValid(const VideoCodec& video_codec) {
   // Check pl_name matches codec_type.
   if (video_codec.codecType == kVideoCodecRED) {
diff --git a/video_engine/vie_codec_impl.h b/video_engine/vie_codec_impl.h
index aa5cc71..5a998c8 100644
--- a/video_engine/vie_codec_impl.h
+++ b/video_engine/vie_codec_impl.h
@@ -70,6 +70,8 @@
   virtual int StartDebugRecording(int video_channel,
                                   const char* file_name_utf8);
   virtual int StopDebugRecording(int video_channel);
+  virtual void EnableAutoMuting(int video_channel, int threshold_bps,
+                                int window_bps);
 
  protected:
   explicit ViECodecImpl(ViESharedData* shared_data);
diff --git a/video_engine/vie_encoder.cc b/video_engine/vie_encoder.cc
index e0d9a43..99154cd 100644
--- a/video_engine/vie_encoder.cc
+++ b/video_engine/vie_encoder.cc
@@ -158,7 +158,8 @@
     picture_id_sli_(0),
     has_received_rpsi_(false),
     picture_id_rpsi_(0),
-    qm_callback_(NULL) {
+    qm_callback_(NULL),
+    video_auto_muted_(false) {
   WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo,
                ViEId(engine_id, channel_id),
                "%s(engine_id: %d) 0x%p - Constructor", __FUNCTION__, engine_id,
@@ -1034,6 +1035,7 @@
                __FUNCTION__, bitrate_bps, fraction_lost, round_trip_time_ms);
 
   vcm_.SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
+  bool video_is_muted = vcm_.VideoMuted();
   int bitrate_kbps = bitrate_bps / 1000;
   VideoCodec send_codec;
   if (vcm_.SendCodec(&send_codec) != 0) {
@@ -1069,6 +1071,17 @@
                                max_padding_bitrate_kbps,
                                pad_up_to_bitrate_kbps);
   default_rtp_rtcp_->SetTargetSendBitrate(stream_bitrates);
+  if (video_is_muted != video_auto_muted_) {
+    // State changed now. Send callback to inform about that.
+    video_auto_muted_ = video_is_muted;
+    if (codec_observer_) {
+      WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo,
+                   ViEId(engine_id_, channel_id_),
+                   "%s: video_auto_muted_ changed to %i",
+                   __FUNCTION__, video_auto_muted_);
+      codec_observer_->VideoAutoMuted(video_auto_muted_);
+    }
+  }
 }
 
 PacedSender* ViEEncoder::GetPacedSender() {
@@ -1110,6 +1123,10 @@
   return vcm_.StopDebugRecording();
 }
 
+void ViEEncoder::EnableAutoMuting(int threshold_bps, int window_bps) {
+  vcm_.EnableAutoMuting(threshold_bps, window_bps);
+}
+
 QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm)
     : vpm_(vpm) {
 }
diff --git a/video_engine/vie_encoder.h b/video_engine/vie_encoder.h
index cbadcd7..8d18bb6 100644
--- a/video_engine/vie_encoder.h
+++ b/video_engine/vie_encoder.h
@@ -162,6 +162,11 @@
   // Disables recording of debugging information.
   virtual int StopDebugRecording();
 
+  // Enables AutoMuter to turn off video when the rate drops below
+  // |threshold_bps|, and turns back on when the rate goes back up above
+  // |threshold_bps| + |window_bps|.
+  virtual void EnableAutoMuting(int threshold_bps, int window_bps);
+
   int channel_id() const { return channel_id_; }
  protected:
   // Called by BitrateObserver.
@@ -216,6 +221,7 @@
 
   // Quality modes callback
   QMVideoSettingsCallback* qm_callback_;
+  bool video_auto_muted_;
 };
 
 }  // namespace webrtc