Move to_queued_task.h and pending_task_safety_flag.h into public API

Bug: b/235812579
Change-Id: I9fa3dc4a65044df8b44fff4e9bfeac7233fa381c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/266080
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37248}
diff --git a/BUILD.gn b/BUILD.gn
index 461138a..75af939 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -555,6 +555,8 @@
       "api/audio/test:audio_api_unittests",
       "api/audio_codecs/test:audio_codecs_api_unittests",
       "api/numerics:numerics_unittests",
+      "api/task_queue:pending_task_safety_flag_unittests",
+      "api/task_queue:to_queued_task_unittests",
       "api/transport:stun_unittest",
       "api/video/test:rtc_api_video_unittests",
       "api/video_codecs/test:video_codecs_api_unittests",
@@ -574,9 +576,7 @@
       "rtc_base:weak_ptr_unittests",
       "rtc_base/experiments:experiments_unittests",
       "rtc_base/system:file_wrapper_unittests",
-      "rtc_base/task_utils:pending_task_safety_flag_unittests",
       "rtc_base/task_utils:repeating_task_unittests",
-      "rtc_base/task_utils:to_queued_task_unittests",
       "rtc_base/time:timestamp_extrapolator_unittests",
       "rtc_base/units:units_unittests",
       "sdk:sdk_tests",
diff --git a/api/metronome/test/BUILD.gn b/api/metronome/test/BUILD.gn
index d25d5a8..160b45c 100644
--- a/api/metronome/test/BUILD.gn
+++ b/api/metronome/test/BUILD.gn
@@ -18,12 +18,12 @@
     "..:metronome",
     "../..:priority",
     "../..:sequence_checker",
+    "../../../api/task_queue:to_queued_task",
     "../../../rtc_base:macromagic",
     "../../../rtc_base:rtc_event",
     "../../../rtc_base:rtc_task_queue",
     "../../../rtc_base/synchronization:mutex",
     "../../../rtc_base/task_utils:repeating_task",
-    "../../../rtc_base/task_utils:to_queued_task",
     "../../task_queue",
     "../../units:time_delta",
   ]
diff --git a/api/metronome/test/fake_metronome.cc b/api/metronome/test/fake_metronome.cc
index 83b5ea7..7813a3d 100644
--- a/api/metronome/test/fake_metronome.cc
+++ b/api/metronome/test/fake_metronome.cc
@@ -13,10 +13,10 @@
 #include "api/priority.h"
 #include "api/sequence_checker.h"
 #include "api/task_queue/task_queue_factory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/time_delta.h"
 #include "rtc_base/event.h"
 #include "rtc_base/task_utils/repeating_task.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc::test {
 
diff --git a/api/task_queue/BUILD.gn b/api/task_queue/BUILD.gn
index 1072057..94b8deb 100644
--- a/api/task_queue/BUILD.gn
+++ b/api/task_queue/BUILD.gn
@@ -61,10 +61,10 @@
   } else {
     deps = [
       ":task_queue",
+      "../../api/task_queue:to_queued_task",
       "../../rtc_base:refcount",
       "../../rtc_base:rtc_event",
       "../../rtc_base:timeutils",
-      "../../rtc_base/task_utils:to_queued_task",
       "../../test:test_support",
     ]
     absl_deps = [
@@ -97,6 +97,29 @@
   }
 }
 
+rtc_source_set("to_queued_task") {
+  visibility = [ "*" ]
+  sources = [ "to_queued_task.h" ]
+  deps = [
+    ":task_queue",
+    "../../api/task_queue:pending_task_safety_flag",
+  ]
+}
+
+rtc_library("pending_task_safety_flag") {
+  sources = [
+    "pending_task_safety_flag.cc",
+    "pending_task_safety_flag.h",
+  ]
+  deps = [
+    "../../api:refcountedbase",
+    "../../api:scoped_refptr",
+    "../../api:sequence_checker",
+    "../../rtc_base:checks",
+    "../../rtc_base/system:no_unique_address",
+  ]
+}
+
 if (rtc_include_tests) {
   rtc_library("task_queue_default_factory_unittests") {
     testonly = true
@@ -107,4 +130,29 @@
       "../../test:test_support",
     ]
   }
+
+  rtc_library("to_queued_task_unittests") {
+    testonly = true
+    sources = [ "to_queued_task_unittest.cc" ]
+    deps = [
+      ":to_queued_task",
+      "../../api/task_queue",
+      "../../test:test_support",
+    ]
+    absl_deps = [ "//third_party/abseil-cpp/absl/memory" ]
+  }
+
+  rtc_library("pending_task_safety_flag_unittests") {
+    testonly = true
+    sources = [ "pending_task_safety_flag_unittest.cc" ]
+    deps = [
+      ":pending_task_safety_flag",
+      ":to_queued_task",
+      "../../rtc_base:logging",
+      "../../rtc_base:rtc_event",
+      "../../rtc_base:rtc_task_queue",
+      "../../rtc_base:task_queue_for_test",
+      "../../test:test_support",
+    ]
+  }
 }
diff --git a/api/task_queue/DEPS b/api/task_queue/DEPS
index fab6056..1365edb 100644
--- a/api/task_queue/DEPS
+++ b/api/task_queue/DEPS
@@ -7,4 +7,8 @@
   "task_queue_test\.h": [
     "+test/gtest.h",
   ],
+  "pending_task_safety_flag.h": [
+    "+rtc_base/checks.h",
+    "+rtc_base/system/no_unique_address.h",
+  ],
 }
