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