1 /*
2 * Copyright (c) 2015 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 "api/audio_codecs/builtin_audio_decoder_factory.h"
14 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
15 #include "media/engine/webrtcmediaengine.h"
16 #include "test/gtest.h"
17
18 using webrtc::RtpExtension;
19
20 namespace cricket {
21 namespace {
22
MakeUniqueExtensions()23 std::vector<RtpExtension> MakeUniqueExtensions() {
24 std::vector<RtpExtension> result;
25 char name[] = "a";
26 for (int i = 0; i < 7; ++i) {
27 result.push_back(RtpExtension(name, 1 + i));
28 name[0]++;
29 result.push_back(RtpExtension(name, 14 - i));
30 name[0]++;
31 }
32 return result;
33 }
34
MakeRedundantExtensions()35 std::vector<RtpExtension> MakeRedundantExtensions() {
36 std::vector<RtpExtension> result;
37 char name[] = "a";
38 for (int i = 0; i < 7; ++i) {
39 result.push_back(RtpExtension(name, 1 + i));
40 result.push_back(RtpExtension(name, 14 - i));
41 name[0]++;
42 }
43 return result;
44 }
45
SupportedExtensions1(const std::string & name)46 bool SupportedExtensions1(const std::string& name) {
47 return name == "c" || name == "i";
48 }
49
SupportedExtensions2(const std::string & name)50 bool SupportedExtensions2(const std::string& name) {
51 return name != "a" && name != "n";
52 }
53
IsSorted(const std::vector<webrtc::RtpExtension> & extensions)54 bool IsSorted(const std::vector<webrtc::RtpExtension>& extensions) {
55 const std::string* last = nullptr;
56 for (const auto& extension : extensions) {
57 if (last && *last > extension.uri) {
58 return false;
59 }
60 last = &extension.uri;
61 }
62 return true;
63 }
64 } // namespace
65
TEST(WebRtcMediaEngineTest,ValidateRtpExtensions_EmptyList)66 TEST(WebRtcMediaEngineTest, ValidateRtpExtensions_EmptyList) {
67 std::vector<RtpExtension> extensions;
68 EXPECT_TRUE(ValidateRtpExtensions(extensions));
69 }
70
TEST(WebRtcMediaEngineTest,ValidateRtpExtensions_AllGood)71 TEST(WebRtcMediaEngineTest, ValidateRtpExtensions_AllGood) {
72 std::vector<RtpExtension> extensions = MakeUniqueExtensions();
73 EXPECT_TRUE(ValidateRtpExtensions(extensions));
74 }
75
TEST(WebRtcMediaEngineTest,ValidateRtpExtensions_OutOfRangeId_Low)76 TEST(WebRtcMediaEngineTest, ValidateRtpExtensions_OutOfRangeId_Low) {
77 std::vector<RtpExtension> extensions = MakeUniqueExtensions();
78 extensions.push_back(RtpExtension("foo", 0));
79 EXPECT_FALSE(ValidateRtpExtensions(extensions));
80 }
81
TEST(WebRtcMediaEngineTest,ValidateRtpExtensions_OutOfRangeId_High)82 TEST(WebRtcMediaEngineTest, ValidateRtpExtensions_OutOfRangeId_High) {
83 std::vector<RtpExtension> extensions = MakeUniqueExtensions();
84 extensions.push_back(RtpExtension("foo", 15));
85 EXPECT_FALSE(ValidateRtpExtensions(extensions));
86 }
87
TEST(WebRtcMediaEngineTest,ValidateRtpExtensions_OverlappingIds_StartOfSet)88 TEST(WebRtcMediaEngineTest, ValidateRtpExtensions_OverlappingIds_StartOfSet) {
89 std::vector<RtpExtension> extensions = MakeUniqueExtensions();
90 extensions.push_back(RtpExtension("foo", 1));
91 EXPECT_FALSE(ValidateRtpExtensions(extensions));
92 }
93
TEST(WebRtcMediaEngineTest,ValidateRtpExtensions_OverlappingIds_EndOfSet)94 TEST(WebRtcMediaEngineTest, ValidateRtpExtensions_OverlappingIds_EndOfSet) {
95 std::vector<RtpExtension> extensions = MakeUniqueExtensions();
96 extensions.push_back(RtpExtension("foo", 14));
97 EXPECT_FALSE(ValidateRtpExtensions(extensions));
98 }
99
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_EmptyList)100 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_EmptyList) {
101 std::vector<RtpExtension> extensions;
102 std::vector<webrtc::RtpExtension> filtered =
103 FilterRtpExtensions(extensions, SupportedExtensions1, true);
104 EXPECT_EQ(0, filtered.size());
105 }
106
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_IncludeOnlySupported)107 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_IncludeOnlySupported) {
108 std::vector<RtpExtension> extensions = MakeUniqueExtensions();
109 std::vector<webrtc::RtpExtension> filtered =
110 FilterRtpExtensions(extensions, SupportedExtensions1, false);
111 EXPECT_EQ(2, filtered.size());
112 EXPECT_EQ("c", filtered[0].uri);
113 EXPECT_EQ("i", filtered[1].uri);
114 }
115
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_SortedByName_1)116 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_SortedByName_1) {
117 std::vector<RtpExtension> extensions = MakeUniqueExtensions();
118 std::vector<webrtc::RtpExtension> filtered =
119 FilterRtpExtensions(extensions, SupportedExtensions2, false);
120 EXPECT_EQ(12, filtered.size());
121 EXPECT_TRUE(IsSorted(filtered));
122 }
123
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_SortedByName_2)124 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_SortedByName_2) {
125 std::vector<RtpExtension> extensions = MakeUniqueExtensions();
126 std::vector<webrtc::RtpExtension> filtered =
127 FilterRtpExtensions(extensions, SupportedExtensions2, true);
128 EXPECT_EQ(12, filtered.size());
129 EXPECT_TRUE(IsSorted(filtered));
130 }
131
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_DontRemoveRedundant)132 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_DontRemoveRedundant) {
133 std::vector<RtpExtension> extensions = MakeRedundantExtensions();
134 std::vector<webrtc::RtpExtension> filtered =
135 FilterRtpExtensions(extensions, SupportedExtensions2, false);
136 EXPECT_EQ(12, filtered.size());
137 EXPECT_TRUE(IsSorted(filtered));
138 EXPECT_EQ(filtered[0].uri, filtered[1].uri);
139 }
140
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_RemoveRedundant)141 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_RemoveRedundant) {
142 std::vector<RtpExtension> extensions = MakeRedundantExtensions();
143 std::vector<webrtc::RtpExtension> filtered =
144 FilterRtpExtensions(extensions, SupportedExtensions2, true);
145 EXPECT_EQ(6, filtered.size());
146 EXPECT_TRUE(IsSorted(filtered));
147 EXPECT_NE(filtered[0].uri, filtered[1].uri);
148 }
149
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_RemoveRedundantEncrypted_1)150 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_RemoveRedundantEncrypted_1) {
151 std::vector<RtpExtension> extensions;
152 extensions.push_back(webrtc::RtpExtension("b", 1));
153 extensions.push_back(webrtc::RtpExtension("b", 2, true));
154 extensions.push_back(webrtc::RtpExtension("c", 3));
155 extensions.push_back(webrtc::RtpExtension("b", 4));
156 std::vector<webrtc::RtpExtension> filtered =
157 FilterRtpExtensions(extensions, SupportedExtensions2, true);
158 EXPECT_EQ(3, filtered.size());
159 EXPECT_TRUE(IsSorted(filtered));
160 EXPECT_EQ(filtered[0].uri, filtered[1].uri);
161 EXPECT_NE(filtered[0].encrypt, filtered[1].encrypt);
162 EXPECT_NE(filtered[0].uri, filtered[2].uri);
163 EXPECT_NE(filtered[1].uri, filtered[2].uri);
164 }
165
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_RemoveRedundantEncrypted_2)166 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_RemoveRedundantEncrypted_2) {
167 std::vector<RtpExtension> extensions;
168 extensions.push_back(webrtc::RtpExtension("b", 1, true));
169 extensions.push_back(webrtc::RtpExtension("b", 2));
170 extensions.push_back(webrtc::RtpExtension("c", 3));
171 extensions.push_back(webrtc::RtpExtension("b", 4));
172 std::vector<webrtc::RtpExtension> filtered =
173 FilterRtpExtensions(extensions, SupportedExtensions2, true);
174 EXPECT_EQ(3, filtered.size());
175 EXPECT_TRUE(IsSorted(filtered));
176 EXPECT_EQ(filtered[0].uri, filtered[1].uri);
177 EXPECT_NE(filtered[0].encrypt, filtered[1].encrypt);
178 EXPECT_NE(filtered[0].uri, filtered[2].uri);
179 EXPECT_NE(filtered[1].uri, filtered[2].uri);
180 }
181
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_RemoveRedundantBwe_1)182 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_RemoveRedundantBwe_1) {
183 std::vector<RtpExtension> extensions;
184 extensions.push_back(
185 RtpExtension(RtpExtension::kTransportSequenceNumberUri, 3));
186 extensions.push_back(RtpExtension(RtpExtension::kTimestampOffsetUri, 9));
187 extensions.push_back(RtpExtension(RtpExtension::kAbsSendTimeUri, 6));
188 extensions.push_back(
189 RtpExtension(RtpExtension::kTransportSequenceNumberUri, 1));
190 extensions.push_back(RtpExtension(RtpExtension::kTimestampOffsetUri, 14));
191 std::vector<webrtc::RtpExtension> filtered =
192 FilterRtpExtensions(extensions, SupportedExtensions2, true);
193 EXPECT_EQ(1, filtered.size());
194 EXPECT_EQ(RtpExtension::kTransportSequenceNumberUri, filtered[0].uri);
195 }
196
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_RemoveRedundantBweEncrypted_1)197 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_RemoveRedundantBweEncrypted_1) {
198 std::vector<RtpExtension> extensions;
199 extensions.push_back(
200 RtpExtension(RtpExtension::kTransportSequenceNumberUri, 3));
201 extensions.push_back(
202 RtpExtension(RtpExtension::kTransportSequenceNumberUri, 4, true));
203 extensions.push_back(RtpExtension(RtpExtension::kTimestampOffsetUri, 9));
204 extensions.push_back(RtpExtension(RtpExtension::kAbsSendTimeUri, 6));
205 extensions.push_back(
206 RtpExtension(RtpExtension::kTransportSequenceNumberUri, 1));
207 extensions.push_back(
208 RtpExtension(RtpExtension::kTransportSequenceNumberUri, 2, true));
209 extensions.push_back(RtpExtension(RtpExtension::kTimestampOffsetUri, 14));
210 std::vector<webrtc::RtpExtension> filtered =
211 FilterRtpExtensions(extensions, SupportedExtensions2, true);
212 EXPECT_EQ(2, filtered.size());
213 EXPECT_EQ(RtpExtension::kTransportSequenceNumberUri, filtered[0].uri);
214 EXPECT_EQ(RtpExtension::kTransportSequenceNumberUri, filtered[1].uri);
215 EXPECT_NE(filtered[0].encrypt, filtered[1].encrypt);
216 }
217
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_RemoveRedundantBwe_2)218 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_RemoveRedundantBwe_2) {
219 std::vector<RtpExtension> extensions;
220 extensions.push_back(RtpExtension(RtpExtension::kTimestampOffsetUri, 1));
221 extensions.push_back(RtpExtension(RtpExtension::kAbsSendTimeUri, 14));
222 extensions.push_back(RtpExtension(RtpExtension::kTimestampOffsetUri, 7));
223 std::vector<webrtc::RtpExtension> filtered =
224 FilterRtpExtensions(extensions, SupportedExtensions2, true);
225 EXPECT_EQ(1, filtered.size());
226 EXPECT_EQ(RtpExtension::kAbsSendTimeUri, filtered[0].uri);
227 }
228
TEST(WebRtcMediaEngineTest,FilterRtpExtensions_RemoveRedundantBwe_3)229 TEST(WebRtcMediaEngineTest, FilterRtpExtensions_RemoveRedundantBwe_3) {
230 std::vector<RtpExtension> extensions;
231 extensions.push_back(RtpExtension(RtpExtension::kTimestampOffsetUri, 2));
232 extensions.push_back(RtpExtension(RtpExtension::kTimestampOffsetUri, 14));
233 std::vector<webrtc::RtpExtension> filtered =
234 FilterRtpExtensions(extensions, SupportedExtensions2, true);
235 EXPECT_EQ(1, filtered.size());
236 EXPECT_EQ(RtpExtension::kTimestampOffsetUri, filtered[0].uri);
237 }
238
TEST(WebRtcMediaEngineFactoryTest,CreateWithBuiltinDecoders)239 TEST(WebRtcMediaEngineFactoryTest, CreateWithBuiltinDecoders) {
240 std::unique_ptr<MediaEngineInterface> engine(WebRtcMediaEngineFactory::Create(
241 nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
242 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
243 EXPECT_TRUE(engine);
244 }
245
246 } // namespace cricket
247