| 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_sender.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <atomic> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/algorithm/container.h" |
| 20 | #include "api/audio_options.h" |
| 21 | #include "api/media_stream_interface.h" |
| 22 | #include "api/priority.h" |
| 23 | #include "api/rtc_error.h" |
| 24 | #include "media/base/media_engine.h" |
| 25 | #include "pc/legacy_stats_collector_interface.h" |
| 26 | #include "rtc_base/checks.h" |
| 27 | #include "rtc_base/helpers.h" |
| 28 | #include "rtc_base/logging.h" |
| 29 | #include "rtc_base/trace_event.h" |
| 30 |
| 31 | namespace webrtc { |
| 32 |
| 33 | namespace { |
| 34 |
| 35 | // This function is only expected to be called on the signaling thread. |
| 36 | // On the other hand, some test or even production setups may use |
| 37 | // several signaling threads. |
| 38 | int GenerateUniqueId() { |
| 39 | static std::atomic<int> g_unique_id{0}; |
| 40 |
| 41 | return ++g_unique_id; |
| 42 | } |
| 43 |
| 44 | // Returns true if a "per-sender" encoding parameter contains a value that isn't |
| 45 | // its default. Currently max_bitrate_bps and bitrate_priority both are |
| 46 | // implemented "per-sender," meaning that these encoding parameters |
| 47 | // are used for the RtpSender as a whole, not for a specific encoding layer. |
| 48 | // This is done by setting these encoding parameters at index 0 of |
| 49 | // RtpParameters.encodings. This function can be used to check if these |
| 50 | // parameters are set at any index other than 0 of RtpParameters.encodings, |
| 51 | // because they are currently unimplemented to be used for a specific encoding |
| 52 | // layer. |
| 53 | bool PerSenderRtpEncodingParameterHasValue( |
| 54 | const RtpEncodingParameters& encoding_params) { |
| 55 | if (encoding_params.bitrate_priority != kDefaultBitratePriority || |
| 56 | encoding_params.network_priority != Priority::kLow) { |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 |
| 62 | void RemoveEncodingLayers(const std::vector<std::string>& rids, |
| 63 | std::vector<RtpEncodingParameters>* encodings) { |
| 64 | RTC_DCHECK(encodings); |
| 65 | encodings->erase( |
| 66 | std::remove_if(encodings->begin(), encodings->end(), |
| 67 | [&rids](const RtpEncodingParameters& encoding) { |
| 68 | return absl::c_linear_search(rids, encoding.rid); |
| 69 | }), |
| 70 | encodings->end()); |
| 71 | } |
| 72 |
| 73 | RtpParameters RestoreEncodingLayers( |
| 74 | const RtpParameters& parameters, |
| 75 | const std::vector<std::string>& removed_rids, |
| 76 | const std::vector<RtpEncodingParameters>& all_layers) { |
| 77 | RTC_CHECK_EQ(parameters.encodings.size() + removed_rids.size(), |
| 78 | all_layers.size()); |
| 79 | RtpParameters result(parameters); |
| 80 | result.encodings.clear(); |
| 81 | size_t index = 0; |
| 82 | for (const RtpEncodingParameters& encoding : all_layers) { |
| 83 | if (absl::c_linear_search(removed_rids, encoding.rid)) { |
| 84 | result.encodings.push_back(encoding); |
| 85 | continue; |
| 86 | } |
| 87 | result.encodings.push_back(parameters.encodings[index++]); |
| 88 | } |
| 89 | return result; |
| 90 | } |
| 91 |
| 92 | class SignalingThreadCallback { |
| 93 | public: |
| 94 | SignalingThreadCallback(rtc::Thread* signaling_thread, |
| 95 | SetParametersCallback callback) |
| 96 | : signaling_thread_(signaling_thread), callback_(std::move(callback)) {} |
| 97 | SignalingThreadCallback(SignalingThreadCallback&& other) |
| 98 | : signaling_thread_(other.signaling_thread_), |
| 99 | callback_(std::move(other.callback_)) { |
| 100 | other.callback_ = nullptr; |
| 101 | } |
| 102 |
| 103 | ~SignalingThreadCallback() { |
| 104 | if (callback_) { |
| 105 | Resolve(RTCError(RTCErrorType::INTERNAL_ERROR)); |
| 106 |
| 107 | RTC_CHECK_NOTREACHED(); |
| 108 | } |
| 109 | } |
| 110 |
| 111 | void operator()(const RTCError& error) { Resolve(error); } |
| 112 |
| 113 | private: |
| 114 | void Resolve(const RTCError& error) { |
| 115 | if (!signaling_thread_->IsCurrent()) { |
| 116 | signaling_thread_->PostTask( |
| 117 | [callback = std::move(callback_), error]() mutable { |
| 118 | InvokeSetParametersCallback(callback, error); |
| 119 | }); |
| 120 | callback_ = nullptr; |
| 121 | return; |
| 122 | } |
| 123 |
| 124 | InvokeSetParametersCallback(callback_, error); |
| 125 | callback_ = nullptr; |
| 126 | } |
| 127 |
| 128 | rtc::Thread* signaling_thread_; |
| 129 | SetParametersCallback callback_; |
| 130 | }; |
| 131 |
| 132 | } // namespace |
| 133 |
| 134 | // Returns true if any RtpParameters member that isn't implemented contains a |
| 135 | // value. |
| 136 | bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters) { |
| 137 | if (!parameters.mid.empty()) { |
| 138 | return true; |
| 139 | } |
| 140 | for (size_t i = 0; i < parameters.encodings.size(); ++i) { |
| 141 | // Encoding parameters that are per-sender should only contain value at |
| 142 | // index 0. |
| 143 | if (i != 0 && |
| 144 | PerSenderRtpEncodingParameterHasValue(parameters.encodings[i])) { |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 |
| 151 | RtpSenderBase::RtpSenderBase(rtc::Thread* worker_thread, |
| 152 | const std::string& id, |
| 153 | SetStreamsObserver* set_streams_observer) |
| 154 | : signaling_thread_(rtc::Thread::Current()), |
| 155 | worker_thread_(worker_thread), |
| 156 | id_(id), |
| 157 | set_streams_observer_(set_streams_observer) { |
| 158 | RTC_DCHECK(worker_thread); |
| 159 | init_parameters_.encodings.emplace_back(); |
| 160 | } |
| 161 |
| 162 | void RtpSenderBase::SetFrameEncryptor( |
| 163 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) { |
| 164 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 165 | frame_encryptor_ = std::move(frame_encryptor); |
| 166 | // Special Case: Set the frame encryptor to any value on any existing channel. |
| 167 | if (media_channel_ && ssrc_ && !stopped_) { |
| 168 | worker_thread_->BlockingCall( |
| 169 | [&] { media_channel_->SetFrameEncryptor(ssrc_, frame_encryptor_); }); |
| 170 | } |
| 171 | } |
| 172 |
| 173 | void RtpSenderBase::SetEncoderSelector( |
| 174 | std::unique_ptr<VideoEncoderFactory::EncoderSelectorInterface> |
| 175 | encoder_selector) { |
| 176 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 177 | encoder_selector_ = std::move(encoder_selector); |
| 178 | SetEncoderSelectorOnChannel(); |
| 179 | } |
| 180 |
| 181 | void RtpSenderBase::SetEncoderSelectorOnChannel() { |
| 182 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 183 | if (media_channel_ && ssrc_ && !stopped_) { |
| 184 | worker_thread_->BlockingCall([&] { |
| 185 | media_channel_->SetEncoderSelector(ssrc_, encoder_selector_.get()); |
| 186 | }); |
| 187 | } |
| 188 | } |
| 189 |
| 190 | void RtpSenderBase::SetMediaChannel( |
| 191 | cricket::MediaSendChannelInterface* media_channel) { |
| 192 | RTC_DCHECK(media_channel == nullptr || |
| 193 | media_channel->media_type() == media_type()); |
| 194 | media_channel_ = media_channel; |
| 195 | } |
| 196 |
| 197 | RtpParameters RtpSenderBase::GetParametersInternal() const { |
| 198 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 199 | if (stopped_) { |
| 200 | return RtpParameters(); |
| 201 | } |
| 202 | if (!media_channel_ || !ssrc_) { |
| 203 | return init_parameters_; |
| 204 | } |
| 205 | return worker_thread_->BlockingCall([&] { |
| 206 | RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_); |
| 207 | RemoveEncodingLayers(disabled_rids_, &result.encodings); |
| 208 | return result; |
| 209 | }); |
| 210 | } |
| 211 |
| 212 | RtpParameters RtpSenderBase::GetParametersInternalWithAllLayers() const { |
| 213 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 214 | if (stopped_) { |
| 215 | return RtpParameters(); |
| 216 | } |
| 217 | if (!media_channel_ || !ssrc_) { |
| 218 | return init_parameters_; |
| 219 | } |
| 220 | return worker_thread_->BlockingCall([&] { |
| 221 | RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_); |
| 222 | return result; |
| 223 | }); |
| 224 | } |
| 225 |
| 226 | RtpParameters RtpSenderBase::GetParameters() const { |
| 227 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 228 | RtpParameters result = GetParametersInternal(); |
| 229 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 230 | result.transaction_id = last_transaction_id_.value(); |
| 231 | return result; |
| 232 | } |
| 233 |
| 234 | void RtpSenderBase::SetParametersInternal(const RtpParameters& parameters, |
| 235 | SetParametersCallback callback, |
| 236 | bool blocking) { |
| 237 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 238 | RTC_DCHECK(!stopped_); |
| 239 |
| 240 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 241 | RTCError error( |
| 242 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 243 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 244 | RTC_LOG(LS_ERROR) << error.message() << " (" |
| 245 | << ::webrtc::ToString(error.type()) << ")"; |
| 246 | InvokeSetParametersCallback(callback, error); |
| 247 | return; |
| 248 | } |
| 249 | if (!media_channel_ || !ssrc_) { |
| 250 | auto result = cricket::CheckRtpParametersInvalidModificationAndValues( |
| 251 | init_parameters_, parameters, codec_preferences_, absl::nullopt); |
| 252 | if (result.ok()) { |
| 253 | init_parameters_ = parameters; |
| 254 | } |
| 255 | InvokeSetParametersCallback(callback, result); |
| 256 | return; |
| 257 | } |
| 258 | auto task = [&, callback = std::move(callback), |
| 259 | parameters = std::move(parameters)]() mutable { |
| 260 | RtpParameters rtp_parameters = parameters; |
| 261 | RtpParameters old_parameters = media_channel_->GetRtpSendParameters(ssrc_); |
| 262 | if (!disabled_rids_.empty()) { |
| 263 | // Need to add the inactive layers. |
| 264 | rtp_parameters = RestoreEncodingLayers(parameters, disabled_rids_, |
| 265 | old_parameters.encodings); |
| 266 | } |
| 267 |
| 268 | RTCError result = cricket::CheckRtpParametersInvalidModificationAndValues( |
| 269 | old_parameters, rtp_parameters); |
| 270 | if (!result.ok()) { |
| 271 | InvokeSetParametersCallback(callback, result); |
| 272 | return; |
| 273 | } |
| 274 |
| 275 | result = CheckCodecParameters(rtp_parameters); |
| 276 | if (!result.ok()) { |
| 277 | InvokeSetParametersCallback(callback, result); |
| 278 | return; |
| 279 | } |
| 280 |
| 281 | media_channel_->SetRtpSendParameters(ssrc_, rtp_parameters, |
| 282 | std::move(callback)); |
| 283 | }; |
| 284 | if (blocking) |
| 285 | worker_thread_->BlockingCall(task); |
| 286 | else |
| 287 | worker_thread_->PostTask(std::move(task)); |
| 288 | } |
| 289 |
| 290 | RTCError RtpSenderBase::SetParametersInternalWithAllLayers( |
| 291 | const RtpParameters& parameters) { |
| 292 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 293 | RTC_DCHECK(!stopped_); |
| 294 |
| 295 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 296 | LOG_AND_RETURN_ERROR( |
| 297 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 298 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 299 | } |
| 300 | if (!media_channel_ || !ssrc_) { |
| 301 | auto result = cricket::CheckRtpParametersInvalidModificationAndValues( |
| 302 | init_parameters_, parameters, codec_preferences_, absl::nullopt); |
| 303 | if (result.ok()) { |
| 304 | init_parameters_ = parameters; |
| 305 | } |
| 306 | return result; |
| 307 | } |
| 308 | return worker_thread_->BlockingCall([&] { |
| 309 | RtpParameters rtp_parameters = parameters; |
| 310 | return media_channel_->SetRtpSendParameters(ssrc_, rtp_parameters, nullptr); |
| 311 | }); |
| 312 | } |
| 313 |
| 314 | RTCError RtpSenderBase::CheckSetParameters(const RtpParameters& parameters) { |
| 315 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 316 | if (is_transceiver_stopped_) { |
| 317 | LOG_AND_RETURN_ERROR( |
| 318 | RTCErrorType::INVALID_STATE, |
| 319 | "Cannot set parameters on sender of a stopped transceiver."); |
| 320 | } |
| 321 | if (stopped_) { |
| 322 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 323 | "Cannot set parameters on a stopped sender."); |
| 324 | } |
| 325 | if (!last_transaction_id_) { |
| 326 | LOG_AND_RETURN_ERROR( |
| 327 | RTCErrorType::INVALID_STATE, |
| 328 | "Failed to set parameters since getParameters() has never been called" |
| 329 | " on this sender"); |
| 330 | } |
| 331 | if (last_transaction_id_ != parameters.transaction_id) { |
| 332 | LOG_AND_RETURN_ERROR( |
| 333 | RTCErrorType::INVALID_MODIFICATION, |
| 334 | "Failed to set parameters since the transaction_id doesn't match" |
| 335 | " the last value returned from getParameters()"); |
| 336 | } |
| 337 |
| 338 | return RTCError::OK(); |
| 339 | } |
| 340 |
| 341 | RTCError RtpSenderBase::CheckCodecParameters(const RtpParameters& parameters) { |
| 342 | absl::optional<cricket::Codec> send_codec = media_channel_->GetSendCodec(); |
| 343 |
| 344 | // Match the currently used codec against the codec preferences to gather |
| 345 | // the SVC capabilities. |
| 346 | absl::optional<cricket::Codec> send_codec_with_svc_info; |
| 347 | if (send_codec && send_codec->type == cricket::Codec::Type::kVideo) { |
| 348 | auto codec_match = |
| 349 | absl::c_find_if(codec_preferences_, [&](auto& codec_preference) { |
| 350 | return send_codec->Matches(codec_preference); |
| 351 | }); |
| 352 | if (codec_match != codec_preferences_.end()) { |
| 353 | send_codec_with_svc_info = *codec_match; |
| 354 | } |
| 355 | } |
| 356 |
| 357 | return cricket::CheckScalabilityModeValues(parameters, codec_preferences_, |
| 358 | send_codec_with_svc_info); |
| 359 | } |
| 360 |
| 361 | RTCError RtpSenderBase::SetParameters(const RtpParameters& parameters) { |
| 362 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 363 | TRACE_EVENT0("webrtc", "RtpSenderBase::SetParameters"); |
| 364 | RTCError result = CheckSetParameters(parameters); |
| 365 | if (!result.ok()) |
| 366 | return result; |
| 367 |
| 368 | // Some tests rely on working in single thread mode without a run loop and a |
| 369 | // blocking call is required to keep them working. The encoder configuration |
| 370 | // also involves another thread with an asynchronous task, thus we still do |
| 371 | // need to wait for the callback to be resolved this way. |
| 372 | std::unique_ptr<rtc::Event> done_event = std::make_unique<rtc::Event>(); |
| 373 | SetParametersInternal( |
| 374 | parameters, |
| 375 | [done = done_event.get(), &result](RTCError error) { |
| 376 | result = error; |
| 377 | done->Set(); |
| 378 | }, |
| 379 | true); |
| 380 | done_event->Wait(rtc::Event::kForever); |
| 381 | last_transaction_id_.reset(); |
| 382 | return result; |
| 383 | } |
| 384 |
| 385 | void RtpSenderBase::SetParametersAsync(const RtpParameters& parameters, |
| 386 | SetParametersCallback callback) { |
| 387 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 388 | RTC_DCHECK(callback); |
| 389 | TRACE_EVENT0("webrtc", "RtpSenderBase::SetParametersAsync"); |
| 390 | RTCError result = CheckSetParameters(parameters); |
| 391 | if (!result.ok()) { |
| 392 | InvokeSetParametersCallback(callback, result); |
| 393 | return; |
| 394 | } |
| 395 |
| 396 | SetParametersInternal( |
| 397 | parameters, |
| 398 | SignalingThreadCallback( |
| 399 | signaling_thread_, |
| 400 | [this, callback = std::move(callback)](RTCError error) mutable { |
| 401 | last_transaction_id_.reset(); |
| 402 | InvokeSetParametersCallback(callback, error); |
| 403 | }), |
| 404 | false); |
| 405 | } |
| 406 |
| 407 | void RtpSenderBase::set_stream_ids(const std::vector<std::string>& stream_ids) { |
| 408 | stream_ids_.clear(); |
| 409 | absl::c_copy_if(stream_ids, std::back_inserter(stream_ids_), |
| 410 | [this](const std::string& stream_id) { |
| 411 | return !absl::c_linear_search(stream_ids_, stream_id); |
| 412 | }); |
| 413 | } |
| 414 |
| 415 | void RtpSenderBase::SetStreams(const std::vector<std::string>& stream_ids) { |
| 416 | set_stream_ids(stream_ids); |
| 417 | if (set_streams_observer_) |
| 418 | set_streams_observer_->OnSetStreams(); |
| 419 | } |
| 420 |
| 421 | bool RtpSenderBase::SetTrack(MediaStreamTrackInterface* track) { |
| 422 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 423 | TRACE_EVENT0("webrtc", "RtpSenderBase::SetTrack"); |
| 424 | if (stopped_) { |
| 425 | RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; |
| 426 | return false; |
| 427 | } |
| 428 | if (track && track->kind() != track_kind()) { |
| 429 | RTC_LOG(LS_ERROR) << "SetTrack with " << track->kind() |
| 430 | << " called on RtpSender with " << track_kind() |
| 431 | << " track."; |
| 432 | return false; |
| 433 | } |
| 434 |
| 435 | // Detach from old track. |
| 436 | if (track_) { |
| 437 | DetachTrack(); |
| 438 | track_->UnregisterObserver(this); |
| 439 | RemoveTrackFromStats(); |
| 440 | } |
| 441 |
| 442 | // Attach to new track. |
| 443 | bool prev_can_send_track = can_send_track(); |
| 444 | // Keep a reference to the old track to keep it alive until we call SetSend. |
| 445 | rtc::scoped_refptr<MediaStreamTrackInterface> old_track = track_; |
| 446 | track_ = track; |
| 447 | if (track_) { |
| 448 | track_->RegisterObserver(this); |
| 449 | AttachTrack(); |
| 450 | } |
| 451 |
| 452 | // Update channel. |
| 453 | if (can_send_track()) { |
| 454 | SetSend(); |
| 455 | AddTrackToStats(); |
| 456 | } else if (prev_can_send_track) { |
| 457 | ClearSend(); |
| 458 | } |
| 459 | attachment_id_ = (track_ ? GenerateUniqueId() : 0); |
| 460 | return true; |
| 461 | } |
| 462 |
| 463 | void RtpSenderBase::SetSsrc(uint32_t ssrc) { |
| 464 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 465 | TRACE_EVENT0("webrtc", "RtpSenderBase::SetSsrc"); |
| 466 | if (stopped_ || ssrc == ssrc_) { |
| 467 | return; |
| 468 | } |
| 469 | // If we are already sending with a particular SSRC, stop sending. |
| 470 | if (can_send_track()) { |
| 471 | ClearSend(); |
| 472 | RemoveTrackFromStats(); |
| 473 | } |
| 474 | ssrc_ = ssrc; |
| 475 | if (can_send_track()) { |
| 476 | SetSend(); |
| 477 | AddTrackToStats(); |
| 478 | } |
| 479 | if (!init_parameters_.encodings.empty() || |
| 480 | init_parameters_.degradation_preference.has_value()) { |
| 481 | worker_thread_->BlockingCall([&] { |
| 482 | RTC_DCHECK(media_channel_); |
| 483 | // Get the current parameters, which are constructed from the SDP. |
| 484 | // The number of layers in the SDP is currently authoritative to support |
| 485 | // SDP munging for Plan-B simulcast with "a=ssrc-group:SIM <ssrc-id>..." |
| 486 | // lines as described in RFC 5576. |
| 487 | // All fields should be default constructed and the SSRC field set, which |
| 488 | // we need to copy. |
| 489 | RtpParameters current_parameters = |
| 490 | media_channel_->GetRtpSendParameters(ssrc_); |
| 491 | RTC_CHECK_GE(current_parameters.encodings.size(), |
| 492 | init_parameters_.encodings.size()); |
| 493 | for (size_t i = 0; i < init_parameters_.encodings.size(); ++i) { |
| 494 | init_parameters_.encodings[i].ssrc = |
| 495 | current_parameters.encodings[i].ssrc; |
| 496 | init_parameters_.encodings[i].rid = current_parameters.encodings[i].rid; |
| 497 | current_parameters.encodings[i] = init_parameters_.encodings[i]; |
| 498 | } |
| 499 | current_parameters.degradation_preference = |
| 500 | init_parameters_.degradation_preference; |
| 501 | media_channel_->SetRtpSendParameters(ssrc_, current_parameters, nullptr); |
| 502 | init_parameters_.encodings.clear(); |
| 503 | init_parameters_.degradation_preference = absl::nullopt; |
| 504 | }); |
| 505 | } |
| 506 | // Attempt to attach the frame decryptor to the current media channel. |
| 507 | if (frame_encryptor_) { |
| 508 | SetFrameEncryptor(frame_encryptor_); |
| 509 | } |
| 510 | if (frame_transformer_) { |
| 511 | SetEncoderToPacketizerFrameTransformer(frame_transformer_); |
| 512 | } |
| 513 | if (encoder_selector_) { |
| 514 | SetEncoderSelectorOnChannel(); |
| 515 | } |
| 516 | } |
| 517 |
| 518 | void RtpSenderBase::Stop() { |
| 519 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 520 | TRACE_EVENT0("webrtc", "RtpSenderBase::Stop"); |
| 521 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
| 522 | if (stopped_) { |
| 523 | return; |
| 524 | } |
| 525 | if (track_) { |
| 526 | DetachTrack(); |
| 527 | track_->UnregisterObserver(this); |
| 528 | } |
| 529 | if (can_send_track()) { |
| 530 | ClearSend(); |
| 531 | RemoveTrackFromStats(); |
| 532 | } |
| 533 | media_channel_ = nullptr; |
| 534 | set_streams_observer_ = nullptr; |
| 535 | stopped_ = true; |
| 536 | } |
| 537 |
| 538 | RTCError RtpSenderBase::DisableEncodingLayers( |
| 539 | const std::vector<std::string>& rids) { |
| 540 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 541 | if (stopped_) { |
| 542 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 543 | "Cannot disable encodings on a stopped sender."); |
| 544 | } |
| 545 |
| 546 | if (rids.empty()) { |
| 547 | return RTCError::OK(); |
| 548 | } |
| 549 |
| 550 | // Check that all the specified layers exist and disable them in the channel. |
| 551 | RtpParameters parameters = GetParametersInternalWithAllLayers(); |
| 552 | for (const std::string& rid : rids) { |
| 553 | if (absl::c_none_of(parameters.encodings, |
| 554 | [&rid](const RtpEncodingParameters& encoding) { |
| 555 | return encoding.rid == rid; |
| 556 | })) { |
| 557 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 558 | "RID: " + rid + " does not refer to a valid layer."); |
| 559 | } |
| 560 | } |
| 561 |
| 562 | if (!media_channel_ || !ssrc_) { |
| 563 | RemoveEncodingLayers(rids, &init_parameters_.encodings); |
| 564 | // Invalidate any transaction upon success. |
| 565 | last_transaction_id_.reset(); |
| 566 | return RTCError::OK(); |
| 567 | } |
| 568 |
| 569 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 570 | // Remain active if not in the disable list. |
| 571 | encoding.active &= absl::c_none_of( |
| 572 | rids, |
| 573 | [&encoding](const std::string& rid) { return encoding.rid == rid; }); |
| 574 | } |
| 575 |
| 576 | RTCError result = SetParametersInternalWithAllLayers(parameters); |
| 577 | if (result.ok()) { |
| 578 | disabled_rids_.insert(disabled_rids_.end(), rids.begin(), rids.end()); |
| 579 | // Invalidate any transaction upon success. |
| 580 | last_transaction_id_.reset(); |
| 581 | } |
| 582 | return result; |
| 583 | } |
| 584 |
| 585 | void RtpSenderBase::SetEncoderToPacketizerFrameTransformer( |
| 586 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) { |
| 587 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 588 | frame_transformer_ = std::move(frame_transformer); |
| 589 | if (media_channel_ && ssrc_ && !stopped_) { |
| 590 | worker_thread_->BlockingCall([&] { |
| 591 | media_channel_->SetEncoderToPacketizerFrameTransformer( |
| 592 | ssrc_, frame_transformer_); |
| 593 | }); |
| 594 | } |
| 595 | } |
| 596 |
| 597 | LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {} |
| 598 |
| 599 | LocalAudioSinkAdapter::~LocalAudioSinkAdapter() { |
| 600 | MutexLock lock(&lock_); |
| 601 | if (sink_) |
| 602 | sink_->OnClose(); |
| 603 | } |
| 604 |
| 605 | void LocalAudioSinkAdapter::OnData( |
| 606 | const void* audio_data, |
| 607 | int bits_per_sample, |
| 608 | int sample_rate, |
| 609 | size_t number_of_channels, |
| 610 | size_t number_of_frames, |
| 611 | absl::optional<int64_t> absolute_capture_timestamp_ms) { |
| 612 | TRACE_EVENT2("webrtc", "LocalAudioSinkAdapter::OnData", "sample_rate", |
| 613 | sample_rate, "number_of_frames", number_of_frames); |
| 614 | MutexLock lock(&lock_); |
| 615 | if (sink_) { |
| 616 | sink_->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels, |
| 617 | number_of_frames, absolute_capture_timestamp_ms); |
| 618 | num_preferred_channels_ = sink_->NumPreferredChannels(); |
| 619 | } |
| 620 | } |
| 621 |
| 622 | void LocalAudioSinkAdapter::SetSink(cricket::AudioSource::Sink* sink) { |
| 623 | MutexLock lock(&lock_); |
| 624 | RTC_DCHECK(!sink || !sink_); |
| 625 | sink_ = sink; |
| 626 | } |
| 627 |
| 628 | rtc::scoped_refptr<AudioRtpSender> AudioRtpSender::Create( |
| 629 | rtc::Thread* worker_thread, |
| 630 | const std::string& id, |
| 631 | LegacyStatsCollectorInterface* stats, |
| 632 | SetStreamsObserver* set_streams_observer) { |
| 633 | return rtc::make_ref_counted<AudioRtpSender>(worker_thread, id, stats, |
| 634 | set_streams_observer); |
| 635 | } |
| 636 |
| 637 | AudioRtpSender::AudioRtpSender(rtc::Thread* worker_thread, |
| 638 | const std::string& id, |
| 639 | LegacyStatsCollectorInterface* legacy_stats, |
| 640 | SetStreamsObserver* set_streams_observer) |
| 641 | : RtpSenderBase(worker_thread, id, set_streams_observer), |
| 642 | legacy_stats_(legacy_stats), |
| 643 | dtmf_sender_(DtmfSender::Create(rtc::Thread::Current(), this)), |
| 644 | dtmf_sender_proxy_( |
| 645 | DtmfSenderProxy::Create(rtc::Thread::Current(), dtmf_sender_)), |
| 646 | sink_adapter_(new LocalAudioSinkAdapter()) {} |
| 647 |
| 648 | AudioRtpSender::~AudioRtpSender() { |
| 649 | dtmf_sender_->OnDtmfProviderDestroyed(); |
| 650 | Stop(); |
| 651 | } |
| 652 |
| 653 | bool AudioRtpSender::CanInsertDtmf() { |
| 654 | if (!media_channel_) { |
| 655 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists."; |
| 656 | return false; |
| 657 | } |
| 658 | // Check that this RTP sender is active (description has been applied that |
| 659 | // matches an SSRC to its ID). |
| 660 | if (!ssrc_) { |
| 661 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: Sender does not have SSRC."; |
| 662 | return false; |
| 663 | } |
| 664 | return worker_thread_->BlockingCall( |
| 665 | [&] { return voice_media_channel()->CanInsertDtmf(); }); |
| 666 | } |
| 667 |
| 668 | bool AudioRtpSender::InsertDtmf(int code, int duration) { |
| 669 | if (!media_channel_) { |
| 670 | RTC_LOG(LS_ERROR) << "InsertDtmf: No audio channel exists."; |
| 671 | return false; |
| 672 | } |
| 673 | if (!ssrc_) { |
| 674 | RTC_LOG(LS_ERROR) << "InsertDtmf: Sender does not have SSRC."; |
| 675 | return false; |
| 676 | } |
| 677 | bool success = worker_thread_->BlockingCall( |
| 678 | [&] { return voice_media_channel()->InsertDtmf(ssrc_, code, duration); }); |
| 679 | if (!success) { |
| 680 | RTC_LOG(LS_ERROR) << "Failed to insert DTMF to channel."; |
| 681 | } |
| 682 | return success; |
| 683 | } |
| 684 |
| 685 | void AudioRtpSender::OnChanged() { |
| 686 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 687 | TRACE_EVENT0("webrtc", "AudioRtpSender::OnChanged"); |
| 688 | RTC_DCHECK(!stopped_); |
| 689 | if (cached_track_enabled_ != track_->enabled()) { |
| 690 | cached_track_enabled_ = track_->enabled(); |
| 691 | if (can_send_track()) { |
| 692 | SetSend(); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 |
| 697 | void AudioRtpSender::DetachTrack() { |
| 698 | RTC_DCHECK(track_); |
| 699 | audio_track()->RemoveSink(sink_adapter_.get()); |
| 700 | } |
| 701 |
| 702 | void AudioRtpSender::AttachTrack() { |
| 703 | RTC_DCHECK(track_); |
| 704 | cached_track_enabled_ = track_->enabled(); |
| 705 | audio_track()->AddSink(sink_adapter_.get()); |
| 706 | } |
| 707 |
| 708 | void AudioRtpSender::AddTrackToStats() { |
| 709 | if (can_send_track() && legacy_stats_) { |
| 710 | legacy_stats_->AddLocalAudioTrack(audio_track().get(), ssrc_); |
| 711 | } |
| 712 | } |
| 713 |
| 714 | void AudioRtpSender::RemoveTrackFromStats() { |
| 715 | if (can_send_track() && legacy_stats_) { |
| 716 | legacy_stats_->RemoveLocalAudioTrack(audio_track().get(), ssrc_); |
| 717 | } |
| 718 | } |
| 719 |
| 720 | rtc::scoped_refptr<DtmfSenderInterface> AudioRtpSender::GetDtmfSender() const { |
| 721 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 722 | return dtmf_sender_proxy_; |
| 723 | } |
| 724 |
| 725 | RTCError AudioRtpSender::GenerateKeyFrame( |
| 726 | const std::vector<std::string>& rids) { |
| 727 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 728 | RTC_DLOG(LS_ERROR) << "Tried to get generate a key frame for audio."; |
| 729 | return RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 730 | "Generating key frames for audio is not supported."); |
| 731 | } |
| 732 |
| 733 | void AudioRtpSender::SetSend() { |
| 734 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 735 | RTC_DCHECK(!stopped_); |
| 736 | RTC_DCHECK(can_send_track()); |
| 737 | if (!media_channel_) { |
| 738 | RTC_LOG(LS_ERROR) << "SetAudioSend: No audio channel exists."; |
| 739 | return; |
| 740 | } |
| 741 | cricket::AudioOptions options; |
| 742 | #if !defined(WEBRTC_CHROMIUM_BUILD) && !defined(WEBRTC_WEBKIT_BUILD) |
| 743 | // TODO(tommi): Remove this hack when we move CreateAudioSource out of |
| 744 | // PeerConnection. This is a bit of a strange way to apply local audio |
| 745 | // options since it is also applied to all streams/channels, local or remote. |
| 746 | if (track_->enabled() && audio_track()->GetSource() && |
| 747 | !audio_track()->GetSource()->remote()) { |
| 748 | options = audio_track()->GetSource()->options(); |
| 749 | } |
| 750 | #endif |
| 751 |
| 752 | // `track_->enabled()` hops to the signaling thread, so call it before we hop |
| 753 | // to the worker thread or else it will deadlock. |
| 754 | bool track_enabled = track_->enabled(); |
| 755 | bool success = worker_thread_->BlockingCall([&] { |
| 756 | return voice_media_channel()->SetAudioSend(ssrc_, track_enabled, &options, |
| 757 | sink_adapter_.get()); |
| 758 | }); |
| 759 | if (!success) { |
| 760 | RTC_LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc_; |
| 761 | } |
| 762 | } |
| 763 |
| 764 | void AudioRtpSender::ClearSend() { |
| 765 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 766 | RTC_DCHECK(ssrc_ != 0); |
| 767 | RTC_DCHECK(!stopped_); |
| 768 | if (!media_channel_) { |
| 769 | RTC_LOG(LS_WARNING) << "ClearAudioSend: No audio channel exists."; |
| 770 | return; |
| 771 | } |
| 772 | cricket::AudioOptions options; |
| 773 | bool success = worker_thread_->BlockingCall([&] { |
| 774 | return voice_media_channel()->SetAudioSend(ssrc_, false, &options, nullptr); |
| 775 | }); |
| 776 | if (!success) { |
| 777 | RTC_LOG(LS_WARNING) << "ClearAudioSend: ssrc is incorrect: " << ssrc_; |
| 778 | } |
| 779 | } |
| 780 |
| 781 | rtc::scoped_refptr<VideoRtpSender> VideoRtpSender::Create( |
| 782 | rtc::Thread* worker_thread, |
| 783 | const std::string& id, |
| 784 | SetStreamsObserver* set_streams_observer) { |
| 785 | return rtc::make_ref_counted<VideoRtpSender>(worker_thread, id, |
| 786 | set_streams_observer); |
| 787 | } |
| 788 |
| 789 | VideoRtpSender::VideoRtpSender(rtc::Thread* worker_thread, |
| 790 | const std::string& id, |
| 791 | SetStreamsObserver* set_streams_observer) |
| 792 | : RtpSenderBase(worker_thread, id, set_streams_observer) {} |
| 793 |
| 794 | VideoRtpSender::~VideoRtpSender() { |
| 795 | Stop(); |
| 796 | } |
| 797 |
| 798 | void VideoRtpSender::OnChanged() { |
| 799 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 800 | TRACE_EVENT0("webrtc", "VideoRtpSender::OnChanged"); |
| 801 | RTC_DCHECK(!stopped_); |
| 802 |
| 803 | auto content_hint = video_track()->content_hint(); |
| 804 | if (cached_track_content_hint_ != content_hint) { |
| 805 | cached_track_content_hint_ = content_hint; |
| 806 | if (can_send_track()) { |
| 807 | SetSend(); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 |
| 812 | void VideoRtpSender::AttachTrack() { |
| 813 | RTC_DCHECK(track_); |
| 814 | cached_track_content_hint_ = video_track()->content_hint(); |
| 815 | } |
| 816 |
| 817 | rtc::scoped_refptr<DtmfSenderInterface> VideoRtpSender::GetDtmfSender() const { |
| 818 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 819 | RTC_DLOG(LS_ERROR) << "Tried to get DTMF sender from video sender."; |
| 820 | return nullptr; |
| 821 | } |
| 822 |
| 823 | RTCError VideoRtpSender::GenerateKeyFrame( |
| 824 | const std::vector<std::string>& rids) { |
| 825 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 826 | if (video_media_channel() && ssrc_ && !stopped_) { |
| 827 | const auto parameters = GetParametersInternal(); |
| 828 | for (const auto& rid : rids) { |
| 829 | if (rid.empty()) { |
| 830 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 831 | "Attempted to specify an empty rid."); |
| 832 | } |
| 833 | if (!absl::c_any_of(parameters.encodings, |
| 834 | [&rid](const RtpEncodingParameters& parameters) { |
| 835 | return parameters.rid == rid; |
| 836 | })) { |
| 837 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 838 | "Attempted to specify a rid not configured."); |
| 839 | } |
| 840 | } |
| 841 | worker_thread_->PostTask([&, rids] { |
| 842 | video_media_channel()->GenerateSendKeyFrame(ssrc_, rids); |
| 843 | }); |
| 844 | } else { |
| 845 | RTC_LOG(LS_WARNING) << "Tried to generate key frame for sender that is " |
| 846 | "stopped or has no media channel."; |
| 847 | } |
| 848 | return RTCError::OK(); |
| 849 | } |
| 850 |
| 851 | void VideoRtpSender::SetSend() { |
| 852 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 853 | RTC_DCHECK(!stopped_); |
| 854 | RTC_DCHECK(can_send_track()); |
| 855 | if (!media_channel_) { |
| 856 | RTC_LOG(LS_ERROR) << "SetVideoSend: No video channel exists."; |
| 857 | return; |
| 858 | } |
| 859 | cricket::VideoOptions options; |
| 860 | VideoTrackSourceInterface* source = video_track()->GetSource(); |
| 861 | if (source) { |
| 862 | options.is_screencast = source->is_screencast(); |
| 863 | options.video_noise_reduction = source->needs_denoising(); |
| 864 | } |
| 865 | options.content_hint = cached_track_content_hint_; |
| 866 | switch (cached_track_content_hint_) { |
| 867 | case VideoTrackInterface::ContentHint::kNone: |
| 868 | break; |
| 869 | case VideoTrackInterface::ContentHint::kFluid: |
| 870 | options.is_screencast = false; |
| 871 | break; |
| 872 | case VideoTrackInterface::ContentHint::kDetailed: |
| 873 | case VideoTrackInterface::ContentHint::kText: |
| 874 | options.is_screencast = true; |
| 875 | break; |
| 876 | } |
| 877 | bool success = worker_thread_->BlockingCall([&] { |
| 878 | return video_media_channel()->SetVideoSend(ssrc_, &options, |
| 879 | video_track().get()); |
| 880 | }); |
| 881 | RTC_DCHECK(success); |
| 882 | } |
| 883 |
| 884 | void VideoRtpSender::ClearSend() { |
| 885 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 886 | RTC_DCHECK(ssrc_ != 0); |
| 887 | RTC_DCHECK(!stopped_); |
| 888 | if (!media_channel_) { |
| 889 | RTC_LOG(LS_WARNING) << "SetVideoSend: No video channel exists."; |
| 890 | return; |
| 891 | } |
| 892 | // Allow SetVideoSend to fail since `enable` is false and `source` is null. |
| 893 | // This the normal case when the underlying media channel has already been |
| 894 | // deleted. |
| 895 | worker_thread_->BlockingCall( |
| 896 | [&] { video_media_channel()->SetVideoSend(ssrc_, nullptr, nullptr); }); |
| 897 | } |
| 898 |
| 899 | } // namespace webrtc |
| 900 |