Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 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 <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "absl/memory/memory.h" |
| 21 | #include "api/array_view.h" |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame^] | 22 | #include "api/crypto/frameencryptorinterface.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 23 | #include "audio/utility/audio_frame_operations.h" |
| 24 | #include "call/rtp_transport_controller_send_interface.h" |
| 25 | #include "logging/rtc_event_log/events/rtc_event_audio_playout.h" |
| 26 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 27 | #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h" |
| 28 | #include "modules/pacing/packet_router.h" |
| 29 | #include "modules/utility/include/process_thread.h" |
| 30 | #include "rtc_base/checks.h" |
| 31 | #include "rtc_base/criticalsection.h" |
| 32 | #include "rtc_base/format_macros.h" |
| 33 | #include "rtc_base/location.h" |
| 34 | #include "rtc_base/logging.h" |
| 35 | #include "rtc_base/rate_limiter.h" |
| 36 | #include "rtc_base/task_queue.h" |
| 37 | #include "rtc_base/thread_checker.h" |
| 38 | #include "rtc_base/timeutils.h" |
| 39 | #include "system_wrappers/include/field_trial.h" |
| 40 | #include "system_wrappers/include/metrics.h" |
| 41 | |
| 42 | namespace webrtc { |
| 43 | namespace voe { |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | constexpr int64_t kMaxRetransmissionWindowMs = 1000; |
| 48 | constexpr int64_t kMinRetransmissionWindowMs = 30; |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | const int kTelephoneEventAttenuationdB = 10; |
| 53 | |
| 54 | class TransportFeedbackProxy : public TransportFeedbackObserver { |
| 55 | public: |
| 56 | TransportFeedbackProxy() : feedback_observer_(nullptr) { |
| 57 | pacer_thread_.DetachFromThread(); |
| 58 | network_thread_.DetachFromThread(); |
| 59 | } |
| 60 | |
| 61 | void SetTransportFeedbackObserver( |
| 62 | TransportFeedbackObserver* feedback_observer) { |
| 63 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 | rtc::CritScope lock(&crit_); |
| 65 | feedback_observer_ = feedback_observer; |
| 66 | } |
| 67 | |
| 68 | // Implements TransportFeedbackObserver. |
| 69 | void AddPacket(uint32_t ssrc, |
| 70 | uint16_t sequence_number, |
| 71 | size_t length, |
| 72 | const PacedPacketInfo& pacing_info) override { |
| 73 | RTC_DCHECK(pacer_thread_.CalledOnValidThread()); |
| 74 | rtc::CritScope lock(&crit_); |
| 75 | if (feedback_observer_) |
| 76 | feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info); |
| 77 | } |
| 78 | |
| 79 | void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override { |
| 80 | RTC_DCHECK(network_thread_.CalledOnValidThread()); |
| 81 | rtc::CritScope lock(&crit_); |
| 82 | if (feedback_observer_) |
| 83 | feedback_observer_->OnTransportFeedback(feedback); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | rtc::CriticalSection crit_; |
| 88 | rtc::ThreadChecker thread_checker_; |
| 89 | rtc::ThreadChecker pacer_thread_; |
| 90 | rtc::ThreadChecker network_thread_; |
| 91 | TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_); |
| 92 | }; |
| 93 | |
| 94 | class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator { |
| 95 | public: |
| 96 | TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) { |
| 97 | pacer_thread_.DetachFromThread(); |
| 98 | } |
| 99 | |
| 100 | void SetSequenceNumberAllocator( |
| 101 | TransportSequenceNumberAllocator* seq_num_allocator) { |
| 102 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 103 | rtc::CritScope lock(&crit_); |
| 104 | seq_num_allocator_ = seq_num_allocator; |
| 105 | } |
| 106 | |
| 107 | // Implements TransportSequenceNumberAllocator. |
| 108 | uint16_t AllocateSequenceNumber() override { |
| 109 | RTC_DCHECK(pacer_thread_.CalledOnValidThread()); |
| 110 | rtc::CritScope lock(&crit_); |
| 111 | if (!seq_num_allocator_) |
| 112 | return 0; |
| 113 | return seq_num_allocator_->AllocateSequenceNumber(); |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | rtc::CriticalSection crit_; |
| 118 | rtc::ThreadChecker thread_checker_; |
| 119 | rtc::ThreadChecker pacer_thread_; |
| 120 | TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_); |
| 121 | }; |
| 122 | |
| 123 | class RtpPacketSenderProxy : public RtpPacketSender { |
| 124 | public: |
| 125 | RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {} |
| 126 | |
| 127 | void SetPacketSender(RtpPacketSender* rtp_packet_sender) { |
| 128 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 129 | rtc::CritScope lock(&crit_); |
| 130 | rtp_packet_sender_ = rtp_packet_sender; |
| 131 | } |
| 132 | |
| 133 | // Implements RtpPacketSender. |
| 134 | void InsertPacket(Priority priority, |
| 135 | uint32_t ssrc, |
| 136 | uint16_t sequence_number, |
| 137 | int64_t capture_time_ms, |
| 138 | size_t bytes, |
| 139 | bool retransmission) override { |
| 140 | rtc::CritScope lock(&crit_); |
| 141 | if (rtp_packet_sender_) { |
| 142 | rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number, |
| 143 | capture_time_ms, bytes, retransmission); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void SetAccountForAudioPackets(bool account_for_audio) override { |
| 148 | RTC_NOTREACHED(); |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | rtc::ThreadChecker thread_checker_; |
| 153 | rtc::CriticalSection crit_; |
| 154 | RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_); |
| 155 | }; |
| 156 | |
| 157 | class VoERtcpObserver : public RtcpBandwidthObserver { |
| 158 | public: |
| 159 | explicit VoERtcpObserver(ChannelSend* owner) |
| 160 | : owner_(owner), bandwidth_observer_(nullptr) {} |
| 161 | virtual ~VoERtcpObserver() {} |
| 162 | |
| 163 | void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) { |
| 164 | rtc::CritScope lock(&crit_); |
| 165 | bandwidth_observer_ = bandwidth_observer; |
| 166 | } |
| 167 | |
| 168 | void OnReceivedEstimatedBitrate(uint32_t bitrate) override { |
| 169 | rtc::CritScope lock(&crit_); |
| 170 | if (bandwidth_observer_) { |
| 171 | bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks, |
| 176 | int64_t rtt, |
| 177 | int64_t now_ms) override { |
| 178 | { |
| 179 | rtc::CritScope lock(&crit_); |
| 180 | if (bandwidth_observer_) { |
| 181 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt, |
| 182 | now_ms); |
| 183 | } |
| 184 | } |
| 185 | // TODO(mflodman): Do we need to aggregate reports here or can we jut send |
| 186 | // what we get? I.e. do we ever get multiple reports bundled into one RTCP |
| 187 | // report for VoiceEngine? |
| 188 | if (report_blocks.empty()) |
| 189 | return; |
| 190 | |
| 191 | int fraction_lost_aggregate = 0; |
| 192 | int total_number_of_packets = 0; |
| 193 | |
| 194 | // If receiving multiple report blocks, calculate the weighted average based |
| 195 | // on the number of packets a report refers to. |
| 196 | for (ReportBlockList::const_iterator block_it = report_blocks.begin(); |
| 197 | block_it != report_blocks.end(); ++block_it) { |
| 198 | // Find the previous extended high sequence number for this remote SSRC, |
| 199 | // to calculate the number of RTP packets this report refers to. Ignore if |
| 200 | // we haven't seen this SSRC before. |
| 201 | std::map<uint32_t, uint32_t>::iterator seq_num_it = |
| 202 | extended_max_sequence_number_.find(block_it->source_ssrc); |
| 203 | int number_of_packets = 0; |
| 204 | if (seq_num_it != extended_max_sequence_number_.end()) { |
| 205 | number_of_packets = |
| 206 | block_it->extended_highest_sequence_number - seq_num_it->second; |
| 207 | } |
| 208 | fraction_lost_aggregate += number_of_packets * block_it->fraction_lost; |
| 209 | total_number_of_packets += number_of_packets; |
| 210 | |
| 211 | extended_max_sequence_number_[block_it->source_ssrc] = |
| 212 | block_it->extended_highest_sequence_number; |
| 213 | } |
| 214 | int weighted_fraction_lost = 0; |
| 215 | if (total_number_of_packets > 0) { |
| 216 | weighted_fraction_lost = |
| 217 | (fraction_lost_aggregate + total_number_of_packets / 2) / |
| 218 | total_number_of_packets; |
| 219 | } |
| 220 | owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f); |
| 221 | } |
| 222 | |
| 223 | private: |
| 224 | ChannelSend* owner_; |
| 225 | // Maps remote side ssrc to extended highest sequence number received. |
| 226 | std::map<uint32_t, uint32_t> extended_max_sequence_number_; |
| 227 | rtc::CriticalSection crit_; |
| 228 | RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_); |
| 229 | }; |
| 230 | |
| 231 | class ChannelSend::ProcessAndEncodeAudioTask : public rtc::QueuedTask { |
| 232 | public: |
| 233 | ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame, |
| 234 | ChannelSend* channel) |
| 235 | : audio_frame_(std::move(audio_frame)), channel_(channel) { |
| 236 | RTC_DCHECK(channel_); |
| 237 | } |
| 238 | |
| 239 | private: |
| 240 | bool Run() override { |
| 241 | RTC_DCHECK_RUN_ON(channel_->encoder_queue_); |
| 242 | channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get()); |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | std::unique_ptr<AudioFrame> audio_frame_; |
| 247 | ChannelSend* const channel_; |
| 248 | }; |
| 249 | |
| 250 | int32_t ChannelSend::SendData(FrameType frameType, |
| 251 | uint8_t payloadType, |
| 252 | uint32_t timeStamp, |
| 253 | const uint8_t* payloadData, |
| 254 | size_t payloadSize, |
| 255 | const RTPFragmentationHeader* fragmentation) { |
| 256 | RTC_DCHECK_RUN_ON(encoder_queue_); |
| 257 | if (_includeAudioLevelIndication) { |
| 258 | // Store current audio level in the RTP/RTCP module. |
| 259 | // The level will be used in combination with voice-activity state |
| 260 | // (frameType) to add an RTP header extension |
| 261 | _rtpRtcpModule->SetAudioLevel(rms_level_.Average()); |
| 262 | } |
| 263 | |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame^] | 264 | // E2EE Custom Audio Frame Encryption (This is optional). |
| 265 | // Keep this buffer around for the lifetime of the send call. |
| 266 | rtc::Buffer encrypted_audio_payload; |
| 267 | if (frame_encryptor_ != nullptr) { |
| 268 | // TODO(benwright@webrtc.org) - Allocate enough to always encrypt inline. |
| 269 | // Allocate a buffer to hold the maximum possible encrypted payload. |
| 270 | size_t max_ciphertext_size = frame_encryptor_->GetMaxCiphertextByteSize( |
| 271 | cricket::MEDIA_TYPE_AUDIO, payloadSize); |
| 272 | encrypted_audio_payload.SetSize(max_ciphertext_size); |
| 273 | |
| 274 | // Encrypt the audio payload into the buffer. |
| 275 | size_t bytes_written = 0; |
| 276 | int encrypt_status = frame_encryptor_->Encrypt( |
| 277 | cricket::MEDIA_TYPE_AUDIO, _rtpRtcpModule->SSRC(), |
| 278 | /*additional_data=*/nullptr, |
| 279 | rtc::ArrayView<const uint8_t>(payloadData, payloadSize), |
| 280 | encrypted_audio_payload, &bytes_written); |
| 281 | if (encrypt_status != 0) { |
| 282 | RTC_DLOG(LS_ERROR) << "Channel::SendData() failed encrypt audio payload: " |
| 283 | << encrypt_status; |
| 284 | return -1; |
| 285 | } |
| 286 | // Resize the buffer to the exact number of bytes actually used. |
| 287 | encrypted_audio_payload.SetSize(bytes_written); |
| 288 | // Rewrite the payloadData and size to the new encrypted payload. |
| 289 | payloadData = encrypted_audio_payload.data(); |
| 290 | payloadSize = encrypted_audio_payload.size(); |
| 291 | } |
| 292 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 293 | // Push data from ACM to RTP/RTCP-module to deliver audio frame for |
| 294 | // packetization. |
| 295 | // This call will trigger Transport::SendPacket() from the RTP/RTCP module. |
| 296 | if (!_rtpRtcpModule->SendOutgoingData( |
| 297 | (FrameType&)frameType, payloadType, timeStamp, |
| 298 | // Leaving the time when this frame was |
| 299 | // received from the capture device as |
| 300 | // undefined for voice for now. |
| 301 | -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) { |
| 302 | RTC_DLOG(LS_ERROR) |
| 303 | << "ChannelSend::SendData() failed to send data to RTP/RTCP module"; |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | bool ChannelSend::SendRtp(const uint8_t* data, |
| 311 | size_t len, |
| 312 | const PacketOptions& options) { |
| 313 | rtc::CritScope cs(&_callbackCritSect); |
| 314 | |
| 315 | if (_transportPtr == NULL) { |
| 316 | RTC_DLOG(LS_ERROR) |
| 317 | << "ChannelSend::SendPacket() failed to send RTP packet due to" |
| 318 | << " invalid transport object"; |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | if (!_transportPtr->SendRtp(data, len, options)) { |
| 323 | RTC_DLOG(LS_ERROR) << "ChannelSend::SendPacket() RTP transmission failed"; |
| 324 | return false; |
| 325 | } |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | bool ChannelSend::SendRtcp(const uint8_t* data, size_t len) { |
| 330 | rtc::CritScope cs(&_callbackCritSect); |
| 331 | if (_transportPtr == NULL) { |
| 332 | RTC_DLOG(LS_ERROR) |
| 333 | << "ChannelSend::SendRtcp() failed to send RTCP packet due to" |
| 334 | << " invalid transport object"; |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | int n = _transportPtr->SendRtcp(data, len); |
| 339 | if (n < 0) { |
| 340 | RTC_DLOG(LS_ERROR) << "ChannelSend::SendRtcp() transmission failed"; |
| 341 | return false; |
| 342 | } |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | int ChannelSend::PreferredSampleRate() const { |
| 347 | // Return the bigger of playout and receive frequency in the ACM. |
| 348 | return std::max(audio_coding_->ReceiveFrequency(), |
| 349 | audio_coding_->PlayoutFrequency()); |
| 350 | } |
| 351 | |
| 352 | ChannelSend::ChannelSend(rtc::TaskQueue* encoder_queue, |
| 353 | ProcessThread* module_process_thread, |
| 354 | RtcpRttStats* rtcp_rtt_stats, |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame^] | 355 | RtcEventLog* rtc_event_log, |
| 356 | FrameEncryptorInterface* frame_encryptor) |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 357 | : event_log_(rtc_event_log), |
| 358 | _timeStamp(0), // This is just an offset, RTP module will add it's own |
| 359 | // random offset |
| 360 | send_sequence_number_(0), |
| 361 | _moduleProcessThreadPtr(module_process_thread), |
| 362 | _transportPtr(NULL), |
| 363 | input_mute_(false), |
| 364 | previous_frame_muted_(false), |
| 365 | _includeAudioLevelIndication(false), |
| 366 | transport_overhead_per_packet_(0), |
| 367 | rtp_overhead_per_packet_(0), |
| 368 | rtcp_observer_(new VoERtcpObserver(this)), |
| 369 | feedback_observer_proxy_(new TransportFeedbackProxy()), |
| 370 | seq_num_allocator_proxy_(new TransportSequenceNumberProxy()), |
| 371 | rtp_packet_sender_proxy_(new RtpPacketSenderProxy()), |
| 372 | retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(), |
| 373 | kMaxRetransmissionWindowMs)), |
| 374 | use_twcc_plr_for_ana_( |
| 375 | webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled"), |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame^] | 376 | encoder_queue_(encoder_queue), |
| 377 | frame_encryptor_(frame_encryptor) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 378 | RTC_DCHECK(module_process_thread); |
| 379 | RTC_DCHECK(encoder_queue); |
| 380 | audio_coding_.reset(AudioCodingModule::Create(AudioCodingModule::Config())); |
| 381 | |
| 382 | RtpRtcp::Configuration configuration; |
| 383 | configuration.audio = true; |
| 384 | configuration.outgoing_transport = this; |
| 385 | configuration.overhead_observer = this; |
| 386 | configuration.bandwidth_callback = rtcp_observer_.get(); |
| 387 | |
| 388 | configuration.paced_sender = rtp_packet_sender_proxy_.get(); |
| 389 | configuration.transport_sequence_number_allocator = |
| 390 | seq_num_allocator_proxy_.get(); |
| 391 | configuration.transport_feedback_callback = feedback_observer_proxy_.get(); |
| 392 | |
| 393 | configuration.event_log = event_log_; |
| 394 | configuration.rtt_stats = rtcp_rtt_stats; |
| 395 | configuration.retransmission_rate_limiter = |
| 396 | retransmission_rate_limiter_.get(); |
| 397 | |
| 398 | _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration)); |
| 399 | _rtpRtcpModule->SetSendingMediaStatus(false); |
| 400 | Init(); |
| 401 | } |
| 402 | |
| 403 | ChannelSend::~ChannelSend() { |
| 404 | Terminate(); |
| 405 | RTC_DCHECK(!channel_state_.Get().sending); |
| 406 | } |
| 407 | |
| 408 | void ChannelSend::Init() { |
| 409 | channel_state_.Reset(); |
| 410 | |
| 411 | // --- Add modules to process thread (for periodic schedulation) |
| 412 | _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE); |
| 413 | |
| 414 | // --- ACM initialization |
| 415 | int error = audio_coding_->InitializeReceiver(); |
| 416 | RTC_DCHECK_EQ(0, error); |
| 417 | |
| 418 | // --- RTP/RTCP module initialization |
| 419 | |
| 420 | // Ensure that RTCP is enabled by default for the created channel. |
| 421 | // Note that, the module will keep generating RTCP until it is explicitly |
| 422 | // disabled by the user. |
| 423 | // After StopListen (when no sockets exists), RTCP packets will no longer |
| 424 | // be transmitted since the Transport object will then be invalid. |
| 425 | // RTCP is enabled by default. |
| 426 | _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound); |
| 427 | |
| 428 | // --- Register all permanent callbacks |
| 429 | error = audio_coding_->RegisterTransportCallback(this); |
| 430 | RTC_DCHECK_EQ(0, error); |
| 431 | } |
| 432 | |
| 433 | void ChannelSend::Terminate() { |
| 434 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
| 435 | // Must be called on the same thread as Init(). |
| 436 | |
| 437 | StopSend(); |
| 438 | |
| 439 | // The order to safely shutdown modules in a channel is: |
| 440 | // 1. De-register callbacks in modules |
| 441 | // 2. De-register modules in process thread |
| 442 | // 3. Destroy modules |
| 443 | int error = audio_coding_->RegisterTransportCallback(NULL); |
| 444 | RTC_DCHECK_EQ(0, error); |
| 445 | |
| 446 | // De-register modules in process thread |
| 447 | if (_moduleProcessThreadPtr) |
| 448 | _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()); |
| 449 | |
| 450 | // End of modules shutdown |
| 451 | } |
| 452 | |
| 453 | int32_t ChannelSend::StartSend() { |
| 454 | if (channel_state_.Get().sending) { |
| 455 | return 0; |
| 456 | } |
| 457 | channel_state_.SetSending(true); |
| 458 | |
| 459 | // Resume the previous sequence number which was reset by StopSend(). This |
| 460 | // needs to be done before |sending| is set to true on the RTP/RTCP module. |
| 461 | if (send_sequence_number_) { |
| 462 | _rtpRtcpModule->SetSequenceNumber(send_sequence_number_); |
| 463 | } |
| 464 | _rtpRtcpModule->SetSendingMediaStatus(true); |
| 465 | if (_rtpRtcpModule->SetSendingStatus(true) != 0) { |
| 466 | RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to start sending"; |
| 467 | _rtpRtcpModule->SetSendingMediaStatus(false); |
| 468 | rtc::CritScope cs(&_callbackCritSect); |
| 469 | channel_state_.SetSending(false); |
| 470 | return -1; |
| 471 | } |
| 472 | { |
| 473 | // It is now OK to start posting tasks to the encoder task queue. |
| 474 | rtc::CritScope cs(&encoder_queue_lock_); |
| 475 | encoder_queue_is_active_ = true; |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | void ChannelSend::StopSend() { |
| 481 | if (!channel_state_.Get().sending) { |
| 482 | return; |
| 483 | } |
| 484 | channel_state_.SetSending(false); |
| 485 | |
| 486 | // Post a task to the encoder thread which sets an event when the task is |
| 487 | // executed. We know that no more encoding tasks will be added to the task |
| 488 | // queue for this channel since sending is now deactivated. It means that, |
| 489 | // if we wait for the event to bet set, we know that no more pending tasks |
| 490 | // exists and it is therfore guaranteed that the task queue will never try |
| 491 | // to acccess and invalid channel object. |
| 492 | RTC_DCHECK(encoder_queue_); |
| 493 | |
| 494 | rtc::Event flush(false, false); |
| 495 | { |
| 496 | // Clear |encoder_queue_is_active_| under lock to prevent any other tasks |
| 497 | // than this final "flush task" to be posted on the queue. |
| 498 | rtc::CritScope cs(&encoder_queue_lock_); |
| 499 | encoder_queue_is_active_ = false; |
| 500 | encoder_queue_->PostTask([&flush]() { flush.Set(); }); |
| 501 | } |
| 502 | flush.Wait(rtc::Event::kForever); |
| 503 | |
| 504 | // Store the sequence number to be able to pick up the same sequence for |
| 505 | // the next StartSend(). This is needed for restarting device, otherwise |
| 506 | // it might cause libSRTP to complain about packets being replayed. |
| 507 | // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring |
| 508 | // CL is landed. See issue |
| 509 | // https://code.google.com/p/webrtc/issues/detail?id=2111 . |
| 510 | send_sequence_number_ = _rtpRtcpModule->SequenceNumber(); |
| 511 | |
| 512 | // Reset sending SSRC and sequence number and triggers direct transmission |
| 513 | // of RTCP BYE |
| 514 | if (_rtpRtcpModule->SetSendingStatus(false) == -1) { |
| 515 | RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending"; |
| 516 | } |
| 517 | _rtpRtcpModule->SetSendingMediaStatus(false); |
| 518 | } |
| 519 | |
| 520 | bool ChannelSend::SetEncoder(int payload_type, |
| 521 | std::unique_ptr<AudioEncoder> encoder) { |
| 522 | RTC_DCHECK_GE(payload_type, 0); |
| 523 | RTC_DCHECK_LE(payload_type, 127); |
| 524 | // TODO(ossu): Make CodecInsts up, for now: one for the RTP/RTCP module and |
| 525 | // one for for us to keep track of sample rate and number of channels, etc. |
| 526 | |
| 527 | // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate) |
| 528 | // as well as some other things, so we collect this info and send it along. |
| 529 | CodecInst rtp_codec; |
| 530 | rtp_codec.pltype = payload_type; |
| 531 | strncpy(rtp_codec.plname, "audio", sizeof(rtp_codec.plname)); |
| 532 | rtp_codec.plname[sizeof(rtp_codec.plname) - 1] = 0; |
| 533 | // Seems unclear if it should be clock rate or sample rate. CodecInst |
| 534 | // supposedly carries the sample rate, but only clock rate seems sensible to |
| 535 | // send to the RTP/RTCP module. |
| 536 | rtp_codec.plfreq = encoder->RtpTimestampRateHz(); |
| 537 | rtp_codec.pacsize = rtc::CheckedDivExact( |
| 538 | static_cast<int>(encoder->Max10MsFramesInAPacket() * rtp_codec.plfreq), |
| 539 | 100); |
| 540 | rtp_codec.channels = encoder->NumChannels(); |
| 541 | rtp_codec.rate = 0; |
| 542 | |
| 543 | if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) { |
| 544 | _rtpRtcpModule->DeRegisterSendPayload(payload_type); |
| 545 | if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) { |
| 546 | RTC_DLOG(LS_ERROR) |
| 547 | << "SetEncoder() failed to register codec to RTP/RTCP module"; |
| 548 | return false; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | audio_coding_->SetEncoder(std::move(encoder)); |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | void ChannelSend::ModifyEncoder( |
| 557 | rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) { |
| 558 | audio_coding_->ModifyEncoder(modifier); |
| 559 | } |
| 560 | |
| 561 | void ChannelSend::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) { |
| 562 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 563 | if (*encoder) { |
| 564 | (*encoder)->OnReceivedUplinkBandwidth(bitrate_bps, probing_interval_ms); |
| 565 | } |
| 566 | }); |
| 567 | retransmission_rate_limiter_->SetMaxRate(bitrate_bps); |
| 568 | } |
| 569 | |
| 570 | void ChannelSend::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) { |
| 571 | if (!use_twcc_plr_for_ana_) |
| 572 | return; |
| 573 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 574 | if (*encoder) { |
| 575 | (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate); |
| 576 | } |
| 577 | }); |
| 578 | } |
| 579 | |
| 580 | void ChannelSend::OnRecoverableUplinkPacketLossRate( |
| 581 | float recoverable_packet_loss_rate) { |
| 582 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 583 | if (*encoder) { |
| 584 | (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction( |
| 585 | recoverable_packet_loss_rate); |
| 586 | } |
| 587 | }); |
| 588 | } |
| 589 | |
| 590 | void ChannelSend::OnUplinkPacketLossRate(float packet_loss_rate) { |
| 591 | if (use_twcc_plr_for_ana_) |
| 592 | return; |
| 593 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 594 | if (*encoder) { |
| 595 | (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate); |
| 596 | } |
| 597 | }); |
| 598 | } |
| 599 | |
| 600 | bool ChannelSend::EnableAudioNetworkAdaptor(const std::string& config_string) { |
| 601 | bool success = false; |
| 602 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 603 | if (*encoder) { |
| 604 | success = |
| 605 | (*encoder)->EnableAudioNetworkAdaptor(config_string, event_log_); |
| 606 | } |
| 607 | }); |
| 608 | return success; |
| 609 | } |
| 610 | |
| 611 | void ChannelSend::DisableAudioNetworkAdaptor() { |
| 612 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 613 | if (*encoder) |
| 614 | (*encoder)->DisableAudioNetworkAdaptor(); |
| 615 | }); |
| 616 | } |
| 617 | |
| 618 | void ChannelSend::SetReceiverFrameLengthRange(int min_frame_length_ms, |
| 619 | int max_frame_length_ms) { |
| 620 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 621 | if (*encoder) { |
| 622 | (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms, |
| 623 | max_frame_length_ms); |
| 624 | } |
| 625 | }); |
| 626 | } |
| 627 | |
| 628 | void ChannelSend::RegisterTransport(Transport* transport) { |
| 629 | rtc::CritScope cs(&_callbackCritSect); |
| 630 | _transportPtr = transport; |
| 631 | } |
| 632 | |
| 633 | int32_t ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) { |
| 634 | // Deliver RTCP packet to RTP/RTCP module for parsing |
| 635 | _rtpRtcpModule->IncomingRtcpPacket(data, length); |
| 636 | |
| 637 | int64_t rtt = GetRTT(); |
| 638 | if (rtt == 0) { |
| 639 | // Waiting for valid RTT. |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | int64_t nack_window_ms = rtt; |
| 644 | if (nack_window_ms < kMinRetransmissionWindowMs) { |
| 645 | nack_window_ms = kMinRetransmissionWindowMs; |
| 646 | } else if (nack_window_ms > kMaxRetransmissionWindowMs) { |
| 647 | nack_window_ms = kMaxRetransmissionWindowMs; |
| 648 | } |
| 649 | retransmission_rate_limiter_->SetWindowSize(nack_window_ms); |
| 650 | |
| 651 | // Invoke audio encoders OnReceivedRtt(). |
| 652 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 653 | if (*encoder) |
| 654 | (*encoder)->OnReceivedRtt(rtt); |
| 655 | }); |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | void ChannelSend::SetInputMute(bool enable) { |
| 661 | rtc::CritScope cs(&volume_settings_critsect_); |
| 662 | input_mute_ = enable; |
| 663 | } |
| 664 | |
| 665 | bool ChannelSend::InputMute() const { |
| 666 | rtc::CritScope cs(&volume_settings_critsect_); |
| 667 | return input_mute_; |
| 668 | } |
| 669 | |
| 670 | int ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) { |
| 671 | RTC_DCHECK_LE(0, event); |
| 672 | RTC_DCHECK_GE(255, event); |
| 673 | RTC_DCHECK_LE(0, duration_ms); |
| 674 | RTC_DCHECK_GE(65535, duration_ms); |
| 675 | if (!Sending()) { |
| 676 | return -1; |
| 677 | } |
| 678 | if (_rtpRtcpModule->SendTelephoneEventOutband( |
| 679 | event, duration_ms, kTelephoneEventAttenuationdB) != 0) { |
| 680 | RTC_DLOG(LS_ERROR) << "SendTelephoneEventOutband() failed to send event"; |
| 681 | return -1; |
| 682 | } |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | int ChannelSend::SetSendTelephoneEventPayloadType(int payload_type, |
| 687 | int payload_frequency) { |
| 688 | RTC_DCHECK_LE(0, payload_type); |
| 689 | RTC_DCHECK_GE(127, payload_type); |
| 690 | CodecInst codec = {0}; |
| 691 | codec.pltype = payload_type; |
| 692 | codec.plfreq = payload_frequency; |
| 693 | memcpy(codec.plname, "telephone-event", 16); |
| 694 | if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) { |
| 695 | _rtpRtcpModule->DeRegisterSendPayload(codec.pltype); |
| 696 | if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) { |
| 697 | RTC_DLOG(LS_ERROR) |
| 698 | << "SetSendTelephoneEventPayloadType() failed to register " |
| 699 | "send payload type"; |
| 700 | return -1; |
| 701 | } |
| 702 | } |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | int ChannelSend::SetLocalSSRC(unsigned int ssrc) { |
| 707 | if (channel_state_.Get().sending) { |
| 708 | RTC_DLOG(LS_ERROR) << "SetLocalSSRC() already sending"; |
| 709 | return -1; |
| 710 | } |
| 711 | _rtpRtcpModule->SetSSRC(ssrc); |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | void ChannelSend::SetMid(const std::string& mid, int extension_id) { |
| 716 | int ret = SetSendRtpHeaderExtension(true, kRtpExtensionMid, extension_id); |
| 717 | RTC_DCHECK_EQ(0, ret); |
| 718 | _rtpRtcpModule->SetMid(mid); |
| 719 | } |
| 720 | |
| 721 | int ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, |
| 722 | unsigned char id) { |
| 723 | _includeAudioLevelIndication = enable; |
| 724 | return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id); |
| 725 | } |
| 726 | |
| 727 | void ChannelSend::EnableSendTransportSequenceNumber(int id) { |
| 728 | int ret = |
| 729 | SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id); |
| 730 | RTC_DCHECK_EQ(0, ret); |
| 731 | } |
| 732 | |
| 733 | void ChannelSend::RegisterSenderCongestionControlObjects( |
| 734 | RtpTransportControllerSendInterface* transport, |
| 735 | RtcpBandwidthObserver* bandwidth_observer) { |
| 736 | RtpPacketSender* rtp_packet_sender = transport->packet_sender(); |
| 737 | TransportFeedbackObserver* transport_feedback_observer = |
| 738 | transport->transport_feedback_observer(); |
| 739 | PacketRouter* packet_router = transport->packet_router(); |
| 740 | |
| 741 | RTC_DCHECK(rtp_packet_sender); |
| 742 | RTC_DCHECK(transport_feedback_observer); |
| 743 | RTC_DCHECK(packet_router); |
| 744 | RTC_DCHECK(!packet_router_); |
| 745 | rtcp_observer_->SetBandwidthObserver(bandwidth_observer); |
| 746 | feedback_observer_proxy_->SetTransportFeedbackObserver( |
| 747 | transport_feedback_observer); |
| 748 | seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router); |
| 749 | rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender); |
| 750 | _rtpRtcpModule->SetStorePacketsStatus(true, 600); |
| 751 | constexpr bool remb_candidate = false; |
| 752 | packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate); |
| 753 | packet_router_ = packet_router; |
| 754 | } |
| 755 | |
| 756 | void ChannelSend::ResetSenderCongestionControlObjects() { |
| 757 | RTC_DCHECK(packet_router_); |
| 758 | _rtpRtcpModule->SetStorePacketsStatus(false, 600); |
| 759 | rtcp_observer_->SetBandwidthObserver(nullptr); |
| 760 | feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr); |
| 761 | seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr); |
| 762 | packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get()); |
| 763 | packet_router_ = nullptr; |
| 764 | rtp_packet_sender_proxy_->SetPacketSender(nullptr); |
| 765 | } |
| 766 | |
| 767 | void ChannelSend::SetRTCPStatus(bool enable) { |
| 768 | _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff); |
| 769 | } |
| 770 | |
| 771 | int ChannelSend::SetRTCP_CNAME(const char cName[256]) { |
| 772 | if (_rtpRtcpModule->SetCNAME(cName) != 0) { |
| 773 | RTC_DLOG(LS_ERROR) << "SetRTCP_CNAME() failed to set RTCP CNAME"; |
| 774 | return -1; |
| 775 | } |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | int ChannelSend::GetRemoteRTCPReportBlocks( |
| 780 | std::vector<ReportBlock>* report_blocks) { |
| 781 | if (report_blocks == NULL) { |
| 782 | RTC_DLOG(LS_ERROR) << "GetRemoteRTCPReportBlock()s invalid report_blocks."; |
| 783 | return -1; |
| 784 | } |
| 785 | |
| 786 | // Get the report blocks from the latest received RTCP Sender or Receiver |
| 787 | // Report. Each element in the vector contains the sender's SSRC and a |
| 788 | // report block according to RFC 3550. |
| 789 | std::vector<RTCPReportBlock> rtcp_report_blocks; |
| 790 | if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) { |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | if (rtcp_report_blocks.empty()) |
| 795 | return 0; |
| 796 | |
| 797 | std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin(); |
| 798 | for (; it != rtcp_report_blocks.end(); ++it) { |
| 799 | ReportBlock report_block; |
| 800 | report_block.sender_SSRC = it->sender_ssrc; |
| 801 | report_block.source_SSRC = it->source_ssrc; |
| 802 | report_block.fraction_lost = it->fraction_lost; |
| 803 | report_block.cumulative_num_packets_lost = it->packets_lost; |
| 804 | report_block.extended_highest_sequence_number = |
| 805 | it->extended_highest_sequence_number; |
| 806 | report_block.interarrival_jitter = it->jitter; |
| 807 | report_block.last_SR_timestamp = it->last_sender_report_timestamp; |
| 808 | report_block.delay_since_last_SR = it->delay_since_last_sender_report; |
| 809 | report_blocks->push_back(report_block); |
| 810 | } |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | int ChannelSend::GetRTPStatistics(CallSendStatistics& stats) { |
| 815 | // --- RtcpStatistics |
| 816 | |
| 817 | // --- RTT |
| 818 | stats.rttMs = GetRTT(); |
| 819 | |
| 820 | // --- Data counters |
| 821 | |
| 822 | size_t bytesSent(0); |
| 823 | uint32_t packetsSent(0); |
| 824 | |
| 825 | if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) { |
| 826 | RTC_DLOG(LS_WARNING) |
| 827 | << "GetRTPStatistics() failed to retrieve RTP datacounters" |
| 828 | << " => output will not be complete"; |
| 829 | } |
| 830 | |
| 831 | stats.bytesSent = bytesSent; |
| 832 | stats.packetsSent = packetsSent; |
| 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | void ChannelSend::SetNACKStatus(bool enable, int maxNumberOfPackets) { |
| 838 | // None of these functions can fail. |
| 839 | if (enable) |
| 840 | audio_coding_->EnableNack(maxNumberOfPackets); |
| 841 | else |
| 842 | audio_coding_->DisableNack(); |
| 843 | } |
| 844 | |
| 845 | // Called when we are missing one or more packets. |
| 846 | int ChannelSend::ResendPackets(const uint16_t* sequence_numbers, int length) { |
| 847 | return _rtpRtcpModule->SendNACK(sequence_numbers, length); |
| 848 | } |
| 849 | |
| 850 | void ChannelSend::ProcessAndEncodeAudio( |
| 851 | std::unique_ptr<AudioFrame> audio_frame) { |
| 852 | // Avoid posting any new tasks if sending was already stopped in StopSend(). |
| 853 | rtc::CritScope cs(&encoder_queue_lock_); |
| 854 | if (!encoder_queue_is_active_) { |
| 855 | return; |
| 856 | } |
| 857 | // Profile time between when the audio frame is added to the task queue and |
| 858 | // when the task is actually executed. |
| 859 | audio_frame->UpdateProfileTimeStamp(); |
| 860 | encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>( |
| 861 | new ProcessAndEncodeAudioTask(std::move(audio_frame), this))); |
| 862 | } |
| 863 | |
| 864 | void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) { |
| 865 | RTC_DCHECK_RUN_ON(encoder_queue_); |
| 866 | RTC_DCHECK_GT(audio_input->samples_per_channel_, 0); |
| 867 | RTC_DCHECK_LE(audio_input->num_channels_, 2); |
| 868 | |
| 869 | // Measure time between when the audio frame is added to the task queue and |
| 870 | // when the task is actually executed. Goal is to keep track of unwanted |
| 871 | // extra latency added by the task queue. |
| 872 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs", |
| 873 | audio_input->ElapsedProfileTimeMs()); |
| 874 | |
| 875 | bool is_muted = InputMute(); |
| 876 | AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted); |
| 877 | |
| 878 | if (_includeAudioLevelIndication) { |
| 879 | size_t length = |
| 880 | audio_input->samples_per_channel_ * audio_input->num_channels_; |
| 881 | RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes); |
| 882 | if (is_muted && previous_frame_muted_) { |
| 883 | rms_level_.AnalyzeMuted(length); |
| 884 | } else { |
| 885 | rms_level_.Analyze( |
| 886 | rtc::ArrayView<const int16_t>(audio_input->data(), length)); |
| 887 | } |
| 888 | } |
| 889 | previous_frame_muted_ = is_muted; |
| 890 | |
| 891 | // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz. |
| 892 | |
| 893 | // The ACM resamples internally. |
| 894 | audio_input->timestamp_ = _timeStamp; |
| 895 | // This call will trigger AudioPacketizationCallback::SendData if encoding |
| 896 | // is done and payload is ready for packetization and transmission. |
| 897 | // Otherwise, it will return without invoking the callback. |
| 898 | if (audio_coding_->Add10MsData(*audio_input) < 0) { |
| 899 | RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed."; |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_); |
| 904 | } |
| 905 | |
| 906 | void ChannelSend::UpdateOverheadForEncoder() { |
| 907 | size_t overhead_per_packet = |
| 908 | transport_overhead_per_packet_ + rtp_overhead_per_packet_; |
| 909 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 910 | if (*encoder) { |
| 911 | (*encoder)->OnReceivedOverhead(overhead_per_packet); |
| 912 | } |
| 913 | }); |
| 914 | } |
| 915 | |
| 916 | void ChannelSend::SetTransportOverhead(size_t transport_overhead_per_packet) { |
| 917 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 918 | transport_overhead_per_packet_ = transport_overhead_per_packet; |
| 919 | UpdateOverheadForEncoder(); |
| 920 | } |
| 921 | |
| 922 | // TODO(solenberg): Make AudioSendStream an OverheadObserver instead. |
| 923 | void ChannelSend::OnOverheadChanged(size_t overhead_bytes_per_packet) { |
| 924 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 925 | rtp_overhead_per_packet_ = overhead_bytes_per_packet; |
| 926 | UpdateOverheadForEncoder(); |
| 927 | } |
| 928 | |
| 929 | ANAStats ChannelSend::GetANAStatistics() const { |
| 930 | return audio_coding_->GetANAStats(); |
| 931 | } |
| 932 | |
| 933 | RtpRtcp* ChannelSend::GetRtpRtcp() const { |
| 934 | return _rtpRtcpModule.get(); |
| 935 | } |
| 936 | |
| 937 | int ChannelSend::SetSendRtpHeaderExtension(bool enable, |
| 938 | RTPExtensionType type, |
| 939 | unsigned char id) { |
| 940 | int error = 0; |
| 941 | _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type); |
| 942 | if (enable) { |
| 943 | error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id); |
| 944 | } |
| 945 | return error; |
| 946 | } |
| 947 | |
| 948 | int ChannelSend::GetRtpTimestampRateHz() const { |
| 949 | const auto format = audio_coding_->ReceiveFormat(); |
| 950 | // Default to the playout frequency if we've not gotten any packets yet. |
| 951 | // TODO(ossu): Zero clockrate can only happen if we've added an external |
| 952 | // decoder for a format we don't support internally. Remove once that way of |
| 953 | // adding decoders is gone! |
| 954 | return (format && format->clockrate_hz != 0) |
| 955 | ? format->clockrate_hz |
| 956 | : audio_coding_->PlayoutFrequency(); |
| 957 | } |
| 958 | |
| 959 | int64_t ChannelSend::GetRTT() const { |
| 960 | RtcpMode method = _rtpRtcpModule->RTCP(); |
| 961 | if (method == RtcpMode::kOff) { |
| 962 | return 0; |
| 963 | } |
| 964 | std::vector<RTCPReportBlock> report_blocks; |
| 965 | _rtpRtcpModule->RemoteRTCPStat(&report_blocks); |
| 966 | |
| 967 | if (report_blocks.empty()) { |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | int64_t rtt = 0; |
| 972 | int64_t avg_rtt = 0; |
| 973 | int64_t max_rtt = 0; |
| 974 | int64_t min_rtt = 0; |
| 975 | // We don't know in advance the remote ssrc used by the other end's receiver |
| 976 | // reports, so use the SSRC of the first report block for calculating the RTT. |
| 977 | if (_rtpRtcpModule->RTT(report_blocks[0].sender_ssrc, &rtt, &avg_rtt, |
| 978 | &min_rtt, &max_rtt) != 0) { |
| 979 | return 0; |
| 980 | } |
| 981 | return rtt; |
| 982 | } |
| 983 | |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame^] | 984 | void ChannelSend::SetFrameEncryptor(FrameEncryptorInterface* frame_encryptor) { |
| 985 | rtc::CritScope cs(&encoder_queue_lock_); |
| 986 | if (encoder_queue_is_active_) { |
| 987 | encoder_queue_->PostTask([this, frame_encryptor]() { |
| 988 | this->frame_encryptor_ = frame_encryptor; |
| 989 | }); |
| 990 | } else { |
| 991 | frame_encryptor_ = frame_encryptor; |
| 992 | } |
| 993 | } |
| 994 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 995 | } // namespace voe |
| 996 | } // namespace webrtc |