1 /*
2 * Copyright (c) 2012 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 "media/base/stream_params.h"
12
13 #include <stdint.h>
14
15 #include "media/base/test_utils.h"
16 #include "rtc_base/arraysize.h"
17 #include "test/gmock.h"
18 #include "test/gtest.h"
19
20 using ::testing::Each;
21 using ::testing::Ne;
22
23 static const uint32_t kSsrcs1[] = {1};
24 static const uint32_t kSsrcs2[] = {1, 2};
25
CreateStreamParamsWithSsrcGroup(const std::string & semantics,const uint32_t ssrcs_in[],size_t len)26 static cricket::StreamParams CreateStreamParamsWithSsrcGroup(
27 const std::string& semantics,
28 const uint32_t ssrcs_in[],
29 size_t len) {
30 cricket::StreamParams stream;
31 std::vector<uint32_t> ssrcs(ssrcs_in, ssrcs_in + len);
32 cricket::SsrcGroup sg(semantics, ssrcs);
33 stream.ssrcs = ssrcs;
34 stream.ssrc_groups.push_back(sg);
35 return stream;
36 }
37
TEST(SsrcGroup,EqualNotEqual)38 TEST(SsrcGroup, EqualNotEqual) {
39 cricket::SsrcGroup ssrc_groups[] = {
40 cricket::SsrcGroup("ABC", MAKE_VECTOR(kSsrcs1)),
41 cricket::SsrcGroup("ABC", MAKE_VECTOR(kSsrcs2)),
42 cricket::SsrcGroup("Abc", MAKE_VECTOR(kSsrcs2)),
43 cricket::SsrcGroup("abc", MAKE_VECTOR(kSsrcs2)),
44 };
45
46 for (size_t i = 0; i < arraysize(ssrc_groups); ++i) {
47 for (size_t j = 0; j < arraysize(ssrc_groups); ++j) {
48 EXPECT_EQ((ssrc_groups[i] == ssrc_groups[j]), (i == j));
49 EXPECT_EQ((ssrc_groups[i] != ssrc_groups[j]), (i != j));
50 }
51 }
52 }
53
TEST(SsrcGroup,HasSemantics)54 TEST(SsrcGroup, HasSemantics) {
55 cricket::SsrcGroup sg1("ABC", MAKE_VECTOR(kSsrcs1));
56 EXPECT_TRUE(sg1.has_semantics("ABC"));
57
58 cricket::SsrcGroup sg2("Abc", MAKE_VECTOR(kSsrcs1));
59 EXPECT_FALSE(sg2.has_semantics("ABC"));
60
61 cricket::SsrcGroup sg3("abc", MAKE_VECTOR(kSsrcs1));
62 EXPECT_FALSE(sg3.has_semantics("ABC"));
63 }
64
TEST(SsrcGroup,ToString)65 TEST(SsrcGroup, ToString) {
66 cricket::SsrcGroup sg1("ABC", MAKE_VECTOR(kSsrcs1));
67 EXPECT_STREQ("{semantics:ABC;ssrcs:[1]}", sg1.ToString().c_str());
68 }
69
TEST(StreamParams,CreateLegacy)70 TEST(StreamParams, CreateLegacy) {
71 const uint32_t ssrc = 7;
72 cricket::StreamParams one_sp = cricket::StreamParams::CreateLegacy(ssrc);
73 EXPECT_EQ(1U, one_sp.ssrcs.size());
74 EXPECT_EQ(ssrc, one_sp.first_ssrc());
75 EXPECT_TRUE(one_sp.has_ssrcs());
76 EXPECT_TRUE(one_sp.has_ssrc(ssrc));
77 EXPECT_FALSE(one_sp.has_ssrc(ssrc + 1));
78 EXPECT_FALSE(one_sp.has_ssrc_groups());
79 EXPECT_EQ(0U, one_sp.ssrc_groups.size());
80 }
81
TEST(StreamParams,HasSsrcGroup)82 TEST(StreamParams, HasSsrcGroup) {
83 cricket::StreamParams sp =
84 CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, arraysize(kSsrcs2));
85 EXPECT_EQ(2U, sp.ssrcs.size());
86 EXPECT_EQ(kSsrcs2[0], sp.first_ssrc());
87 EXPECT_TRUE(sp.has_ssrcs());
88 EXPECT_TRUE(sp.has_ssrc(kSsrcs2[0]));
89 EXPECT_TRUE(sp.has_ssrc(kSsrcs2[1]));
90 EXPECT_TRUE(sp.has_ssrc_group("XYZ"));
91 EXPECT_EQ(1U, sp.ssrc_groups.size());
92 EXPECT_EQ(2U, sp.ssrc_groups[0].ssrcs.size());
93 EXPECT_EQ(kSsrcs2[0], sp.ssrc_groups[0].ssrcs[0]);
94 EXPECT_EQ(kSsrcs2[1], sp.ssrc_groups[0].ssrcs[1]);
95 }
96
TEST(StreamParams,GetSsrcGroup)97 TEST(StreamParams, GetSsrcGroup) {
98 cricket::StreamParams sp =
99 CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, arraysize(kSsrcs2));
100 EXPECT_EQ(NULL, sp.get_ssrc_group("xyz"));
101 EXPECT_EQ(&sp.ssrc_groups[0], sp.get_ssrc_group("XYZ"));
102 }
103
TEST(StreamParams,HasStreamWithNoSsrcs)104 TEST(StreamParams, HasStreamWithNoSsrcs) {
105 cricket::StreamParams sp_1 = cricket::StreamParams::CreateLegacy(kSsrcs1[0]);
106 cricket::StreamParams sp_2 = cricket::StreamParams::CreateLegacy(kSsrcs2[0]);
107 std::vector<cricket::StreamParams> streams({sp_1, sp_2});
108 EXPECT_FALSE(HasStreamWithNoSsrcs(streams));
109
110 cricket::StreamParams unsignaled_stream;
111 streams.push_back(unsignaled_stream);
112 EXPECT_TRUE(HasStreamWithNoSsrcs(streams));
113 }
114
TEST(StreamParams,EqualNotEqual)115 TEST(StreamParams, EqualNotEqual) {
116 cricket::StreamParams l1 = cricket::StreamParams::CreateLegacy(1);
117 cricket::StreamParams l2 = cricket::StreamParams::CreateLegacy(2);
118 cricket::StreamParams sg1 =
119 CreateStreamParamsWithSsrcGroup("ABC", kSsrcs1, arraysize(kSsrcs1));
120 cricket::StreamParams sg2 =
121 CreateStreamParamsWithSsrcGroup("ABC", kSsrcs2, arraysize(kSsrcs2));
122 cricket::StreamParams sg3 =
123 CreateStreamParamsWithSsrcGroup("Abc", kSsrcs2, arraysize(kSsrcs2));
124 cricket::StreamParams sg4 =
125 CreateStreamParamsWithSsrcGroup("abc", kSsrcs2, arraysize(kSsrcs2));
126 cricket::StreamParams sps[] = {l1, l2, sg1, sg2, sg3, sg4};
127
128 for (size_t i = 0; i < arraysize(sps); ++i) {
129 for (size_t j = 0; j < arraysize(sps); ++j) {
130 EXPECT_EQ((sps[i] == sps[j]), (i == j));
131 EXPECT_EQ((sps[i] != sps[j]), (i != j));
132 }
133 }
134 }
135
TEST(StreamParams,FidFunctions)136 TEST(StreamParams, FidFunctions) {
137 uint32_t fid_ssrc;
138
139 cricket::StreamParams sp = cricket::StreamParams::CreateLegacy(1);
140 EXPECT_FALSE(sp.AddFidSsrc(10, 20));
141 EXPECT_TRUE(sp.AddFidSsrc(1, 2));
142 EXPECT_TRUE(sp.GetFidSsrc(1, &fid_ssrc));
143 EXPECT_EQ(2u, fid_ssrc);
144 EXPECT_FALSE(sp.GetFidSsrc(15, &fid_ssrc));
145
146 sp.add_ssrc(20);
147 EXPECT_TRUE(sp.AddFidSsrc(20, 30));
148 EXPECT_TRUE(sp.GetFidSsrc(20, &fid_ssrc));
149 EXPECT_EQ(30u, fid_ssrc);
150
151 // Manually create SsrcGroup to test bounds-checking
152 // in GetSecondarySsrc. We construct an invalid StreamParams
153 // for this.
154 std::vector<uint32_t> fid_vector;
155 fid_vector.push_back(13);
156 cricket::SsrcGroup invalid_fid_group(cricket::kFidSsrcGroupSemantics,
157 fid_vector);
158 cricket::StreamParams sp_invalid;
159 sp_invalid.add_ssrc(13);
160 sp_invalid.ssrc_groups.push_back(invalid_fid_group);
161 EXPECT_FALSE(sp_invalid.GetFidSsrc(13, &fid_ssrc));
162 }
163
TEST(StreamParams,GetPrimaryAndFidSsrcs)164 TEST(StreamParams, GetPrimaryAndFidSsrcs) {
165 cricket::StreamParams sp;
166 sp.ssrcs.push_back(1);
167 sp.ssrcs.push_back(2);
168 sp.ssrcs.push_back(3);
169
170 std::vector<uint32_t> primary_ssrcs;
171 sp.GetPrimarySsrcs(&primary_ssrcs);
172 std::vector<uint32_t> fid_ssrcs;
173 sp.GetFidSsrcs(primary_ssrcs, &fid_ssrcs);
174 ASSERT_EQ(1u, primary_ssrcs.size());
175 EXPECT_EQ(1u, primary_ssrcs[0]);
176 ASSERT_EQ(0u, fid_ssrcs.size());
177
178 sp.ssrc_groups.push_back(
179 cricket::SsrcGroup(cricket::kSimSsrcGroupSemantics, sp.ssrcs));
180 sp.AddFidSsrc(1, 10);
181 sp.AddFidSsrc(2, 20);
182
183 primary_ssrcs.clear();
184 sp.GetPrimarySsrcs(&primary_ssrcs);
185 fid_ssrcs.clear();
186 sp.GetFidSsrcs(primary_ssrcs, &fid_ssrcs);
187 ASSERT_EQ(3u, primary_ssrcs.size());
188 EXPECT_EQ(1u, primary_ssrcs[0]);
189 EXPECT_EQ(2u, primary_ssrcs[1]);
190 EXPECT_EQ(3u, primary_ssrcs[2]);
191 ASSERT_EQ(2u, fid_ssrcs.size());
192 EXPECT_EQ(10u, fid_ssrcs[0]);
193 EXPECT_EQ(20u, fid_ssrcs[1]);
194 }
195
TEST(StreamParams,FecFrFunctions)196 TEST(StreamParams, FecFrFunctions) {
197 uint32_t fecfr_ssrc;
198
199 cricket::StreamParams sp = cricket::StreamParams::CreateLegacy(1);
200 EXPECT_FALSE(sp.AddFecFrSsrc(10, 20));
201 EXPECT_TRUE(sp.AddFecFrSsrc(1, 2));
202 EXPECT_TRUE(sp.GetFecFrSsrc(1, &fecfr_ssrc));
203 EXPECT_EQ(2u, fecfr_ssrc);
204 EXPECT_FALSE(sp.GetFecFrSsrc(15, &fecfr_ssrc));
205
206 sp.add_ssrc(20);
207 EXPECT_TRUE(sp.AddFecFrSsrc(20, 30));
208 EXPECT_TRUE(sp.GetFecFrSsrc(20, &fecfr_ssrc));
209 EXPECT_EQ(30u, fecfr_ssrc);
210
211 // Manually create SsrcGroup to test bounds-checking
212 // in GetSecondarySsrc. We construct an invalid StreamParams
213 // for this.
214 std::vector<uint32_t> fecfr_vector;
215 fecfr_vector.push_back(13);
216 cricket::SsrcGroup invalid_fecfr_group(cricket::kFecFrSsrcGroupSemantics,
217 fecfr_vector);
218 cricket::StreamParams sp_invalid;
219 sp_invalid.add_ssrc(13);
220 sp_invalid.ssrc_groups.push_back(invalid_fecfr_group);
221 EXPECT_FALSE(sp_invalid.GetFecFrSsrc(13, &fecfr_ssrc));
222 }
223
TEST(StreamParams,ToString)224 TEST(StreamParams, ToString) {
225 cricket::StreamParams sp =
226 CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, arraysize(kSsrcs2));
227 sp.set_stream_ids({"stream_id"});
228 EXPECT_STREQ(
229 "{ssrcs:[1,2];ssrc_groups:{semantics:XYZ;ssrcs:[1,2]};stream_ids:stream_"
230 "id;}",
231 sp.ToString().c_str());
232 }
233
TEST(StreamParams,TestGenerateSsrcs_SingleStreamWithRtxAndFlex)234 TEST(StreamParams, TestGenerateSsrcs_SingleStreamWithRtxAndFlex) {
235 rtc::UniqueRandomIdGenerator generator;
236 cricket::StreamParams stream;
237 stream.GenerateSsrcs(1, true, true, &generator);
238 uint32_t primary_ssrc = stream.first_ssrc();
239 ASSERT_NE(0u, primary_ssrc);
240 uint32_t rtx_ssrc = 0;
241 uint32_t flex_ssrc = 0;
242 EXPECT_EQ(3u, stream.ssrcs.size());
243 EXPECT_TRUE(stream.GetFidSsrc(primary_ssrc, &rtx_ssrc));
244 EXPECT_NE(0u, rtx_ssrc);
245 EXPECT_TRUE(stream.GetFecFrSsrc(primary_ssrc, &flex_ssrc));
246 EXPECT_NE(0u, flex_ssrc);
247 EXPECT_FALSE(stream.has_ssrc_group(cricket::kSimSsrcGroupSemantics));
248 EXPECT_TRUE(stream.has_ssrc_group(cricket::kFidSsrcGroupSemantics));
249 EXPECT_TRUE(stream.has_ssrc_group(cricket::kFecFrSsrcGroupSemantics));
250 }
251
TEST(StreamParams,TestGenerateSsrcs_SingleStreamWithRtx)252 TEST(StreamParams, TestGenerateSsrcs_SingleStreamWithRtx) {
253 rtc::UniqueRandomIdGenerator generator;
254 cricket::StreamParams stream;
255 stream.GenerateSsrcs(1, true, false, &generator);
256 uint32_t primary_ssrc = stream.first_ssrc();
257 ASSERT_NE(0u, primary_ssrc);
258 uint32_t rtx_ssrc = 0;
259 uint32_t flex_ssrc = 0;
260 EXPECT_EQ(2u, stream.ssrcs.size());
261 EXPECT_TRUE(stream.GetFidSsrc(primary_ssrc, &rtx_ssrc));
262 EXPECT_NE(0u, rtx_ssrc);
263 EXPECT_FALSE(stream.GetFecFrSsrc(primary_ssrc, &flex_ssrc));
264 EXPECT_EQ(0u, flex_ssrc);
265 EXPECT_FALSE(stream.has_ssrc_group(cricket::kSimSsrcGroupSemantics));
266 EXPECT_TRUE(stream.has_ssrc_group(cricket::kFidSsrcGroupSemantics));
267 }
268
TEST(StreamParams,TestGenerateSsrcs_SingleStreamWithFlex)269 TEST(StreamParams, TestGenerateSsrcs_SingleStreamWithFlex) {
270 rtc::UniqueRandomIdGenerator generator;
271 cricket::StreamParams stream;
272 stream.GenerateSsrcs(1, false, true, &generator);
273 uint32_t primary_ssrc = stream.first_ssrc();
274 ASSERT_NE(0u, primary_ssrc);
275 uint32_t rtx_ssrc = 0;
276 uint32_t flex_ssrc = 0;
277 EXPECT_EQ(2u, stream.ssrcs.size());
278 EXPECT_FALSE(stream.GetFidSsrc(primary_ssrc, &rtx_ssrc));
279 EXPECT_EQ(0u, rtx_ssrc);
280 EXPECT_TRUE(stream.GetFecFrSsrc(primary_ssrc, &flex_ssrc));
281 EXPECT_NE(0u, flex_ssrc);
282 EXPECT_FALSE(stream.has_ssrc_group(cricket::kSimSsrcGroupSemantics));
283 EXPECT_TRUE(stream.has_ssrc_group(cricket::kFecFrSsrcGroupSemantics));
284 }
285
TEST(StreamParams,TestGenerateSsrcs_SimulcastLayersAndRtx)286 TEST(StreamParams, TestGenerateSsrcs_SimulcastLayersAndRtx) {
287 const size_t kNumStreams = 3;
288 rtc::UniqueRandomIdGenerator generator;
289 cricket::StreamParams stream;
290 stream.GenerateSsrcs(kNumStreams, true, false, &generator);
291 EXPECT_EQ(kNumStreams * 2, stream.ssrcs.size());
292 std::vector<uint32_t> primary_ssrcs, rtx_ssrcs;
293 stream.GetPrimarySsrcs(&primary_ssrcs);
294 EXPECT_EQ(kNumStreams, primary_ssrcs.size());
295 EXPECT_THAT(primary_ssrcs, Each(Ne(0u)));
296 stream.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs);
297 EXPECT_EQ(kNumStreams, rtx_ssrcs.size());
298 EXPECT_THAT(rtx_ssrcs, Each(Ne(0u)));
299 EXPECT_TRUE(stream.has_ssrc_group(cricket::kSimSsrcGroupSemantics));
300 EXPECT_TRUE(stream.has_ssrc_group(cricket::kFidSsrcGroupSemantics));
301 }
302