Move rtc::FunctionView to the public API

Bug: webrtc:10138
Change-Id: Icc25a2a277a9608701aaddd546882366739991ca
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/127898
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27227}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index ec03df3..6b78a6c 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -399,6 +399,16 @@
   ]
 }
 
+rtc_source_set("function_view") {
+  visibility = [ "*" ]
+  sources = [
+    "function_view.h",
+  ]
+  deps = [
+    "../rtc_base:checks",
+  ]
+}
+
 if (rtc_include_tests) {
   if (rtc_enable_protobuf) {
     rtc_source_set("audioproc_f_api") {
@@ -684,6 +694,7 @@
 
     sources = [
       "array_view_unittest.cc",
+      "function_view_unittest.cc",
       "rtc_error_unittest.cc",
       "rtp_parameters_unittest.cc",
       "test/loopback_media_transport_unittest.cc",
@@ -691,6 +702,7 @@
 
     deps = [
       ":array_view",
+      ":function_view",
       ":libjingle_peerconnection_api",
       ":loopback_media_transport",
       "../rtc_base:checks",
diff --git a/api/function_view.h b/api/function_view.h
new file mode 100644
index 0000000..5ae1bd6
--- /dev/null
+++ b/api/function_view.h
@@ -0,0 +1,130 @@
+/*
+ *  Copyright 2016 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_FUNCTION_VIEW_H_
+#define API_FUNCTION_VIEW_H_
+
+#include <type_traits>
+#include <utility>
+
+#include "rtc_base/checks.h"
+
+// Just like std::function, FunctionView will wrap any callable and hide its
+// actual type, exposing only its signature. But unlike std::function,
+// FunctionView doesn't own its callable---it just points to it. Thus, it's a
+// good choice mainly as a function argument when the callable argument will
+// not be called again once the function has returned.
+//
+// Its constructors are implicit, so that callers won't have to convert lambdas
+// and other callables to FunctionView<Blah(Blah, Blah)> explicitly. This is
+// safe because FunctionView is only a reference to the real callable.
+//
+// Example use:
+//
+//   void SomeFunction(rtc::FunctionView<int(int)> index_transform);
+//   ...
+//   SomeFunction([](int i) { return 2 * i + 1; });
+//
+// Note: FunctionView is tiny (essentially just two pointers) and trivially
+// copyable, so it's probably cheaper to pass it by value than by const
+// reference.
+
+namespace rtc {
+
+template <typename T>
+class FunctionView;  // Undefined.
+
+template <typename RetT, typename... ArgT>
+class FunctionView<RetT(ArgT...)> final {
+ public:
+  // Constructor for lambdas and other callables; it accepts every type of
+  // argument except those noted in its enable_if call.
+  template <
+      typename F,
+      typename std::enable_if<
+          // Not for function pointers; we have another constructor for that
+          // below.
+          !std::is_function<typename std::remove_pointer<
+              typename std::remove_reference<F>::type>::type>::value &&
+
+          // Not for nullptr; we have another constructor for that below.
+          !std::is_same<std::nullptr_t,
+                        typename std::remove_cv<F>::type>::value &&
+
+          // Not for FunctionView objects; we have another constructor for that
+          // (the implicitly declared copy constructor).
+          !std::is_same<FunctionView,
+                        typename std::remove_cv<typename std::remove_reference<
+                            F>::type>::type>::value>::type* = nullptr>
+  FunctionView(F&& f)
+      : call_(CallVoidPtr<typename std::remove_reference<F>::type>) {
+    f_.void_ptr = &f;
+  }
+
+  // Constructor that accepts function pointers. If the argument is null, the
+  // result is an empty FunctionView.
+  template <
+      typename F,
+      typename std::enable_if<std::is_function<typename std::remove_pointer<
+          typename std::remove_reference<F>::type>::type>::value>::type* =
+          nullptr>
+  FunctionView(F&& f)
+      : call_(f ? CallFunPtr<typename std::remove_pointer<F>::type> : nullptr) {
+    f_.fun_ptr = reinterpret_cast<void (*)()>(f);
+  }
+
+  // Constructor that accepts nullptr. It creates an empty FunctionView.
+  template <typename F,
+            typename std::enable_if<std::is_same<
+                std::nullptr_t,
+                typename std::remove_cv<F>::type>::value>::type* = nullptr>
+  FunctionView(F&& f) : call_(nullptr) {}
+
+  // Default constructor. Creates an empty FunctionView.
+  FunctionView() : call_(nullptr) {}
+
+  RetT operator()(ArgT... args) const {
+    RTC_DCHECK(call_);
+    return call_(f_, std::forward<ArgT>(args)...);
+  }
+
+  // Returns true if we have a function, false if we don't (i.e., we're null).
+  explicit operator bool() const { return !!call_; }
+
+ private:
+  union VoidUnion {
+    void* void_ptr;
+    void (*fun_ptr)();
+  };
+
+  template <typename F>
+  static RetT CallVoidPtr(VoidUnion vu, ArgT... args) {
+    return (*static_cast<F*>(vu.void_ptr))(std::forward<ArgT>(args)...);
+  }
+  template <typename F>
+  static RetT CallFunPtr(VoidUnion vu, ArgT... args) {
+    return (reinterpret_cast<typename std::add_pointer<F>::type>(vu.fun_ptr))(
+        std::forward<ArgT>(args)...);
+  }
+
+  // A pointer to the callable thing, with type information erased. It's a
+  // union because we have to use separate types depending on if the callable
+  // thing is a function pointer or something else.
+  VoidUnion f_;
+
+  // Pointer to a dispatch function that knows the type of the callable thing
+  // that's stored in f_, and how to call it. A FunctionView object is empty
+  // (null) iff call_ is null.
+  RetT (*call_)(VoidUnion, ArgT...);
+};
+
+}  // namespace rtc
+
+#endif  // API_FUNCTION_VIEW_H_
diff --git a/rtc_base/function_view_unittest.cc b/api/function_view_unittest.cc
similarity index 95%
rename from rtc_base/function_view_unittest.cc
rename to api/function_view_unittest.cc
index d91bac0..3abf0e3 100644
--- a/rtc_base/function_view_unittest.cc
+++ b/api/function_view_unittest.cc
@@ -11,7 +11,7 @@
 #include <memory>
 #include <utility>
 
-#include "rtc_base/function_view.h"
+#include "api/function_view.h"
 #include "test/gtest.h"
 
 namespace rtc {
@@ -97,8 +97,7 @@
 TEST(FunctionViewTest, MoveConstructorIsCopy) {
   auto f17 = [] { return 17; };
   rtc::FunctionView<int()> fv1(f17);
-  // NOLINTNEXTLINE(performance-move-const-arg)
-  rtc::FunctionView<int()> fv2(std::move(fv1));
+  rtc::FunctionView<int()> fv2(std::move(fv1));  // NOLINT
   EXPECT_EQ(17, fv1());
   EXPECT_EQ(17, fv2());
 }
@@ -122,7 +121,7 @@
   rtc::FunctionView<int()> fv2(f23);
   EXPECT_EQ(17, fv1());
   EXPECT_EQ(23, fv2());
-  fv2 = std::move(fv1);  // NOLINT(performance-move-const-arg)
+  fv2 = std::move(fv1);  // NOLINT
   EXPECT_EQ(17, fv1());
   EXPECT_EQ(17, fv2());
 }
diff --git a/audio/BUILD.gn b/audio/BUILD.gn
index ce6525e..4fdb1f6 100644
--- a/audio/BUILD.gn
+++ b/audio/BUILD.gn
@@ -40,6 +40,7 @@
   deps = [
     "../api:array_view",
     "../api:call_api",
+    "../api:function_view",
     "../api:libjingle_peerconnection_api",
     "../api:rtp_headers",
     "../api:scoped_refptr",
diff --git a/audio/audio_send_stream.cc b/audio/audio_send_stream.cc
index 6d16a48..e9b89be 100644
--- a/audio/audio_send_stream.cc
+++ b/audio/audio_send_stream.cc
@@ -20,6 +20,7 @@
 #include "api/audio_codecs/audio_format.h"
 #include "api/call/transport.h"
 #include "api/crypto/frame_encryptor_interface.h"
+#include "api/function_view.h"
 #include "audio/audio_state.h"
 #include "audio/channel_send.h"
 #include "audio/conversion.h"
@@ -33,7 +34,6 @@
 #include "modules/audio_processing/include/audio_processing.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/event.h"
-#include "rtc_base/function_view.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/audio_format_to_string.h"
 #include "rtc_base/task_queue.h"
diff --git a/audio/channel_send.h b/audio/channel_send.h
index f6acd88..761e4b2 100644
--- a/audio/channel_send.h
+++ b/audio/channel_send.h
@@ -18,11 +18,11 @@
 #include "api/audio/audio_frame.h"
 #include "api/audio_codecs/audio_encoder.h"
 #include "api/crypto/crypto_options.h"
+#include "api/function_view.h"
 #include "api/media_transport_interface.h"
 #include "api/task_queue/task_queue_factory.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp.h"
 #include "modules/rtp_rtcp/source/rtp_sender_audio.h"
-#include "rtc_base/function_view.h"
 
 namespace webrtc {
 
diff --git a/logging/BUILD.gn b/logging/BUILD.gn
index 6c072dd..8edfe3c 100644
--- a/logging/BUILD.gn
+++ b/logging/BUILD.gn
@@ -328,6 +328,7 @@
       ":rtc_event_log_impl_encoder",
       ":rtc_event_log_proto",
       ":rtc_stream_config",
+      "../api:function_view",
       "../api:libjingle_peerconnection_api",
       "../api:rtp_headers",
       "../api/units:data_rate",
diff --git a/logging/rtc_event_log/rtc_event_processor.h b/logging/rtc_event_log/rtc_event_processor.h
index dbbdff6..4657f6e 100644
--- a/logging/rtc_event_log/rtc_event_processor.h
+++ b/logging/rtc_event_log/rtc_event_processor.h
@@ -18,8 +18,8 @@
 #include <vector>
 
 #include "absl/memory/memory.h"
+#include "api/function_view.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/function_view.h"
 
 namespace webrtc {
 
diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn
index 8575e71..b19dcc0 100644
--- a/modules/audio_coding/BUILD.gn
+++ b/modules/audio_coding/BUILD.gn
@@ -45,6 +45,7 @@
     "..:module_api",
     "..:module_api_public",
     "../../api:array_view",
+    "../../api:function_view",
     "../../api/audio:audio_frame_api",
     "../../api/audio_codecs:audio_codecs_api",
     "../../common_audio",
diff --git a/modules/audio_coding/include/audio_coding_module.h b/modules/audio_coding/include/audio_coding_module.h
index 0621473..8e0c4d5 100644
--- a/modules/audio_coding/include/audio_coding_module.h
+++ b/modules/audio_coding/include/audio_coding_module.h
@@ -19,9 +19,9 @@
 #include "absl/types/optional.h"
 #include "api/audio_codecs/audio_decoder_factory.h"
 #include "api/audio_codecs/audio_encoder.h"
+#include "api/function_view.h"
 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
 #include "modules/audio_coding/neteq/include/neteq.h"
-#include "rtc_base/function_view.h"
 #include "system_wrappers/include/clock.h"
 
 namespace webrtc {
diff --git a/modules/audio_processing/BUILD.gn b/modules/audio_processing/BUILD.gn
index f99c44d..d02406f 100644
--- a/modules/audio_processing/BUILD.gn
+++ b/modules/audio_processing/BUILD.gn
@@ -158,6 +158,7 @@
     ":noise_suppression_proxy",
     "../..:webrtc_common",
     "../../api:array_view",
+    "../../api:function_view",
     "../../api/audio:aec3_config",
     "../../api/audio:audio_frame_api",
     "../../api/audio:echo_control",
diff --git a/modules/audio_processing/agc2/rnn_vad/BUILD.gn b/modules/audio_processing/agc2/rnn_vad/BUILD.gn
index 3748a71..221c352 100644
--- a/modules/audio_processing/agc2/rnn_vad/BUILD.gn
+++ b/modules/audio_processing/agc2/rnn_vad/BUILD.gn
@@ -35,6 +35,7 @@
   deps = [
     "..:biquad_filter",
     "../../../../api:array_view",
+    "../../../../api:function_view",
     "../../../../common_audio/",
     "../../../../rtc_base:checks",
     "../../../../rtc_base:rtc_base_approved",
diff --git a/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.h b/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.h
index edfd18c..14ff560 100644
--- a/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.h
+++ b/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.h
@@ -16,8 +16,8 @@
 #include <complex>
 
 #include "api/array_view.h"
+#include "api/function_view.h"
 #include "modules/audio_processing/agc2/rnn_vad/common.h"
-#include "rtc_base/function_view.h"
 
 namespace webrtc {
 namespace rnn_vad {
diff --git a/modules/audio_processing/audio_processing_impl.h b/modules/audio_processing/audio_processing_impl.h
index 9b66c26..f164407 100644
--- a/modules/audio_processing/audio_processing_impl.h
+++ b/modules/audio_processing/audio_processing_impl.h
@@ -15,6 +15,7 @@
 #include <memory>
 #include <vector>
 
+#include "api/function_view.h"
 #include "modules/audio_processing/audio_buffer.h"
 #include "modules/audio_processing/include/aec_dump.h"
 #include "modules/audio_processing/include/audio_processing.h"
@@ -22,7 +23,6 @@
 #include "modules/audio_processing/render_queue_item_verifier.h"
 #include "modules/audio_processing/rms_level.h"
 #include "rtc_base/critical_section.h"
-#include "rtc_base/function_view.h"
 #include "rtc_base/gtest_prod_util.h"
 #include "rtc_base/ignore_wundef.h"
 #include "rtc_base/swap_queue.h"
diff --git a/modules/desktop_capture/BUILD.gn b/modules/desktop_capture/BUILD.gn
index 9e7f53c..57f7a34 100644
--- a/modules/desktop_capture/BUILD.gn
+++ b/modules/desktop_capture/BUILD.gn
@@ -47,6 +47,7 @@
 
     sources = []
     deps = [
+      "../../api:function_view",
       "../../api:scoped_refptr",
       "../../rtc_base:checks",
       "//third_party/abseil-cpp/absl/memory",
@@ -428,6 +429,7 @@
 
   deps = [
     ":primitives",
+    "../../api:function_view",
     "../../api:refcountedbase",
     "../../api:scoped_refptr",
     "../../rtc_base",  # TODO(kjellander): Cleanup in bugs.webrtc.org/3806.
diff --git a/modules/desktop_capture/linux/window_list_utils.h b/modules/desktop_capture/linux/window_list_utils.h
index 8c68106..243680d 100644
--- a/modules/desktop_capture/linux/window_list_utils.h
+++ b/modules/desktop_capture/linux/window_list_utils.h
@@ -15,9 +15,9 @@
 #include <X11/Xlib.h>
 #include <stdint.h>
 
+#include "api/function_view.h"
 #include "modules/desktop_capture/desktop_geometry.h"
 #include "modules/desktop_capture/linux/x_atom_cache.h"
-#include "rtc_base/function_view.h"
 
 namespace webrtc {
 
diff --git a/modules/desktop_capture/mac/window_list_utils.h b/modules/desktop_capture/mac/window_list_utils.h
index b54ca3d..ea622c4 100644
--- a/modules/desktop_capture/mac/window_list_utils.h
+++ b/modules/desktop_capture/mac/window_list_utils.h
@@ -13,11 +13,11 @@
 
 #include <ApplicationServices/ApplicationServices.h>
 
+#include "api/function_view.h"
 #include "modules/desktop_capture/desktop_capture_types.h"
 #include "modules/desktop_capture/desktop_capturer.h"
 #include "modules/desktop_capture/desktop_geometry.h"
 #include "modules/desktop_capture/mac/desktop_configuration.h"
-#include "rtc_base/function_view.h"
 
 namespace webrtc {
 
diff --git a/modules/desktop_capture/screen_drawer_unittest.cc b/modules/desktop_capture/screen_drawer_unittest.cc
index 2186ada..0bb8376 100644
--- a/modules/desktop_capture/screen_drawer_unittest.cc
+++ b/modules/desktop_capture/screen_drawer_unittest.cc
@@ -14,8 +14,8 @@
 #include <atomic>
 
 #include "absl/memory/memory.h"
+#include "api/function_view.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/function_view.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/random.h"
diff --git a/modules/rtp_rtcp/BUILD.gn b/modules/rtp_rtcp/BUILD.gn
index ec84ed8..9fe7fdc 100644
--- a/modules/rtp_rtcp/BUILD.gn
+++ b/modules/rtp_rtcp/BUILD.gn
@@ -91,6 +91,7 @@
     "..:module_api_public",
     "../..:webrtc_common",
     "../../api:array_view",
+    "../../api:function_view",
     "../../api:libjingle_peerconnection_api",
     "../../api:rtp_headers",
     "../../api/audio_codecs:audio_codecs_api",
diff --git a/modules/rtp_rtcp/source/rtcp_packet.h b/modules/rtp_rtcp/source/rtcp_packet.h
index 94bf9f0..d41afcb 100644
--- a/modules/rtp_rtcp/source/rtcp_packet.h
+++ b/modules/rtp_rtcp/source/rtcp_packet.h
@@ -15,8 +15,8 @@
 #include <stdint.h>
 
 #include "api/array_view.h"
+#include "api/function_view.h"
 #include "rtc_base/buffer.h"
-#include "rtc_base/function_view.h"
 
 namespace webrtc {
 namespace rtcp {
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index 280e077..2b4d111 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -355,6 +355,7 @@
     ]
     deps = [
       ":pc_test_utils",
+      "../api:function_view",
       "../api:libjingle_peerconnection_api",
       "../api:rtc_stats_api",
       "../api:scoped_refptr",
@@ -490,6 +491,7 @@
       "../api:create_peerconnection_factory",
       "../api:fake_frame_decryptor",
       "../api:fake_frame_encryptor",
+      "../api:function_view",
       "../api:libjingle_logging_api",
       "../api:libjingle_peerconnection_api",
       "../api:loopback_media_transport",
diff --git a/pc/peer_connection_wrapper.cc b/pc/peer_connection_wrapper.cc
index 6bfb59f..74089f6 100644
--- a/pc/peer_connection_wrapper.cc
+++ b/pc/peer_connection_wrapper.cc
@@ -16,11 +16,11 @@
 #include <utility>
 #include <vector>
 
+#include "api/function_view.h"
 #include "api/set_remote_description_observer_interface.h"
 #include "pc/sdp_utils.h"
 #include "pc/test/fake_video_track_source.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/function_view.h"
 #include "rtc_base/gunit.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/ref_counted_object.h"
diff --git a/pc/peer_connection_wrapper.h b/pc/peer_connection_wrapper.h
index d7f10b4..fafee24 100644
--- a/pc/peer_connection_wrapper.h
+++ b/pc/peer_connection_wrapper.h
@@ -16,6 +16,7 @@
 #include <vector>
 
 #include "api/data_channel_interface.h"
+#include "api/function_view.h"
 #include "api/jsep.h"
 #include "api/media_stream_interface.h"
 #include "api/media_types.h"
@@ -26,7 +27,6 @@
 #include "api/scoped_refptr.h"
 #include "api/stats/rtc_stats_report.h"
 #include "pc/test/mock_peer_connection_observers.h"
-#include "rtc_base/function_view.h"
 
 namespace webrtc {
 
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index f4e9f76..5882ade 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -79,6 +79,7 @@
     ":safe_minmax",
     ":type_traits",
     "../api:array_view",
+    "../api:function_view",
     "../api:scoped_refptr",
     "../system_wrappers:field_trial",
     "experiments:field_trial_parser",
@@ -1209,7 +1210,6 @@
       "critical_section_unittest.cc",
       "event_tracer_unittest.cc",
       "event_unittest.cc",
-      "function_view_unittest.cc",
       "logging_unittest.cc",
       "numerics/histogram_percentile_counter_unittest.cc",
       "numerics/mod_ops_unittest.cc",
diff --git a/rtc_base/function_view.h b/rtc_base/function_view.h
index 91ab88e..f465cc8 100644
--- a/rtc_base/function_view.h
+++ b/rtc_base/function_view.h
@@ -11,120 +11,9 @@
 #ifndef RTC_BASE_FUNCTION_VIEW_H_
 #define RTC_BASE_FUNCTION_VIEW_H_
 
-#include <type_traits>
-#include <utility>
+// This header is deprecated and will be removed. Please use the one,
+// that is specified below instead.
 
-#include "rtc_base/checks.h"
-
-// Just like std::function, FunctionView will wrap any callable and hide its
-// actual type, exposing only its signature. But unlike std::function,
-// FunctionView doesn't own its callable---it just points to it. Thus, it's a
-// good choice mainly as a function argument when the callable argument will
-// not be called again once the function has returned.
-//
-// Its constructors are implicit, so that callers won't have to convert lambdas
-// and other callables to FunctionView<Blah(Blah, Blah)> explicitly. This is
-// safe because FunctionView is only a reference to the real callable.
-//
-// Example use:
-//
-//   void SomeFunction(rtc::FunctionView<int(int)> index_transform);
-//   ...
-//   SomeFunction([](int i) { return 2 * i + 1; });
-//
-// Note: FunctionView is tiny (essentially just two pointers) and trivially
-// copyable, so it's probably cheaper to pass it by value than by const
-// reference.
-
-namespace rtc {
-
-template <typename T>
-class FunctionView;  // Undefined.
-
-template <typename RetT, typename... ArgT>
-class FunctionView<RetT(ArgT...)> final {
- public:
-  // Constructor for lambdas and other callables; it accepts every type of
-  // argument except those noted in its enable_if call.
-  template <
-      typename F,
-      typename std::enable_if<
-          // Not for function pointers; we have another constructor for that
-          // below.
-          !std::is_function<typename std::remove_pointer<
-              typename std::remove_reference<F>::type>::type>::value &&
-
-          // Not for nullptr; we have another constructor for that below.
-          !std::is_same<std::nullptr_t,
-                        typename std::remove_cv<F>::type>::value &&
-
-          // Not for FunctionView objects; we have another constructor for that
-          // (the implicitly declared copy constructor).
-          !std::is_same<FunctionView,
-                        typename std::remove_cv<typename std::remove_reference<
-                            F>::type>::type>::value>::type* = nullptr>
-  FunctionView(F&& f)
-      : call_(CallVoidPtr<typename std::remove_reference<F>::type>) {
-    f_.void_ptr = &f;
-  }
-
-  // Constructor that accepts function pointers. If the argument is null, the
-  // result is an empty FunctionView.
-  template <
-      typename F,
-      typename std::enable_if<std::is_function<typename std::remove_pointer<
-          typename std::remove_reference<F>::type>::type>::value>::type* =
-          nullptr>
-  FunctionView(F&& f)
-      : call_(f ? CallFunPtr<typename std::remove_pointer<F>::type> : nullptr) {
-    f_.fun_ptr = reinterpret_cast<void (*)()>(f);
-  }
-
-  // Constructor that accepts nullptr. It creates an empty FunctionView.
-  template <typename F,
-            typename std::enable_if<std::is_same<
-                std::nullptr_t,
-                typename std::remove_cv<F>::type>::value>::type* = nullptr>
-  FunctionView(F&& f) : call_(nullptr) {}
-
-  // Default constructor. Creates an empty FunctionView.
-  FunctionView() : call_(nullptr) {}
-
-  RetT operator()(ArgT... args) const {
-    RTC_DCHECK(call_);
-    return call_(f_, std::forward<ArgT>(args)...);
-  }
-
-  // Returns true if we have a function, false if we don't (i.e., we're null).
-  explicit operator bool() const { return !!call_; }
-
- private:
-  union VoidUnion {
-    void* void_ptr;
-    void (*fun_ptr)();
-  };
-
-  template <typename F>
-  static RetT CallVoidPtr(VoidUnion vu, ArgT... args) {
-    return (*static_cast<F*>(vu.void_ptr))(std::forward<ArgT>(args)...);
-  }
-  template <typename F>
-  static RetT CallFunPtr(VoidUnion vu, ArgT... args) {
-    return (reinterpret_cast<typename std::add_pointer<F>::type>(vu.fun_ptr))(
-        std::forward<ArgT>(args)...);
-  }
-
-  // A pointer to the callable thing, with type information erased. It's a
-  // union because we have to use separate types depending on if the callable
-  // thing is a function pointer or something else.
-  VoidUnion f_;
-
-  // Pointer to a dispatch function that knows the type of the callable thing
-  // that's stored in f_, and how to call it. A FunctionView object is empty
-  // (null) iff call_ is null.
-  RetT (*call_)(VoidUnion, ArgT...);
-};
-
-}  // namespace rtc
+#include "api/function_view.h"
 
 #endif  // RTC_BASE_FUNCTION_VIEW_H_
diff --git a/rtc_tools/BUILD.gn b/rtc_tools/BUILD.gn
index 6269f4c..609598f 100644
--- a/rtc_tools/BUILD.gn
+++ b/rtc_tools/BUILD.gn
@@ -299,6 +299,7 @@
       deps = [
         ":chart_proto",
         "../:webrtc_common",
+        "../api:function_view",
 
         # TODO(kwiberg): Remove this dependency.
         "../api/audio_codecs:audio_codecs_api",
diff --git a/rtc_tools/event_log_visualizer/analyzer.cc b/rtc_tools/event_log_visualizer/analyzer.cc
index 2023336..304b2aa 100644
--- a/rtc_tools/event_log_visualizer/analyzer.cc
+++ b/rtc_tools/event_log_visualizer/analyzer.cc
@@ -19,6 +19,7 @@
 
 #include "absl/memory/memory.h"
 #include "absl/strings/string_view.h"
+#include "api/function_view.h"
 #include "api/transport/field_trial_based_config.h"
 #include "api/transport/goog_cc_factory.h"
 #include "call/audio_receive_stream.h"
@@ -54,7 +55,6 @@
 #include "modules/rtp_rtcp/source/rtp_utility.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/format_macros.h"
-#include "rtc_base/function_view.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/sequence_number_util.h"
 #include "rtc_base/rate_statistics.h"
diff --git a/test/pc/e2e/api/BUILD.gn b/test/pc/e2e/api/BUILD.gn
index 24c1cbf..9f5a835 100644
--- a/test/pc/e2e/api/BUILD.gn
+++ b/test/pc/e2e/api/BUILD.gn
@@ -58,6 +58,7 @@
     ":video_quality_analyzer_api",
     "../../../../api:callfactory_api",
     "../../../../api:fec_controller_api",
+    "../../../../api:function_view",
     "../../../../api:libjingle_peerconnection_api",
     "../../../../api:simulated_network_api",
     "../../../../api/transport:network_control",
diff --git a/test/pc/e2e/api/peerconnection_quality_test_fixture.h b/test/pc/e2e/api/peerconnection_quality_test_fixture.h
index 6afaf09..5862125 100644
--- a/test/pc/e2e/api/peerconnection_quality_test_fixture.h
+++ b/test/pc/e2e/api/peerconnection_quality_test_fixture.h
@@ -18,6 +18,7 @@
 #include "api/async_resolver_factory.h"
 #include "api/call/call_factory_interface.h"
 #include "api/fec_controller.h"
+#include "api/function_view.h"
 #include "api/media_transport_interface.h"
 #include "api/peer_connection_interface.h"
 #include "api/test/simulated_network.h"
@@ -27,7 +28,6 @@
 #include "api/video_codecs/video_encoder.h"
 #include "api/video_codecs/video_encoder_factory.h"
 #include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
-#include "rtc_base/function_view.h"
 #include "rtc_base/network.h"
 #include "rtc_base/rtc_certificate_generator.h"
 #include "rtc_base/ssl_certificate.h"