| 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 "call/rtp_demuxer.h" |
| 12 |
| 13 | #include "absl/strings/string_view.h" |
| 14 | #include "call/rtp_packet_sink_interface.h" |
| 15 | #include "modules/rtp_rtcp/source/rtp_header_extensions.h" |
| 16 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
| 19 | #include "rtc_base/strings/string_builder.h" |
| 20 |
| 21 | namespace webrtc { |
| 22 | namespace { |
| 23 |
| 24 | template <typename Container, typename Value> |
| 25 | size_t RemoveFromMultimapByValue(Container* multimap, const Value& value) { |
| 26 | size_t count = 0; |
| 27 | for (auto it = multimap->begin(); it != multimap->end();) { |
| 28 | if (it->second == value) { |
| 29 | it = multimap->erase(it); |
| 30 | ++count; |
| 31 | } else { |
| 32 | ++it; |
| 33 | } |
| 34 | } |
| 35 | return count; |
| 36 | } |
| 37 |
| 38 | template <typename Map, typename Value> |
| 39 | size_t RemoveFromMapByValue(Map* map, const Value& value) { |
| 40 | return EraseIf(*map, [&](const auto& elem) { return elem.second == value; }); |
| 41 | } |
| 42 |
| 43 | // Temp fix: MID in SDP is allowed to be slightly longer than what's allowed |
| 44 | // in the RTP demuxer. Truncate if needed; this won't match, but it only |
| 45 | // makes sense in places that wouldn't use this for matching anyway. |
| 46 | // TODO(bugs.webrtc.org/12517): remove when length 16 is policed by parser. |
| 47 | std::string CheckMidLength(absl::string_view mid) { |
| 48 | std::string new_mid(mid); |
| 49 | if (new_mid.length() > BaseRtpStringExtension::kMaxValueSizeBytes) { |
| 50 | RTC_LOG(LS_WARNING) << "`mid` attribute too long. Truncating."; |
| 51 | new_mid.resize(BaseRtpStringExtension::kMaxValueSizeBytes); |
| 52 | } |
| 53 | return new_mid; |
| 54 | } |
| 55 |
| 56 | } // namespace |
| 57 |
| 58 | RtpDemuxerCriteria::RtpDemuxerCriteria( |
| 59 | absl::string_view mid, |
| 60 | absl::string_view rsid /*= absl::string_view()*/) |
| 61 | : mid_(CheckMidLength(mid)), rsid_(rsid) {} |
| 62 |
| 63 | RtpDemuxerCriteria::RtpDemuxerCriteria() = default; |
| 64 | RtpDemuxerCriteria::~RtpDemuxerCriteria() = default; |
| 65 |
| 66 | bool RtpDemuxerCriteria::operator==(const RtpDemuxerCriteria& other) const { |
| 67 | return mid_ == other.mid_ && rsid_ == other.rsid_ && ssrcs_ == other.ssrcs_ && |
| 68 | payload_types_ == other.payload_types_; |
| 69 | } |
| 70 |
| 71 | bool RtpDemuxerCriteria::operator!=(const RtpDemuxerCriteria& other) const { |
| 72 | return !(*this == other); |
| 73 | } |
| 74 |
| 75 | std::string RtpDemuxerCriteria::ToString() const { |
| 76 | rtc::StringBuilder sb; |
| 77 | sb << "{mid: " << (mid_.empty() ? "<empty>" : mid_) |
| 78 | << ", rsid: " << (rsid_.empty() ? "<empty>" : rsid_) << ", ssrcs: ["; |
| 79 |
| 80 | for (auto ssrc : ssrcs_) { |
| 81 | sb << ssrc << ", "; |
| 82 | } |
| 83 |
| 84 | sb << "], payload_types = ["; |
| 85 |
| 86 | for (auto pt : payload_types_) { |
| 87 | sb << pt << ", "; |
| 88 | } |
| 89 |
| 90 | sb << "]}"; |
| 91 | return sb.Release(); |
| 92 | } |
| 93 |
| 94 | // static |
| 95 | std::string RtpDemuxer::DescribePacket(const RtpPacketReceived& packet) { |
| 96 | rtc::StringBuilder sb; |
| 97 | sb << "PT=" << packet.PayloadType() << " SSRC=" << packet.Ssrc(); |
| 98 | std::string mid; |
| 99 | if (packet.GetExtension<RtpMid>(&mid)) { |
| 100 | sb << " MID=" << mid; |
| 101 | } |
| 102 | std::string rsid; |
| 103 | if (packet.GetExtension<RtpStreamId>(&rsid)) { |
| 104 | sb << " RSID=" << rsid; |
| 105 | } |
| 106 | std::string rrsid; |
| 107 | if (packet.GetExtension<RepairedRtpStreamId>(&rrsid)) { |
| 108 | sb << " RRSID=" << rrsid; |
| 109 | } |
| 110 | return sb.Release(); |
| 111 | } |
| 112 |
| 113 | RtpDemuxer::RtpDemuxer(bool use_mid /* = true*/) : use_mid_(use_mid) {} |
| 114 |
| 115 | RtpDemuxer::~RtpDemuxer() { |
| 116 | RTC_DCHECK(sink_by_mid_.empty()); |
| 117 | RTC_DCHECK(sink_by_ssrc_.empty()); |
| 118 | RTC_DCHECK(sinks_by_pt_.empty()); |
| 119 | RTC_DCHECK(sink_by_mid_and_rsid_.empty()); |
| 120 | RTC_DCHECK(sink_by_rsid_.empty()); |
| 121 | } |
| 122 |
| 123 | bool RtpDemuxer::AddSink(const RtpDemuxerCriteria& criteria, |
| 124 | RtpPacketSinkInterface* sink) { |
| 125 | RTC_DCHECK(!criteria.payload_types().empty() || !criteria.ssrcs().empty() || |
| 126 | !criteria.mid().empty() || !criteria.rsid().empty()); |
| 127 | RTC_DCHECK(criteria.mid().empty() || IsLegalMidName(criteria.mid())); |
| 128 | RTC_DCHECK(criteria.rsid().empty() || IsLegalRsidName(criteria.rsid())); |
| 129 | RTC_DCHECK(sink); |
| 130 |
| 131 | // We return false instead of DCHECKing for logical conflicts with the new |
| 132 | // criteria because new sinks are created according to user-specified SDP and |
| 133 | // we do not want to crash due to a data validation error. |
| 134 | if (CriteriaWouldConflict(criteria)) { |
| 135 | RTC_LOG(LS_ERROR) << "Unable to add sink=" << sink |
| 136 | << " due to conflicting criteria " << criteria.ToString(); |
| 137 | return false; |
| 138 | } |
| 139 |
| 140 | if (!criteria.mid().empty()) { |
| 141 | if (criteria.rsid().empty()) { |
| 142 | sink_by_mid_.emplace(criteria.mid(), sink); |
| 143 | } else { |
| 144 | sink_by_mid_and_rsid_.emplace( |
| 145 | std::make_pair(criteria.mid(), criteria.rsid()), sink); |
| 146 | } |
| 147 | } else { |
| 148 | if (!criteria.rsid().empty()) { |
| 149 | sink_by_rsid_.emplace(criteria.rsid(), sink); |
| 150 | } |
| 151 | } |
| 152 |
| 153 | for (uint32_t ssrc : criteria.ssrcs()) { |
| 154 | sink_by_ssrc_.emplace(ssrc, sink); |
| 155 | } |
| 156 |
| 157 | for (uint8_t payload_type : criteria.payload_types()) { |
| 158 | sinks_by_pt_.emplace(payload_type, sink); |
| 159 | } |
| 160 |
| 161 | RefreshKnownMids(); |
| 162 |
| 163 | RTC_DLOG(LS_INFO) << "Added sink = " << sink << " for criteria " |
| 164 | << criteria.ToString(); |
| 165 |
| 166 | return true; |
| 167 | } |
| 168 |
| 169 | bool RtpDemuxer::CriteriaWouldConflict( |
| 170 | const RtpDemuxerCriteria& criteria) const { |
| 171 | if (!criteria.mid().empty()) { |
| 172 | if (criteria.rsid().empty()) { |
| 173 | // If the MID is in the known_mids_ set, then there is already a sink |
| 174 | // added for this MID directly, or there is a sink already added with a |
| 175 | // MID, RSID pair for our MID and some RSID. |
| 176 | // Adding this criteria would cause one of these rules to be shadowed, so |
| 177 | // reject this new criteria. |
| 178 | if (known_mids_.find(criteria.mid()) != known_mids_.end()) { |
| 179 | RTC_LOG(LS_INFO) << criteria.ToString() |
| 180 | << " would conflict with known mid"; |
| 181 | return true; |
| 182 | } |
| 183 | } else { |
| 184 | // If the exact rule already exists, then reject this duplicate. |
| 185 | const auto sink_by_mid_and_rsid = sink_by_mid_and_rsid_.find( |
| 186 | std::make_pair(criteria.mid(), criteria.rsid())); |
| 187 | if (sink_by_mid_and_rsid != sink_by_mid_and_rsid_.end()) { |
| 188 | RTC_LOG(LS_INFO) << criteria.ToString() |
| 189 | << " would conflict with existing sink = " |
| 190 | << sink_by_mid_and_rsid->second |
| 191 | << " by mid+rsid binding"; |
| 192 | return true; |
| 193 | } |
| 194 | // If there is already a sink registered for the bare MID, then this |
| 195 | // criteria will never receive any packets because they will just be |
| 196 | // directed to that MID sink, so reject this new criteria. |
| 197 | const auto sink_by_mid = sink_by_mid_.find(criteria.mid()); |
| 198 | if (sink_by_mid != sink_by_mid_.end()) { |
| 199 | RTC_LOG(LS_INFO) << criteria.ToString() |
| 200 | << " would conflict with existing sink = " |
| 201 | << sink_by_mid->second << " by mid binding"; |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 |
| 207 | for (uint32_t ssrc : criteria.ssrcs()) { |
| 208 | const auto sink_by_ssrc = sink_by_ssrc_.find(ssrc); |
| 209 | if (sink_by_ssrc != sink_by_ssrc_.end()) { |
| 210 | RTC_LOG(LS_INFO) << criteria.ToString() |
| 211 | << " would conflict with existing sink = " |
| 212 | << sink_by_ssrc->second << " binding by SSRC=" << ssrc; |
| 213 | return true; |
| 214 | } |
| 215 | } |
| 216 |
| 217 | // TODO(steveanton): May also sanity check payload types. |
| 218 |
| 219 | return false; |
| 220 | } |
| 221 |
| 222 | void RtpDemuxer::RefreshKnownMids() { |
| 223 | known_mids_.clear(); |
| 224 |
| 225 | for (auto const& item : sink_by_mid_) { |
| 226 | const std::string& mid = item.first; |
| 227 | known_mids_.insert(mid); |
| 228 | } |
| 229 |
| 230 | for (auto const& item : sink_by_mid_and_rsid_) { |
| 231 | const std::string& mid = item.first.first; |
| 232 | known_mids_.insert(mid); |
| 233 | } |
| 234 | } |
| 235 |
| 236 | bool RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { |
| 237 | RtpDemuxerCriteria criteria; |
| 238 | criteria.ssrcs().insert(ssrc); |
| 239 | return AddSink(criteria, sink); |
| 240 | } |
| 241 |
| 242 | void RtpDemuxer::AddSink(absl::string_view rsid, RtpPacketSinkInterface* sink) { |
| 243 | RtpDemuxerCriteria criteria(absl::string_view() /* mid */, rsid); |
| 244 | AddSink(criteria, sink); |
| 245 | } |
| 246 |
| 247 | bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { |
| 248 | RTC_DCHECK(sink); |
| 249 | size_t num_removed = RemoveFromMapByValue(&sink_by_mid_, sink) + |
| 250 | RemoveFromMapByValue(&sink_by_ssrc_, sink) + |
| 251 | RemoveFromMultimapByValue(&sinks_by_pt_, sink) + |
| 252 | RemoveFromMapByValue(&sink_by_mid_and_rsid_, sink) + |
| 253 | RemoveFromMapByValue(&sink_by_rsid_, sink); |
| 254 | RefreshKnownMids(); |
| 255 | return num_removed > 0; |
| 256 | } |
| 257 |
| 258 | flat_set<uint32_t> RtpDemuxer::GetSsrcsForSink( |
| 259 | const RtpPacketSinkInterface* sink) const { |
| 260 | flat_set<uint32_t> ssrcs; |
| 261 | if (sink) { |
| 262 | for (const auto& it : sink_by_ssrc_) { |
| 263 | if (it.second == sink) { |
| 264 | ssrcs.insert(it.first); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | return ssrcs; |
| 269 | } |
| 270 |
| 271 | bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { |
| 272 | RtpPacketSinkInterface* sink = ResolveSink(packet); |
| 273 | if (sink != nullptr) { |
| 274 | sink->OnRtpPacket(packet); |
| 275 | return true; |
| 276 | } |
| 277 | return false; |
| 278 | } |
| 279 |
| 280 | RtpPacketSinkInterface* RtpDemuxer::ResolveSink( |
| 281 | const RtpPacketReceived& packet) { |
| 282 | // See the BUNDLE spec for high level reference to this algorithm: |
| 283 | // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-38#section-10.2 |
| 284 |
| 285 | // RSID and RRID are routed to the same sinks. If an RSID is specified on a |
| 286 | // repair packet, it should be ignored and the RRID should be used. |
| 287 | std::string packet_mid, packet_rsid; |
| 288 | bool has_mid = use_mid_ && packet.GetExtension<RtpMid>(&packet_mid); |
| 289 | bool has_rsid = packet.GetExtension<RepairedRtpStreamId>(&packet_rsid); |
| 290 | if (!has_rsid) { |
| 291 | has_rsid = packet.GetExtension<RtpStreamId>(&packet_rsid); |
| 292 | } |
| 293 | uint32_t ssrc = packet.Ssrc(); |
| 294 |
| 295 | // The BUNDLE spec says to drop any packets with unknown MIDs, even if the |
| 296 | // SSRC is known/latched. |
| 297 | if (has_mid && known_mids_.find(packet_mid) == known_mids_.end()) { |
| 298 | return nullptr; |
| 299 | } |
| 300 |
| 301 | // Cache information we learn about SSRCs and IDs. We need to do this even if |
| 302 | // there isn't a rule/sink yet because we might add an MID/RSID rule after |
| 303 | // learning an MID/RSID<->SSRC association. |
| 304 |
| 305 | std::string* mid = nullptr; |
| 306 | if (has_mid) { |
| 307 | mid_by_ssrc_[ssrc] = packet_mid; |
| 308 | mid = &packet_mid; |
| 309 | } else { |
| 310 | // If the packet does not include a MID header extension, check if there is |
| 311 | // a latched MID for the SSRC. |
| 312 | const auto it = mid_by_ssrc_.find(ssrc); |
| 313 | if (it != mid_by_ssrc_.end()) { |
| 314 | mid = &it->second; |
| 315 | } |
| 316 | } |
| 317 |
| 318 | std::string* rsid = nullptr; |
| 319 | if (has_rsid) { |
| 320 | rsid_by_ssrc_[ssrc] = packet_rsid; |
| 321 | rsid = &packet_rsid; |
| 322 | } else { |
| 323 | // If the packet does not include an RRID/RSID header extension, check if |
| 324 | // there is a latched RSID for the SSRC. |
| 325 | const auto it = rsid_by_ssrc_.find(ssrc); |
| 326 | if (it != rsid_by_ssrc_.end()) { |
| 327 | rsid = &it->second; |
| 328 | } |
| 329 | } |
| 330 |
| 331 | // If MID and/or RSID is specified, prioritize that for demuxing the packet. |
| 332 | // The motivation behind the BUNDLE algorithm is that we trust these are used |
| 333 | // deliberately by senders and are more likely to be correct than SSRC/payload |
| 334 | // type which are included with every packet. |
| 335 | // TODO(steveanton): According to the BUNDLE spec, new SSRC mappings are only |
| 336 | // accepted if the packet's extended sequence number is |
| 337 | // greater than that of the last SSRC mapping update. |
| 338 | // https://tools.ietf.org/html/rfc7941#section-4.2.6 |
| 339 | if (mid != nullptr) { |
| 340 | RtpPacketSinkInterface* sink_by_mid = ResolveSinkByMid(*mid, ssrc); |
| 341 | if (sink_by_mid != nullptr) { |
| 342 | return sink_by_mid; |
| 343 | } |
| 344 |
| 345 | // RSID is scoped to a given MID if both are included. |
| 346 | if (rsid != nullptr) { |
| 347 | RtpPacketSinkInterface* sink_by_mid_rsid = |
| 348 | ResolveSinkByMidRsid(*mid, *rsid, ssrc); |
| 349 | if (sink_by_mid_rsid != nullptr) { |
| 350 | return sink_by_mid_rsid; |
| 351 | } |
| 352 | } |
| 353 |
| 354 | // At this point, there is at least one sink added for this MID and an RSID |
| 355 | // but either the packet does not have an RSID or it is for a different |
| 356 | // RSID. This falls outside the BUNDLE spec so drop the packet. |
| 357 | return nullptr; |
| 358 | } |
| 359 |
| 360 | // RSID can be used without MID as long as they are unique. |
| 361 | if (rsid != nullptr) { |
| 362 | RtpPacketSinkInterface* sink_by_rsid = ResolveSinkByRsid(*rsid, ssrc); |
| 363 | if (sink_by_rsid != nullptr) { |
| 364 | return sink_by_rsid; |
| 365 | } |
| 366 | } |
| 367 |
| 368 | // We trust signaled SSRC more than payload type which is likely to conflict |
| 369 | // between streams. |
| 370 | const auto ssrc_sink_it = sink_by_ssrc_.find(ssrc); |
| 371 | if (ssrc_sink_it != sink_by_ssrc_.end()) { |
| 372 | return ssrc_sink_it->second; |
| 373 | } |
| 374 |
| 375 | // Legacy senders will only signal payload type, support that as last resort. |
| 376 | return ResolveSinkByPayloadType(packet.PayloadType(), ssrc); |
| 377 | } |
| 378 |
| 379 | RtpPacketSinkInterface* RtpDemuxer::ResolveSinkByMid(absl::string_view mid, |
| 380 | uint32_t ssrc) { |
| 381 | const auto it = sink_by_mid_.find(mid); |
| 382 | if (it != sink_by_mid_.end()) { |
| 383 | RtpPacketSinkInterface* sink = it->second; |
| 384 | AddSsrcSinkBinding(ssrc, sink); |
| 385 | return sink; |
| 386 | } |
| 387 | return nullptr; |
| 388 | } |
| 389 |
| 390 | RtpPacketSinkInterface* RtpDemuxer::ResolveSinkByMidRsid(absl::string_view mid, |
| 391 | absl::string_view rsid, |
| 392 | uint32_t ssrc) { |
| 393 | const auto it = sink_by_mid_and_rsid_.find( |
| 394 | std::make_pair(std::string(mid), std::string(rsid))); |
| 395 | if (it != sink_by_mid_and_rsid_.end()) { |
| 396 | RtpPacketSinkInterface* sink = it->second; |
| 397 | AddSsrcSinkBinding(ssrc, sink); |
| 398 | return sink; |
| 399 | } |
| 400 | return nullptr; |
| 401 | } |
| 402 |
| 403 | RtpPacketSinkInterface* RtpDemuxer::ResolveSinkByRsid(absl::string_view rsid, |
| 404 | uint32_t ssrc) { |
| 405 | const auto it = sink_by_rsid_.find(rsid); |
| 406 | if (it != sink_by_rsid_.end()) { |
| 407 | RtpPacketSinkInterface* sink = it->second; |
| 408 | AddSsrcSinkBinding(ssrc, sink); |
| 409 | return sink; |
| 410 | } |
| 411 | return nullptr; |
| 412 | } |
| 413 |
| 414 | RtpPacketSinkInterface* RtpDemuxer::ResolveSinkByPayloadType( |
| 415 | uint8_t payload_type, |
| 416 | uint32_t ssrc) { |
| 417 | const auto range = sinks_by_pt_.equal_range(payload_type); |
| 418 | if (range.first != range.second) { |
| 419 | auto it = range.first; |
| 420 | const auto end = range.second; |
| 421 | if (std::next(it) == end) { |
| 422 | RtpPacketSinkInterface* sink = it->second; |
| 423 | AddSsrcSinkBinding(ssrc, sink); |
| 424 | return sink; |
| 425 | } |
| 426 | } |
| 427 | return nullptr; |
| 428 | } |
| 429 |
| 430 | void RtpDemuxer::AddSsrcSinkBinding(uint32_t ssrc, |
| 431 | RtpPacketSinkInterface* sink) { |
| 432 | if (sink_by_ssrc_.size() >= kMaxSsrcBindings) { |
| 433 | RTC_LOG(LS_WARNING) << "New SSRC=" << ssrc |
| 434 | << " sink binding ignored; limit of" << kMaxSsrcBindings |
| 435 | << " bindings has been reached."; |
| 436 | return; |
| 437 | } |
| 438 |
| 439 | auto result = sink_by_ssrc_.emplace(ssrc, sink); |
| 440 | auto it = result.first; |
| 441 | bool inserted = result.second; |
| 442 | if (inserted) { |
| 443 | RTC_DLOG(LS_INFO) << "Added sink = " << sink |
| 444 | << " binding with SSRC=" << ssrc; |
| 445 | } else if (it->second != sink) { |
| 446 | RTC_DLOG(LS_INFO) << "Updated sink = " << sink |
| 447 | << " binding with SSRC=" << ssrc; |
| 448 | it->second = sink; |
| 449 | } |
| 450 | } |
| 451 |
| 452 | } // namespace webrtc |
| 453 |