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_receive.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 "audio/channel_send.h" |
| 22 | #include "audio/utility/audio_frame_operations.h" |
| 23 | #include "logging/rtc_event_log/events/rtc_event_audio_playout.h" |
| 24 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 25 | #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h" |
| 26 | #include "modules/audio_device/include/audio_device.h" |
| 27 | #include "modules/pacing/packet_router.h" |
| 28 | #include "modules/rtp_rtcp/include/receive_statistics.h" |
| 29 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 30 | #include "modules/utility/include/process_thread.h" |
| 31 | #include "rtc_base/checks.h" |
| 32 | #include "rtc_base/criticalsection.h" |
| 33 | #include "rtc_base/format_macros.h" |
| 34 | #include "rtc_base/location.h" |
| 35 | #include "rtc_base/logging.h" |
| 36 | #include "rtc_base/thread_checker.h" |
| 37 | #include "rtc_base/timeutils.h" |
| 38 | #include "system_wrappers/include/metrics.h" |
| 39 | |
| 40 | namespace webrtc { |
| 41 | namespace voe { |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | constexpr double kAudioSampleDurationSeconds = 0.01; |
| 46 | constexpr int64_t kMaxRetransmissionWindowMs = 1000; |
| 47 | constexpr int64_t kMinRetransmissionWindowMs = 30; |
| 48 | |
| 49 | // Video Sync. |
| 50 | constexpr int kVoiceEngineMinMinPlayoutDelayMs = 0; |
| 51 | constexpr int kVoiceEngineMaxMinPlayoutDelayMs = 10000; |
| 52 | |
| 53 | } // namespace |
| 54 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 55 | int32_t ChannelReceive::OnReceivedPayloadData( |
| 56 | const uint8_t* payloadData, |
| 57 | size_t payloadSize, |
| 58 | const WebRtcRTPHeader* rtpHeader) { |
| 59 | if (!channel_state_.Get().playing) { |
| 60 | // Avoid inserting into NetEQ when we are not playing. Count the |
| 61 | // packet as discarded. |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | // Push the incoming payload (parsed and ready for decoding) into the ACM |
| 66 | if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) != |
| 67 | 0) { |
| 68 | RTC_DLOG(LS_ERROR) << "ChannelReceive::OnReceivedPayloadData() unable to " |
| 69 | "push data to the ACM"; |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | int64_t round_trip_time = 0; |
| 74 | _rtpRtcpModule->RTT(remote_ssrc_, &round_trip_time, NULL, NULL, NULL); |
| 75 | |
| 76 | std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time); |
| 77 | if (!nack_list.empty()) { |
| 78 | // Can't use nack_list.data() since it's not supported by all |
| 79 | // compilers. |
| 80 | ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size())); |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | AudioMixer::Source::AudioFrameInfo ChannelReceive::GetAudioFrameWithInfo( |
| 86 | int sample_rate_hz, |
| 87 | AudioFrame* audio_frame) { |
| 88 | audio_frame->sample_rate_hz_ = sample_rate_hz; |
| 89 | |
| 90 | unsigned int ssrc; |
| 91 | RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0); |
| 92 | event_log_->Log(absl::make_unique<RtcEventAudioPlayout>(ssrc)); |
| 93 | // Get 10ms raw PCM data from the ACM (mixer limits output frequency) |
| 94 | bool muted; |
| 95 | if (audio_coding_->PlayoutData10Ms(audio_frame->sample_rate_hz_, audio_frame, |
| 96 | &muted) == -1) { |
| 97 | RTC_DLOG(LS_ERROR) |
| 98 | << "ChannelReceive::GetAudioFrame() PlayoutData10Ms() failed!"; |
| 99 | // In all likelihood, the audio in this frame is garbage. We return an |
| 100 | // error so that the audio mixer module doesn't add it to the mix. As |
| 101 | // a result, it won't be played out and the actions skipped here are |
| 102 | // irrelevant. |
| 103 | return AudioMixer::Source::AudioFrameInfo::kError; |
| 104 | } |
| 105 | |
| 106 | if (muted) { |
| 107 | // TODO(henrik.lundin): We should be able to do better than this. But we |
| 108 | // will have to go through all the cases below where the audio samples may |
| 109 | // be used, and handle the muted case in some way. |
| 110 | AudioFrameOperations::Mute(audio_frame); |
| 111 | } |
| 112 | |
| 113 | { |
| 114 | // Pass the audio buffers to an optional sink callback, before applying |
| 115 | // scaling/panning, as that applies to the mix operation. |
| 116 | // External recipients of the audio (e.g. via AudioTrack), will do their |
| 117 | // own mixing/dynamic processing. |
| 118 | rtc::CritScope cs(&_callbackCritSect); |
| 119 | if (audio_sink_) { |
| 120 | AudioSinkInterface::Data data( |
| 121 | audio_frame->data(), audio_frame->samples_per_channel_, |
| 122 | audio_frame->sample_rate_hz_, audio_frame->num_channels_, |
| 123 | audio_frame->timestamp_); |
| 124 | audio_sink_->OnData(data); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | float output_gain = 1.0f; |
| 129 | { |
| 130 | rtc::CritScope cs(&volume_settings_critsect_); |
| 131 | output_gain = _outputGain; |
| 132 | } |
| 133 | |
| 134 | // Output volume scaling |
| 135 | if (output_gain < 0.99f || output_gain > 1.01f) { |
| 136 | // TODO(solenberg): Combine with mute state - this can cause clicks! |
| 137 | AudioFrameOperations::ScaleWithSat(output_gain, audio_frame); |
| 138 | } |
| 139 | |
| 140 | // Measure audio level (0-9) |
| 141 | // TODO(henrik.lundin) Use the |muted| information here too. |
| 142 | // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see |
| 143 | // https://crbug.com/webrtc/7517). |
| 144 | _outputAudioLevel.ComputeLevel(*audio_frame, kAudioSampleDurationSeconds); |
| 145 | |
| 146 | if (capture_start_rtp_time_stamp_ < 0 && audio_frame->timestamp_ != 0) { |
| 147 | // The first frame with a valid rtp timestamp. |
| 148 | capture_start_rtp_time_stamp_ = audio_frame->timestamp_; |
| 149 | } |
| 150 | |
| 151 | if (capture_start_rtp_time_stamp_ >= 0) { |
| 152 | // audio_frame.timestamp_ should be valid from now on. |
| 153 | |
| 154 | // Compute elapsed time. |
| 155 | int64_t unwrap_timestamp = |
| 156 | rtp_ts_wraparound_handler_->Unwrap(audio_frame->timestamp_); |
| 157 | audio_frame->elapsed_time_ms_ = |
| 158 | (unwrap_timestamp - capture_start_rtp_time_stamp_) / |
| 159 | (GetRtpTimestampRateHz() / 1000); |
| 160 | |
| 161 | { |
| 162 | rtc::CritScope lock(&ts_stats_lock_); |
| 163 | // Compute ntp time. |
| 164 | audio_frame->ntp_time_ms_ = |
| 165 | ntp_estimator_.Estimate(audio_frame->timestamp_); |
| 166 | // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received. |
| 167 | if (audio_frame->ntp_time_ms_ > 0) { |
| 168 | // Compute |capture_start_ntp_time_ms_| so that |
| 169 | // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_| |
| 170 | capture_start_ntp_time_ms_ = |
| 171 | audio_frame->ntp_time_ms_ - audio_frame->elapsed_time_ms_; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | { |
| 177 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.TargetJitterBufferDelayMs", |
| 178 | audio_coding_->TargetDelayMs()); |
| 179 | const int jitter_buffer_delay = audio_coding_->FilteredCurrentDelayMs(); |
| 180 | rtc::CritScope lock(&video_sync_lock_); |
| 181 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDelayEstimateMs", |
| 182 | jitter_buffer_delay + playout_delay_ms_); |
| 183 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverJitterBufferDelayMs", |
| 184 | jitter_buffer_delay); |
| 185 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDeviceDelayMs", |
| 186 | playout_delay_ms_); |
| 187 | } |
| 188 | |
| 189 | return muted ? AudioMixer::Source::AudioFrameInfo::kMuted |
| 190 | : AudioMixer::Source::AudioFrameInfo::kNormal; |
| 191 | } |
| 192 | |
| 193 | int ChannelReceive::PreferredSampleRate() const { |
| 194 | // Return the bigger of playout and receive frequency in the ACM. |
| 195 | return std::max(audio_coding_->ReceiveFrequency(), |
| 196 | audio_coding_->PlayoutFrequency()); |
| 197 | } |
| 198 | |
| 199 | ChannelReceive::ChannelReceive( |
| 200 | ProcessThread* module_process_thread, |
| 201 | AudioDeviceModule* audio_device_module, |
Niels Möller | ae4237e | 2018-10-05 11:28:38 +0200 | [diff] [blame^] | 202 | Transport* rtcp_send_transport, |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 203 | RtcEventLog* rtc_event_log, |
| 204 | uint32_t remote_ssrc, |
| 205 | size_t jitter_buffer_max_packets, |
| 206 | bool jitter_buffer_fast_playout, |
| 207 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 208 | absl::optional<AudioCodecPairId> codec_pair_id, |
| 209 | FrameDecryptorInterface* frame_decryptor) |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 210 | : event_log_(rtc_event_log), |
| 211 | rtp_receive_statistics_( |
| 212 | ReceiveStatistics::Create(Clock::GetRealTimeClock())), |
| 213 | remote_ssrc_(remote_ssrc), |
| 214 | _outputAudioLevel(), |
| 215 | ntp_estimator_(Clock::GetRealTimeClock()), |
| 216 | playout_timestamp_rtp_(0), |
| 217 | playout_delay_ms_(0), |
| 218 | rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()), |
| 219 | capture_start_rtp_time_stamp_(-1), |
| 220 | capture_start_ntp_time_ms_(-1), |
| 221 | _moduleProcessThreadPtr(module_process_thread), |
| 222 | _audioDeviceModulePtr(audio_device_module), |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 223 | _outputGain(1.0f), |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 224 | associated_send_channel_(nullptr), |
| 225 | frame_decryptor_(frame_decryptor) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 226 | RTC_DCHECK(module_process_thread); |
| 227 | RTC_DCHECK(audio_device_module); |
| 228 | AudioCodingModule::Config acm_config; |
| 229 | acm_config.decoder_factory = decoder_factory; |
| 230 | acm_config.neteq_config.codec_pair_id = codec_pair_id; |
| 231 | acm_config.neteq_config.max_packets_in_buffer = jitter_buffer_max_packets; |
| 232 | acm_config.neteq_config.enable_fast_accelerate = jitter_buffer_fast_playout; |
| 233 | acm_config.neteq_config.enable_muted_state = true; |
| 234 | audio_coding_.reset(AudioCodingModule::Create(acm_config)); |
| 235 | |
| 236 | _outputAudioLevel.Clear(); |
| 237 | |
| 238 | rtp_receive_statistics_->EnableRetransmitDetection(remote_ssrc_, true); |
| 239 | RtpRtcp::Configuration configuration; |
| 240 | configuration.audio = true; |
Niels Möller | ae4237e | 2018-10-05 11:28:38 +0200 | [diff] [blame^] | 241 | // TODO(nisse): Also set receiver_only = true, but that seems to break RTT |
| 242 | // estimation, resulting in test failures for |
| 243 | // PeerConnectionIntegrationTest.GetCaptureStartNtpTimeWithOldStatsApi |
| 244 | configuration.outgoing_transport = rtcp_send_transport; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 245 | configuration.receive_statistics = rtp_receive_statistics_.get(); |
| 246 | |
| 247 | configuration.event_log = event_log_; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 248 | |
| 249 | _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration)); |
| 250 | _rtpRtcpModule->SetSendingMediaStatus(false); |
| 251 | _rtpRtcpModule->SetRemoteSSRC(remote_ssrc_); |
| 252 | Init(); |
| 253 | } |
| 254 | |
| 255 | ChannelReceive::~ChannelReceive() { |
| 256 | Terminate(); |
| 257 | RTC_DCHECK(!channel_state_.Get().playing); |
| 258 | } |
| 259 | |
| 260 | void ChannelReceive::Init() { |
| 261 | channel_state_.Reset(); |
| 262 | |
| 263 | // --- Add modules to process thread (for periodic schedulation) |
| 264 | _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE); |
| 265 | |
| 266 | // --- ACM initialization |
| 267 | int error = audio_coding_->InitializeReceiver(); |
| 268 | RTC_DCHECK_EQ(0, error); |
| 269 | |
| 270 | // --- RTP/RTCP module initialization |
| 271 | |
| 272 | // Ensure that RTCP is enabled by default for the created channel. |
| 273 | // Note that, the module will keep generating RTCP until it is explicitly |
| 274 | // disabled by the user. |
| 275 | // After StopListen (when no sockets exists), RTCP packets will no longer |
| 276 | // be transmitted since the Transport object will then be invalid. |
| 277 | // RTCP is enabled by default. |
| 278 | _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound); |
| 279 | } |
| 280 | |
| 281 | void ChannelReceive::Terminate() { |
| 282 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
| 283 | // Must be called on the same thread as Init(). |
| 284 | rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL); |
| 285 | |
| 286 | StopPlayout(); |
| 287 | |
| 288 | // The order to safely shutdown modules in a channel is: |
| 289 | // 1. De-register callbacks in modules |
| 290 | // 2. De-register modules in process thread |
| 291 | // 3. Destroy modules |
| 292 | int error = audio_coding_->RegisterTransportCallback(NULL); |
| 293 | RTC_DCHECK_EQ(0, error); |
| 294 | |
| 295 | // De-register modules in process thread |
| 296 | if (_moduleProcessThreadPtr) |
| 297 | _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()); |
| 298 | |
| 299 | // End of modules shutdown |
| 300 | } |
| 301 | |
| 302 | void ChannelReceive::SetSink(AudioSinkInterface* sink) { |
| 303 | rtc::CritScope cs(&_callbackCritSect); |
| 304 | audio_sink_ = sink; |
| 305 | } |
| 306 | |
| 307 | int32_t ChannelReceive::StartPlayout() { |
| 308 | if (channel_state_.Get().playing) { |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | channel_state_.SetPlaying(true); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | int32_t ChannelReceive::StopPlayout() { |
| 318 | if (!channel_state_.Get().playing) { |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | channel_state_.SetPlaying(false); |
| 323 | _outputAudioLevel.Clear(); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | int32_t ChannelReceive::GetRecCodec(CodecInst& codec) { |
| 329 | return (audio_coding_->ReceiveCodec(&codec)); |
| 330 | } |
| 331 | |
| 332 | std::vector<webrtc::RtpSource> ChannelReceive::GetSources() const { |
| 333 | int64_t now_ms = rtc::TimeMillis(); |
| 334 | std::vector<RtpSource> sources; |
| 335 | { |
| 336 | rtc::CritScope cs(&rtp_sources_lock_); |
| 337 | sources = contributing_sources_.GetSources(now_ms); |
| 338 | if (last_received_rtp_system_time_ms_ >= |
| 339 | now_ms - ContributingSources::kHistoryMs) { |
| 340 | sources.emplace_back(*last_received_rtp_system_time_ms_, remote_ssrc_, |
| 341 | RtpSourceType::SSRC); |
| 342 | sources.back().set_audio_level(last_received_rtp_audio_level_); |
| 343 | } |
| 344 | } |
| 345 | return sources; |
| 346 | } |
| 347 | |
| 348 | void ChannelReceive::SetReceiveCodecs( |
| 349 | const std::map<int, SdpAudioFormat>& codecs) { |
| 350 | for (const auto& kv : codecs) { |
| 351 | RTC_DCHECK_GE(kv.second.clockrate_hz, 1000); |
| 352 | payload_type_frequencies_[kv.first] = kv.second.clockrate_hz; |
| 353 | } |
| 354 | audio_coding_->SetReceiveCodecs(codecs); |
| 355 | } |
| 356 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 357 | // TODO(nisse): Move receive logic up to AudioReceiveStream. |
| 358 | void ChannelReceive::OnRtpPacket(const RtpPacketReceived& packet) { |
| 359 | int64_t now_ms = rtc::TimeMillis(); |
| 360 | uint8_t audio_level; |
| 361 | bool voice_activity; |
| 362 | bool has_audio_level = |
| 363 | packet.GetExtension<::webrtc::AudioLevel>(&voice_activity, &audio_level); |
| 364 | |
| 365 | { |
| 366 | rtc::CritScope cs(&rtp_sources_lock_); |
| 367 | last_received_rtp_timestamp_ = packet.Timestamp(); |
| 368 | last_received_rtp_system_time_ms_ = now_ms; |
| 369 | if (has_audio_level) |
| 370 | last_received_rtp_audio_level_ = audio_level; |
| 371 | std::vector<uint32_t> csrcs = packet.Csrcs(); |
| 372 | contributing_sources_.Update(now_ms, csrcs); |
| 373 | } |
| 374 | |
| 375 | // Store playout timestamp for the received RTP packet |
| 376 | UpdatePlayoutTimestamp(false); |
| 377 | |
| 378 | const auto& it = payload_type_frequencies_.find(packet.PayloadType()); |
| 379 | if (it == payload_type_frequencies_.end()) |
| 380 | return; |
| 381 | // TODO(nisse): Set payload_type_frequency earlier, when packet is parsed. |
| 382 | RtpPacketReceived packet_copy(packet); |
| 383 | packet_copy.set_payload_type_frequency(it->second); |
| 384 | |
| 385 | rtp_receive_statistics_->OnRtpPacket(packet_copy); |
| 386 | |
| 387 | RTPHeader header; |
| 388 | packet_copy.GetHeader(&header); |
| 389 | |
| 390 | ReceivePacket(packet_copy.data(), packet_copy.size(), header); |
| 391 | } |
| 392 | |
| 393 | bool ChannelReceive::ReceivePacket(const uint8_t* packet, |
| 394 | size_t packet_length, |
| 395 | const RTPHeader& header) { |
| 396 | const uint8_t* payload = packet + header.headerLength; |
| 397 | assert(packet_length >= header.headerLength); |
| 398 | size_t payload_length = packet_length - header.headerLength; |
| 399 | WebRtcRTPHeader webrtc_rtp_header = {}; |
| 400 | webrtc_rtp_header.header = header; |
| 401 | |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 402 | size_t payload_data_length = payload_length - header.paddingLength; |
| 403 | |
| 404 | // E2EE Custom Audio Frame Decryption (This is optional). |
| 405 | // Keep this buffer around for the lifetime of the OnReceivedPayloadData call. |
| 406 | rtc::Buffer decrypted_audio_payload; |
| 407 | if (frame_decryptor_ != nullptr) { |
| 408 | size_t max_plaintext_size = frame_decryptor_->GetMaxPlaintextByteSize( |
| 409 | cricket::MEDIA_TYPE_AUDIO, payload_length); |
| 410 | decrypted_audio_payload.SetSize(max_plaintext_size); |
| 411 | |
| 412 | size_t bytes_written = 0; |
| 413 | std::vector<uint32_t> csrcs(header.arrOfCSRCs, |
| 414 | header.arrOfCSRCs + header.numCSRCs); |
| 415 | int decrypt_status = frame_decryptor_->Decrypt( |
| 416 | cricket::MEDIA_TYPE_AUDIO, csrcs, |
| 417 | /*additional_data=*/nullptr, |
| 418 | rtc::ArrayView<const uint8_t>(payload, payload_data_length), |
| 419 | decrypted_audio_payload, &bytes_written); |
| 420 | |
| 421 | // In this case just interpret the failure as a silent frame. |
| 422 | if (decrypt_status != 0) { |
| 423 | bytes_written = 0; |
| 424 | } |
| 425 | |
| 426 | // Resize the decrypted audio payload to the number of bytes actually |
| 427 | // written. |
| 428 | decrypted_audio_payload.SetSize(bytes_written); |
| 429 | // Update the final payload. |
| 430 | payload = decrypted_audio_payload.data(); |
| 431 | payload_data_length = decrypted_audio_payload.size(); |
| 432 | } |
| 433 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 434 | if (payload_data_length == 0) { |
| 435 | webrtc_rtp_header.frameType = kEmptyFrame; |
| 436 | return OnReceivedPayloadData(nullptr, 0, &webrtc_rtp_header); |
| 437 | } |
| 438 | return OnReceivedPayloadData(payload, payload_data_length, |
| 439 | &webrtc_rtp_header); |
| 440 | } |
| 441 | |
| 442 | int32_t ChannelReceive::ReceivedRTCPPacket(const uint8_t* data, size_t length) { |
| 443 | // Store playout timestamp for the received RTCP packet |
| 444 | UpdatePlayoutTimestamp(true); |
| 445 | |
| 446 | // Deliver RTCP packet to RTP/RTCP module for parsing |
| 447 | _rtpRtcpModule->IncomingRtcpPacket(data, length); |
| 448 | |
| 449 | int64_t rtt = GetRTT(); |
| 450 | if (rtt == 0) { |
| 451 | // Waiting for valid RTT. |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | int64_t nack_window_ms = rtt; |
| 456 | if (nack_window_ms < kMinRetransmissionWindowMs) { |
| 457 | nack_window_ms = kMinRetransmissionWindowMs; |
| 458 | } else if (nack_window_ms > kMaxRetransmissionWindowMs) { |
| 459 | nack_window_ms = kMaxRetransmissionWindowMs; |
| 460 | } |
| 461 | |
| 462 | uint32_t ntp_secs = 0; |
| 463 | uint32_t ntp_frac = 0; |
| 464 | uint32_t rtp_timestamp = 0; |
| 465 | if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL, |
| 466 | &rtp_timestamp)) { |
| 467 | // Waiting for RTCP. |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | { |
| 472 | rtc::CritScope lock(&ts_stats_lock_); |
| 473 | ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp); |
| 474 | } |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | int ChannelReceive::GetSpeechOutputLevelFullRange() const { |
| 479 | return _outputAudioLevel.LevelFullRange(); |
| 480 | } |
| 481 | |
| 482 | double ChannelReceive::GetTotalOutputEnergy() const { |
| 483 | return _outputAudioLevel.TotalEnergy(); |
| 484 | } |
| 485 | |
| 486 | double ChannelReceive::GetTotalOutputDuration() const { |
| 487 | return _outputAudioLevel.TotalDuration(); |
| 488 | } |
| 489 | |
| 490 | void ChannelReceive::SetChannelOutputVolumeScaling(float scaling) { |
| 491 | rtc::CritScope cs(&volume_settings_critsect_); |
| 492 | _outputGain = scaling; |
| 493 | } |
| 494 | |
| 495 | int ChannelReceive::SetLocalSSRC(unsigned int ssrc) { |
| 496 | _rtpRtcpModule->SetSSRC(ssrc); |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | // TODO(nisse): Pass ssrc in return value instead. |
| 501 | int ChannelReceive::GetRemoteSSRC(unsigned int& ssrc) { |
| 502 | ssrc = remote_ssrc_; |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | void ChannelReceive::RegisterReceiverCongestionControlObjects( |
| 507 | PacketRouter* packet_router) { |
| 508 | RTC_DCHECK(packet_router); |
| 509 | RTC_DCHECK(!packet_router_); |
| 510 | constexpr bool remb_candidate = false; |
| 511 | packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate); |
| 512 | packet_router_ = packet_router; |
| 513 | } |
| 514 | |
| 515 | void ChannelReceive::ResetReceiverCongestionControlObjects() { |
| 516 | RTC_DCHECK(packet_router_); |
| 517 | packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get()); |
| 518 | packet_router_ = nullptr; |
| 519 | } |
| 520 | |
| 521 | int ChannelReceive::GetRTPStatistics(CallReceiveStatistics& stats) { |
| 522 | // --- RtcpStatistics |
| 523 | |
| 524 | // The jitter statistics is updated for each received RTP packet and is |
| 525 | // based on received packets. |
| 526 | RtcpStatistics statistics; |
| 527 | StreamStatistician* statistician = |
| 528 | rtp_receive_statistics_->GetStatistician(remote_ssrc_); |
| 529 | if (statistician) { |
| 530 | statistician->GetStatistics(&statistics, |
| 531 | _rtpRtcpModule->RTCP() == RtcpMode::kOff); |
| 532 | } |
| 533 | |
| 534 | stats.fractionLost = statistics.fraction_lost; |
| 535 | stats.cumulativeLost = statistics.packets_lost; |
| 536 | stats.extendedMax = statistics.extended_highest_sequence_number; |
| 537 | stats.jitterSamples = statistics.jitter; |
| 538 | |
| 539 | // --- RTT |
| 540 | stats.rttMs = GetRTT(); |
| 541 | |
| 542 | // --- Data counters |
| 543 | |
| 544 | size_t bytesReceived(0); |
| 545 | uint32_t packetsReceived(0); |
| 546 | |
| 547 | if (statistician) { |
| 548 | statistician->GetDataCounters(&bytesReceived, &packetsReceived); |
| 549 | } |
| 550 | |
| 551 | stats.bytesReceived = bytesReceived; |
| 552 | stats.packetsReceived = packetsReceived; |
| 553 | |
| 554 | // --- Timestamps |
| 555 | { |
| 556 | rtc::CritScope lock(&ts_stats_lock_); |
| 557 | stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_; |
| 558 | } |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | void ChannelReceive::SetNACKStatus(bool enable, int maxNumberOfPackets) { |
| 563 | // None of these functions can fail. |
| 564 | rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets); |
| 565 | if (enable) |
| 566 | audio_coding_->EnableNack(maxNumberOfPackets); |
| 567 | else |
| 568 | audio_coding_->DisableNack(); |
| 569 | } |
| 570 | |
| 571 | // Called when we are missing one or more packets. |
| 572 | int ChannelReceive::ResendPackets(const uint16_t* sequence_numbers, |
| 573 | int length) { |
| 574 | return _rtpRtcpModule->SendNACK(sequence_numbers, length); |
| 575 | } |
| 576 | |
| 577 | void ChannelReceive::SetAssociatedSendChannel(ChannelSend* channel) { |
| 578 | rtc::CritScope lock(&assoc_send_channel_lock_); |
| 579 | associated_send_channel_ = channel; |
| 580 | } |
| 581 | |
| 582 | int ChannelReceive::GetNetworkStatistics(NetworkStatistics& stats) { |
| 583 | return audio_coding_->GetNetworkStatistics(&stats); |
| 584 | } |
| 585 | |
| 586 | void ChannelReceive::GetDecodingCallStatistics( |
| 587 | AudioDecodingCallStats* stats) const { |
| 588 | audio_coding_->GetDecodingCallStatistics(stats); |
| 589 | } |
| 590 | |
| 591 | uint32_t ChannelReceive::GetDelayEstimate() const { |
| 592 | rtc::CritScope lock(&video_sync_lock_); |
| 593 | return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_; |
| 594 | } |
| 595 | |
| 596 | int ChannelReceive::SetMinimumPlayoutDelay(int delayMs) { |
| 597 | if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) || |
| 598 | (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) { |
| 599 | RTC_DLOG(LS_ERROR) << "SetMinimumPlayoutDelay() invalid min delay"; |
| 600 | return -1; |
| 601 | } |
| 602 | if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) { |
| 603 | RTC_DLOG(LS_ERROR) |
| 604 | << "SetMinimumPlayoutDelay() failed to set min playout delay"; |
| 605 | return -1; |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | int ChannelReceive::GetPlayoutTimestamp(unsigned int& timestamp) { |
| 611 | uint32_t playout_timestamp_rtp = 0; |
| 612 | { |
| 613 | rtc::CritScope lock(&video_sync_lock_); |
| 614 | playout_timestamp_rtp = playout_timestamp_rtp_; |
| 615 | } |
| 616 | if (playout_timestamp_rtp == 0) { |
| 617 | RTC_DLOG(LS_ERROR) << "GetPlayoutTimestamp() failed to retrieve timestamp"; |
| 618 | return -1; |
| 619 | } |
| 620 | timestamp = playout_timestamp_rtp; |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | absl::optional<Syncable::Info> ChannelReceive::GetSyncInfo() const { |
| 625 | Syncable::Info info; |
| 626 | if (_rtpRtcpModule->RemoteNTP(&info.capture_time_ntp_secs, |
| 627 | &info.capture_time_ntp_frac, nullptr, nullptr, |
| 628 | &info.capture_time_source_clock) != 0) { |
| 629 | return absl::nullopt; |
| 630 | } |
| 631 | { |
| 632 | rtc::CritScope cs(&rtp_sources_lock_); |
| 633 | if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) { |
| 634 | return absl::nullopt; |
| 635 | } |
| 636 | info.latest_received_capture_timestamp = *last_received_rtp_timestamp_; |
| 637 | info.latest_receive_time_ms = *last_received_rtp_system_time_ms_; |
| 638 | } |
| 639 | return info; |
| 640 | } |
| 641 | |
| 642 | void ChannelReceive::UpdatePlayoutTimestamp(bool rtcp) { |
| 643 | jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp(); |
| 644 | |
| 645 | if (!jitter_buffer_playout_timestamp_) { |
| 646 | // This can happen if this channel has not received any RTP packets. In |
| 647 | // this case, NetEq is not capable of computing a playout timestamp. |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | uint16_t delay_ms = 0; |
| 652 | if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) { |
| 653 | RTC_DLOG(LS_WARNING) |
| 654 | << "ChannelReceive::UpdatePlayoutTimestamp() failed to read" |
| 655 | << " playout delay from the ADM"; |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | RTC_DCHECK(jitter_buffer_playout_timestamp_); |
| 660 | uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_; |
| 661 | |
| 662 | // Remove the playout delay. |
| 663 | playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000)); |
| 664 | |
| 665 | { |
| 666 | rtc::CritScope lock(&video_sync_lock_); |
| 667 | if (!rtcp) { |
| 668 | playout_timestamp_rtp_ = playout_timestamp; |
| 669 | } |
| 670 | playout_delay_ms_ = delay_ms; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | int ChannelReceive::GetRtpTimestampRateHz() const { |
| 675 | const auto format = audio_coding_->ReceiveFormat(); |
| 676 | // Default to the playout frequency if we've not gotten any packets yet. |
| 677 | // TODO(ossu): Zero clockrate can only happen if we've added an external |
| 678 | // decoder for a format we don't support internally. Remove once that way of |
| 679 | // adding decoders is gone! |
| 680 | return (format && format->clockrate_hz != 0) |
| 681 | ? format->clockrate_hz |
| 682 | : audio_coding_->PlayoutFrequency(); |
| 683 | } |
| 684 | |
| 685 | int64_t ChannelReceive::GetRTT() const { |
| 686 | RtcpMode method = _rtpRtcpModule->RTCP(); |
| 687 | if (method == RtcpMode::kOff) { |
| 688 | return 0; |
| 689 | } |
| 690 | std::vector<RTCPReportBlock> report_blocks; |
| 691 | _rtpRtcpModule->RemoteRTCPStat(&report_blocks); |
| 692 | |
| 693 | // TODO(nisse): Could we check the return value from the ->RTT() call below, |
| 694 | // instead of checking if we have any report blocks? |
| 695 | if (report_blocks.empty()) { |
| 696 | rtc::CritScope lock(&assoc_send_channel_lock_); |
| 697 | // Tries to get RTT from an associated channel. |
| 698 | if (!associated_send_channel_) { |
| 699 | return 0; |
| 700 | } |
| 701 | return associated_send_channel_->GetRTT(); |
| 702 | } |
| 703 | |
| 704 | int64_t rtt = 0; |
| 705 | int64_t avg_rtt = 0; |
| 706 | int64_t max_rtt = 0; |
| 707 | int64_t min_rtt = 0; |
| 708 | if (_rtpRtcpModule->RTT(remote_ssrc_, &rtt, &avg_rtt, &min_rtt, &max_rtt) != |
| 709 | 0) { |
| 710 | return 0; |
| 711 | } |
| 712 | return rtt; |
| 713 | } |
| 714 | |
| 715 | } // namespace voe |
| 716 | } // namespace webrtc |