| 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/rtp_transceiver.h" |
| 12 |
| 13 | #include <stdint.h> |
| 14 |
| 15 | #include <iterator> |
| 16 | #include <set> |
| 17 | #include <string> |
| 18 | #include <utility> |
| 19 | #include <vector> |
| 20 |
| 21 | #include "absl/algorithm/container.h" |
| 22 | #include "absl/memory/memory.h" |
| 23 | #include "api/audio_codecs/audio_codec_pair_id.h" |
| 24 | #include "api/field_trials_view.h" |
| 25 | #include "api/rtp_parameters.h" |
| 26 | #include "api/sequence_checker.h" |
| 27 | #include "media/base/codec.h" |
| 28 | #include "media/base/media_channel.h" |
| 29 | #include "media/base/media_channel_impl.h" |
| 30 | #include "media/base/media_constants.h" |
| 31 | #include "media/base/media_engine.h" |
| 32 | #include "pc/channel.h" |
| 33 | #include "pc/rtp_media_utils.h" |
| 34 | #include "pc/session_description.h" |
| 35 | #include "rtc_base/checks.h" |
| 36 | #include "rtc_base/logging.h" |
| 37 | #include "rtc_base/thread.h" |
| 38 |
| 39 | namespace webrtc { |
| 40 | namespace { |
| 41 |
| 42 | RTCError VerifyCodecPreferences( |
| 43 | const std::vector<RtpCodecCapability>& codecs, |
| 44 | const std::vector<cricket::Codec>& send_codecs, |
| 45 | const std::vector<cricket::Codec>& recv_codecs) { |
| 46 | // If the intersection between codecs and |
| 47 | // RTCRtpSender.getCapabilities(kind).codecs or the intersection between |
| 48 | // codecs and RTCRtpReceiver.getCapabilities(kind).codecs only contains RTX, |
| 49 | // RED or FEC codecs or is an empty set, throw InvalidModificationError. |
| 50 | // This ensures that we always have something to offer, regardless of |
| 51 | // transceiver.direction. |
| 52 |
| 53 | if (!absl::c_any_of(codecs, [&recv_codecs](const RtpCodecCapability& codec) { |
| 54 | return codec.IsMediaCodec() && |
| 55 | absl::c_any_of(recv_codecs, |
| 56 | [&codec](const cricket::Codec& recv_codec) { |
| 57 | return recv_codec.MatchesRtpCodec(codec); |
| 58 | }); |
| 59 | })) { |
| 60 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 61 | "Invalid codec preferences: Missing codec from recv " |
| 62 | "codec capabilities."); |
| 63 | } |
| 64 |
| 65 | if (!absl::c_any_of(codecs, [&send_codecs](const RtpCodecCapability& codec) { |
| 66 | return codec.IsMediaCodec() && |
| 67 | absl::c_any_of(send_codecs, |
| 68 | [&codec](const cricket::Codec& send_codec) { |
| 69 | return send_codec.MatchesRtpCodec(codec); |
| 70 | }); |
| 71 | })) { |
| 72 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 73 | "Invalid codec preferences: Missing codec from send " |
| 74 | "codec capabilities."); |
| 75 | } |
| 76 |
| 77 | // Let codecCapabilities be the union of |
| 78 | // RTCRtpSender.getCapabilities(kind).codecs and |
| 79 | // RTCRtpReceiver.getCapabilities(kind).codecs. For each codec in codecs, If |
| 80 | // codec is not in codecCapabilities, throw InvalidModificationError. |
| 81 | for (const auto& codec_preference : codecs) { |
| 82 | bool is_recv_codec = absl::c_any_of( |
| 83 | recv_codecs, [&codec_preference](const cricket::Codec& codec) { |
| 84 | return codec.MatchesRtpCodec(codec_preference); |
| 85 | }); |
| 86 |
| 87 | bool is_send_codec = absl::c_any_of( |
| 88 | send_codecs, [&codec_preference](const cricket::Codec& codec) { |
| 89 | return codec.MatchesRtpCodec(codec_preference); |
| 90 | }); |
| 91 |
| 92 | if (!is_recv_codec && !is_send_codec) { |
| 93 | LOG_AND_RETURN_ERROR( |
| 94 | RTCErrorType::INVALID_MODIFICATION, |
| 95 | std::string("Invalid codec preferences: invalid codec with name \"") + |
| 96 | codec_preference.name + "\"."); |
| 97 | } |
| 98 | } |
| 99 |
| 100 | // Check we have a real codec (not just rtx, red, fec or CN) |
| 101 | if (absl::c_all_of(codecs, [](const RtpCodecCapability& codec) { |
| 102 | return !codec.IsMediaCodec(); |
| 103 | })) { |
| 104 | LOG_AND_RETURN_ERROR( |
| 105 | RTCErrorType::INVALID_MODIFICATION, |
| 106 | "Invalid codec preferences: codec list must have a non " |
| 107 | "RTX, RED or FEC entry."); |
| 108 | } |
| 109 |
| 110 | return RTCError::OK(); |
| 111 | } |
| 112 |
| 113 | // Matches the list of codecs as capabilities (potentially without SVC related |
| 114 | // information) to the list of send codecs and returns the list of codecs with |
| 115 | // all the SVC related information. |
| 116 | std::vector<cricket::VideoCodec> MatchCodecPreferences( |
| 117 | const std::vector<RtpCodecCapability>& codecs, |
| 118 | const std::vector<cricket::VideoCodec>& send_codecs) { |
| 119 | std::vector<cricket::VideoCodec> result; |
| 120 |
| 121 | for (const auto& codec_preference : codecs) { |
| 122 | for (const cricket::VideoCodec& send_codec : send_codecs) { |
| 123 | if (send_codec.MatchesRtpCodec(codec_preference)) { |
| 124 | result.push_back(send_codec); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 |
| 129 | return result; |
| 130 | } |
| 131 |
| 132 | TaskQueueBase* GetCurrentTaskQueueOrThread() { |
| 133 | TaskQueueBase* current = TaskQueueBase::Current(); |
| 134 | if (!current) |
| 135 | current = rtc::ThreadManager::Instance()->CurrentThread(); |
| 136 | return current; |
| 137 | } |
| 138 |
| 139 | } // namespace |
| 140 |
| 141 | RtpTransceiver::RtpTransceiver(cricket::MediaType media_type, |
| 142 | ConnectionContext* context) |
| 143 | : thread_(GetCurrentTaskQueueOrThread()), |
| 144 | unified_plan_(false), |
| 145 | media_type_(media_type), |
| 146 | context_(context) { |
| 147 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 148 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 149 | } |
| 150 |
| 151 | RtpTransceiver::RtpTransceiver( |
| 152 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 153 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 154 | receiver, |
| 155 | ConnectionContext* context, |
| 156 | std::vector<RtpHeaderExtensionCapability> header_extensions_to_negotiate, |
| 157 | std::function<void()> on_negotiation_needed) |
| 158 | : thread_(GetCurrentTaskQueueOrThread()), |
| 159 | unified_plan_(true), |
| 160 | media_type_(sender->media_type()), |
| 161 | context_(context), |
| 162 | header_extensions_to_negotiate_( |
| 163 | std::move(header_extensions_to_negotiate)), |
| 164 | on_negotiation_needed_(std::move(on_negotiation_needed)) { |
| 165 | RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO || |
| 166 | media_type_ == cricket::MEDIA_TYPE_VIDEO); |
| 167 | RTC_DCHECK_EQ(sender->media_type(), receiver->media_type()); |
| 168 | sender->internal()->SetCodecPreferences( |
| 169 | sender->media_type() == cricket::MEDIA_TYPE_VIDEO |
| 170 | ? media_engine()->video().send_codecs(false) |
| 171 | : media_engine()->voice().send_codecs()); |
| 172 | senders_.push_back(sender); |
| 173 | receivers_.push_back(receiver); |
| 174 |
| 175 | // Set default header extensions depending on whether simulcast/SVC is used. |
| 176 | RtpParameters parameters = sender->internal()->GetParametersInternal(); |
| 177 | bool uses_simulcast = parameters.encodings.size() > 1; |
| 178 | bool uses_svc = !parameters.encodings.empty() && |
| 179 | parameters.encodings[0].scalability_mode.has_value() && |
| 180 | parameters.encodings[0].scalability_mode != |
| 181 | ScalabilityModeToString(ScalabilityMode::kL1T1); |
| 182 | if (uses_simulcast || uses_svc) { |
| 183 | // Enable DD and VLA extensions, can be deactivated by the API. |
| 184 | // Skip this if the GFD extension was enabled via field trial |
| 185 | // for backward compability reasons. |
| 186 | bool uses_gfd = |
| 187 | absl::c_find_if( |
| 188 | header_extensions_to_negotiate_, |
| 189 | [](const RtpHeaderExtensionCapability& ext) { |
| 190 | return ext.uri == RtpExtension::kGenericFrameDescriptorUri00 && |
| 191 | ext.direction != webrtc::RtpTransceiverDirection::kStopped; |
| 192 | }) != header_extensions_to_negotiate_.end(); |
| 193 | if (!uses_gfd) { |
| 194 | for (RtpHeaderExtensionCapability& ext : |
| 195 | header_extensions_to_negotiate_) { |
| 196 | if (ext.uri == RtpExtension::kVideoLayersAllocationUri || |
| 197 | ext.uri == RtpExtension::kDependencyDescriptorUri) { |
| 198 | ext.direction = RtpTransceiverDirection::kSendRecv; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 |
| 205 | RtpTransceiver::~RtpTransceiver() { |
| 206 | // TODO(tommi): On Android, when running PeerConnectionClientTest (e.g. |
| 207 | // PeerConnectionClientTest#testCameraSwitch), the instance doesn't get |
| 208 | // deleted on `thread_`. See if we can fix that. |
| 209 | if (!stopped_) { |
| 210 | RTC_DCHECK_RUN_ON(thread_); |
| 211 | StopInternal(); |
| 212 | } |
| 213 |
| 214 | RTC_CHECK(!channel_) << "Missing call to ClearChannel?"; |
| 215 | } |
| 216 |
| 217 | RTCError RtpTransceiver::CreateChannel( |
| 218 | absl::string_view mid, |
| 219 | Call* call_ptr, |
| 220 | const cricket::MediaConfig& media_config, |
| 221 | bool srtp_required, |
| 222 | CryptoOptions crypto_options, |
| 223 | const cricket::AudioOptions& audio_options, |
| 224 | const cricket::VideoOptions& video_options, |
| 225 | VideoBitrateAllocatorFactory* video_bitrate_allocator_factory, |
| 226 | std::function<RtpTransportInternal*(absl::string_view)> transport_lookup) { |
| 227 | RTC_DCHECK_RUN_ON(thread_); |
| 228 | if (!media_engine()) { |
| 229 | // TODO(hta): Must be a better way |
| 230 | return RTCError(RTCErrorType::INTERNAL_ERROR, |
| 231 | "No media engine for mid=" + std::string(mid)); |
| 232 | } |
| 233 | std::unique_ptr<cricket::ChannelInterface> new_channel; |
| 234 | if (media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 235 | // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to |
| 236 | // the worker thread. We shouldn't be using the `call_ptr_` hack here but |
| 237 | // simply be on the worker thread and use `call_` (update upstream code). |
| 238 | RTC_DCHECK(call_ptr); |
| 239 | RTC_DCHECK(media_engine()); |
| 240 | // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in |
| 241 | // PeerConnection and add the expectation that we're already on the right |
| 242 | // thread. |
| 243 | context()->worker_thread()->BlockingCall([&] { |
| 244 | RTC_DCHECK_RUN_ON(context()->worker_thread()); |
| 245 |
| 246 | AudioCodecPairId codec_pair_id = AudioCodecPairId::Create(); |
| 247 |
| 248 | std::unique_ptr<cricket::VoiceMediaSendChannelInterface> |
| 249 | media_send_channel = media_engine()->voice().CreateSendChannel( |
| 250 | call_ptr, media_config, audio_options, crypto_options, |
| 251 | codec_pair_id); |
| 252 | if (!media_send_channel) { |
| 253 | // TODO(bugs.webrtc.org/14912): Consider CHECK or reporting failure |
| 254 | return; |
| 255 | } |
| 256 | std::unique_ptr<cricket::VoiceMediaReceiveChannelInterface> |
| 257 | media_receive_channel = media_engine()->voice().CreateReceiveChannel( |
| 258 | call_ptr, media_config, audio_options, crypto_options, |
| 259 | codec_pair_id); |
| 260 | if (!media_receive_channel) { |
| 261 | return; |
| 262 | } |
| 263 | // Note that this is safe because both sending and |
| 264 | // receiving channels will be deleted at the same time. |
| 265 | media_send_channel->SetSsrcListChangedCallback( |
| 266 | [receive_channel = |
| 267 | media_receive_channel.get()](const std::set<uint32_t>& choices) { |
| 268 | receive_channel->ChooseReceiverReportSsrc(choices); |
| 269 | }); |
| 270 |
| 271 | new_channel = std::make_unique<cricket::VoiceChannel>( |
| 272 | context()->worker_thread(), context()->network_thread(), |
| 273 | context()->signaling_thread(), std::move(media_send_channel), |
| 274 | std::move(media_receive_channel), mid, srtp_required, crypto_options, |
| 275 | context()->ssrc_generator()); |
| 276 | }); |
| 277 | } else { |
| 278 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, media_type()); |
| 279 |
| 280 | // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to |
| 281 | // the worker thread. We shouldn't be using the `call_ptr_` hack here but |
| 282 | // simply be on the worker thread and use `call_` (update upstream code). |
| 283 | context()->worker_thread()->BlockingCall([&] { |
| 284 | RTC_DCHECK_RUN_ON(context()->worker_thread()); |
| 285 |
| 286 | std::unique_ptr<cricket::VideoMediaSendChannelInterface> |
| 287 | media_send_channel = media_engine()->video().CreateSendChannel( |
| 288 | call_ptr, media_config, video_options, crypto_options, |
| 289 | video_bitrate_allocator_factory); |
| 290 | if (!media_send_channel) { |
| 291 | return; |
| 292 | } |
| 293 |
| 294 | std::unique_ptr<cricket::VideoMediaReceiveChannelInterface> |
| 295 | media_receive_channel = media_engine()->video().CreateReceiveChannel( |
| 296 | call_ptr, media_config, video_options, crypto_options); |
| 297 | if (!media_receive_channel) { |
| 298 | return; |
| 299 | } |
| 300 | // Note that this is safe because both sending and |
| 301 | // receiving channels will be deleted at the same time. |
| 302 | media_send_channel->SetSsrcListChangedCallback( |
| 303 | [receive_channel = |
| 304 | media_receive_channel.get()](const std::set<uint32_t>& choices) { |
| 305 | receive_channel->ChooseReceiverReportSsrc(choices); |
| 306 | }); |
| 307 |
| 308 | new_channel = std::make_unique<cricket::VideoChannel>( |
| 309 | context()->worker_thread(), context()->network_thread(), |
| 310 | context()->signaling_thread(), std::move(media_send_channel), |
| 311 | std::move(media_receive_channel), mid, srtp_required, crypto_options, |
| 312 | context()->ssrc_generator()); |
| 313 | }); |
| 314 | } |
| 315 | if (!new_channel) { |
| 316 | // TODO(hta): Must be a better way |
| 317 | return RTCError(RTCErrorType::INTERNAL_ERROR, |
| 318 | "Failed to create channel for mid=" + std::string(mid)); |
| 319 | } |
| 320 | SetChannel(std::move(new_channel), transport_lookup); |
| 321 | return RTCError::OK(); |
| 322 | } |
| 323 |
| 324 | void RtpTransceiver::SetChannel( |
| 325 | std::unique_ptr<cricket::ChannelInterface> channel, |
| 326 | std::function<RtpTransportInternal*(const std::string&)> transport_lookup) { |
| 327 | RTC_DCHECK_RUN_ON(thread_); |
| 328 | RTC_DCHECK(channel); |
| 329 | RTC_DCHECK(transport_lookup); |
| 330 | RTC_DCHECK(!channel_); |
| 331 | // Cannot set a channel on a stopped transceiver. |
| 332 | if (stopped_) { |
| 333 | return; |
| 334 | } |
| 335 |
| 336 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 337 |
| 338 | RTC_DCHECK_EQ(media_type(), channel->media_type()); |
| 339 | signaling_thread_safety_ = PendingTaskSafetyFlag::Create(); |
| 340 |
| 341 | std::unique_ptr<cricket::ChannelInterface> channel_to_delete; |
| 342 |
| 343 | // An alternative to this, could be to require SetChannel to be called |
| 344 | // on the network thread. The channel object operates for the most part |
| 345 | // on the network thread, as part of its initialization being on the network |
| 346 | // thread is required, so setting a channel object as part of the construction |
| 347 | // (without thread hopping) might be the more efficient thing to do than |
| 348 | // how SetChannel works today. |
| 349 | // Similarly, if the channel() accessor is limited to the network thread, that |
| 350 | // helps with keeping the channel implementation requirements being met and |
| 351 | // avoids synchronization for accessing the pointer or network related state. |
| 352 | context()->network_thread()->BlockingCall([&]() { |
| 353 | if (channel_) { |
| 354 | channel_->SetFirstPacketReceivedCallback(nullptr); |
| 355 | channel_->SetRtpTransport(nullptr); |
| 356 | channel_to_delete = std::move(channel_); |
| 357 | } |
| 358 |
| 359 | channel_ = std::move(channel); |
| 360 |
| 361 | channel_->SetRtpTransport(transport_lookup(channel_->mid())); |
| 362 | channel_->SetFirstPacketReceivedCallback( |
| 363 | [thread = thread_, flag = signaling_thread_safety_, this]() mutable { |
| 364 | thread->PostTask( |
| 365 | SafeTask(std::move(flag), [this]() { OnFirstPacketReceived(); })); |
| 366 | }); |
| 367 | }); |
| 368 | PushNewMediaChannelAndDeleteChannel(nullptr); |
| 369 |
| 370 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
| 371 | } |
| 372 |
| 373 | void RtpTransceiver::ClearChannel() { |
| 374 | RTC_DCHECK_RUN_ON(thread_); |
| 375 |
| 376 | if (!channel_) { |
| 377 | return; |
| 378 | } |
| 379 |
| 380 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 381 |
| 382 | if (channel_) { |
| 383 | signaling_thread_safety_->SetNotAlive(); |
| 384 | signaling_thread_safety_ = nullptr; |
| 385 | } |
| 386 | std::unique_ptr<cricket::ChannelInterface> channel_to_delete; |
| 387 |
| 388 | context()->network_thread()->BlockingCall([&]() { |
| 389 | if (channel_) { |
| 390 | channel_->SetFirstPacketReceivedCallback(nullptr); |
| 391 | channel_->SetRtpTransport(nullptr); |
| 392 | channel_to_delete = std::move(channel_); |
| 393 | } |
| 394 | }); |
| 395 |
| 396 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1); |
| 397 | PushNewMediaChannelAndDeleteChannel(std::move(channel_to_delete)); |
| 398 |
| 399 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
| 400 | } |
| 401 |
| 402 | void RtpTransceiver::PushNewMediaChannelAndDeleteChannel( |
| 403 | std::unique_ptr<cricket::ChannelInterface> channel_to_delete) { |
| 404 | // The clumsy combination of pushing down media channel and deleting |
| 405 | // the channel is due to the desire to do both things in one Invoke(). |
| 406 | if (!channel_to_delete && senders_.empty() && receivers_.empty()) { |
| 407 | return; |
| 408 | } |
| 409 | context()->worker_thread()->BlockingCall([&]() { |
| 410 | // Push down the new media_channel, if any, otherwise clear it. |
| 411 | auto* media_send_channel = |
| 412 | channel_ ? channel_->media_send_channel() : nullptr; |
| 413 | for (const auto& sender : senders_) { |
| 414 | sender->internal()->SetMediaChannel(media_send_channel); |
| 415 | } |
| 416 |
| 417 | auto* media_receive_channel = |
| 418 | channel_ ? channel_->media_receive_channel() : nullptr; |
| 419 | for (const auto& receiver : receivers_) { |
| 420 | receiver->internal()->SetMediaChannel(media_receive_channel); |
| 421 | } |
| 422 |
| 423 | // Destroy the channel, if we had one, now _after_ updating the receivers |
| 424 | // who might have had references to the previous channel. |
| 425 | if (channel_to_delete) { |
| 426 | channel_to_delete.reset(nullptr); |
| 427 | } |
| 428 | }); |
| 429 | } |
| 430 |
| 431 | void RtpTransceiver::AddSender( |
| 432 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) { |
| 433 | RTC_DCHECK_RUN_ON(thread_); |
| 434 | RTC_DCHECK(!stopped_); |
| 435 | RTC_DCHECK(!unified_plan_); |
| 436 | RTC_DCHECK(sender); |
| 437 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
| 438 | RTC_DCHECK(!absl::c_linear_search(senders_, sender)); |
| 439 |
| 440 | std::vector<cricket::Codec> send_codecs = |
| 441 | media_type() == cricket::MEDIA_TYPE_VIDEO |
| 442 | ? media_engine()->video().send_codecs(false) |
| 443 | : media_engine()->voice().send_codecs(); |
| 444 | sender->internal()->SetCodecPreferences( |
| 445 | codec_preferences_.empty() |
| 446 | ? send_codecs |
| 447 | : MatchCodecPreferences(codec_preferences_, send_codecs)); |
| 448 | senders_.push_back(sender); |
| 449 | } |
| 450 |
| 451 | bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) { |
| 452 | RTC_DCHECK(!unified_plan_); |
| 453 | if (sender) { |
| 454 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
| 455 | } |
| 456 | auto it = absl::c_find(senders_, sender); |
| 457 | if (it == senders_.end()) { |
| 458 | return false; |
| 459 | } |
| 460 | (*it)->internal()->Stop(); |
| 461 | senders_.erase(it); |
| 462 | return true; |
| 463 | } |
| 464 |
| 465 | void RtpTransceiver::AddReceiver( |
| 466 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 467 | receiver) { |
| 468 | RTC_DCHECK_RUN_ON(thread_); |
| 469 | RTC_DCHECK(!stopped_); |
| 470 | RTC_DCHECK(!unified_plan_); |
| 471 | RTC_DCHECK(receiver); |
| 472 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
| 473 | RTC_DCHECK(!absl::c_linear_search(receivers_, receiver)); |
| 474 | receivers_.push_back(receiver); |
| 475 | } |
| 476 |
| 477 | bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) { |
| 478 | RTC_DCHECK_RUN_ON(thread_); |
| 479 | RTC_DCHECK(!unified_plan_); |
| 480 | if (receiver) { |
| 481 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
| 482 | } |
| 483 | auto it = absl::c_find(receivers_, receiver); |
| 484 | if (it == receivers_.end()) { |
| 485 | return false; |
| 486 | } |
| 487 |
| 488 | (*it)->internal()->Stop(); |
| 489 | context()->worker_thread()->BlockingCall([&]() { |
| 490 | // `Stop()` will clear the receiver's pointer to the media channel. |
| 491 | (*it)->internal()->SetMediaChannel(nullptr); |
| 492 | }); |
| 493 |
| 494 | receivers_.erase(it); |
| 495 | return true; |
| 496 | } |
| 497 |
| 498 | rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const { |
| 499 | RTC_DCHECK(unified_plan_); |
| 500 | RTC_CHECK_EQ(1u, senders_.size()); |
| 501 | return rtc::scoped_refptr<RtpSenderInternal>(senders_[0]->internal()); |
| 502 | } |
| 503 |
| 504 | rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal() |
| 505 | const { |
| 506 | RTC_DCHECK(unified_plan_); |
| 507 | RTC_CHECK_EQ(1u, receivers_.size()); |
| 508 | return rtc::scoped_refptr<RtpReceiverInternal>(receivers_[0]->internal()); |
| 509 | } |
| 510 |
| 511 | cricket::MediaType RtpTransceiver::media_type() const { |
| 512 | return media_type_; |
| 513 | } |
| 514 |
| 515 | absl::optional<std::string> RtpTransceiver::mid() const { |
| 516 | return mid_; |
| 517 | } |
| 518 |
| 519 | void RtpTransceiver::OnFirstPacketReceived() { |
| 520 | for (const auto& receiver : receivers_) { |
| 521 | receiver->internal()->NotifyFirstPacketReceived(); |
| 522 | } |
| 523 | } |
| 524 |
| 525 | rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const { |
| 526 | RTC_DCHECK(unified_plan_); |
| 527 | RTC_CHECK_EQ(1u, senders_.size()); |
| 528 | return senders_[0]; |
| 529 | } |
| 530 |
| 531 | rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const { |
| 532 | RTC_DCHECK(unified_plan_); |
| 533 | RTC_CHECK_EQ(1u, receivers_.size()); |
| 534 | return receivers_[0]; |
| 535 | } |
| 536 |
| 537 | void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) { |
| 538 | RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>") |
| 539 | << ") current direction from " |
| 540 | << (current_direction_ ? RtpTransceiverDirectionToString( |
| 541 | *current_direction_) |
| 542 | : "<not set>") |
| 543 | << " to " << RtpTransceiverDirectionToString(direction) |
| 544 | << "."; |
| 545 | current_direction_ = direction; |
| 546 | if (RtpTransceiverDirectionHasSend(*current_direction_)) { |
| 547 | has_ever_been_used_to_send_ = true; |
| 548 | } |
| 549 | } |
| 550 |
| 551 | void RtpTransceiver::set_fired_direction( |
| 552 | absl::optional<RtpTransceiverDirection> direction) { |
| 553 | fired_direction_ = direction; |
| 554 | } |
| 555 |
| 556 | bool RtpTransceiver::stopped() const { |
| 557 | RTC_DCHECK_RUN_ON(thread_); |
| 558 | return stopped_; |
| 559 | } |
| 560 |
| 561 | bool RtpTransceiver::stopping() const { |
| 562 | RTC_DCHECK_RUN_ON(thread_); |
| 563 | return stopping_; |
| 564 | } |
| 565 |
| 566 | RtpTransceiverDirection RtpTransceiver::direction() const { |
| 567 | if (unified_plan_ && stopping()) |
| 568 | return RtpTransceiverDirection::kStopped; |
| 569 |
| 570 | return direction_; |
| 571 | } |
| 572 |
| 573 | RTCError RtpTransceiver::SetDirectionWithError( |
| 574 | RtpTransceiverDirection new_direction) { |
| 575 | if (unified_plan_ && stopping()) { |
| 576 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 577 | "Cannot set direction on a stopping transceiver."); |
| 578 | } |
| 579 | if (new_direction == direction_) |
| 580 | return RTCError::OK(); |
| 581 |
| 582 | if (new_direction == RtpTransceiverDirection::kStopped) { |
| 583 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 584 | "The set direction 'stopped' is invalid."); |
| 585 | } |
| 586 |
| 587 | direction_ = new_direction; |
| 588 | on_negotiation_needed_(); |
| 589 |
| 590 | return RTCError::OK(); |
| 591 | } |
| 592 |
| 593 | absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction() |
| 594 | const { |
| 595 | if (unified_plan_ && stopped()) |
| 596 | return RtpTransceiverDirection::kStopped; |
| 597 |
| 598 | return current_direction_; |
| 599 | } |
| 600 |
| 601 | absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction() |
| 602 | const { |
| 603 | return fired_direction_; |
| 604 | } |
| 605 |
| 606 | void RtpTransceiver::StopSendingAndReceiving() { |
| 607 | // 1. Let sender be transceiver.[[Sender]]. |
| 608 | // 2. Let receiver be transceiver.[[Receiver]]. |
| 609 | // |
| 610 | // 3. Stop sending media with sender. |
| 611 | // |
| 612 | RTC_DCHECK_RUN_ON(thread_); |
| 613 |
| 614 | // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as |
| 615 | // specified in [RFC3550]. |
| 616 | for (const auto& sender : senders_) |
| 617 | sender->internal()->Stop(); |
| 618 |
| 619 | // Signal to receiver sources that we're stopping. |
| 620 | for (const auto& receiver : receivers_) |
| 621 | receiver->internal()->Stop(); |
| 622 |
| 623 | context()->worker_thread()->BlockingCall([&]() { |
| 624 | // 5 Stop receiving media with receiver. |
| 625 | for (const auto& receiver : receivers_) |
| 626 | receiver->internal()->SetMediaChannel(nullptr); |
| 627 | }); |
| 628 |
| 629 | stopping_ = true; |
| 630 | direction_ = RtpTransceiverDirection::kInactive; |
| 631 | } |
| 632 |
| 633 | RTCError RtpTransceiver::StopStandard() { |
| 634 | RTC_DCHECK_RUN_ON(thread_); |
| 635 | // If we're on Plan B, do what Stop() used to do there. |
| 636 | if (!unified_plan_) { |
| 637 | StopInternal(); |
| 638 | return RTCError::OK(); |
| 639 | } |
| 640 | // 1. Let transceiver be the RTCRtpTransceiver object on which the method is |
| 641 | // invoked. |
| 642 | // |
| 643 | // 2. Let connection be the RTCPeerConnection object associated with |
| 644 | // transceiver. |
| 645 | // |
| 646 | // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError. |
| 647 | if (is_pc_closed_) { |
| 648 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 649 | "PeerConnection is closed."); |
| 650 | } |
| 651 |
| 652 | // 4. If transceiver.[[Stopping]] is true, abort these steps. |
| 653 | if (stopping_) |
| 654 | return RTCError::OK(); |
| 655 |
| 656 | // 5. Stop sending and receiving given transceiver, and update the |
| 657 | // negotiation-needed flag for connection. |
| 658 | StopSendingAndReceiving(); |
| 659 | on_negotiation_needed_(); |
| 660 |
| 661 | return RTCError::OK(); |
| 662 | } |
| 663 |
| 664 | void RtpTransceiver::StopInternal() { |
| 665 | RTC_DCHECK_RUN_ON(thread_); |
| 666 | StopTransceiverProcedure(); |
| 667 | } |
| 668 |
| 669 | void RtpTransceiver::StopTransceiverProcedure() { |
| 670 | RTC_DCHECK_RUN_ON(thread_); |
| 671 | // As specified in the "Stop the RTCRtpTransceiver" procedure |
| 672 | // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given |
| 673 | // transceiver. |
| 674 | if (!stopping_) |
| 675 | StopSendingAndReceiving(); |
| 676 |
| 677 | // 2. Set transceiver.[[Stopped]] to true. |
| 678 | stopped_ = true; |
| 679 |
| 680 | // Signal the updated change to the senders. |
| 681 | for (const auto& sender : senders_) |
| 682 | sender->internal()->SetTransceiverAsStopped(); |
| 683 |
| 684 | // 3. Set transceiver.[[Receptive]] to false. |
| 685 | // 4. Set transceiver.[[CurrentDirection]] to null. |
| 686 | current_direction_ = absl::nullopt; |
| 687 | } |
| 688 |
| 689 | RTCError RtpTransceiver::SetCodecPreferences( |
| 690 | rtc::ArrayView<RtpCodecCapability> codec_capabilities) { |
| 691 | RTC_DCHECK(unified_plan_); |
| 692 | // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot |
| 693 | // to codecs and abort these steps. |
| 694 | if (codec_capabilities.empty()) { |
| 695 | codec_preferences_.clear(); |
| 696 | senders_.front()->internal()->SetCodecPreferences( |
| 697 | media_type() == cricket::MEDIA_TYPE_VIDEO |
| 698 | ? media_engine()->video().send_codecs(false) |
| 699 | : media_engine()->voice().send_codecs()); |
| 700 | return RTCError::OK(); |
| 701 | } |
| 702 |
| 703 | // 4. Remove any duplicate values in codecs. |
| 704 | std::vector<RtpCodecCapability> codecs; |
| 705 | absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs), |
| 706 | [&codecs](const RtpCodecCapability& codec) { |
| 707 | return absl::c_linear_search(codecs, codec); |
| 708 | }); |
| 709 |
| 710 | // 6. to 8. |
| 711 | RTCError result; |
| 712 | std::vector<cricket::Codec> recv_codecs, send_codecs; |
| 713 | if (media_type_ == cricket::MEDIA_TYPE_AUDIO) { |
| 714 | send_codecs = media_engine()->voice().send_codecs(); |
| 715 | recv_codecs = media_engine()->voice().recv_codecs(); |
| 716 | } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) { |
| 717 | send_codecs = media_engine()->video().send_codecs(context()->use_rtx()); |
| 718 | recv_codecs = media_engine()->video().recv_codecs(context()->use_rtx()); |
| 719 | } |
| 720 | result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs); |
| 721 |
| 722 | if (result.ok()) { |
| 723 | senders_.front()->internal()->SetCodecPreferences( |
| 724 | MatchCodecPreferences(codecs, send_codecs)); |
| 725 | codec_preferences_ = codecs; |
| 726 | } |
| 727 |
| 728 | return result; |
| 729 | } |
| 730 |
| 731 | std::vector<RtpHeaderExtensionCapability> |
| 732 | RtpTransceiver::GetHeaderExtensionsToNegotiate() const { |
| 733 | return header_extensions_to_negotiate_; |
| 734 | } |
| 735 |
| 736 | std::vector<RtpHeaderExtensionCapability> |
| 737 | RtpTransceiver::GetNegotiatedHeaderExtensions() const { |
| 738 | RTC_DCHECK_RUN_ON(thread_); |
| 739 | std::vector<RtpHeaderExtensionCapability> result; |
| 740 | result.reserve(header_extensions_to_negotiate_.size()); |
| 741 | for (const auto& ext : header_extensions_to_negotiate_) { |
| 742 | auto negotiated = absl::c_find_if(negotiated_header_extensions_, |
| 743 | [&ext](const RtpExtension& negotiated) { |
| 744 | return negotiated.uri == ext.uri; |
| 745 | }); |
| 746 | RtpHeaderExtensionCapability capability(ext.uri); |
| 747 | // TODO(bugs.webrtc.org/7477): extend when header extensions support |
| 748 | // direction. |
| 749 | capability.direction = negotiated != negotiated_header_extensions_.end() |
| 750 | ? RtpTransceiverDirection::kSendRecv |
| 751 | : RtpTransceiverDirection::kStopped; |
| 752 | result.push_back(capability); |
| 753 | } |
| 754 | return result; |
| 755 | } |
| 756 |
| 757 | // Helper function to determine mandatory-to-negotiate extensions. |
| 758 | // See https://www.rfc-editor.org/rfc/rfc8834#name-header-extensions |
| 759 | // and https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface |
| 760 | // Since BUNDLE is offered by default, MID is mandatory and can not be turned |
| 761 | // off via this API. |
| 762 | bool IsMandatoryHeaderExtension(const std::string& uri) { |
| 763 | return uri == RtpExtension::kMidUri; |
| 764 | } |
| 765 |
| 766 | RTCError RtpTransceiver::SetHeaderExtensionsToNegotiate( |
| 767 | rtc::ArrayView<const RtpHeaderExtensionCapability> header_extensions) { |
| 768 | // https://w3c.github.io/webrtc-extensions/#dom-rtcrtptransceiver-setheaderextensionstonegotiate |
| 769 | if (header_extensions.size() != header_extensions_to_negotiate_.size()) { |
| 770 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 771 | "Size of extensions to negotiate does not match."); |
| 772 | } |
| 773 | // For each index i of extensions, run the following steps: ... |
| 774 | for (size_t i = 0; i < header_extensions.size(); i++) { |
| 775 | const auto& extension = header_extensions[i]; |
| 776 | if (extension.uri != header_extensions_to_negotiate_[i].uri) { |
| 777 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 778 | "Reordering extensions is not allowed."); |
| 779 | } |
| 780 | if (IsMandatoryHeaderExtension(extension.uri) && |
| 781 | extension.direction != RtpTransceiverDirection::kSendRecv) { |
| 782 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 783 | "Attempted to stop a mandatory extension."); |
| 784 | } |
| 785 |
| 786 | // TODO(bugs.webrtc.org/7477): Currently there are no recvonly extensions so |
| 787 | // this can not be checked: "When there exists header extension capabilities |
| 788 | // that have directions other than kSendRecv, restrict extension.direction |
| 789 | // as to not exceed that capability." |
| 790 | } |
| 791 |
| 792 | // Apply mutation after error checking. |
| 793 | for (size_t i = 0; i < header_extensions.size(); i++) { |
| 794 | header_extensions_to_negotiate_[i].direction = |
| 795 | header_extensions[i].direction; |
| 796 | } |
| 797 |
| 798 | return RTCError::OK(); |
| 799 | } |
| 800 |
| 801 | void RtpTransceiver::OnNegotiationUpdate( |
| 802 | SdpType sdp_type, |
| 803 | const cricket::MediaContentDescription* content) { |
| 804 | RTC_DCHECK_RUN_ON(thread_); |
| 805 | RTC_DCHECK(content); |
| 806 | if (sdp_type == SdpType::kAnswer) |
| 807 | negotiated_header_extensions_ = content->rtp_header_extensions(); |
| 808 | } |
| 809 |
| 810 | void RtpTransceiver::SetPeerConnectionClosed() { |
| 811 | is_pc_closed_ = true; |
| 812 | } |
| 813 |
| 814 | } // namespace webrtc |
| 815 |