media: Use per-frame task runners at WebRTCMediaStreamAdapterMap and WebRtcMediaStreamTrackAdapterMap

This is part of efforts to replace base::ThreadTaskRunnerHandle::Get()
and SequencedTaskRunnerHandle::Get() with other appropriate task runners
in the renderer.

TBR=hajimehoshi@chromium.org

(cherry picked from commit d2f2ce6989291ada20b50bae26a7449f719cd683)

Bug: 786332
Change-Id: I300a38816bef6bb8f371203aaad52cb31154309a
Reviewed-on: https://chromium-review.googlesource.com/882713
Reviewed-by: Dale Curtis <dalecurtis@chromium.org>
Reviewed-by: Alexander Timin <altimin@chromium.org>
Commit-Queue: Hajime Hoshi <hajimehoshi@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#531820}
Reviewed-on: https://chromium-review.googlesource.com/931803
Reviewed-by: Guido Urdaneta <guidou@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#554}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/content/renderer/media/rtc_peer_connection_handler.cc b/content/renderer/media/rtc_peer_connection_handler.cc
index b8109b9..b3fbc42 100644
--- a/content/renderer/media/rtc_peer_connection_handler.cc
+++ b/content/renderer/media/rtc_peer_connection_handler.cc
@@ -1300,10 +1300,12 @@
       is_closed_(false),
       dependency_factory_(dependency_factory),
       track_adapter_map_(
-          new WebRtcMediaStreamTrackAdapterMap(dependency_factory_)),
+          new WebRtcMediaStreamTrackAdapterMap(dependency_factory_,
+                                               task_runner)),
       stream_adapter_map_(new WebRtcMediaStreamAdapterMap(dependency_factory_,
+                                                          task_runner,
                                                           track_adapter_map_)),
-      task_runner_(task_runner),
+      task_runner_(std::move(task_runner)),
       weak_factory_(this) {
   CHECK(client_);
   GetPeerConnectionHandlers()->insert(this);
diff --git a/content/renderer/media/webrtc/rtc_rtp_sender_unittest.cc b/content/renderer/media/webrtc/rtc_rtp_sender_unittest.cc
index 5574bae..95713b3 100644
--- a/content/renderer/media/webrtc/rtc_rtp_sender_unittest.cc
+++ b/content/renderer/media/webrtc/rtc_rtp_sender_unittest.cc
@@ -20,6 +20,7 @@
 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
 #include "third_party/WebKit/public/platform/WebRTCVoidRequest.h"
 #include "third_party/WebKit/public/platform/WebString.h"
+#include "third_party/WebKit/public/platform/scheduler/test/renderer_scheduler_test_support.h"
 #include "third_party/WebKit/public/web/WebHeap.h"
 #include "third_party/webrtc/api/test/mock_rtpsender.h"
 
@@ -32,10 +33,11 @@
  public:
   void SetUp() override {
     dependency_factory_.reset(new MockPeerConnectionDependencyFactory());
-    main_thread_ = base::ThreadTaskRunnerHandle::Get();
+    main_thread_ = blink::scheduler::GetSingleThreadTaskRunnerForTesting();
     stream_map_ = new WebRtcMediaStreamAdapterMap(
-        dependency_factory_.get(),
-        new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get()));
+        dependency_factory_.get(), main_thread_,
+        new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get(),
+                                             main_thread_));
     mock_webrtc_sender_ = new rtc::RefCountedObject<webrtc::MockRtpSender>();
   }
 
diff --git a/content/renderer/media/webrtc/webrtc_media_stream_adapter_map.cc b/content/renderer/media/webrtc/webrtc_media_stream_adapter_map.cc
index 89ef430..c7452c3 100644
--- a/content/renderer/media/webrtc/webrtc_media_stream_adapter_map.cc
+++ b/content/renderer/media/webrtc/webrtc_media_stream_adapter_map.cc
@@ -66,9 +66,10 @@
 
 WebRtcMediaStreamAdapterMap::WebRtcMediaStreamAdapterMap(
     PeerConnectionDependencyFactory* const factory,
+    scoped_refptr<base::SingleThreadTaskRunner> main_thread,
     scoped_refptr<WebRtcMediaStreamTrackAdapterMap> track_adapter_map)
     : factory_(factory),