diff --git a/rtc_base/task_utils/pending_task_safety_flag.cc b/api/task_queue/pending_task_safety_flag.cc
similarity index 96%
rename from rtc_base/task_utils/pending_task_safety_flag.cc
rename to api/task_queue/pending_task_safety_flag.cc
index 8bff213..437ce07 100644
--- a/rtc_base/task_utils/pending_task_safety_flag.cc
+++ b/api/task_queue/pending_task_safety_flag.cc
@@ -8,7 +8,7 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 
 namespace webrtc {
 
diff --git a/api/task_queue/pending_task_safety_flag.h b/api/task_queue/pending_task_safety_flag.h
new file mode 100644
index 0000000..edfd8e9
--- /dev/null
+++ b/api/task_queue/pending_task_safety_flag.h
@@ -0,0 +1,160 @@
+/*
+ *  Copyright 2020 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_TASK_QUEUE_PENDING_TASK_SAFETY_FLAG_H_
+#define API_TASK_QUEUE_PENDING_TASK_SAFETY_FLAG_H_
+
+#include <utility>
+
+#include "api/ref_counted_base.h"
+#include "api/scoped_refptr.h"
+#include "api/sequence_checker.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/system/no_unique_address.h"
+
+namespace webrtc {
+
+// The PendingTaskSafetyFlag and the ScopedTaskSafety are designed to address
+// the issue where you have a task to be executed later that has references,
+// but cannot guarantee that the referenced object is alive when the task is
+// executed.
+
+// This mechanism can be used with tasks that are created and destroyed
+// on a single thread / task queue, and with tasks posted to the same
+// thread/task queue, but tasks can be posted from any thread/TQ.
+
+// Typical usage:
+// When posting a task, post a copy (capture by-value in a lambda) of the flag
+// reference and before performing the work, check the `alive()` state. Abort if
+// alive() returns `false`:
+//
+// class ExampleClass {
+// ....
+//    my_task_queue_->PostTask(ToQueuedTask(
+//        [safety = pending_task_safety_flag_, this]() {
+//          // Now running on the main thread.
+//          if (!safety->alive())
+//            return;
+//          MyMethod();
+//        }));
+//   ....
+//   ~ExampleClass() {
+//     pending_task_safety_flag_->SetNotAlive();
+//   }
+//   scoped_refptr<PendingTaskSafetyFlag> pending_task_safety_flag_
+//        = PendingTaskSafetyFlag::Create();
+// }
+//
+// ToQueuedTask has an overload that makes this check automatic:
+//
+//    my_task_queue_->PostTask(ToQueuedTask(pending_task_safety_flag_,
+//        [this]() { MyMethod(); }));
+//
+class PendingTaskSafetyFlag final
+    : public rtc::RefCountedNonVirtual<PendingTaskSafetyFlag> {
+ public:
+  static rtc::scoped_refptr<PendingTaskSafetyFlag> Create();
+
+  // Creates a flag, but with its SequenceChecker initially detached. Hence, it
+  // may be created on a different thread than the flag will be used on.
+  static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateDetached();
+
+  // Same as `CreateDetached()` except the initial state of the returned flag
+  // will be `!alive()`.
+  static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateDetachedInactive();
+
+  ~PendingTaskSafetyFlag() = default;
+
+  void SetNotAlive();
+  // The SetAlive method is intended to support Start/Stop/Restart usecases.
+  // When a class has called SetNotAlive on a flag used for posted tasks, and
+  // decides it wants to post new tasks and have them run, there are two
+  // reasonable ways to do that:
+  //
+  // (i) Use the below SetAlive method. One subtlety is that any task posted
+  //     prior to SetNotAlive, and still in the queue, is resurrected and will
+  //     run.
+  //
+  // (ii) Create a fresh flag, and just drop the reference to the old one. This
+  //      avoids the above problem, and ensures that tasks poster prior to
+  //      SetNotAlive stay cancelled. Instead, there's a potential data race on
+  //      the flag pointer itself. Some synchronization is required between the
+  //      thread overwriting the flag pointer, and the threads that want to post
+  //      tasks and therefore read that same pointer.
+  void SetAlive();
+  bool alive() const;
+
+ protected:
+  explicit PendingTaskSafetyFlag(bool alive) : alive_(alive) {}
+
+ private:
+  static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateInternal(bool alive);
+
+  bool alive_ = true;
+  RTC_NO_UNIQUE_ADDRESS SequenceChecker main_sequence_;
+};
+
+// The ScopedTaskSafety makes using PendingTaskSafetyFlag very simple.
+// It does automatic PTSF creation and signalling of destruction when the
+// ScopedTaskSafety instance goes out of scope.
+//
+// ToQueuedTask has an overload that takes a ScopedTaskSafety too, so there
+// is no need to explicitly call the "flag" method.
+//
+// Example usage:
+//
+//     my_task_queue->PostTask(ToQueuedTask(scoped_task_safety,
+//        [this]() {
+//             // task goes here
+//        }
+//
+// This should be used by the class that wants tasks dropped after destruction.
+// The requirement is that the instance has to be constructed and destructed on
+// the same thread as the potentially dropped tasks would be running on.
+class ScopedTaskSafety final {
+ public:
+  ScopedTaskSafety() = default;
+  explicit ScopedTaskSafety(rtc::scoped_refptr<PendingTaskSafetyFlag> flag)
+      : flag_(std::move(flag)) {}
+  ~ScopedTaskSafety() { flag_->SetNotAlive(); }
+
+  // Returns a new reference to the safety flag.
+  rtc::scoped_refptr<PendingTaskSafetyFlag> flag() const { return flag_; }
+
+  // Marks the current flag as not-alive and attaches to a new one.
+  void reset(rtc::scoped_refptr<PendingTaskSafetyFlag> new_flag =
+                 PendingTaskSafetyFlag::Create()) {
+    flag_->SetNotAlive();
+    flag_ = std::move(new_flag);
+  }
+
+ private:
+  rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ =
+      PendingTaskSafetyFlag::Create();
+};
+
+// Like ScopedTaskSafety, but allows construction on a different thread than
+// where the flag will be used.
+class ScopedTaskSafetyDetached final {
+ public:
+  ScopedTaskSafetyDetached() = default;
+  ~ScopedTaskSafetyDetached() { flag_->SetNotAlive(); }
+
+  // Returns a new reference to the safety flag.
+  rtc::scoped_refptr<PendingTaskSafetyFlag> flag() const { return flag_; }
+
+ private:
+  rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ =
+      PendingTaskSafetyFlag::CreateDetached();
+};
+
+}  // namespace webrtc
+
+#endif  // API_TASK_QUEUE_PENDING_TASK_SAFETY_FLAG_H_
diff --git a/rtc_base/task_utils/pending_task_safety_flag_unittest.cc b/api/task_queue/pending_task_safety_flag_unittest.cc
similarity index 96%
rename from rtc_base/task_utils/pending_task_safety_flag_unittest.cc
rename to api/task_queue/pending_task_safety_flag_unittest.cc
index ec5f6a5..f045cab 100644
--- a/rtc_base/task_utils/pending_task_safety_flag_unittest.cc
+++ b/api/task_queue/pending_task_safety_flag_unittest.cc
@@ -8,14 +8,14 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 
 #include <memory>
 
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/event.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/task_queue_for_test.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 
@@ -120,9 +120,7 @@
       RTC_DCHECK(tq_main_);
       *stuff_done_ = false;
     }
-    ~Owner() {
-      RTC_DCHECK(tq_main_->IsCurrent());
-    }
+    ~Owner() { RTC_DCHECK(tq_main_->IsCurrent()); }
 
     void DoStuff() {
       RTC_DCHECK(!tq_main_->IsCurrent());
diff --git a/api/task_queue/task_queue_test.cc b/api/task_queue/task_queue_test.cc
index 3458edb..0473b05 100644
--- a/api/task_queue/task_queue_test.cc
+++ b/api/task_queue/task_queue_test.cc
@@ -11,9 +11,9 @@
 
 #include "absl/memory/memory.h"
 #include "absl/strings/string_view.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/event.h"
 #include "rtc_base/ref_counter.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 
 namespace webrtc {
diff --git a/api/task_queue/to_queued_task.h b/api/task_queue/to_queued_task.h
new file mode 100644
index 0000000..658eecc
--- /dev/null
+++ b/api/task_queue/to_queued_task.h
@@ -0,0 +1,111 @@
+/*
+ *  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_TASK_QUEUE_TO_QUEUED_TASK_H_
+#define API_TASK_QUEUE_TO_QUEUED_TASK_H_
+
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/queued_task.h"
+
+namespace webrtc {
+namespace webrtc_new_closure_impl {
+// Simple implementation of QueuedTask for use with lambdas.
+template <typename Closure>
+class ClosureTask : public QueuedTask {
+ public:
+  explicit ClosureTask(Closure&& closure)
+      : closure_(std::forward<Closure>(closure)) {}
+
+ private:
+  bool Run() override {
+    closure_();
+    return true;
+  }
+
+  typename std::decay<Closure>::type closure_;
+};
+
+template <typename Closure>
+class SafetyClosureTask : public QueuedTask {
+ public:
+  explicit SafetyClosureTask(rtc::scoped_refptr<PendingTaskSafetyFlag> safety,
+                             Closure&& closure)
+      : closure_(std::forward<Closure>(closure)),
+        safety_flag_(std::move(safety)) {}
+
+ private:
+  bool Run() override {
+    if (safety_flag_->alive())
+      closure_();
+    return true;
+  }
+
+  typename std::decay<Closure>::type closure_;
+  rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag_;
+};
+
+// Extends ClosureTask to also allow specifying cleanup code.
+// This is useful when using lambdas if guaranteeing cleanup, even if a task
+// was dropped (queue is too full), is required.
+template <typename Closure, typename Cleanup>
+class ClosureTaskWithCleanup : public ClosureTask<Closure> {
+ public:
+  ClosureTaskWithCleanup(Closure&& closure, Cleanup&& cleanup)
+      : ClosureTask<Closure>(std::forward<Closure>(closure)),
+        cleanup_(std::forward<Cleanup>(cleanup)) {}
+  ~ClosureTaskWithCleanup() override { cleanup_(); }
+
+ private:
+  typename std::decay<Cleanup>::type cleanup_;
+};
+}  // namespace webrtc_new_closure_impl
+
+// Convenience function to construct closures that can be passed directly
+// to methods that support std::unique_ptr<QueuedTask> but not template
+// based parameters.
+template <typename Closure>
+std::unique_ptr<QueuedTask> ToQueuedTask(Closure&& closure) {
+  return std::make_unique<webrtc_new_closure_impl::ClosureTask<Closure>>(
+      std::forward<Closure>(closure));
+}
+
+template <typename Closure>
+std::unique_ptr<QueuedTask> ToQueuedTask(
+    rtc::scoped_refptr<PendingTaskSafetyFlag> safety,
+    Closure&& closure) {
+  return std::make_unique<webrtc_new_closure_impl::SafetyClosureTask<Closure>>(
+      std::move(safety), std::forward<Closure>(closure));
+}
+
+template <typename Closure>
+std::unique_ptr<QueuedTask> ToQueuedTask(const ScopedTaskSafety& safety,
+                                         Closure&& closure) {
+  return ToQueuedTask(safety.flag(), std::forward<Closure>(closure));
+}
+
+template <typename Closure,
+          typename Cleanup,
+          typename std::enable_if<!std::is_same<
+              typename std::remove_const<
+                  typename std::remove_reference<Closure>::type>::type,
+              ScopedTaskSafety>::value>::type* = nullptr>
+std::unique_ptr<QueuedTask> ToQueuedTask(Closure&& closure, Cleanup&& cleanup) {
+  return std::make_unique<
+      webrtc_new_closure_impl::ClosureTaskWithCleanup<Closure, Cleanup>>(
+      std::forward<Closure>(closure), std::forward<Cleanup>(cleanup));
+}
+
+}  // namespace webrtc
+
+#endif  // API_TASK_QUEUE_TO_QUEUED_TASK_H_
diff --git a/rtc_base/task_utils/to_queued_task_unittest.cc b/api/task_queue/to_queued_task_unittest.cc
similarity index 98%
rename from rtc_base/task_utils/to_queued_task_unittest.cc
rename to api/task_queue/to_queued_task_unittest.cc
index 852307a..aa11041 100644
--- a/rtc_base/task_utils/to_queued_task_unittest.cc
+++ b/api/task_queue/to_queued_task_unittest.cc
@@ -8,7 +8,7 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "rtc_base/task_utils/to_queued_task.h"
+#include "api/task_queue/to_queued_task.h"
 
 #include <memory>
 
diff --git a/audio/BUILD.gn b/audio/BUILD.gn
index 1878154..3be1cad 100644
--- a/audio/BUILD.gn
+++ b/audio/BUILD.gn
@@ -61,6 +61,8 @@
     "../api/neteq:neteq_api",
     "../api/rtc_event_log",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/transport/rtp:rtp_source",
     "../call:audio_sender_interface",
     "../call:bitrate_allocator",
@@ -105,8 +107,6 @@
     "../rtc_base/experiments:field_trial_parser",
     "../rtc_base/synchronization:mutex",
     "../rtc_base/system:no_unique_address",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../system_wrappers",
     "../system_wrappers:field_trial",
     "../system_wrappers:metrics",
diff --git a/audio/channel_receive.cc b/audio/channel_receive.cc
index 0bf60c0..1573765 100644
--- a/audio/channel_receive.cc
+++ b/audio/channel_receive.cc
@@ -21,7 +21,9 @@
 #include "api/frame_transformer_interface.h"
 #include "api/rtc_event_log/rtc_event_log.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "audio/audio_level.h"
 #include "audio/channel_receive_frame_transformer_delegate.h"
 #include "audio/channel_send.h"
@@ -46,8 +48,6 @@
 #include "rtc_base/race_checker.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/system/no_unique_address.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "system_wrappers/include/metrics.h"
 
diff --git a/audio/channel_receive_frame_transformer_delegate.cc b/audio/channel_receive_frame_transformer_delegate.cc
index c9865cb..085721e 100644
--- a/audio/channel_receive_frame_transformer_delegate.cc
+++ b/audio/channel_receive_frame_transformer_delegate.cc
@@ -12,8 +12,8 @@
 
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/buffer.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc {
 namespace {
diff --git a/call/BUILD.gn b/call/BUILD.gn
index 594996d..1f5e707 100644
--- a/call/BUILD.gn
+++ b/call/BUILD.gn
@@ -303,6 +303,7 @@
     "../api:simulated_network_api",
     "../api:transport_api",
     "../api/rtc_event_log",
+    "../api/task_queue:pending_task_safety_flag",
     "../api/transport:network_control",
     "../api/units:time_delta",
     "../api/video_codecs:video_codecs_api",
@@ -331,7 +332,6 @@
     "../rtc_base/experiments:field_trial_parser",
     "../rtc_base/network:sent_packet",
     "../rtc_base/system:no_unique_address",
-    "../rtc_base/task_utils:pending_task_safety_flag",
     "../system_wrappers",
     "../system_wrappers:field_trial",
     "../system_wrappers:metrics",
@@ -557,6 +557,7 @@
         "../api/rtc_event_log:rtc_event_log_factory",
         "../api/task_queue",
         "../api/task_queue:default_task_queue_factory",
+        "../api/task_queue:pending_task_safety_flag",
         "../api/video:builtin_video_bitrate_allocator_factory",
         "../api/video:video_bitrate_allocation",
         "../api/video_codecs:video_codecs_api",
@@ -579,7 +580,6 @@
         "../rtc_base:threading",
         "../rtc_base:timeutils",
         "../rtc_base/synchronization:mutex",
-        "../rtc_base/task_utils:pending_task_safety_flag",
         "../rtc_base/task_utils:repeating_task",
         "../system_wrappers",
         "../system_wrappers:metrics",
diff --git a/call/adaptation/BUILD.gn b/call/adaptation/BUILD.gn
index 3c87920..1aa4da7 100644
--- a/call/adaptation/BUILD.gn
+++ b/call/adaptation/BUILD.gn
@@ -39,6 +39,7 @@
     "../../api:sequence_checker",
     "../../api/adaptation:resource_adaptation_api",
     "../../api/task_queue:task_queue",
+    "../../api/task_queue:to_queued_task",
     "../../api/video:video_adaptation",
     "../../api/video:video_frame",
     "../../api/video:video_stream_encoder",
@@ -54,7 +55,6 @@
     "../../rtc_base/experiments:balanced_degradation_settings",
     "../../rtc_base/synchronization:mutex",
     "../../rtc_base/system:no_unique_address",
-    "../../rtc_base/task_utils:to_queued_task",
   ]
   absl_deps = [
     "//third_party/abseil-cpp/absl/algorithm:container",
@@ -121,8 +121,8 @@
       "../../api:sequence_checker",
       "../../api/adaptation:resource_adaptation_api",
       "../../api/task_queue:task_queue",
+      "../../api/task_queue:to_queued_task",
       "../../api/video:video_stream_encoder",
-      "../../rtc_base/task_utils:to_queued_task",
       "../../test:test_support",
     ]
     absl_deps = [
diff --git a/call/adaptation/resource_adaptation_processor.cc b/call/adaptation/resource_adaptation_processor.cc
index 2042424..1f9b662 100644
--- a/call/adaptation/resource_adaptation_processor.cc
+++ b/call/adaptation/resource_adaptation_processor.cc
@@ -17,11 +17,11 @@
 #include "absl/algorithm/container.h"
 #include "absl/strings/string_view.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/video/video_adaptation_counters.h"
 #include "call/adaptation/video_stream_adapter.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc {
 
diff --git a/call/call.cc b/call/call.cc
index 73efe6f..d84964d 100644
--- a/call/call.cc
+++ b/call/call.cc
@@ -25,6 +25,7 @@
 #include "absl/types/optional.h"
 #include "api/rtc_event_log/rtc_event_log.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/transport/network_control.h"
 #include "audio/audio_receive_stream.h"
 #include "audio/audio_send_stream.h"
@@ -56,7 +57,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/string_builder.h"
 #include "rtc_base/system/no_unique_address.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
diff --git a/call/call_perf_tests.cc b/call/call_perf_tests.cc
index db384c1..5be6385 100644
--- a/call/call_perf_tests.cc
+++ b/call/call_perf_tests.cc
@@ -16,6 +16,7 @@
 #include "absl/strings/string_view.h"
 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
 #include "api/rtc_event_log/rtc_event_log.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/test/simulated_network.h"
 #include "api/video/builtin_video_bitrate_allocator_factory.h"
@@ -34,7 +35,6 @@
 #include "rtc_base/checks.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/task_queue_for_test.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
 #include "system_wrappers/include/metrics.h"
diff --git a/call/degraded_call.h b/call/degraded_call.h
index a527677..fe5fd7c 100644
--- a/call/degraded_call.h
+++ b/call/degraded_call.h
@@ -25,6 +25,7 @@
 #include "api/fec_controller.h"
 #include "api/media_types.h"
 #include "api/rtp_headers.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/test/simulated_network.h"
 #include "api/video_codecs/video_encoder_config.h"
 #include "call/audio_receive_stream.h"
@@ -40,7 +41,6 @@
 #include "rtc_base/copy_on_write_buffer.h"
 #include "rtc_base/network/sent_packet.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "system_wrappers/include/clock.h"
 
 namespace webrtc {
diff --git a/media/BUILD.gn b/media/BUILD.gn
index 9370200..4079d1b 100644
--- a/media/BUILD.gn
+++ b/media/BUILD.gn
@@ -64,6 +64,8 @@
     "../api/crypto:frame_decryptor_interface",
     "../api/crypto:frame_encryptor_interface",
     "../api/crypto:options",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/transport:datagram_transport_interface",
     "../api/transport:stun_types",
     "../api/transport/rtp:rtp_source",
@@ -95,8 +97,6 @@
     "../rtc_base/system:file_wrapper",
     "../rtc_base/system:no_unique_address",
     "../rtc_base/system:rtc_export",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/sigslot",
     "../system_wrappers:field_trial",
   ]
@@ -277,6 +277,8 @@
     "../api/audio:audio_mixer_api",
     "../api/audio_codecs:audio_codecs_api",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/transport:bitrate_settings",
     "../api/transport:field_trial_based_config",
     "../api/transport/rtp:rtp_source",
@@ -326,8 +328,6 @@
     "../rtc_base/experiments:rate_control_settings",
     "../rtc_base/synchronization:mutex",
     "../rtc_base/system:rtc_export",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/base64",
     "../system_wrappers",
     "../system_wrappers:metrics",
@@ -419,7 +419,9 @@
     deps = [
       ":rtc_data_sctp_transport_internal",
       "../api:array_view",
+      "../api/task_queue:pending_task_safety_flag",
       "../api/task_queue:task_queue",
+      "../api/task_queue:to_queued_task",
       "../media:rtc_media_base",
       "../net/dcsctp/public:factory",
       "../net/dcsctp/public:socket",
@@ -436,8 +438,6 @@
       "../rtc_base:stringutils",
       "../rtc_base:threading",
       "../rtc_base/containers:flat_map",
-      "../rtc_base/task_utils:pending_task_safety_flag",
-      "../rtc_base/task_utils:to_queued_task",
       "../rtc_base/third_party/sigslot:sigslot",
       "../system_wrappers",
     ]
diff --git a/media/base/media_channel.cc b/media/base/media_channel.cc
index 1b11fcc..c3b2480 100644
--- a/media/base/media_channel.cc
+++ b/media/base/media_channel.cc
@@ -10,8 +10,8 @@
 
 #include "media/base/media_channel.h"
 
+#include "api/task_queue/to_queued_task.h"
 #include "media/base/rtp_utils.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace cricket {
 using webrtc::FrameDecryptorInterface;
diff --git a/media/base/media_channel.h b/media/base/media_channel.h
index ec78ec8..22673ce 100644
--- a/media/base/media_channel.h
+++ b/media/base/media_channel.h
@@ -26,6 +26,7 @@
 #include "api/media_stream_interface.h"
 #include "api/rtc_error.h"
 #include "api/rtp_parameters.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/transport/data_channel_transport_interface.h"
 #include "api/transport/rtp/rtp_source.h"
 #include "api/units/time_delta.h"
@@ -52,7 +53,6 @@
 #include "rtc_base/socket.h"
 #include "rtc_base/string_encode.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 
 namespace rtc {
 class Timing;
diff --git a/media/engine/webrtc_video_engine.h b/media/engine/webrtc_video_engine.h
index b2780c4..e080eda 100644
--- a/media/engine/webrtc_video_engine.h
+++ b/media/engine/webrtc_video_engine.h
@@ -20,6 +20,7 @@
 #include "absl/types/optional.h"
 #include "api/call/transport.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/transport/field_trial_based_config.h"
 #include "api/video/video_bitrate_allocator_factory.h"
 #include "api/video/video_frame.h"
@@ -34,7 +35,6 @@
 #include "media/engine/unhandled_packets_buffer.h"
 #include "rtc_base/network_route.h"
 #include "rtc_base/synchronization/mutex.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace webrtc {
diff --git a/media/engine/webrtc_voice_engine.cc b/media/engine/webrtc_voice_engine.cc
index 78ad7a1..efdb549 100644
--- a/media/engine/webrtc_voice_engine.cc
+++ b/media/engine/webrtc_voice_engine.cc
@@ -24,6 +24,8 @@
 #include "api/audio_codecs/audio_codec_pair_id.h"
 #include "api/call/audio_sink.h"
 #include "api/field_trials_view.h"
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "media/base/audio_source.h"
 #include "media/base/media_constants.h"
 #include "media/base/stream_params.h"
@@ -48,8 +50,6 @@
 #include "rtc_base/strings/audio_format_to_string.h"
 #include "rtc_base/strings/string_builder.h"
 #include "rtc_base/strings/string_format.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/third_party/base64/base64.h"
 #include "rtc_base/trace_event.h"
 #include "system_wrappers/include/metrics.h"
diff --git a/media/engine/webrtc_voice_engine.h b/media/engine/webrtc_voice_engine.h
index e28a12a..e55a0a3 100644
--- a/media/engine/webrtc_voice_engine.h
+++ b/media/engine/webrtc_voice_engine.h
@@ -20,6 +20,7 @@
 #include "api/field_trials_view.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_factory.h"
 #include "api/transport/rtp/rtp_source.h"
 #include "call/audio_state.h"
@@ -30,7 +31,6 @@
 #include "rtc_base/buffer.h"
 #include "rtc_base/network_route.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 
 namespace webrtc {
 class AudioFrameProcessor;
diff --git a/modules/pacing/BUILD.gn b/modules/pacing/BUILD.gn
index ed6d2bd..3095df9 100644
--- a/modules/pacing/BUILD.gn
+++ b/modules/pacing/BUILD.gn
@@ -39,6 +39,7 @@
     "../../api:sequence_checker",
     "../../api/rtc_event_log",
     "../../api/task_queue:task_queue",
+    "../../api/task_queue:to_queued_task",
     "../../api/transport:field_trial_based_config",
     "../../api/transport:network_control",
     "../../api/units:data_rate",
@@ -58,7 +59,6 @@
     "../../rtc_base/experiments:field_trial_parser",
     "../../rtc_base/synchronization:mutex",
     "../../rtc_base/system:unused",
-    "../../rtc_base/task_utils:to_queued_task",
     "../../system_wrappers",
     "../../system_wrappers:metrics",
     "../rtp_rtcp",
@@ -101,6 +101,7 @@
       ":interval_budget",
       ":pacing",
       "../../api/task_queue:task_queue",
+      "../../api/task_queue:to_queued_task",
       "../../api/transport:network_control",
       "../../api/units:data_rate",
       "../../api/units:time_delta",
@@ -109,7 +110,6 @@
       "../../rtc_base:checks",
       "../../rtc_base:rtc_base_tests_utils",
       "../../rtc_base/experiments:alr_experiment",
-      "../../rtc_base/task_utils:to_queued_task",
       "../../system_wrappers",
       "../../test:explicit_key_value_config",
       "../../test:scoped_key_value_config",
diff --git a/modules/pacing/task_queue_paced_sender_unittest.cc b/modules/pacing/task_queue_paced_sender_unittest.cc
index 70ee31e..44316ef 100644
--- a/modules/pacing/task_queue_paced_sender_unittest.cc
+++ b/modules/pacing/task_queue_paced_sender_unittest.cc
@@ -19,11 +19,11 @@
 #include <vector>
 
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/network_types.h"
 #include "api/units/data_rate.h"
 #include "modules/pacing/packet_router.h"
 #include "modules/utility/include/mock/mock_process_thread.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/scoped_key_value_config.h"
diff --git a/modules/rtp_rtcp/BUILD.gn b/modules/rtp_rtcp/BUILD.gn
index ce9c409..bab5bf9 100644
--- a/modules/rtp_rtcp/BUILD.gn
+++ b/modules/rtp_rtcp/BUILD.gn
@@ -273,7 +273,9 @@
     "../../api/audio_codecs:audio_codecs_api",
     "../../api/crypto:frame_encryptor_interface",
     "../../api/rtc_event_log",
+    "../../api/task_queue:pending_task_safety_flag",
     "../../api/task_queue:task_queue",
+    "../../api/task_queue:to_queued_task",
     "../../api/transport:field_trial_based_config",
     "../../api/transport/rtp:dependency_descriptor",
     "../../api/transport/rtp:rtp_source",
@@ -320,9 +322,7 @@
     "../../rtc_base/experiments:field_trial_parser",
     "../../rtc_base/synchronization:mutex",
     "../../rtc_base/system:no_unique_address",
-    "../../rtc_base/task_utils:pending_task_safety_flag",
     "../../rtc_base/task_utils:repeating_task",
-    "../../rtc_base/task_utils:to_queued_task",
     "../../rtc_base/time:timestamp_extrapolator",
     "../../system_wrappers",
     "../../system_wrappers:metrics",
@@ -394,6 +394,7 @@
     "../../api:rtp_headers",
     "../../api:transport_api",
     "../../api/task_queue",
+    "../../api/task_queue:to_queued_task",
     "../../api/units:data_rate",
     "../../api/units:time_delta",
     "../../api/units:timestamp",
@@ -406,7 +407,6 @@
     "../../rtc_base:timeutils",
     "../../rtc_base/containers:flat_map",
     "../../rtc_base/task_utils:repeating_task",
-    "../../rtc_base/task_utils:to_queued_task",
     "../../system_wrappers",
   ]
   absl_deps = [
@@ -616,6 +616,7 @@
       "../../api:time_controller",
       "../../api:transport_api",
       "../../api/rtc_event_log",
+      "../../api/task_queue:to_queued_task",
       "../../api/transport:field_trial_based_config",
       "../../api/transport/rtp:dependency_descriptor",
       "../../api/units:data_rate",
@@ -650,7 +651,6 @@
       "../../rtc_base:task_queue_for_test",
       "../../rtc_base:threading",
       "../../rtc_base:timeutils",
-      "../../rtc_base/task_utils:to_queued_task",
       "../../system_wrappers",
       "../../test:field_trial",
       "../../test:mock_frame_transformer",
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver.cc b/modules/rtp_rtcp/source/rtcp_transceiver.cc
index 108d92c..61739f6 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver.cc
+++ b/modules/rtp_rtcp/source/rtcp_transceiver.cc
@@ -14,11 +14,11 @@
 #include <utility>
 #include <vector>
 
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/timestamp.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/event.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "system_wrappers/include/clock.h"
 
 namespace webrtc {
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver_impl.cc b/modules/rtp_rtcp/source/rtcp_transceiver_impl.cc
index f6c9085..6dc8145 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver_impl.cc
+++ b/modules/rtp_rtcp/source/rtcp_transceiver_impl.cc
@@ -16,6 +16,7 @@
 #include "absl/algorithm/container.h"
 #include "absl/memory/memory.h"
 #include "api/call/transport.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/video/video_bitrate_allocation.h"
 #include "modules/rtp_rtcp/include/receive_statistics.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
@@ -36,7 +37,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/divide_round.h"
 #include "rtc_base/task_utils/repeating_task.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "system_wrappers/include/clock.h"
 
 namespace webrtc {
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver_impl_unittest.cc b/modules/rtp_rtcp/source/rtcp_transceiver_impl_unittest.cc
index 97ceb56..da7a875 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver_impl_unittest.cc
+++ b/modules/rtp_rtcp/source/rtcp_transceiver_impl_unittest.cc
@@ -16,6 +16,7 @@
 
 #include "absl/memory/memory.h"
 #include "api/rtp_headers.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/test/create_time_controller.h"
 #include "api/test/time_controller.h"
 #include "api/units/data_rate.h"
@@ -28,7 +29,6 @@
 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
 #include "modules/rtp_rtcp/source/time_util.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "system_wrappers/include/clock.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
diff --git a/modules/rtp_rtcp/source/rtp_rtcp_impl2.cc b/modules/rtp_rtcp/source/rtp_rtcp_impl2.cc
index 115727e..a8b6235e 100644
--- a/modules/rtp_rtcp/source/rtp_rtcp_impl2.cc
+++ b/modules/rtp_rtcp/source/rtp_rtcp_impl2.cc
@@ -22,6 +22,7 @@
 #include "absl/strings/string_view.h"
 #include "absl/types/optional.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/field_trial_based_config.h"
 #include "api/units/time_delta.h"
 #include "api/units/timestamp.h"
@@ -29,7 +30,6 @@
 #include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "system_wrappers/include/ntp_time.h"
 
diff --git a/modules/rtp_rtcp/source/rtp_rtcp_impl2.h b/modules/rtp_rtcp/source/rtp_rtcp_impl2.h
index 5f924e9..67c45e5 100644
--- a/modules/rtp_rtcp/source/rtp_rtcp_impl2.h
+++ b/modules/rtp_rtcp/source/rtp_rtcp_impl2.h
@@ -23,7 +23,9 @@
 #include "absl/types/optional.h"
 #include "api/rtp_headers.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/time_delta.h"
 #include "api/video/video_bitrate_allocation.h"
 #include "modules/include/module_fec_types.h"
@@ -40,9 +42,7 @@
 #include "rtc_base/gtest_prod_util.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/system/no_unique_address.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/repeating_task.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace webrtc {
diff --git a/modules/rtp_rtcp/source/rtp_sender_egress.cc b/modules/rtp_rtcp/source/rtp_sender_egress.cc
index 4a72059..e0f68b4 100644
--- a/modules/rtp_rtcp/source/rtp_sender_egress.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_egress.cc
@@ -16,10 +16,10 @@
 #include <utility>
 
 #include "absl/strings/match.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/field_trial_based_config.h"
 #include "logging/rtc_event_log/events/rtc_event_rtp_packet_outgoing.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc {
 namespace {
diff --git a/modules/rtp_rtcp/source/rtp_sender_egress.h b/modules/rtp_rtcp/source/rtp_sender_egress.h
index 1fd7128..0b440c2 100644
--- a/modules/rtp_rtcp/source/rtp_sender_egress.h
+++ b/modules/rtp_rtcp/source/rtp_sender_egress.h
@@ -20,6 +20,7 @@
 #include "api/call/transport.h"
 #include "api/rtc_event_log/rtc_event_log.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/units/data_rate.h"
 #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
@@ -32,7 +33,6 @@
 #include "rtc_base/rate_statistics.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/system/no_unique_address.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/repeating_task.h"
 #include "rtc_base/thread_annotations.h"
 
diff --git a/modules/rtp_rtcp/source/rtp_sender_unittest.cc b/modules/rtp_rtcp/source/rtp_sender_unittest.cc
index 0a1e3eb..2beb97c 100644
--- a/modules/rtp_rtcp/source/rtp_sender_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_unittest.cc
@@ -15,6 +15,7 @@
 
 #include "absl/strings/string_view.h"
 #include "api/rtc_event_log/rtc_event.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/field_trial_based_config.h"
 #include "api/video/video_codec_constants.h"
 #include "api/video/video_timing.h"
@@ -36,7 +37,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/rate_limiter.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "test/field_trial.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
diff --git a/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.cc b/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.cc
index c3295c6..af14b8c 100644
--- a/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.cc
@@ -14,9 +14,9 @@
 #include <vector>
 
 #include "absl/memory/memory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "modules/rtp_rtcp/source/rtp_descriptor_authentication.h"
 #include "modules/rtp_rtcp/source/rtp_sender_video.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc {
 namespace {
diff --git a/modules/video_coding/BUILD.gn b/modules/video_coding/BUILD.gn
index 5923605..d0e6c37 100644
--- a/modules/video_coding/BUILD.gn
+++ b/modules/video_coding/BUILD.gn
@@ -83,6 +83,7 @@
     "../../api:field_trials_view",
     "../../api:sequence_checker",
     "../../api/task_queue",
+    "../../api/task_queue:pending_task_safety_flag",
     "../../api/units:time_delta",
     "../../api/units:timestamp",
     "../../rtc_base:checks",
@@ -91,7 +92,6 @@
     "../../rtc_base:rtc_numerics",
     "../../rtc_base:rtc_task_queue",
     "../../rtc_base/experiments:field_trial_parser",
-    "../../rtc_base/task_utils:pending_task_safety_flag",
     "../../rtc_base/task_utils:repeating_task",
     "../../system_wrappers",
     "../utility",
@@ -224,6 +224,7 @@
     "../../api:scoped_refptr",
     "../../api:sequence_checker",
     "../../api/task_queue",
+    "../../api/task_queue:to_queued_task",
     "../../api/units:data_rate",
     "../../api/units:data_size",
     "../../api/units:frequency",
@@ -263,7 +264,6 @@
     "../../rtc_base/synchronization:mutex",
     "../../rtc_base/system:no_unique_address",
     "../../rtc_base/task_utils:repeating_task",
-    "../../rtc_base/task_utils:to_queued_task",
     "../../rtc_base/third_party/base64",
     "../../rtc_base/time:timestamp_extrapolator",
     "../../system_wrappers",
@@ -425,6 +425,7 @@
     "../../api:field_trials_view",
     "../../api:scoped_refptr",
     "../../api:sequence_checker",
+    "../../api/task_queue:to_queued_task",
     "../../api/video:encoded_frame",
     "../../api/video:encoded_image",
     "../../api/video:video_adaptation",
@@ -456,7 +457,6 @@
     "../../rtc_base/system:file_wrapper",
     "../../rtc_base/system:no_unique_address",
     "../../rtc_base/task_utils:repeating_task",
-    "../../rtc_base/task_utils:to_queued_task",
     "../../system_wrappers:field_trial",
     "../rtp_rtcp:rtp_rtcp_format",
   ]
@@ -856,6 +856,7 @@
       "../../api:sequence_checker",
       "../../api:videocodec_test_fixture_api",
       "../../api/task_queue",
+      "../../api/task_queue:to_queued_task",
       "../../api/video:builtin_video_bitrate_allocator_factory",
       "../../api/video:encoded_image",
       "../../api/video:video_bitrate_allocation",
@@ -873,7 +874,6 @@
       "../../rtc_base:timeutils",
       "../../rtc_base/synchronization:mutex",
       "../../rtc_base/system:no_unique_address",
-      "../../rtc_base/task_utils:to_queued_task",
       "../../test:test_support",
       "../../test:video_test_common",
       "../../test:video_test_support",
diff --git a/modules/video_coding/codecs/test/videoprocessor.cc b/modules/video_coding/codecs/test/videoprocessor.cc
index f5fd003..78fb90f 100644
--- a/modules/video_coding/codecs/test/videoprocessor.cc
+++ b/modules/video_coding/codecs/test/videoprocessor.cc
@@ -19,6 +19,7 @@
 #include <utility>
 
 #include "api/scoped_refptr.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/video/builtin_video_bitrate_allocator_factory.h"
 #include "api/video/i420_buffer.h"
 #include "api/video/video_bitrate_allocator_factory.h"
@@ -32,7 +33,6 @@
 #include "modules/video_coding/codecs/interface/common_constants.h"
 #include "modules/video_coding/include/video_error_codes.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "test/gtest.h"
 #include "third_party/libyuv/include/libyuv/compare.h"
diff --git a/modules/video_coding/nack_requester.h b/modules/video_coding/nack_requester.h
index fc44a59..066d395 100644
--- a/modules/video_coding/nack_requester.h
+++ b/modules/video_coding/nack_requester.h
@@ -19,12 +19,12 @@
 
 #include "api/field_trials_view.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/units/time_delta.h"
 #include "modules/include/module_common_types.h"
 #include "modules/video_coding/histogram.h"
 #include "rtc_base/numerics/sequence_number_util.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/repeating_task.h"
 #include "rtc_base/thread_annotations.h"
 #include "system_wrappers/include/clock.h"
diff --git a/modules/video_coding/utility/bandwidth_quality_scaler.cc b/modules/video_coding/utility/bandwidth_quality_scaler.cc
index 7632100..c23e5f0 100644
--- a/modules/video_coding/utility/bandwidth_quality_scaler.cc
+++ b/modules/video_coding/utility/bandwidth_quality_scaler.cc
@@ -15,6 +15,7 @@
 #include <utility>
 #include <vector>
 
+#include "api/task_queue/to_queued_task.h"
 #include "api/video/video_adaptation_reason.h"
 #include "api/video_codecs/video_encoder.h"
 #include "rtc_base/checks.h"
@@ -22,7 +23,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/exp_filter.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/weak_ptr.h"
 
diff --git a/modules/video_coding/utility/quality_scaler.cc b/modules/video_coding/utility/quality_scaler.cc
index 4d5e2b1..53b770b 100644
--- a/modules/video_coding/utility/quality_scaler.cc
+++ b/modules/video_coding/utility/quality_scaler.cc
@@ -13,13 +13,13 @@
 #include <memory>
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "api/video/video_adaptation_reason.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/experiments/quality_scaler_settings.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/exp_filter.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/weak_ptr.h"
 
 // TODO(kthelgason): Some versions of Android have issues with log2.
diff --git a/net/dcsctp/socket/BUILD.gn b/net/dcsctp/socket/BUILD.gn
index 4b6ab02..9456a42 100644
--- a/net/dcsctp/socket/BUILD.gn
+++ b/net/dcsctp/socket/BUILD.gn
@@ -228,6 +228,7 @@
       "../../../api:create_network_emulation_manager",
       "../../../api:network_emulation_manager_api",
       "../../../api/task_queue:task_queue",
+      "../../../api/task_queue:to_queued_task",
       "../../../api/units:time_delta",
       "../../../call:simulated_network",
       "../../../rtc_base:checks",
@@ -238,7 +239,6 @@
       "../../../rtc_base:socket_address",
       "../../../rtc_base:stringutils",
       "../../../rtc_base:timeutils",
-      "../../../rtc_base/task_utils:to_queued_task",
       "../../../test:test_support",
       "../common:handover_testing",
       "../common:internal_types",
diff --git a/net/dcsctp/socket/dcsctp_socket_network_test.cc b/net/dcsctp/socket/dcsctp_socket_network_test.cc
index 486679a..206ad6a 100644
--- a/net/dcsctp/socket/dcsctp_socket_network_test.cc
+++ b/net/dcsctp/socket/dcsctp_socket_network_test.cc
@@ -19,6 +19,7 @@
 #include "absl/types/optional.h"
 #include "api/array_view.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/test/create_network_emulation_manager.h"
 #include "api/test/network_emulation_manager.h"
 #include "api/units/time_delta.h"
@@ -34,7 +35,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/strings/string_format.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "test/gmock.h"
 
diff --git a/net/dcsctp/timer/BUILD.gn b/net/dcsctp/timer/BUILD.gn
index de1261f..a426a7a 100644
--- a/net/dcsctp/timer/BUILD.gn
+++ b/net/dcsctp/timer/BUILD.gn
@@ -33,11 +33,11 @@
 rtc_library("task_queue_timeout") {
   deps = [
     "../../../api:array_view",
+    "../../../api/task_queue:pending_task_safety_flag",
     "../../../api/task_queue:task_queue",
+    "../../../api/task_queue:to_queued_task",
     "../../../rtc_base:checks",
     "../../../rtc_base:logging",
-    "../../../rtc_base/task_utils:pending_task_safety_flag",
-    "../../../rtc_base/task_utils:to_queued_task",
     "../public:socket",
     "../public:types",
   ]
diff --git a/net/dcsctp/timer/task_queue_timeout.cc b/net/dcsctp/timer/task_queue_timeout.cc
index 4d1dc19..9df2f19 100644
--- a/net/dcsctp/timer/task_queue_timeout.cc
+++ b/net/dcsctp/timer/task_queue_timeout.cc
@@ -9,9 +9,9 @@
  */
 #include "net/dcsctp/timer/task_queue_timeout.h"
 
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace dcsctp {
 
diff --git a/net/dcsctp/timer/task_queue_timeout.h b/net/dcsctp/timer/task_queue_timeout.h
index 600b292..faae144 100644
--- a/net/dcsctp/timer/task_queue_timeout.h
+++ b/net/dcsctp/timer/task_queue_timeout.h
@@ -13,9 +13,9 @@
 #include <memory>
 #include <utility>
 
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
 #include "net/dcsctp/public/timeout.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 
 namespace dcsctp {
 
diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn
index 46ce807..171b1fe 100644
--- a/p2p/BUILD.gn
+++ b/p2p/BUILD.gn
@@ -135,13 +135,13 @@
     "../rtc_base/system:no_unique_address",
 
     # Needed by pseudo_tcp, which should move to a separate target.
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../rtc_base:safe_minmax",
     "../rtc_base:weak_ptr",
     "../rtc_base/network:sent_packet",
     "../rtc_base/synchronization:mutex",
     "../rtc_base/system:rtc_export",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/base64",
     "../rtc_base/third_party/sigslot",
     "../system_wrappers:metrics",
@@ -162,10 +162,10 @@
     deps = [
       ":rtc_p2p",
       "../api:libjingle_peerconnection_api",
+      "../api/task_queue:pending_task_safety_flag",
+      "../api/task_queue:to_queued_task",
       "../rtc_base",
       "../rtc_base:copy_on_write_buffer",
-      "../rtc_base/task_utils:pending_task_safety_flag",
-      "../rtc_base/task_utils:to_queued_task",
     ]
     absl_deps = [
       "//third_party/abseil-cpp/absl/algorithm:container",
@@ -311,6 +311,7 @@
     "../api:array_view",
     "../api:packet_socket_factory",
     "../api:sequence_checker",
+    "../api/task_queue:to_queued_task",
     "../api/transport:stun_types",
     "../rtc_base",
     "../rtc_base:byte_buffer",
@@ -320,7 +321,6 @@
     "../rtc_base:socket_address",
     "../rtc_base:stringutils",
     "../rtc_base:threading",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/sigslot",
   ]
   absl_deps = [
@@ -340,6 +340,8 @@
     ":rtc_p2p",
     "../api:packet_socket_factory",
     "../api:sequence_checker",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/transport:stun_types",
     "../rtc_base",
     "../rtc_base:async_resolver_interface",
@@ -351,8 +353,6 @@
     "../rtc_base:threading",
     "../rtc_base:timeutils",
     "../rtc_base/system:rtc_export",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
   ]
 }
 
diff --git a/p2p/base/fake_ice_transport.h b/p2p/base/fake_ice_transport.h
index 3ee4cd3..7a3d834 100644
--- a/p2p/base/fake_ice_transport.h
+++ b/p2p/base/fake_ice_transport.h
@@ -19,10 +19,10 @@
 #include "absl/algorithm/container.h"
 #include "absl/types/optional.h"
 #include "api/ice_transport_interface.h"
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "p2p/base/ice_transport_internal.h"
 #include "rtc_base/copy_on_write_buffer.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace cricket {
 
diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc
index 3afd0d3..509b332 100644
--- a/p2p/base/p2p_transport_channel.cc
+++ b/p2p/base/p2p_transport_channel.cc
@@ -26,6 +26,7 @@
 #include "api/candidate.h"
 #include "api/field_trials_view.h"
 #include "api/task_queue/queued_task.h"
+#include "api/task_queue/to_queued_task.h"
 #include "logging/rtc_event_log/ice_logger.h"
 #include "p2p/base/basic_async_resolver_factory.h"
 #include "p2p/base/basic_ice_controller.h"
@@ -41,7 +42,6 @@
 #include "rtc_base/network.h"
 #include "rtc_base/network_constants.h"
 #include "rtc_base/string_encode.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
diff --git a/p2p/base/p2p_transport_channel.h b/p2p/base/p2p_transport_channel.h
index cf7de90..df60629 100644
--- a/p2p/base/p2p_transport_channel.h
+++ b/p2p/base/p2p_transport_channel.h
@@ -40,6 +40,7 @@
 #include "api/ice_transport_interface.h"
 #include "api/rtc_error.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/transport/enums.h"
 #include "api/transport/stun.h"
 #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h"
@@ -67,7 +68,6 @@
 #include "rtc_base/socket_address.h"
 #include "rtc_base/strings/string_builder.h"
 #include "rtc_base/system/rtc_export.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
diff --git a/p2p/base/regathering_controller.cc b/p2p/base/regathering_controller.cc
index 9326c1a..3fe5c00 100644
--- a/p2p/base/regathering_controller.cc
+++ b/p2p/base/regathering_controller.cc
@@ -9,7 +9,8 @@
  */
 
 #include "p2p/base/regathering_controller.h"
-#include "rtc_base/task_utils/to_queued_task.h"
+
+#include "api/task_queue/to_queued_task.h"
 
 namespace webrtc {
 
diff --git a/p2p/base/regathering_controller.h b/p2p/base/regathering_controller.h
index 116d820..a0dfb80 100644
--- a/p2p/base/regathering_controller.h
+++ b/p2p/base/regathering_controller.h
@@ -13,9 +13,9 @@
 
 #include <memory>
 
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "p2p/base/ice_transport_internal.h"
 #include "p2p/base/port_allocator.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread.h"
 
 namespace webrtc {
diff --git a/p2p/base/stun_port.h b/p2p/base/stun_port.h
index cf865f9..2b04108 100644
--- a/p2p/base/stun_port.h
+++ b/p2p/base/stun_port.h
@@ -18,10 +18,10 @@
 
 #include "absl/memory/memory.h"
 #include "absl/strings/string_view.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "p2p/base/port.h"
 #include "p2p/base/stun_request.h"
 #include "rtc_base/async_packet_socket.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 
 namespace cricket {
 
diff --git a/p2p/base/stun_request.cc b/p2p/base/stun_request.cc
index c4d586c..b4bce73 100644
--- a/p2p/base/stun_request.cc
+++ b/p2p/base/stun_request.cc
@@ -16,11 +16,11 @@
 #include <vector>
 
 #include "absl/memory/memory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/helpers.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/string_encode.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"  // For TimeMillis
 
 namespace cricket {
diff --git a/p2p/base/stun_request.h b/p2p/base/stun_request.h
index 56d2597..cc1e1bf 100644
--- a/p2p/base/stun_request.h
+++ b/p2p/base/stun_request.h
@@ -19,10 +19,10 @@
 #include <memory>
 #include <string>
 
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/transport/stun.h"
 #include "api/units/time_delta.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 
 namespace cricket {
 
diff --git a/p2p/base/tcp_port.cc b/p2p/base/tcp_port.cc
index 8407134..546b544 100644
--- a/p2p/base/tcp_port.cc
+++ b/p2p/base/tcp_port.cc
@@ -74,6 +74,7 @@
 #include "absl/algorithm/container.h"
 #include "absl/memory/memory.h"
 #include "absl/strings/string_view.h"
+#include "api/task_queue/to_queued_task.h"
 #include "p2p/base/p2p_constants.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/ip_address.h"
@@ -81,7 +82,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/net_helper.h"
 #include "rtc_base/rate_tracker.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 
 namespace cricket {
diff --git a/p2p/base/tcp_port.h b/p2p/base/tcp_port.h
index 70fa584..8ef4963 100644
--- a/p2p/base/tcp_port.h
+++ b/p2p/base/tcp_port.h
@@ -17,11 +17,11 @@
 
 #include "absl/memory/memory.h"
 #include "absl/strings/string_view.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "p2p/base/connection.h"
 #include "p2p/base/port.h"
 #include "rtc_base/async_packet_socket.h"
 #include "rtc_base/containers/flat_map.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 
 namespace cricket {
 
diff --git a/p2p/base/turn_port.cc b/p2p/base/turn_port.cc
index 7117bee..6ab7bb1 100644
--- a/p2p/base/turn_port.cc
+++ b/p2p/base/turn_port.cc
@@ -19,6 +19,7 @@
 #include "absl/strings/match.h"
 #include "absl/strings/string_view.h"
 #include "absl/types/optional.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/stun.h"
 #include "p2p/base/connection.h"
 #include "p2p/base/p2p_constants.h"
@@ -29,7 +30,6 @@
 #include "rtc_base/net_helpers.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace cricket {
 
diff --git a/p2p/base/turn_port.h b/p2p/base/turn_port.h
index 7dd6d3f..b5abe1a 100644
--- a/p2p/base/turn_port.h
+++ b/p2p/base/turn_port.h
@@ -23,11 +23,11 @@
 #include "absl/memory/memory.h"
 #include "absl/strings/string_view.h"
 #include "api/async_dns_resolver.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "p2p/base/port.h"
 #include "p2p/client/basic_port_allocator.h"
 #include "rtc_base/async_packet_socket.h"
 #include "rtc_base/ssl_certificate.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 
 namespace webrtc {
 class TurnCustomizer;
diff --git a/p2p/base/turn_server.cc b/p2p/base/turn_server.cc
index 1e2d5df..95ff533 100644
--- a/p2p/base/turn_server.cc
+++ b/p2p/base/turn_server.cc
@@ -18,6 +18,7 @@
 #include "absl/memory/memory.h"
 #include "api/array_view.h"
 #include "api/packet_socket_factory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/stun.h"
 #include "p2p/base/async_stun_tcp_socket.h"
 #include "rtc_base/byte_buffer.h"
@@ -27,7 +28,6 @@
 #include "rtc_base/message_digest.h"
 #include "rtc_base/socket_adapters.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace cricket {
diff --git a/p2p/client/basic_port_allocator.cc b/p2p/client/basic_port_allocator.cc
index a8a0b62..9500ed02 100644
--- a/p2p/client/basic_port_allocator.cc
+++ b/p2p/client/basic_port_allocator.cc
@@ -21,6 +21,7 @@
 #include "absl/algorithm/container.h"
 #include "absl/memory/memory.h"
 #include "absl/strings/string_view.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/field_trial_based_config.h"
 #include "p2p/base/basic_packet_socket_factory.h"
 #include "p2p/base/port.h"
@@ -32,7 +33,6 @@
 #include "rtc_base/helpers.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/trace_event.h"
 #include "system_wrappers/include/metrics.h"
 
diff --git a/p2p/client/basic_port_allocator.h b/p2p/client/basic_port_allocator.h
index ae3a5360..9a423b9 100644
--- a/p2p/client/basic_port_allocator.h
+++ b/p2p/client/basic_port_allocator.h
@@ -17,6 +17,7 @@
 
 #include "absl/strings/string_view.h"
 #include "api/field_trials_view.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/turn_customizer.h"
 #include "p2p/base/port_allocator.h"
 #include "p2p/client/relay_port_factory_interface.h"
@@ -25,7 +26,6 @@
 #include "rtc_base/memory/always_valid_pointer.h"
 #include "rtc_base/network.h"
 #include "rtc_base/system/rtc_export.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
 
diff --git a/p2p/stunprober/stun_prober.cc b/p2p/stunprober/stun_prober.cc
index cbbf03b..55122b9 100644
--- a/p2p/stunprober/stun_prober.cc
+++ b/p2p/stunprober/stun_prober.cc
@@ -17,13 +17,13 @@
 #include <utility>
 
 #include "api/packet_socket_factory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/stun.h"
 #include "rtc_base/async_packet_socket.h"
 #include "rtc_base/async_resolver_interface.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/helpers.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/time_utils.h"
 
diff --git a/p2p/stunprober/stun_prober.h b/p2p/stunprober/stun_prober.h
index 1fe7deb..7d5094a 100644
--- a/p2p/stunprober/stun_prober.h
+++ b/p2p/stunprober/stun_prober.h
@@ -16,12 +16,12 @@
 #include <vector>
 
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "rtc_base/byte_buffer.h"
 #include "rtc_base/ip_address.h"
 #include "rtc_base/network.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/system/rtc_export.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread.h"
 
 namespace rtc {
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index 148ade0..8e6523a 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -77,6 +77,8 @@
     "../api:scoped_refptr",
     "../api:sequence_checker",
     "../api/crypto:options",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/units:timestamp",
     "../call:rtp_interfaces",
     "../call:rtp_receiver",
@@ -95,8 +97,6 @@
     "../rtc_base:threading",
     "../rtc_base/containers:flat_set",
     "../rtc_base/network:sent_packet",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/sigslot",
   ]
   absl_deps = [
@@ -788,6 +788,8 @@
     "../api/neteq:neteq_api",
     "../api/rtc_event_log",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/transport:bitrate_settings",
     "../api/transport:datagram_transport_interface",
     "../api/transport:enums",
@@ -829,8 +831,6 @@
     "../rtc_base/system:no_unique_address",
     "../rtc_base/system:rtc_export",
     "../rtc_base/system:unused",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/base64",
     "../rtc_base/third_party/sigslot",
     "../stats",
@@ -859,6 +859,7 @@
     "../api:priority",
     "../api:rtc_error",
     "../api:scoped_refptr",
+    "../api/task_queue:to_queued_task",
     "../api/transport:datagram_transport_interface",
     "../media:rtc_data_sctp_transport_internal",
     "../media:rtc_media_base",
@@ -871,7 +872,6 @@
     "../rtc_base:threading",
     "../rtc_base:threading",
     "../rtc_base/system:unused",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/sigslot:sigslot",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
@@ -907,6 +907,7 @@
     "../api:scoped_refptr",
     "../api:sequence_checker",
     "../api/neteq:neteq_api",
+    "../api/task_queue:to_queued_task",
     "../api/transport:field_trial_based_config",
     "../api/transport:sctp_transport_factory_interface",
     "../media:rtc_data_sctp_transport_factory",
@@ -920,7 +921,6 @@
     "../rtc_base:threading",
     "../rtc_base:timeutils",
     "../rtc_base/memory:always_valid_pointer",
-    "../rtc_base/task_utils:to_queued_task",
   ]
 }
 
@@ -939,6 +939,7 @@
     "../api:rtc_error",
     "../api:scoped_refptr",
     "../api:sequence_checker",
+    "../api/task_queue:to_queued_task",
     "../api/transport:datagram_transport_interface",
     "../media:rtc_media_base",
     "../rtc_base",
@@ -949,7 +950,6 @@
     "../rtc_base:macromagic",
     "../rtc_base:threading",
     "../rtc_base:weak_ptr",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/sigslot",
   ]
   absl_deps = [
@@ -1196,6 +1196,8 @@
     "../api/adaptation:resource_adaptation_api",
     "../api/crypto:options",
     "../api/rtc_event_log",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/transport:bitrate_settings",
     "../api/transport:datagram_transport_interface",
     "../api/transport:enums",
@@ -1218,8 +1220,6 @@
     "../rtc_base:stringutils",
     "../rtc_base:threading",
     "../rtc_base:weak_ptr",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/sigslot",
     "../system_wrappers:metrics",
   ]
@@ -1557,6 +1557,8 @@
     "../api:scoped_refptr",
     "../api:sequence_checker",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/video:video_bitrate_allocator_factory",
     "../media:rtc_media_base",
     "../rtc_base:checks",
@@ -1564,8 +1566,6 @@
     "../rtc_base:logging",
     "../rtc_base:macromagic",
     "../rtc_base:threading",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/sigslot",
   ]
   absl_deps = [
@@ -1686,6 +1686,8 @@
     "../api:scoped_refptr",
     "../api:sequence_checker",
     "../api/crypto:frame_decryptor_interface",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/transport/rtp:rtp_source",
     "../media:rtc_media_base",
     "../rtc_base",
@@ -1694,8 +1696,6 @@
     "../rtc_base:macromagic",
     "../rtc_base:threading",
     "../rtc_base/system:no_unique_address",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
   ]
   absl_deps = [
     "//third_party/abseil-cpp/absl/algorithm:container",
@@ -1926,6 +1926,8 @@
     "../api:libjingle_peerconnection_api",
     "../api:scoped_refptr",
     "../api:sequence_checker",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../rtc_base:checks",
     "../rtc_base:location",
     "../rtc_base:logging",
@@ -1933,8 +1935,6 @@
     "../rtc_base:refcount",
     "../rtc_base:rtc_base",
     "../rtc_base:threading",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/third_party/sigslot",
   ]
   absl_deps = [
@@ -2070,7 +2070,9 @@
       "../api:rtp_parameters",
       "../api:scoped_refptr",
       "../api:sequence_checker",
+      "../api/task_queue:pending_task_safety_flag",
       "../api/task_queue:task_queue",
+      "../api/task_queue:to_queued_task",
       "../api/transport:datagram_transport_interface",
       "../api/transport:enums",
       "../api/video:builtin_video_bitrate_allocator_factory",
@@ -2100,8 +2102,6 @@
       "../rtc_base:stringutils",
       "../rtc_base:threading",
       "../rtc_base/containers:flat_set",
-      "../rtc_base/task_utils:pending_task_safety_flag",
-      "../rtc_base/task_utils:to_queued_task",
       "../rtc_base/third_party/sigslot",
       "../system_wrappers:metrics",
       "../test:explicit_key_value_config",
@@ -2534,6 +2534,8 @@
       "../api/rtc_event_log:rtc_event_log_factory",
       "../api/task_queue",
       "../api/task_queue:default_task_queue_factory",
+      "../api/task_queue:pending_task_safety_flag",
+      "../api/task_queue:to_queued_task",
       "../api/transport:field_trial_based_config",
       "../api/transport/rtp:rtp_source",
       "../api/units:time_delta",
@@ -2573,9 +2575,7 @@
       "../rtc_base:threading",
       "../rtc_base:timeutils",
       "../rtc_base/synchronization:mutex",
-      "../rtc_base/task_utils:pending_task_safety_flag",
       "../rtc_base/task_utils:repeating_task",
-      "../rtc_base/task_utils:to_queued_task",
       "../rtc_base/third_party/base64",
       "../rtc_base/third_party/sigslot",
       "../system_wrappers:metrics",
diff --git a/pc/audio_rtp_receiver.cc b/pc/audio_rtp_receiver.cc
index 1dd6a41..be2d81e 100644
--- a/pc/audio_rtp_receiver.cc
+++ b/pc/audio_rtp_receiver.cc
@@ -17,11 +17,11 @@
 #include <vector>
 
 #include "api/sequence_checker.h"
+#include "api/task_queue/to_queued_task.h"
 #include "pc/audio_track.h"
 #include "pc/media_stream_track_proxy.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/location.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc {
 
diff --git a/pc/audio_rtp_receiver.h b/pc/audio_rtp_receiver.h
index c643be6..c683158 100644
--- a/pc/audio_rtp_receiver.h
+++ b/pc/audio_rtp_receiver.h
@@ -26,6 +26,7 @@
 #include "api/rtp_receiver_interface.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/transport/rtp/rtp_source.h"
 #include "media/base/media_channel.h"
 #include "pc/audio_track.h"
@@ -34,7 +35,6 @@
 #include "pc/remote_audio_source.h"
 #include "pc/rtp_receiver.h"
 #include "rtc_base/system/no_unique_address.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
 
diff --git a/pc/channel.cc b/pc/channel.cc
index 9b8e8f8..01cd432 100644
--- a/pc/channel.cc
+++ b/pc/channel.cc
@@ -19,6 +19,8 @@
 #include "absl/strings/string_view.h"
 #include "api/rtp_parameters.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/timestamp.h"
 #include "media/base/codec.h"
 #include "media/base/rid_description.h"
@@ -32,8 +34,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/network_route.h"
 #include "rtc_base/strings/string_format.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/trace_event.h"
 
 namespace cricket {
diff --git a/pc/channel.h b/pc/channel.h
index 8e121b2..5bf4823 100644
--- a/pc/channel.h
+++ b/pc/channel.h
@@ -28,6 +28,7 @@
 #include "api/rtp_transceiver_direction.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "call/rtp_demuxer.h"
 #include "call/rtp_packet_sink_interface.h"
 #include "media/base/media_channel.h"
@@ -43,7 +44,6 @@
 #include "rtc_base/network/sent_packet.h"
 #include "rtc_base/network_route.h"
 #include "rtc_base/socket.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
diff --git a/pc/channel_unittest.cc b/pc/channel_unittest.cc
index 201fd45..4e718d3 100644
--- a/pc/channel_unittest.cc
+++ b/pc/channel_unittest.cc
@@ -19,6 +19,8 @@
 #include "api/array_view.h"
 #include "api/audio_options.h"
 #include "api/rtp_parameters.h"
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "media/base/codec.h"
 #include "media/base/fake_media_engine.h"
 #include "media/base/fake_rtp.h"
@@ -42,8 +44,6 @@
 #include "rtc_base/location.h"
 #include "rtc_base/rtc_certificate.h"
 #include "rtc_base/ssl_identity.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/scoped_key_value_config.h"
diff --git a/pc/connection_context.cc b/pc/connection_context.cc
index 8314e08..29af2c8 100644
--- a/pc/connection_context.cc
+++ b/pc/connection_context.cc
@@ -13,13 +13,13 @@
 #include <type_traits>
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/field_trial_based_config.h"
 #include "media/base/media_engine.h"
 #include "media/sctp/sctp_transport_factory.h"
 #include "rtc_base/helpers.h"
 #include "rtc_base/internal/default_socket_server.h"
 #include "rtc_base/socket_server.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 
 namespace webrtc {
diff --git a/pc/data_channel_controller.cc b/pc/data_channel_controller.cc
index 8dde417..b1461cd 100644
--- a/pc/data_channel_controller.cc
+++ b/pc/data_channel_controller.cc
@@ -14,11 +14,11 @@
 
 #include "api/peer_connection_interface.h"
 #include "api/rtc_error.h"
+#include "api/task_queue/to_queued_task.h"
 #include "pc/peer_connection_internal.h"
 #include "pc/sctp_utils.h"
 #include "rtc_base/location.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc {
 
diff --git a/pc/dtmf_sender.cc b/pc/dtmf_sender.cc
index b28c005..40d9e38 100644
--- a/pc/dtmf_sender.cc
+++ b/pc/dtmf_sender.cc
@@ -13,9 +13,9 @@
 #include <ctype.h>
 #include <string.h>
 
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace webrtc {
diff --git a/pc/dtmf_sender.h b/pc/dtmf_sender.h
index ae213b3..06cd3a2 100644
--- a/pc/dtmf_sender.h
+++ b/pc/dtmf_sender.h
@@ -18,10 +18,10 @@
 #include "api/dtmf_sender_interface.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "pc/proxy.h"
 #include "rtc_base/location.h"
 #include "rtc_base/ref_count.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index a95e3aa..557f2e6 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -25,6 +25,7 @@
 #include "api/jsep_ice_candidate.h"
 #include "api/rtp_parameters.h"
 #include "api/rtp_transceiver_direction.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/uma_metrics.h"
 #include "api/video/video_codec_constants.h"
 #include "call/audio_state.h"
@@ -59,7 +60,6 @@
 #include "rtc_base/network_constants.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/string_encode.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/trace_event.h"
 #include "rtc_base/unique_id_generator.h"
 #include "system_wrappers/include/metrics.h"
diff --git a/pc/peer_connection.h b/pc/peer_connection.h
index 7dd6446..b7fdbf9 100644
--- a/pc/peer_connection.h
+++ b/pc/peer_connection.h
@@ -45,6 +45,7 @@
 #include "api/set_local_description_observer_interface.h"
 #include "api/set_remote_description_observer_interface.h"
 #include "api/stats/rtc_stats_collector_callback.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/transport/bitrate_settings.h"
 #include "api/transport/data_channel_transport_interface.h"
 #include "api/transport/enums.h"
@@ -78,7 +79,6 @@
 #include "rtc_base/rtc_certificate.h"
 #include "rtc_base/ssl_certificate.h"
 #include "rtc_base/ssl_stream_adapter.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
diff --git a/pc/rtp_transceiver.cc b/pc/rtp_transceiver.cc
index 5ca662c..387d344 100644
--- a/pc/rtp_transceiver.cc
+++ b/pc/rtp_transceiver.cc
@@ -21,6 +21,7 @@
 #include "api/peer_connection_interface.h"
 #include "api/rtp_parameters.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/to_queued_task.h"
 #include "media/base/codec.h"
 #include "media/base/media_constants.h"
 #include "media/base/media_engine.h"
@@ -30,7 +31,6 @@
 #include "rtc_base/checks.h"
 #include "rtc_base/location.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace webrtc {
diff --git a/pc/rtp_transceiver.h b/pc/rtp_transceiver.h
index 037f3bb..625d0a5 100644
--- a/pc/rtp_transceiver.h
+++ b/pc/rtp_transceiver.h
@@ -30,6 +30,7 @@
 #include "api/rtp_transceiver_direction.h"
 #include "api/rtp_transceiver_interface.h"
 #include "api/scoped_refptr.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/video/video_bitrate_allocator_factory.h"
 #include "media/base/media_channel.h"
@@ -42,7 +43,6 @@
 #include "pc/rtp_sender_proxy.h"
 #include "pc/rtp_transport_internal.h"
 #include "pc/session_description.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread_annotations.h"
 
diff --git a/pc/sctp_data_channel.cc b/pc/sctp_data_channel.cc
index a05713a..08882c9 100644
--- a/pc/sctp_data_channel.cc
+++ b/pc/sctp_data_channel.cc
@@ -15,6 +15,7 @@
 #include <string>
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "media/sctp/sctp_transport_internal.h"
 #include "pc/proxy.h"
 #include "pc/sctp_utils.h"
@@ -22,7 +23,6 @@
 #include "rtc_base/location.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/system/unused.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace webrtc {
diff --git a/pc/test/integration_test_helpers.h b/pc/test/integration_test_helpers.h
index 795966e..53df9fc 100644
--- a/pc/test/integration_test_helpers.h
+++ b/pc/test/integration_test_helpers.h
@@ -52,7 +52,9 @@
 #include "api/stats/rtc_stats_report.h"
 #include "api/stats/rtcstats_objects.h"
 #include "api/task_queue/default_task_queue_factory.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_factory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/field_trial_based_config.h"
 #include "api/uma_metrics.h"
 #include "api/video/video_rotation.h"
@@ -111,9 +113,7 @@
 #include "rtc_base/rtc_certificate_generator.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/ssl_stream_adapter.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/repeating_task.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/test_certificate_verifier.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index fb8187d..b58b4a9 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -608,8 +608,8 @@
   deps = [
     ":macromagic",
     "../api/task_queue",
+    "../api/task_queue:to_queued_task",
     "system:rtc_export",
-    "task_utils:to_queued_task",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/memory" ]
 }
@@ -946,11 +946,11 @@
     "../api:scoped_refptr",
     "../api:sequence_checker",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "synchronization:mutex",
     "system:no_unique_address",
     "system:rtc_export",
-    "task_utils:pending_task_safety_flag",
-    "task_utils:to_queued_task",
     "third_party/sigslot",
   ]
   if (is_android) {
@@ -1089,6 +1089,8 @@
     "../api:sequence_checker",
     "../api/numerics",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/transport:field_trial_based_config",
     "../system_wrappers:field_trial",
     "memory:always_valid_pointer",
@@ -1098,9 +1100,7 @@
     "system:inline",
     "system:no_unique_address",
     "system:rtc_export",
-    "task_utils:pending_task_safety_flag",
     "task_utils:repeating_task",
-    "task_utils:to_queued_task",
     "third_party/base64",
     "third_party/sigslot",
   ]
@@ -1376,13 +1376,13 @@
     ":stringutils",
     ":threading",
     ":timeutils",
+    "../api/task_queue:to_queued_task",
     "../api/units:time_delta",
     "../api/units:timestamp",
     "../test:scoped_key_value_config",
     "memory:always_valid_pointer",
     "memory:fifo_buffer",
     "synchronization:mutex",
-    "task_utils:to_queued_task",
     "third_party/sigslot",
   ]
   absl_deps = [
@@ -1407,7 +1407,7 @@
     ":rtc_task_queue",
     "../api/task_queue",
     "../api/task_queue:default_task_queue_factory",
-    "task_utils:to_queued_task",
+    "../api/task_queue:to_queued_task",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
 }
@@ -1594,6 +1594,7 @@
         "../api:make_ref_counted",
         "../api:scoped_refptr",
         "../api/numerics",
+        "../api/task_queue:to_queued_task",
         "../api/units:time_delta",
         "../system_wrappers",
         "../test:fileutils",
@@ -1603,7 +1604,6 @@
         "containers:unittests",
         "memory:unittests",
         "synchronization:mutex",
-        "task_utils:to_queued_task",
         "third_party/base64",
         "third_party/sigslot",
       ]
@@ -1735,7 +1735,9 @@
         "../api:array_view",
         "../api:make_ref_counted",
         "../api/task_queue",
+        "../api/task_queue:pending_task_safety_flag",
         "../api/task_queue:task_queue_test",
+        "../api/task_queue:to_queued_task",
         "../test:field_trial",
         "../test:fileutils",
         "../test:rtc_expect_death",
@@ -1744,8 +1746,6 @@
         "../test:test_support",
         "memory:fifo_buffer",
         "synchronization:mutex",
-        "task_utils:pending_task_safety_flag",
-        "task_utils:to_queued_task",
         "third_party/sigslot",
       ]
       if (enable_google_benchmarks) {
diff --git a/rtc_base/async_resolver.cc b/rtc_base/async_resolver.cc
index 07d77b4..2975d9f 100644
--- a/rtc_base/async_resolver.cc
+++ b/rtc_base/async_resolver.cc
@@ -34,11 +34,11 @@
 #endif  // defined(WEBRTC_POSIX) && !defined(__native_client__)
 
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/ip_address.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"  // for signal_with_thread...
 
 #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
diff --git a/rtc_base/async_resolver.h b/rtc_base/async_resolver.h
index 8257f46..b7125ba 100644
--- a/rtc_base/async_resolver.h
+++ b/rtc_base/async_resolver.h
@@ -20,13 +20,13 @@
 #include <vector>
 
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "rtc_base/async_resolver_interface.h"
 #include "rtc_base/event.h"
 #include "rtc_base/ip_address.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/system/no_unique_address.h"
 #include "rtc_base/system/rtc_export.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
 
diff --git a/rtc_base/fake_mdns_responder.h b/rtc_base/fake_mdns_responder.h
index 457f959..a7dcb96 100644
--- a/rtc_base/fake_mdns_responder.h
+++ b/rtc_base/fake_mdns_responder.h
@@ -16,10 +16,10 @@
 #include <string>
 
 #include "absl/strings/string_view.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/ip_address.h"
 #include "rtc_base/location.h"
 #include "rtc_base/mdns_responder_interface.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace webrtc {
diff --git a/rtc_base/memory/BUILD.gn b/rtc_base/memory/BUILD.gn
index 8965a2c..a42c2e1 100644
--- a/rtc_base/memory/BUILD.gn
+++ b/rtc_base/memory/BUILD.gn
@@ -35,9 +35,9 @@
   deps = [
     "..:rtc_base",
     "..:threading",
+    "../../api/task_queue:pending_task_safety_flag",
+    "../../api/task_queue:to_queued_task",
     "../synchronization:mutex",
-    "../task_utils:pending_task_safety_flag",
-    "../task_utils:to_queued_task",
   ]
 }
 
diff --git a/rtc_base/memory/fifo_buffer.h b/rtc_base/memory/fifo_buffer.h
index e3d29f4..2fa8213 100644
--- a/rtc_base/memory/fifo_buffer.h
+++ b/rtc_base/memory/fifo_buffer.h
@@ -13,10 +13,10 @@
 
 #include <memory>
 
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/stream.h"
 #include "rtc_base/synchronization/mutex.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace rtc {
 
diff --git a/rtc_base/network.cc b/rtc_base/network.cc
index 74eacb6..364a52d 100644
--- a/rtc_base/network.cc
+++ b/rtc_base/network.cc
@@ -30,6 +30,7 @@
 #include "absl/memory/memory.h"
 #include "absl/strings/match.h"
 #include "absl/strings/string_view.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/transport/field_trial_based_config.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
@@ -39,7 +40,6 @@
 #include "rtc_base/string_encode.h"
 #include "rtc_base/string_utils.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace rtc {
diff --git a/rtc_base/network.h b/rtc_base/network.h
index f814ef4..688cf46 100644
--- a/rtc_base/network.h
+++ b/rtc_base/network.h
@@ -24,6 +24,7 @@
 #include "api/array_view.h"
 #include "api/field_trials_view.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/transport/field_trial_based_config.h"
 #include "rtc_base/ip_address.h"
 #include "rtc_base/mdns_responder_interface.h"
@@ -32,7 +33,6 @@
 #include "rtc_base/network_monitor_factory.h"
 #include "rtc_base/socket_factory.h"
 #include "rtc_base/system/rtc_export.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread_annotations.h"
 
diff --git a/rtc_base/openssl_stream_adapter.cc b/rtc_base/openssl_stream_adapter.cc
index ee63c70..d6f6621 100644
--- a/rtc_base/openssl_stream_adapter.cc
+++ b/rtc_base/openssl_stream_adapter.cc
@@ -39,10 +39,10 @@
 #else
 #include "rtc_base/openssl_identity.h"
 #endif
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/openssl_utility.h"
 #include "rtc_base/ssl_certificate.h"
 #include "rtc_base/stream.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/time_utils.h"
 #include "system_wrappers/include/field_trial.h"
diff --git a/rtc_base/openssl_stream_adapter.h b/rtc_base/openssl_stream_adapter.h
index 288ed84..891f0e6 100644
--- a/rtc_base/openssl_stream_adapter.h
+++ b/rtc_base/openssl_stream_adapter.h
@@ -27,11 +27,11 @@
 #else
 #include "rtc_base/openssl_identity.h"
 #endif
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "rtc_base/ssl_identity.h"
 #include "rtc_base/ssl_stream_adapter.h"
 #include "rtc_base/stream.h"
 #include "rtc_base/system/rtc_export.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/repeating_task.h"
 
 namespace rtc {
diff --git a/rtc_base/ssl_stream_adapter_unittest.cc b/rtc_base/ssl_stream_adapter_unittest.cc
index 4786876..8b794a0 100644
--- a/rtc_base/ssl_stream_adapter_unittest.cc
+++ b/rtc_base/ssl_stream_adapter_unittest.cc
@@ -17,6 +17,8 @@
 
 #include "absl/memory/memory.h"
 #include "absl/strings/string_view.h"
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/buffer_queue.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/gunit.h"
@@ -28,8 +30,6 @@
 #include "rtc_base/ssl_adapter.h"
 #include "rtc_base/ssl_identity.h"
 #include "rtc_base/stream.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "test/field_trial.h"
 
 using ::testing::Combine;
diff --git a/rtc_base/task_queue.h b/rtc_base/task_queue.h
index fe381b9..882f751 100644
--- a/rtc_base/task_queue.h
+++ b/rtc_base/task_queue.h
@@ -20,8 +20,8 @@
 #include "api/task_queue/queued_task.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/task_queue/task_queue_factory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/system/rtc_export.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace rtc {
diff --git a/rtc_base/task_queue_for_test.h b/rtc_base/task_queue_for_test.h
index 97ca254..c1de874 100644
--- a/rtc_base/task_queue_for_test.h
+++ b/rtc_base/task_queue_for_test.h
@@ -15,11 +15,11 @@
 
 #include "absl/strings/string_view.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/event.h"
 #include "rtc_base/location.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace webrtc {
diff --git a/rtc_base/task_utils/BUILD.gn b/rtc_base/task_utils/BUILD.gn
index 893031d..04960b2 100644
--- a/rtc_base/task_utils/BUILD.gn
+++ b/rtc_base/task_utils/BUILD.gn
@@ -14,12 +14,12 @@
     "repeating_task.h",
   ]
   deps = [
-    ":pending_task_safety_flag",
-    ":to_queued_task",
     "..:logging",
     "..:timeutils",
     "../../api:sequence_checker",
     "../../api/task_queue",
+    "../../api/task_queue:pending_task_safety_flag",
+    "../../api/task_queue:to_queued_task",
     "../../api/units:time_delta",
     "../../api/units:timestamp",
     "../../system_wrappers:system_wrappers",
@@ -28,66 +28,29 @@
 }
 
 rtc_library("pending_task_safety_flag") {
-  sources = [
-    "pending_task_safety_flag.cc",
-    "pending_task_safety_flag.h",
-  ]
-  deps = [
-    "..:checks",
-    "../../api:refcountedbase",
-    "../../api:scoped_refptr",
-    "../../api:sequence_checker",
-    "../system:no_unique_address",
-  ]
+  sources = [ "pending_task_safety_flag.h" ]
+  deps = [ "../../api/task_queue:pending_task_safety_flag" ]
 }
 
 rtc_source_set("to_queued_task") {
   sources = [ "to_queued_task.h" ]
-  deps = [
-    ":pending_task_safety_flag",
-    "../../api/task_queue",
-  ]
+  deps = [ "../../api/task_queue:to_queued_task" ]
 }
 
 if (rtc_include_tests) {
-  rtc_library("pending_task_safety_flag_unittests") {
-    testonly = true
-    sources = [ "pending_task_safety_flag_unittest.cc" ]
-    deps = [
-      ":pending_task_safety_flag",
-      ":to_queued_task",
-      "..:logging",
-      "..:rtc_event",
-      "..:rtc_task_queue",
-      "..:task_queue_for_test",
-      "../../test:test_support",
-    ]
-  }
-
   rtc_library("repeating_task_unittests") {
     testonly = true
     sources = [ "repeating_task_unittest.cc" ]
     deps = [
       ":repeating_task",
-      ":to_queued_task",
       "..:rtc_event",
       "..:rtc_task_queue",
       "..:task_queue_for_test",
       "../../api/task_queue",
+      "../../api/task_queue:to_queued_task",
       "../../api/units:timestamp",
       "../../system_wrappers:system_wrappers",
       "../../test:test_support",
     ]
   }
-
-  rtc_library("to_queued_task_unittests") {
-    testonly = true
-    sources = [ "to_queued_task_unittest.cc" ]
-    deps = [
-      ":to_queued_task",
-      "../../api/task_queue",
-      "../../test:test_support",
-    ]
-    absl_deps = [ "//third_party/abseil-cpp/absl/memory" ]
-  }
 }
diff --git a/rtc_base/task_utils/pending_task_safety_flag.h b/rtc_base/task_utils/pending_task_safety_flag.h
index 604e058..8321276 100644
--- a/rtc_base/task_utils/pending_task_safety_flag.h
+++ b/rtc_base/task_utils/pending_task_safety_flag.h
@@ -11,148 +11,6 @@
 #ifndef RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_
 #define RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_
 
-#include "api/ref_counted_base.h"
-#include "api/scoped_refptr.h"
-#include "api/sequence_checker.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/system/no_unique_address.h"
-
-namespace webrtc {
-
-// The PendingTaskSafetyFlag and the ScopedTaskSafety are designed to address
-// the issue where you have a task to be executed later that has references,
-// but cannot guarantee that the referenced object is alive when the task is
-// executed.
-
-// This mechanism can be used with tasks that are created and destroyed
-// on a single thread / task queue, and with tasks posted to the same
-// thread/task queue, but tasks can be posted from any thread/TQ.
-
-// Typical usage:
-// When posting a task, post a copy (capture by-value in a lambda) of the flag
-// reference and before performing the work, check the `alive()` state. Abort if
-// alive() returns `false`:
-//
-// class ExampleClass {
-// ....
-//    my_task_queue_->PostTask(ToQueuedTask(
-//        [safety = pending_task_safety_flag_, this]() {
-//          // Now running on the main thread.
-//          if (!safety->alive())
-//            return;
-//          MyMethod();
-//        }));
-//   ....
-//   ~ExampleClass() {
-//     pending_task_safety_flag_->SetNotAlive();
-//   }
-//   scoped_refptr<PendingTaskSafetyFlag> pending_task_safety_flag_
-//        = PendingTaskSafetyFlag::Create();
-// }
-//
-// ToQueuedTask has an overload that makes this check automatic:
-//
-//    my_task_queue_->PostTask(ToQueuedTask(pending_task_safety_flag_,
-//        [this]() { MyMethod(); }));
-//
-class PendingTaskSafetyFlag final
-    : public rtc::RefCountedNonVirtual<PendingTaskSafetyFlag> {
- public:
-  static rtc::scoped_refptr<PendingTaskSafetyFlag> Create();
-
-  // Creates a flag, but with its SequenceChecker initially detached. Hence, it
-  // may be created on a different thread than the flag will be used on.
-  static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateDetached();
-
-  // Same as `CreateDetached()` except the initial state of the returned flag
-  // will be `!alive()`.
-  static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateDetachedInactive();
-
-  ~PendingTaskSafetyFlag() = default;
-
-  void SetNotAlive();
-  // The SetAlive method is intended to support Start/Stop/Restart usecases.
-  // When a class has called SetNotAlive on a flag used for posted tasks, and
-  // decides it wants to post new tasks and have them run, there are two
-  // reasonable ways to do that:
-  //
-  // (i) Use the below SetAlive method. One subtlety is that any task posted
-  //     prior to SetNotAlive, and still in the queue, is resurrected and will
-  //     run.
-  //
-  // (ii) Create a fresh flag, and just drop the reference to the old one. This
-  //      avoids the above problem, and ensures that tasks poster prior to
-  //      SetNotAlive stay cancelled. Instead, there's a potential data race on
-  //      the flag pointer itself. Some synchronization is required between the
-  //      thread overwriting the flag pointer, and the threads that want to post
-  //      tasks and therefore read that same pointer.
-  void SetAlive();
-  bool alive() const;
-
- protected:
-  explicit PendingTaskSafetyFlag(bool alive) : alive_(alive) {}
-
- private:
-  static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateInternal(bool alive);
-
-  bool alive_ = true;
-  RTC_NO_UNIQUE_ADDRESS SequenceChecker main_sequence_;
-};
-
-// The ScopedTaskSafety makes using PendingTaskSafetyFlag very simple.
-// It does automatic PTSF creation and signalling of destruction when the
-// ScopedTaskSafety instance goes out of scope.
-//
-// ToQueuedTask has an overload that takes a ScopedTaskSafety too, so there
-// is no need to explicitly call the "flag" method.
-//
-// Example usage:
-//
-//     my_task_queue->PostTask(ToQueuedTask(scoped_task_safety,
-//        [this]() {
-//             // task goes here
-//        }
-//
-// This should be used by the class that wants tasks dropped after destruction.
-// The requirement is that the instance has to be constructed and destructed on
-// the same thread as the potentially dropped tasks would be running on.
-class ScopedTaskSafety final {
- public:
-  ScopedTaskSafety() = default;
-  explicit ScopedTaskSafety(rtc::scoped_refptr<PendingTaskSafetyFlag> flag)
-      : flag_(std::move(flag)) {}
-  ~ScopedTaskSafety() { flag_->SetNotAlive(); }
-
-  // Returns a new reference to the safety flag.
-  rtc::scoped_refptr<PendingTaskSafetyFlag> flag() const { return flag_; }
-
-  // Marks the current flag as not-alive and attaches to a new one.
-  void reset(rtc::scoped_refptr<PendingTaskSafetyFlag> new_flag =
-                 PendingTaskSafetyFlag::Create()) {
-    flag_->SetNotAlive();
-    flag_ = std::move(new_flag);
-  }
-
- private:
-  rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ =
-      PendingTaskSafetyFlag::Create();
-};
-
-// Like ScopedTaskSafety, but allows construction on a different thread than
-// where the flag will be used.
-class ScopedTaskSafetyDetached final {
- public:
-  ScopedTaskSafetyDetached() = default;
-  ~ScopedTaskSafetyDetached() { flag_->SetNotAlive(); }
-
-  // Returns a new reference to the safety flag.
-  rtc::scoped_refptr<PendingTaskSafetyFlag> flag() const { return flag_; }
-
- private:
-  rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ =
-      PendingTaskSafetyFlag::CreateDetached();
-};
-
-}  // namespace webrtc
+#include "api/task_queue/pending_task_safety_flag.h"
 
 #endif  // RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_
diff --git a/rtc_base/task_utils/repeating_task.cc b/rtc_base/task_utils/repeating_task.cc
index c07df45..fc19b99 100644
--- a/rtc_base/task_utils/repeating_task.cc
+++ b/rtc_base/task_utils/repeating_task.cc
@@ -11,9 +11,9 @@
 #include "rtc_base/task_utils/repeating_task.h"
 
 #include "absl/memory/memory.h"
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 
 namespace webrtc {
diff --git a/rtc_base/task_utils/repeating_task.h b/rtc_base/task_utils/repeating_task.h
index 1086c16..4d2b7eb 100644
--- a/rtc_base/task_utils/repeating_task.h
+++ b/rtc_base/task_utils/repeating_task.h
@@ -15,11 +15,11 @@
 #include <type_traits>
 #include <utility>
 
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/queued_task.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/units/time_delta.h"
 #include "api/units/timestamp.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "system_wrappers/include/clock.h"
 
 namespace webrtc {
diff --git a/rtc_base/task_utils/repeating_task_unittest.cc b/rtc_base/task_utils/repeating_task_unittest.cc
index d6ab920..ca33446 100644
--- a/rtc_base/task_utils/repeating_task_unittest.cc
+++ b/rtc_base/task_utils/repeating_task_unittest.cc
@@ -15,10 +15,10 @@
 
 #include "api/task_queue/queued_task.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/timestamp.h"
 #include "rtc_base/event.h"
 #include "rtc_base/task_queue_for_test.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "system_wrappers/include/clock.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
diff --git a/rtc_base/task_utils/to_queued_task.h b/rtc_base/task_utils/to_queued_task.h
index b2e3aae..049fb01 100644
--- a/rtc_base/task_utils/to_queued_task.h
+++ b/rtc_base/task_utils/to_queued_task.h
@@ -11,101 +11,6 @@
 #ifndef RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_
 #define RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_
 
-#include <memory>
-#include <type_traits>
-#include <utility>
-
-#include "api/task_queue/queued_task.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-
-namespace webrtc {
-namespace webrtc_new_closure_impl {
-// Simple implementation of QueuedTask for use with lambdas.
-template <typename Closure>
-class ClosureTask : public QueuedTask {
- public:
-  explicit ClosureTask(Closure&& closure)
-      : closure_(std::forward<Closure>(closure)) {}
-
- private:
-  bool Run() override {
-    closure_();
-    return true;
-  }
-
-  typename std::decay<Closure>::type closure_;
-};
-
-template <typename Closure>
-class SafetyClosureTask : public QueuedTask {
- public:
-  explicit SafetyClosureTask(rtc::scoped_refptr<PendingTaskSafetyFlag> safety,
-                             Closure&& closure)
-      : closure_(std::forward<Closure>(closure)),
-        safety_flag_(std::move(safety)) {}
-
- private:
-  bool Run() override {
-    if (safety_flag_->alive())
-      closure_();
-    return true;
-  }
-
-  typename std::decay<Closure>::type closure_;
-  rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag_;
-};
-
-// Extends ClosureTask to also allow specifying cleanup code.
-// This is useful when using lambdas if guaranteeing cleanup, even if a task
-// was dropped (queue is too full), is required.
-template <typename Closure, typename Cleanup>
-class ClosureTaskWithCleanup : public ClosureTask<Closure> {
- public:
-  ClosureTaskWithCleanup(Closure&& closure, Cleanup&& cleanup)
-      : ClosureTask<Closure>(std::forward<Closure>(closure)),
-        cleanup_(std::forward<Cleanup>(cleanup)) {}
-  ~ClosureTaskWithCleanup() override { cleanup_(); }
-
- private:
-  typename std::decay<Cleanup>::type cleanup_;
-};
-}  // namespace webrtc_new_closure_impl
-
-// Convenience function to construct closures that can be passed directly
-// to methods that support std::unique_ptr<QueuedTask> but not template
-// based parameters.
-template <typename Closure>
-std::unique_ptr<QueuedTask> ToQueuedTask(Closure&& closure) {
-  return std::make_unique<webrtc_new_closure_impl::ClosureTask<Closure>>(
-      std::forward<Closure>(closure));
-}
-
-template <typename Closure>
-std::unique_ptr<QueuedTask> ToQueuedTask(
-    rtc::scoped_refptr<PendingTaskSafetyFlag> safety,
-    Closure&& closure) {
-  return std::make_unique<webrtc_new_closure_impl::SafetyClosureTask<Closure>>(
-      std::move(safety), std::forward<Closure>(closure));
-}
-
-template <typename Closure>
-std::unique_ptr<QueuedTask> ToQueuedTask(const ScopedTaskSafety& safety,
-                                         Closure&& closure) {
-  return ToQueuedTask(safety.flag(), std::forward<Closure>(closure));
-}
-
-template <typename Closure,
-          typename Cleanup,
-          typename std::enable_if<!std::is_same<
-              typename std::remove_const<
-                  typename std::remove_reference<Closure>::type>::type,
-              ScopedTaskSafety>::value>::type* = nullptr>
-std::unique_ptr<QueuedTask> ToQueuedTask(Closure&& closure, Cleanup&& cleanup) {
-  return std::make_unique<
-      webrtc_new_closure_impl::ClosureTaskWithCleanup<Closure, Cleanup>>(
-      std::forward<Closure>(closure), std::forward<Cleanup>(cleanup));
-}
-
-}  // namespace webrtc
+#include "api/task_queue/to_queued_task.h"
 
 #endif  // RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_
diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc
index f968065..6693979 100644
--- a/rtc_base/thread.cc
+++ b/rtc_base/thread.cc
@@ -32,6 +32,7 @@
 
 #include "absl/algorithm/container.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/atomic_ops.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/deprecated/recursive_critical_section.h"
@@ -39,7 +40,6 @@
 #include "rtc_base/internal/default_socket_server.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/null_socket_server.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
 
diff --git a/rtc_base/thread.h b/rtc_base/thread.h
index 052a71b..885d7b8 100644
--- a/rtc_base/thread.h
+++ b/rtc_base/thread.h
@@ -31,6 +31,7 @@
 #include "api/function_view.h"
 #include "api/task_queue/queued_task.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/deprecated/recursive_critical_section.h"
 #include "rtc_base/location.h"
@@ -38,7 +39,6 @@
 #include "rtc_base/platform_thread_types.h"
 #include "rtc_base/socket_server.h"
 #include "rtc_base/system/rtc_export.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/thread_message.h"
 
diff --git a/rtc_base/thread_unittest.cc b/rtc_base/thread_unittest.cc
index 5497f8d..5490e61 100644
--- a/rtc_base/thread_unittest.cc
+++ b/rtc_base/thread_unittest.cc
@@ -14,6 +14,7 @@
 
 #include "api/task_queue/task_queue_factory.h"
 #include "api/task_queue/task_queue_test.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/async_invoker.h"
 #include "rtc_base/async_udp_socket.h"
 #include "rtc_base/atomic_ops.h"
@@ -25,7 +26,6 @@
 #include "rtc_base/physical_socket_server.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/synchronization/mutex.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "test/testsupport/rtc_expect_death.h"
 
diff --git a/rtc_base/time_utils_unittest.cc b/rtc_base/time_utils_unittest.cc
index 2663714..ced6e35 100644
--- a/rtc_base/time_utils_unittest.cc
+++ b/rtc_base/time_utils_unittest.cc
@@ -12,13 +12,13 @@
 
 #include <memory>
 
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/time_delta.h"
 #include "rtc_base/event.h"
 #include "rtc_base/fake_clock.h"
 #include "rtc_base/helpers.h"
 #include "rtc_base/location.h"
 #include "rtc_base/message_handler.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 #include "test/gtest.h"
 
diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn
index 1530cfb..e9a8dbf 100644
--- a/sdk/BUILD.gn
+++ b/sdk/BUILD.gn
@@ -1573,13 +1573,13 @@
           ":network_monitor_observer",
           "../api:field_trials_view",
           "../api:sequence_checker",
+          "../api/task_queue:pending_task_safety_flag",
+          "../api/task_queue:to_queued_task",
           "../rtc_base",
           "../rtc_base:logging",
           "../rtc_base:macromagic",
           "../rtc_base:stringutils",
           "../rtc_base:threading",
-          "../rtc_base/task_utils:pending_task_safety_flag",
-          "../rtc_base/task_utils:to_queued_task",
         ]
 
         absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn
index c1ceea4..812984a 100644
--- a/sdk/android/BUILD.gn
+++ b/sdk/android/BUILD.gn
@@ -582,6 +582,8 @@
       "../../api:libjingle_peerconnection_api",
       "../../api:scoped_refptr",
       "../../api:sequence_checker",
+      "../../api/task_queue:pending_task_safety_flag",
+      "../../api/task_queue:to_queued_task",
       "../../rtc_base",
       "../../rtc_base:checks",
       "../../rtc_base:ip_address",
@@ -590,8 +592,6 @@
       "../../rtc_base:refcount",
       "../../rtc_base:stringutils",
       "../../rtc_base:threading",
-      "../../rtc_base/task_utils:pending_task_safety_flag",
-      "../../rtc_base/task_utils:to_queued_task",
       "../../system_wrappers:field_trial",
       "../../system_wrappers:metrics",
     ]
@@ -675,6 +675,7 @@
       "../../api:media_stream_interface",
       "../../api:sequence_checker",
       "../../api/task_queue",
+      "../../api/task_queue:to_queued_task",
       "../../api/video:encoded_image",
       "../../api/video:render_resolution",
       "../../api/video:video_frame",
@@ -699,7 +700,6 @@
       "../../rtc_base:timestamp_aligner",
       "../../rtc_base:timeutils",
       "../../rtc_base/synchronization:mutex",
-      "../../rtc_base/task_utils:to_queued_task",
       "//third_party/libyuv",
     ]
     absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
diff --git a/sdk/android/src/jni/android_network_monitor.cc b/sdk/android/src/jni/android_network_monitor.cc
index c17e1ee..8b2e6c5 100644
--- a/sdk/android/src/jni/android_network_monitor.cc
+++ b/sdk/android/src/jni/android_network_monitor.cc
@@ -19,11 +19,11 @@
 #endif
 
 #include "api/sequence_checker.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/ip_address.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "sdk/android/generated_base_jni/NetworkChangeDetector_jni.h"
 #include "sdk/android/generated_base_jni/NetworkMonitor_jni.h"
 #include "sdk/android/native_api/jni/java_types.h"
diff --git a/sdk/android/src/jni/android_network_monitor.h b/sdk/android/src/jni/android_network_monitor.h
index dfeb2be..d0aad5e 100644
--- a/sdk/android/src/jni/android_network_monitor.h
+++ b/sdk/android/src/jni/android_network_monitor.h
@@ -20,10 +20,10 @@
 #include "absl/strings/string_view.h"
 #include "absl/types/optional.h"
 #include "api/field_trials_view.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "rtc_base/network_monitor.h"
 #include "rtc_base/network_monitor_factory.h"
 #include "rtc_base/string_utils.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
 #include "sdk/android/src/jni/jni_helpers.h"
diff --git a/sdk/android/src/jni/video_encoder_wrapper.cc b/sdk/android/src/jni/video_encoder_wrapper.cc
index 1a841e6..bd40d77 100644
--- a/sdk/android/src/jni/video_encoder_wrapper.cc
+++ b/sdk/android/src/jni/video_encoder_wrapper.cc
@@ -12,6 +12,7 @@
 
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "common_video/h264/h264_common.h"
 #include "modules/video_coding/include/video_codec_interface.h"
 #include "modules/video_coding/include/video_error_codes.h"
@@ -19,7 +20,6 @@
 #include "modules/video_coding/utility/vp8_header_parser.h"
 #include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "sdk/android/generated_video_jni/VideoEncoderWrapper_jni.h"
 #include "sdk/android/generated_video_jni/VideoEncoder_jni.h"
diff --git a/sdk/objc/native/src/objc_network_monitor.h b/sdk/objc/native/src/objc_network_monitor.h
index a47a0a9..709e9df 100644
--- a/sdk/objc/native/src/objc_network_monitor.h
+++ b/sdk/objc/native/src/objc_network_monitor.h
@@ -16,10 +16,10 @@
 #include "absl/strings/string_view.h"
 #include "api/field_trials_view.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "rtc_base/network_monitor.h"
 #include "rtc_base/network_monitor_factory.h"
 #include "rtc_base/string_utils.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
 #include "sdk/objc/components/network/RTCNetworkMonitor+Private.h"
diff --git a/sdk/objc/native/src/objc_network_monitor.mm b/sdk/objc/native/src/objc_network_monitor.mm
index 28fd72b..3afa2e6 100644
--- a/sdk/objc/native/src/objc_network_monitor.mm
+++ b/sdk/objc/native/src/objc_network_monitor.mm
@@ -11,7 +11,7 @@
 #include "sdk/objc/native/src/objc_network_monitor.h"
 #include "absl/strings/string_view.h"
 
-#include "rtc_base/task_utils/to_queued_task.h"
+#include "api/task_queue/to_queued_task.h"
 
 #include <algorithm>
 
diff --git a/test/BUILD.gn b/test/BUILD.gn
index da4a31a..4e41076 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -555,6 +555,7 @@
       "../api:scoped_refptr",
       "../api:simulcast_test_fixture_api",
       "../api/task_queue:task_queue_test",
+      "../api/task_queue:to_queued_task",
       "../api/test/video:function_video_factory",
       "../api/video:encoded_image",
       "../api/video:video_frame",
@@ -574,7 +575,6 @@
       "../rtc_base:rtc_task_queue",
       "../rtc_base/synchronization:mutex",
       "../rtc_base/system:file_wrapper",
-      "../rtc_base/task_utils:to_queued_task",
       "pc/e2e:e2e_unittests",
       "peer_scenario/tests",
       "scenario:scenario_unittests",
@@ -870,9 +870,9 @@
     "run_loop.h",
   ]
   deps = [
+    "../api/task_queue:to_queued_task",
     "../rtc_base:threading",
     "../rtc_base:timeutils",
-    "../rtc_base/task_utils:to_queued_task",
   ]
 }
 
@@ -914,6 +914,7 @@
     "../api/rtc_event_log",
     "../api/task_queue",
     "../api/task_queue:default_task_queue_factory",
+    "../api/task_queue:to_queued_task",
     "../api/test/video:function_video_factory",
     "../api/transport:field_trial_based_config",
     "../api/video:builtin_video_bitrate_allocator_factory",
@@ -938,7 +939,6 @@
     "../rtc_base:task_queue_for_test",
     "../rtc_base:threading",
     "../rtc_base:timeutils",
-    "../rtc_base/task_utils:to_queued_task",
     "../system_wrappers",
     "../system_wrappers:field_trial",
   ]
diff --git a/test/network/BUILD.gn b/test/network/BUILD.gn
index 62ad48a..fc4f4bc 100644
--- a/test/network/BUILD.gn
+++ b/test/network/BUILD.gn
@@ -47,6 +47,8 @@
     "../../api:simulated_network_api",
     "../../api:time_controller",
     "../../api/numerics",
+    "../../api/task_queue:pending_task_safety_flag",
+    "../../api/task_queue:to_queued_task",
     "../../api/test/network_emulation",
     "../../api/transport:stun_types",
     "../../api/units:data_rate",
@@ -75,9 +77,7 @@
     "../../rtc_base:threading",
     "../../rtc_base/memory:always_valid_pointer",
     "../../rtc_base/synchronization:mutex",
-    "../../rtc_base/task_utils:pending_task_safety_flag",
     "../../rtc_base/task_utils:repeating_task",
-    "../../rtc_base/task_utils:to_queued_task",
     "../../system_wrappers",
     "../../test:scoped_key_value_config",
     "../scenario:column_printer",
diff --git a/test/network/fake_network_socket_server.cc b/test/network/fake_network_socket_server.cc
index 28c0473..de0f687 100644
--- a/test/network/fake_network_socket_server.cc
+++ b/test/network/fake_network_socket_server.cc
@@ -17,9 +17,9 @@
 
 #include "absl/algorithm/container.h"
 #include "api/scoped_refptr.h"
+#include "api/task_queue/pending_task_safety_flag.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace webrtc {
diff --git a/test/run_loop.cc b/test/run_loop.cc
index ebb0d20..adce7ff 100644
--- a/test/run_loop.cc
+++ b/test/run_loop.cc
@@ -9,7 +9,7 @@
  */
 #include "test/run_loop.h"
 
-#include "rtc_base/task_utils/to_queued_task.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 
 namespace webrtc {
diff --git a/test/run_loop.h b/test/run_loop.h
index 9a91d44..529a545 100644
--- a/test/run_loop.h
+++ b/test/run_loop.h
@@ -10,7 +10,7 @@
 #ifndef TEST_RUN_LOOP_H_
 #define TEST_RUN_LOOP_H_
 
-#include "rtc_base/task_utils/to_queued_task.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace webrtc {
diff --git a/test/run_loop_unittest.cc b/test/run_loop_unittest.cc
index 160aba0..f094ad1 100644
--- a/test/run_loop_unittest.cc
+++ b/test/run_loop_unittest.cc
@@ -10,8 +10,8 @@
 
 #include "test/run_loop.h"
 
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "test/gtest.h"
 
 namespace webrtc {
diff --git a/test/time_controller/BUILD.gn b/test/time_controller/BUILD.gn
index 4fa0ab6..bdb271d 100644
--- a/test/time_controller/BUILD.gn
+++ b/test/time_controller/BUILD.gn
@@ -30,6 +30,7 @@
     "../../api:time_controller",
     "../../api/task_queue",
     "../../api/task_queue:default_task_queue_factory",
+    "../../api/task_queue:to_queued_task",
     "../../api/units:time_delta",
     "../../api/units:timestamp",
     "../../modules:module_api",
@@ -42,7 +43,6 @@
     "../../rtc_base:rtc_event",
     "../../rtc_base/synchronization:mutex",
     "../../rtc_base/synchronization:yield_policy",
-    "../../rtc_base/task_utils:to_queued_task",
     "../../system_wrappers",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
@@ -60,6 +60,7 @@
       ":time_controller",
       "../:test_support",
       "../../api:time_controller",
+      "../../api/task_queue:to_queued_task",
       "../../api/units:time_delta",
       "../../rtc_base",
       "../../rtc_base:location",
@@ -69,7 +70,6 @@
       "../../rtc_base:threading",
       "../../rtc_base/synchronization:mutex",
       "../../rtc_base/task_utils:repeating_task",
-      "../../rtc_base/task_utils:to_queued_task",
     ]
   }
 }
diff --git a/test/time_controller/simulated_thread.cc b/test/time_controller/simulated_thread.cc
index 4de36c5..fa2b8ee 100644
--- a/test/time_controller/simulated_thread.cc
+++ b/test/time_controller/simulated_thread.cc
@@ -12,7 +12,7 @@
 #include <algorithm>
 #include <utility>
 
-#include "rtc_base/task_utils/to_queued_task.h"
+#include "api/task_queue/to_queued_task.h"
 
 namespace webrtc {
 namespace {
diff --git a/test/time_controller/time_controller_conformance_test.cc b/test/time_controller/time_controller_conformance_test.cc
index ec3da19..308edd1 100644
--- a/test/time_controller/time_controller_conformance_test.cc
+++ b/test/time_controller/time_controller_conformance_test.cc
@@ -11,12 +11,12 @@
 #include <memory>
 #include <vector>
 
+#include "api/task_queue/to_queued_task.h"
 #include "api/test/time_controller.h"
 #include "api/units/time_delta.h"
 #include "rtc_base/event.h"
 #include "rtc_base/location.h"
 #include "rtc_base/synchronization/mutex.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/thread_annotations.h"
 #include "test/gmock.h"
diff --git a/video/BUILD.gn b/video/BUILD.gn
index 35e12e4..65f02d8 100644
--- a/video/BUILD.gn
+++ b/video/BUILD.gn
@@ -73,6 +73,8 @@
     "../api/crypto:options",
     "../api/rtc_event_log",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/units:frequency",
     "../api/units:time_delta",
     "../api/units:timestamp",
@@ -142,9 +144,7 @@
     "../rtc_base/experiments:rtt_mult_experiment",
     "../rtc_base/synchronization:mutex",
     "../rtc_base/system:no_unique_address",
-    "../rtc_base/task_utils:pending_task_safety_flag",
     "../rtc_base/task_utils:repeating_task",
-    "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/time:timestamp_extrapolator",
     "../system_wrappers",
     "../system_wrappers:field_trial",
@@ -228,6 +228,8 @@
     "../api:field_trials_view",
     "../api:sequence_checker",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/units:time_delta",
     "../api/units:timestamp",
     "../api/video:video_frame",
@@ -239,9 +241,7 @@
     "../rtc_base:timeutils",
     "../rtc_base/synchronization:mutex",
     "../rtc_base/system:no_unique_address",
-    "../rtc_base/task_utils:pending_task_safety_flag",
     "../rtc_base/task_utils:repeating_task",
-    "../rtc_base/task_utils:to_queued_task",
     "../system_wrappers",
     "../system_wrappers:field_trial",
     "../system_wrappers:metrics",
@@ -308,10 +308,10 @@
     ":frame_decode_timing",
     "../api:sequence_checker",
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
+    "../api/task_queue:to_queued_task",
     "../api/units:timestamp",
     "../rtc_base:checks",
-    "../rtc_base/task_utils:pending_task_safety_flag",
-    "../rtc_base/task_utils:to_queued_task",
     "../system_wrappers",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
@@ -324,10 +324,10 @@
   ]
   deps = [
     "../api/task_queue",
+    "../api/task_queue:pending_task_safety_flag",
     "../api/units:time_delta",
     "../modules/video_coding/timing:timing_module",
     "../rtc_base:logging",
-    "../rtc_base/task_utils:pending_task_safety_flag",
     "../system_wrappers",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
@@ -392,7 +392,9 @@
     "../api:rtp_parameters",
     "../api:sequence_checker",
     "../api/adaptation:resource_adaptation_api",
+    "../api/task_queue:pending_task_safety_flag",
     "../api/task_queue:task_queue",
+    "../api/task_queue:to_queued_task",
     "../api/units:data_rate",
     "../api/video:encoded_image",
     "../api/video:render_resolution",
@@ -440,9 +442,7 @@
     "../rtc_base/experiments:rate_control_settings",
     "../rtc_base/synchronization:mutex",
     "../rtc_base/system:no_unique_address",
-    "../rtc_base/task_utils:pending_task_safety_flag",
     "../rtc_base/task_utils:repeating_task",
-    "../rtc_base/task_utils:to_queued_task",
     "../system_wrappers",
     "../system_wrappers:field_trial",
     "../system_wrappers:metrics",
@@ -809,6 +809,7 @@
       "../api/rtc_event_log",
       "../api/task_queue",
       "../api/task_queue:default_task_queue_factory",
+      "../api/task_queue:to_queued_task",
       "../api/test/video:function_video_factory",
       "../api/units:data_rate",
       "../api/units:frequency",
@@ -890,7 +891,6 @@
       "../rtc_base/experiments:alr_experiment",
       "../rtc_base/experiments:encoder_info_settings",
       "../rtc_base/synchronization:mutex",
-      "../rtc_base/task_utils:to_queued_task",
       "../system_wrappers",
       "../system_wrappers:field_trial",
       "../system_wrappers:metrics",
diff --git a/video/adaptation/BUILD.gn b/video/adaptation/BUILD.gn
index 48be51d..82c18e6 100644
--- a/video/adaptation/BUILD.gn
+++ b/video/adaptation/BUILD.gn
@@ -39,6 +39,7 @@
     "../../api:sequence_checker",
     "../../api/adaptation:resource_adaptation_api",
     "../../api/task_queue:task_queue",
+    "../../api/task_queue:to_queued_task",
     "../../api/units:data_rate",
     "../../api/units:time_delta",
     "../../api/video:video_adaptation",
@@ -66,7 +67,6 @@
     "../../rtc_base/synchronization:mutex",
     "../../rtc_base/system:no_unique_address",
     "../../rtc_base/task_utils:repeating_task",
-    "../../rtc_base/task_utils:to_queued_task",
     "../../system_wrappers:field_trial",
     "../../system_wrappers:system_wrappers",
   ]
@@ -93,6 +93,7 @@
       "../../api:field_trials_view",
       "../../api:scoped_refptr",
       "../../api/task_queue:task_queue",
+      "../../api/task_queue:to_queued_task",
       "../../api/units:time_delta",
       "../../api/units:timestamp",
       "../../api/video:encoded_image",
@@ -112,7 +113,6 @@
       "../../rtc_base:rtc_task_queue",
       "../../rtc_base:task_queue_for_test",
       "../../rtc_base:threading",
-      "../../rtc_base/task_utils:to_queued_task",
       "../../test:field_trial",
       "../../test:rtc_expect_death",
       "../../test:scoped_key_value_config",
diff --git a/video/adaptation/balanced_constraint.cc b/video/adaptation/balanced_constraint.cc
index 623b696..481259c 100644
--- a/video/adaptation/balanced_constraint.cc
+++ b/video/adaptation/balanced_constraint.cc
@@ -14,7 +14,7 @@
 #include <utility>
 
 #include "api/sequence_checker.h"
-#include "rtc_base/task_utils/to_queued_task.h"
+#include "api/task_queue/to_queued_task.h"
 
 namespace webrtc {
 
diff --git a/video/adaptation/bandwidth_quality_scaler_resource.cc b/video/adaptation/bandwidth_quality_scaler_resource.cc
index 6f75e59..b8d664c 100644
--- a/video/adaptation/bandwidth_quality_scaler_resource.cc
+++ b/video/adaptation/bandwidth_quality_scaler_resource.cc
@@ -12,10 +12,10 @@
 
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/experiments/balanced_degradation_settings.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 
 namespace webrtc {
diff --git a/video/adaptation/pixel_limit_resource_unittest.cc b/video/adaptation/pixel_limit_resource_unittest.cc
index 7b633b3..28ee6af 100644
--- a/video/adaptation/pixel_limit_resource_unittest.cc
+++ b/video/adaptation/pixel_limit_resource_unittest.cc
@@ -13,12 +13,12 @@
 #include <memory>
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/timestamp.h"
 #include "call/adaptation/test/fake_video_stream_input_state_provider.h"
 #include "call/adaptation/test/mock_resource_listener.h"
 #include "call/adaptation/video_stream_adapter.h"
 #include "rtc_base/task_queue_for_test.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/time_controller/simulated_time_controller.h"
diff --git a/video/adaptation/quality_scaler_resource.cc b/video/adaptation/quality_scaler_resource.cc
index 6f6b252..853c098 100644
--- a/video/adaptation/quality_scaler_resource.cc
+++ b/video/adaptation/quality_scaler_resource.cc
@@ -12,9 +12,9 @@
 
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/experiments/balanced_degradation_settings.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 
 namespace webrtc {
diff --git a/video/call_stats2.cc b/video/call_stats2.cc
index e2d7b3e..f212397 100644
--- a/video/call_stats2.cc
+++ b/video/call_stats2.cc
@@ -15,9 +15,9 @@
 #include <utility>
 
 #include "absl/algorithm/container.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/location.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "system_wrappers/include/metrics.h"
 
 namespace webrtc {
diff --git a/video/call_stats2.h b/video/call_stats2.h
index b626e48..760f7a0 100644
--- a/video/call_stats2.h
+++ b/video/call_stats2.h
@@ -14,11 +14,11 @@
 #include <list>
 #include <memory>
 
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/units/timestamp.h"
 #include "modules/include/module_common_types.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/repeating_task.h"
 #include "system_wrappers/include/clock.h"
 
diff --git a/video/call_stats2_unittest.cc b/video/call_stats2_unittest.cc
index 4f371e8..13880d2 100644
--- a/video/call_stats2_unittest.cc
+++ b/video/call_stats2_unittest.cc
@@ -13,8 +13,8 @@
 #include <memory>
 
 #include "api/task_queue/default_task_queue_factory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 #include "system_wrappers/include/metrics.h"
 #include "test/gmock.h"
diff --git a/video/end_to_end_tests/bandwidth_tests.cc b/video/end_to_end_tests/bandwidth_tests.cc
index 6a38271..9d14e6a 100644
--- a/video/end_to_end_tests/bandwidth_tests.cc
+++ b/video/end_to_end_tests/bandwidth_tests.cc
@@ -11,6 +11,7 @@
 #include <memory>
 
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/test/simulated_network.h"
 #include "api/video/builtin_video_bitrate_allocator_factory.h"
 #include "api/video/video_bitrate_allocation.h"
@@ -20,7 +21,6 @@
 #include "rtc_base/rate_limiter.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/task_queue_for_test.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "system_wrappers/include/sleep.h"
 #include "test/call_test.h"
 #include "test/fake_encoder.h"
diff --git a/video/end_to_end_tests/network_state_tests.cc b/video/end_to_end_tests/network_state_tests.cc
index 5b5015b..c52f01d 100644
--- a/video/end_to_end_tests/network_state_tests.cc
+++ b/video/end_to_end_tests/network_state_tests.cc
@@ -14,6 +14,7 @@
 #include "api/task_queue/default_task_queue_factory.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/task_queue/task_queue_factory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/test/simulated_network.h"
 #include "api/video_codecs/video_encoder.h"
 #include "call/fake_network_pipe.h"
@@ -22,7 +23,6 @@
 #include "rtc_base/location.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/task_queue_for_test.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "system_wrappers/include/sleep.h"
 #include "test/call_test.h"
 #include "test/fake_encoder.h"
diff --git a/video/frame_cadence_adapter.cc b/video/frame_cadence_adapter.cc
index 94fd318..486c26b 100644
--- a/video/frame_cadence_adapter.cc
+++ b/video/frame_cadence_adapter.cc
@@ -19,7 +19,9 @@
 #include "absl/algorithm/container.h"
 #include "absl/base/attributes.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/time_delta.h"
 #include "api/units/timestamp.h"
 #include "api/video/video_frame.h"
@@ -29,9 +31,7 @@
 #include "rtc_base/rate_statistics.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/system/no_unique_address.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/repeating_task.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/time_utils.h"
 #include "system_wrappers/include/clock.h"
diff --git a/video/frame_cadence_adapter_unittest.cc b/video/frame_cadence_adapter_unittest.cc
index 3a2767a..6d57f30 100644
--- a/video/frame_cadence_adapter_unittest.cc
+++ b/video/frame_cadence_adapter_unittest.cc
@@ -16,12 +16,12 @@
 #include "api/task_queue/default_task_queue_factory.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/task_queue/task_queue_factory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/timestamp.h"
 #include "api/video/nv12_buffer.h"
 #include "api/video/video_frame.h"
 #include "rtc_base/event.h"
 #include "rtc_base/rate_statistics.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "system_wrappers/include/metrics.h"
 #include "system_wrappers/include/ntp_time.h"
diff --git a/video/frame_decode_timing.h b/video/frame_decode_timing.h
index 59247dc..6bde470 100644
--- a/video/frame_decode_timing.h
+++ b/video/frame_decode_timing.h
@@ -15,9 +15,9 @@
 
 #include <functional>
 
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
 #include "modules/video_coding/timing/timing.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "system_wrappers/include/clock.h"
 
 namespace webrtc {
diff --git a/video/receive_statistics_proxy2.cc b/video/receive_statistics_proxy2.cc
index e2c05a9..0f816bb 100644
--- a/video/receive_statistics_proxy2.cc
+++ b/video/receive_statistics_proxy2.cc
@@ -14,11 +14,11 @@
 #include <cmath>
 #include <utility>
 
+#include "api/task_queue/to_queued_task.h"
 #include "modules/video_coding/include/video_codec_interface.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/time_utils.h"
 #include "system_wrappers/include/clock.h"
diff --git a/video/receive_statistics_proxy2.h b/video/receive_statistics_proxy2.h
index a2e9366..7470768 100644
--- a/video/receive_statistics_proxy2.h
+++ b/video/receive_statistics_proxy2.h
@@ -19,6 +19,7 @@
 #include "absl/types/optional.h"
 #include "api/field_trials_view.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/units/timestamp.h"
 #include "call/video_receive_stream.h"
@@ -30,7 +31,6 @@
 #include "rtc_base/rate_statistics.h"
 #include "rtc_base/rate_tracker.h"
 #include "rtc_base/system/no_unique_address.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread_annotations.h"
 #include "video/quality_threshold.h"
 #include "video/stats_counter.h"
diff --git a/video/receive_statistics_proxy2_unittest.cc b/video/receive_statistics_proxy2_unittest.cc
index ca135df..f38f9cf 100644
--- a/video/receive_statistics_proxy2_unittest.cc
+++ b/video/receive_statistics_proxy2_unittest.cc
@@ -18,11 +18,11 @@
 
 #include "absl/types/optional.h"
 #include "api/scoped_refptr.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/video/i420_buffer.h"
 #include "api/video/video_frame.h"
 #include "api/video/video_frame_buffer.h"
 #include "api/video/video_rotation.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 #include "system_wrappers/include/metrics.h"
 #include "test/gtest.h"
diff --git a/video/rtp_video_stream_receiver_frame_transformer_delegate.cc b/video/rtp_video_stream_receiver_frame_transformer_delegate.cc
index 90f9ad9..e6601d0 100644
--- a/video/rtp_video_stream_receiver_frame_transformer_delegate.cc
+++ b/video/rtp_video_stream_receiver_frame_transformer_delegate.cc
@@ -14,8 +14,8 @@
 #include <vector>
 
 #include "absl/memory/memory.h"
+#include "api/task_queue/to_queued_task.h"
 #include "modules/rtp_rtcp/source/rtp_descriptor_authentication.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread.h"
 
 namespace webrtc {
diff --git a/video/rtp_video_stream_receiver_frame_transformer_delegate_unittest.cc b/video/rtp_video_stream_receiver_frame_transformer_delegate_unittest.cc
index 8e80683..20a5953 100644
--- a/video/rtp_video_stream_receiver_frame_transformer_delegate_unittest.cc
+++ b/video/rtp_video_stream_receiver_frame_transformer_delegate_unittest.cc
@@ -17,10 +17,10 @@
 
 #include "absl/memory/memory.h"
 #include "api/call/transport.h"
+#include "api/task_queue/to_queued_task.h"
 #include "call/video_receive_stream.h"
 #include "modules/rtp_rtcp/source/rtp_descriptor_authentication.h"
 #include "rtc_base/event.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/mock_frame_transformer.h"
diff --git a/video/task_queue_frame_decode_scheduler.cc b/video/task_queue_frame_decode_scheduler.cc
index 72de3c3..594f8b8 100644
--- a/video/task_queue_frame_decode_scheduler.cc
+++ b/video/task_queue_frame_decode_scheduler.cc
@@ -14,8 +14,8 @@
 #include <utility>
 
 #include "api/sequence_checker.h"
+#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 
 namespace webrtc {
 
diff --git a/video/video_receive_stream2.cc b/video/video_receive_stream2.cc
index cd38d03..80c58c3 100644
--- a/video/video_receive_stream2.cc
+++ b/video/video_receive_stream2.cc
@@ -27,7 +27,9 @@
 #include "api/crypto/frame_decryptor_interface.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/units/frequency.h"
 #include "api/units/time_delta.h"
 #include "api/units/timestamp.h"
@@ -55,8 +57,6 @@
 #include "rtc_base/strings/string_builder.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
diff --git a/video/video_receive_stream2.h b/video/video_receive_stream2.h
index 71dbf41..06978b4 100644
--- a/video/video_receive_stream2.h
+++ b/video/video_receive_stream2.h
@@ -16,6 +16,7 @@
 #include <vector>
 
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_factory.h"
 #include "api/units/time_delta.h"
 #include "api/units/timestamp.h"
@@ -30,7 +31,6 @@
 #include "modules/video_coding/video_receiver2.h"
 #include "rtc_base/system/no_unique_address.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread_annotations.h"
 #include "system_wrappers/include/clock.h"
 #include "video/frame_buffer_proxy.h"
diff --git a/video/video_send_stream.cc b/video/video_send_stream.cc
index 86fd0d9..1b60844 100644
--- a/video/video_send_stream.cc
+++ b/video/video_send_stream.cc
@@ -13,6 +13,7 @@
 
 #include "api/array_view.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/video/video_stream_encoder_settings.h"
 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
 #include "modules/rtp_rtcp/source/rtp_header_extension_size.h"
@@ -20,7 +21,6 @@
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "system_wrappers/include/clock.h"
 #include "video/adaptation/overuse_frame_detector.h"
 #include "video/frame_cadence_adapter.h"
diff --git a/video/video_send_stream.h b/video/video_send_stream.h
index 1912307..0faebf9 100644
--- a/video/video_send_stream.h
+++ b/video/video_send_stream.h
@@ -18,6 +18,7 @@
 #include "api/fec_controller.h"
 #include "api/field_trials_view.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/video/video_stream_encoder_interface.h"
 #include "call/bitrate_allocator.h"
 #include "call/video_receive_stream.h"
@@ -25,7 +26,6 @@
 #include "rtc_base/event.h"
 #include "rtc_base/system/no_unique_address.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "video/encoder_rtcp_feedback.h"
 #include "video/send_delay_stats.h"
 #include "video/send_statistics_proxy.h"
diff --git a/video/video_send_stream_impl.cc b/video/video_send_stream_impl.cc
index 53596a8..7e81528 100644
--- a/video/video_send_stream_impl.cc
+++ b/video/video_send_stream_impl.cc
@@ -21,6 +21,7 @@
 #include "api/rtp_parameters.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/video_codecs/video_codec.h"
 #include "call/rtp_transport_controller_send_interface.h"
 #include "call/video_send_stream.h"
@@ -33,7 +34,6 @@
 #include "rtc_base/experiments/rate_control_settings.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/safe_conversions.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/trace_event.h"
 #include "system_wrappers/include/clock.h"
 #include "system_wrappers/include/field_trial.h"
diff --git a/video/video_send_stream_impl.h b/video/video_send_stream_impl.h
index 076b25d..26f8d2e 100644
--- a/video/video_send_stream_impl.h
+++ b/video/video_send_stream_impl.h
@@ -20,6 +20,7 @@
 
 #include "absl/types/optional.h"
 #include "api/field_trials_view.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/video/encoded_image.h"
 #include "api/video/video_bitrate_allocation.h"
 #include "api/video/video_bitrate_allocator.h"
@@ -36,7 +37,6 @@
 #include "rtc_base/experiments/field_trial_parser.h"
 #include "rtc_base/system/no_unique_address.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/repeating_task.h"
 #include "rtc_base/thread_annotations.h"
 #include "video/send_statistics_proxy.h"
diff --git a/video/video_send_stream_tests.cc b/video/video_send_stream_tests.cc
index 7101ace..0cb5339 100644
--- a/video/video_send_stream_tests.cc
+++ b/video/video_send_stream_tests.cc
@@ -16,6 +16,7 @@
 #include "api/sequence_checker.h"
 #include "api/task_queue/default_task_queue_factory.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/test/simulated_network.h"
 #include "api/video/builtin_video_bitrate_allocator_factory.h"
 #include "api/video/encoded_image.h"
@@ -52,7 +53,6 @@
 #include "rtc_base/strings/string_builder.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/task_queue_for_test.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/unique_id_generator.h"
 #include "system_wrappers/include/sleep.h"
diff --git a/video/video_stream_encoder.cc b/video/video_stream_encoder.cc
index 791bdad..0cc56f5 100644
--- a/video/video_stream_encoder.cc
+++ b/video/video_stream_encoder.cc
@@ -23,6 +23,7 @@
 #include "api/sequence_checker.h"
 #include "api/task_queue/queued_task.h"
 #include "api/task_queue/task_queue_base.h"
+#include "api/task_queue/to_queued_task.h"
 #include "api/video/encoded_image.h"
 #include "api/video/i420_buffer.h"
 #include "api/video/render_resolution.h"
@@ -46,7 +47,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/string_builder.h"
 #include "rtc_base/system/no_unique_address.h"
-#include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/trace_event.h"
 #include "system_wrappers/include/metrics.h"
diff --git a/video/video_stream_encoder.h b/video/video_stream_encoder.h
index 34d7895..1953b91 100644
--- a/video/video_stream_encoder.h
+++ b/video/video_stream_encoder.h
@@ -20,6 +20,7 @@
 #include "api/adaptation/resource.h"
 #include "api/field_trials_view.h"
 #include "api/sequence_checker.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/units/data_rate.h"
 #include "api/video/video_bitrate_allocator.h"
 #include "api/video/video_rotation.h"
@@ -41,7 +42,6 @@
 #include "rtc_base/race_checker.h"
 #include "rtc_base/rate_statistics.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/thread_annotations.h"
 #include "system_wrappers/include/clock.h"
 #include "video/adaptation/video_stream_encoder_resource_manager.h"