Move ownership of ICE from DtlsTransport to JsepTransport.

It does not make sense for DtlsTransport to own ICE, and this arrangement will
not work when negotiating datagram or DTLS transport.  During negotiation, both
a DTLS transport and a datagram transport need to be ready to receive from the
same ICE transport, depending on which protocol is chosen by the answerer.  Once
the answerer chooses a protocol, the transport that is not chosen must be
deleted, but ICE must be left intact for use by the remaining transport.

Bug: webrtc:9719
Change-Id: Ibab969b574c981e3834ced71f8ff88008cb26a6c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/139340
Reviewed-by: Anton Sukhanov <sukhanov@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Commit-Queue: Bjorn Mellem <mellem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28113}
diff --git a/pc/jsep_transport_controller.cc b/pc/jsep_transport_controller.cc
index 55f1d1c..4f14e00 100644
--- a/pc/jsep_transport_controller.cc
+++ b/pc/jsep_transport_controller.cc
@@ -474,7 +474,7 @@
 
 std::unique_ptr<cricket::DtlsTransportInternal>
 JsepTransportController::CreateDtlsTransport(
-    std::unique_ptr<cricket::IceTransportInternal> ice,
+    cricket::IceTransportInternal* ice,
     std::unique_ptr<DatagramTransportInterface> datagram_transport) {
   RTC_DCHECK(network_thread_->IsCurrent());
 
@@ -485,7 +485,7 @@
 
     // Create DTLS wrapper around DatagramTransportInterface.
     dtls = absl::make_unique<cricket::DatagramDtlsAdaptor>(
-        std::move(ice), std::move(datagram_transport), config_.crypto_options,
+        ice, std::move(datagram_transport), config_.crypto_options,
         config_.event_log);
   } else if (config_.media_transport_factory &&
              config_.use_media_transport_for_media &&
@@ -494,13 +494,13 @@
     // then we don't need to create DTLS.
     // Otherwise, DTLS is still created.
     dtls = absl::make_unique<cricket::NoOpDtlsTransport>(
-        std::move(ice), config_.crypto_options);
+        ice, config_.crypto_options);
   } else if (config_.external_transport_factory) {
     dtls = config_.external_transport_factory->CreateDtlsTransport(
-        std::move(ice), config_.crypto_options);
+        ice, config_.crypto_options);
   } else {
     dtls = absl::make_unique<cricket::DtlsTransport>(
-        std::move(ice), config_.crypto_options, config_.event_log);
+        ice, config_.crypto_options, config_.event_log);
   }
 
   RTC_DCHECK(dtls);
@@ -1170,21 +1170,22 @@
   }
 
   std::unique_ptr<cricket::DtlsTransportInternal> rtp_dtls_transport =
-      CreateDtlsTransport(std::move(ice), std::move(datagram_transport));
+      CreateDtlsTransport(ice.get(), std::move(datagram_transport));
 
   std::unique_ptr<cricket::DtlsTransportInternal> rtcp_dtls_transport;
   std::unique_ptr<RtpTransport> unencrypted_rtp_transport;
   std::unique_ptr<SrtpTransport> sdes_transport;
   std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport;
 
+  std::unique_ptr<cricket::IceTransportInternal> rtcp_ice;
   if (config_.rtcp_mux_policy !=
           PeerConnectionInterface::kRtcpMuxPolicyRequire &&
       content_info.type == cricket::MediaProtocolType::kRtp) {
     RTC_DCHECK(media_transport == nullptr);
     RTC_DCHECK(datagram_transport == nullptr);
-    rtcp_dtls_transport = CreateDtlsTransport(
-        CreateIceTransport(content_info.name, /*rtcp=*/true),
-        /*datagram_transport=*/nullptr);
+    rtcp_ice = CreateIceTransport(content_info.name, /*rtcp=*/true);
+    rtcp_dtls_transport = CreateDtlsTransport(rtcp_ice.get(),
+                                              /*datagram_transport=*/nullptr);
   }
 
   if (datagram_transport) {
@@ -1217,10 +1218,10 @@
 
   std::unique_ptr<cricket::JsepTransport> jsep_transport =
       absl::make_unique<cricket::JsepTransport>(
-          content_info.name, certificate_, std::move(unencrypted_rtp_transport),
-          std::move(sdes_transport), std::move(dtls_srtp_transport),
-          std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport),
-          std::move(media_transport));
+          content_info.name, certificate_, std::move(ice), std::move(rtcp_ice),
+          std::move(unencrypted_rtp_transport), std::move(sdes_transport),
+          std::move(dtls_srtp_transport), std::move(rtp_dtls_transport),
+          std::move(rtcp_dtls_transport), std::move(media_transport));
 
   jsep_transport->SignalRtcpMuxActive.connect(
       this, &JsepTransportController::UpdateAggregateStates_n);