OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2017 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/fakenetwork.h" | |
14 #include "webrtc/base/gunit.h" | |
15 #include "webrtc/base/physicalsocketserver.h" | |
16 #include "webrtc/base/virtualsocketserver.h" | |
17 #include "webrtc/media/base/fakemediaengine.h" | |
18 #include "webrtc/ortc/ortcfactory.h" | |
19 #include "webrtc/ortc/testrtpparameters.h" | |
20 #include "webrtc/p2p/base/fakepackettransport.h" | |
21 | |
22 namespace webrtc { | |
23 | |
24 // This test uses a virtual network and fake media engine, in order to test the | |
25 // OrtcFactory at only an API level. Any end-to-end test should go in | |
26 // ortcfactory_integrationtest.cc instead. | |
27 class OrtcFactoryTest : public testing::Test { | |
28 public: | |
29 OrtcFactoryTest() | |
30 : virtual_socket_server_(&physical_socket_server_), | |
31 socket_server_scope_(&virtual_socket_server_), | |
32 fake_packet_transport_("fake transport") { | |
33 ortc_factory_ = OrtcFactory::Create( | |
34 nullptr, nullptr, &fake_network_manager_, nullptr, | |
35 nullptr, std::unique_ptr<cricket::MediaEngineInterface>( | |
36 new cricket::FakeMediaEngine())) | |
37 .MoveValue(); | |
38 } | |
39 | |
40 protected: | |
41 // Uses a single premade FakePacketTransport, so shouldn't be called twice in | |
pthatcher1
2017/02/21 20:11:55
pre-made
Taylor Brandstetter
2017/02/22 01:41:59
Done.
| |
42 // the same test. | |
43 std::unique_ptr<RtpTransportInterface> | |
44 CreateRtpTransportWithFakePacketTransport() { | |
45 return ortc_factory_ | |
46 ->CreateRtpTransport(MakeRtcpMuxParameters(), &fake_packet_transport_, | |
47 nullptr, nullptr) | |
48 .MoveValue(); | |
49 } | |
50 | |
51 rtc::PhysicalSocketServer physical_socket_server_; | |
52 rtc::VirtualSocketServer virtual_socket_server_; | |
53 rtc::SocketServerScope socket_server_scope_; | |
54 rtc::FakeNetworkManager fake_network_manager_; | |
55 rtc::FakePacketTransport fake_packet_transport_; | |
56 std::unique_ptr<OrtcFactoryInterface> ortc_factory_; | |
57 }; | |
58 | |
59 TEST_F(OrtcFactoryTest, CanCreateMultipleRtpTransportControllers) { | |
60 auto controller_result1 = ortc_factory_->CreateRtpTransportController(); | |
61 EXPECT_TRUE(controller_result1.ok()); | |
62 auto controller_result2 = ortc_factory_->CreateRtpTransportController(); | |
63 EXPECT_TRUE(controller_result1.ok()); | |
64 } | |
65 | |
66 // Simple test for the successful cases of CreateRtpTransport. | |
67 TEST_F(OrtcFactoryTest, CreateRtpTransportWithAndWithoutMux) { | |
68 rtc::FakePacketTransport rtp("rtp"); | |
69 rtc::FakePacketTransport rtcp("rtcp"); | |
70 // With muxed RTCP. | |
71 RtcpParameters rtcp_parameters; | |
72 rtcp_parameters.mux = true; | |
73 auto result = ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp, | |
74 nullptr, nullptr); | |
75 EXPECT_TRUE(result.ok()); | |
76 result.MoveValue().reset(); | |
77 // With non-muxed RTCP. | |
78 rtcp_parameters.mux = false; | |
79 result = | |
80 ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp, &rtcp, nullptr); | |
81 EXPECT_TRUE(result.ok()); | |
82 } | |
83 | |
84 // If no CNAME is provided, one should be generated and returned by | |
85 // GetRtpParameters. | |
86 TEST_F(OrtcFactoryTest, CreateRtpTransportGeneratesCname) { | |
87 rtc::FakePacketTransport rtp("rtp"); | |
88 RtcpParameters rtcp_parameters; | |
89 rtcp_parameters.mux = true; | |
90 auto result = ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp, | |
91 nullptr, nullptr); | |
92 ASSERT_TRUE(result.ok()); | |
93 EXPECT_FALSE(result.value()->GetRtcpParameters().cname.empty()); | |
94 } | |
95 | |
96 // Extension of the above test; multiple transports created by the same factory | |
97 // should use the same generated CNAME. | |
98 TEST_F(OrtcFactoryTest, MultipleRtpTransportsUseSameGeneratedCname) { | |
99 rtc::FakePacketTransport packet_transport1("1"); | |
100 rtc::FakePacketTransport packet_transport2("2"); | |
101 RtcpParameters rtcp_parameters; | |
102 rtcp_parameters.mux = true; | |
103 // Sanity check. | |
104 ASSERT_TRUE(rtcp_parameters.cname.empty()); | |
105 auto result = ortc_factory_->CreateRtpTransport( | |
106 rtcp_parameters, &packet_transport1, nullptr, nullptr); | |
107 ASSERT_TRUE(result.ok()); | |
108 auto rtp_transport1 = result.MoveValue(); | |
109 result = ortc_factory_->CreateRtpTransport( | |
110 rtcp_parameters, &packet_transport2, nullptr, nullptr); | |
111 ASSERT_TRUE(result.ok()); | |
112 auto rtp_transport2 = result.MoveValue(); | |
113 RtcpParameters params1 = rtp_transport1->GetRtcpParameters(); | |
114 RtcpParameters params2 = rtp_transport2->GetRtcpParameters(); | |
115 EXPECT_FALSE(params1.cname.empty()); | |
116 EXPECT_EQ(params1.cname, params2.cname); | |
117 } | |
118 | |
119 TEST_F(OrtcFactoryTest, CreateRtpTransportWithNoPacketTransport) { | |
120 auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(), | |
121 nullptr, nullptr, nullptr); | |
122 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type()); | |
123 } | |
124 | |
125 // If the |mux| member of the RtcpParameters is false, both an RTP and RTCP | |
126 // packet transport are needed. | |
127 TEST_F(OrtcFactoryTest, CreateRtpTransportWithMissingRtcpTransport) { | |
128 rtc::FakePacketTransport rtp("rtp"); | |
129 RtcpParameters rtcp_parameters; | |
130 rtcp_parameters.mux = false; | |
131 auto result = ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp, | |
132 nullptr, nullptr); | |
133 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type()); | |
134 } | |
135 | |
136 // If the |mux| member of the RtcpParameters is true, only an RTP packet | |
137 // transport is necessary. So, passing in an RTCP transport is most likely | |
138 // an accident, and thus should be treated as an error. | |
139 TEST_F(OrtcFactoryTest, CreateRtpTransportWithExtraneousRtcpTransport) { | |
140 rtc::FakePacketTransport rtp("rtp"); | |
141 rtc::FakePacketTransport rtcp("rtcp"); | |
142 RtcpParameters rtcp_parameters; | |
143 rtcp_parameters.mux = true; | |
144 auto result = | |
145 ortc_factory_->CreateRtpTransport(rtcp_parameters, &rtp, &rtcp, nullptr); | |
146 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type()); | |
147 } | |
148 | |
149 // Basic test that CreateUdpTransport works with AF_INET and AF_INET6. | |
150 TEST_F(OrtcFactoryTest, CreateUdpTransport) { | |
151 auto result = ortc_factory_->CreateUdpTransport(AF_INET); | |
152 EXPECT_TRUE(result.ok()); | |
153 result = ortc_factory_->CreateUdpTransport(AF_INET6); | |
154 EXPECT_TRUE(result.ok()); | |
155 } | |
156 | |
157 // Test CreateUdpPort with the |min_port| and |max_port| arguments. | |
158 TEST_F(OrtcFactoryTest, CreateUdpTransportWithPortRange) { | |
159 auto socket_result1 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002); | |
160 ASSERT_TRUE(socket_result1.ok()); | |
161 EXPECT_EQ(2000, socket_result1.value()->GetLocalAddress().port()); | |
162 auto socket_result2 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002); | |
163 ASSERT_TRUE(socket_result2.ok()); | |
164 EXPECT_EQ(2001, socket_result2.value()->GetLocalAddress().port()); | |
165 auto socket_result3 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002); | |
166 ASSERT_TRUE(socket_result3.ok()); | |
167 EXPECT_EQ(2002, socket_result3.value()->GetLocalAddress().port()); | |
168 | |
169 // All sockets in the range have been exhausted, so the next call should | |
170 // fail. | |
171 auto failed_result = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002); | |
172 EXPECT_EQ(RTCErrorType::RESOURCE_EXHAUSTED, failed_result.error().type()); | |
173 | |
174 // If one socket is destroyed, that port should be freed up again. | |
175 socket_result2.MoveValue().reset(); | |
176 auto socket_result4 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002); | |
177 ASSERT_TRUE(socket_result4.ok()); | |
178 EXPECT_EQ(2001, socket_result4.value()->GetLocalAddress().port()); | |
179 } | |
180 | |
181 // Basic test that CreateUdpTransport works with AF_INET and AF_INET6. | |
182 TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidAddressFamily) { | |
183 auto result = ortc_factory_->CreateUdpTransport(12345); | |
184 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type()); | |
185 } | |
186 | |
187 TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidPortRange) { | |
188 auto result = ortc_factory_->CreateUdpTransport(AF_INET, 3000, 2000); | |
189 EXPECT_EQ(RTCErrorType::INVALID_RANGE, result.error().type()); | |
190 } | |
191 | |
192 // Just sanity check that each "GetCapabilities" method returns some codecs. | |
193 TEST_F(OrtcFactoryTest, GetSenderAndReceiverCapabilities) { | |
194 RtpCapabilities audio_send_caps = | |
195 ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_AUDIO); | |
196 EXPECT_GT(audio_send_caps.codecs.size(), 0u); | |
197 RtpCapabilities video_send_caps = | |
198 ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_VIDEO); | |
199 EXPECT_GT(video_send_caps.codecs.size(), 0u); | |
200 RtpCapabilities audio_receive_caps = | |
201 ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_AUDIO); | |
202 EXPECT_GT(audio_receive_caps.codecs.size(), 0u); | |
203 RtpCapabilities video_receive_caps = | |
204 ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_VIDEO); | |
205 EXPECT_GT(video_receive_caps.codecs.size(), 0u); | |
206 } | |
207 | |
208 // Calling CreateRtpSender with a null track should fail, since that makes it | |
209 // impossible to know whether to create an audio or video sender. The | |
210 // application should be using the method that takes a cricket::MediaType | |
211 // instead. | |
212 TEST_F(OrtcFactoryTest, CreateSenderWithNullTrack) { | |
213 auto rtp_transport = CreateRtpTransportWithFakePacketTransport(); | |
214 auto result = ortc_factory_->CreateRtpSender(nullptr, rtp_transport.get()); | |
215 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type()); | |
216 } | |
217 | |
218 // Calling CreateRtpSender or CreateRtpReceiver with MEDIA_TYPE_DATA should | |
219 // fail. | |
220 TEST_F(OrtcFactoryTest, CreateSenderOrReceieverWithInvalidKind) { | |
221 auto rtp_transport = CreateRtpTransportWithFakePacketTransport(); | |
222 auto sender_result = ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_DATA, | |
223 rtp_transport.get()); | |
224 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type()); | |
225 auto receiver_result = ortc_factory_->CreateRtpReceiver( | |
226 cricket::MEDIA_TYPE_DATA, rtp_transport.get()); | |
227 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type()); | |
228 } | |
229 | |
230 TEST_F(OrtcFactoryTest, CreateSendersOrReceieversWithNullTransport) { | |
231 auto sender_result = | |
232 ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_AUDIO, nullptr); | |
233 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type()); | |
234 auto receiver_result = | |
235 ortc_factory_->CreateRtpReceiver(cricket::MEDIA_TYPE_AUDIO, nullptr); | |
236 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type()); | |
237 } | |
238 | |
239 } // namespace webrtc | |
OLD | NEW |