1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/peerconnection/rtc_peer_connection.h"
#include <string>
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/web/web_heap.h"
#include "third_party/blink/public/web/web_script_source.h"
#include "third_party/blink/renderer/bindings/core/v8/dictionary.h"
#include "third_party/blink/renderer/bindings/core/v8/to_v8_traits.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_void_function.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_answer_options.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_ice_server.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_offer_options.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_peer_connection_error_callback.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_session_description_callback.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_session_description_init.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/testing/sim/sim_request.h"
#include "third_party/blink/renderer/core/testing/sim/sim_test.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_track.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_track_impl.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_video_track.h"
#include "third_party/blink/renderer/modules/mediastream/mock_media_stream_video_source.h"
#include "third_party/blink/renderer/modules/peerconnection/mock_rtc_peer_connection_handler_platform.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_vector.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_audio_track.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_track_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_rtp_receiver_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_rtp_sender_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_session_description_platform.h"
#include "third_party/blink/renderer/platform/testing/io_task_runner_testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/webrtc/api/rtc_error.h"
#include "v8/include/v8.h"
namespace blink {
class RTCOfferOptionsPlatform;
class RTCPeerConnectionTest : public testing::Test {
public:
RTCPeerConnection* CreatePC(V8TestingScope& scope) {
RTCConfiguration* config = RTCConfiguration::Create();
RTCIceServer* ice_server = RTCIceServer::Create();
ice_server->setUrl("stun:fake.stun.url");
HeapVector<Member<RTCIceServer>> ice_servers;
ice_servers.push_back(ice_server);
config->setIceServers(ice_servers);
RTCPeerConnection::SetRtcPeerConnectionHandlerFactoryForTesting(
base::BindRepeating(
&RTCPeerConnectionTest::CreateRTCPeerConnectionHandler,
base::Unretained(this)));
return RTCPeerConnection::Create(scope.GetExecutionContext(), config,
scope.GetExceptionState());
}
virtual std::unique_ptr<RTCPeerConnectionHandler>
CreateRTCPeerConnectionHandler() {
return std::make_unique<MockRTCPeerConnectionHandlerPlatform>();
}
MediaStreamTrack* CreateTrack(V8TestingScope& scope,
MediaStreamSource::StreamType type,
String id) {
auto platform_source = std::make_unique<MockMediaStreamVideoSource>();
auto* platform_source_ptr = platform_source.get();
auto* source = MakeGarbageCollected<MediaStreamSource>(
"sourceId", type, "sourceName", /*remote=*/false,
std::move(platform_source));
std::unique_ptr<MediaStreamTrackPlatform> platform_track;
if (type == MediaStreamSource::kTypeAudio) {
platform_track =
std::make_unique<MediaStreamAudioTrack>(/*is_local_track=*/true);
} else {
platform_track = std::make_unique<MediaStreamVideoTrack>(
platform_source_ptr,
MediaStreamVideoSource::ConstraintsOnceCallback(),
/*enabled=*/true);
}
auto* component = MakeGarbageCollected<MediaStreamComponentImpl>(
id, source, std::move(platform_track));
return MakeGarbageCollected<MediaStreamTrackImpl>(
scope.GetExecutionContext(), component);
}
std::string GetExceptionMessage(V8TestingScope& scope) {
DummyExceptionStateForTesting& exception_state = scope.GetExceptionState();
return exception_state.HadException() ? exception_state.Message().Utf8()
: "";
}
void AddStream(V8TestingScope& scope,
RTCPeerConnection* pc,
MediaStream* stream) {
pc->addStream(scope.GetScriptState(), stream, scope.GetExceptionState());
EXPECT_EQ("", GetExceptionMessage(scope));
}
void RemoveStream(V8TestingScope& scope,
RTCPeerConnection* pc,
MediaStream* stream) {
pc->removeStream(stream, scope.GetExceptionState());
EXPECT_EQ("", GetExceptionMessage(scope));
}
protected:
test::TaskEnvironment task_environment_;
ScopedTestingPlatformSupport<IOTaskRunnerTestingPlatformSupport> platform_;
};
TEST_F(RTCPeerConnectionTest, GetAudioTrack) {
V8TestingScope scope;
RTCPeerConnection* pc = CreatePC(scope);
EXPECT_EQ("", GetExceptionMessage(scope));
ASSERT_TRUE(pc);
MediaStreamTrack* track =
CreateTrack(scope, MediaStreamSource::kTypeAudio, "audioTrack");
HeapVector<Member<MediaStreamTrack>> tracks;
tracks.push_back(track);
MediaStream* stream =
MediaStream::Create(scope.GetExecutionContext(), tracks);
ASSERT_TRUE(stream);
EXPECT_FALSE(pc->GetTrackForTesting(track->Component()));
AddStream(scope, pc, stream);
EXPECT_TRUE(pc->GetTrackForTesting(track->Component()));
}
TEST_F(RTCPeerConnectionTest, GetVideoTrack) {
V8TestingScope scope;
RTCPeerConnection* pc = CreatePC(scope);
EXPECT_EQ("", GetExceptionMessage(scope));
ASSERT_TRUE(pc);
MediaStreamTrack* track =
CreateTrack(scope, MediaStreamSource::kTypeVideo, "videoTrack");
HeapVector<Member<MediaStreamTrack>> tracks;
tracks.push_back(track);
MediaStream* stream =
MediaStream::Create(scope.GetExecutionContext(), tracks);
ASSERT_TRUE(stream);
EXPECT_FALSE(pc->GetTrackForTesting(track->Component()));
AddStream(scope, pc, stream);
EXPECT_TRUE(pc->GetTrackForTesting(track->Component()));
}
TEST_F(RTCPeerConnectionTest, GetAudioAndVideoTrack) {
V8TestingScope scope;
RTCPeerConnection* pc = CreatePC(scope);
EXPECT_EQ("", GetExceptionMessage(scope));
ASSERT_TRUE(pc);
HeapVector<Member<MediaStreamTrack>> tracks;
MediaStreamTrack* audio_track =
CreateTrack(scope, MediaStreamSource::kTypeAudio, "audioTrack");
tracks.push_back(audio_track);
MediaStreamTrack* video_track =
CreateTrack(scope, MediaStreamSource::kTypeVideo, "videoTrack");
tracks.push_back(video_track);
MediaStream* stream =
MediaStream::Create(scope.GetExecutionContext(), tracks);
ASSERT_TRUE(stream);
EXPECT_FALSE(pc->GetTrackForTesting(audio_track->Component()));
EXPECT_FALSE(pc->GetTrackForTesting(video_track->Component()));
AddStream(scope, pc, stream);
EXPECT_TRUE(pc->GetTrackForTesting(audio_track->Component()));
EXPECT_TRUE(pc->GetTrackForTesting(video_track->Component()));
}
TEST_F(RTCPeerConnectionTest, GetTrackRemoveStreamAndGCAll) {
V8TestingScope scope;
Persistent<RTCPeerConnection> pc = CreatePC(scope);
EXPECT_EQ("", GetExceptionMessage(scope));
ASSERT_TRUE(pc);
MediaStreamTrack* track =
CreateTrack(scope, MediaStreamSource::kTypeAudio, "audioTrack");
MediaStreamComponent* track_component = track->Component();
{
HeapVector<Member<MediaStreamTrack>> tracks;
tracks.push_back(track);
MediaStream* stream =
MediaStream::Create(scope.GetExecutionContext(), tracks);
ASSERT_TRUE(stream);
EXPECT_FALSE(pc->GetTrackForTesting(track_component));
AddStream(scope, pc, stream);
EXPECT_TRUE(pc->GetTrackForTesting(track_component));
RemoveStream(scope, pc, stream);
// Transceivers will still reference the stream even after it is "removed".
// To make the GC tests work, clear the stream from tracks so that the
// stream does not keep tracks alive.
while (!stream->getTracks().empty())
stream->removeTrack(stream->getTracks()[0], scope.GetExceptionState());
}
// This will destroy |MediaStream|, |MediaStreamTrack| and its
// |MediaStreamComponent|, which will remove its mapping from the peer
// connection.
WebHeap::CollectAllGarbageForTesting();
EXPECT_FALSE(pc->GetTrackForTesting(track_component));
}
TEST_F(RTCPeerConnectionTest,
GetTrackRemoveStreamAndGCWithPersistentComponent) {
V8TestingScope scope;
Persistent<RTCPeerConnection> pc = CreatePC(scope);
EXPECT_EQ("", GetExceptionMessage(scope));
ASSERT_TRUE(pc);
MediaStreamTrack* track =
CreateTrack(scope, MediaStreamSource::kTypeAudio, "audioTrack");
Persistent<MediaStreamComponent> track_component = track->Component();
{
HeapVector<Member<MediaStreamTrack>> tracks;
tracks.push_back(track);
MediaStream* stream =
MediaStream::Create(scope.GetExecutionContext(), tracks);
ASSERT_TRUE(stream);
EXPECT_FALSE(pc->GetTrackForTesting(track_component.Get()));
AddStream(scope, pc, stream);
EXPECT_TRUE(pc->GetTrackForTesting(track_component.Get()));
RemoveStream(scope, pc, stream);
// Transceivers will still reference the stream even after it is "removed".
// To make the GC tests work, clear the stream from tracks so that the
// stream does not keep tracks alive.
while (!stream->getTracks().empty())
stream->removeTrack(stream->getTracks()[0], scope.GetExceptionState());
}
// This will destroy |MediaStream| and |MediaStreamTrack| (but not
// |MediaStreamComponent|), which will remove its mapping from the peer
// connection.
WebHeap::CollectAllGarbageForTesting();
EXPECT_FALSE(pc->GetTrackForTesting(track_component.Get()));
}
enum class AsyncOperationAction {
kLeavePending,
kResolve,
kReject,
};
template <typename RequestType>
void CompleteRequest(RequestType* request, bool resolve);
template <>
void CompleteRequest(RTCVoidRequest* request, bool resolve) {
if (resolve) {
request->RequestSucceeded();
} else {
request->RequestFailed(
webrtc::RTCError(webrtc::RTCErrorType::INVALID_MODIFICATION));
}
}
template <>
void CompleteRequest(RTCSessionDescriptionRequest* request, bool resolve) {
if (resolve) {
auto* description =
MakeGarbageCollected<RTCSessionDescriptionPlatform>(String(), String());
request->RequestSucceeded(description);
} else {
request->RequestFailed(
webrtc::RTCError(webrtc::RTCErrorType::INVALID_MODIFICATION));
}
}
template <typename RequestType>
void PostToCompleteRequest(AsyncOperationAction action, RequestType* request) {
switch (action) {
case AsyncOperationAction::kLeavePending:
return;
case AsyncOperationAction::kResolve:
scheduler::GetSequencedTaskRunnerForTesting()->PostTask(
FROM_HERE, WTF::BindOnce(&CompleteRequest<RequestType>,
WrapWeakPersistent(request), true));
return;
case AsyncOperationAction::kReject:
scheduler::GetSequencedTaskRunnerForTesting()->PostTask(
FROM_HERE, WTF::BindOnce(&CompleteRequest<RequestType>,
WrapWeakPersistent(request), false));
return;
}
}
class FakeRTCPeerConnectionHandlerPlatform
: public MockRTCPeerConnectionHandlerPlatform {
public:
Vector<std::unique_ptr<RTCRtpTransceiverPlatform>> CreateOffer(
RTCSessionDescriptionRequest* request,
RTCOfferOptionsPlatform*) override {
PostToCompleteRequest<RTCSessionDescriptionRequest>(async_operation_action_,
request);
return {};
}
void CreateAnswer(RTCSessionDescriptionRequest* request,
RTCAnswerOptionsPlatform*) override {
PostToCompleteRequest<RTCSessionDescriptionRequest>(async_operation_action_,
request);
}
void SetLocalDescription(RTCVoidRequest* request,
ParsedSessionDescription) override {
PostToCompleteRequest<RTCVoidRequest>(async_operation_action_, request);
}
void SetRemoteDescription(RTCVoidRequest* request,
ParsedSessionDescription) override {
PostToCompleteRequest<RTCVoidRequest>(async_operation_action_, request);
}
void set_async_operation_action(AsyncOperationAction action) {
async_operation_action_ = action;
}
private:
// Decides what to do with future async operations' promises/callbacks.
AsyncOperationAction async_operation_action_ =
AsyncOperationAction::kLeavePending;
};
TEST_F(RTCPeerConnectionTest, MediaStreamTrackStopsThrottling) {
V8TestingScope scope;
auto* scheduler = scope.GetFrame().GetFrameScheduler()->GetPageScheduler();
EXPECT_FALSE(scheduler->OptedOutFromAggressiveThrottlingForTest());
// Creating the RTCPeerConnection doesn't disable throttling.
RTCPeerConnection* pc = CreatePC(scope);
EXPECT_EQ("", GetExceptionMessage(scope));
ASSERT_TRUE(pc);
EXPECT_FALSE(scheduler->OptedOutFromAggressiveThrottlingForTest());
// But creating a media stream track does.
MediaStreamTrack* track =
CreateTrack(scope, MediaStreamSource::kTypeAudio, "audioTrack");
HeapVector<Member<MediaStreamTrack>> tracks;
tracks.push_back(track);
MediaStream* stream =
MediaStream::Create(scope.GetExecutionContext(), tracks);
ASSERT_TRUE(stream);
EXPECT_TRUE(scheduler->OptedOutFromAggressiveThrottlingForTest());
// Stopping the track disables the opt-out.
track->stopTrack(scope.GetExecutionContext());
EXPECT_FALSE(scheduler->OptedOutFromAggressiveThrottlingForTest());
}
TEST_F(RTCPeerConnectionTest, GettingRtpTransportEarlySucceeds) {
V8TestingScope scope;
RTCPeerConnection* pc = CreatePC(scope);
EXPECT_NE(pc->rtpTransport(), nullptr);
EXPECT_EQ("", GetExceptionMessage(scope));
}
} // namespace blink
|