forked from JumpingYang001/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathortcfactory_unittest.cc
250 lines (225 loc) · 10.7 KB
/
ortcfactory_unittest.cc
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
/*
* Copyright 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <memory>
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "media/base/fakemediaengine.h"
#include "ortc/ortcfactory.h"
#include "ortc/testrtpparameters.h"
#include "p2p/base/fakepackettransport.h"
#include "rtc_base/fakenetwork.h"
#include "rtc_base/gunit.h"
#include "rtc_base/virtualsocketserver.h"
namespace webrtc {
// This test uses a virtual network and fake media engine, in order to test the
// OrtcFactory at only an API level. Any end-to-end test should go in
// ortcfactory_integrationtest.cc instead.
class OrtcFactoryTest : public testing::Test {
public:
OrtcFactoryTest()
: thread_(&virtual_socket_server_),
fake_packet_transport_("fake transport") {
ortc_factory_ =
OrtcFactory::Create(&thread_, nullptr, &fake_network_manager_, nullptr,
nullptr,
std::unique_ptr<cricket::MediaEngineInterface>(
new cricket::FakeMediaEngine()),
CreateBuiltinAudioEncoderFactory(),
CreateBuiltinAudioDecoderFactory())
.MoveValue();
}
protected:
// Uses a single pre-made FakePacketTransport, so shouldn't be called twice in
// the same test.
std::unique_ptr<RtpTransportInterface>
CreateRtpTransportWithFakePacketTransport() {
return ortc_factory_
->CreateRtpTransport(MakeRtcpMuxParameters(), &fake_packet_transport_,
nullptr, nullptr)
.MoveValue();
}
rtc::VirtualSocketServer virtual_socket_server_;
rtc::AutoSocketServerThread thread_;
rtc::FakeNetworkManager fake_network_manager_;
rtc::FakePacketTransport fake_packet_transport_;
std::unique_ptr<OrtcFactoryInterface> ortc_factory_;
};
TEST_F(OrtcFactoryTest, CanCreateMultipleRtpTransportControllers) {
auto controller_result1 = ortc_factory_->CreateRtpTransportController();
EXPECT_TRUE(controller_result1.ok());
auto controller_result2 = ortc_factory_->CreateRtpTransportController();
EXPECT_TRUE(controller_result1.ok());
}
// Simple test for the successful cases of CreateRtpTransport.
TEST_F(OrtcFactoryTest, CreateRtpTransportWithAndWithoutMux) {
rtc::FakePacketTransport rtp("rtp");
rtc::FakePacketTransport rtcp("rtcp");
// With muxed RTCP.
RtpTransportParameters parameters = MakeRtcpMuxParameters();
auto result =
ortc_factory_->CreateRtpTransport(parameters, &rtp, nullptr, nullptr);
EXPECT_TRUE(result.ok());
result.MoveValue().reset();
// With non-muxed RTCP.
parameters.rtcp.mux = false;
result = ortc_factory_->CreateRtpTransport(parameters, &rtp, &rtcp, nullptr);
EXPECT_TRUE(result.ok());
}
// Simple test for the successful cases of CreateSrtpTransport.
TEST_F(OrtcFactoryTest, CreateSrtpTransport) {
rtc::FakePacketTransport rtp("rtp");
rtc::FakePacketTransport rtcp("rtcp");
// With muxed RTCP.
RtpTransportParameters parameters = MakeRtcpMuxParameters();
auto result =
ortc_factory_->CreateSrtpTransport(parameters, &rtp, nullptr, nullptr);
EXPECT_TRUE(result.ok());
result.MoveValue().reset();
// With non-muxed RTCP.
parameters.rtcp.mux = false;
result = ortc_factory_->CreateSrtpTransport(parameters, &rtp, &rtcp, nullptr);
EXPECT_TRUE(result.ok());
}
// If no CNAME is provided, one should be generated and returned by
// GetRtpParameters.
TEST_F(OrtcFactoryTest, CreateRtpTransportGeneratesCname) {
rtc::FakePacketTransport rtp("rtp");
auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(), &rtp,
nullptr, nullptr);
ASSERT_TRUE(result.ok());
EXPECT_FALSE(result.value()->GetParameters().rtcp.cname.empty());
}
// Extension of the above test; multiple transports created by the same factory
// should use the same generated CNAME.
TEST_F(OrtcFactoryTest, MultipleRtpTransportsUseSameGeneratedCname) {
rtc::FakePacketTransport packet_transport1("1");
rtc::FakePacketTransport packet_transport2("2");
RtpTransportParameters parameters = MakeRtcpMuxParameters();
// Sanity check.
ASSERT_TRUE(parameters.rtcp.cname.empty());
auto result = ortc_factory_->CreateRtpTransport(
parameters, &packet_transport1, nullptr, nullptr);
ASSERT_TRUE(result.ok());
auto rtp_transport1 = result.MoveValue();
result = ortc_factory_->CreateRtpTransport(parameters, &packet_transport2,
nullptr, nullptr);
ASSERT_TRUE(result.ok());
auto rtp_transport2 = result.MoveValue();
RtcpParameters params1 = rtp_transport1->GetParameters().rtcp;
RtcpParameters params2 = rtp_transport2->GetParameters().rtcp;
EXPECT_FALSE(params1.cname.empty());
EXPECT_EQ(params1.cname, params2.cname);
}
TEST_F(OrtcFactoryTest, CreateRtpTransportWithNoPacketTransport) {
auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(),
nullptr, nullptr, nullptr);
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
}
// If the |mux| member of the RtcpParameters is false, both an RTP and RTCP
// packet transport are needed.
TEST_F(OrtcFactoryTest, CreateRtpTransportWithMissingRtcpTransport) {
rtc::FakePacketTransport rtp("rtp");
RtpTransportParameters parameters;
parameters.rtcp.mux = false;
auto result =
ortc_factory_->CreateRtpTransport(parameters, &rtp, nullptr, nullptr);
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
}
// If the |mux| member of the RtcpParameters is true, only an RTP packet
// transport is necessary. So, passing in an RTCP transport is most likely
// an accident, and thus should be treated as an error.
TEST_F(OrtcFactoryTest, CreateRtpTransportWithExtraneousRtcpTransport) {
rtc::FakePacketTransport rtp("rtp");
rtc::FakePacketTransport rtcp("rtcp");
auto result = ortc_factory_->CreateRtpTransport(MakeRtcpMuxParameters(), &rtp,
&rtcp, nullptr);
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
}
// Basic test that CreateUdpTransport works with AF_INET and AF_INET6.
TEST_F(OrtcFactoryTest, CreateUdpTransport) {
auto result = ortc_factory_->CreateUdpTransport(AF_INET);
EXPECT_TRUE(result.ok());
result = ortc_factory_->CreateUdpTransport(AF_INET6);
EXPECT_TRUE(result.ok());
}
// Test CreateUdpPort with the |min_port| and |max_port| arguments.
TEST_F(OrtcFactoryTest, CreateUdpTransportWithPortRange) {
auto socket_result1 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
ASSERT_TRUE(socket_result1.ok());
EXPECT_EQ(2000, socket_result1.value()->GetLocalAddress().port());
auto socket_result2 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
ASSERT_TRUE(socket_result2.ok());
EXPECT_EQ(2001, socket_result2.value()->GetLocalAddress().port());
auto socket_result3 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
ASSERT_TRUE(socket_result3.ok());
EXPECT_EQ(2002, socket_result3.value()->GetLocalAddress().port());
// All sockets in the range have been exhausted, so the next call should
// fail.
auto failed_result = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
EXPECT_EQ(RTCErrorType::RESOURCE_EXHAUSTED, failed_result.error().type());
// If one socket is destroyed, that port should be freed up again.
socket_result2.MoveValue().reset();
auto socket_result4 = ortc_factory_->CreateUdpTransport(AF_INET, 2000, 2002);
ASSERT_TRUE(socket_result4.ok());
EXPECT_EQ(2001, socket_result4.value()->GetLocalAddress().port());
}
// Basic test that CreateUdpTransport works with AF_INET and AF_INET6.
TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidAddressFamily) {
auto result = ortc_factory_->CreateUdpTransport(12345);
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
}
TEST_F(OrtcFactoryTest, CreateUdpTransportWithInvalidPortRange) {
auto result = ortc_factory_->CreateUdpTransport(AF_INET, 3000, 2000);
EXPECT_EQ(RTCErrorType::INVALID_RANGE, result.error().type());
}
// Just sanity check that each "GetCapabilities" method returns some codecs.
TEST_F(OrtcFactoryTest, GetSenderAndReceiverCapabilities) {
RtpCapabilities audio_send_caps =
ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_AUDIO);
EXPECT_GT(audio_send_caps.codecs.size(), 0u);
RtpCapabilities video_send_caps =
ortc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_VIDEO);
EXPECT_GT(video_send_caps.codecs.size(), 0u);
RtpCapabilities audio_receive_caps =
ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_AUDIO);
EXPECT_GT(audio_receive_caps.codecs.size(), 0u);
RtpCapabilities video_receive_caps =
ortc_factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_VIDEO);
EXPECT_GT(video_receive_caps.codecs.size(), 0u);
}
// Calling CreateRtpSender with a null track should fail, since that makes it
// impossible to know whether to create an audio or video sender. The
// application should be using the method that takes a cricket::MediaType
// instead.
TEST_F(OrtcFactoryTest, CreateSenderWithNullTrack) {
auto rtp_transport = CreateRtpTransportWithFakePacketTransport();
auto result = ortc_factory_->CreateRtpSender(nullptr, rtp_transport.get());
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
}
// Calling CreateRtpSender or CreateRtpReceiver with MEDIA_TYPE_DATA should
// fail.
TEST_F(OrtcFactoryTest, CreateSenderOrReceieverWithInvalidKind) {
auto rtp_transport = CreateRtpTransportWithFakePacketTransport();
auto sender_result = ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_DATA,
rtp_transport.get());
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type());
auto receiver_result = ortc_factory_->CreateRtpReceiver(
cricket::MEDIA_TYPE_DATA, rtp_transport.get());
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type());
}
TEST_F(OrtcFactoryTest, CreateSendersOrReceieversWithNullTransport) {
auto sender_result =
ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_AUDIO, nullptr);
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, sender_result.error().type());
auto receiver_result =
ortc_factory_->CreateRtpReceiver(cricket::MEDIA_TYPE_AUDIO, nullptr);
EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, receiver_result.error().type());
}
} // namespace webrtc