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