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/base/gunit.h" 29 #include "talk/media/base/streamparams.h" 30 #include "talk/media/base/testutils.h" 31 32 static const uint32 kSsrcs1[] = {1}; 33 static const uint32 kSsrcs2[] = {1, 2}; 34 static const uint32 kSsrcs3[] = {1, 2, 3}; 35 static const uint32 kRtxSsrcs3[] = {4, 5, 6}; 36 37 static cricket::StreamParams CreateStreamParamsWithSsrcGroup( 38 const std::string& semantics, const uint32 ssrcs_in[], size_t len) { 39 cricket::StreamParams stream; 40 std::vector<uint32> ssrcs(ssrcs_in, ssrcs_in + len); 41 cricket::SsrcGroup sg(semantics, ssrcs); 42 stream.ssrcs = ssrcs; 43 stream.ssrc_groups.push_back(sg); 44 return stream; 45 } 46 47 TEST(SsrcGroup, EqualNotEqual) { 48 cricket::SsrcGroup ssrc_groups[] = { 49 cricket::SsrcGroup("ABC", MAKE_VECTOR(kSsrcs1)), 50 cricket::SsrcGroup("ABC", MAKE_VECTOR(kSsrcs2)), 51 cricket::SsrcGroup("Abc", MAKE_VECTOR(kSsrcs2)), 52 cricket::SsrcGroup("abc", MAKE_VECTOR(kSsrcs2)), 53 }; 54 55 for (size_t i = 0; i < ARRAY_SIZE(ssrc_groups); ++i) { 56 for (size_t j = 0; j < ARRAY_SIZE(ssrc_groups); ++j) { 57 EXPECT_EQ((ssrc_groups[i] == ssrc_groups[j]), (i == j)); 58 EXPECT_EQ((ssrc_groups[i] != ssrc_groups[j]), (i != j)); 59 } 60 } 61 } 62 63 TEST(SsrcGroup, HasSemantics) { 64 cricket::SsrcGroup sg1("ABC", MAKE_VECTOR(kSsrcs1)); 65 EXPECT_TRUE(sg1.has_semantics("ABC")); 66 67 cricket::SsrcGroup sg2("Abc", MAKE_VECTOR(kSsrcs1)); 68 EXPECT_FALSE(sg2.has_semantics("ABC")); 69 70 cricket::SsrcGroup sg3("abc", MAKE_VECTOR(kSsrcs1)); 71 EXPECT_FALSE(sg3.has_semantics("ABC")); 72 } 73 74 TEST(SsrcGroup, ToString) { 75 cricket::SsrcGroup sg1("ABC", MAKE_VECTOR(kSsrcs1)); 76 EXPECT_STREQ("{semantics:ABC;ssrcs:[1]}", sg1.ToString().c_str()); 77 } 78 79 TEST(StreamParams, CreateLegacy) { 80 const uint32 ssrc = 7; 81 cricket::StreamParams one_sp = cricket::StreamParams::CreateLegacy(ssrc); 82 EXPECT_EQ(1U, one_sp.ssrcs.size()); 83 EXPECT_EQ(ssrc, one_sp.first_ssrc()); 84 EXPECT_TRUE(one_sp.has_ssrcs()); 85 EXPECT_TRUE(one_sp.has_ssrc(ssrc)); 86 EXPECT_FALSE(one_sp.has_ssrc(ssrc+1)); 87 EXPECT_FALSE(one_sp.has_ssrc_groups()); 88 EXPECT_EQ(0U, one_sp.ssrc_groups.size()); 89 } 90 91 TEST(StreamParams, HasSsrcGroup) { 92 cricket::StreamParams sp = 93 CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, ARRAY_SIZE(kSsrcs2)); 94 EXPECT_EQ(2U, sp.ssrcs.size()); 95 EXPECT_EQ(kSsrcs2[0], sp.first_ssrc()); 96 EXPECT_TRUE(sp.has_ssrcs()); 97 EXPECT_TRUE(sp.has_ssrc(kSsrcs2[0])); 98 EXPECT_TRUE(sp.has_ssrc(kSsrcs2[1])); 99 EXPECT_TRUE(sp.has_ssrc_group("XYZ")); 100 EXPECT_EQ(1U, sp.ssrc_groups.size()); 101 EXPECT_EQ(2U, sp.ssrc_groups[0].ssrcs.size()); 102 EXPECT_EQ(kSsrcs2[0], sp.ssrc_groups[0].ssrcs[0]); 103 EXPECT_EQ(kSsrcs2[1], sp.ssrc_groups[0].ssrcs[1]); 104 } 105 106 TEST(StreamParams, GetSsrcGroup) { 107 cricket::StreamParams sp = 108 CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, ARRAY_SIZE(kSsrcs2)); 109 EXPECT_EQ(NULL, sp.get_ssrc_group("xyz")); 110 EXPECT_EQ(&sp.ssrc_groups[0], sp.get_ssrc_group("XYZ")); 111 } 112 113 TEST(StreamParams, EqualNotEqual) { 114 cricket::StreamParams l1 = cricket::StreamParams::CreateLegacy(1); 115 cricket::StreamParams l2 = cricket::StreamParams::CreateLegacy(2); 116 cricket::StreamParams sg1 = 117 CreateStreamParamsWithSsrcGroup("ABC", kSsrcs1, ARRAY_SIZE(kSsrcs1)); 118 cricket::StreamParams sg2 = 119 CreateStreamParamsWithSsrcGroup("ABC", kSsrcs2, ARRAY_SIZE(kSsrcs2)); 120 cricket::StreamParams sg3 = 121 CreateStreamParamsWithSsrcGroup("Abc", kSsrcs2, ARRAY_SIZE(kSsrcs2)); 122 cricket::StreamParams sg4 = 123 CreateStreamParamsWithSsrcGroup("abc", kSsrcs2, ARRAY_SIZE(kSsrcs2)); 124 cricket::StreamParams sps[] = {l1, l2, sg1, sg2, sg3, sg4}; 125 126 for (size_t i = 0; i < ARRAY_SIZE(sps); ++i) { 127 for (size_t j = 0; j < ARRAY_SIZE(sps); ++j) { 128 EXPECT_EQ((sps[i] == sps[j]), (i == j)); 129 EXPECT_EQ((sps[i] != sps[j]), (i != j)); 130 } 131 } 132 } 133 134 TEST(StreamParams, FidFunctions) { 135 uint32 fid_ssrc; 136 137 cricket::StreamParams sp = cricket::StreamParams::CreateLegacy(1); 138 EXPECT_FALSE(sp.AddFidSsrc(10, 20)); 139 EXPECT_TRUE(sp.AddFidSsrc(1, 2)); 140 EXPECT_TRUE(sp.GetFidSsrc(1, &fid_ssrc)); 141 EXPECT_EQ(2u, fid_ssrc); 142 EXPECT_FALSE(sp.GetFidSsrc(15, &fid_ssrc)); 143 144 sp.add_ssrc(20); 145 sp.AddFidSsrc(20, 30); 146 EXPECT_TRUE(sp.GetFidSsrc(20, &fid_ssrc)); 147 EXPECT_EQ(30u, fid_ssrc); 148 149 // Manually create SsrcGroup to test bounds-checking 150 // in GetSecondarySsrc. We construct an invalid StreamParams 151 // for this. 152 std::vector<uint32> fid_vector; 153 fid_vector.push_back(13); 154 cricket::SsrcGroup invalid_fid_group(cricket::kFidSsrcGroupSemantics, 155 fid_vector); 156 cricket::StreamParams sp_invalid; 157 sp_invalid.add_ssrc(13); 158 sp_invalid.ssrc_groups.push_back(invalid_fid_group); 159 EXPECT_FALSE(sp_invalid.GetFidSsrc(13, &fid_ssrc)); 160 } 161 162 TEST(StreamParams, ToString) { 163 cricket::StreamParams sp = 164 CreateStreamParamsWithSsrcGroup("XYZ", kSsrcs2, ARRAY_SIZE(kSsrcs2)); 165 EXPECT_STREQ("{ssrcs:[1,2];ssrc_groups:{semantics:XYZ;ssrcs:[1,2]};}", 166 sp.ToString().c_str()); 167 } 168 169 170 TEST(StreamParams, TestIsOneSsrcStream_LegacyStream) { 171 EXPECT_TRUE( 172 cricket::IsOneSsrcStream(cricket::StreamParams::CreateLegacy(13))); 173 } 174 175 TEST(StreamParams, TestIsOneSsrcStream_SingleRtxStream) { 176 cricket::StreamParams stream; 177 stream.add_ssrc(13); 178 EXPECT_TRUE(stream.AddFidSsrc(13, 14)); 179 EXPECT_TRUE(cricket::IsOneSsrcStream(stream)); 180 } 181 182 TEST(StreamParams, TestIsOneSsrcStream_SimulcastStream) { 183 EXPECT_FALSE(cricket::IsOneSsrcStream( 184 cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs2)))); 185 EXPECT_FALSE(cricket::IsOneSsrcStream( 186 cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs3)))); 187 } 188 189 TEST(StreamParams, TestIsOneSsrcStream_SimRtxStream) { 190 cricket::StreamParams stream = 191 cricket::CreateSimWithRtxStreamParams("cname", 192 MAKE_VECTOR(kSsrcs3), 193 MAKE_VECTOR(kRtxSsrcs3)); 194 EXPECT_FALSE(cricket::IsOneSsrcStream(stream)); 195 } 196 197 TEST(StreamParams, TestIsSimulcastStream_LegacyStream) { 198 EXPECT_FALSE( 199 cricket::IsSimulcastStream(cricket::StreamParams::CreateLegacy(13))); 200 } 201 202 TEST(StreamParams, TestIsSimulcastStream_SingleRtxStream) { 203 cricket::StreamParams stream; 204 stream.add_ssrc(13); 205 EXPECT_TRUE(stream.AddFidSsrc(13, 14)); 206 EXPECT_FALSE(cricket::IsSimulcastStream(stream)); 207 } 208 209 TEST(StreamParams, TestIsSimulcastStream_SimulcastStream) { 210 EXPECT_TRUE(cricket::IsSimulcastStream( 211 cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs2)))); 212 EXPECT_TRUE(cricket::IsSimulcastStream( 213 cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs3)))); 214 } 215 216 TEST(StreamParams, TestIsSimulcastStream_SimRtxStream) { 217 cricket::StreamParams stream = 218 cricket::CreateSimWithRtxStreamParams("cname", 219 MAKE_VECTOR(kSsrcs3), 220 MAKE_VECTOR(kRtxSsrcs3)); 221 EXPECT_TRUE(cricket::IsSimulcastStream(stream)); 222 } 223 224 TEST(StreamParams, TestIsSimulcastStream_InvalidStreams) { 225 // stream1 has extra non-sim, non-fid ssrc. 226 cricket::StreamParams stream1 = 227 cricket::CreateSimWithRtxStreamParams("cname", 228 MAKE_VECTOR(kSsrcs3), 229 MAKE_VECTOR(kRtxSsrcs3)); 230 stream1.add_ssrc(25); 231 EXPECT_FALSE(cricket::IsSimulcastStream(stream1)); 232 233 // stream2 has invalid fid-group (no primary). 234 cricket::StreamParams stream2; 235 stream2.add_ssrc(13); 236 EXPECT_TRUE(stream2.AddFidSsrc(13, 14)); 237 std::remove(stream2.ssrcs.begin(), stream2.ssrcs.end(), 13); 238 EXPECT_FALSE(cricket::IsSimulcastStream(stream2)); 239 240 // stream3 has two SIM groups. 241 cricket::StreamParams stream3 = 242 cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs2)); 243 std::vector<uint32> sim_ssrcs = MAKE_VECTOR(kRtxSsrcs3); 244 cricket::SsrcGroup sg(cricket::kSimSsrcGroupSemantics, sim_ssrcs); 245 for (size_t i = 0; i < sim_ssrcs.size(); i++) { 246 stream3.add_ssrc(sim_ssrcs[i]); 247 } 248 stream3.ssrc_groups.push_back(sg); 249 EXPECT_FALSE(cricket::IsSimulcastStream(stream3)); 250 } 251