Set up start bitrate in WebRtcVideoEngine2.

R=stefan@webrtc.org
BUG=1788

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk/talk@7476 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/media/webrtc/webrtcvideoengine2.cc b/media/webrtc/webrtcvideoengine2.cc
index 96df0f7..2d9879c 100644
--- a/media/webrtc/webrtcvideoengine2.cc
+++ b/media/webrtc/webrtcvideoengine2.cc
@@ -419,10 +419,11 @@
   assert(initialized_);
   LOG(LS_INFO) << "CreateChannel: "
                << (voice_channel != NULL ? "With" : "Without")
-               << " voice channel.";
+               << " voice channel. Options: " << options.ToString();
   WebRtcVideoChannel2* channel =
       new WebRtcVideoChannel2(call_factory_,
                               voice_channel,
+                              options,
                               external_encoder_factory_,
                               external_decoder_factory_,
                               GetVideoEncoderFactory());
@@ -430,7 +431,6 @@
     delete channel;
     return NULL;
   }
-  channel->SetOptions(options);
   channel->SetRecvCodecs(video_codecs_);
   return channel;
 }
@@ -745,6 +745,7 @@
 WebRtcVideoChannel2::WebRtcVideoChannel2(
     WebRtcCallFactory* call_factory,
     VoiceMediaChannel* voice_channel,
+    const VideoOptions& options,
     WebRtcVideoEncoderFactory* external_encoder_factory,
     WebRtcVideoDecoderFactory* external_decoder_factory,
     WebRtcVideoEncoderFactory2* encoder_factory)
@@ -753,15 +754,21 @@
       external_decoder_factory_(external_decoder_factory),
       encoder_factory_(encoder_factory) {
   // TODO(pbos): Connect the video and audio with |voice_channel|.
+  SetDefaultOptions();
+  options_.SetAll(options);
   webrtc::Call::Config config(this);
   config.overuse_callback = this;
+
+  // Set start bitrate for the call. A default is provided by SetDefaultOptions.
+  int start_bitrate_kbps;
+  options_.video_start_bitrate.Get(&start_bitrate_kbps);
+  config.stream_start_bitrate_bps = start_bitrate_kbps * 1000;
+
   call_.reset(call_factory->CreateCall(config));
 
   rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
   sending_ = false;
   default_send_ssrc_ = 0;
-
-  SetDefaultOptions();
 }
 
 void WebRtcVideoChannel2::SetDefaultOptions() {
@@ -769,6 +776,8 @@
   options_.suspend_below_min_bitrate.Set(false);
   options_.use_payload_padding.Set(false);
   options_.video_noise_reduction.Set(true);
+  options_.video_start_bitrate.Set(
+      webrtc::Call::Config::kDefaultStartBitrateBps / 1000);
 }
 
 WebRtcVideoChannel2::~WebRtcVideoChannel2() {
diff --git a/media/webrtc/webrtcvideoengine2.h b/media/webrtc/webrtcvideoengine2.h
index 8831a1b..2b2e107 100644
--- a/media/webrtc/webrtcvideoengine2.h
+++ b/media/webrtc/webrtcvideoengine2.h
@@ -212,6 +212,7 @@
  public:
   WebRtcVideoChannel2(WebRtcCallFactory* call_factory,
                       VoiceMediaChannel* voice_channel,
+                      const VideoOptions& options,
                       WebRtcVideoEncoderFactory* external_encoder_factory,
                       WebRtcVideoDecoderFactory* external_decoder_factory,
                       WebRtcVideoEncoderFactory2* encoder_factory);
diff --git a/media/webrtc/webrtcvideoengine2_unittest.cc b/media/webrtc/webrtcvideoengine2_unittest.cc
index 85b7b9e..814aa93 100644
--- a/media/webrtc/webrtcvideoengine2_unittest.cc
+++ b/media/webrtc/webrtcvideoengine2_unittest.cc
@@ -330,6 +330,9 @@
   VideoMediaChannel* SetUpForExternalEncoderFactory(
       cricket::WebRtcVideoEncoderFactory* encoder_factory,
       const std::vector<VideoCodec>& codecs);
+
+  void TestStartBitrate(bool override_start_bitrate, int start_bitrate_bps);
+
   WebRtcVideoEngine2 engine_;
   VideoCodec default_codec_;
   VideoCodec default_red_codec_;
@@ -425,6 +428,58 @@
   FAIL() << "Absolute Sender Time extension not in header-extension list.";
 }
 
+void WebRtcVideoEngine2Test::TestStartBitrate(bool override_start_bitrate,
+                                              int start_bitrate_bps) {
+  class FakeCallFactory : public WebRtcCallFactory {
+   public:
+    FakeCallFactory() : fake_call_(NULL) {}
+
+    FakeCall* GetCall() {
+      return fake_call_;
+    }
+
+   private:
+    virtual webrtc::Call* CreateCall(
+        const webrtc::Call::Config& config) OVERRIDE {
+      assert(fake_call_ == NULL);
+      fake_call_ = new FakeCall(config);
+      return fake_call_;
+    }
+
+    FakeCall* fake_call_;
+  };
+
+  FakeCallFactory call_factory;
+  engine_.SetCallFactory(&call_factory);
+
+  engine_.Init(rtc::Thread::Current());
+
+  cricket::VideoOptions options;
+  if (override_start_bitrate) {
+    options.video_start_bitrate.Set(start_bitrate_bps / 1000);
+  }
+
+  rtc::scoped_ptr<VideoMediaChannel> channel(
+      engine_.CreateChannel(options, NULL));
+
+  EXPECT_EQ(override_start_bitrate
+                ? start_bitrate_bps
+                : webrtc::Call::Config::kDefaultStartBitrateBps,
+            call_factory.GetCall()->GetConfig().stream_start_bitrate_bps);
+}
+
+TEST_F(WebRtcVideoEngine2Test, UsesCorrectDefaultStartBitrate) {
+  TestStartBitrate(false, -1);
+}
+
+TEST_F(WebRtcVideoEngine2Test, CreateChannelCanUseIncreasedStartBitrate) {
+  TestStartBitrate(true, 2 * webrtc::Call::Config::kDefaultStartBitrateBps);
+}
+
+TEST_F(WebRtcVideoEngine2Test, CreateChannelCanUseDecreasedStartBitrate) {
+  TestStartBitrate(true, webrtc::Call::Config::kDefaultStartBitrateBps / 2);
+}
+
 TEST_F(WebRtcVideoEngine2Test, SetSendFailsBeforeSettingCodecs) {
   engine_.Init(rtc::Thread::Current());
   rtc::scoped_ptr<VideoMediaChannel> channel(