| 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 "pc/channel.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <cstdint> |
| 15 | #include <string> |
| 16 | #include <type_traits> |
| 17 | #include <utility> |
| 18 |
| 19 | #include "absl/strings/string_view.h" |
| 20 | #include "api/rtp_parameters.h" |
| 21 | #include "api/sequence_checker.h" |
| 22 | #include "api/task_queue/pending_task_safety_flag.h" |
| 23 | #include "api/units/timestamp.h" |
| 24 | #include "media/base/codec.h" |
| 25 | #include "media/base/rid_description.h" |
| 26 | #include "media/base/rtp_utils.h" |
| 27 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 28 | #include "p2p/base/dtls_transport_internal.h" |
| 29 | #include "pc/rtp_media_utils.h" |
| 30 | #include "rtc_base/checks.h" |
| 31 | #include "rtc_base/copy_on_write_buffer.h" |
| 32 | #include "rtc_base/logging.h" |
| 33 | #include "rtc_base/network_route.h" |
| 34 | #include "rtc_base/strings/string_format.h" |
| 35 | #include "rtc_base/trace_event.h" |
| 36 |
| 37 | namespace cricket { |
| 38 | namespace { |
| 39 |
| 40 | using ::rtc::StringFormat; |
| 41 | using ::rtc::UniqueRandomIdGenerator; |
| 42 | using ::webrtc::PendingTaskSafetyFlag; |
| 43 | using ::webrtc::SdpType; |
| 44 |
| 45 | // Finds a stream based on target's Primary SSRC or RIDs. |
| 46 | // This struct is used in BaseChannel::UpdateLocalStreams_w. |
| 47 | struct StreamFinder { |
| 48 | explicit StreamFinder(const StreamParams* target) : target_(target) { |
| 49 | RTC_DCHECK(target); |
| 50 | } |
| 51 |
| 52 | bool operator()(const StreamParams& sp) const { |
| 53 | if (target_->has_ssrcs() && sp.has_ssrcs()) { |
| 54 | return sp.has_ssrc(target_->first_ssrc()); |
| 55 | } |
| 56 |
| 57 | if (!target_->has_rids() && !sp.has_rids()) { |
| 58 | return false; |
| 59 | } |
| 60 |
| 61 | const std::vector<RidDescription>& target_rids = target_->rids(); |
| 62 | const std::vector<RidDescription>& source_rids = sp.rids(); |
| 63 | if (source_rids.size() != target_rids.size()) { |
| 64 | return false; |
| 65 | } |
| 66 |
| 67 | // Check that all RIDs match. |
| 68 | return std::equal(source_rids.begin(), source_rids.end(), |
| 69 | target_rids.begin(), |
| 70 | [](const RidDescription& lhs, const RidDescription& rhs) { |
| 71 | return lhs.rid == rhs.rid; |
| 72 | }); |
| 73 | } |
| 74 |
| 75 | const StreamParams* target_; |
| 76 | }; |
| 77 |
| 78 | } // namespace |
| 79 |
| 80 | template <class Codec> |
| 81 | void RtpParametersFromMediaDescription( |
| 82 | const MediaContentDescriptionImpl<Codec>* desc, |
| 83 | const RtpHeaderExtensions& extensions, |
| 84 | bool is_stream_active, |
| 85 | RtpParameters<Codec>* params) { |
| 86 | params->is_stream_active = is_stream_active; |
| 87 | params->codecs = desc->codecs(); |
| 88 | // TODO(bugs.webrtc.org/11513): See if we really need |
| 89 | // rtp_header_extensions_set() and remove it if we don't. |
| 90 | if (desc->rtp_header_extensions_set()) { |
| 91 | params->extensions = extensions; |
| 92 | } |
| 93 | params->rtcp.reduced_size = desc->rtcp_reduced_size(); |
| 94 | params->rtcp.remote_estimate = desc->remote_estimate(); |
| 95 | } |
| 96 |
| 97 | template <class Codec> |
| 98 | void RtpSendParametersFromMediaDescription( |
| 99 | const MediaContentDescriptionImpl<Codec>* desc, |
| 100 | webrtc::RtpExtension::Filter extensions_filter, |
| 101 | RtpSendParameters<Codec>* send_params) { |
| 102 | RtpHeaderExtensions extensions = |
| 103 | webrtc::RtpExtension::DeduplicateHeaderExtensions( |
| 104 | desc->rtp_header_extensions(), extensions_filter); |
| 105 | const bool is_stream_active = |
| 106 | webrtc::RtpTransceiverDirectionHasRecv(desc->direction()); |
| 107 | RtpParametersFromMediaDescription(desc, extensions, is_stream_active, |
| 108 | send_params); |
| 109 | send_params->max_bandwidth_bps = desc->bandwidth(); |
| 110 | send_params->extmap_allow_mixed = desc->extmap_allow_mixed(); |
| 111 | } |
| 112 |
| 113 | BaseChannel::BaseChannel(rtc::Thread* worker_thread, |
| 114 | rtc::Thread* network_thread, |
| 115 | rtc::Thread* signaling_thread, |
| 116 | std::unique_ptr<MediaChannel> media_channel, |
| 117 | absl::string_view mid, |
| 118 | bool srtp_required, |
| 119 | webrtc::CryptoOptions crypto_options, |
| 120 | UniqueRandomIdGenerator* ssrc_generator) |
| 121 | : worker_thread_(worker_thread), |
| 122 | network_thread_(network_thread), |
| 123 | signaling_thread_(signaling_thread), |
| 124 | alive_(PendingTaskSafetyFlag::Create()), |
| 125 | srtp_required_(srtp_required), |
| 126 | extensions_filter_( |
| 127 | crypto_options.srtp.enable_encrypted_rtp_header_extensions |
| 128 | ? webrtc::RtpExtension::kPreferEncryptedExtension |
| 129 | : webrtc::RtpExtension::kDiscardEncryptedExtension), |
| 130 | media_channel_(std::move(media_channel)), |
| 131 | demuxer_criteria_(mid), |
| 132 | ssrc_generator_(ssrc_generator) { |
| 133 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 134 | RTC_DCHECK(media_channel_); |
| 135 | RTC_DCHECK(ssrc_generator_); |
| 136 | RTC_DLOG(LS_INFO) << "Created channel: " << ToString(); |
| 137 | } |
| 138 |
| 139 | BaseChannel::~BaseChannel() { |
| 140 | TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel"); |
| 141 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 142 |
| 143 | // Eats any outstanding messages or packets. |
| 144 | alive_->SetNotAlive(); |
| 145 | // The media channel is destroyed at the end of the destructor, since it |
| 146 | // is a std::unique_ptr. The transport channel (rtp_transport) must outlive |
| 147 | // the media channel. |
| 148 | } |
| 149 |
| 150 | std::string BaseChannel::ToString() const { |
| 151 | return StringFormat("{mid: %s, media_type: %s}", mid().c_str(), |
| 152 | MediaTypeToString(media_channel_->media_type()).c_str()); |
| 153 | } |
| 154 |
| 155 | bool BaseChannel::ConnectToRtpTransport_n() { |
| 156 | RTC_DCHECK(rtp_transport_); |
| 157 | RTC_DCHECK(media_channel()); |
| 158 |
| 159 | // We don't need to call OnDemuxerCriteriaUpdatePending/Complete because |
| 160 | // there's no previous criteria to worry about. |
| 161 | if (!rtp_transport_->RegisterRtpDemuxerSink(demuxer_criteria_, this)) { |
| 162 | return false; |
| 163 | } |
| 164 | rtp_transport_->SignalReadyToSend.connect( |
| 165 | this, &BaseChannel::OnTransportReadyToSend); |
| 166 | rtp_transport_->SignalNetworkRouteChanged.connect( |
| 167 | this, &BaseChannel::OnNetworkRouteChanged); |
| 168 | rtp_transport_->SignalWritableState.connect(this, |
| 169 | &BaseChannel::OnWritableState); |
| 170 | rtp_transport_->SignalSentPacket.connect(this, |
| 171 | &BaseChannel::SignalSentPacket_n); |
| 172 | return true; |
| 173 | } |
| 174 |
| 175 | void BaseChannel::DisconnectFromRtpTransport_n() { |
| 176 | RTC_DCHECK(rtp_transport_); |
| 177 | RTC_DCHECK(media_channel()); |
| 178 | rtp_transport_->UnregisterRtpDemuxerSink(this); |
| 179 | rtp_transport_->SignalReadyToSend.disconnect(this); |
| 180 | rtp_transport_->SignalNetworkRouteChanged.disconnect(this); |
| 181 | rtp_transport_->SignalWritableState.disconnect(this); |
| 182 | rtp_transport_->SignalSentPacket.disconnect(this); |
| 183 | rtp_transport_ = nullptr; |
| 184 | media_channel_->SetInterface(nullptr); |
| 185 | } |
| 186 |
| 187 | bool BaseChannel::SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport) { |
| 188 | TRACE_EVENT0("webrtc", "BaseChannel::SetRtpTransport"); |
| 189 | RTC_DCHECK_RUN_ON(network_thread()); |
| 190 | if (rtp_transport == rtp_transport_) { |
| 191 | return true; |
| 192 | } |
| 193 |
| 194 | if (rtp_transport_) { |
| 195 | DisconnectFromRtpTransport_n(); |
| 196 | // Clear the cached header extensions on the worker. |
| 197 | worker_thread_->PostTask(SafeTask(alive_, [this] { |
| 198 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 199 | rtp_header_extensions_.clear(); |
| 200 | })); |
| 201 | } |
| 202 |
| 203 | rtp_transport_ = rtp_transport; |
| 204 | if (rtp_transport_) { |
| 205 | if (!ConnectToRtpTransport_n()) { |
| 206 | return false; |
| 207 | } |
| 208 |
| 209 | RTC_DCHECK(!media_channel_->HasNetworkInterface()); |
| 210 | media_channel_->SetInterface(this); |
| 211 |
| 212 | media_channel_->OnReadyToSend(rtp_transport_->IsReadyToSend()); |
| 213 | UpdateWritableState_n(); |
| 214 |
| 215 | // Set the cached socket options. |
| 216 | for (const auto& pair : socket_options_) { |
| 217 | rtp_transport_->SetRtpOption(pair.first, pair.second); |
| 218 | } |
| 219 | if (!rtp_transport_->rtcp_mux_enabled()) { |
| 220 | for (const auto& pair : rtcp_socket_options_) { |
| 221 | rtp_transport_->SetRtcpOption(pair.first, pair.second); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 |
| 226 | return true; |
| 227 | } |
| 228 |
| 229 | void BaseChannel::Enable(bool enable) { |
| 230 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 231 |
| 232 | if (enable == enabled_s_) |
| 233 | return; |
| 234 |
| 235 | enabled_s_ = enable; |
| 236 |
| 237 | worker_thread_->PostTask(SafeTask(alive_, [this, enable] { |
| 238 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 239 | // Sanity check to make sure that enabled_ and enabled_s_ |
| 240 | // stay in sync. |
| 241 | RTC_DCHECK_NE(enabled_, enable); |
| 242 | if (enable) { |
| 243 | EnableMedia_w(); |
| 244 | } else { |
| 245 | DisableMedia_w(); |
| 246 | } |
| 247 | })); |
| 248 | } |
| 249 |
| 250 | bool BaseChannel::SetLocalContent(const MediaContentDescription* content, |
| 251 | SdpType type, |
| 252 | std::string& error_desc) { |
| 253 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 254 | TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent"); |
| 255 | return SetLocalContent_w(content, type, error_desc); |
| 256 | } |
| 257 |
| 258 | bool BaseChannel::SetRemoteContent(const MediaContentDescription* content, |
| 259 | SdpType type, |
| 260 | std::string& error_desc) { |
| 261 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 262 | TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent"); |
| 263 | return SetRemoteContent_w(content, type, error_desc); |
| 264 | } |
| 265 |
| 266 | bool BaseChannel::SetPayloadTypeDemuxingEnabled(bool enabled) { |
| 267 | // TODO(bugs.webrtc.org/11993): The demuxer state needs to be managed on the |
| 268 | // network thread. At the moment there's a workaround for inconsistent state |
| 269 | // between the worker and network thread because of this (see |
| 270 | // OnDemuxerCriteriaUpdatePending elsewhere in this file) and |
| 271 | // SetPayloadTypeDemuxingEnabled_w has a BlockingCall over to the network |
| 272 | // thread to apply state updates. |
| 273 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 274 | TRACE_EVENT0("webrtc", "BaseChannel::SetPayloadTypeDemuxingEnabled"); |
| 275 | return SetPayloadTypeDemuxingEnabled_w(enabled); |
| 276 | } |
| 277 |
| 278 | bool BaseChannel::IsReadyToSendMedia_w() const { |
| 279 | // Send outgoing data if we are enabled, have local and remote content, |
| 280 | // and we have had some form of connectivity. |
| 281 | return enabled_ && |
| 282 | webrtc::RtpTransceiverDirectionHasRecv(remote_content_direction_) && |
| 283 | webrtc::RtpTransceiverDirectionHasSend(local_content_direction_) && |
| 284 | was_ever_writable_; |
| 285 | } |
| 286 |
| 287 | bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet, |
| 288 | const rtc::PacketOptions& options) { |
| 289 | return SendPacket(false, packet, options); |
| 290 | } |
| 291 |
| 292 | bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet, |
| 293 | const rtc::PacketOptions& options) { |
| 294 | return SendPacket(true, packet, options); |
| 295 | } |
| 296 |
| 297 | int BaseChannel::SetOption(SocketType type, |
| 298 | rtc::Socket::Option opt, |
| 299 | int value) { |
| 300 | RTC_DCHECK_RUN_ON(network_thread()); |
| 301 | RTC_DCHECK(network_initialized()); |
| 302 | RTC_DCHECK(rtp_transport_); |
| 303 | switch (type) { |
| 304 | case ST_RTP: |
| 305 | socket_options_.push_back( |
| 306 | std::pair<rtc::Socket::Option, int>(opt, value)); |
| 307 | return rtp_transport_->SetRtpOption(opt, value); |
| 308 | case ST_RTCP: |
| 309 | rtcp_socket_options_.push_back( |
| 310 | std::pair<rtc::Socket::Option, int>(opt, value)); |
| 311 | return rtp_transport_->SetRtcpOption(opt, value); |
| 312 | } |
| 313 | return -1; |
| 314 | } |
| 315 |
| 316 | void BaseChannel::OnWritableState(bool writable) { |
| 317 | RTC_DCHECK_RUN_ON(network_thread()); |
| 318 | RTC_DCHECK(network_initialized()); |
| 319 | if (writable) { |
| 320 | ChannelWritable_n(); |
| 321 | } else { |
| 322 | ChannelNotWritable_n(); |
| 323 | } |
| 324 | } |
| 325 |
| 326 | void BaseChannel::OnNetworkRouteChanged( |
| 327 | absl::optional<rtc::NetworkRoute> network_route) { |
| 328 | RTC_DCHECK_RUN_ON(network_thread()); |
| 329 | RTC_DCHECK(network_initialized()); |
| 330 |
| 331 | RTC_LOG(LS_INFO) << "Network route changed for " << ToString(); |
| 332 |
| 333 | rtc::NetworkRoute new_route; |
| 334 | if (network_route) { |
| 335 | new_route = *(network_route); |
| 336 | } |
| 337 | // Note: When the RTCP-muxing is not enabled, RTCP transport and RTP transport |
| 338 | // use the same transport name and MediaChannel::OnNetworkRouteChanged cannot |
| 339 | // work correctly. Intentionally leave it broken to simplify the code and |
| 340 | // encourage the users to stop using non-muxing RTCP. |
| 341 | media_channel_->OnNetworkRouteChanged(transport_name(), new_route); |
| 342 | } |
| 343 |
| 344 | void BaseChannel::SetFirstPacketReceivedCallback( |
| 345 | std::function<void()> callback) { |
| 346 | RTC_DCHECK_RUN_ON(network_thread()); |
| 347 | RTC_DCHECK(!on_first_packet_received_ || !callback); |
| 348 |
| 349 | // TODO(bugs.webrtc.org/11992): Rename SetFirstPacketReceivedCallback to |
| 350 | // something that indicates network thread initialization/uninitialization and |
| 351 | // call Init_n() / Deinit_n() respectively. |
| 352 | // if (!callback) |
| 353 | // Deinit_n(); |
| 354 |
| 355 | on_first_packet_received_ = std::move(callback); |
| 356 | } |
| 357 |
| 358 | void BaseChannel::OnTransportReadyToSend(bool ready) { |
| 359 | RTC_DCHECK_RUN_ON(network_thread()); |
| 360 | RTC_DCHECK(network_initialized()); |
| 361 | media_channel_->OnReadyToSend(ready); |
| 362 | } |
| 363 |
| 364 | bool BaseChannel::SendPacket(bool rtcp, |
| 365 | rtc::CopyOnWriteBuffer* packet, |
| 366 | const rtc::PacketOptions& options) { |
| 367 | RTC_DCHECK_RUN_ON(network_thread()); |
| 368 | RTC_DCHECK(network_initialized()); |
| 369 | TRACE_EVENT0("webrtc", "BaseChannel::SendPacket"); |
| 370 |
| 371 | // Until all the code is migrated to use RtpPacketType instead of bool. |
| 372 | RtpPacketType packet_type = rtcp ? RtpPacketType::kRtcp : RtpPacketType::kRtp; |
| 373 |
| 374 | // Ensure we have a place to send this packet before doing anything. We might |
| 375 | // get RTCP packets that we don't intend to send. If we've negotiated RTCP |
| 376 | // mux, send RTCP over the RTP transport. |
| 377 | if (!rtp_transport_ || !rtp_transport_->IsWritable(rtcp)) { |
| 378 | return false; |
| 379 | } |
| 380 |
| 381 | // Protect ourselves against crazy data. |
| 382 | if (!IsValidRtpPacketSize(packet_type, packet->size())) { |
| 383 | RTC_LOG(LS_ERROR) << "Dropping outgoing " << ToString() << " " |
| 384 | << RtpPacketTypeToString(packet_type) |
| 385 | << " packet: wrong size=" << packet->size(); |
| 386 | return false; |
| 387 | } |
| 388 |
| 389 | if (!srtp_active()) { |
| 390 | if (srtp_required_) { |
| 391 | // The audio/video engines may attempt to send RTCP packets as soon as the |
| 392 | // streams are created, so don't treat this as an error for RTCP. |
| 393 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809 |
| 394 | // However, there shouldn't be any RTP packets sent before SRTP is set |
| 395 | // up (and SetSend(true) is called). |
| 396 | RTC_DCHECK(rtcp) << "Can't send outgoing RTP packet for " << ToString() |
| 397 | << " when SRTP is inactive and crypto is required"; |
| 398 | return false; |
| 399 | } |
| 400 |
| 401 | RTC_DLOG(LS_WARNING) << "Sending an " << (rtcp ? "RTCP" : "RTP") |
| 402 | << " packet without encryption for " << ToString() |
| 403 | << "."; |
| 404 | } |
| 405 |
| 406 | return rtcp ? rtp_transport_->SendRtcpPacket(packet, options, PF_SRTP_BYPASS) |
| 407 | : rtp_transport_->SendRtpPacket(packet, options, PF_SRTP_BYPASS); |
| 408 | } |
| 409 |
| 410 | void BaseChannel::OnRtpPacket(const webrtc::RtpPacketReceived& parsed_packet) { |
| 411 | RTC_DCHECK_RUN_ON(network_thread()); |
| 412 | RTC_DCHECK(network_initialized()); |
| 413 |
| 414 | if (on_first_packet_received_) { |
| 415 | on_first_packet_received_(); |
| 416 | on_first_packet_received_ = nullptr; |
| 417 | } |
| 418 |
| 419 | if (!srtp_active() && srtp_required_) { |
| 420 | // Our session description indicates that SRTP is required, but we got a |
| 421 | // packet before our SRTP filter is active. This means either that |
| 422 | // a) we got SRTP packets before we received the SDES keys, in which case |
| 423 | // we can't decrypt it anyway, or |
| 424 | // b) we got SRTP packets before DTLS completed on both the RTP and RTCP |
| 425 | // transports, so we haven't yet extracted keys, even if DTLS did |
| 426 | // complete on the transport that the packets are being sent on. It's |
| 427 | // really good practice to wait for both RTP and RTCP to be good to go |
| 428 | // before sending media, to prevent weird failure modes, so it's fine |
| 429 | // for us to just eat packets here. This is all sidestepped if RTCP mux |
| 430 | // is used anyway. |
| 431 | RTC_LOG(LS_WARNING) << "Can't process incoming RTP packet when " |
| 432 | "SRTP is inactive and crypto is required " |
| 433 | << ToString(); |
| 434 | return; |
| 435 | } |
| 436 |
| 437 | webrtc::Timestamp packet_time = parsed_packet.arrival_time(); |
| 438 | if (media_channel_) { |
| 439 | media_channel_->OnPacketReceived( |
| 440 | parsed_packet.Buffer(), |
| 441 | packet_time.IsMinusInfinity() ? -1 : packet_time.us()); |
| 442 | } |
| 443 | } |
| 444 |
| 445 | bool BaseChannel::MaybeUpdateDemuxerAndRtpExtensions_w( |
| 446 | bool update_demuxer, |
| 447 | absl::optional<RtpHeaderExtensions> extensions, |
| 448 | std::string& error_desc) { |
| 449 | if (extensions) { |
| 450 | if (rtp_header_extensions_ == extensions) { |
| 451 | extensions.reset(); // No need to update header extensions. |
| 452 | } else { |
| 453 | rtp_header_extensions_ = *extensions; |
| 454 | } |
| 455 | } |
| 456 |
| 457 | if (!update_demuxer && !extensions) |
| 458 | return true; // No update needed. |
| 459 |
| 460 | // TODO(bugs.webrtc.org/13536): See if we can do this asynchronously. |
| 461 |
| 462 | if (update_demuxer) |
| 463 | media_channel()->OnDemuxerCriteriaUpdatePending(); |
| 464 |
| 465 | bool success = network_thread()->BlockingCall([&]() mutable { |
| 466 | RTC_DCHECK_RUN_ON(network_thread()); |
| 467 | // NOTE: This doesn't take the BUNDLE case in account meaning the RTP header |
| 468 | // extension maps are not merged when BUNDLE is enabled. This is fine |
| 469 | // because the ID for MID should be consistent among all the RTP transports. |
| 470 | if (extensions) |
| 471 | rtp_transport_->UpdateRtpHeaderExtensionMap(*extensions); |
| 472 |
| 473 | if (!update_demuxer) |
| 474 | return true; |
| 475 |
| 476 | if (!rtp_transport_->RegisterRtpDemuxerSink(demuxer_criteria_, this)) { |
| 477 | error_desc = |
| 478 | StringFormat("Failed to apply demuxer criteria for '%s': '%s'.", |
| 479 | mid().c_str(), demuxer_criteria_.ToString().c_str()); |
| 480 | return false; |
| 481 | } |
| 482 | return true; |
| 483 | }); |
| 484 |
| 485 | if (update_demuxer) |
| 486 | media_channel()->OnDemuxerCriteriaUpdateComplete(); |
| 487 |
| 488 | return success; |
| 489 | } |
| 490 |
| 491 | bool BaseChannel::RegisterRtpDemuxerSink_w() { |
| 492 | media_channel_->OnDemuxerCriteriaUpdatePending(); |
| 493 | // Copy demuxer criteria, since they're a worker-thread variable |
| 494 | // and we want to pass them to the network thread |
| 495 | bool ret = network_thread_->BlockingCall( |
| 496 | [this, demuxer_criteria = demuxer_criteria_] { |
| 497 | RTC_DCHECK_RUN_ON(network_thread()); |
| 498 | if (!rtp_transport_) { |
| 499 | // Transport was disconnected before attempting to update the |
| 500 | // criteria. This can happen while setting the remote description. |
| 501 | // See chromium:1295469 for an example. |
| 502 | return false; |
| 503 | } |
| 504 | // Note that RegisterRtpDemuxerSink first unregisters the sink if |
| 505 | // already registered. So this will change the state of the class |
| 506 | // whether the call succeeds or not. |
| 507 | return rtp_transport_->RegisterRtpDemuxerSink(demuxer_criteria, this); |
| 508 | }); |
| 509 |
| 510 | media_channel_->OnDemuxerCriteriaUpdateComplete(); |
| 511 |
| 512 | return ret; |
| 513 | } |
| 514 |
| 515 | void BaseChannel::EnableMedia_w() { |
| 516 | if (enabled_) |
| 517 | return; |
| 518 |
| 519 | RTC_LOG(LS_INFO) << "Channel enabled: " << ToString(); |
| 520 | enabled_ = true; |
| 521 | UpdateMediaSendRecvState_w(); |
| 522 | } |
| 523 |
| 524 | void BaseChannel::DisableMedia_w() { |
| 525 | if (!enabled_) |
| 526 | return; |
| 527 |
| 528 | RTC_LOG(LS_INFO) << "Channel disabled: " << ToString(); |
| 529 | enabled_ = false; |
| 530 | UpdateMediaSendRecvState_w(); |
| 531 | } |
| 532 |
| 533 | void BaseChannel::UpdateWritableState_n() { |
| 534 | TRACE_EVENT0("webrtc", "BaseChannel::UpdateWritableState_n"); |
| 535 | if (rtp_transport_->IsWritable(/*rtcp=*/true) && |
| 536 | rtp_transport_->IsWritable(/*rtcp=*/false)) { |
| 537 | ChannelWritable_n(); |
| 538 | } else { |
| 539 | ChannelNotWritable_n(); |
| 540 | } |
| 541 | } |
| 542 |
| 543 | void BaseChannel::ChannelWritable_n() { |
| 544 | TRACE_EVENT0("webrtc", "BaseChannel::ChannelWritable_n"); |
| 545 | if (writable_) { |
| 546 | return; |
| 547 | } |
| 548 | writable_ = true; |
| 549 | RTC_LOG(LS_INFO) << "Channel writable (" << ToString() << ")" |
| 550 | << (was_ever_writable_n_ ? "" : " for the first time"); |
| 551 | // We only have to do this PostTask once, when first transitioning to |
| 552 | // writable. |
| 553 | if (!was_ever_writable_n_) { |
| 554 | worker_thread_->PostTask(SafeTask(alive_, [this] { |
| 555 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 556 | was_ever_writable_ = true; |
| 557 | UpdateMediaSendRecvState_w(); |
| 558 | })); |
| 559 | } |
| 560 | was_ever_writable_n_ = true; |
| 561 | } |
| 562 |
| 563 | void BaseChannel::ChannelNotWritable_n() { |
| 564 | TRACE_EVENT0("webrtc", "BaseChannel::ChannelNotWritable_n"); |
| 565 | if (!writable_) { |
| 566 | return; |
| 567 | } |
| 568 | writable_ = false; |
| 569 | RTC_LOG(LS_INFO) << "Channel not writable (" << ToString() << ")"; |
| 570 | } |
| 571 |
| 572 | bool BaseChannel::SetPayloadTypeDemuxingEnabled_w(bool enabled) { |
| 573 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 574 |
| 575 | if (enabled == payload_type_demuxing_enabled_) { |
| 576 | return true; |
| 577 | } |
| 578 |
| 579 | payload_type_demuxing_enabled_ = enabled; |
| 580 |
| 581 | bool config_changed = false; |
| 582 |
| 583 | if (!enabled) { |
| 584 | // TODO(crbug.com/11477): This will remove *all* unsignaled streams (those |
| 585 | // without an explicitly signaled SSRC), which may include streams that |
| 586 | // were matched to this channel by MID or RID. Ideally we'd remove only the |
| 587 | // streams that were matched based on payload type alone, but currently |
| 588 | // there is no straightforward way to identify those streams. |
| 589 | media_channel()->ResetUnsignaledRecvStream(); |
| 590 | if (!demuxer_criteria_.payload_types().empty()) { |
| 591 | config_changed = true; |
| 592 | demuxer_criteria_.payload_types().clear(); |
| 593 | } |
| 594 | } else if (!payload_types_.empty()) { |
| 595 | for (const auto& type : payload_types_) { |
| 596 | if (demuxer_criteria_.payload_types().insert(type).second) { |
| 597 | config_changed = true; |
| 598 | } |
| 599 | } |
| 600 | } else { |
| 601 | RTC_DCHECK(demuxer_criteria_.payload_types().empty()); |
| 602 | } |
| 603 |
| 604 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0); |
| 605 |
| 606 | if (!config_changed) |
| 607 | return true; |
| 608 |
| 609 | // Note: This synchronously hops to the network thread. |
| 610 | return RegisterRtpDemuxerSink_w(); |
| 611 | } |
| 612 |
| 613 | bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams, |
| 614 | SdpType type, |
| 615 | std::string& error_desc) { |
| 616 | // In the case of RIDs (where SSRCs are not negotiated), this method will |
| 617 | // generate an SSRC for each layer in StreamParams. That representation will |
| 618 | // be stored internally in `local_streams_`. |
| 619 | // In subsequent offers, the same stream can appear in `streams` again |
| 620 | // (without the SSRCs), so it should be looked up using RIDs (if available) |
| 621 | // and then by primary SSRC. |
| 622 | // In both scenarios, it is safe to assume that the media channel will be |
| 623 | // created with a StreamParams object with SSRCs. However, it is not safe to |
| 624 | // assume that `local_streams_` will always have SSRCs as there are scenarios |
| 625 | // in which niether SSRCs or RIDs are negotiated. |
| 626 |
| 627 | // Check for streams that have been removed. |
| 628 | bool ret = true; |
| 629 | for (const StreamParams& old_stream : local_streams_) { |
| 630 | if (!old_stream.has_ssrcs() || |
| 631 | GetStream(streams, StreamFinder(&old_stream))) { |
| 632 | continue; |
| 633 | } |
| 634 | if (!media_channel()->RemoveSendStream(old_stream.first_ssrc())) { |
| 635 | error_desc = StringFormat( |
| 636 | "Failed to remove send stream with ssrc %u from m-section with " |
| 637 | "mid='%s'.", |
| 638 | old_stream.first_ssrc(), mid().c_str()); |
| 639 | ret = false; |
| 640 | } |
| 641 | } |
| 642 | // Check for new streams. |
| 643 | std::vector<StreamParams> all_streams; |
| 644 | for (const StreamParams& stream : streams) { |
| 645 | StreamParams* existing = GetStream(local_streams_, StreamFinder(&stream)); |
| 646 | if (existing) { |
| 647 | // Parameters cannot change for an existing stream. |
| 648 | all_streams.push_back(*existing); |
| 649 | continue; |
| 650 | } |
| 651 |
| 652 | all_streams.push_back(stream); |
| 653 | StreamParams& new_stream = all_streams.back(); |
| 654 |
| 655 | if (!new_stream.has_ssrcs() && !new_stream.has_rids()) { |
| 656 | continue; |
| 657 | } |
| 658 |
| 659 | RTC_DCHECK(new_stream.has_ssrcs() || new_stream.has_rids()); |
| 660 | if (new_stream.has_ssrcs() && new_stream.has_rids()) { |
| 661 | error_desc = StringFormat( |
| 662 | "Failed to add send stream: %u into m-section with mid='%s'. Stream " |
| 663 | "has both SSRCs and RIDs.", |
| 664 | new_stream.first_ssrc(), mid().c_str()); |
| 665 | ret = false; |
| 666 | continue; |
| 667 | } |
| 668 |
| 669 | // At this point we use the legacy simulcast group in StreamParams to |
| 670 | // indicate that we want multiple layers to the media channel. |
| 671 | if (!new_stream.has_ssrcs()) { |
| 672 | // TODO(bugs.webrtc.org/10250): Indicate if flex is desired here. |
| 673 | new_stream.GenerateSsrcs(new_stream.rids().size(), /* rtx = */ true, |
| 674 | /* flex_fec = */ false, ssrc_generator_); |
| 675 | } |
| 676 |
| 677 | if (media_channel()->AddSendStream(new_stream)) { |
| 678 | RTC_LOG(LS_INFO) << "Add send stream ssrc: " << new_stream.ssrcs[0] |
| 679 | << " into " << ToString(); |
| 680 | } else { |
| 681 | error_desc = StringFormat( |
| 682 | "Failed to add send stream ssrc: %u into m-section with mid='%s'", |
| 683 | new_stream.first_ssrc(), mid().c_str()); |
| 684 | ret = false; |
| 685 | } |
| 686 | } |
| 687 | local_streams_ = all_streams; |
| 688 | return ret; |
| 689 | } |
| 690 |
| 691 | bool BaseChannel::UpdateRemoteStreams_w(const MediaContentDescription* content, |
| 692 | SdpType type, |
| 693 | std::string& error_desc) { |
| 694 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 695 | bool needs_re_registration = false; |
| 696 | if (!webrtc::RtpTransceiverDirectionHasSend(content->direction())) { |
| 697 | RTC_DLOG(LS_VERBOSE) << "UpdateRemoteStreams_w: remote side will not send " |
| 698 | "- disable payload type demuxing for " |
| 699 | << ToString(); |
| 700 | if (ClearHandledPayloadTypes()) { |
| 701 | needs_re_registration = payload_type_demuxing_enabled_; |
| 702 | } |
| 703 | } |
| 704 |
| 705 | const std::vector<StreamParams>& streams = content->streams(); |
| 706 | const bool new_has_unsignaled_ssrcs = HasStreamWithNoSsrcs(streams); |
| 707 | const bool old_has_unsignaled_ssrcs = HasStreamWithNoSsrcs(remote_streams_); |
| 708 |
| 709 | // Check for streams that have been removed. |
| 710 | for (const StreamParams& old_stream : remote_streams_) { |
| 711 | // If we no longer have an unsignaled stream, we would like to remove |
| 712 | // the unsignaled stream params that are cached. |
| 713 | if (!old_stream.has_ssrcs() && !new_has_unsignaled_ssrcs) { |
| 714 | media_channel()->ResetUnsignaledRecvStream(); |
| 715 | RTC_LOG(LS_INFO) << "Reset unsignaled remote stream for " << ToString() |
| 716 | << "."; |
| 717 | } else if (old_stream.has_ssrcs() && |
| 718 | !GetStreamBySsrc(streams, old_stream.first_ssrc())) { |
| 719 | if (media_channel()->RemoveRecvStream(old_stream.first_ssrc())) { |
| 720 | RTC_LOG(LS_INFO) << "Remove remote ssrc: " << old_stream.first_ssrc() |
| 721 | << " from " << ToString() << "."; |
| 722 | } else { |
| 723 | error_desc = StringFormat( |
| 724 | "Failed to remove remote stream with ssrc %u from m-section with " |
| 725 | "mid='%s'.", |
| 726 | old_stream.first_ssrc(), mid().c_str()); |
| 727 | return false; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 |
| 732 | // Check for new streams. |
| 733 | webrtc::flat_set<uint32_t> ssrcs; |
| 734 | for (const StreamParams& new_stream : streams) { |
| 735 | // We allow a StreamParams with an empty list of SSRCs, in which case the |
| 736 | // MediaChannel will cache the parameters and use them for any unsignaled |
| 737 | // stream received later. |
| 738 | if ((!new_stream.has_ssrcs() && !old_has_unsignaled_ssrcs) || |
| 739 | !GetStreamBySsrc(remote_streams_, new_stream.first_ssrc())) { |
| 740 | if (media_channel()->AddRecvStream(new_stream)) { |
| 741 | RTC_LOG(LS_INFO) << "Add remote ssrc: " |
| 742 | << (new_stream.has_ssrcs() |
| 743 | ? std::to_string(new_stream.first_ssrc()) |
| 744 | : "unsignaled") |
| 745 | << " to " << ToString(); |
| 746 | } else { |
| 747 | error_desc = |
| 748 | StringFormat("Failed to add remote stream ssrc: %s to %s", |
| 749 | new_stream.has_ssrcs() |
| 750 | ? std::to_string(new_stream.first_ssrc()).c_str() |
| 751 | : "unsignaled", |
| 752 | ToString().c_str()); |
| 753 | return false; |
| 754 | } |
| 755 | } |
| 756 | // Update the receiving SSRCs. |
| 757 | ssrcs.insert(new_stream.ssrcs.begin(), new_stream.ssrcs.end()); |
| 758 | } |
| 759 |
| 760 | if (demuxer_criteria_.ssrcs() != ssrcs) { |
| 761 | demuxer_criteria_.ssrcs() = std::move(ssrcs); |
| 762 | needs_re_registration = true; |
| 763 | } |
| 764 |
| 765 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0); |
| 766 |
| 767 | // Re-register the sink to update after changing the demuxer criteria. |
| 768 | if (needs_re_registration && !RegisterRtpDemuxerSink_w()) { |
| 769 | error_desc = StringFormat("Failed to set up audio demuxing for mid='%s'.", |
| 770 | mid().c_str()); |
| 771 | return false; |
| 772 | } |
| 773 |
| 774 | remote_streams_ = streams; |
| 775 |
| 776 | set_remote_content_direction(content->direction()); |
| 777 | UpdateMediaSendRecvState_w(); |
| 778 |
| 779 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1); |
| 780 |
| 781 | return true; |
| 782 | } |
| 783 |
| 784 | RtpHeaderExtensions BaseChannel::GetDeduplicatedRtpHeaderExtensions( |
| 785 | const RtpHeaderExtensions& extensions) { |
| 786 | return webrtc::RtpExtension::DeduplicateHeaderExtensions(extensions, |
| 787 | extensions_filter_); |
| 788 | } |
| 789 |
| 790 | bool BaseChannel::MaybeAddHandledPayloadType(int payload_type) { |
| 791 | bool demuxer_criteria_modified = false; |
| 792 | if (payload_type_demuxing_enabled_) { |
| 793 | demuxer_criteria_modified = demuxer_criteria_.payload_types() |
| 794 | .insert(static_cast<uint8_t>(payload_type)) |
| 795 | .second; |
| 796 | } |
| 797 | // Even if payload type demuxing is currently disabled, we need to remember |
| 798 | // the payload types in case it's re-enabled later. |
| 799 | payload_types_.insert(static_cast<uint8_t>(payload_type)); |
| 800 | return demuxer_criteria_modified; |
| 801 | } |
| 802 |
| 803 | bool BaseChannel::ClearHandledPayloadTypes() { |
| 804 | const bool was_empty = demuxer_criteria_.payload_types().empty(); |
| 805 | demuxer_criteria_.payload_types().clear(); |
| 806 | payload_types_.clear(); |
| 807 | return !was_empty; |
| 808 | } |
| 809 |
| 810 | void BaseChannel::SignalSentPacket_n(const rtc::SentPacket& sent_packet) { |
| 811 | RTC_DCHECK_RUN_ON(network_thread()); |
| 812 | RTC_DCHECK(network_initialized()); |
| 813 | media_channel()->OnPacketSent(sent_packet); |
| 814 | } |
| 815 |
| 816 | VoiceChannel::VoiceChannel(rtc::Thread* worker_thread, |
| 817 | rtc::Thread* network_thread, |
| 818 | rtc::Thread* signaling_thread, |
| 819 | std::unique_ptr<VoiceMediaChannel> media_channel, |
| 820 | absl::string_view mid, |
| 821 | bool srtp_required, |
| 822 | webrtc::CryptoOptions crypto_options, |
| 823 | UniqueRandomIdGenerator* ssrc_generator) |
| 824 | : BaseChannel(worker_thread, |
| 825 | network_thread, |
| 826 | signaling_thread, |
| 827 | std::move(media_channel), |
| 828 | mid, |
| 829 | srtp_required, |
| 830 | crypto_options, |
| 831 | ssrc_generator) {} |
| 832 |
| 833 | VoiceChannel::~VoiceChannel() { |
| 834 | TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel"); |
| 835 | // this can't be done in the base class, since it calls a virtual |
| 836 | DisableMedia_w(); |
| 837 | } |
| 838 |
| 839 | void VoiceChannel::UpdateMediaSendRecvState_w() { |
| 840 | // Render incoming data if we're the active call, and we have the local |
| 841 | // content. We receive data on the default channel and multiplexed streams. |
| 842 | bool ready_to_receive = enabled() && webrtc::RtpTransceiverDirectionHasRecv( |
| 843 | local_content_direction()); |
| 844 | media_channel()->SetPlayout(ready_to_receive); |
| 845 |
| 846 | // Send outgoing data if we're the active call, we have the remote content, |
| 847 | // and we have had some form of connectivity. |
| 848 | bool send = IsReadyToSendMedia_w(); |
| 849 | media_channel()->SetSend(send); |
| 850 |
| 851 | RTC_LOG(LS_INFO) << "Changing voice state, recv=" << ready_to_receive |
| 852 | << " send=" << send << " for " << ToString(); |
| 853 | } |
| 854 |
| 855 | bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content, |
| 856 | SdpType type, |
| 857 | std::string& error_desc) { |
| 858 | TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w"); |
| 859 | RTC_DLOG(LS_INFO) << "Setting local voice description for " << ToString(); |
| 860 |
| 861 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 862 |
| 863 | RtpHeaderExtensions header_extensions = |
| 864 | GetDeduplicatedRtpHeaderExtensions(content->rtp_header_extensions()); |
| 865 | bool update_header_extensions = true; |
| 866 | media_channel()->SetExtmapAllowMixed(content->extmap_allow_mixed()); |
| 867 |
| 868 | AudioRecvParameters recv_params = last_recv_params_; |
| 869 | RtpParametersFromMediaDescription( |
| 870 | content->as_audio(), header_extensions, |
| 871 | webrtc::RtpTransceiverDirectionHasRecv(content->direction()), |
| 872 | &recv_params); |
| 873 |
| 874 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 875 | error_desc = StringFormat( |
| 876 | "Failed to set local audio description recv parameters for m-section " |
| 877 | "with mid='%s'.", |
| 878 | mid().c_str()); |
| 879 | return false; |
| 880 | } |
| 881 |
| 882 | bool criteria_modified = false; |
| 883 | if (webrtc::RtpTransceiverDirectionHasRecv(content->direction())) { |
| 884 | for (const AudioCodec& codec : content->as_audio()->codecs()) { |
| 885 | if (MaybeAddHandledPayloadType(codec.id)) { |
| 886 | criteria_modified = true; |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 |
| 891 | last_recv_params_ = recv_params; |
| 892 |
| 893 | if (!UpdateLocalStreams_w(content->as_audio()->streams(), type, error_desc)) { |
| 894 | RTC_DCHECK(!error_desc.empty()); |
| 895 | return false; |
| 896 | } |
| 897 |
| 898 | set_local_content_direction(content->direction()); |
| 899 | UpdateMediaSendRecvState_w(); |
| 900 |
| 901 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0); |
| 902 |
| 903 | bool success = MaybeUpdateDemuxerAndRtpExtensions_w( |
| 904 | criteria_modified, |
| 905 | update_header_extensions |
| 906 | ? absl::optional<RtpHeaderExtensions>(std::move(header_extensions)) |
| 907 | : absl::nullopt, |
| 908 | error_desc); |
| 909 |
| 910 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1); |
| 911 |
| 912 | return success; |
| 913 | } |
| 914 |
| 915 | bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content, |
| 916 | SdpType type, |
| 917 | std::string& error_desc) { |
| 918 | TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w"); |
| 919 | RTC_LOG(LS_INFO) << "Setting remote voice description for " << ToString(); |
| 920 |
| 921 | AudioSendParameters send_params = last_send_params_; |
| 922 | RtpSendParametersFromMediaDescription(content->as_audio(), |
| 923 | extensions_filter(), &send_params); |
| 924 | send_params.mid = mid(); |
| 925 |
| 926 | bool parameters_applied = media_channel()->SetSendParameters(send_params); |
| 927 | if (!parameters_applied) { |
| 928 | error_desc = StringFormat( |
| 929 | "Failed to set remote audio description send parameters for m-section " |
| 930 | "with mid='%s'.", |
| 931 | mid().c_str()); |
| 932 | return false; |
| 933 | } |
| 934 | last_send_params_ = send_params; |
| 935 |
| 936 | return UpdateRemoteStreams_w(content, type, error_desc); |
| 937 | } |
| 938 |
| 939 | VideoChannel::VideoChannel(rtc::Thread* worker_thread, |
| 940 | rtc::Thread* network_thread, |
| 941 | rtc::Thread* signaling_thread, |
| 942 | std::unique_ptr<VideoMediaChannel> media_channel, |
| 943 | absl::string_view mid, |
| 944 | bool srtp_required, |
| 945 | webrtc::CryptoOptions crypto_options, |
| 946 | UniqueRandomIdGenerator* ssrc_generator) |
| 947 | : BaseChannel(worker_thread, |
| 948 | network_thread, |
| 949 | signaling_thread, |
| 950 | std::move(media_channel), |
| 951 | mid, |
| 952 | srtp_required, |
| 953 | crypto_options, |
| 954 | ssrc_generator) {} |
| 955 |
| 956 | VideoChannel::~VideoChannel() { |
| 957 | TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel"); |
| 958 | // this can't be done in the base class, since it calls a virtual |
| 959 | DisableMedia_w(); |
| 960 | } |
| 961 |
| 962 | void VideoChannel::UpdateMediaSendRecvState_w() { |
| 963 | // Send outgoing data if we're the active call, we have the remote content, |
| 964 | // and we have had some form of connectivity. |
| 965 | bool send = IsReadyToSendMedia_w(); |
| 966 | media_channel()->SetSend(send); |
| 967 | RTC_LOG(LS_INFO) << "Changing video state, send=" << send << " for " |
| 968 | << ToString(); |
| 969 | } |
| 970 |
| 971 | bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content, |
| 972 | SdpType type, |
| 973 | std::string& error_desc) { |
| 974 | TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w"); |
| 975 | RTC_DLOG(LS_INFO) << "Setting local video description for " << ToString(); |
| 976 |
| 977 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 978 |
| 979 | RtpHeaderExtensions header_extensions = |
| 980 | GetDeduplicatedRtpHeaderExtensions(content->rtp_header_extensions()); |
| 981 | bool update_header_extensions = true; |
| 982 | media_channel()->SetExtmapAllowMixed(content->extmap_allow_mixed()); |
| 983 |
| 984 | VideoRecvParameters recv_params = last_recv_params_; |
| 985 |
| 986 | RtpParametersFromMediaDescription( |
| 987 | content->as_video(), header_extensions, |
| 988 | webrtc::RtpTransceiverDirectionHasRecv(content->direction()), |
| 989 | &recv_params); |
| 990 |
| 991 | VideoSendParameters send_params = last_send_params_; |
| 992 |
| 993 | bool needs_send_params_update = false; |
| 994 | if (type == SdpType::kAnswer || type == SdpType::kPrAnswer) { |
| 995 | for (auto& send_codec : send_params.codecs) { |
| 996 | auto* recv_codec = FindMatchingCodec(recv_params.codecs, send_codec); |
| 997 | if (recv_codec) { |
| 998 | if (!recv_codec->packetization && send_codec.packetization) { |
| 999 | send_codec.packetization.reset(); |
| 1000 | needs_send_params_update = true; |
| 1001 | } else if (recv_codec->packetization != send_codec.packetization) { |
| 1002 | error_desc = StringFormat( |
| 1003 | "Failed to set local answer due to invalid codec packetization " |
| 1004 | "specified in m-section with mid='%s'.", |
| 1005 | mid().c_str()); |
| 1006 | return false; |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 |
| 1012 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 1013 | error_desc = StringFormat( |
| 1014 | "Failed to set local video description recv parameters for m-section " |
| 1015 | "with mid='%s'.", |
| 1016 | mid().c_str()); |
| 1017 | return false; |
| 1018 | } |
| 1019 |
| 1020 | bool criteria_modified = false; |
| 1021 | if (webrtc::RtpTransceiverDirectionHasRecv(content->direction())) { |
| 1022 | for (const VideoCodec& codec : content->as_video()->codecs()) { |
| 1023 | if (MaybeAddHandledPayloadType(codec.id)) |
| 1024 | criteria_modified = true; |
| 1025 | } |
| 1026 | } |
| 1027 |
| 1028 | last_recv_params_ = recv_params; |
| 1029 |
| 1030 | if (needs_send_params_update) { |
| 1031 | if (!media_channel()->SetSendParameters(send_params)) { |
| 1032 | error_desc = StringFormat( |
| 1033 | "Failed to set send parameters for m-section with mid='%s'.", |
| 1034 | mid().c_str()); |
| 1035 | return false; |
| 1036 | } |
| 1037 | last_send_params_ = send_params; |
| 1038 | } |
| 1039 |
| 1040 | if (!UpdateLocalStreams_w(content->as_video()->streams(), type, error_desc)) { |
| 1041 | RTC_DCHECK(!error_desc.empty()); |
| 1042 | return false; |
| 1043 | } |
| 1044 |
| 1045 | set_local_content_direction(content->direction()); |
| 1046 | UpdateMediaSendRecvState_w(); |
| 1047 |
| 1048 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0); |
| 1049 |
| 1050 | bool success = MaybeUpdateDemuxerAndRtpExtensions_w( |
| 1051 | criteria_modified, |
| 1052 | update_header_extensions |
| 1053 | ? absl::optional<RtpHeaderExtensions>(std::move(header_extensions)) |
| 1054 | : absl::nullopt, |
| 1055 | error_desc); |
| 1056 |
| 1057 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1); |
| 1058 |
| 1059 | return success; |
| 1060 | } |
| 1061 |
| 1062 | bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content, |
| 1063 | SdpType type, |
| 1064 | std::string& error_desc) { |
| 1065 | TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w"); |
| 1066 | RTC_LOG(LS_INFO) << "Setting remote video description for " << ToString(); |
| 1067 |
| 1068 | const VideoContentDescription* video = content->as_video(); |
| 1069 |
| 1070 | VideoSendParameters send_params = last_send_params_; |
| 1071 | RtpSendParametersFromMediaDescription(video, extensions_filter(), |
| 1072 | &send_params); |
| 1073 | send_params.mid = mid(); |
| 1074 | send_params.conference_mode = video->conference_mode(); |
| 1075 |
| 1076 | VideoRecvParameters recv_params = last_recv_params_; |
| 1077 |
| 1078 | bool needs_recv_params_update = false; |
| 1079 | if (type == SdpType::kAnswer || type == SdpType::kPrAnswer) { |
| 1080 | for (auto& recv_codec : recv_params.codecs) { |
| 1081 | auto* send_codec = FindMatchingCodec(send_params.codecs, recv_codec); |
| 1082 | if (send_codec) { |
| 1083 | if (!send_codec->packetization && recv_codec.packetization) { |
| 1084 | recv_codec.packetization.reset(); |
| 1085 | needs_recv_params_update = true; |
| 1086 | } else if (send_codec->packetization != recv_codec.packetization) { |
| 1087 | error_desc = StringFormat( |
| 1088 | "Failed to set remote answer due to invalid codec packetization " |
| 1089 | "specifid in m-section with mid='%s'.", |
| 1090 | mid().c_str()); |
| 1091 | return false; |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 |
| 1097 | if (!media_channel()->SetSendParameters(send_params)) { |
| 1098 | error_desc = StringFormat( |
| 1099 | "Failed to set remote video description send parameters for m-section " |
| 1100 | "with mid='%s'.", |
| 1101 | mid().c_str()); |
| 1102 | return false; |
| 1103 | } |
| 1104 | last_send_params_ = send_params; |
| 1105 |
| 1106 | if (needs_recv_params_update) { |
| 1107 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 1108 | error_desc = StringFormat( |
| 1109 | "Failed to set recv parameters for m-section with mid='%s'.", |
| 1110 | mid().c_str()); |
| 1111 | return false; |
| 1112 | } |
| 1113 | last_recv_params_ = recv_params; |
| 1114 | } |
| 1115 |
| 1116 | return UpdateRemoteStreams_w(content, type, error_desc); |
| 1117 | } |
| 1118 |
| 1119 | } // namespace cricket |
| 1120 |