1 /*
2 * libjingle
3 * Copyright 2011 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
30 #include <list>
31 #include <sstream>
32
33 namespace cricket {
34
35 const char kFecSsrcGroupSemantics[] = "FEC";
36 const char kFidSsrcGroupSemantics[] = "FID";
37 const char kSimSsrcGroupSemantics[] = "SIM";
38
SsrcsToString(const std::vector<uint32> & ssrcs)39 static std::string SsrcsToString(const std::vector<uint32>& ssrcs) {
40 std::ostringstream ost;
41 ost << "ssrcs:[";
42 for (std::vector<uint32>::const_iterator it = ssrcs.begin();
43 it != ssrcs.end(); ++it) {
44 if (it != ssrcs.begin()) {
45 ost << ",";
46 }
47 ost << *it;
48 }
49 ost << "]";
50 return ost.str();
51 }
52
has_semantics(const std::string & semantics_in) const53 bool SsrcGroup::has_semantics(const std::string& semantics_in) const {
54 return (semantics == semantics_in && ssrcs.size() > 0);
55 }
56
ToString() const57 std::string SsrcGroup::ToString() const {
58 std::ostringstream ost;
59 ost << "{";
60 ost << "semantics:" << semantics << ";";
61 ost << SsrcsToString(ssrcs);
62 ost << "}";
63 return ost.str();
64 }
65
ToString() const66 std::string StreamParams::ToString() const {
67 std::ostringstream ost;
68 ost << "{";
69 if (!groupid.empty()) {
70 ost << "groupid:" << groupid << ";";
71 }
72 if (!id.empty()) {
73 ost << "id:" << id << ";";
74 }
75 ost << SsrcsToString(ssrcs) << ";";
76 ost << "ssrc_groups:";
77 for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin();
78 it != ssrc_groups.end(); ++it) {
79 if (it != ssrc_groups.begin()) {
80 ost << ",";
81 }
82 ost << it->ToString();
83 }
84 ost << ";";
85 if (!type.empty()) {
86 ost << "type:" << type << ";";
87 }
88 if (!display.empty()) {
89 ost << "display:" << display << ";";
90 }
91 if (!cname.empty()) {
92 ost << "cname:" << cname << ";";
93 }
94 if (!sync_label.empty()) {
95 ost << "sync_label:" << sync_label;
96 }
97 ost << "}";
98 return ost.str();
99 }
GetPrimarySsrcs(std::vector<uint32> * ssrcs) const100 void StreamParams::GetPrimarySsrcs(std::vector<uint32>* ssrcs) const {
101 const SsrcGroup* sim_group = get_ssrc_group(kSimSsrcGroupSemantics);
102 if (sim_group == NULL) {
103 ssrcs->push_back(first_ssrc());
104 } else {
105 for (size_t i = 0; i < sim_group->ssrcs.size(); ++i) {
106 ssrcs->push_back(sim_group->ssrcs[i]);
107 }
108 }
109 }
110
GetFidSsrcs(const std::vector<uint32> & primary_ssrcs,std::vector<uint32> * fid_ssrcs) const111 void StreamParams::GetFidSsrcs(const std::vector<uint32>& primary_ssrcs,
112 std::vector<uint32>* fid_ssrcs) const {
113 for (size_t i = 0; i < primary_ssrcs.size(); ++i) {
114 uint32 fid_ssrc;
115 if (GetFidSsrc(primary_ssrcs[i], &fid_ssrc)) {
116 fid_ssrcs->push_back(fid_ssrc);
117 }
118 }
119 }
120
AddSecondarySsrc(const std::string & semantics,uint32 primary_ssrc,uint32 secondary_ssrc)121 bool StreamParams::AddSecondarySsrc(const std::string& semantics,
122 uint32 primary_ssrc,
123 uint32 secondary_ssrc) {
124 if (!has_ssrc(primary_ssrc)) {
125 return false;
126 }
127
128 ssrcs.push_back(secondary_ssrc);
129 std::vector<uint32> ssrc_vector;
130 ssrc_vector.push_back(primary_ssrc);
131 ssrc_vector.push_back(secondary_ssrc);
132 SsrcGroup ssrc_group = SsrcGroup(semantics, ssrc_vector);
133 ssrc_groups.push_back(ssrc_group);
134 return true;
135 }
136
GetSecondarySsrc(const std::string & semantics,uint32 primary_ssrc,uint32 * secondary_ssrc) const137 bool StreamParams::GetSecondarySsrc(const std::string& semantics,
138 uint32 primary_ssrc,
139 uint32* secondary_ssrc) const {
140 for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin();
141 it != ssrc_groups.end(); ++it) {
142 if (it->has_semantics(semantics) &&
143 it->ssrcs.size() >= 2 &&
144 it->ssrcs[0] == primary_ssrc) {
145 *secondary_ssrc = it->ssrcs[1];
146 return true;
147 }
148 }
149 return false;
150 }
151
GetStream(const StreamParamsVec & streams,const StreamSelector & selector,StreamParams * stream_out)152 bool GetStream(const StreamParamsVec& streams,
153 const StreamSelector& selector,
154 StreamParams* stream_out) {
155 for (StreamParamsVec::const_iterator stream = streams.begin();
156 stream != streams.end(); ++stream) {
157 if (selector.Matches(*stream)) {
158 if (stream_out != NULL) {
159 *stream_out = *stream;
160 }
161 return true;
162 }
163 }
164 return false;
165 }
166
GetStreamBySsrc(const StreamParamsVec & streams,uint32 ssrc,StreamParams * stream_out)167 bool GetStreamBySsrc(const StreamParamsVec& streams, uint32 ssrc,
168 StreamParams* stream_out) {
169 return GetStream(streams, StreamSelector(ssrc), stream_out);
170 }
171
GetStreamByIds(const StreamParamsVec & streams,const std::string & groupid,const std::string & id,StreamParams * stream_out)172 bool GetStreamByIds(const StreamParamsVec& streams,
173 const std::string& groupid,
174 const std::string& id,
175 StreamParams* stream_out) {
176 return GetStream(streams, StreamSelector(groupid, id), stream_out);
177 }
178
RemoveStream(StreamParamsVec * streams,const StreamSelector & selector)179 bool RemoveStream(StreamParamsVec* streams,
180 const StreamSelector& selector) {
181 bool ret = false;
182 for (StreamParamsVec::iterator stream = streams->begin();
183 stream != streams->end(); ) {
184 if (selector.Matches(*stream)) {
185 stream = streams->erase(stream);
186 ret = true;
187 } else {
188 ++stream;
189 }
190 }
191 return ret;
192 }
193
RemoveStreamBySsrc(StreamParamsVec * streams,uint32 ssrc)194 bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32 ssrc) {
195 return RemoveStream(streams, StreamSelector(ssrc));
196 }
197
RemoveStreamByIds(StreamParamsVec * streams,const std::string & groupid,const std::string & id)198 bool RemoveStreamByIds(StreamParamsVec* streams,
199 const std::string& groupid,
200 const std::string& id) {
201 return RemoveStream(streams, StreamSelector(groupid, id));
202 }
203
IsOneSsrcStream(const StreamParams & sp)204 bool IsOneSsrcStream(const StreamParams& sp) {
205 if (sp.ssrcs.size() == 1 && sp.ssrc_groups.empty()) {
206 return true;
207 }
208 if (sp.ssrcs.size() == 2) {
209 const SsrcGroup* fid_group = sp.get_ssrc_group(kFidSsrcGroupSemantics);
210 if (fid_group != NULL) {
211 return (sp.ssrcs == fid_group->ssrcs);
212 }
213 }
214 return false;
215 }
216
RemoveFirst(std::list<uint32> * ssrcs,uint32 value)217 static void RemoveFirst(std::list<uint32>* ssrcs, uint32 value) {
218 std::list<uint32>::iterator it =
219 std::find(ssrcs->begin(), ssrcs->end(), value);
220 if (it != ssrcs->end()) {
221 ssrcs->erase(it);
222 }
223 }
224
IsSimulcastStream(const StreamParams & sp)225 bool IsSimulcastStream(const StreamParams& sp) {
226 const SsrcGroup* const sg = sp.get_ssrc_group(kSimSsrcGroupSemantics);
227 if (sg == NULL || sg->ssrcs.size() < 2) {
228 return false;
229 }
230 // Start with all StreamParams SSRCs. Remove simulcast SSRCs (from sg) and
231 // RTX SSRCs. If we still have SSRCs left, we don't know what they're for.
232 // Also we remove first-found SSRCs only. So duplicates should lead to errors.
233 std::list<uint32> sp_ssrcs(sp.ssrcs.begin(), sp.ssrcs.end());
234 for (size_t i = 0; i < sg->ssrcs.size(); ++i) {
235 RemoveFirst(&sp_ssrcs, sg->ssrcs[i]);
236 }
237 for (size_t i = 0; i < sp.ssrc_groups.size(); ++i) {
238 const SsrcGroup& group = sp.ssrc_groups[i];
239 if (group.semantics.compare(kFidSsrcGroupSemantics) != 0 ||
240 group.ssrcs.size() != 2) {
241 continue;
242 }
243 RemoveFirst(&sp_ssrcs, group.ssrcs[1]);
244 }
245 // If there's SSRCs left that we don't know how to handle, we bail out.
246 return sp_ssrcs.size() == 0;
247 }
248
249 } // namespace cricket
250