• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libjingle
3  * Copyright 2012 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "talk/media/base/streamparams.h"
29 #include "talk/media/base/testutils.h"
30 #include "webrtc/base/arraysize.h"
31 #include "webrtc/base/gunit.h"
32 
33 static const uint32_t kSsrcs1[] = {1};
34 static const uint32_t kSsrcs2[] = {1, 2};
35 static const uint32_t kSsrcs3[] = {1, 2, 3};
36 static const uint32_t kRtxSsrcs3[] = {4, 5, 6};
37 
CreateStreamParamsWithSsrcGroup(const std::string & semantics,const uint32_t ssrcs_in[],size_t len)38 static cricket::StreamParams CreateStreamParamsWithSsrcGroup(
39     const std::string& semantics,
40     const uint32_t ssrcs_in[],
41     size_t len) {
42   cricket::StreamParams stream;
43   std::vector<uint32_t> ssrcs(ssrcs_in, ssrcs_in + len);
44   cricket::SsrcGroup sg(semantics, ssrcs);
45   stream.ssrcs = ssrcs;
46   stream.ssrc_groups.push_back(sg);
47   return stream;
48 }
49 
TEST(SsrcGroup,EqualNotEqual)50 TEST(SsrcGroup, EqualNotEqual) {
51   cricket::SsrcGroup ssrc_groups[] = {
52     cricket::SsrcGroup("ABC", MAKE_VECTOR(kSsrcs1)),
53     cricket::SsrcGroup("ABC", MAKE_VECTOR(kSsrcs2)),
54     cricket::SsrcGroup("Abc", MAKE_VECTOR(kSsrcs2)),
55     cricket::SsrcGroup("abc", MAKE_VECTOR(kSsrcs2)),
56   };
57 
58   for (size_t i = 0; i < arraysize(ssrc_groups); ++i) {
59     for (size_t j = 0; j < arraysize(ssrc_groups); ++j) {
60       EXPECT_EQ((ssrc_groups[i] == ssrc_groups[j]), (i == j));
61       EXPECT_EQ((ssrc_groups[i] != ssrc_groups[j]), (i != j));
62     }
63   }
64 }
65 
TEST(SsrcGroup,HasSemantics)66 TEST(SsrcGroup, HasSemantics) {
67   cricket::SsrcGroup sg1("ABC", MAKE_VECTOR(kSsrcs1));
68   EXPECT_TRUE(sg1.has_semantics("ABC"));
69 
70   cricket::SsrcGroup sg2("Abc", MAKE_VECTOR(kSsrcs1));
71   EXPECT_FALSE(sg2.has_semantics("ABC"));
72 
73   cricket::SsrcGroup sg3("abc", MAKE_VECTOR(kSsrcs1));
74   EXPECT_FALSE(sg3.has_semantics("ABC"));
75 }
76 
TEST(SsrcGroup,ToString)77 TEST(SsrcGroup, ToString) {
78   cricket::SsrcGroup sg1("ABC", MAKE_VECTOR(kSsrcs1));
79   EXPECT_STREQ("{semantics:ABC;ssrcs:[1]}", sg1.ToString().c_str());
80 }
81 
TEST(StreamParams,CreateLegacy)82 TEST(StreamParams, CreateLegacy) {
83   const uint32_t ssrc = 7;
84   cricket::StreamParams one_sp = cricket::StreamParams::CreateLegacy(ssrc);
85   EXPECT_EQ(1U, one_sp.ssrcs.size());
86   EXPECT_EQ(ssrc, one_sp.first_ssrc());
87   EXPECT_TRUE(one_sp.has_ssrcs());
88   EXPECT_TRUE(one_sp.has_ssrc(ssrc));
89   EXPECT_FALSE(one_sp.has_ssrc(ssrc+1));
90   EXPECT_FALSE(one_sp.has_ssrc_groups());
91   EXPECT_EQ(0U, one_sp.ssrc_groups.size());
92 }
93 
TEST(StreamParams,HasSsrcGroup)94 TEST(StreamParams, HasSsrcGroup) {
95   cricket::StreamParams sp =
96       CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, arraysize(kSsrcs2));
97   EXPECT_EQ(2U, sp.ssrcs.size());
98   EXPECT_EQ(kSsrcs2[0], sp.first_ssrc());
99   EXPECT_TRUE(sp.has_ssrcs());
100   EXPECT_TRUE(sp.has_ssrc(kSsrcs2[0]));
101   EXPECT_TRUE(sp.has_ssrc(kSsrcs2[1]));
102   EXPECT_TRUE(sp.has_ssrc_group("XYZ"));
103   EXPECT_EQ(1U, sp.ssrc_groups.size());
104   EXPECT_EQ(2U, sp.ssrc_groups[0].ssrcs.size());
105   EXPECT_EQ(kSsrcs2[0], sp.ssrc_groups[0].ssrcs[0]);
106   EXPECT_EQ(kSsrcs2[1], sp.ssrc_groups[0].ssrcs[1]);
107 }
108 
TEST(StreamParams,GetSsrcGroup)109 TEST(StreamParams, GetSsrcGroup) {
110   cricket::StreamParams sp =
111       CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, arraysize(kSsrcs2));
112   EXPECT_EQ(NULL, sp.get_ssrc_group("xyz"));
113   EXPECT_EQ(&sp.ssrc_groups[0], sp.get_ssrc_group("XYZ"));
114 }
115 
TEST(StreamParams,EqualNotEqual)116 TEST(StreamParams, EqualNotEqual) {
117   cricket::StreamParams l1 = cricket::StreamParams::CreateLegacy(1);
118   cricket::StreamParams l2 = cricket::StreamParams::CreateLegacy(2);
119   cricket::StreamParams sg1 =
120       CreateStreamParamsWithSsrcGroup("ABC", kSsrcs1, arraysize(kSsrcs1));
121   cricket::StreamParams sg2 =
122       CreateStreamParamsWithSsrcGroup("ABC", kSsrcs2, arraysize(kSsrcs2));
123   cricket::StreamParams sg3 =
124       CreateStreamParamsWithSsrcGroup("Abc", kSsrcs2, arraysize(kSsrcs2));
125   cricket::StreamParams sg4 =
126       CreateStreamParamsWithSsrcGroup("abc", kSsrcs2, arraysize(kSsrcs2));
127   cricket::StreamParams sps[] = {l1, l2, sg1, sg2, sg3, sg4};
128 
129   for (size_t i = 0; i < arraysize(sps); ++i) {
130     for (size_t j = 0; j < arraysize(sps); ++j) {
131       EXPECT_EQ((sps[i] == sps[j]), (i == j));
132       EXPECT_EQ((sps[i] != sps[j]), (i != j));
133     }
134   }
135 }
136 
TEST(StreamParams,FidFunctions)137 TEST(StreamParams, FidFunctions) {
138   uint32_t fid_ssrc;
139 
140   cricket::StreamParams sp = cricket::StreamParams::CreateLegacy(1);
141   EXPECT_FALSE(sp.AddFidSsrc(10, 20));
142   EXPECT_TRUE(sp.AddFidSsrc(1, 2));
143   EXPECT_TRUE(sp.GetFidSsrc(1, &fid_ssrc));
144   EXPECT_EQ(2u, fid_ssrc);
145   EXPECT_FALSE(sp.GetFidSsrc(15, &fid_ssrc));
146 
147   sp.add_ssrc(20);
148   sp.AddFidSsrc(20, 30);
149   EXPECT_TRUE(sp.GetFidSsrc(20, &fid_ssrc));
150   EXPECT_EQ(30u, fid_ssrc);
151 
152   // Manually create SsrcGroup to test bounds-checking
153   // in GetSecondarySsrc. We construct an invalid StreamParams
154   // for this.
155   std::vector<uint32_t> fid_vector;
156   fid_vector.push_back(13);
157   cricket::SsrcGroup invalid_fid_group(cricket::kFidSsrcGroupSemantics,
158                                         fid_vector);
159   cricket::StreamParams sp_invalid;
160   sp_invalid.add_ssrc(13);
161   sp_invalid.ssrc_groups.push_back(invalid_fid_group);
162   EXPECT_FALSE(sp_invalid.GetFidSsrc(13, &fid_ssrc));
163 }
164 
TEST(StreamParams,GetPrimaryAndFidSsrcs)165 TEST(StreamParams, GetPrimaryAndFidSsrcs) {
166   cricket::StreamParams sp;
167   sp.ssrcs.push_back(1);
168   sp.ssrcs.push_back(2);
169   sp.ssrcs.push_back(3);
170 
171   std::vector<uint32_t> primary_ssrcs;
172   sp.GetPrimarySsrcs(&primary_ssrcs);
173   std::vector<uint32_t> fid_ssrcs;
174   sp.GetFidSsrcs(primary_ssrcs, &fid_ssrcs);
175   ASSERT_EQ(1u, primary_ssrcs.size());
176   EXPECT_EQ(1u, primary_ssrcs[0]);
177   ASSERT_EQ(0u, fid_ssrcs.size());
178 
179   sp.ssrc_groups.push_back(
180       cricket::SsrcGroup(cricket::kSimSsrcGroupSemantics, sp.ssrcs));
181   sp.AddFidSsrc(1, 10);
182   sp.AddFidSsrc(2, 20);
183 
184   primary_ssrcs.clear();
185   sp.GetPrimarySsrcs(&primary_ssrcs);
186   fid_ssrcs.clear();
187   sp.GetFidSsrcs(primary_ssrcs, &fid_ssrcs);
188   ASSERT_EQ(3u, primary_ssrcs.size());
189   EXPECT_EQ(1u, primary_ssrcs[0]);
190   EXPECT_EQ(2u, primary_ssrcs[1]);
191   EXPECT_EQ(3u, primary_ssrcs[2]);
192   ASSERT_EQ(2u, fid_ssrcs.size());
193   EXPECT_EQ(10u, fid_ssrcs[0]);
194   EXPECT_EQ(20u, fid_ssrcs[1]);
195 }
196 
TEST(StreamParams,ToString)197 TEST(StreamParams, ToString) {
198   cricket::StreamParams sp =
199       CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, arraysize(kSsrcs2));
200   EXPECT_STREQ("{ssrcs:[1,2];ssrc_groups:{semantics:XYZ;ssrcs:[1,2]};}",
201                sp.ToString().c_str());
202 }
203 
204 
TEST(StreamParams,TestIsOneSsrcStream_LegacyStream)205 TEST(StreamParams, TestIsOneSsrcStream_LegacyStream) {
206   EXPECT_TRUE(
207       cricket::IsOneSsrcStream(cricket::StreamParams::CreateLegacy(13)));
208 }
209 
TEST(StreamParams,TestIsOneSsrcStream_SingleRtxStream)210 TEST(StreamParams, TestIsOneSsrcStream_SingleRtxStream) {
211   cricket::StreamParams stream;
212   stream.add_ssrc(13);
213   EXPECT_TRUE(stream.AddFidSsrc(13, 14));
214   EXPECT_TRUE(cricket::IsOneSsrcStream(stream));
215 }
216 
TEST(StreamParams,TestIsOneSsrcStream_SimulcastStream)217 TEST(StreamParams, TestIsOneSsrcStream_SimulcastStream) {
218   EXPECT_FALSE(cricket::IsOneSsrcStream(
219       cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs2))));
220   EXPECT_FALSE(cricket::IsOneSsrcStream(
221       cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs3))));
222 }
223 
TEST(StreamParams,TestIsOneSsrcStream_SimRtxStream)224 TEST(StreamParams, TestIsOneSsrcStream_SimRtxStream) {
225   cricket::StreamParams stream =
226       cricket::CreateSimWithRtxStreamParams("cname",
227                                             MAKE_VECTOR(kSsrcs3),
228                                             MAKE_VECTOR(kRtxSsrcs3));
229   EXPECT_FALSE(cricket::IsOneSsrcStream(stream));
230 }
231 
TEST(StreamParams,TestIsSimulcastStream_LegacyStream)232 TEST(StreamParams, TestIsSimulcastStream_LegacyStream) {
233   EXPECT_FALSE(
234       cricket::IsSimulcastStream(cricket::StreamParams::CreateLegacy(13)));
235 }
236 
TEST(StreamParams,TestIsSimulcastStream_SingleRtxStream)237 TEST(StreamParams, TestIsSimulcastStream_SingleRtxStream) {
238   cricket::StreamParams stream;
239   stream.add_ssrc(13);
240   EXPECT_TRUE(stream.AddFidSsrc(13, 14));
241   EXPECT_FALSE(cricket::IsSimulcastStream(stream));
242 }
243 
TEST(StreamParams,TestIsSimulcastStream_SimulcastStream)244 TEST(StreamParams, TestIsSimulcastStream_SimulcastStream) {
245   EXPECT_TRUE(cricket::IsSimulcastStream(
246       cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs2))));
247   EXPECT_TRUE(cricket::IsSimulcastStream(
248       cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs3))));
249 }
250 
TEST(StreamParams,TestIsSimulcastStream_SimRtxStream)251 TEST(StreamParams, TestIsSimulcastStream_SimRtxStream) {
252   cricket::StreamParams stream =
253       cricket::CreateSimWithRtxStreamParams("cname",
254                                             MAKE_VECTOR(kSsrcs3),
255                                             MAKE_VECTOR(kRtxSsrcs3));
256   EXPECT_TRUE(cricket::IsSimulcastStream(stream));
257 }
258 
TEST(StreamParams,TestIsSimulcastStream_InvalidStreams)259 TEST(StreamParams, TestIsSimulcastStream_InvalidStreams) {
260   // stream1 has extra non-sim, non-fid ssrc.
261   cricket::StreamParams stream1 =
262       cricket::CreateSimWithRtxStreamParams("cname",
263                                             MAKE_VECTOR(kSsrcs3),
264                                             MAKE_VECTOR(kRtxSsrcs3));
265   stream1.add_ssrc(25);
266   EXPECT_FALSE(cricket::IsSimulcastStream(stream1));
267 
268   // stream2 has invalid fid-group (no primary).
269   cricket::StreamParams stream2;
270   stream2.add_ssrc(13);
271   EXPECT_TRUE(stream2.AddFidSsrc(13, 14));
272   std::remove(stream2.ssrcs.begin(), stream2.ssrcs.end(), 13);
273   EXPECT_FALSE(cricket::IsSimulcastStream(stream2));
274 
275   // stream3 has two SIM groups.
276   cricket::StreamParams stream3 =
277       cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs2));
278   std::vector<uint32_t> sim_ssrcs = MAKE_VECTOR(kRtxSsrcs3);
279   cricket::SsrcGroup sg(cricket::kSimSsrcGroupSemantics, sim_ssrcs);
280   for (size_t i = 0; i < sim_ssrcs.size(); i++) {
281     stream3.add_ssrc(sim_ssrcs[i]);
282   }
283   stream3.ssrc_groups.push_back(sg);
284   EXPECT_FALSE(cricket::IsSimulcastStream(stream3));
285 }
286