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 }
100
AddSecondarySsrc(const std::string & semantics,uint32 primary_ssrc,uint32 secondary_ssrc)101 bool StreamParams::AddSecondarySsrc(const std::string& semantics,
102 uint32 primary_ssrc,
103 uint32 secondary_ssrc) {
104 if (!has_ssrc(primary_ssrc)) {
105 return false;
106 }
107
108 ssrcs.push_back(secondary_ssrc);
109 std::vector<uint32> ssrc_vector;
110 ssrc_vector.push_back(primary_ssrc);
111 ssrc_vector.push_back(secondary_ssrc);
112 SsrcGroup ssrc_group = SsrcGroup(semantics, ssrc_vector);
113 ssrc_groups.push_back(ssrc_group);
114 return true;
115 }
116
GetSecondarySsrc(const std::string & semantics,uint32 primary_ssrc,uint32 * secondary_ssrc) const117 bool StreamParams::GetSecondarySsrc(const std::string& semantics,
118 uint32 primary_ssrc,
119 uint32* secondary_ssrc) const {
120 for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin();
121 it != ssrc_groups.end(); ++it) {
122 if (it->has_semantics(semantics) &&
123 it->ssrcs.size() >= 2 &&
124 it->ssrcs[0] == primary_ssrc) {
125 *secondary_ssrc = it->ssrcs[1];
126 return true;
127 }
128 }
129 return false;
130 }
131
GetStream(const StreamParamsVec & streams,const StreamSelector & selector,StreamParams * stream_out)132 bool GetStream(const StreamParamsVec& streams,
133 const StreamSelector& selector,
134 StreamParams* stream_out) {
135 for (StreamParamsVec::const_iterator stream = streams.begin();
136 stream != streams.end(); ++stream) {
137 if (selector.Matches(*stream)) {
138 if (stream_out != NULL) {
139 *stream_out = *stream;
140 }
141 return true;
142 }
143 }
144 return false;
145 }
146
GetStreamBySsrc(const StreamParamsVec & streams,uint32 ssrc,StreamParams * stream_out)147 bool GetStreamBySsrc(const StreamParamsVec& streams, uint32 ssrc,
148 StreamParams* stream_out) {
149 return GetStream(streams, StreamSelector(ssrc), stream_out);
150 }
151
GetStreamByIds(const StreamParamsVec & streams,const std::string & groupid,const std::string & id,StreamParams * stream_out)152 bool GetStreamByIds(const StreamParamsVec& streams,
153 const std::string& groupid,
154 const std::string& id,
155 StreamParams* stream_out) {
156 return GetStream(streams, StreamSelector(groupid, id), stream_out);
157 }
158
RemoveStream(StreamParamsVec * streams,const StreamSelector & selector)159 bool RemoveStream(StreamParamsVec* streams,
160 const StreamSelector& selector) {
161 bool ret = false;
162 for (StreamParamsVec::iterator stream = streams->begin();
163 stream != streams->end(); ) {
164 if (selector.Matches(*stream)) {
165 stream = streams->erase(stream);
166 ret = true;
167 } else {
168 ++stream;
169 }
170 }
171 return ret;
172 }
173
RemoveStreamBySsrc(StreamParamsVec * streams,uint32 ssrc)174 bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32 ssrc) {
175 return RemoveStream(streams, StreamSelector(ssrc));
176 }
177
RemoveStreamByIds(StreamParamsVec * streams,const std::string & groupid,const std::string & id)178 bool RemoveStreamByIds(StreamParamsVec* streams,
179 const std::string& groupid,
180 const std::string& id) {
181 return RemoveStream(streams, StreamSelector(groupid, id));
182 }
183
IsOneSsrcStream(const StreamParams & sp)184 bool IsOneSsrcStream(const StreamParams& sp) {
185 if (sp.ssrcs.size() == 1 && sp.ssrc_groups.empty()) {
186 return true;
187 }
188 if (sp.ssrcs.size() == 2) {
189 const SsrcGroup* fid_group = sp.get_ssrc_group(kFidSsrcGroupSemantics);
190 if (fid_group != NULL) {
191 return (sp.ssrcs == fid_group->ssrcs);
192 }
193 }
194 return false;
195 }
196
RemoveFirst(std::list<uint32> * ssrcs,uint32 value)197 static void RemoveFirst(std::list<uint32>* ssrcs, uint32 value) {
198 std::list<uint32>::iterator it =
199 std::find(ssrcs->begin(), ssrcs->end(), value);
200 if (it != ssrcs->end()) {
201 ssrcs->erase(it);
202 }
203 }
204
IsSimulcastStream(const StreamParams & sp)205 bool IsSimulcastStream(const StreamParams& sp) {
206 const SsrcGroup* const sg = sp.get_ssrc_group(kSimSsrcGroupSemantics);
207 if (sg == NULL || sg->ssrcs.size() < 2) {
208 return false;
209 }
210 // Start with all StreamParams SSRCs. Remove simulcast SSRCs (from sg) and
211 // RTX SSRCs. If we still have SSRCs left, we don't know what they're for.
212 // Also we remove first-found SSRCs only. So duplicates should lead to errors.
213 std::list<uint32> sp_ssrcs(sp.ssrcs.begin(), sp.ssrcs.end());
214 for (size_t i = 0; i < sg->ssrcs.size(); ++i) {
215 RemoveFirst(&sp_ssrcs, sg->ssrcs[i]);
216 }
217 for (size_t i = 0; i < sp.ssrc_groups.size(); ++i) {
218 const SsrcGroup& group = sp.ssrc_groups[i];
219 if (group.semantics.compare(kFidSsrcGroupSemantics) != 0 ||
220 group.ssrcs.size() != 2) {
221 continue;
222 }
223 RemoveFirst(&sp_ssrcs, group.ssrcs[1]);
224 }
225 // If there's SSRCs left that we don't know how to handle, we bail out.
226 return sp_ssrcs.size() == 0;
227 }
228
229 } // namespace cricket
230