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