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