Embed a cricket::MediaConfig in RTCConfiguration.

This eliminates some instances rtc:Optional and makes the code
simpler. No changes in defaults or other behaviour are intended.

BUG=webrtc:4906

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

Cr-Commit-Position: refs/heads/master@{#12326}
diff --git a/webrtc/api/mediaconstraintsinterface.cc b/webrtc/api/mediaconstraintsinterface.cc
index af25891..a567870 100644
--- a/webrtc/api/mediaconstraintsinterface.cc
+++ b/webrtc/api/mediaconstraintsinterface.cc
@@ -179,29 +179,23 @@
     return;
   }
 
-  bool value;
+  bool enable_ipv6;
   if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6,
-                     &value, nullptr)) {
-    if (!value) {
-      configuration->disable_ipv6 = true;
-    }
+                     &enable_ipv6, nullptr)) {
+    configuration->disable_ipv6 = !enable_ipv6;
   }
-  ConstraintToOptionalBool(constraints, MediaConstraintsInterface::kEnableDscp,
-                           &configuration->enable_dscp);
-  ConstraintToOptionalBool(constraints,
-                           MediaConstraintsInterface::kCpuOveruseDetection,
-                           &configuration->cpu_overuse_detection);
-  if (FindConstraint(constraints,
-                     MediaConstraintsInterface::kEnableRtpDataChannels, &value,
-                     NULL) &&
-      value) {
-    configuration->enable_rtp_data_channel = true;
-  }
+  FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp,
+                 &configuration->media_config.enable_dscp, nullptr);
+  FindConstraint(
+      constraints, MediaConstraintsInterface::kCpuOveruseDetection,
+      &configuration->media_config.video.enable_cpu_overuse_detection, nullptr);
+  FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
+                 &configuration->enable_rtp_data_channel, nullptr);
   // Find Suspend Below Min Bitrate constraint.
-  ConstraintToOptionalBool(
-      constraints,
-      MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
-      &configuration->suspend_below_min_bitrate);
+  FindConstraint(constraints,
+                 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
+                 &configuration->media_config.video.suspend_below_min_bitrate,
+                 nullptr);
   ConstraintToOptionalInt(constraints,
                           MediaConstraintsInterface::kScreencastMinBitrate,
                           &configuration->screencast_min_bitrate);
