blob: 40afbd328b6dfe176a68e11b2323c84e33593855 [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
Karl Wiberg3e9e5b32017-11-06 05:01:56 +010013#include "api/audio_codecs/builtin_audio_decoder_factory.h"
14#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "media/base/fakemediaengine.h"
16#include "ortc/ortcfactory.h"
17#include "ortc/testrtpparameters.h"
18#include "p2p/base/fakepackettransport.h"
19#include "rtc_base/fakenetwork.h"
20#include "rtc_base/gunit.h"
21#include "rtc_base/virtualsocketserver.h"
deadbeefe814a0d2017-02-25 18:15:09 -080022
23namespace webrtc {
24
25// This test uses a virtual network and fake media engine, in order to test the
26// OrtcFactory at only an API level. Any end-to-end test should go in
27// ortcfactory_integrationtest.cc instead.
28class OrtcFactoryTest : public testing::Test {
29 public:
30 OrtcFactoryTest()
deadbeef98e186c2017-05-16 18:00:06 -070031 : thread_(&virtual_socket_server_),
deadbeefe814a0d2017-02-25 18:15:09 -080032 fake_packet_transport_("fake transport") {
33 ortc_factory_ =
deadbeef57ca81a2017-06-29 05:34:45 -070034 OrtcFactory::Create(&thread_, nullptr, &fake_network_manager_, nullptr,
deadbeefe814a0d2017-02-25 18:15:09 -080035 nullptr,
36 std::unique_ptr<cricket::MediaEngineInterface>(
Karl Wiberg3e9e5b32017-11-06 05:01:56 +010037 new cricket::FakeMediaEngine()),
38 CreateBuiltinAudioEncoderFactory(),
39 CreateBuiltinAudioDecoderFactory())
deadbeefe814a0d2017-02-25 18:15:09 -080040 .MoveValue();
41 }
42
43 protected:
44 // Uses a single pre-made FakePacketTransport, so shouldn't be called twice in
45 // the same test.
46 std::unique_ptr<RtpTransportInterface>
47 CreateRtpTransportWithFakePacketTransport() {
48 return ortc_factory_
49 ->CreateRtpTransport(MakeRtcpMuxParameters(), &fake_packet_transport_,
50 nullptr, nullptr)
51 .MoveValue();
52 }
53
deadbeefe814a0d2017-02-25 18:15:09 -080054 rtc::VirtualSocketServer virtual_socket_server_;
nisse7eaa4ea2017-05-08 05:25:41 -070055 rtc::AutoSocketServerThread thread_;
deadbeefe814a0d2017-02-25 18:15:09 -080056 rtc::FakeNetworkManager fake_network_manager_;
57 rtc::FakePacketTransport fake_packet_transport_;
58 std::unique_ptr<OrtcFactoryInterface> ortc_factory_;
59};
60
61TEST_F(OrtcFactoryTest, CanCreateMultipleRtpTransportControllers) {
62 auto controller_result1 = ortc_factory_->CreateRtpTransportController();
63 EXPECT_TRUE(controller_result1.ok());
64 auto controller_result2 = ortc_factory_->CreateRtpTransportController();
65 EXPECT_TRUE(controller_result1.ok());
66}
67
68// Simple test for the successful cases of CreateRtpTransport.
69TEST_F(OrtcFactoryTest, CreateRtpTransportWithAndWithoutMux) {
70 rtc::FakePacketTransport rtp("rtp");
71 rtc::FakePacketTransport rtcp("rtcp");
72 // With muxed RTCP.
sprangdb2a9fc2017-08-09 06:42:32 -070073 RtpTransportParameters parameters = MakeRtcpMuxParameters();
74 auto result =
75 ortc_factory_->CreateRtpTransport(parameters, &rtp, nullptr, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -080076 EXPECT_TRUE(result.ok());
77 result.MoveValue().reset();
78 // With non-muxed RTCP.
sprangdb2a9fc2017-08-09 06:42:32 -070079 parameters.rtcp.mux = false;
80 result = ortc_factory_->CreateRtpTransport(parameters, &rtp, &rtcp, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -080081 EXPECT_TRUE(result.ok());
82}
83
zhihuangd3501ad2017-03-03 14:39:06 -080084// Simple test for the successful cases of CreateSrtpTransport.
85TEST_F(OrtcFactoryTest, CreateSrtpTransport) {
86 rtc::FakePacketTransport rtp("rtp");
87 rtc::FakePacketTransport rtcp("rtcp");
88 // With muxed RTCP.
sprangdb2a9fc2017-08-09 06:42:32 -070089 RtpTransportParameters parameters = MakeRtcpMuxParameters();
90 auto result =
91 ortc_factory_->CreateSrtpTransport(parameters, &rtp, nullptr, nullptr);
zhihuangd3501ad2017-03-03 14:39:06 -080092 EXPECT_TRUE(result.ok());
93 result.MoveValue().reset();
94 // With non-muxed RTCP.
sprangdb2a9fc2017-08-09 06:42:32 -070095 parameters.rtcp.mux = false;
96 result = ortc_factory_->CreateSrtpTransport(parameters, &rtp, &rtcp, nullptr);
zhihuangd3501ad2017-03-03 14:39:06 -080097 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");
sprangdb2a9fc2017-08-09 06:42:32 -0700104 auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(), &rtp,
deadbeefe814a0d2017-02-25 18:15:09 -0800105 nullptr, nullptr);
106 ASSERT_TRUE(result.ok());
sprangdb2a9fc2017-08-09 06:42:32 -0700107 EXPECT_FALSE(result.value()->GetParameters().rtcp.cname.empty());
deadbeefe814a0d2017-02-25 18:15:09 -0800108}
109
110// Extension of the above test; multiple transports created by the same factory
111// should use the same generated CNAME.
112TEST_F(OrtcFactoryTest, MultipleRtpTransportsUseSameGeneratedCname) {
113 rtc::FakePacketTransport packet_transport1("1");
114 rtc::FakePacketTransport packet_transport2("2");
sprangdb2a9fc2017-08-09 06:42:32 -0700115 RtpTransportParameters parameters = MakeRtcpMuxParameters();
deadbeefe814a0d2017-02-25 18:15:09 -0800116 // Sanity check.
sprangdb2a9fc2017-08-09 06:42:32 -0700117 ASSERT_TRUE(parameters.rtcp.cname.empty());
deadbeefe814a0d2017-02-25 18:15:09 -0800118 auto result = ortc_factory_->CreateRtpTransport(
sprangdb2a9fc2017-08-09 06:42:32 -0700119 parameters, &packet_transport1, nullptr, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -0800120 ASSERT_TRUE(result.ok());
121 auto rtp_transport1 = result.MoveValue();
sprangdb2a9fc2017-08-09 06:42:32 -0700122 result = ortc_factory_->CreateRtpTransport(parameters, &packet_transport2,
123 nullptr, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -0800124 ASSERT_TRUE(result.ok());
125 auto rtp_transport2 = result.MoveValue();
sprangdb2a9fc2017-08-09 06:42:32 -0700126 RtcpParameters params1 = rtp_transport1->GetParameters().rtcp;
127 RtcpParameters params2 = rtp_transport2->GetParameters().rtcp;
deadbeefe814a0d2017-02-25 18:15:09 -0800128 EXPECT_FALSE(params1.cname.empty());
129 EXPECT_EQ(params1.cname, params2.cname);
130}
131
132TEST_F(OrtcFactoryTest, CreateRtpTransportWithNoPacketTransport) {
133 auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(),
134 nullptr, nullptr, nullptr);
135 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
136}
137
138// If the |mux| member of the RtcpParameters is false, both an RTP and RTCP
139// packet transport are needed.
140TEST_F(OrtcFactoryTest, CreateRtpTransportWithMissingRtcpTransport) {
141 rtc::FakePacketTransport rtp("rtp");
sprangdb2a9fc2017-08-09 06:42:32 -0700142 RtpTransportParameters parameters;
143 parameters.rtcp.mux = false;
144 auto result =
145 ortc_factory_->CreateRtpTransport(parameters, &rtp, nullptr, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -0800146 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
147}
148
149// If the |mux| member of the RtcpParameters is true, only an RTP packet
150// transport is necessary. So, passing in an RTCP transport is most likely
151// an accident, and thus should be treated as an error.
152TEST_F(OrtcFactoryTest, CreateRtpTransportWithExtraneousRtcpTransport) {
153 rtc::FakePacketTransport rtp("rtp");
154 rtc::FakePacketTransport rtcp("rtcp");
sprangdb2a9fc2017-08-09 06:42:32 -0700155 auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(), &rtp,
156 &rtcp, nullptr);
deadbeefe814a0d2017-02-25 18:15:09 -0800157 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
158}
159
160// Basic test that CreateUdpTransport works with AF_INET and AF_INET6.
161TEST_F(OrtcFactoryTest, CreateUdpTransport) {
162 auto result = ortc_factory_->CreateUdpTransport(AF_INET);
163 EXPECT_TRUE(result.ok());
164 result = ortc_factory_->CreateUdpTransport(AF_INET6);
165 EXPECT_TRUE(result.ok());
166}
167
168// Test CreateUdpPort with the |min_port| and |max_port| arguments.
169TEST_F(OrtcFactoryTest, CreateUdpTransportWithPortRange) {
170 auto socket_result1 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
171 ASSERT_TRUE(socket_result1.ok());
172 EXPECT_EQ(2000, socket_result1.value()->GetLocalAddress().port());
173 auto socket_result2 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
174 ASSERT_TRUE(socket_result2.ok());
175 EXPECT_EQ(2001, socket_result2.value()->GetLocalAddress().port());
176 auto socket_result3 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
177 ASSERT_TRUE(socket_result3.ok());
178 EXPECT_EQ(2002, socket_result3.value()->GetLocalAddress().port());
179
180 // All sockets in the range have been exhausted, so the next call should
181 // fail.
182 auto failed_result = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
183 EXPECT_EQ(RTCErrorType::RESOURCE_EXHAUSTED, failed_result.error().type());
184
185 // If one socket is destroyed, that port should be freed up again.
186 socket_result2.MoveValue().reset();
187 auto socket_result4 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
188 ASSERT_TRUE(socket_result4.ok());
189 EXPECT_EQ(2001, socket_result4.value()->GetLocalAddress().port());
190}
191
192// Basic test that CreateUdpTransport works with AF_INET and AF_INET6.
193TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidAddressFamily) {
194 auto result = ortc_factory_->CreateUdpTransport(12345);
195 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
196}
197
198TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidPortRange) {
199 auto result = ortc_factory_->CreateUdpTransport(AF_INET, 3000, 2000);
200 EXPECT_EQ(RTCErrorType::INVALID_RANGE, result.error().type());
201}
202
203// Just sanity check that each "GetCapabilities" method returns some codecs.
204TEST_F(OrtcFactoryTest, GetSenderAndReceiverCapabilities) {
205 RtpCapabilities audio_send_caps =
206 ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_AUDIO);
207 EXPECT_GT(audio_send_caps.codecs.size(), 0u);
208 RtpCapabilities video_send_caps =
209 ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_VIDEO);
210 EXPECT_GT(video_send_caps.codecs.size(), 0u);
211 RtpCapabilities audio_receive_caps =
212 ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_AUDIO);
213 EXPECT_GT(audio_receive_caps.codecs.size(), 0u);
214 RtpCapabilities video_receive_caps =
215 ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_VIDEO);
216 EXPECT_GT(video_receive_caps.codecs.size(), 0u);
217}
218
219// Calling CreateRtpSender with a null track should fail, since that makes it
220// impossible to know whether to create an audio or video sender. The
221// application should be using the method that takes a cricket::MediaType
222// instead.
223TEST_F(OrtcFactoryTest, CreateSenderWithNullTrack) {
224 auto rtp_transport = CreateRtpTransportWithFakePacketTransport();
225 auto result = ortc_factory_->CreateRtpSender(nullptr, rtp_transport.get());
226 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
227}
228
229// Calling CreateRtpSender or CreateRtpReceiver with MEDIA_TYPE_DATA should
230// fail.
231TEST_F(OrtcFactoryTest, CreateSenderOrReceieverWithInvalidKind) {
232 auto rtp_transport = CreateRtpTransportWithFakePacketTransport();
233 auto sender_result = ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_DATA,
234 rtp_transport.get());
235 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type());
236 auto receiver_result = ortc_factory_->CreateRtpReceiver(
237 cricket::MEDIA_TYPE_DATA, rtp_transport.get());
238 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type());
239}
240
241TEST_F(OrtcFactoryTest, CreateSendersOrReceieversWithNullTransport) {
242 auto sender_result =
243 ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_AUDIO, nullptr);
244 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type());
245 auto receiver_result =
246 ortc_factory_->CreateRtpReceiver(cricket::MEDIA_TYPE_AUDIO, nullptr);
247 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type());
248}
249
250} // namespace webrtc