channel_send.cc (39373B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 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 "audio/channel_send.h" 12 13 #include <algorithm> 14 #include <atomic> 15 #include <cstddef> 16 #include <cstdint> 17 #include <memory> 18 #include <optional> 19 #include <string> 20 #include <utility> 21 #include <vector> 22 23 #include "absl/functional/any_invocable.h" 24 #include "absl/strings/string_view.h" 25 #include "api/array_view.h" 26 #include "api/audio_codecs/audio_encoder.h" 27 #include "api/audio_codecs/audio_format.h" 28 #include "api/call/bitrate_allocation.h" 29 #include "api/call/transport.h" 30 #include "api/crypto/crypto_options.h" 31 #include "api/crypto/frame_encryptor_interface.h" 32 #include "api/environment/environment.h" 33 #include "api/frame_transformer_interface.h" 34 #include "api/function_view.h" 35 #include "api/make_ref_counted.h" 36 #include "api/media_types.h" 37 #include "api/rtp_headers.h" 38 #include "api/scoped_refptr.h" 39 #include "api/sequence_checker.h" 40 #include "api/task_queue/task_queue_base.h" 41 #include "api/task_queue/task_queue_factory.h" 42 #include "api/units/data_rate.h" 43 #include "api/units/data_size.h" 44 #include "api/units/time_delta.h" 45 #include "api/units/timestamp.h" 46 #include "audio/channel_send_frame_transformer_delegate.h" 47 #include "audio/utility/audio_frame_operations.h" 48 #include "call/rtp_transport_controller_send_interface.h" 49 #include "modules/audio_coding/include/audio_coding_module.h" 50 #include "modules/audio_coding/include/audio_coding_module_typedefs.h" 51 #include "modules/audio_processing/rms_level.h" 52 #include "modules/pacing/packet_router.h" 53 #include "modules/rtp_rtcp/include/report_block_data.h" 54 #include "modules/rtp_rtcp/include/rtcp_statistics.h" 55 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 56 #include "modules/rtp_rtcp/source/rtp_header_extensions.h" 57 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h" 58 #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h" 59 #include "modules/rtp_rtcp/source/rtp_sender_audio.h" 60 #include "rtc_base/buffer.h" 61 #include "rtc_base/checks.h" 62 #include "rtc_base/event.h" 63 #include "rtc_base/logging.h" 64 #include "rtc_base/race_checker.h" 65 #include "rtc_base/rate_limiter.h" 66 #include "rtc_base/strings/string_builder.h" 67 #include "rtc_base/synchronization/mutex.h" 68 #include "rtc_base/system/no_unique_address.h" 69 #include "rtc_base/thread_annotations.h" 70 #include "rtc_base/trace_event.h" 71 #include "system_wrappers/include/metrics.h" 72 73 namespace webrtc { 74 namespace voe { 75 76 namespace { 77 78 constexpr TimeDelta kMaxRetransmissionWindow = TimeDelta::Seconds(1); 79 constexpr TimeDelta kMinRetransmissionWindow = TimeDelta::Millis(30); 80 81 class RtpPacketSenderProxy; 82 class TransportSequenceNumberProxy; 83 84 class RtcpCounterObserver : public RtcpPacketTypeCounterObserver { 85 public: 86 explicit RtcpCounterObserver(uint32_t ssrc) : ssrc_(ssrc) {} 87 88 void RtcpPacketTypesCounterUpdated( 89 uint32_t ssrc, 90 const RtcpPacketTypeCounter& packet_counter) override { 91 if (ssrc_ != ssrc) { 92 return; 93 } 94 95 MutexLock lock(&mutex_); 96 packet_counter_ = packet_counter; 97 } 98 99 RtcpPacketTypeCounter GetCounts() { 100 MutexLock lock(&mutex_); 101 return packet_counter_; 102 } 103 104 private: 105 Mutex mutex_; 106 const uint32_t ssrc_; 107 RtcpPacketTypeCounter packet_counter_; 108 }; 109 110 class AudioBitrateAccountant { 111 public: 112 void RegisterPacketOverhead(int packet_byte_overhead) { 113 packet_overhead_ = DataSize::Bytes(packet_byte_overhead); 114 } 115 116 void Reset() { 117 rate_last_frame_ = DataRate::BitsPerSec(0); 118 next_frame_duration_ = TimeDelta::Millis(0); 119 report_rate_ = std::nullopt; 120 } 121 122 // A new frame is formed when bytesize is nonzero. 123 void UpdateBpsEstimate(DataSize payload_size, TimeDelta frame_duration) { 124 next_frame_duration_ += frame_duration; 125 // Do not have a full frame yet. 126 if (payload_size.bytes() == 0) 127 return; 128 129 // We report the larger of the rates computed using the last frame, and 130 // second last frame. Under DTX, frame sizes sometimes alternate, it is 131 // preferable to report the upper envelop. 132 DataRate rate_cur_frame = 133 (payload_size + packet_overhead_) / next_frame_duration_; 134 135 report_rate_ = 136 (rate_cur_frame > rate_last_frame_) ? rate_cur_frame : rate_last_frame_; 137 138 rate_last_frame_ = rate_cur_frame; 139 next_frame_duration_ = TimeDelta::Millis(0); 140 } 141 142 std::optional<DataRate> GetUsedRate() const { return report_rate_; } 143 144 private: 145 TimeDelta next_frame_duration_ = TimeDelta::Millis(0); 146 DataSize packet_overhead_ = DataSize::Bytes(72); 147 DataRate rate_last_frame_ = DataRate::BitsPerSec(0); 148 std::optional<DataRate> report_rate_; 149 }; 150 151 class ChannelSend : public ChannelSendInterface, 152 public AudioPacketizationCallback, // receive encoded 153 // packets from the ACM 154 public RtcpPacketTypeCounterObserver, 155 public ReportBlockDataObserver { 156 public: 157 ChannelSend(const Environment& env, 158 Transport* rtp_transport, 159 RtcpRttStats* rtcp_rtt_stats, 160 FrameEncryptorInterface* frame_encryptor, 161 const CryptoOptions& crypto_options, 162 bool extmap_allow_mixed, 163 int rtcp_report_interval_ms, 164 uint32_t ssrc, 165 scoped_refptr<FrameTransformerInterface> frame_transformer, 166 RtpTransportControllerSendInterface* transport_controller); 167 168 ~ChannelSend() override; 169 170 // Send using this encoder, with this payload type. 171 void SetEncoder(int payload_type, 172 const SdpAudioFormat& encoder_format, 173 std::unique_ptr<AudioEncoder> encoder) override; 174 void ModifyEncoder( 175 FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) override; 176 void CallEncoder(FunctionView<void(AudioEncoder*)> modifier) override; 177 178 // API methods 179 void StartSend() override; 180 void StopSend() override; 181 182 // Codecs 183 void OnBitrateAllocation(BitrateAllocationUpdate update) override; 184 int GetTargetBitrate() const override; 185 186 // Network 187 void ReceivedRTCPPacket(const uint8_t* data, size_t length) override; 188 189 // Muting, Volume and Level. 190 void SetInputMute(bool enable) override; 191 192 // CSRCs. 193 void SetCsrcs(ArrayView<const uint32_t> csrcs) override; 194 195 // Stats. 196 ANAStats GetANAStatistics() const override; 197 198 // Used by AudioSendStream. 199 RtpRtcpInterface* GetRtpRtcp() const override; 200 201 void RegisterCngPayloadType(int payload_type, int payload_frequency) override; 202 203 // DTMF. 204 bool SendTelephoneEventOutband(int event, int duration_ms) override; 205 void SetSendTelephoneEventPayloadType(int payload_type, 206 int payload_frequency) override; 207 208 // RTP+RTCP 209 void SetSendAudioLevelIndicationStatus(bool enable, int id) override; 210 211 void RegisterSenderCongestionControlObjects( 212 RtpTransportControllerSendInterface* transport) override; 213 void ResetSenderCongestionControlObjects() override; 214 void SetRTCP_CNAME(absl::string_view c_name) override; 215 std::vector<ReportBlockData> GetRemoteRTCPReportBlocks() const override; 216 ChannelSendStatistics GetRTCPStatistics() const override; 217 218 // ProcessAndEncodeAudio() posts a task on the shared encoder task queue, 219 // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where 220 // the actual processing of the audio takes place. The processing mainly 221 // consists of encoding and preparing the result for sending by adding it to a 222 // send queue. 223 // The main reason for using a task queue here is to release the native, 224 // OS-specific, audio capture thread as soon as possible to ensure that it 225 // can go back to sleep and be prepared to deliver an new captured audio 226 // packet. 227 void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame) override; 228 229 // E2EE Custom Audio Frame Encryption 230 void SetFrameEncryptor( 231 scoped_refptr<FrameEncryptorInterface> frame_encryptor) override; 232 233 // Sets a frame transformer between encoder and packetizer, to transform 234 // encoded frames before sending them out the network. 235 void SetEncoderToPacketizerFrameTransformer( 236 scoped_refptr<FrameTransformerInterface> frame_transformer) override; 237 238 // RtcpPacketTypeCounterObserver. 239 void RtcpPacketTypesCounterUpdated( 240 uint32_t ssrc, 241 const RtcpPacketTypeCounter& packet_counter) override; 242 243 // ReportBlockDataObserver. 244 void OnReportBlockDataUpdated(ReportBlockData report_block) override; 245 246 // Reports actual bitrate used (vs allocated). 247 std::optional<DataRate> GetUsedRate() const override { 248 MutexLock lock(&bitrate_accountant_mutex_); 249 return bitrate_accountant_.GetUsedRate(); 250 } 251 252 void RegisterPacketOverhead(int packet_byte_overhead) override { 253 MutexLock lock(&bitrate_accountant_mutex_); 254 bitrate_accountant_.RegisterPacketOverhead(packet_byte_overhead); 255 } 256 257 private: 258 // From AudioPacketizationCallback in the ACM 259 int32_t SendData(AudioFrameType frameType, 260 uint8_t payloadType, 261 uint32_t rtp_timestamp, 262 const uint8_t* payloadData, 263 size_t payloadSize, 264 int64_t absolute_capture_timestamp_ms) override; 265 266 bool InputMute() const; 267 268 int32_t SendRtpAudio(AudioFrameType frameType, 269 uint8_t payloadType, 270 uint32_t rtp_timestamp_without_offset, 271 ArrayView<const uint8_t> payload, 272 int64_t absolute_capture_timestamp_ms, 273 ArrayView<const uint32_t> csrcs, 274 std::optional<uint8_t> audio_level_dbov) 275 RTC_RUN_ON(encoder_queue_checker_); 276 277 void OnReceivedRtt(int64_t rtt_ms); 278 279 void InitFrameTransformerDelegate( 280 scoped_refptr<FrameTransformerInterface> frame_transformer); 281 282 // Calls the encoder on the encoder queue (instead of blocking). 283 void CallEncoderAsync(absl::AnyInvocable<void(AudioEncoder*)> modifier); 284 285 const Environment env_; 286 287 // Thread checkers document and lock usage of some methods on voe::Channel to 288 // specific threads we know about. The goal is to eventually split up 289 // voe::Channel into parts with single-threaded semantics, and thereby reduce 290 // the need for locks. 291 RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_thread_checker_; 292 // Methods accessed from audio and video threads are checked for sequential- 293 // only access. We don't necessarily own and control these threads, so thread 294 // checkers cannot be used. E.g. Chromium may transfer "ownership" from one 295 // audio thread to another, but access is still sequential. 296 RaceChecker audio_thread_race_checker_; 297 298 mutable Mutex volume_settings_mutex_; 299 300 const uint32_t ssrc_; 301 bool sending_ RTC_GUARDED_BY(&worker_thread_checker_) = false; 302 303 std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp_; 304 std::unique_ptr<RTPSenderAudio> rtp_sender_audio_; 305 306 std::unique_ptr<AudioCodingModule> audio_coding_; 307 308 // This is just an offset, RTP module will add its own random offset. 309 uint32_t timestamp_ RTC_GUARDED_BY(audio_thread_race_checker_) = 0; 310 std::optional<int64_t> last_capture_timestamp_ms_ 311 RTC_GUARDED_BY(audio_thread_race_checker_); 312 313 RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_checker_); 314 bool input_mute_ RTC_GUARDED_BY(volume_settings_mutex_) = false; 315 bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_checker_) = false; 316 317 const std::unique_ptr<RtcpCounterObserver> rtcp_counter_observer_; 318 319 PacketRouter* packet_router_ RTC_GUARDED_BY(&worker_thread_checker_) = 320 nullptr; 321 const std::unique_ptr<RtpPacketSenderProxy> rtp_packet_pacer_proxy_; 322 const std::unique_ptr<RateLimiter> retransmission_rate_limiter_; 323 324 RTC_NO_UNIQUE_ADDRESS SequenceChecker construction_thread_; 325 326 std::atomic<bool> include_audio_level_indication_ = false; 327 std::atomic<bool> encoder_queue_is_active_ = false; 328 std::atomic<bool> first_frame_ = true; 329 330 // E2EE Audio Frame Encryption 331 scoped_refptr<FrameEncryptorInterface> frame_encryptor_ 332 RTC_GUARDED_BY(encoder_queue_checker_); 333 // E2EE Frame Encryption Options 334 const CryptoOptions crypto_options_; 335 336 // Delegates calls to a frame transformer to transform audio, and 337 // receives callbacks with the transformed frames; delegates calls to 338 // ChannelSend::SendRtpAudio to send the transformed audio. 339 scoped_refptr<ChannelSendFrameTransformerDelegate> frame_transformer_delegate_ 340 RTC_GUARDED_BY(encoder_queue_checker_); 341 342 mutable Mutex rtcp_counter_mutex_; 343 RtcpPacketTypeCounter rtcp_packet_type_counter_ 344 RTC_GUARDED_BY(rtcp_counter_mutex_); 345 346 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> encoder_queue_; 347 RTC_NO_UNIQUE_ADDRESS SequenceChecker encoder_queue_checker_; 348 349 SdpAudioFormat encoder_format_; 350 351 mutable Mutex bitrate_accountant_mutex_; 352 AudioBitrateAccountant bitrate_accountant_ 353 RTC_GUARDED_BY(bitrate_accountant_mutex_); 354 355 std::vector<uint32_t> csrcs_ RTC_GUARDED_BY(encoder_queue_checker_); 356 }; 357 358 const int kTelephoneEventAttenuationdB = 10; 359 360 class RtpPacketSenderProxy : public RtpPacketSender { 361 public: 362 RtpPacketSenderProxy() : rtp_packet_pacer_(nullptr) {} 363 364 void SetPacketPacer(RtpPacketSender* rtp_packet_pacer) { 365 RTC_DCHECK(thread_checker_.IsCurrent()); 366 MutexLock lock(&mutex_); 367 rtp_packet_pacer_ = rtp_packet_pacer; 368 } 369 370 void EnqueuePackets( 371 std::vector<std::unique_ptr<RtpPacketToSend>> packets) override { 372 MutexLock lock(&mutex_); 373 374 // Since we allow having an instance with no rtp_packet_pacer_ set we 375 // should handle calls to member functions in this state gracefully rather 376 // than null dereferencing. 377 if (!rtp_packet_pacer_) { 378 RTC_DLOG(LS_WARNING) 379 << "Dropping packets queued while rtp_packet_pacer_ is null."; 380 return; 381 } 382 rtp_packet_pacer_->EnqueuePackets(std::move(packets)); 383 } 384 385 void RemovePacketsForSsrc(uint32_t ssrc) override { 386 MutexLock lock(&mutex_); 387 388 // Since we allow having an instance with no rtp_packet_pacer_ set we 389 // should handle calls to member functions in this state gracefully rather 390 // than null dereferencing. 391 if (!rtp_packet_pacer_) { 392 return; 393 } 394 rtp_packet_pacer_->RemovePacketsForSsrc(ssrc); 395 } 396 397 private: 398 RTC_NO_UNIQUE_ADDRESS SequenceChecker thread_checker_; 399 Mutex mutex_; 400 RtpPacketSender* rtp_packet_pacer_ RTC_GUARDED_BY(&mutex_); 401 }; 402 403 int32_t ChannelSend::SendData(AudioFrameType frameType, 404 uint8_t payloadType, 405 uint32_t rtp_timestamp, 406 const uint8_t* payloadData, 407 size_t payloadSize, 408 int64_t absolute_capture_timestamp_ms) { 409 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 410 ArrayView<const uint8_t> payload(payloadData, payloadSize); 411 412 std::optional<uint8_t> audio_level_dbov; 413 if (include_audio_level_indication_.load()) { 414 // Take the averaged audio levels from rms_level_ and reset it before 415 // invoking any async transformer. 416 audio_level_dbov = rms_level_.Average(); 417 } 418 419 if (frame_transformer_delegate_) { 420 // Asynchronously transform the payload before sending it. After the payload 421 // is transformed, the delegate will call SendRtpAudio to send it. 422 char buf[1024]; 423 SimpleStringBuilder mime_type(buf); 424 mime_type << MediaTypeToString(MediaType::AUDIO) << "/" 425 << encoder_format_.name; 426 frame_transformer_delegate_->Transform( 427 frameType, payloadType, rtp_timestamp + rtp_rtcp_->StartTimestamp(), 428 payloadData, payloadSize, absolute_capture_timestamp_ms, 429 rtp_rtcp_->SSRC(), mime_type.str(), audio_level_dbov, csrcs_); 430 return 0; 431 } 432 return SendRtpAudio(frameType, payloadType, rtp_timestamp, payload, 433 absolute_capture_timestamp_ms, csrcs_, audio_level_dbov); 434 } 435 436 int32_t ChannelSend::SendRtpAudio(AudioFrameType frameType, 437 uint8_t payloadType, 438 uint32_t rtp_timestamp_without_offset, 439 ArrayView<const uint8_t> payload, 440 int64_t absolute_capture_timestamp_ms, 441 ArrayView<const uint32_t> csrcs, 442 std::optional<uint8_t> audio_level_dbov) { 443 // E2EE Custom Audio Frame Encryption (This is optional). 444 // Keep this buffer around for the lifetime of the send call. 445 Buffer encrypted_audio_payload; 446 // We don't invoke encryptor if payload is empty, which means we are to send 447 // DTMF, or the encoder entered DTX. 448 // TODO(minyue): see whether DTMF packets should be encrypted or not. In 449 // current implementation, they are not. 450 if (!payload.empty()) { 451 if (frame_encryptor_ != nullptr) { 452 // TODO(benwright@webrtc.org) - Allocate enough to always encrypt inline. 453 // Allocate a buffer to hold the maximum possible encrypted payload. 454 size_t max_ciphertext_size = frame_encryptor_->GetMaxCiphertextByteSize( 455 MediaType::AUDIO, payload.size()); 456 encrypted_audio_payload.SetSize(max_ciphertext_size); 457 458 // Encrypt the audio payload into the buffer. 459 size_t bytes_written = 0; 460 int encrypt_status = 461 frame_encryptor_->Encrypt(MediaType::AUDIO, rtp_rtcp_->SSRC(), 462 /*additional_data=*/nullptr, payload, 463 encrypted_audio_payload, &bytes_written); 464 if (encrypt_status != 0) { 465 RTC_DLOG(LS_ERROR) 466 << "Channel::SendData() failed encrypt audio payload: " 467 << encrypt_status; 468 return -1; 469 } 470 // Resize the buffer to the exact number of bytes actually used. 471 encrypted_audio_payload.SetSize(bytes_written); 472 // Rewrite the payloadData and size to the new encrypted payload. 473 payload = encrypted_audio_payload; 474 } else if (crypto_options_.sframe.require_frame_encryption) { 475 RTC_DLOG(LS_ERROR) << "Channel::SendData() failed sending audio payload: " 476 "A frame encryptor is required but one is not set."; 477 return -1; 478 } 479 } 480 481 // Push data from ACM to RTP/RTCP-module to deliver audio frame for 482 // packetization. 483 if (!rtp_rtcp_->OnSendingRtpFrame(rtp_timestamp_without_offset, 484 absolute_capture_timestamp_ms, payloadType, 485 /*force_sender_report=*/false)) { 486 return -1; 487 } 488 489 // RTCPSender has it's own copy of the timestamp offset, added in 490 // RTCPSender::BuildSR, hence we must not add the in the offset for the above 491 // call. 492 // TODO(nisse): Delete RTCPSender:timestamp_offset_, and see if we can confine 493 // knowledge of the offset to a single place. 494 495 // This call will trigger Transport::SendPacket() from the RTP/RTCP module. 496 RTPSenderAudio::RtpAudioFrame frame = { 497 .type = frameType, 498 .payload = payload, 499 .payload_id = payloadType, 500 .rtp_timestamp = 501 rtp_timestamp_without_offset + rtp_rtcp_->StartTimestamp(), 502 .csrcs = csrcs}; 503 if (absolute_capture_timestamp_ms > 0) { 504 frame.capture_time = Timestamp::Millis(absolute_capture_timestamp_ms); 505 } 506 if (include_audio_level_indication_.load() && audio_level_dbov) { 507 frame.audio_level_dbov = *audio_level_dbov; 508 } 509 if (!rtp_sender_audio_->SendAudio(frame)) { 510 RTC_DLOG(LS_ERROR) 511 << "ChannelSend::SendData() failed to send data to RTP/RTCP module"; 512 return -1; 513 } 514 515 return 0; 516 } 517 518 ChannelSend::ChannelSend( 519 const Environment& env, 520 Transport* rtp_transport, 521 RtcpRttStats* rtcp_rtt_stats, 522 FrameEncryptorInterface* frame_encryptor, 523 const CryptoOptions& crypto_options, 524 bool extmap_allow_mixed, 525 int rtcp_report_interval_ms, 526 uint32_t ssrc, 527 scoped_refptr<FrameTransformerInterface> frame_transformer, 528 RtpTransportControllerSendInterface* transport_controller) 529 : env_(env), 530 ssrc_(ssrc), 531 rtcp_counter_observer_(new RtcpCounterObserver(ssrc)), 532 rtp_packet_pacer_proxy_(new RtpPacketSenderProxy()), 533 retransmission_rate_limiter_( 534 new RateLimiter(&env_.clock(), kMaxRetransmissionWindow.ms())), 535 frame_encryptor_(frame_encryptor), 536 crypto_options_(crypto_options), 537 encoder_queue_(env_.task_queue_factory().CreateTaskQueue( 538 "AudioEncoder", 539 TaskQueueFactory::Priority::NORMAL)), 540 encoder_queue_checker_(encoder_queue_.get()), 541 encoder_format_("x-unknown", 0, 0) { 542 audio_coding_ = AudioCodingModule::Create(); 543 544 RtpRtcpInterface::Configuration configuration; 545 configuration.report_block_data_observer = this; 546 configuration.network_link_rtcp_observer = 547 transport_controller->GetRtcpObserver(); 548 configuration.audio = true; 549 configuration.outgoing_transport = rtp_transport; 550 551 configuration.paced_sender = rtp_packet_pacer_proxy_.get(); 552 configuration.rtt_stats = rtcp_rtt_stats; 553 configuration.rtcp_packet_type_counter_observer = 554 rtcp_counter_observer_.get(); 555 if (env_.field_trials().IsDisabled("WebRTC-DisableRtxRateLimiter")) { 556 configuration.retransmission_rate_limiter = 557 retransmission_rate_limiter_.get(); 558 } 559 configuration.extmap_allow_mixed = extmap_allow_mixed; 560 configuration.rtcp_report_interval_ms = rtcp_report_interval_ms; 561 configuration.rtcp_packet_type_counter_observer = this; 562 configuration.local_media_ssrc = ssrc; 563 564 rtp_rtcp_ = std::make_unique<ModuleRtpRtcpImpl2>(env_, configuration); 565 rtp_rtcp_->SetSendingMediaStatus(false); 566 567 rtp_sender_audio_ = 568 std::make_unique<RTPSenderAudio>(&env_.clock(), rtp_rtcp_->RtpSender()); 569 570 // Ensure that RTCP is enabled by default for the created channel. 571 rtp_rtcp_->SetRTCPStatus(RtcpMode::kCompound); 572 573 int error = audio_coding_->RegisterTransportCallback(this); 574 RTC_DCHECK_EQ(0, error); 575 } 576 577 ChannelSend::~ChannelSend() { 578 RTC_DCHECK(construction_thread_.IsCurrent()); 579 580 // Reset and clear the frame_transformer_delegate_ on the encoder queue 581 // to avoid race conditions. 582 Event delegate_reset_event; 583 encoder_queue_->PostTask([this, &delegate_reset_event] { 584 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 585 if (frame_transformer_delegate_) { 586 frame_transformer_delegate_->Reset(); 587 frame_transformer_delegate_ = nullptr; 588 } 589 delegate_reset_event.Set(); 590 }); 591 delegate_reset_event.Wait(Event::kForever); 592 593 StopSend(); 594 int error = audio_coding_->RegisterTransportCallback(nullptr); 595 RTC_DCHECK_EQ(0, error); 596 597 // Delete the encoder task queue first to ensure that there are no running 598 // tasks when the other members are destroyed. 599 encoder_queue_ = nullptr; 600 } 601 602 void ChannelSend::StartSend() { 603 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 604 RTC_DCHECK(!sending_); 605 sending_ = true; 606 607 RTC_DCHECK(packet_router_); 608 packet_router_->AddSendRtpModule(rtp_rtcp_.get(), /*remb_candidate=*/false); 609 rtp_rtcp_->SetSendingMediaStatus(true); 610 int ret = rtp_rtcp_->SetSendingStatus(true); 611 RTC_DCHECK_EQ(0, ret); 612 613 // It is now OK to start processing on the encoder task queue. 614 first_frame_.store(true); 615 encoder_queue_is_active_.store(true); 616 } 617 618 void ChannelSend::StopSend() { 619 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 620 if (!sending_) { 621 return; 622 } 623 sending_ = false; 624 encoder_queue_is_active_.store(false); 625 626 // Wait until all pending encode tasks are executed and clear any remaining 627 // buffers in the encoder. 628 Event flush; 629 encoder_queue_->PostTask([this, &flush]() { 630 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 631 audio_coding_->Reset(); 632 flush.Set(); 633 }); 634 flush.Wait(Event::kForever); 635 636 // Reset sending SSRC and sequence number and triggers direct transmission 637 // of RTCP BYE 638 if (rtp_rtcp_->SetSendingStatus(false) == -1) { 639 RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending"; 640 } 641 rtp_rtcp_->SetSendingMediaStatus(false); 642 643 RTC_DCHECK(packet_router_); 644 packet_router_->RemoveSendRtpModule(rtp_rtcp_.get()); 645 rtp_packet_pacer_proxy_->RemovePacketsForSsrc(rtp_rtcp_->SSRC()); 646 } 647 648 void ChannelSend::SetEncoder(int payload_type, 649 const SdpAudioFormat& encoder_format, 650 std::unique_ptr<AudioEncoder> encoder) { 651 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 652 RTC_DCHECK_GE(payload_type, 0); 653 RTC_DCHECK_LE(payload_type, 127); 654 655 // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate) 656 // as well as some other things, so we collect this info and send it along. 657 rtp_rtcp_->RegisterSendPayloadFrequency(payload_type, 658 encoder->RtpTimestampRateHz()); 659 rtp_sender_audio_->RegisterAudioPayload("audio", payload_type, 660 encoder->RtpTimestampRateHz(), 661 encoder->NumChannels(), 0); 662 663 encoder_format_ = encoder_format; 664 audio_coding_->SetEncoder(std::move(encoder)); 665 } 666 667 void ChannelSend::ModifyEncoder( 668 FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) { 669 // This method can be called on the worker thread, module process thread 670 // or network thread. Audio coding is thread safe, so we do not need to 671 // enforce the calling thread. 672 audio_coding_->ModifyEncoder(modifier); 673 } 674 675 void ChannelSend::CallEncoder(FunctionView<void(AudioEncoder*)> modifier) { 676 ModifyEncoder([modifier](std::unique_ptr<AudioEncoder>* encoder_ptr) { 677 if (*encoder_ptr) { 678 modifier(encoder_ptr->get()); 679 } else { 680 RTC_DLOG(LS_WARNING) << "Trying to call unset encoder."; 681 } 682 }); 683 } 684 685 void ChannelSend::CallEncoderAsync( 686 absl::AnyInvocable<void(AudioEncoder*)> modifier) { 687 encoder_queue_->PostTask([this, modifier = std::move(modifier)]() mutable { 688 CallEncoder(modifier); 689 }); 690 } 691 692 void ChannelSend::OnBitrateAllocation(BitrateAllocationUpdate update) { 693 CallEncoderAsync([update](AudioEncoder* encoder) { 694 encoder->OnReceivedUplinkAllocation(update); 695 }); 696 retransmission_rate_limiter_->SetMaxRate(update.target_bitrate.bps()); 697 } 698 699 int ChannelSend::GetTargetBitrate() const { 700 return audio_coding_->GetTargetBitrate(); 701 } 702 703 void ChannelSend::OnReportBlockDataUpdated(ReportBlockData report_block) { 704 float packet_loss_rate = report_block.fraction_lost(); 705 CallEncoderAsync([packet_loss_rate](AudioEncoder* encoder) { 706 encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate); 707 }); 708 } 709 710 void ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) { 711 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 712 713 // Deliver RTCP packet to RTP/RTCP module for parsing 714 rtp_rtcp_->IncomingRtcpPacket(MakeArrayView(data, length)); 715 716 std::optional<TimeDelta> rtt = rtp_rtcp_->LastRtt(); 717 if (!rtt.has_value()) { 718 // Waiting for valid RTT. 719 return; 720 } 721 722 retransmission_rate_limiter_->SetWindowSize( 723 rtt->Clamped(kMinRetransmissionWindow, kMaxRetransmissionWindow).ms()); 724 725 OnReceivedRtt(rtt->ms()); 726 } 727 728 void ChannelSend::SetInputMute(bool enable) { 729 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 730 MutexLock lock(&volume_settings_mutex_); 731 input_mute_ = enable; 732 } 733 734 bool ChannelSend::InputMute() const { 735 MutexLock lock(&volume_settings_mutex_); 736 return input_mute_; 737 } 738 739 void ChannelSend::SetCsrcs(ArrayView<const uint32_t> csrcs) { 740 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 741 std::vector<uint32_t> csrcs_copy( 742 csrcs.begin(), 743 csrcs.begin() + std::min<size_t>(csrcs.size(), kRtpCsrcSize)); 744 encoder_queue_->PostTask([this, csrcs = std::move(csrcs_copy)]() mutable { 745 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 746 csrcs_ = csrcs; 747 }); 748 } 749 750 bool ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) { 751 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 752 RTC_DCHECK_LE(0, event); 753 RTC_DCHECK_GE(255, event); 754 RTC_DCHECK_LE(0, duration_ms); 755 RTC_DCHECK_GE(65535, duration_ms); 756 if (!sending_) { 757 return false; 758 } 759 if (rtp_sender_audio_->SendTelephoneEvent( 760 event, duration_ms, kTelephoneEventAttenuationdB) != 0) { 761 RTC_DLOG(LS_ERROR) << "SendTelephoneEvent() failed to send event"; 762 return false; 763 } 764 return true; 765 } 766 767 void ChannelSend::RegisterCngPayloadType(int payload_type, 768 int payload_frequency) { 769 rtp_rtcp_->RegisterSendPayloadFrequency(payload_type, payload_frequency); 770 rtp_sender_audio_->RegisterAudioPayload("CN", payload_type, payload_frequency, 771 1, 0); 772 } 773 774 void ChannelSend::SetSendTelephoneEventPayloadType(int payload_type, 775 int payload_frequency) { 776 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 777 RTC_DCHECK_LE(0, payload_type); 778 RTC_DCHECK_GE(127, payload_type); 779 rtp_rtcp_->RegisterSendPayloadFrequency(payload_type, payload_frequency); 780 rtp_sender_audio_->RegisterAudioPayload("telephone-event", payload_type, 781 payload_frequency, 0, 0); 782 } 783 784 void ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, int id) { 785 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 786 include_audio_level_indication_.store(enable); 787 if (enable) { 788 rtp_rtcp_->RegisterRtpHeaderExtension(AudioLevelExtension::Uri(), id); 789 } else { 790 rtp_rtcp_->DeregisterSendRtpHeaderExtension(AudioLevelExtension::Uri()); 791 } 792 } 793 794 void ChannelSend::RegisterSenderCongestionControlObjects( 795 RtpTransportControllerSendInterface* transport) { 796 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 797 RtpPacketSender* rtp_packet_pacer = transport->packet_sender(); 798 PacketRouter* packet_router = transport->packet_router(); 799 800 RTC_DCHECK(rtp_packet_pacer); 801 RTC_DCHECK(packet_router); 802 RTC_DCHECK(!packet_router_); 803 rtp_packet_pacer_proxy_->SetPacketPacer(rtp_packet_pacer); 804 rtp_rtcp_->SetStorePacketsStatus(true, 600); 805 packet_router_ = packet_router; 806 } 807 808 void ChannelSend::ResetSenderCongestionControlObjects() { 809 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 810 RTC_DCHECK(packet_router_); 811 rtp_rtcp_->SetStorePacketsStatus(false, 600); 812 packet_router_ = nullptr; 813 rtp_packet_pacer_proxy_->SetPacketPacer(nullptr); 814 } 815 816 void ChannelSend::SetRTCP_CNAME(absl::string_view c_name) { 817 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 818 // Note: SetCNAME() accepts a c string of length at most 255. 819 const std::string c_name_limited(c_name.substr(0, 255)); 820 int ret = rtp_rtcp_->SetCNAME(c_name_limited.c_str()) != 0; 821 RTC_DCHECK_EQ(0, ret) << "SetRTCP_CNAME() failed to set RTCP CNAME"; 822 } 823 824 std::vector<ReportBlockData> ChannelSend::GetRemoteRTCPReportBlocks() const { 825 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 826 // Get the report blocks from the latest received RTCP Sender or Receiver 827 // Report. Each element in the vector contains the sender's SSRC and a 828 // report block according to RFC 3550. 829 return rtp_rtcp_->GetLatestReportBlockData(); 830 } 831 832 ChannelSendStatistics ChannelSend::GetRTCPStatistics() const { 833 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 834 ChannelSendStatistics stats = { 835 .round_trip_time = rtp_rtcp_->LastRtt().value_or(TimeDelta::Zero())}; 836 stats.rtcp_packet_type_counts = rtcp_counter_observer_->GetCounts(); 837 838 StreamDataCounters rtp_stats; 839 StreamDataCounters rtx_stats; 840 rtp_rtcp_->GetSendStreamDataCounters(&rtp_stats, &rtx_stats); 841 stats.payload_bytes_sent = 842 rtp_stats.transmitted.payload_bytes + rtx_stats.transmitted.payload_bytes; 843 stats.header_and_padding_bytes_sent = 844 rtp_stats.transmitted.padding_bytes + rtp_stats.transmitted.header_bytes + 845 rtx_stats.transmitted.padding_bytes + rtx_stats.transmitted.header_bytes; 846 847 // TODO(https://crbug.com/webrtc/10555): RTX retransmissions should show up in 848 // separate outbound-rtp stream objects. 849 stats.retransmitted_bytes_sent = rtp_stats.retransmitted.payload_bytes; 850 stats.packets_sent = 851 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets; 852 stats.packets_sent_with_ect1 = rtp_stats.transmitted.packets_with_ect1 + 853 rtx_stats.transmitted.packets_with_ect1; 854 stats.total_packet_send_delay = rtp_stats.transmitted.total_packet_delay; 855 stats.retransmitted_packets_sent = rtp_stats.retransmitted.packets; 856 stats.report_block_datas = rtp_rtcp_->GetLatestReportBlockData(); 857 858 { 859 MutexLock lock(&rtcp_counter_mutex_); 860 stats.nacks_received = rtcp_packet_type_counter_.nack_packets; 861 } 862 863 return stats; 864 } 865 866 void ChannelSend::RtcpPacketTypesCounterUpdated( 867 uint32_t ssrc, 868 const RtcpPacketTypeCounter& packet_counter) { 869 if (ssrc != ssrc_) { 870 return; 871 } 872 MutexLock lock(&rtcp_counter_mutex_); 873 rtcp_packet_type_counter_ = packet_counter; 874 } 875 876 void ChannelSend::ProcessAndEncodeAudio( 877 std::unique_ptr<AudioFrame> audio_frame) { 878 TRACE_EVENT0("webrtc", "ChannelSend::ProcessAndEncodeAudio"); 879 880 RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_); 881 RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0); 882 RTC_DCHECK_LE(audio_frame->num_channels_, 8); 883 884 if (!encoder_queue_is_active_.load()) { 885 return; 886 } 887 888 // Update `timestamp_` based on the capture timestamp for the first frame 889 // after sending is resumed. 890 if (first_frame_.load()) { 891 first_frame_.store(false); 892 if (last_capture_timestamp_ms_ && 893 audio_frame->absolute_capture_timestamp_ms()) { 894 int64_t diff_ms = *audio_frame->absolute_capture_timestamp_ms() - 895 *last_capture_timestamp_ms_; 896 // Truncate to whole frames and subtract one since `timestamp_` was 897 // incremented after the last frame. 898 int64_t diff_frames = diff_ms * audio_frame->sample_rate_hz() / 1000 / 899 audio_frame->samples_per_channel() - 900 1; 901 timestamp_ += std::max<int64_t>( 902 diff_frames * audio_frame->samples_per_channel(), 0); 903 } 904 } 905 906 audio_frame->timestamp_ = timestamp_; 907 timestamp_ += audio_frame->samples_per_channel_; 908 last_capture_timestamp_ms_ = audio_frame->absolute_capture_timestamp_ms(); 909 910 // Profile time between when the audio frame is added to the task queue and 911 // when the task is actually executed. 912 Timestamp post_task_time = env_.clock().CurrentTime(); 913 encoder_queue_->PostTask( 914 [this, post_task_time, audio_frame = std::move(audio_frame)]() mutable { 915 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 916 if (!encoder_queue_is_active_.load()) { 917 return; 918 } 919 // Measure time between when the audio frame is added to the task queue 920 // and when the task is actually executed. Goal is to keep track of 921 // unwanted extra latency added by the task queue. 922 TimeDelta latency = post_task_time - env_.clock().CurrentTime(); 923 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs", 924 latency.ms()); 925 926 bool is_muted = InputMute(); 927 AudioFrameOperations::Mute(audio_frame.get(), previous_frame_muted_, 928 is_muted); 929 930 if (include_audio_level_indication_.load()) { 931 size_t length = 932 audio_frame->samples_per_channel_ * audio_frame->num_channels_; 933 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes); 934 if (is_muted && previous_frame_muted_) { 935 rms_level_.AnalyzeMuted(length); 936 } else { 937 rms_level_.Analyze( 938 ArrayView<const int16_t>(audio_frame->data(), length)); 939 } 940 } 941 previous_frame_muted_ = is_muted; 942 943 // This call will trigger AudioPacketizationCallback::SendData if 944 // encoding is done and payload is ready for packetization and 945 // transmission. Otherwise, it will return without invoking the 946 // callback. 947 int32_t encoded_bytes = audio_coding_->Add10MsData(*audio_frame); 948 MutexLock lock(&bitrate_accountant_mutex_); 949 if (encoded_bytes < 0) { 950 RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed."; 951 bitrate_accountant_.Reset(); 952 return; 953 } 954 bitrate_accountant_.UpdateBpsEstimate(DataSize::Bytes(encoded_bytes), 955 TimeDelta::Millis(10)); 956 }); 957 } 958 959 ANAStats ChannelSend::GetANAStatistics() const { 960 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 961 return audio_coding_->GetANAStats(); 962 } 963 964 RtpRtcpInterface* ChannelSend::GetRtpRtcp() const { 965 return rtp_rtcp_.get(); 966 } 967 968 void ChannelSend::SetFrameEncryptor( 969 scoped_refptr<FrameEncryptorInterface> frame_encryptor) { 970 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 971 encoder_queue_->PostTask([this, frame_encryptor]() mutable { 972 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 973 frame_encryptor_ = std::move(frame_encryptor); 974 }); 975 } 976 977 void ChannelSend::SetEncoderToPacketizerFrameTransformer( 978 scoped_refptr<FrameTransformerInterface> frame_transformer) { 979 RTC_DCHECK_RUN_ON(&worker_thread_checker_); 980 if (!frame_transformer) 981 return; 982 983 encoder_queue_->PostTask( 984 [this, frame_transformer = std::move(frame_transformer)]() mutable { 985 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 986 InitFrameTransformerDelegate(std::move(frame_transformer)); 987 }); 988 } 989 990 void ChannelSend::OnReceivedRtt(int64_t rtt_ms) { 991 CallEncoderAsync( 992 [rtt_ms](AudioEncoder* encoder) { encoder->OnReceivedRtt(rtt_ms); }); 993 } 994 995 void ChannelSend::InitFrameTransformerDelegate( 996 scoped_refptr<FrameTransformerInterface> frame_transformer) { 997 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 998 RTC_DCHECK(frame_transformer); 999 RTC_DCHECK(!frame_transformer_delegate_); 1000 1001 // Pass a callback to ChannelSend::SendRtpAudio, to be called by the delegate 1002 // to send the transformed audio. 1003 ChannelSendFrameTransformerDelegate::SendFrameCallback send_audio_callback = 1004 [this](AudioFrameType frameType, uint8_t payloadType, 1005 uint32_t rtp_timestamp_with_offset, 1006 ArrayView<const uint8_t> payload, 1007 int64_t absolute_capture_timestamp_ms, 1008 ArrayView<const uint32_t> csrcs, 1009 std::optional<uint8_t> audio_level_dbov) { 1010 RTC_DCHECK_RUN_ON(&encoder_queue_checker_); 1011 return SendRtpAudio( 1012 frameType, payloadType, 1013 rtp_timestamp_with_offset - rtp_rtcp_->StartTimestamp(), payload, 1014 absolute_capture_timestamp_ms, csrcs, audio_level_dbov); 1015 }; 1016 frame_transformer_delegate_ = 1017 make_ref_counted<ChannelSendFrameTransformerDelegate>( 1018 std::move(send_audio_callback), std::move(frame_transformer), 1019 encoder_queue_.get()); 1020 frame_transformer_delegate_->Init(); 1021 } 1022 1023 } // namespace 1024 1025 std::unique_ptr<ChannelSendInterface> CreateChannelSend( 1026 const Environment& env, 1027 Transport* rtp_transport, 1028 RtcpRttStats* rtcp_rtt_stats, 1029 FrameEncryptorInterface* frame_encryptor, 1030 const CryptoOptions& crypto_options, 1031 bool extmap_allow_mixed, 1032 int rtcp_report_interval_ms, 1033 uint32_t ssrc, 1034 scoped_refptr<FrameTransformerInterface> frame_transformer, 1035 RtpTransportControllerSendInterface* transport_controller) { 1036 return std::make_unique<ChannelSend>( 1037 env, rtp_transport, rtcp_rtt_stats, frame_encryptor, crypto_options, 1038 extmap_allow_mixed, rtcp_report_interval_ms, ssrc, 1039 std::move(frame_transformer), transport_controller); 1040 } 1041 1042 } // namespace voe 1043 } // namespace webrtc