| 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. |
| 9 | */ |
| 10 |
| 11 | #include "media/base/stream_params.h" |
| 12 |
| 13 | #include <stdint.h> |
| 14 |
| 15 | #include <list> |
| 16 |
| 17 | #include "absl/algorithm/container.h" |
| 18 | #include "api/array_view.h" |
| 19 | #include "rtc_base/strings/string_builder.h" |
| 20 |
| 21 | namespace cricket { |
| 22 | namespace { |
| 23 |
| 24 | void AppendSsrcs(rtc::ArrayView<const uint32_t> ssrcs, |
| 25 | rtc::SimpleStringBuilder* sb) { |
| 26 | *sb << "ssrcs:["; |
| 27 | const char* delimiter = ""; |
| 28 | for (uint32_t ssrc : ssrcs) { |
| 29 | *sb << delimiter << ssrc; |
| 30 | delimiter = ","; |
| 31 | } |
| 32 | *sb << "]"; |
| 33 | } |
| 34 |
| 35 | void AppendSsrcGroups(rtc::ArrayView<const SsrcGroup> ssrc_groups, |
| 36 | rtc::SimpleStringBuilder* sb) { |
| 37 | *sb << "ssrc_groups:"; |
| 38 | const char* delimiter = ""; |
| 39 | for (const SsrcGroup& ssrc_group : ssrc_groups) { |
| 40 | *sb << delimiter << ssrc_group.ToString(); |
| 41 | delimiter = ","; |
| 42 | } |
| 43 | } |
| 44 |
| 45 | void AppendStreamIds(rtc::ArrayView<const std::string> stream_ids, |
| 46 | rtc::SimpleStringBuilder* sb) { |
| 47 | *sb << "stream_ids:"; |
| 48 | const char* delimiter = ""; |
| 49 | for (const std::string& stream_id : stream_ids) { |
| 50 | *sb << delimiter << stream_id; |
| 51 | delimiter = ","; |
| 52 | } |
| 53 | } |
| 54 |
| 55 | void AppendRids(rtc::ArrayView<const RidDescription> rids, |
| 56 | rtc::SimpleStringBuilder* sb) { |
| 57 | *sb << "rids:["; |
| 58 | const char* delimiter = ""; |
| 59 | for (const RidDescription& rid : rids) { |
| 60 | *sb << delimiter << rid.rid; |
| 61 | delimiter = ","; |
| 62 | } |
| 63 | *sb << "]"; |
| 64 | } |
| 65 |
| 66 | } // namespace |
| 67 |
| 68 | const char kFecSsrcGroupSemantics[] = "FEC"; |
| 69 | const char kFecFrSsrcGroupSemantics[] = "FEC-FR"; |
| 70 | const char kFidSsrcGroupSemantics[] = "FID"; |
| 71 | const char kSimSsrcGroupSemantics[] = "SIM"; |
| 72 |
| 73 | bool GetStream(const StreamParamsVec& streams, |
| 74 | const StreamSelector& selector, |
| 75 | StreamParams* stream_out) { |
| 76 | const StreamParams* found = GetStream(streams, selector); |
| 77 | if (found && stream_out) |
| 78 | *stream_out = *found; |
| 79 | return found != nullptr; |
| 80 | } |
| 81 |
| 82 | SsrcGroup::SsrcGroup(const std::string& usage, |
| 83 | const std::vector<uint32_t>& ssrcs) |
| 84 | : semantics(usage), ssrcs(ssrcs) {} |
| 85 | SsrcGroup::SsrcGroup(const SsrcGroup&) = default; |
| 86 | SsrcGroup::SsrcGroup(SsrcGroup&&) = default; |
| 87 | SsrcGroup::~SsrcGroup() = default; |
| 88 |
| 89 | SsrcGroup& SsrcGroup::operator=(const SsrcGroup&) = default; |
| 90 | SsrcGroup& SsrcGroup::operator=(SsrcGroup&&) = default; |
| 91 |
| 92 | bool SsrcGroup::has_semantics(const std::string& semantics_in) const { |
| 93 | return (semantics == semantics_in && ssrcs.size() > 0); |
| 94 | } |
| 95 |
| 96 | std::string SsrcGroup::ToString() const { |
| 97 | char buf[1024]; |
| 98 | rtc::SimpleStringBuilder sb(buf); |
| 99 | sb << "{"; |
| 100 | sb << "semantics:" << semantics << ";"; |
| 101 | AppendSsrcs(ssrcs, &sb); |
| 102 | sb << "}"; |
| 103 | return sb.str(); |
| 104 | } |
| 105 |
| 106 | StreamParams::StreamParams() = default; |
| 107 | StreamParams::StreamParams(const StreamParams&) = default; |
| 108 | StreamParams::StreamParams(StreamParams&&) = default; |
| 109 | StreamParams::~StreamParams() = default; |
| 110 | StreamParams& StreamParams::operator=(const StreamParams&) = default; |
| 111 | StreamParams& StreamParams::operator=(StreamParams&&) = default; |
| 112 |
| 113 | bool StreamParams::operator==(const StreamParams& other) const { |
| 114 | return (id == other.id && ssrcs == other.ssrcs && |
| 115 | ssrc_groups == other.ssrc_groups && cname == other.cname && |
| 116 | stream_ids_ == other.stream_ids_ && |
| 117 | // RIDs are not required to be in the same order for equality. |
| 118 | absl::c_is_permutation(rids_, other.rids_)); |
| 119 | } |
| 120 |
| 121 | std::string StreamParams::ToString() const { |
| 122 | char buf[2 * 1024]; |
| 123 | rtc::SimpleStringBuilder sb(buf); |
| 124 | sb << "{"; |
| 125 | if (!id.empty()) { |
| 126 | sb << "id:" << id << ";"; |
| 127 | } |
| 128 | AppendSsrcs(ssrcs, &sb); |
| 129 | sb << ";"; |
| 130 | AppendSsrcGroups(ssrc_groups, &sb); |
| 131 | sb << ";"; |
| 132 | if (!cname.empty()) { |
| 133 | sb << "cname:" << cname << ";"; |
| 134 | } |
| 135 | AppendStreamIds(stream_ids_, &sb); |
| 136 | sb << ";"; |
| 137 | if (!rids_.empty()) { |
| 138 | AppendRids(rids_, &sb); |
| 139 | sb << ";"; |
| 140 | } |
| 141 | sb << "}"; |
| 142 | return sb.str(); |
| 143 | } |
| 144 |
| 145 | void StreamParams::GenerateSsrcs(int num_layers, |
| 146 | bool generate_fid, |
| 147 | bool generate_fec_fr, |
| 148 | rtc::UniqueRandomIdGenerator* ssrc_generator) { |
| 149 | RTC_DCHECK_GE(num_layers, 0); |
| 150 | RTC_DCHECK(ssrc_generator); |
| 151 | std::vector<uint32_t> primary_ssrcs; |
| 152 | for (int i = 0; i < num_layers; ++i) { |
| 153 | uint32_t ssrc = ssrc_generator->GenerateId(); |
| 154 | primary_ssrcs.push_back(ssrc); |
| 155 | add_ssrc(ssrc); |
| 156 | } |
| 157 |
| 158 | if (num_layers > 1) { |
| 159 | SsrcGroup simulcast(kSimSsrcGroupSemantics, primary_ssrcs); |
| 160 | ssrc_groups.push_back(simulcast); |
| 161 | } |
| 162 |
| 163 | if (generate_fid) { |
| 164 | for (uint32_t ssrc : primary_ssrcs) { |
| 165 | AddFidSsrc(ssrc, ssrc_generator->GenerateId()); |
| 166 | } |
| 167 | } |
| 168 |
| 169 | if (generate_fec_fr) { |
| 170 | for (uint32_t ssrc : primary_ssrcs) { |
| 171 | AddFecFrSsrc(ssrc, ssrc_generator->GenerateId()); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 |
| 176 | void StreamParams::GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const { |
| 177 | const SsrcGroup* sim_group = get_ssrc_group(kSimSsrcGroupSemantics); |
| 178 | if (sim_group == NULL) { |
| 179 | ssrcs->push_back(first_ssrc()); |
| 180 | } else { |
| 181 | ssrcs->insert(ssrcs->end(), sim_group->ssrcs.begin(), |
| 182 | sim_group->ssrcs.end()); |
| 183 | } |
| 184 | } |
| 185 |
| 186 | void StreamParams::GetSecondarySsrcs( |
| 187 | const std::string& semantics, |
| 188 | const std::vector<uint32_t>& primary_ssrcs, |
| 189 | std::vector<uint32_t>* secondary_ssrcs) const { |
| 190 | for (uint32_t primary_ssrc : primary_ssrcs) { |
| 191 | uint32_t secondary_ssrc; |
| 192 | if (GetSecondarySsrc(semantics, primary_ssrc, &secondary_ssrc)) { |
| 193 | secondary_ssrcs->push_back(secondary_ssrc); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 |
| 198 | void StreamParams::GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs, |
| 199 | std::vector<uint32_t>* fid_ssrcs) const { |
| 200 | return GetSecondarySsrcs(kFidSsrcGroupSemantics, primary_ssrcs, fid_ssrcs); |
| 201 | } |
| 202 |
| 203 | bool StreamParams::AddSecondarySsrc(const std::string& semantics, |
| 204 | uint32_t primary_ssrc, |
| 205 | uint32_t secondary_ssrc) { |
| 206 | if (!has_ssrc(primary_ssrc)) { |
| 207 | return false; |
| 208 | } |
| 209 |
| 210 | ssrcs.push_back(secondary_ssrc); |
| 211 | ssrc_groups.push_back(SsrcGroup(semantics, {primary_ssrc, secondary_ssrc})); |
| 212 | return true; |
| 213 | } |
| 214 |
| 215 | bool StreamParams::GetSecondarySsrc(const std::string& semantics, |
| 216 | uint32_t primary_ssrc, |
| 217 | uint32_t* secondary_ssrc) const { |
| 218 | for (const SsrcGroup& ssrc_group : ssrc_groups) { |
| 219 | if (ssrc_group.has_semantics(semantics) && ssrc_group.ssrcs.size() >= 2 && |
| 220 | ssrc_group.ssrcs[0] == primary_ssrc) { |
| 221 | *secondary_ssrc = ssrc_group.ssrcs[1]; |
| 222 | return true; |
| 223 | } |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 |
| 228 | std::vector<std::string> StreamParams::stream_ids() const { |
| 229 | return stream_ids_; |
| 230 | } |
| 231 |
| 232 | void StreamParams::set_stream_ids(const std::vector<std::string>& stream_ids) { |
| 233 | stream_ids_ = stream_ids; |
| 234 | } |
| 235 |
| 236 | std::string StreamParams::first_stream_id() const { |
| 237 | return stream_ids_.empty() ? "" : stream_ids_[0]; |
| 238 | } |
| 239 |
| 240 | } // namespace cricket |
| 241 |