OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <memory> | |
12 | |
13 #include "webrtc/base/gunit.h" | |
14 #include "webrtc/base/logging.h" | |
15 #include "webrtc/base/ptr_util.h" | |
16 #include "webrtc/base/ssladapter.h" | |
17 #include "webrtc/base/sslstreamadapter.h" | |
18 #include "webrtc/base/stringencode.h" | |
19 #include "webrtc/base/stringutils.h" | |
20 #include "webrtc/base/thread.h" | |
21 #ifdef WEBRTC_ANDROID | |
22 #include "webrtc/pc/test/androidtestinitializer.h" | |
23 #endif | |
24 #include "webrtc/pc/test/peerconnectiontestwrapper.h" | |
25 // Notice that mockpeerconnectionobservers.h must be included after the above! | |
26 #include "webrtc/pc/test/mockpeerconnectionobservers.h" | |
27 | |
28 using webrtc::DataChannelInterface; | |
29 using webrtc::FakeConstraints; | |
30 using webrtc::MediaConstraintsInterface; | |
31 using webrtc::MediaStreamInterface; | |
32 using webrtc::PeerConnectionInterface; | |
33 | |
34 namespace { | |
35 | |
36 const int kMaxWait = 10000; | |
37 | |
38 } // namespace | |
39 | |
40 class PeerConnectionEndToEndTest : public sigslot::has_slots<>, | |
41 public testing::Test { | |
42 public: | |
43 typedef std::vector<rtc::scoped_refptr<DataChannelInterface> > | |
44 DataChannelList; | |
45 | |
46 PeerConnectionEndToEndTest() { | |
47 RTC_CHECK(network_thread_.Start()); | |
48 RTC_CHECK(worker_thread_.Start()); | |
49 caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>( | |
50 "caller", &network_thread_, &worker_thread_); | |
51 callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>( | |
52 "callee", &network_thread_, &worker_thread_); | |
53 webrtc::PeerConnectionInterface::IceServer ice_server; | |
54 ice_server.uri = "stun:stun.l.google.com:19302"; | |
55 config_.servers.push_back(ice_server); | |
56 | |
57 #ifdef WEBRTC_ANDROID | |
58 webrtc::InitializeAndroidObjects(); | |
59 #endif | |
60 } | |
61 | |
62 void CreatePcs( | |
63 const MediaConstraintsInterface* pc_constraints, | |
64 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory, | |
65 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) { | |
66 EXPECT_TRUE(caller_->CreatePc( | |
67 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory)); | |
68 EXPECT_TRUE(callee_->CreatePc( | |
69 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory)); | |
70 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get()); | |
71 | |
72 caller_->SignalOnDataChannel.connect( | |
73 this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel); | |
74 callee_->SignalOnDataChannel.connect( | |
75 this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel); | |
76 } | |
77 | |
78 void Negotiate() { caller_->CreateOffer(NULL); } | |
79 | |
80 void WaitForCallEstablished() { | |
81 caller_->WaitForCallEstablished(); | |
82 callee_->WaitForCallEstablished(); | |
83 } | |
84 | |
85 void WaitForConnection() { | |
86 caller_->WaitForConnection(); | |
87 callee_->WaitForConnection(); | |
88 } | |
89 | |
90 void OnCallerAddedDataChanel(DataChannelInterface* dc) { | |
91 caller_signaled_data_channels_.push_back(dc); | |
92 } | |
93 | |
94 void OnCalleeAddedDataChannel(DataChannelInterface* dc) { | |
95 callee_signaled_data_channels_.push_back(dc); | |
96 } | |
97 | |
98 // Tests that |dc1| and |dc2| can send to and receive from each other. | |
99 void TestDataChannelSendAndReceive(DataChannelInterface* dc1, | |
100 DataChannelInterface* dc2) { | |
101 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer( | |
102 new webrtc::MockDataChannelObserver(dc1)); | |
103 | |
104 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer( | |
105 new webrtc::MockDataChannelObserver(dc2)); | |
106 | |
107 static const std::string kDummyData = "abcdefg"; | |
108 webrtc::DataBuffer buffer(kDummyData); | |
109 EXPECT_TRUE(dc1->Send(buffer)); | |
110 EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait); | |
111 | |
112 EXPECT_TRUE(dc2->Send(buffer)); | |
113 EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait); | |
114 | |
115 EXPECT_EQ(1U, dc1_observer->received_message_count()); | |
116 EXPECT_EQ(1U, dc2_observer->received_message_count()); | |
117 } | |
118 | |
119 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc, | |
120 const DataChannelList& remote_dc_list, | |
121 size_t remote_dc_index) { | |
122 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait); | |
123 | |
124 EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait); | |
125 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, | |
126 remote_dc_list[remote_dc_index]->state(), kMaxWait); | |
127 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id()); | |
128 } | |
129 | |
130 void CloseDataChannels(DataChannelInterface* local_dc, | |
131 const DataChannelList& remote_dc_list, | |
132 size_t remote_dc_index) { | |
133 local_dc->Close(); | |
134 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait); | |
135 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, | |
136 remote_dc_list[remote_dc_index]->state(), kMaxWait); | |
137 } | |
138 | |
139 protected: | |
140 rtc::Thread network_thread_; | |
141 rtc::Thread worker_thread_; | |
142 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_; | |
143 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_; | |
144 DataChannelList caller_signaled_data_channels_; | |
145 DataChannelList callee_signaled_data_channels_; | |
146 webrtc::PeerConnectionInterface::RTCConfiguration config_; | |
147 }; | |
148 | |
149 #ifdef HAVE_SCTP | |
Taylor Brandstetter
2017/05/11 04:43:11
I was wondering what would happen if you built pee
Zhi Huang
2017/05/12 20:05:33
Good question!
I changed the place of the #ifdef.
| |
150 // Verifies that the message is received by the right remote DataChannel when | |
151 // there are multiple DataChannels. | |
152 TEST_F(PeerConnectionEndToEndTest, | |
153 MessageTransferBetweenTwoPairsOfDataChannels) { | |
154 CreatePcs(nullptr, rtc::scoped_refptr<webrtc::AudioEncoderFactory>(), | |
155 rtc::scoped_refptr<webrtc::AudioDecoderFactory>()); | |
156 | |
157 webrtc::DataChannelInit init; | |
158 | |
159 rtc::scoped_refptr<DataChannelInterface> caller_dc_1( | |
160 caller_->CreateDataChannel("data", init)); | |
161 rtc::scoped_refptr<DataChannelInterface> caller_dc_2( | |
162 caller_->CreateDataChannel("data", init)); | |
163 | |
164 Negotiate(); | |
165 WaitForConnection(); | |
166 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0); | |
167 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1); | |
168 | |
169 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer( | |
170 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0])); | |
171 | |
172 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer( | |
173 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1])); | |
174 | |
175 const std::string message_1 = "hello 1"; | |
176 const std::string message_2 = "hello 2"; | |
177 | |
178 caller_dc_1->Send(webrtc::DataBuffer(message_1)); | |
179 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait); | |
180 | |
181 caller_dc_2->Send(webrtc::DataBuffer(message_2)); | |
182 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait); | |
183 | |
184 EXPECT_EQ(1U, dc_1_observer->received_message_count()); | |
185 EXPECT_EQ(1U, dc_2_observer->received_message_count()); | |
186 } | |
187 #endif // HAVE_SCTP | |
OLD | NEW |