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