-      main_thread_(base::ThreadTaskRunnerHandle::Get()),
+      main_thread_(std::move(main_thread)),
       track_adapter_map_(std::move(track_adapter_map)) {
   DCHECK(factory_);
   DCHECK(main_thread_);
diff --git a/content/renderer/media/webrtc/webrtc_media_stream_adapter_map.h b/content/renderer/media/webrtc/webrtc_media_stream_adapter_map.h
index 1d7ab4c..be9eac0 100644
--- a/content/renderer/media/webrtc/webrtc_media_stream_adapter_map.h
+++ b/content/renderer/media/webrtc/webrtc_media_stream_adapter_map.h
@@ -78,6 +78,7 @@
   // Must be invoked on the main thread.
   WebRtcMediaStreamAdapterMap(
       PeerConnectionDependencyFactory* const factory,
+      scoped_refptr<base::SingleThreadTaskRunner> main_thread,
       scoped_refptr<WebRtcMediaStreamTrackAdapterMap> track_adapter_map);
 
   // Gets a new reference to the local stream adapter, or null if no such
diff --git a/content/renderer/media/webrtc/webrtc_media_stream_adapter_map_unittest.cc b/content/renderer/media/webrtc/webrtc_media_stream_adapter_map_unittest.cc
index 9a01fa4..8cbd874 100644
--- a/content/renderer/media/webrtc/webrtc_media_stream_adapter_map_unittest.cc
+++ b/content/renderer/media/webrtc/webrtc_media_stream_adapter_map_unittest.cc
@@ -39,8 +39,9 @@
     dependency_factory_.reset(new MockPeerConnectionDependencyFactory());
     main_thread_ = blink::scheduler::GetSingleThreadTaskRunnerForTesting();
     map_ = new WebRtcMediaStreamAdapterMap(
-        dependency_factory_.get(),
-        new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get()));
+        dependency_factory_.get(), main_thread_,
+        new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get(),
+                                             main_thread_));
   }
 
   void TearDown() override { blink::WebHeap::CollectAllGarbageForTesting(); }
diff --git a/content/renderer/media/webrtc/webrtc_media_stream_adapter_unittest.cc b/content/renderer/media/webrtc/webrtc_media_stream_adapter_unittest.cc
index b07bb6a..cb0e7f2 100644
--- a/content/renderer/media/webrtc/webrtc_media_stream_adapter_unittest.cc
+++ b/content/renderer/media/webrtc/webrtc_media_stream_adapter_unittest.cc
@@ -41,8 +41,9 @@
  public:
   void SetUp() override {
     dependency_factory_.reset(new MockPeerConnectionDependencyFactory());
-    track_adapter_map_ =
-        new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get());
+    track_adapter_map_ = new WebRtcMediaStreamTrackAdapterMap(
+        dependency_factory_.get(),
+        blink::scheduler::GetSingleThreadTaskRunnerForTesting());
   }
 
   void TearDown() override {
diff --git a/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.cc b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.cc
index 66b8406..183d53f 100644
--- a/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.cc
+++ b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.cc
@@ -60,8 +60,9 @@
 }
 
 WebRtcMediaStreamTrackAdapterMap::WebRtcMediaStreamTrackAdapterMap(
-    PeerConnectionDependencyFactory* const factory)
-    : factory_(factory), main_thread_(base::ThreadTaskRunnerHandle::Get()) {
+    PeerConnectionDependencyFactory* const factory,
+    scoped_refptr<base::SingleThreadTaskRunner> main_thread)
+    : factory_(factory), main_thread_(std::move(main_thread)) {
   DCHECK(factory_);
   DCHECK(main_thread_);
 }
diff --git a/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h
index 0616c63..870d018 100644
--- a/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h
+++ b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h
@@ -71,7 +71,8 @@
 
   // Must be invoked on the main thread.
   WebRtcMediaStreamTrackAdapterMap(
-      PeerConnectionDependencyFactory* const factory);
+      PeerConnectionDependencyFactory* const factory,
+      scoped_refptr<base::SingleThreadTaskRunner> main_thread);
 
   // Gets a new reference to the local track adapter, or null if no such adapter
   // was found. When all references are destroyed the adapter is disposed and
diff --git a/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map_unittest.cc b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map_unittest.cc
index 5ea62f9..7a98eca 100644
--- a/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map_unittest.cc
+++ b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map_unittest.cc
@@ -27,7 +27,8 @@
   void SetUp() override {
     dependency_factory_.reset(new MockPeerConnectionDependencyFactory());
     main_thread_ = blink::scheduler::GetSingleThreadTaskRunnerForTesting();
-    map_ = new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get());
+    map_ = new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get(),
+                                                main_thread_);
   }
 
   void TearDown() override { blink::WebHeap::CollectAllGarbageForTesting(); }
diff --git a/content/renderer/media/webrtc/webrtc_set_remote_description_observer_unittest.cc b/content/renderer/media/webrtc/webrtc_set_remote_description_observer_unittest.cc
index 64ceefa..395be68 100644
--- a/content/renderer/media/webrtc/webrtc_set_remote_description_observer_unittest.cc
+++ b/content/renderer/media/webrtc/webrtc_set_remote_description_observer_unittest.cc
@@ -68,8 +68,9 @@
     main_thread_ = blink::scheduler::GetSingleThreadTaskRunnerForTesting();
     scoped_refptr<WebRtcMediaStreamAdapterMap> map =
         new WebRtcMediaStreamAdapterMap(
-            dependency_factory_.get(),
-            new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get()));
+            dependency_factory_.get(), main_thread_,
+            new WebRtcMediaStreamTrackAdapterMap(dependency_factory_.get(),
+                                                 main_thread_));
     observer_ = new WebRtcSetRemoteDescriptionObserverForTest();
     observer_handler_ = WebRtcSetRemoteDescriptionObserverHandler::Create(
         main_thread_, pc_, map, observer_);