Add IceTransportInterface object

This creates the API for an ICE transport object, and lets it
be accessible from a DTLS transport object.

Bug: chromium:907849
Change-Id: Ieb24570217dec75ce0deca8420739c1f116fbba4
Reviewed-on: https://webrtc-review.googlesource.com/c/118703
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26472}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index 6a24aba..52da137 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -80,6 +80,7 @@
     "data_channel_interface.h",
     "dtls_transport_interface.h",
     "dtmf_sender_interface.h",
+    "ice_transport_interface.h",
     "jsep.cc",
     "jsep.h",
     "jsep_ice_candidate.cc",
@@ -347,6 +348,21 @@
   ]
 }
 
+rtc_source_set("ice_transport_factory") {
+  visibility = [ "*" ]
+  sources = [
+    "ice_transport_factory.cc",
+    "ice_transport_factory.h",
+  ]
+  deps = [
+    ":libjingle_peerconnection_api",
+    ":scoped_refptr",
+    "../p2p:rtc_p2p",
+    "../rtc_base:rtc_base",
+    "//third_party/abseil-cpp/absl/memory:memory",
+  ]
+}
+
 rtc_source_set("libjingle_peerconnection_test_api") {
   visibility = [ "*" ]
   testonly = true
diff --git a/api/DEPS b/api/DEPS
index edc9a23..96fd36b 100644
--- a/api/DEPS
+++ b/api/DEPS
@@ -87,6 +87,10 @@
     "+modules/include/module_fec_types.h",
   ],
 
+  "ice_transport_interface\.h": [
+    "+rtc_base/ref_count.h",
+  ],
+
   "jsep\.h": [
     "+rtc_base/ref_count.h",
   ],
diff --git a/api/dtls_transport_interface.h b/api/dtls_transport_interface.h
index 1faf3f5..ff3b107 100644
--- a/api/dtls_transport_interface.h
+++ b/api/dtls_transport_interface.h
@@ -11,7 +11,9 @@
 #ifndef API_DTLS_TRANSPORT_INTERFACE_H_
 #define API_DTLS_TRANSPORT_INTERFACE_H_
 
+#include "api/ice_transport_interface.h"
 #include "api/rtc_error.h"
+#include "api/scoped_refptr.h"
 #include "rtc_base/ref_count.h"
 
 namespace webrtc {
@@ -59,6 +61,8 @@
 // be initiated by other threads.
 class DtlsTransportInterface : public rtc::RefCountInterface {
  public:
+  // Returns a pointer to the ICE transport that is owned by the DTLS transport.
+  virtual rtc::scoped_refptr<IceTransportInterface> ice_transport() = 0;
   // These functions can only be called from the signalling thread.
   virtual DtlsTransportInformation Information() = 0;
   // Observer management.
diff --git a/api/ice_transport_factory.cc b/api/ice_transport_factory.cc
new file mode 100644
index 0000000..b632f09
--- /dev/null
+++ b/api/ice_transport_factory.cc
@@ -0,0 +1,58 @@
+/*
+ *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "api/ice_transport_factory.h"
+
+#include <memory>
+#include <utility>
+
+#include "absl/memory/memory.h"
+#include "p2p/base/ice_transport_internal.h"
+#include "p2p/base/p2p_transport_channel.h"
+#include "p2p/base/port_allocator.h"
+#include "rtc_base/thread.h"
+
+namespace webrtc {
+
+namespace {
+
+// This implementation of IceTransportInterface is used in cases where
+// the only reference to the P2PTransport will be through this class.
+// It must be constructed, accessed and destroyed on the signaling thread.
+class IceTransportWithTransportChannel : public IceTransportInterface {
+ public:
+  IceTransportWithTransportChannel(
+      std::unique_ptr<cricket::IceTransportInternal> internal)
+      : internal_(std::move(internal)) {}
+
+  ~IceTransportWithTransportChannel() override {
+    RTC_DCHECK_RUN_ON(&thread_checker_);
+  }
+
+  cricket::IceTransportInternal* internal() override {
+    RTC_DCHECK_RUN_ON(&thread_checker_);
+    return internal_.get();
+  }
+
+ private:
+  const rtc::ThreadChecker thread_checker_;
+  const std::unique_ptr<cricket::IceTransportInternal> internal_
+      RTC_GUARDED_BY(thread_checker_);
+};
+
+}  // namespace
+
+rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
+    cricket::PortAllocator* port_allocator) {
+  return new rtc::RefCountedObject<IceTransportWithTransportChannel>(
+      absl::make_unique<cricket::P2PTransportChannel>("", 0, port_allocator));
+}
+
+}  // namespace webrtc
diff --git a/api/ice_transport_factory.h b/api/ice_transport_factory.h
new file mode 100644
index 0000000..a8330df
--- /dev/null
+++ b/api/ice_transport_factory.h
@@ -0,0 +1,33 @@
+/*
+ *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef API_ICE_TRANSPORT_FACTORY_H_
+#define API_ICE_TRANSPORT_FACTORY_H_
+
+#include "api/ice_transport_interface.h"
+#include "api/scoped_refptr.h"
+
+namespace cricket {
+class PortAllocator;
+}
+
+namespace webrtc {
+
+// Static factory for an IceTransport object that can be created
+// without using a webrtc::PeerConnection.
+// The returned object must be accessed and destroyed on the thread that
+// created it.
+// The PortAllocator must outlive the created IceTransportInterface object.
+rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
+    cricket::PortAllocator* port_allocator);
+
+}  // namespace webrtc
+
+#endif  // API_ICE_TRANSPORT_FACTORY_H_
diff --git a/api/ice_transport_interface.h b/api/ice_transport_interface.h
new file mode 100644
index 0000000..6e63045
--- /dev/null
+++ b/api/ice_transport_interface.h
@@ -0,0 +1,38 @@
+/*
+ *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef API_ICE_TRANSPORT_INTERFACE_H_
+#define API_ICE_TRANSPORT_INTERFACE_H_
+
+#include "api/rtc_error.h"
+#include "api/scoped_refptr.h"
+#include "rtc_base/ref_count.h"
+
+namespace cricket {
+class IceTransportInternal;
+}  // namespace cricket
+
+namespace webrtc {
+
+// An ICE transport, as represented to the outside world.
+// This object is refcounted, and is therefore alive until the
+// last holder has released it.
+class IceTransportInterface : public rtc::RefCountInterface {
+ public:
+  // Accessor for the internal representation of an ICE transport.
+  // The returned object can only be safely used on the signalling thread.
+  // TODO(crbug.com/907849): Add API calls for the functions that have to
+  // be exposed to clients, and stop allowing access to the
+  // cricket::IceTransportInternal API.
+  virtual cricket::IceTransportInternal* internal() = 0;
+};
+
+}  // namespace webrtc
+#endif  // API_ICE_TRANSPORT_INTERFACE_H_