Create conversions between webrtc::TaskQueueBase and rtc::TaskQueue

Bug: webrtc:10191
Change-Id: Ia6642081ac758e31c14780bdd83dbc88279cce6d
Reviewed-on: https://webrtc-review.googlesource.com/c/124826
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26890}
diff --git a/api/task_queue/task_queue.cc b/api/task_queue/task_queue.cc
index 0635850..ed489eb 100644
--- a/api/task_queue/task_queue.cc
+++ b/api/task_queue/task_queue.cc
@@ -14,14 +14,21 @@
 
 namespace rtc {
 
-TaskQueue::TaskQueue(const char* queue_name, Priority priority)
-    : impl_(webrtc::GlobalTaskQueueFactory()
-                .CreateTaskQueue(queue_name, priority)
-                .release()) {
+TaskQueue::TaskQueue(
+    std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter> task_queue)
+    : impl_(task_queue.release()) {
   impl_->task_queue_ = this;
 }
 
+TaskQueue::TaskQueue(const char* queue_name, Priority priority)
+    : TaskQueue(webrtc::GlobalTaskQueueFactory().CreateTaskQueue(queue_name,
+                                                                 priority)) {}
+
 TaskQueue::~TaskQueue() {
+  // There might running task that tries to rescheduler itself to the TaskQueue
+  // and not yet aware TaskQueue destructor is called.
+  // Calling back to TaskQueue::PostTask need impl_ pointer still be valid, so
+  // do not invalidate impl_ pointer until Delete returns.
   impl_->Delete();
 }