blob: f61f298ae37e26321643c65d6aa78ecee137dc4b [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"
Niels Möller349ade32018-11-16 09:50:42 +010021#include "audio/audio_level.h"
Niels Möller530ead42018-10-04 14:28:39 +020022#include "audio/channel_send.h"
23#include "audio/utility/audio_frame_operations.h"
24#include "logging/rtc_event_log/events/rtc_event_audio_playout.h"
25#include "logging/rtc_event_log/rtc_event_log.h"
26#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
Niels Möller349ade32018-11-16 09:50:42 +010027#include "modules/audio_coding/include/audio_coding_module.h"
Niels Möller530ead42018-10-04 14:28:39 +020028#include "modules/audio_device/include/audio_device.h"
29#include "modules/pacing/packet_router.h"
30#include "modules/rtp_rtcp/include/receive_statistics.h"
Niels Möller349ade32018-11-16 09:50:42 +010031#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
32#include "modules/rtp_rtcp/include/rtp_rtcp.h"
33#include "modules/rtp_rtcp/source/contributing_sources.h"
Yves Gerey988cc082018-10-23 12:03:01 +020034#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
Niels Möller530ead42018-10-04 14:28:39 +020035#include "modules/rtp_rtcp/source/rtp_packet_received.h"
36#include "modules/utility/include/process_thread.h"
37#include "rtc_base/checks.h"
38#include "rtc_base/criticalsection.h"
39#include "rtc_base/format_macros.h"
40#include "rtc_base/location.h"
41#include "rtc_base/logging.h"
Niels Möller349ade32018-11-16 09:50:42 +010042#include "rtc_base/numerics/safe_minmax.h"
43#include "rtc_base/race_checker.h"
Niels Möller530ead42018-10-04 14:28:39 +020044#include "rtc_base/thread_checker.h"
45#include "rtc_base/timeutils.h"
46#include "system_wrappers/include/metrics.h"
47
48namespace webrtc {
49namespace voe {
50
51namespace {
52
53constexpr double kAudioSampleDurationSeconds = 0.01;
54constexpr int64_t kMaxRetransmissionWindowMs = 1000;
55constexpr int64_t kMinRetransmissionWindowMs = 30;
56
57// Video Sync.
58constexpr int kVoiceEngineMinMinPlayoutDelayMs = 0;
59constexpr int kVoiceEngineMaxMinPlayoutDelayMs = 10000;
60
Niels Möller7d76a312018-10-26 12:57:07 +020061webrtc::FrameType WebrtcFrameTypeForMediaTransportFrameType(
62 MediaTransportEncodedAudioFrame::FrameType frame_type) {
63 switch (frame_type) {
64 case MediaTransportEncodedAudioFrame::FrameType::kSpeech:
65 return kAudioFrameSpeech;
66 break;
67
68 case MediaTransportEncodedAudioFrame::FrameType::
69 kDiscountinuousTransmission:
70 return kAudioFrameCN;
71 break;
72 }
73}
74
75WebRtcRTPHeader CreateWebrtcRTPHeaderForMediaTransportFrame(
76 const MediaTransportEncodedAudioFrame& frame,
77 uint64_t channel_id) {
78 webrtc::WebRtcRTPHeader webrtc_header = {};
79 webrtc_header.header.payloadType = frame.payload_type();
80 webrtc_header.header.payload_type_frequency = frame.sampling_rate_hz();
81 webrtc_header.header.timestamp = frame.starting_sample_index();
82 webrtc_header.header.sequenceNumber = frame.sequence_number();
83
84 webrtc_header.frameType =
85 WebrtcFrameTypeForMediaTransportFrameType(frame.frame_type());
86
87 webrtc_header.header.ssrc = static_cast<uint32_t>(channel_id);
88
89 // The rest are initialized by the RTPHeader constructor.
90 return webrtc_header;
91}
92
Niels Möller349ade32018-11-16 09:50:42 +010093class ChannelReceive : public ChannelReceiveInterface,
94 public MediaTransportAudioSinkInterface {
95 public:
96 // Used for receive streams.
97 ChannelReceive(ProcessThread* module_process_thread,
98 AudioDeviceModule* audio_device_module,
99 MediaTransportInterface* media_transport,
100 Transport* rtcp_send_transport,
101 RtcEventLog* rtc_event_log,
102 uint32_t remote_ssrc,
103 size_t jitter_buffer_max_packets,
104 bool jitter_buffer_fast_playout,
105 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
106 absl::optional<AudioCodecPairId> codec_pair_id,
107 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor,
108 const webrtc::CryptoOptions& crypto_options);
109 ~ChannelReceive() override;
110
111 void SetSink(AudioSinkInterface* sink) override;
112
113 void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) override;
114
115 // API methods
116
117 void StartPlayout() override;
118 void StopPlayout() override;
119
120 // Codecs
121 bool GetRecCodec(CodecInst* codec) const override;
122
123 bool ReceivedRTCPPacket(const uint8_t* data, size_t length) override;
124
125 // RtpPacketSinkInterface.
126 void OnRtpPacket(const RtpPacketReceived& packet) override;
127
128 // Muting, Volume and Level.
129 void SetChannelOutputVolumeScaling(float scaling) override;
130 int GetSpeechOutputLevelFullRange() const override;
131 // See description of "totalAudioEnergy" in the WebRTC stats spec:
132 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
133 double GetTotalOutputEnergy() const override;
134 double GetTotalOutputDuration() const override;
135
136 // Stats.
137 NetworkStatistics GetNetworkStatistics() const override;
138 AudioDecodingCallStats GetDecodingCallStatistics() const override;
139
140 // Audio+Video Sync.
141 uint32_t GetDelayEstimate() const override;
142 void SetMinimumPlayoutDelay(int delayMs) override;
143 uint32_t GetPlayoutTimestamp() const override;
144
145 // Produces the transport-related timestamps; current_delay_ms is left unset.
146 absl::optional<Syncable::Info> GetSyncInfo() const override;
147
148 // RTP+RTCP
149 void SetLocalSSRC(unsigned int ssrc) override;
150
151 void RegisterReceiverCongestionControlObjects(
152 PacketRouter* packet_router) override;
153 void ResetReceiverCongestionControlObjects() override;
154
155 CallReceiveStatistics GetRTCPStatistics() const override;
156 void SetNACKStatus(bool enable, int maxNumberOfPackets) override;
157
158 AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
159 int sample_rate_hz,
160 AudioFrame* audio_frame) override;
161
162 int PreferredSampleRate() const override;
163
164 // Associate to a send channel.
165 // Used for obtaining RTT for a receive-only channel.
Niels Möllerdced9f62018-11-19 10:27:07 +0100166 void SetAssociatedSendChannel(const ChannelSendInterface* channel) override;
Niels Möller349ade32018-11-16 09:50:42 +0100167
168 std::vector<RtpSource> GetSources() const override;
169
170 private:
Niels Möller349ade32018-11-16 09:50:42 +0100171 bool ReceivePacket(const uint8_t* packet,
172 size_t packet_length,
173 const RTPHeader& header);
174 int ResendPackets(const uint16_t* sequence_numbers, int length);
175 void UpdatePlayoutTimestamp(bool rtcp);
176
177 int GetRtpTimestampRateHz() const;
178 int64_t GetRTT() const;
179
180 // MediaTransportAudioSinkInterface override;
181 void OnData(uint64_t channel_id,
182 MediaTransportEncodedAudioFrame frame) override;
183
184 int32_t OnReceivedPayloadData(const uint8_t* payloadData,
185 size_t payloadSize,
186 const WebRtcRTPHeader* rtpHeader);
187
Fredrik Solenbergc5e8be32018-11-19 11:56:13 +0100188 bool Playing() const {
189 rtc::CritScope lock(&playing_lock_);
190 return playing_;
191 }
192
Niels Möller349ade32018-11-16 09:50:42 +0100193 // Thread checkers document and lock usage of some methods to specific threads
194 // we know about. The goal is to eventually split up voe::ChannelReceive into
195 // parts with single-threaded semantics, and thereby reduce the need for
196 // locks.
197 rtc::ThreadChecker worker_thread_checker_;
198 rtc::ThreadChecker module_process_thread_checker_;
199 // Methods accessed from audio and video threads are checked for sequential-
200 // only access. We don't necessarily own and control these threads, so thread
201 // checkers cannot be used. E.g. Chromium may transfer "ownership" from one
202 // audio thread to another, but access is still sequential.
203 rtc::RaceChecker audio_thread_race_checker_;
204 rtc::RaceChecker video_capture_thread_race_checker_;
205 rtc::CriticalSection _callbackCritSect;
206 rtc::CriticalSection volume_settings_critsect_;
207
Fredrik Solenbergc5e8be32018-11-19 11:56:13 +0100208 rtc::CriticalSection playing_lock_;
209 bool playing_ RTC_GUARDED_BY(&playing_lock_) = false;
Niels Möller349ade32018-11-16 09:50:42 +0100210
211 RtcEventLog* const event_log_;
212
213 // Indexed by payload type.
214 std::map<uint8_t, int> payload_type_frequencies_;
215
216 std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
217 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
218 const uint32_t remote_ssrc_;
219
220 // Info for GetSources and GetSyncInfo is updated on network or worker thread,
221 // queried on the worker thread.
222 rtc::CriticalSection rtp_sources_lock_;
223 ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
224 absl::optional<uint32_t> last_received_rtp_timestamp_
225 RTC_GUARDED_BY(&rtp_sources_lock_);
226 absl::optional<int64_t> last_received_rtp_system_time_ms_
227 RTC_GUARDED_BY(&rtp_sources_lock_);
228 absl::optional<uint8_t> last_received_rtp_audio_level_
229 RTC_GUARDED_BY(&rtp_sources_lock_);
230
231 std::unique_ptr<AudioCodingModule> audio_coding_;
232 AudioSinkInterface* audio_sink_ = nullptr;
233 AudioLevel _outputAudioLevel;
234
235 RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(ts_stats_lock_);
236
237 // Timestamp of the audio pulled from NetEq.
238 absl::optional<uint32_t> jitter_buffer_playout_timestamp_;
239
240 rtc::CriticalSection video_sync_lock_;
241 uint32_t playout_timestamp_rtp_ RTC_GUARDED_BY(video_sync_lock_);
242 uint32_t playout_delay_ms_ RTC_GUARDED_BY(video_sync_lock_);
243
244 rtc::CriticalSection ts_stats_lock_;
245
246 std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_;
247 // The rtp timestamp of the first played out audio frame.
248 int64_t capture_start_rtp_time_stamp_;
249 // The capture ntp time (in local timebase) of the first played out audio
250 // frame.
251 int64_t capture_start_ntp_time_ms_ RTC_GUARDED_BY(ts_stats_lock_);
252
253 // uses
254 ProcessThread* _moduleProcessThreadPtr;
255 AudioDeviceModule* _audioDeviceModulePtr;
256 float _outputGain RTC_GUARDED_BY(volume_settings_critsect_);
257
258 // An associated send channel.
259 rtc::CriticalSection assoc_send_channel_lock_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100260 const ChannelSendInterface* associated_send_channel_
Niels Möller349ade32018-11-16 09:50:42 +0100261 RTC_GUARDED_BY(assoc_send_channel_lock_);
262
263 PacketRouter* packet_router_ = nullptr;
264
265 rtc::ThreadChecker construction_thread_;
266
267 MediaTransportInterface* const media_transport_;
268
269 // E2EE Audio Frame Decryption
270 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
271 webrtc::CryptoOptions crypto_options_;
272};
Niels Möller530ead42018-10-04 14:28:39 +0200273
Niels Möller530ead42018-10-04 14:28:39 +0200274int32_t ChannelReceive::OnReceivedPayloadData(
275 const uint8_t* payloadData,
276 size_t payloadSize,
277 const WebRtcRTPHeader* rtpHeader) {
Niels Möller7d76a312018-10-26 12:57:07 +0200278 // We should not be receiving any RTP packets if media_transport is set.
279 RTC_CHECK(!media_transport_);
280
Fredrik Solenbergc5e8be32018-11-19 11:56:13 +0100281 if (!Playing()) {
Niels Möller530ead42018-10-04 14:28:39 +0200282 // Avoid inserting into NetEQ when we are not playing. Count the
283 // packet as discarded.
284 return 0;
285 }
286
287 // Push the incoming payload (parsed and ready for decoding) into the ACM
288 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
289 0) {
290 RTC_DLOG(LS_ERROR) << "ChannelReceive::OnReceivedPayloadData() unable to "
291 "push data to the ACM";
292 return -1;
293 }
294
295 int64_t round_trip_time = 0;
296 _rtpRtcpModule->RTT(remote_ssrc_, &round_trip_time, NULL, NULL, NULL);
297
298 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
299 if (!nack_list.empty()) {
300 // Can't use nack_list.data() since it's not supported by all
301 // compilers.
302 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
303 }
304 return 0;
305}
306
Niels Möller7d76a312018-10-26 12:57:07 +0200307// MediaTransportAudioSinkInterface override.
308void ChannelReceive::OnData(uint64_t channel_id,
309 MediaTransportEncodedAudioFrame frame) {
310 RTC_CHECK(media_transport_);
311
Fredrik Solenbergc5e8be32018-11-19 11:56:13 +0100312 if (!Playing()) {
Niels Möller7d76a312018-10-26 12:57:07 +0200313 // Avoid inserting into NetEQ when we are not playing. Count the
314 // packet as discarded.
315 return;
316 }
317
318 // Send encoded audio frame to Decoder / NetEq.
319 if (audio_coding_->IncomingPacket(
320 frame.encoded_data().data(), frame.encoded_data().size(),
321 CreateWebrtcRTPHeaderForMediaTransportFrame(frame, channel_id)) !=
322 0) {
323 RTC_DLOG(LS_ERROR) << "ChannelReceive::OnData: unable to "
324 "push data to the ACM";
325 }
326}
327
Niels Möller530ead42018-10-04 14:28:39 +0200328AudioMixer::Source::AudioFrameInfo ChannelReceive::GetAudioFrameWithInfo(
329 int sample_rate_hz,
330 AudioFrame* audio_frame) {
Niels Möller349ade32018-11-16 09:50:42 +0100331 RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200332 audio_frame->sample_rate_hz_ = sample_rate_hz;
333
Fredrik Solenbergc5e8be32018-11-19 11:56:13 +0100334 event_log_->Log(absl::make_unique<RtcEventAudioPlayout>(remote_ssrc_));
335
Niels Möller530ead42018-10-04 14:28:39 +0200336 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
337 bool muted;
338 if (audio_coding_->PlayoutData10Ms(audio_frame->sample_rate_hz_, audio_frame,
339 &muted) == -1) {
340 RTC_DLOG(LS_ERROR)
341 << "ChannelReceive::GetAudioFrame() PlayoutData10Ms() failed!";
342 // In all likelihood, the audio in this frame is garbage. We return an
343 // error so that the audio mixer module doesn't add it to the mix. As
344 // a result, it won't be played out and the actions skipped here are
345 // irrelevant.
346 return AudioMixer::Source::AudioFrameInfo::kError;
347 }
348
349 if (muted) {
350 // TODO(henrik.lundin): We should be able to do better than this. But we
351 // will have to go through all the cases below where the audio samples may
352 // be used, and handle the muted case in some way.
353 AudioFrameOperations::Mute(audio_frame);
354 }
355
356 {
357 // Pass the audio buffers to an optional sink callback, before applying
358 // scaling/panning, as that applies to the mix operation.
359 // External recipients of the audio (e.g. via AudioTrack), will do their
360 // own mixing/dynamic processing.
361 rtc::CritScope cs(&_callbackCritSect);
362 if (audio_sink_) {
363 AudioSinkInterface::Data data(
364 audio_frame->data(), audio_frame->samples_per_channel_,
365 audio_frame->sample_rate_hz_, audio_frame->num_channels_,
366 audio_frame->timestamp_);
367 audio_sink_->OnData(data);
368 }
369 }
370
371 float output_gain = 1.0f;
372 {
373 rtc::CritScope cs(&volume_settings_critsect_);
374 output_gain = _outputGain;
375 }
376
377 // Output volume scaling
378 if (output_gain < 0.99f || output_gain > 1.01f) {
379 // TODO(solenberg): Combine with mute state - this can cause clicks!
380 AudioFrameOperations::ScaleWithSat(output_gain, audio_frame);
381 }
382
383 // Measure audio level (0-9)
384 // TODO(henrik.lundin) Use the |muted| information here too.
385 // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see
386 // https://crbug.com/webrtc/7517).
387 _outputAudioLevel.ComputeLevel(*audio_frame, kAudioSampleDurationSeconds);
388
389 if (capture_start_rtp_time_stamp_ < 0 && audio_frame->timestamp_ != 0) {
390 // The first frame with a valid rtp timestamp.
391 capture_start_rtp_time_stamp_ = audio_frame->timestamp_;
392 }
393
394 if (capture_start_rtp_time_stamp_ >= 0) {
395 // audio_frame.timestamp_ should be valid from now on.
396
397 // Compute elapsed time.
398 int64_t unwrap_timestamp =
399 rtp_ts_wraparound_handler_->Unwrap(audio_frame->timestamp_);
400 audio_frame->elapsed_time_ms_ =
401 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
402 (GetRtpTimestampRateHz() / 1000);
403
404 {
405 rtc::CritScope lock(&ts_stats_lock_);
406 // Compute ntp time.
407 audio_frame->ntp_time_ms_ =
408 ntp_estimator_.Estimate(audio_frame->timestamp_);
409 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
410 if (audio_frame->ntp_time_ms_ > 0) {
411 // Compute |capture_start_ntp_time_ms_| so that
412 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
413 capture_start_ntp_time_ms_ =
414 audio_frame->ntp_time_ms_ - audio_frame->elapsed_time_ms_;
415 }
416 }
417 }
418
419 {
420 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.TargetJitterBufferDelayMs",
421 audio_coding_->TargetDelayMs());
422 const int jitter_buffer_delay = audio_coding_->FilteredCurrentDelayMs();
423 rtc::CritScope lock(&video_sync_lock_);
424 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDelayEstimateMs",
425 jitter_buffer_delay + playout_delay_ms_);
426 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverJitterBufferDelayMs",
427 jitter_buffer_delay);
428 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDeviceDelayMs",
429 playout_delay_ms_);
430 }
431
432 return muted ? AudioMixer::Source::AudioFrameInfo::kMuted
433 : AudioMixer::Source::AudioFrameInfo::kNormal;
434}
435
436int ChannelReceive::PreferredSampleRate() const {
Niels Möller349ade32018-11-16 09:50:42 +0100437 RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200438 // Return the bigger of playout and receive frequency in the ACM.
439 return std::max(audio_coding_->ReceiveFrequency(),
440 audio_coding_->PlayoutFrequency());
441}
442
443ChannelReceive::ChannelReceive(
444 ProcessThread* module_process_thread,
445 AudioDeviceModule* audio_device_module,
Niels Möller7d76a312018-10-26 12:57:07 +0200446 MediaTransportInterface* media_transport,
Niels Möllerae4237e2018-10-05 11:28:38 +0200447 Transport* rtcp_send_transport,
Niels Möller530ead42018-10-04 14:28:39 +0200448 RtcEventLog* rtc_event_log,
449 uint32_t remote_ssrc,
450 size_t jitter_buffer_max_packets,
451 bool jitter_buffer_fast_playout,
452 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
Benjamin Wright84583f62018-10-04 14:22:34 -0700453 absl::optional<AudioCodecPairId> codec_pair_id,
Benjamin Wright78410ad2018-10-25 09:52:57 -0700454 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700455 const webrtc::CryptoOptions& crypto_options)
Niels Möller530ead42018-10-04 14:28:39 +0200456 : event_log_(rtc_event_log),
457 rtp_receive_statistics_(
458 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
459 remote_ssrc_(remote_ssrc),
460 _outputAudioLevel(),
461 ntp_estimator_(Clock::GetRealTimeClock()),
462 playout_timestamp_rtp_(0),
463 playout_delay_ms_(0),
464 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
465 capture_start_rtp_time_stamp_(-1),
466 capture_start_ntp_time_ms_(-1),
467 _moduleProcessThreadPtr(module_process_thread),
468 _audioDeviceModulePtr(audio_device_module),
Niels Möller530ead42018-10-04 14:28:39 +0200469 _outputGain(1.0f),
Benjamin Wright84583f62018-10-04 14:22:34 -0700470 associated_send_channel_(nullptr),
Niels Möller7d76a312018-10-26 12:57:07 +0200471 media_transport_(media_transport),
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700472 frame_decryptor_(frame_decryptor),
473 crypto_options_(crypto_options) {
Niels Möller349ade32018-11-16 09:50:42 +0100474 // TODO(nisse): Use _moduleProcessThreadPtr instead?
475 module_process_thread_checker_.DetachFromThread();
476
Niels Möller530ead42018-10-04 14:28:39 +0200477 RTC_DCHECK(module_process_thread);
478 RTC_DCHECK(audio_device_module);
479 AudioCodingModule::Config acm_config;
480 acm_config.decoder_factory = decoder_factory;
481 acm_config.neteq_config.codec_pair_id = codec_pair_id;
482 acm_config.neteq_config.max_packets_in_buffer = jitter_buffer_max_packets;
483 acm_config.neteq_config.enable_fast_accelerate = jitter_buffer_fast_playout;
484 acm_config.neteq_config.enable_muted_state = true;
485 audio_coding_.reset(AudioCodingModule::Create(acm_config));
486
487 _outputAudioLevel.Clear();
488
489 rtp_receive_statistics_->EnableRetransmitDetection(remote_ssrc_, true);
490 RtpRtcp::Configuration configuration;
491 configuration.audio = true;
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100492 configuration.receiver_only = true;
Niels Möllerae4237e2018-10-05 11:28:38 +0200493 configuration.outgoing_transport = rtcp_send_transport;
Niels Möller530ead42018-10-04 14:28:39 +0200494 configuration.receive_statistics = rtp_receive_statistics_.get();
495
496 configuration.event_log = event_log_;
Niels Möller530ead42018-10-04 14:28:39 +0200497
498 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
499 _rtpRtcpModule->SetSendingMediaStatus(false);
500 _rtpRtcpModule->SetRemoteSSRC(remote_ssrc_);
Niels Möller530ead42018-10-04 14:28:39 +0200501
Niels Möller530ead42018-10-04 14:28:39 +0200502 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
503
Niels Möller530ead42018-10-04 14:28:39 +0200504 // Ensure that RTCP is enabled by default for the created channel.
505 // Note that, the module will keep generating RTCP until it is explicitly
506 // disabled by the user.
507 // After StopListen (when no sockets exists), RTCP packets will no longer
508 // be transmitted since the Transport object will then be invalid.
509 // RTCP is enabled by default.
510 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
Niels Möller7d76a312018-10-26 12:57:07 +0200511
512 if (media_transport_) {
513 media_transport_->SetReceiveAudioSink(this);
514 }
Niels Möller530ead42018-10-04 14:28:39 +0200515}
516
Fredrik Solenbergc5e8be32018-11-19 11:56:13 +0100517ChannelReceive::~ChannelReceive() {
Niels Möller530ead42018-10-04 14:28:39 +0200518 RTC_DCHECK(construction_thread_.CalledOnValidThread());
Niels Möller7d76a312018-10-26 12:57:07 +0200519
520 if (media_transport_) {
521 media_transport_->SetReceiveAudioSink(nullptr);
522 }
523
Niels Möller530ead42018-10-04 14:28:39 +0200524 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
525
526 StopPlayout();
527
Niels Möller530ead42018-10-04 14:28:39 +0200528 int error = audio_coding_->RegisterTransportCallback(NULL);
529 RTC_DCHECK_EQ(0, error);
530
Niels Möller530ead42018-10-04 14:28:39 +0200531 if (_moduleProcessThreadPtr)
532 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
Niels Möller530ead42018-10-04 14:28:39 +0200533}
534
535void ChannelReceive::SetSink(AudioSinkInterface* sink) {
Niels Möller349ade32018-11-16 09:50:42 +0100536 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200537 rtc::CritScope cs(&_callbackCritSect);
538 audio_sink_ = sink;
539}
540
Niels Möller80c67622018-11-12 13:22:47 +0100541void ChannelReceive::StartPlayout() {
Niels Möller349ade32018-11-16 09:50:42 +0100542 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergc5e8be32018-11-19 11:56:13 +0100543 rtc::CritScope lock(&playing_lock_);
544 playing_ = true;
Niels Möller530ead42018-10-04 14:28:39 +0200545}
546
Niels Möller80c67622018-11-12 13:22:47 +0100547void ChannelReceive::StopPlayout() {
Niels Möller349ade32018-11-16 09:50:42 +0100548 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergc5e8be32018-11-19 11:56:13 +0100549 rtc::CritScope lock(&playing_lock_);
550 playing_ = false;
Niels Möller530ead42018-10-04 14:28:39 +0200551 _outputAudioLevel.Clear();
Niels Möller530ead42018-10-04 14:28:39 +0200552}
553
Niels Möller349ade32018-11-16 09:50:42 +0100554bool ChannelReceive::GetRecCodec(CodecInst* codec) const {
555 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller80c67622018-11-12 13:22:47 +0100556 return (audio_coding_->ReceiveCodec(codec) == 0);
Niels Möller530ead42018-10-04 14:28:39 +0200557}
558
559std::vector<webrtc::RtpSource> ChannelReceive::GetSources() const {
Niels Möller349ade32018-11-16 09:50:42 +0100560 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200561 int64_t now_ms = rtc::TimeMillis();
562 std::vector<RtpSource> sources;
563 {
564 rtc::CritScope cs(&rtp_sources_lock_);
565 sources = contributing_sources_.GetSources(now_ms);
566 if (last_received_rtp_system_time_ms_ >=
567 now_ms - ContributingSources::kHistoryMs) {
568 sources.emplace_back(*last_received_rtp_system_time_ms_, remote_ssrc_,
569 RtpSourceType::SSRC);
570 sources.back().set_audio_level(last_received_rtp_audio_level_);
571 }
572 }
573 return sources;
574}
575
576void ChannelReceive::SetReceiveCodecs(
577 const std::map<int, SdpAudioFormat>& codecs) {
Niels Möller349ade32018-11-16 09:50:42 +0100578 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200579 for (const auto& kv : codecs) {
580 RTC_DCHECK_GE(kv.second.clockrate_hz, 1000);
581 payload_type_frequencies_[kv.first] = kv.second.clockrate_hz;
582 }
583 audio_coding_->SetReceiveCodecs(codecs);
584}
585
Niels Möller349ade32018-11-16 09:50:42 +0100586// May be called on either worker thread or network thread.
Niels Möller530ead42018-10-04 14:28:39 +0200587void ChannelReceive::OnRtpPacket(const RtpPacketReceived& packet) {
588 int64_t now_ms = rtc::TimeMillis();
589 uint8_t audio_level;
590 bool voice_activity;
591 bool has_audio_level =
592 packet.GetExtension<::webrtc::AudioLevel>(&voice_activity, &audio_level);
593
594 {
595 rtc::CritScope cs(&rtp_sources_lock_);
596 last_received_rtp_timestamp_ = packet.Timestamp();
597 last_received_rtp_system_time_ms_ = now_ms;
598 if (has_audio_level)
599 last_received_rtp_audio_level_ = audio_level;
600 std::vector<uint32_t> csrcs = packet.Csrcs();
Jonas Oreland967f7d52018-11-06 07:35:06 +0100601 contributing_sources_.Update(
602 now_ms, csrcs,
603 has_audio_level ? absl::optional<uint8_t>(audio_level) : absl::nullopt);
Niels Möller530ead42018-10-04 14:28:39 +0200604 }
605
606 // Store playout timestamp for the received RTP packet
607 UpdatePlayoutTimestamp(false);
608
609 const auto& it = payload_type_frequencies_.find(packet.PayloadType());
610 if (it == payload_type_frequencies_.end())
611 return;
612 // TODO(nisse): Set payload_type_frequency earlier, when packet is parsed.
613 RtpPacketReceived packet_copy(packet);
614 packet_copy.set_payload_type_frequency(it->second);
615
616 rtp_receive_statistics_->OnRtpPacket(packet_copy);
617
618 RTPHeader header;
619 packet_copy.GetHeader(&header);
620
621 ReceivePacket(packet_copy.data(), packet_copy.size(), header);
622}
623
624bool ChannelReceive::ReceivePacket(const uint8_t* packet,
625 size_t packet_length,
626 const RTPHeader& header) {
627 const uint8_t* payload = packet + header.headerLength;
628 assert(packet_length >= header.headerLength);
629 size_t payload_length = packet_length - header.headerLength;
630 WebRtcRTPHeader webrtc_rtp_header = {};
631 webrtc_rtp_header.header = header;
632
Benjamin Wright84583f62018-10-04 14:22:34 -0700633 size_t payload_data_length = payload_length - header.paddingLength;
634
635 // E2EE Custom Audio Frame Decryption (This is optional).
636 // Keep this buffer around for the lifetime of the OnReceivedPayloadData call.
637 rtc::Buffer decrypted_audio_payload;
638 if (frame_decryptor_ != nullptr) {
639 size_t max_plaintext_size = frame_decryptor_->GetMaxPlaintextByteSize(
640 cricket::MEDIA_TYPE_AUDIO, payload_length);
641 decrypted_audio_payload.SetSize(max_plaintext_size);
642
643 size_t bytes_written = 0;
644 std::vector<uint32_t> csrcs(header.arrOfCSRCs,
645 header.arrOfCSRCs + header.numCSRCs);
646 int decrypt_status = frame_decryptor_->Decrypt(
647 cricket::MEDIA_TYPE_AUDIO, csrcs,
648 /*additional_data=*/nullptr,
649 rtc::ArrayView<const uint8_t>(payload, payload_data_length),
650 decrypted_audio_payload, &bytes_written);
651
652 // In this case just interpret the failure as a silent frame.
653 if (decrypt_status != 0) {
654 bytes_written = 0;
655 }
656
657 // Resize the decrypted audio payload to the number of bytes actually
658 // written.
659 decrypted_audio_payload.SetSize(bytes_written);
660 // Update the final payload.
661 payload = decrypted_audio_payload.data();
662 payload_data_length = decrypted_audio_payload.size();
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700663 } else if (crypto_options_.sframe.require_frame_encryption) {
664 RTC_DLOG(LS_ERROR)
665 << "FrameDecryptor required but not set, dropping packet";
666 payload_data_length = 0;
Benjamin Wright84583f62018-10-04 14:22:34 -0700667 }
668
Niels Möller530ead42018-10-04 14:28:39 +0200669 if (payload_data_length == 0) {
670 webrtc_rtp_header.frameType = kEmptyFrame;
671 return OnReceivedPayloadData(nullptr, 0, &webrtc_rtp_header);
672 }
673 return OnReceivedPayloadData(payload, payload_data_length,
674 &webrtc_rtp_header);
675}
676
Niels Möller349ade32018-11-16 09:50:42 +0100677// May be called on either worker thread or network thread.
Niels Möller80c67622018-11-12 13:22:47 +0100678// TODO(nisse): Drop always-true return value.
679bool ChannelReceive::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
Niels Möller530ead42018-10-04 14:28:39 +0200680 // Store playout timestamp for the received RTCP packet
681 UpdatePlayoutTimestamp(true);
682
683 // Deliver RTCP packet to RTP/RTCP module for parsing
684 _rtpRtcpModule->IncomingRtcpPacket(data, length);
685
686 int64_t rtt = GetRTT();
687 if (rtt == 0) {
688 // Waiting for valid RTT.
Niels Möller80c67622018-11-12 13:22:47 +0100689 return true;
Niels Möller530ead42018-10-04 14:28:39 +0200690 }
691
692 int64_t nack_window_ms = rtt;
693 if (nack_window_ms < kMinRetransmissionWindowMs) {
694 nack_window_ms = kMinRetransmissionWindowMs;
695 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
696 nack_window_ms = kMaxRetransmissionWindowMs;
697 }
698
699 uint32_t ntp_secs = 0;
700 uint32_t ntp_frac = 0;
701 uint32_t rtp_timestamp = 0;
702 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
703 &rtp_timestamp)) {
704 // Waiting for RTCP.
Niels Möller80c67622018-11-12 13:22:47 +0100705 return true;
Niels Möller530ead42018-10-04 14:28:39 +0200706 }
707
708 {
709 rtc::CritScope lock(&ts_stats_lock_);
710 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
711 }
Niels Möller80c67622018-11-12 13:22:47 +0100712 return true;
Niels Möller530ead42018-10-04 14:28:39 +0200713}
714
715int ChannelReceive::GetSpeechOutputLevelFullRange() const {
Niels Möller349ade32018-11-16 09:50:42 +0100716 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200717 return _outputAudioLevel.LevelFullRange();
718}
719
720double ChannelReceive::GetTotalOutputEnergy() const {
Niels Möller349ade32018-11-16 09:50:42 +0100721 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200722 return _outputAudioLevel.TotalEnergy();
723}
724
725double ChannelReceive::GetTotalOutputDuration() const {
Niels Möller349ade32018-11-16 09:50:42 +0100726 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200727 return _outputAudioLevel.TotalDuration();
728}
729
730void ChannelReceive::SetChannelOutputVolumeScaling(float scaling) {
Niels Möller349ade32018-11-16 09:50:42 +0100731 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200732 rtc::CritScope cs(&volume_settings_critsect_);
733 _outputGain = scaling;
734}
735
Niels Möller349ade32018-11-16 09:50:42 +0100736void ChannelReceive::SetLocalSSRC(uint32_t ssrc) {
737 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200738 _rtpRtcpModule->SetSSRC(ssrc);
Niels Möller530ead42018-10-04 14:28:39 +0200739}
740
Niels Möller530ead42018-10-04 14:28:39 +0200741void ChannelReceive::RegisterReceiverCongestionControlObjects(
742 PacketRouter* packet_router) {
Niels Möller349ade32018-11-16 09:50:42 +0100743 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200744 RTC_DCHECK(packet_router);
745 RTC_DCHECK(!packet_router_);
746 constexpr bool remb_candidate = false;
747 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
748 packet_router_ = packet_router;
749}
750
751void ChannelReceive::ResetReceiverCongestionControlObjects() {
Niels Möller349ade32018-11-16 09:50:42 +0100752 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200753 RTC_DCHECK(packet_router_);
754 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
755 packet_router_ = nullptr;
756}
757
Niels Möller349ade32018-11-16 09:50:42 +0100758CallReceiveStatistics ChannelReceive::GetRTCPStatistics() const {
759 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200760 // --- RtcpStatistics
Niels Möller80c67622018-11-12 13:22:47 +0100761 CallReceiveStatistics stats;
Niels Möller530ead42018-10-04 14:28:39 +0200762
763 // The jitter statistics is updated for each received RTP packet and is
764 // based on received packets.
765 RtcpStatistics statistics;
766 StreamStatistician* statistician =
767 rtp_receive_statistics_->GetStatistician(remote_ssrc_);
768 if (statistician) {
769 statistician->GetStatistics(&statistics,
770 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
771 }
772
773 stats.fractionLost = statistics.fraction_lost;
774 stats.cumulativeLost = statistics.packets_lost;
775 stats.extendedMax = statistics.extended_highest_sequence_number;
776 stats.jitterSamples = statistics.jitter;
777
778 // --- RTT
779 stats.rttMs = GetRTT();
780
781 // --- Data counters
782
783 size_t bytesReceived(0);
784 uint32_t packetsReceived(0);
785
786 if (statistician) {
787 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
788 }
789
790 stats.bytesReceived = bytesReceived;
791 stats.packetsReceived = packetsReceived;
792
793 // --- Timestamps
794 {
795 rtc::CritScope lock(&ts_stats_lock_);
796 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
797 }
Niels Möller80c67622018-11-12 13:22:47 +0100798 return stats;
Niels Möller530ead42018-10-04 14:28:39 +0200799}
800
Niels Möller349ade32018-11-16 09:50:42 +0100801void ChannelReceive::SetNACKStatus(bool enable, int max_packets) {
802 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200803 // None of these functions can fail.
Niels Möller349ade32018-11-16 09:50:42 +0100804 rtp_receive_statistics_->SetMaxReorderingThreshold(max_packets);
Niels Möller530ead42018-10-04 14:28:39 +0200805 if (enable)
Niels Möller349ade32018-11-16 09:50:42 +0100806 audio_coding_->EnableNack(max_packets);
Niels Möller530ead42018-10-04 14:28:39 +0200807 else
808 audio_coding_->DisableNack();
809}
810
811// Called when we are missing one or more packets.
812int ChannelReceive::ResendPackets(const uint16_t* sequence_numbers,
813 int length) {
814 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
815}
816
Niels Möllerdced9f62018-11-19 10:27:07 +0100817void ChannelReceive::SetAssociatedSendChannel(
818 const ChannelSendInterface* channel) {
Niels Möller349ade32018-11-16 09:50:42 +0100819 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200820 rtc::CritScope lock(&assoc_send_channel_lock_);
821 associated_send_channel_ = channel;
822}
823
Niels Möller80c67622018-11-12 13:22:47 +0100824NetworkStatistics ChannelReceive::GetNetworkStatistics() const {
Niels Möller349ade32018-11-16 09:50:42 +0100825 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller80c67622018-11-12 13:22:47 +0100826 NetworkStatistics stats;
827 int error = audio_coding_->GetNetworkStatistics(&stats);
828 RTC_DCHECK_EQ(0, error);
829 return stats;
Niels Möller530ead42018-10-04 14:28:39 +0200830}
831
Niels Möller80c67622018-11-12 13:22:47 +0100832AudioDecodingCallStats ChannelReceive::GetDecodingCallStatistics() const {
Niels Möller349ade32018-11-16 09:50:42 +0100833 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Niels Möller80c67622018-11-12 13:22:47 +0100834 AudioDecodingCallStats stats;
835 audio_coding_->GetDecodingCallStatistics(&stats);
836 return stats;
Niels Möller530ead42018-10-04 14:28:39 +0200837}
838
839uint32_t ChannelReceive::GetDelayEstimate() const {
Niels Möller349ade32018-11-16 09:50:42 +0100840 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() ||
841 module_process_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200842 rtc::CritScope lock(&video_sync_lock_);
843 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
844}
845
Niels Möller349ade32018-11-16 09:50:42 +0100846void ChannelReceive::SetMinimumPlayoutDelay(int delay_ms) {
847 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
848 // Limit to range accepted by both VoE and ACM, so we're at least getting as
849 // close as possible, instead of failing.
850 delay_ms = rtc::SafeClamp(delay_ms, 0, 10000);
851 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
852 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs)) {
Niels Möller530ead42018-10-04 14:28:39 +0200853 RTC_DLOG(LS_ERROR) << "SetMinimumPlayoutDelay() invalid min delay";
Niels Möller80c67622018-11-12 13:22:47 +0100854 return;
Niels Möller530ead42018-10-04 14:28:39 +0200855 }
Niels Möller349ade32018-11-16 09:50:42 +0100856 if (audio_coding_->SetMinimumPlayoutDelay(delay_ms) != 0) {
Niels Möller530ead42018-10-04 14:28:39 +0200857 RTC_DLOG(LS_ERROR)
858 << "SetMinimumPlayoutDelay() failed to set min playout delay";
Niels Möller530ead42018-10-04 14:28:39 +0200859 }
Niels Möller530ead42018-10-04 14:28:39 +0200860}
861
Niels Möller349ade32018-11-16 09:50:42 +0100862uint32_t ChannelReceive::GetPlayoutTimestamp() const {
863 RTC_DCHECK_RUNS_SERIALIZED(&video_capture_thread_race_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200864 {
865 rtc::CritScope lock(&video_sync_lock_);
Niels Möller80c67622018-11-12 13:22:47 +0100866 return playout_timestamp_rtp_;
Niels Möller530ead42018-10-04 14:28:39 +0200867 }
Niels Möller530ead42018-10-04 14:28:39 +0200868}
869
870absl::optional<Syncable::Info> ChannelReceive::GetSyncInfo() const {
Niels Möller349ade32018-11-16 09:50:42 +0100871 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200872 Syncable::Info info;
873 if (_rtpRtcpModule->RemoteNTP(&info.capture_time_ntp_secs,
874 &info.capture_time_ntp_frac, nullptr, nullptr,
875 &info.capture_time_source_clock) != 0) {
876 return absl::nullopt;
877 }
878 {
879 rtc::CritScope cs(&rtp_sources_lock_);
880 if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
881 return absl::nullopt;
882 }
883 info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
884 info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
885 }
886 return info;
887}
888
889void ChannelReceive::UpdatePlayoutTimestamp(bool rtcp) {
890 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
891
892 if (!jitter_buffer_playout_timestamp_) {
893 // This can happen if this channel has not received any RTP packets. In
894 // this case, NetEq is not capable of computing a playout timestamp.
895 return;
896 }
897
898 uint16_t delay_ms = 0;
899 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
900 RTC_DLOG(LS_WARNING)
901 << "ChannelReceive::UpdatePlayoutTimestamp() failed to read"
902 << " playout delay from the ADM";
903 return;
904 }
905
906 RTC_DCHECK(jitter_buffer_playout_timestamp_);
907 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
908
909 // Remove the playout delay.
910 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
911
912 {
913 rtc::CritScope lock(&video_sync_lock_);
914 if (!rtcp) {
915 playout_timestamp_rtp_ = playout_timestamp;
916 }
917 playout_delay_ms_ = delay_ms;
918 }
919}
920
921int ChannelReceive::GetRtpTimestampRateHz() const {
922 const auto format = audio_coding_->ReceiveFormat();
923 // Default to the playout frequency if we've not gotten any packets yet.
924 // TODO(ossu): Zero clockrate can only happen if we've added an external
925 // decoder for a format we don't support internally. Remove once that way of
926 // adding decoders is gone!
927 return (format && format->clockrate_hz != 0)
928 ? format->clockrate_hz
929 : audio_coding_->PlayoutFrequency();
930}
931
932int64_t ChannelReceive::GetRTT() const {
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800933 if (media_transport_) {
934 auto target_rate = media_transport_->GetLatestTargetTransferRate();
935 if (target_rate.has_value()) {
936 return target_rate->network_estimate.round_trip_time.ms();
937 }
938
939 return 0;
940 }
Niels Möller530ead42018-10-04 14:28:39 +0200941 RtcpMode method = _rtpRtcpModule->RTCP();
942 if (method == RtcpMode::kOff) {
943 return 0;
944 }
945 std::vector<RTCPReportBlock> report_blocks;
946 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
947
948 // TODO(nisse): Could we check the return value from the ->RTT() call below,
949 // instead of checking if we have any report blocks?
950 if (report_blocks.empty()) {
951 rtc::CritScope lock(&assoc_send_channel_lock_);
952 // Tries to get RTT from an associated channel.
953 if (!associated_send_channel_) {
954 return 0;
955 }
956 return associated_send_channel_->GetRTT();
957 }
958
959 int64_t rtt = 0;
960 int64_t avg_rtt = 0;
961 int64_t max_rtt = 0;
962 int64_t min_rtt = 0;
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100963 // TODO(nisse): This method computes RTT based on sender reports, even though
964 // a receive stream is not supposed to do that.
Niels Möller530ead42018-10-04 14:28:39 +0200965 if (_rtpRtcpModule->RTT(remote_ssrc_, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
966 0) {
967 return 0;
968 }
969 return rtt;
970}
971
Niels Möller349ade32018-11-16 09:50:42 +0100972} // namespace
973
974std::unique_ptr<ChannelReceiveInterface> CreateChannelReceive(
975 ProcessThread* module_process_thread,
976 AudioDeviceModule* audio_device_module,
977 MediaTransportInterface* media_transport,
978 Transport* rtcp_send_transport,
979 RtcEventLog* rtc_event_log,
980 uint32_t remote_ssrc,
981 size_t jitter_buffer_max_packets,
982 bool jitter_buffer_fast_playout,
983 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
984 absl::optional<AudioCodecPairId> codec_pair_id,
985 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor,
986 const webrtc::CryptoOptions& crypto_options) {
987 return absl::make_unique<ChannelReceive>(
988 module_process_thread, audio_device_module, media_transport,
989 rtcp_send_transport, rtc_event_log, remote_ssrc,
990 jitter_buffer_max_packets, jitter_buffer_fast_playout, decoder_factory,
991 codec_pair_id, frame_decryptor, crypto_options);
992}
993
Niels Möller530ead42018-10-04 14:28:39 +0200994} // namespace voe
995} // namespace webrtc