Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/media/webrtc/webrtc_media_stream_track_collection.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "content/child/child_process.h" | |
| 14 #include "content/renderer/media/media_stream_audio_source.h" | |
| 15 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory. h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | |
| 18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | |
| 19 #include "third_party/WebKit/public/platform/WebString.h" | |
| 20 #include "third_party/WebKit/public/web/WebHeap.h" | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 class WebRtcMediaStreamTrackCollectionTest : public ::testing::Test { | |
| 25 public: | |
| 26 void SetUp() override { | |
| 27 dependency_factory_.reset(new MockPeerConnectionDependencyFactory()); | |
| 28 main_thread_ = base::ThreadTaskRunnerHandle::Get(); | |
| 29 collection_ = new WebRtcMediaStreamTrackCollection( | |
| 30 dependency_factory_.get(), main_thread_); | |
| 31 } | |
| 32 | |
| 33 void TearDown() override { blink::WebHeap::CollectAllGarbageForTesting(); } | |
| 34 | |
| 35 blink::WebMediaStreamTrack CreateLocalTrack(const std::string& id) { | |
| 36 blink::WebMediaStreamSource web_source; | |
| 37 web_source.Initialize( | |
| 38 blink::WebString::FromUTF8(id), blink::WebMediaStreamSource::kTypeAudio, | |
| 39 blink::WebString::FromUTF8("local_audio_track"), false); | |
| 40 MediaStreamAudioSource* audio_source = new MediaStreamAudioSource(true); | |
| 41 // Takes ownership of |audio_source|. | |
| 42 web_source.SetExtraData(audio_source); | |
| 43 | |
| 44 blink::WebMediaStreamTrack web_track; | |
| 45 web_track.Initialize(web_source.Id(), web_source); | |
| 46 audio_source->ConnectToTrack(web_track); | |
| 47 return web_track; | |
| 48 } | |
| 49 | |
| 50 scoped_refptr<WebRtcMediaStreamTrackAdapter> GetOrCreateRemoteTrack( | |
| 51 webrtc::MediaStreamTrackInterface* webrtc_track) { | |
| 52 DCHECK(main_thread_->BelongsToCurrentThread()); | |
| 53 scoped_refptr<WebRtcMediaStreamTrackAdapter> track; | |
| 54 dependency_factory_->GetWebRtcSignalingThread()->PostTask( | |
| 55 FROM_HERE, base::Bind(&WebRtcMediaStreamTrackCollectionTest:: | |
| 56 GetOrCreateRemoteTrackOnSignalingThread, | |
| 57 base::Unretained(this), webrtc_track, &track)); | |
| 58 RunMessageLoopsUntilIdle(); | |
| 59 return track; | |
| 60 } | |
| 61 | |
| 62 void GetOrCreateRemoteTrackOnSignalingThread( | |
| 63 webrtc::MediaStreamTrackInterface* webrtc_track, | |
| 64 scoped_refptr<WebRtcMediaStreamTrackAdapter>* track) { | |
| 65 DCHECK(dependency_factory_->GetWebRtcSignalingThread() | |
| 66 ->BelongsToCurrentThread()); | |
| 67 *track = collection_->GetOrCreateRemoteTrack(webrtc_track); | |
| 68 } | |
| 69 | |
| 70 // Runs message loops on the webrtc signaling thread and the main thread until | |
| 71 // idle. | |
| 72 void RunMessageLoopsUntilIdle() { | |
| 73 DCHECK(main_thread_->BelongsToCurrentThread()); | |
| 74 base::WaitableEvent waitable_event( | |
| 75 base::WaitableEvent::ResetPolicy::MANUAL, | |
| 76 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 77 dependency_factory_->GetWebRtcSignalingThread()->PostTask( | |
| 78 FROM_HERE, base::Bind(&WebRtcMediaStreamTrackCollectionTest:: | |
| 79 RunMessageLoopUntilIdleOnSignalingThread, | |
| 80 base::Unretained(this), &waitable_event)); | |
| 81 waitable_event.Wait(); | |
| 82 base::RunLoop().RunUntilIdle(); | |
| 83 } | |
| 84 | |
| 85 void RunMessageLoopUntilIdleOnSignalingThread( | |
| 86 base::WaitableEvent* waitable_event) { | |
| 87 DCHECK(dependency_factory_->GetWebRtcSignalingThread() | |
| 88 ->BelongsToCurrentThread()); | |
| 89 base::RunLoop().RunUntilIdle(); | |
| 90 waitable_event->Signal(); | |
| 91 } | |
| 92 | |
| 93 protected: | |
| 94 // Message loop and child processes is needed for task queues and threading to | |
| 95 // work, as is necessary to create tracks and adapters. | |
| 96 base::MessageLoop message_loop_; | |
| 97 ChildProcess child_process_; | |
| 98 | |
| 99 std::unique_ptr<MockPeerConnectionDependencyFactory> dependency_factory_; | |
| 100 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; | |
| 101 scoped_refptr<WebRtcMediaStreamTrackCollection> collection_; | |
| 102 }; | |
| 103 | |
| 104 TEST_F(WebRtcMediaStreamTrackCollectionTest, AddAndRemoveLocalTrack) { | |
| 105 blink::WebMediaStreamTrack web_track = CreateLocalTrack("local_track"); | |
| 106 scoped_refptr<WebRtcMediaStreamTrackAdapter> track = | |
| 107 collection_->GetOrCreateLocalTrack(web_track); | |
| 108 DCHECK(track); | |
| 109 EXPECT_TRUE(track->is_initialized()); | |
| 110 EXPECT_EQ(track, collection_->GetLocalTrack(web_track.Id().Utf8())); | |
| 111 EXPECT_EQ(1u, collection_->GetLocalTrackCount()); | |
| 112 | |
| 113 // "GetOrCreate" for already existing track. | |
| 114 EXPECT_EQ(track, collection_->GetOrCreateLocalTrack(web_track)); | |
| 115 EXPECT_EQ(1u, collection_->GetLocalTrackCount()); | |
| 116 | |
| 117 // Remove and uninitialize track. Must not hold references to track. | |
| 118 track = nullptr; | |
| 119 collection_->RemoveAndUninitializeLocalTrack(web_track.Id().Utf8()); | |
| 120 EXPECT_EQ(0u, collection_->GetLocalTrackCount()); | |
| 121 EXPECT_EQ(nullptr, collection_->GetLocalTrack(web_track.Id().Utf8())); | |
| 122 // Allow uninitialization of track to occur. | |
| 123 RunMessageLoopsUntilIdle(); | |
| 124 } | |
| 125 | |
| 126 TEST_F(WebRtcMediaStreamTrackCollectionTest, AddAndRemoveRemoteTrack) { | |
| 127 scoped_refptr<MockWebRtcAudioTrack> webrtc_track = | |
| 128 MockWebRtcAudioTrack::Create("remote_track"); | |
| 129 scoped_refptr<WebRtcMediaStreamTrackAdapter> track = | |
| 130 GetOrCreateRemoteTrack(webrtc_track.get()); | |
| 131 DCHECK(track); | |
| 132 EXPECT_TRUE(track->is_initialized()); | |
| 133 EXPECT_EQ(track, collection_->GetRemoteTrack(webrtc_track->id())); | |
| 134 EXPECT_EQ(1u, collection_->GetRemoteTrackCount()); | |
| 135 | |
| 136 // "GetOrCreate" for already existing track. | |
| 137 EXPECT_EQ(track, GetOrCreateRemoteTrack(webrtc_track.get())); | |
| 138 EXPECT_EQ(1u, collection_->GetRemoteTrackCount()); | |
| 139 | |
| 140 // Remove and uninitialize track. Must not hold references to track. | |
| 141 track = nullptr; | |
| 142 collection_->RemoveAndUninitializeRemoteTrack(webrtc_track->id()); | |
| 143 EXPECT_EQ(0u, collection_->GetRemoteTrackCount()); | |
| 144 EXPECT_EQ(nullptr, collection_->GetRemoteTrack(webrtc_track->id())); | |
| 145 // Allow uninitialization of track to occur. | |
| 146 RunMessageLoopsUntilIdle(); | |
| 147 } | |
| 148 | |
| 149 TEST_F(WebRtcMediaStreamTrackCollectionTest, AddAndClearLocalAndRemoteTrack) { | |
| 150 // Local and remote tracks should be able to use the same id without conflict. | |
| 151 const char* id = "id"; | |
| 152 | |
| 153 // Add local track. | |
| 154 blink::WebMediaStreamTrack local_web_track = CreateLocalTrack(id); | |
| 155 scoped_refptr<WebRtcMediaStreamTrackAdapter> local_track = | |
| 156 collection_->GetOrCreateLocalTrack(local_web_track); | |
| 157 DCHECK(local_track); | |
| 158 EXPECT_TRUE(local_track->is_initialized()); | |
| 159 EXPECT_EQ(local_track, collection_->GetLocalTrack(id)); | |
| 160 EXPECT_EQ(1u, collection_->GetLocalTrackCount()); | |
| 161 | |
| 162 // Add remote track. | |
| 163 scoped_refptr<MockWebRtcAudioTrack> remote_webrtc_track = | |
| 164 MockWebRtcAudioTrack::Create(id); | |
| 165 scoped_refptr<WebRtcMediaStreamTrackAdapter> remote_track = | |
| 166 GetOrCreateRemoteTrack(remote_webrtc_track.get()); | |
| 167 DCHECK(remote_track); | |
| 168 EXPECT_TRUE(remote_track->is_initialized()); | |
| 169 EXPECT_EQ(remote_track, collection_->GetRemoteTrack(id)); | |
| 170 EXPECT_NE(local_track, remote_track); | |
| 171 EXPECT_EQ(1u, collection_->GetRemoteTrackCount()); | |
| 172 | |
| 173 // Clear and uninitialize tracks. Must not hold references to tracks. | |
| 174 local_track = nullptr; | |
| 175 remote_track = nullptr; | |
| 176 collection_->ClearAndUninitializeTracks(); | |
| 177 EXPECT_EQ(0u, collection_->GetLocalTrackCount()); | |
| 178 EXPECT_EQ(0u, collection_->GetRemoteTrackCount()); | |
| 179 EXPECT_EQ(nullptr, collection_->GetLocalTrack(id)); | |
| 180 EXPECT_EQ(nullptr, collection_->GetRemoteTrack(id)); | |
| 181 // Allow uninitialization of tracks to occur. | |
| 182 RunMessageLoopsUntilIdle(); | |
| 183 } | |
| 184 | |
|
Guido Urdaneta
2017/05/22 16:14:49
Add a few extra tests to check what happens if, fo
hbos_chromium
2017/05/29 13:28:51
Done.
| |
| 185 } // namespace content | |
| OLD | NEW |