diff --git a/webrtc/api/mediaconstraintsinterface_unittest.cc b/webrtc/api/mediaconstraintsinterface_unittest.cc
index 07338c1..dcf4bb7 100644
--- a/webrtc/api/mediaconstraintsinterface_unittest.cc
+++ b/webrtc/api/mediaconstraintsinterface_unittest.cc
@@ -17,11 +17,24 @@
 
 namespace {
 
+// Checks all settings touched by CopyConstraintsIntoRtcConfiguration,
+// plus audio_jitter_buffer_max_packets.
 bool Matches(const PeerConnectionInterface::RTCConfiguration& a,
              const PeerConnectionInterface::RTCConfiguration& b) {
-  return a.audio_jitter_buffer_max_packets ==
+  return a.disable_ipv6 == b.disable_ipv6 &&
+         a.audio_jitter_buffer_max_packets ==
              b.audio_jitter_buffer_max_packets &&
-         a.disable_prerenderer_smoothing == b.disable_prerenderer_smoothing;
+         a.enable_rtp_data_channel == b.enable_rtp_data_channel &&
+         a.screencast_min_bitrate == b.screencast_min_bitrate &&
+         a.combined_audio_video_bwe == b.combined_audio_video_bwe &&
+         a.enable_dtls_srtp == b.enable_dtls_srtp &&
+         a.media_config.enable_dscp == b.media_config.enable_dscp &&
+         a.media_config.video.enable_cpu_overuse_detection ==
+             b.media_config.video.enable_cpu_overuse_detection &&
+         a.media_config.video.disable_prerenderer_smoothing ==
+             b.media_config.video.disable_prerenderer_smoothing &&
+         a.media_config.video.suspend_below_min_bitrate ==
+             b.media_config.video.suspend_below_min_bitrate;
 }
 
 TEST(MediaConstraintsInterface, CopyConstraintsIntoRtcConfiguration) {
diff --git a/webrtc/api/peerconnection.cc b/webrtc/api/peerconnection.cc
index b2b8062..70a386e 100644
--- a/webrtc/api/peerconnection.cc
+++ b/webrtc/api/peerconnection.cc
@@ -525,7 +525,6 @@
 }
 
 bool PeerConnection::Initialize(
-    const cricket::MediaConfig& media_config,
     const PeerConnectionInterface::RTCConfiguration& configuration,
     rtc::scoped_ptr<cricket::PortAllocator> allocator,
     rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
@@ -569,7 +568,8 @@
   // No step delay is used while allocating ports.
   port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
 
-  media_controller_.reset(factory_->CreateMediaController(media_config));
+  media_controller_.reset(
+      factory_->CreateMediaController(configuration.media_config));
 
   session_.reset(
       new WebRtcSession(media_controller_.get(), factory_->signaling_thread(),
diff --git a/webrtc/api/peerconnection.h b/webrtc/api/peerconnection.h
index d167673..1574af6 100644
--- a/webrtc/api/peerconnection.h
+++ b/webrtc/api/peerconnection.h
@@ -68,7 +68,6 @@
   explicit PeerConnection(PeerConnectionFactory* factory);
 
   bool Initialize(
-      const cricket::MediaConfig& media_config,
       const PeerConnectionInterface::RTCConfiguration& configuration,
       rtc::scoped_ptr<cricket::PortAllocator> allocator,
       rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
diff --git a/webrtc/api/peerconnectionfactory.cc b/webrtc/api/peerconnectionfactory.cc
index c3c120c..8e1ece6 100644
--- a/webrtc/api/peerconnectionfactory.cc
+++ b/webrtc/api/peerconnectionfactory.cc
@@ -286,24 +286,8 @@
 
   rtc::scoped_refptr<PeerConnection> pc(
       new rtc::RefCountedObject<PeerConnection>(this));
-  // We rely on default values when constraints aren't found.
-  cricket::MediaConfig media_config;
 
-  media_config.video.disable_prerenderer_smoothing =
-      configuration.disable_prerenderer_smoothing;
-  if (configuration.enable_dscp) {
-    media_config.enable_dscp = *(configuration.enable_dscp);
-  }
-  if (configuration.cpu_overuse_detection) {
-    media_config.video.enable_cpu_overuse_detection =
-        *(configuration.cpu_overuse_detection);
-  }
-  if (configuration.suspend_below_min_bitrate) {
-    media_config.video.suspend_below_min_bitrate =
-        *(configuration.suspend_below_min_bitrate);
-  }
-
-  if (!pc->Initialize(media_config, configuration, std::move(allocator),
+  if (!pc->Initialize(configuration, std::move(allocator),
                       std::move(dtls_identity_store), observer)) {
     return nullptr;
   }
diff --git a/webrtc/api/peerconnectioninterface.h b/webrtc/api/peerconnectioninterface.h
index 9259275..59bc160 100644
--- a/webrtc/api/peerconnectioninterface.h
+++ b/webrtc/api/peerconnectioninterface.h
@@ -70,6 +70,7 @@
 #include "webrtc/base/rtccertificate.h"
 #include "webrtc/base/socketaddress.h"
 #include "webrtc/base/sslstreamadapter.h"
+#include "webrtc/media/base/mediachannel.h"
 #include "webrtc/p2p/base/portallocator.h"
 
 namespace rtc {
@@ -222,6 +223,11 @@
   };
 
   // TODO(hbos): Change into class with private data and public getters.
+  // TODO(nisse): In particular, accessing fields directly from an
+  // application is brittle, since the organization mirrors the
+  // organization of the implementation, which isn't stable. So we
+  // need getters and setters at least for fields which applications
+  // are interested in.
   struct RTCConfiguration {
     // This struct is subject to reorganization, both for naming
     // consistency, and to group settings to match where they are used
@@ -229,28 +235,33 @@
     // methods for all settings which are of interest to applications,
     // Chrome in particular.
 
-    bool dscp() { return enable_dscp.value_or(false); }
-    void set_dscp(bool enable) { enable_dscp = rtc::Optional<bool>(enable); }
+    bool dscp() { return media_config.enable_dscp; }
+    void set_dscp(bool enable) { media_config.enable_dscp = enable; }
 
     // TODO(nisse): The corresponding flag in MediaConfig and
     // elsewhere should be renamed enable_cpu_adaptation.
-    bool cpu_adaptation() { return cpu_overuse_detection.value_or(true); }
+    bool cpu_adaptation() {
+      return media_config.video.enable_cpu_overuse_detection;
+    }
     void set_cpu_adaptation(bool enable) {
-      cpu_overuse_detection = rtc::Optional<bool>(enable);
+      media_config.video.enable_cpu_overuse_detection = enable;
     }
 
-    // TODO(nisse): Currently no getter method, since it collides with
-    // the flag itself. Add when the flag is moved to MediaConfig.
+    bool suspend_below_min_bitrate() {
+      return media_config.video.suspend_below_min_bitrate;
+    }
     void set_suspend_below_min_bitrate(bool enable) {
-      suspend_below_min_bitrate = rtc::Optional<bool>(enable);
+      media_config.video.suspend_below_min_bitrate = enable;
     }
 
     // TODO(nisse): The negation in the corresponding MediaConfig
     // attribute is inconsistent, and it should be renamed at some
     // point.
-    bool prerenderer_smoothing() { return !disable_prerenderer_smoothing; }
+    bool prerenderer_smoothing() {
+      return !media_config.video.disable_prerenderer_smoothing;
+    }
     void set_prerenderer_smoothing(bool enable) {
-      disable_prerenderer_smoothing = !enable;
+      media_config.video.disable_prerenderer_smoothing = !enable;
     }
 
     static const int kUndefined = -1;
@@ -271,16 +282,13 @@
     int ice_backup_candidate_pair_ping_interval;  // ms
     ContinualGatheringPolicy continual_gathering_policy;
     std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates;
-    bool disable_prerenderer_smoothing;
     bool prioritize_most_likely_ice_candidate_pairs;
+    struct cricket::MediaConfig media_config;
     // Flags corresponding to values set by constraint flags.
     // rtc::Optional flags can be "missing", in which case the webrtc
     // default applies.
     bool disable_ipv6;
-    rtc::Optional<bool> enable_dscp;
     bool enable_rtp_data_channel;
-    rtc::Optional<bool> cpu_overuse_detection;
-    rtc::Optional<bool> suspend_below_min_bitrate;
     rtc::Optional<int> screencast_min_bitrate;
     rtc::Optional<bool> combined_audio_video_bwe;
     rtc::Optional<bool> enable_dtls_srtp;
@@ -294,7 +302,6 @@
           ice_connection_receiving_timeout(kUndefined),
           ice_backup_candidate_pair_ping_interval(kUndefined),
           continual_gathering_policy(GATHER_ONCE),
-          disable_prerenderer_smoothing(false),
           prioritize_most_likely_ice_candidate_pairs(false),
           disable_ipv6(false),
           enable_rtp_data_channel(false) {}