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