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