henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "media/base/streamparams.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 13 | #include <list> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | #include <sstream> |
| 15 | |
Steve Anton | ac7539e | 2018-02-26 17:20:29 -0800 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
| 17 | |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 18 | namespace cricket { |
pthatcher@webrtc.org | e2b7585 | 2014-12-16 21:09:08 +0000 | [diff] [blame] | 19 | namespace { |
pthatcher@webrtc.org | e2b7585 | 2014-12-16 21:09:08 +0000 | [diff] [blame] | 20 | // NOTE: There is no check here for duplicate streams, so check before |
| 21 | // adding. |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 22 | void AddStream(std::vector<StreamParams>* streams, const StreamParams& stream) { |
pthatcher@webrtc.org | e2b7585 | 2014-12-16 21:09:08 +0000 | [diff] [blame] | 23 | streams->push_back(stream); |
| 24 | } |
pthatcher@webrtc.org | e2b7585 | 2014-12-16 21:09:08 +0000 | [diff] [blame] | 25 | } |
| 26 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | const char kFecSsrcGroupSemantics[] = "FEC"; |
brandtr | 9688e38 | 2016-11-22 00:59:48 -0800 | [diff] [blame] | 28 | const char kFecFrSsrcGroupSemantics[] = "FEC-FR"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | const char kFidSsrcGroupSemantics[] = "FID"; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 30 | const char kSimSsrcGroupSemantics[] = "SIM"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 31 | |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 32 | bool GetStream(const StreamParamsVec& streams, |
| 33 | const StreamSelector& selector, |
| 34 | StreamParams* stream_out) { |
| 35 | const StreamParams* found = GetStream(streams, selector); |
| 36 | if (found && stream_out) |
| 37 | *stream_out = *found; |
| 38 | return found != nullptr; |
| 39 | } |
| 40 | |
pthatcher@webrtc.org | e2b7585 | 2014-12-16 21:09:08 +0000 | [diff] [blame] | 41 | bool MediaStreams::GetAudioStream( |
| 42 | const StreamSelector& selector, StreamParams* stream) { |
| 43 | return GetStream(audio_, selector, stream); |
| 44 | } |
| 45 | |
| 46 | bool MediaStreams::GetVideoStream( |
| 47 | const StreamSelector& selector, StreamParams* stream) { |
| 48 | return GetStream(video_, selector, stream); |
| 49 | } |
| 50 | |
| 51 | bool MediaStreams::GetDataStream( |
| 52 | const StreamSelector& selector, StreamParams* stream) { |
| 53 | return GetStream(data_, selector, stream); |
| 54 | } |
| 55 | |
| 56 | void MediaStreams::CopyFrom(const MediaStreams& streams) { |
| 57 | audio_ = streams.audio_; |
| 58 | video_ = streams.video_; |
| 59 | data_ = streams.data_; |
| 60 | } |
| 61 | |
| 62 | void MediaStreams::AddAudioStream(const StreamParams& stream) { |
| 63 | AddStream(&audio_, stream); |
| 64 | } |
| 65 | |
| 66 | void MediaStreams::AddVideoStream(const StreamParams& stream) { |
| 67 | AddStream(&video_, stream); |
| 68 | } |
| 69 | |
| 70 | void MediaStreams::AddDataStream(const StreamParams& stream) { |
| 71 | AddStream(&data_, stream); |
| 72 | } |
| 73 | |
| 74 | bool MediaStreams::RemoveAudioStream( |
| 75 | const StreamSelector& selector) { |
| 76 | return RemoveStream(&audio_, selector); |
| 77 | } |
| 78 | |
| 79 | bool MediaStreams::RemoveVideoStream( |
| 80 | const StreamSelector& selector) { |
| 81 | return RemoveStream(&video_, selector); |
| 82 | } |
| 83 | |
| 84 | bool MediaStreams::RemoveDataStream( |
| 85 | const StreamSelector& selector) { |
| 86 | return RemoveStream(&data_, selector); |
| 87 | } |
| 88 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 89 | static std::string SsrcsToString(const std::vector<uint32_t>& ssrcs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | std::ostringstream ost; |
| 91 | ost << "ssrcs:["; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 92 | for (std::vector<uint32_t>::const_iterator it = ssrcs.begin(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | it != ssrcs.end(); ++it) { |
| 94 | if (it != ssrcs.begin()) { |
| 95 | ost << ","; |
| 96 | } |
| 97 | ost << *it; |
| 98 | } |
| 99 | ost << "]"; |
| 100 | return ost.str(); |
| 101 | } |
| 102 | |
| 103 | bool SsrcGroup::has_semantics(const std::string& semantics_in) const { |
| 104 | return (semantics == semantics_in && ssrcs.size() > 0); |
| 105 | } |
| 106 | |
| 107 | std::string SsrcGroup::ToString() const { |
| 108 | std::ostringstream ost; |
| 109 | ost << "{"; |
| 110 | ost << "semantics:" << semantics << ";"; |
| 111 | ost << SsrcsToString(ssrcs); |
| 112 | ost << "}"; |
| 113 | return ost.str(); |
| 114 | } |
| 115 | |
| 116 | std::string StreamParams::ToString() const { |
| 117 | std::ostringstream ost; |
| 118 | ost << "{"; |
| 119 | if (!groupid.empty()) { |
| 120 | ost << "groupid:" << groupid << ";"; |
| 121 | } |
| 122 | if (!id.empty()) { |
| 123 | ost << "id:" << id << ";"; |
| 124 | } |
| 125 | ost << SsrcsToString(ssrcs) << ";"; |
| 126 | ost << "ssrc_groups:"; |
| 127 | for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin(); |
| 128 | it != ssrc_groups.end(); ++it) { |
| 129 | if (it != ssrc_groups.begin()) { |
| 130 | ost << ","; |
| 131 | } |
| 132 | ost << it->ToString(); |
| 133 | } |
| 134 | ost << ";"; |
| 135 | if (!type.empty()) { |
| 136 | ost << "type:" << type << ";"; |
| 137 | } |
| 138 | if (!display.empty()) { |
| 139 | ost << "display:" << display << ";"; |
| 140 | } |
| 141 | if (!cname.empty()) { |
| 142 | ost << "cname:" << cname << ";"; |
| 143 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 144 | ost << "stream_ids:"; |
| 145 | for (std::vector<std::string>::const_iterator it = stream_ids_.begin(); |
| 146 | it != stream_ids_.end(); ++it) { |
| 147 | if (it != stream_ids_.begin()) { |
| 148 | ost << ","; |
| 149 | } |
| 150 | ost << *it; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 152 | ost << ";"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | ost << "}"; |
| 154 | return ost.str(); |
| 155 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 156 | void StreamParams::GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const { |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 157 | const SsrcGroup* sim_group = get_ssrc_group(kSimSsrcGroupSemantics); |
| 158 | if (sim_group == NULL) { |
| 159 | ssrcs->push_back(first_ssrc()); |
| 160 | } else { |
| 161 | for (size_t i = 0; i < sim_group->ssrcs.size(); ++i) { |
| 162 | ssrcs->push_back(sim_group->ssrcs[i]); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 167 | void StreamParams::GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs, |
| 168 | std::vector<uint32_t>* fid_ssrcs) const { |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 169 | for (size_t i = 0; i < primary_ssrcs.size(); ++i) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 170 | uint32_t fid_ssrc; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 171 | if (GetFidSsrc(primary_ssrcs[i], &fid_ssrc)) { |
| 172 | fid_ssrcs->push_back(fid_ssrc); |
| 173 | } |
| 174 | } |
| 175 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 176 | |
| 177 | bool StreamParams::AddSecondarySsrc(const std::string& semantics, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 178 | uint32_t primary_ssrc, |
| 179 | uint32_t secondary_ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | if (!has_ssrc(primary_ssrc)) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | ssrcs.push_back(secondary_ssrc); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 185 | std::vector<uint32_t> ssrc_vector; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 186 | ssrc_vector.push_back(primary_ssrc); |
| 187 | ssrc_vector.push_back(secondary_ssrc); |
| 188 | SsrcGroup ssrc_group = SsrcGroup(semantics, ssrc_vector); |
| 189 | ssrc_groups.push_back(ssrc_group); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool StreamParams::GetSecondarySsrc(const std::string& semantics, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 194 | uint32_t primary_ssrc, |
| 195 | uint32_t* secondary_ssrc) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 196 | for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin(); |
| 197 | it != ssrc_groups.end(); ++it) { |
| 198 | if (it->has_semantics(semantics) && |
| 199 | it->ssrcs.size() >= 2 && |
| 200 | it->ssrcs[0] == primary_ssrc) { |
| 201 | *secondary_ssrc = it->ssrcs[1]; |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 208 | std::vector<std::string> StreamParams::stream_ids() const { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 209 | return stream_ids_; |
Steve Anton | ac7539e | 2018-02-26 17:20:29 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 212 | void StreamParams::set_stream_ids(const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 213 | stream_ids_ = stream_ids; |
Steve Anton | ac7539e | 2018-02-26 17:20:29 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 216 | std::string StreamParams::first_stream_id() const { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 217 | return stream_ids_.empty() ? "" : stream_ids_[0]; |
Steve Anton | 5a26a3a | 2018-02-28 11:38:47 -0800 | [diff] [blame] | 218 | } |
| 219 | |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 220 | bool IsOneSsrcStream(const StreamParams& sp) { |
| 221 | if (sp.ssrcs.size() == 1 && sp.ssrc_groups.empty()) { |
| 222 | return true; |
| 223 | } |
brandtr | 9688e38 | 2016-11-22 00:59:48 -0800 | [diff] [blame] | 224 | const SsrcGroup* fid_group = sp.get_ssrc_group(kFidSsrcGroupSemantics); |
| 225 | const SsrcGroup* fecfr_group = sp.get_ssrc_group(kFecFrSsrcGroupSemantics); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 226 | if (sp.ssrcs.size() == 2) { |
brandtr | 9688e38 | 2016-11-22 00:59:48 -0800 | [diff] [blame] | 227 | if (fid_group != nullptr && sp.ssrcs == fid_group->ssrcs) { |
| 228 | return true; |
| 229 | } |
| 230 | if (fecfr_group != nullptr && sp.ssrcs == fecfr_group->ssrcs) { |
| 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | if (sp.ssrcs.size() == 3) { |
| 235 | if (fid_group == nullptr || fecfr_group == nullptr) { |
| 236 | return false; |
| 237 | } |
| 238 | if (sp.ssrcs[0] != fid_group->ssrcs[0] || |
| 239 | sp.ssrcs[0] != fecfr_group->ssrcs[0]) { |
| 240 | return false; |
| 241 | } |
| 242 | // We do not check for FlexFEC over RTX, |
| 243 | // as this combination is not supported. |
| 244 | if (sp.ssrcs[1] == fid_group->ssrcs[1] && |
| 245 | sp.ssrcs[2] == fecfr_group->ssrcs[1]) { |
| 246 | return true; |
| 247 | } |
| 248 | if (sp.ssrcs[1] == fecfr_group->ssrcs[1] && |
| 249 | sp.ssrcs[2] == fid_group->ssrcs[1]) { |
| 250 | return true; |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 256 | static void RemoveFirst(std::list<uint32_t>* ssrcs, uint32_t value) { |
| 257 | std::list<uint32_t>::iterator it = |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 258 | std::find(ssrcs->begin(), ssrcs->end(), value); |
| 259 | if (it != ssrcs->end()) { |
| 260 | ssrcs->erase(it); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | bool IsSimulcastStream(const StreamParams& sp) { |
| 265 | const SsrcGroup* const sg = sp.get_ssrc_group(kSimSsrcGroupSemantics); |
| 266 | if (sg == NULL || sg->ssrcs.size() < 2) { |
| 267 | return false; |
| 268 | } |
| 269 | // Start with all StreamParams SSRCs. Remove simulcast SSRCs (from sg) and |
| 270 | // RTX SSRCs. If we still have SSRCs left, we don't know what they're for. |
| 271 | // Also we remove first-found SSRCs only. So duplicates should lead to errors. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 272 | std::list<uint32_t> sp_ssrcs(sp.ssrcs.begin(), sp.ssrcs.end()); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 273 | for (size_t i = 0; i < sg->ssrcs.size(); ++i) { |
| 274 | RemoveFirst(&sp_ssrcs, sg->ssrcs[i]); |
| 275 | } |
| 276 | for (size_t i = 0; i < sp.ssrc_groups.size(); ++i) { |
| 277 | const SsrcGroup& group = sp.ssrc_groups[i]; |
| 278 | if (group.semantics.compare(kFidSsrcGroupSemantics) != 0 || |
| 279 | group.ssrcs.size() != 2) { |
| 280 | continue; |
| 281 | } |
| 282 | RemoveFirst(&sp_ssrcs, group.ssrcs[1]); |
| 283 | } |
| 284 | // If there's SSRCs left that we don't know how to handle, we bail out. |
| 285 | return sp_ssrcs.size() == 0; |
| 286 | } |
| 287 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 288 | } // namespace cricket |