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