blob: 8125e967382b90877b6e2290cbe007682604fa3b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Bjorn Terelius440216f2017-09-29 21:01:42 +020014#include <map>
15#include <string>
Tommif888bb52015-12-12 01:37:01 +010016#include <utility>
Bjorn Terelius440216f2017-09-29 21:01:42 +020017#include <vector>
Henrik Lundin64dad832015-05-11 12:44:23 +020018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/array_view.h"
20#include "audio/utility/audio_frame_operations.h"
21#include "call/rtp_transport_controller_send_interface.h"
22#include "logging/rtc_event_log/rtc_event_log.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020023// TODO(eladalon): Remove events/* after removing the deprecated functions.
24#include "logging/rtc_event_log/events/rtc_event_audio_network_adaptation.h"
25#include "logging/rtc_event_log/events/rtc_event_audio_playout.h"
26#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
27#include "logging/rtc_event_log/events/rtc_event_audio_send_stream_config.h"
28#include "logging/rtc_event_log/events/rtc_event_bwe_update_delay_based.h"
29#include "logging/rtc_event_log/events/rtc_event_bwe_update_loss_based.h"
30#include "logging/rtc_event_log/events/rtc_event_logging_started.h"
31#include "logging/rtc_event_log/events/rtc_event_logging_stopped.h"
32#include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
33#include "logging/rtc_event_log/events/rtc_event_probe_result_failure.h"
34#include "logging/rtc_event_log/events/rtc_event_probe_result_success.h"
35#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
36#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_outgoing.h"
37#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
38#include "logging/rtc_event_log/events/rtc_event_rtp_packet_outgoing.h"
39#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
40#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
41#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "modules/audio_coding/codecs/audio_format_conversion.h"
43#include "modules/audio_device/include/audio_device.h"
44#include "modules/audio_processing/include/audio_processing.h"
45#include "modules/include/module_common_types.h"
46#include "modules/pacing/packet_router.h"
47#include "modules/rtp_rtcp/include/receive_statistics.h"
48#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
49#include "modules/rtp_rtcp/include/rtp_receiver.h"
50#include "modules/rtp_rtcp/source/rtp_packet_received.h"
51#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
52#include "modules/utility/include/process_thread.h"
53#include "rtc_base/checks.h"
54#include "rtc_base/criticalsection.h"
55#include "rtc_base/format_macros.h"
56#include "rtc_base/location.h"
57#include "rtc_base/logging.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020058#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/rate_limiter.h"
60#include "rtc_base/task_queue.h"
61#include "rtc_base/thread_checker.h"
62#include "rtc_base/timeutils.h"
63#include "system_wrappers/include/field_trial.h"
henrika45802172017-09-28 09:39:34 +020064#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000066
andrew@webrtc.org50419b02012-11-14 19:07:54 +000067namespace webrtc {
68namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000069
kwibergc8d071e2016-04-06 12:22:38 -070070namespace {
71
zsteine76bd3a2017-07-14 12:17:49 -070072constexpr double kAudioSampleDurationSeconds = 0.01;
Erik Språng737336d2016-07-29 12:59:36 +020073constexpr int64_t kMaxRetransmissionWindowMs = 1000;
74constexpr int64_t kMinRetransmissionWindowMs = 30;
75
kwibergc8d071e2016-04-06 12:22:38 -070076} // namespace
77
solenberg8842c3e2016-03-11 03:06:41 -080078const int kTelephoneEventAttenuationdB = 10;
79
ivoc14d5dbe2016-07-04 07:06:55 -070080class RtcEventLogProxy final : public webrtc::RtcEventLog {
81 public:
82 RtcEventLogProxy() : event_log_(nullptr) {}
83
Elad Alon83ccca12017-10-04 13:18:26 +020084 bool StartLogging(std::unique_ptr<RtcEventLogOutput> output) override {
85 RTC_NOTREACHED();
86 return false;
87 }
88
ivoc14d5dbe2016-07-04 07:06:55 -070089 bool StartLogging(const std::string& file_name,
90 int64_t max_size_bytes) override {
91 RTC_NOTREACHED();
92 return false;
93 }
94
95 bool StartLogging(rtc::PlatformFile log_file,
96 int64_t max_size_bytes) override {
97 RTC_NOTREACHED();
98 return false;
99 }
100
101 void StopLogging() override { RTC_NOTREACHED(); }
102
Elad Alon4a87e1c2017-10-03 16:11:34 +0200103 void Log(std::unique_ptr<RtcEvent> event) override {
104 rtc::CritScope lock(&crit_);
105 if (event_log_) {
106 event_log_->Log(std::move(event));
107 }
108 }
109
ivoc14d5dbe2016-07-04 07:06:55 -0700110 void LogVideoReceiveStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700111 const webrtc::rtclog::StreamConfig&) override {
112 RTC_NOTREACHED();
ivoc14d5dbe2016-07-04 07:06:55 -0700113 }
114
perkjc0876aa2017-05-22 04:08:28 -0700115 void LogVideoSendStreamConfig(const webrtc::rtclog::StreamConfig&) override {
116 RTC_NOTREACHED();
ivoc14d5dbe2016-07-04 07:06:55 -0700117 }
118
ivoce0928d82016-10-10 05:12:51 -0700119 void LogAudioReceiveStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700120 const webrtc::rtclog::StreamConfig& config) override {
ivoce0928d82016-10-10 05:12:51 -0700121 rtc::CritScope lock(&crit_);
122 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200123 event_log_->Log(rtc::MakeUnique<RtcEventAudioReceiveStreamConfig>(
124 rtc::MakeUnique<webrtc::rtclog::StreamConfig>(config)));
ivoce0928d82016-10-10 05:12:51 -0700125 }
126 }
127
128 void LogAudioSendStreamConfig(
perkjf4726992017-05-22 10:12:26 -0700129 const webrtc::rtclog::StreamConfig& config) override {
ivoce0928d82016-10-10 05:12:51 -0700130 rtc::CritScope lock(&crit_);
131 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200132 event_log_->Log(rtc::MakeUnique<RtcEventAudioSendStreamConfig>(
133 rtc::MakeUnique<webrtc::rtclog::StreamConfig>(config)));
ivoce0928d82016-10-10 05:12:51 -0700134 }
135 }
136
Bjorn Terelius440216f2017-09-29 21:01:42 +0200137 void LogIncomingRtpHeader(const RtpPacketReceived& packet) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700138 rtc::CritScope lock(&crit_);
139 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200140 event_log_->Log(rtc::MakeUnique<RtcEventRtpPacketIncoming>(packet));
ivoc14d5dbe2016-07-04 07:06:55 -0700141 }
142 }
143
Bjorn Terelius440216f2017-09-29 21:01:42 +0200144 void LogOutgoingRtpHeader(const RtpPacketToSend& packet,
145 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700146 rtc::CritScope lock(&crit_);
147 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200148 event_log_->Log(
149 rtc::MakeUnique<RtcEventRtpPacketOutgoing>(packet, probe_cluster_id));
Bjorn Terelius440216f2017-09-29 21:01:42 +0200150 }
151 }
152
153 void LogIncomingRtcpPacket(rtc::ArrayView<const uint8_t> packet) override {
154 rtc::CritScope lock(&crit_);
155 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200156 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketIncoming>(packet));
Bjorn Terelius440216f2017-09-29 21:01:42 +0200157 }
158 }
159
160 void LogOutgoingRtcpPacket(rtc::ArrayView<const uint8_t> packet) override {
161 rtc::CritScope lock(&crit_);
162 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200163 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketOutgoing>(packet));
ivoc14d5dbe2016-07-04 07:06:55 -0700164 }
165 }
166
167 void LogAudioPlayout(uint32_t ssrc) override {
168 rtc::CritScope lock(&crit_);
169 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200170 event_log_->Log(rtc::MakeUnique<RtcEventAudioPlayout>(ssrc));
ivoc14d5dbe2016-07-04 07:06:55 -0700171 }
172 }
173
terelius424e6cf2017-02-20 05:14:41 -0800174 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700175 uint8_t fraction_loss,
176 int32_t total_packets) override {
177 rtc::CritScope lock(&crit_);
178 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200179 event_log_->Log(rtc::MakeUnique<RtcEventBweUpdateLossBased>(
180 bitrate_bps, fraction_loss, total_packets));
ivoc14d5dbe2016-07-04 07:06:55 -0700181 }
182 }
183
terelius424e6cf2017-02-20 05:14:41 -0800184 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800185 BandwidthUsage detector_state) override {
186 rtc::CritScope lock(&crit_);
187 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200188 event_log_->Log(rtc::MakeUnique<RtcEventBweUpdateDelayBased>(
189 bitrate_bps, detector_state));
terelius0baf55d2017-02-17 03:38:28 -0800190 }
191 }
192
minyue4b7c9522017-01-24 04:54:59 -0800193 void LogAudioNetworkAdaptation(
michaeltcde46b72017-04-06 05:59:10 -0700194 const AudioEncoderRuntimeConfig& config) override {
minyue4b7c9522017-01-24 04:54:59 -0800195 rtc::CritScope lock(&crit_);
196 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200197 event_log_->Log(rtc::MakeUnique<RtcEventAudioNetworkAdaptation>(
198 rtc::MakeUnique<AudioEncoderRuntimeConfig>(config)));
minyue4b7c9522017-01-24 04:54:59 -0800199 }
200 }
201
philipel32d00102017-02-27 02:18:46 -0800202 void LogProbeClusterCreated(int id,
203 int bitrate_bps,
204 int min_probes,
205 int min_bytes) override {
206 rtc::CritScope lock(&crit_);
207 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200208 event_log_->Log(rtc::MakeUnique<RtcEventProbeClusterCreated>(
209 id, bitrate_bps, min_probes, min_bytes));
philipel32d00102017-02-27 02:18:46 -0800210 }
211 };
212
213 void LogProbeResultSuccess(int id, int bitrate_bps) override {
214 rtc::CritScope lock(&crit_);
215 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200216 event_log_->Log(
217 rtc::MakeUnique<RtcEventProbeResultSuccess>(id, bitrate_bps));
philipel32d00102017-02-27 02:18:46 -0800218 }
219 };
220
221 void LogProbeResultFailure(int id,
222 ProbeFailureReason failure_reason) override {
223 rtc::CritScope lock(&crit_);
224 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200225 event_log_->Log(
226 rtc::MakeUnique<RtcEventProbeResultFailure>(id, failure_reason));
philipel32d00102017-02-27 02:18:46 -0800227 }
228 };
229
ivoc14d5dbe2016-07-04 07:06:55 -0700230 void SetEventLog(RtcEventLog* event_log) {
231 rtc::CritScope lock(&crit_);
232 event_log_ = event_log;
233 }
234
235 private:
236 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700237 RtcEventLog* event_log_ RTC_GUARDED_BY(crit_);
ivoc14d5dbe2016-07-04 07:06:55 -0700238 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
239};
240
michaelt9332b7d2016-11-30 07:51:13 -0800241class RtcpRttStatsProxy final : public RtcpRttStats {
242 public:
243 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
244
245 void OnRttUpdate(int64_t rtt) override {
246 rtc::CritScope lock(&crit_);
247 if (rtcp_rtt_stats_)
248 rtcp_rtt_stats_->OnRttUpdate(rtt);
249 }
250
251 int64_t LastProcessedRtt() const override {
252 rtc::CritScope lock(&crit_);
253 if (!rtcp_rtt_stats_)
254 return 0;
255 return rtcp_rtt_stats_->LastProcessedRtt();
256 }
257
258 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
259 rtc::CritScope lock(&crit_);
260 rtcp_rtt_stats_ = rtcp_rtt_stats;
261 }
262
263 private:
264 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700265 RtcpRttStats* rtcp_rtt_stats_ RTC_GUARDED_BY(crit_);
michaelt9332b7d2016-11-30 07:51:13 -0800266 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
267};
268
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100269class TransportFeedbackProxy : public TransportFeedbackObserver {
270 public:
271 TransportFeedbackProxy() : feedback_observer_(nullptr) {
272 pacer_thread_.DetachFromThread();
273 network_thread_.DetachFromThread();
274 }
275
276 void SetTransportFeedbackObserver(
277 TransportFeedbackObserver* feedback_observer) {
278 RTC_DCHECK(thread_checker_.CalledOnValidThread());
279 rtc::CritScope lock(&crit_);
280 feedback_observer_ = feedback_observer;
281 }
282
283 // Implements TransportFeedbackObserver.
elad.alond12a8e12017-03-23 11:04:48 -0700284 void AddPacket(uint32_t ssrc,
285 uint16_t sequence_number,
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100286 size_t length,
philipel8aadd502017-02-23 02:56:13 -0800287 const PacedPacketInfo& pacing_info) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100288 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
289 rtc::CritScope lock(&crit_);
290 if (feedback_observer_)
elad.alond12a8e12017-03-23 11:04:48 -0700291 feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100292 }
philipel8aadd502017-02-23 02:56:13 -0800293
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100294 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
295 RTC_DCHECK(network_thread_.CalledOnValidThread());
296 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700297 if (feedback_observer_)
298 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200299 }
elad.alonf9490002017-03-06 05:32:21 -0800300 std::vector<PacketFeedback> GetTransportFeedbackVector() const override {
Stefan Holmer60e43462016-09-07 09:58:20 +0200301 RTC_NOTREACHED();
elad.alonf9490002017-03-06 05:32:21 -0800302 return std::vector<PacketFeedback>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100303 }
304
305 private:
306 rtc::CriticalSection crit_;
307 rtc::ThreadChecker thread_checker_;
308 rtc::ThreadChecker pacer_thread_;
309 rtc::ThreadChecker network_thread_;
danilchapa37de392017-09-09 04:17:22 -0700310 TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100311};
312
313class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
314 public:
315 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
316 pacer_thread_.DetachFromThread();
317 }
318
319 void SetSequenceNumberAllocator(
320 TransportSequenceNumberAllocator* seq_num_allocator) {
321 RTC_DCHECK(thread_checker_.CalledOnValidThread());
322 rtc::CritScope lock(&crit_);
323 seq_num_allocator_ = seq_num_allocator;
324 }
325
326 // Implements TransportSequenceNumberAllocator.
327 uint16_t AllocateSequenceNumber() override {
328 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
329 rtc::CritScope lock(&crit_);
330 if (!seq_num_allocator_)
331 return 0;
332 return seq_num_allocator_->AllocateSequenceNumber();
333 }
334
335 private:
336 rtc::CriticalSection crit_;
337 rtc::ThreadChecker thread_checker_;
338 rtc::ThreadChecker pacer_thread_;
danilchapa37de392017-09-09 04:17:22 -0700339 TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100340};
341
342class RtpPacketSenderProxy : public RtpPacketSender {
343 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800344 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100345
346 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
347 RTC_DCHECK(thread_checker_.CalledOnValidThread());
348 rtc::CritScope lock(&crit_);
349 rtp_packet_sender_ = rtp_packet_sender;
350 }
351
352 // Implements RtpPacketSender.
353 void InsertPacket(Priority priority,
354 uint32_t ssrc,
355 uint16_t sequence_number,
356 int64_t capture_time_ms,
357 size_t bytes,
358 bool retransmission) override {
359 rtc::CritScope lock(&crit_);
360 if (rtp_packet_sender_) {
361 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
362 capture_time_ms, bytes, retransmission);
363 }
364 }
365
366 private:
367 rtc::ThreadChecker thread_checker_;
368 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700369 RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100370};
371
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000372class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000373 public:
stefan7de8d642017-02-07 07:14:08 -0800374 explicit VoERtcpObserver(Channel* owner)
375 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000376 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000377
stefan7de8d642017-02-07 07:14:08 -0800378 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
379 rtc::CritScope lock(&crit_);
380 bandwidth_observer_ = bandwidth_observer;
381 }
382
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000383 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800384 rtc::CritScope lock(&crit_);
385 if (bandwidth_observer_) {
386 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
387 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000388 }
389
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000390 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
391 int64_t rtt,
392 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800393 {
394 rtc::CritScope lock(&crit_);
395 if (bandwidth_observer_) {
396 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
397 now_ms);
398 }
399 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000400 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
401 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
402 // report for VoiceEngine?
403 if (report_blocks.empty())
404 return;
405
406 int fraction_lost_aggregate = 0;
407 int total_number_of_packets = 0;
408
409 // If receiving multiple report blocks, calculate the weighted average based
410 // on the number of packets a report refers to.
411 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
412 block_it != report_blocks.end(); ++block_it) {
413 // Find the previous extended high sequence number for this remote SSRC,
414 // to calculate the number of RTP packets this report refers to. Ignore if
415 // we haven't seen this SSRC before.
416 std::map<uint32_t, uint32_t>::iterator seq_num_it =
srte3e69e5c2017-08-09 06:13:45 -0700417 extended_max_sequence_number_.find(block_it->source_ssrc);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000418 int number_of_packets = 0;
419 if (seq_num_it != extended_max_sequence_number_.end()) {
srte3e69e5c2017-08-09 06:13:45 -0700420 number_of_packets =
421 block_it->extended_highest_sequence_number - seq_num_it->second;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000422 }
srte3e69e5c2017-08-09 06:13:45 -0700423 fraction_lost_aggregate += number_of_packets * block_it->fraction_lost;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000424 total_number_of_packets += number_of_packets;
425
srte3e69e5c2017-08-09 06:13:45 -0700426 extended_max_sequence_number_[block_it->source_ssrc] =
427 block_it->extended_highest_sequence_number;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000428 }
429 int weighted_fraction_lost = 0;
430 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800431 weighted_fraction_lost =
432 (fraction_lost_aggregate + total_number_of_packets / 2) /
433 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000434 }
elad.alond12a8e12017-03-23 11:04:48 -0700435 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000436 }
437
438 private:
439 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000440 // Maps remote side ssrc to extended highest sequence number received.
441 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800442 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700443 RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000444};
445
henrikaec6fbd22017-03-31 05:43:36 -0700446class Channel::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
447 public:
448 ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
449 Channel* channel)
450 : audio_frame_(std::move(audio_frame)), channel_(channel) {
451 RTC_DCHECK(channel_);
452 }
453
454 private:
455 bool Run() override {
456 RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
457 channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
458 return true;
459 }
460
461 std::unique_ptr<AudioFrame> audio_frame_;
462 Channel* const channel_;
463};
464
kwiberg55b97fe2016-01-28 05:22:45 -0800465int32_t Channel::SendData(FrameType frameType,
466 uint8_t payloadType,
467 uint32_t timeStamp,
468 const uint8_t* payloadData,
469 size_t payloadSize,
470 const RTPFragmentationHeader* fragmentation) {
henrikaec6fbd22017-03-31 05:43:36 -0700471 RTC_DCHECK_RUN_ON(encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800472 if (_includeAudioLevelIndication) {
473 // Store current audio level in the RTP/RTCP module.
474 // The level will be used in combination with voice-activity state
475 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800476 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800477 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
kwiberg55b97fe2016-01-28 05:22:45 -0800479 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
480 // packetization.
481 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700482 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800483 (FrameType&)frameType, payloadType, timeStamp,
484 // Leaving the time when this frame was
485 // received from the capture device as
486 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700487 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
solenberg1c239d42017-09-29 06:00:28 -0700488 LOG(LS_ERROR) <<
489 "Channel::SendData() failed to send data to RTP/RTCP module";
kwiberg55b97fe2016-01-28 05:22:45 -0800490 return -1;
491 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000494}
495
stefan1d8a5062015-10-02 03:39:33 -0700496bool Channel::SendRtp(const uint8_t* data,
497 size_t len,
498 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800499 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000500
kwiberg55b97fe2016-01-28 05:22:45 -0800501 if (_transportPtr == NULL) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200502 LOG(LS_ERROR) << "Channel::SendPacket() failed to send RTP packet due to"
503 << " invalid transport object";
kwiberg55b97fe2016-01-28 05:22:45 -0800504 return false;
505 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000506
kwiberg55b97fe2016-01-28 05:22:45 -0800507 uint8_t* bufferToSendPtr = (uint8_t*)data;
508 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000509
kwiberg55b97fe2016-01-28 05:22:45 -0800510 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
solenberg1c239d42017-09-29 06:00:28 -0700511 LOG(LS_ERROR) << "Channel::SendPacket() RTP transmission failed";
kwiberg55b97fe2016-01-28 05:22:45 -0800512 return false;
513 }
514 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000515}
516
kwiberg55b97fe2016-01-28 05:22:45 -0800517bool Channel::SendRtcp(const uint8_t* data, size_t len) {
kwiberg55b97fe2016-01-28 05:22:45 -0800518 rtc::CritScope cs(&_callbackCritSect);
519 if (_transportPtr == NULL) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200520 LOG(LS_ERROR) << "Channel::SendRtcp() failed to send RTCP packet due to"
521 << " invalid transport object";
kwiberg55b97fe2016-01-28 05:22:45 -0800522 return false;
523 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000524
kwiberg55b97fe2016-01-28 05:22:45 -0800525 uint8_t* bufferToSendPtr = (uint8_t*)data;
526 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000527
kwiberg55b97fe2016-01-28 05:22:45 -0800528 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
529 if (n < 0) {
solenberg1c239d42017-09-29 06:00:28 -0700530 LOG(LS_ERROR) << "Channel::SendRtcp() transmission failed";
kwiberg55b97fe2016-01-28 05:22:45 -0800531 return false;
532 }
533 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000534}
535
kwiberg55b97fe2016-01-28 05:22:45 -0800536void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
kwiberg55b97fe2016-01-28 05:22:45 -0800537 // Update ssrc so that NTP for AV sync can be updated.
538 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000539}
540
Peter Boströmac547a62015-09-17 23:03:57 +0200541void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200542 // TODO(saza): remove.
niklase@google.com470e71d2011-07-07 08:21:25 +0000543}
544
Peter Boströmac547a62015-09-17 23:03:57 +0200545int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000546 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000547 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000548 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800549 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200550 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800551 CodecInst receiveCodec = {0};
552 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000553
kwiberg55b97fe2016-01-28 05:22:45 -0800554 receiveCodec.pltype = payloadType;
555 receiveCodec.plfreq = frequency;
556 receiveCodec.channels = channels;
557 receiveCodec.rate = rate;
558 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000559
kwiberg55b97fe2016-01-28 05:22:45 -0800560 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
561 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000562
kwiberg55b97fe2016-01-28 05:22:45 -0800563 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700564 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
565 CodecInstToSdp(receiveCodec))) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200566 LOG(LS_WARNING) << "Channel::OnInitializeDecoder() invalid codec (pt="
567 << payloadType << ", name=" << payloadName
568 << ") received - 1";
kwiberg55b97fe2016-01-28 05:22:45 -0800569 return -1;
570 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000571
kwiberg55b97fe2016-01-28 05:22:45 -0800572 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000573}
574
kwiberg55b97fe2016-01-28 05:22:45 -0800575int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
576 size_t payloadSize,
577 const WebRtcRTPHeader* rtpHeader) {
kwiberg55b97fe2016-01-28 05:22:45 -0800578 if (!channel_state_.Get().playing) {
579 // Avoid inserting into NetEQ when we are not playing. Count the
580 // packet as discarded.
niklase@google.com470e71d2011-07-07 08:21:25 +0000581 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800582 }
583
584 // Push the incoming payload (parsed and ready for decoding) into the ACM
585 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
586 0) {
solenberg1c239d42017-09-29 06:00:28 -0700587 LOG(LS_ERROR) <<
588 "Channel::OnReceivedPayloadData() unable to push data to the ACM";
kwiberg55b97fe2016-01-28 05:22:45 -0800589 return -1;
590 }
591
kwiberg55b97fe2016-01-28 05:22:45 -0800592 int64_t round_trip_time = 0;
593 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
594 NULL);
595
596 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
597 if (!nack_list.empty()) {
598 // Can't use nack_list.data() since it's not supported by all
599 // compilers.
600 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
601 }
602 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000603}
604
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000605bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000606 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000607 RTPHeader header;
608 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200609 LOG(LS_WARNING) << "IncomingPacket invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000610 return false;
611 }
612 header.payload_type_frequency =
613 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
614 if (header.payload_type_frequency < 0)
615 return false;
616 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
617}
618
solenberg2397b9a2017-09-22 06:48:10 -0700619AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
620 int sample_rate_hz,
621 AudioFrame* audio_frame) {
622 audio_frame->sample_rate_hz_ = sample_rate_hz;
623
ivoc14d5dbe2016-07-04 07:06:55 -0700624 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800625 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
Elad Alon4a87e1c2017-10-03 16:11:34 +0200626 event_log_proxy_->Log(rtc::MakeUnique<RtcEventAudioPlayout>(ssrc));
kwiberg55b97fe2016-01-28 05:22:45 -0800627 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700628 bool muted;
solenberg2397b9a2017-09-22 06:48:10 -0700629 if (audio_coding_->PlayoutData10Ms(audio_frame->sample_rate_hz_, audio_frame,
henrik.lundind4ccb002016-05-17 12:21:55 -0700630 &muted) == -1) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200631 LOG(LS_ERROR) << "Channel::GetAudioFrame() PlayoutData10Ms() failed!";
kwiberg55b97fe2016-01-28 05:22:45 -0800632 // In all likelihood, the audio in this frame is garbage. We return an
633 // error so that the audio mixer module doesn't add it to the mix. As
634 // a result, it won't be played out and the actions skipped here are
635 // irrelevant.
solenberg2397b9a2017-09-22 06:48:10 -0700636 return AudioMixer::Source::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800637 }
henrik.lundina89ab962016-05-18 08:52:45 -0700638
639 if (muted) {
640 // TODO(henrik.lundin): We should be able to do better than this. But we
641 // will have to go through all the cases below where the audio samples may
642 // be used, and handle the muted case in some way.
solenberg2397b9a2017-09-22 06:48:10 -0700643 AudioFrameOperations::Mute(audio_frame);
henrik.lundina89ab962016-05-18 08:52:45 -0700644 }
kwiberg55b97fe2016-01-28 05:22:45 -0800645
kwiberg55b97fe2016-01-28 05:22:45 -0800646 // Store speech type for dead-or-alive detection
solenberg2397b9a2017-09-22 06:48:10 -0700647 _outputSpeechType = audio_frame->speech_type_;
kwiberg55b97fe2016-01-28 05:22:45 -0800648
kwiberg55b97fe2016-01-28 05:22:45 -0800649 {
650 // Pass the audio buffers to an optional sink callback, before applying
651 // scaling/panning, as that applies to the mix operation.
652 // External recipients of the audio (e.g. via AudioTrack), will do their
653 // own mixing/dynamic processing.
654 rtc::CritScope cs(&_callbackCritSect);
655 if (audio_sink_) {
656 AudioSinkInterface::Data data(
solenberg2397b9a2017-09-22 06:48:10 -0700657 audio_frame->data(), audio_frame->samples_per_channel_,
658 audio_frame->sample_rate_hz_, audio_frame->num_channels_,
659 audio_frame->timestamp_);
kwiberg55b97fe2016-01-28 05:22:45 -0800660 audio_sink_->OnData(data);
661 }
662 }
663
664 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800665 {
666 rtc::CritScope cs(&volume_settings_critsect_);
667 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800668 }
669
670 // Output volume scaling
671 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800672 // TODO(solenberg): Combine with mute state - this can cause clicks!
solenberg2397b9a2017-09-22 06:48:10 -0700673 AudioFrameOperations::ScaleWithSat(output_gain, audio_frame);
kwiberg55b97fe2016-01-28 05:22:45 -0800674 }
675
kwiberg55b97fe2016-01-28 05:22:45 -0800676 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700677 // TODO(henrik.lundin) Use the |muted| information here too.
zstein3c451862017-07-20 09:57:42 -0700678 // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see
zsteine76bd3a2017-07-14 12:17:49 -0700679 // https://crbug.com/webrtc/7517).
solenberg2397b9a2017-09-22 06:48:10 -0700680 _outputAudioLevel.ComputeLevel(*audio_frame, kAudioSampleDurationSeconds);
kwiberg55b97fe2016-01-28 05:22:45 -0800681
solenberg2397b9a2017-09-22 06:48:10 -0700682 if (capture_start_rtp_time_stamp_ < 0 && audio_frame->timestamp_ != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800683 // The first frame with a valid rtp timestamp.
solenberg2397b9a2017-09-22 06:48:10 -0700684 capture_start_rtp_time_stamp_ = audio_frame->timestamp_;
kwiberg55b97fe2016-01-28 05:22:45 -0800685 }
686
687 if (capture_start_rtp_time_stamp_ >= 0) {
solenberg2397b9a2017-09-22 06:48:10 -0700688 // audio_frame.timestamp_ should be valid from now on.
kwiberg55b97fe2016-01-28 05:22:45 -0800689
690 // Compute elapsed time.
691 int64_t unwrap_timestamp =
solenberg2397b9a2017-09-22 06:48:10 -0700692 rtp_ts_wraparound_handler_->Unwrap(audio_frame->timestamp_);
693 audio_frame->elapsed_time_ms_ =
kwiberg55b97fe2016-01-28 05:22:45 -0800694 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700695 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800696
niklase@google.com470e71d2011-07-07 08:21:25 +0000697 {
kwiberg55b97fe2016-01-28 05:22:45 -0800698 rtc::CritScope lock(&ts_stats_lock_);
699 // Compute ntp time.
solenberg2397b9a2017-09-22 06:48:10 -0700700 audio_frame->ntp_time_ms_ =
701 ntp_estimator_.Estimate(audio_frame->timestamp_);
kwiberg55b97fe2016-01-28 05:22:45 -0800702 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
solenberg2397b9a2017-09-22 06:48:10 -0700703 if (audio_frame->ntp_time_ms_ > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800704 // Compute |capture_start_ntp_time_ms_| so that
705 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
706 capture_start_ntp_time_ms_ =
solenberg2397b9a2017-09-22 06:48:10 -0700707 audio_frame->ntp_time_ms_ - audio_frame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000708 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000709 }
kwiberg55b97fe2016-01-28 05:22:45 -0800710 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
solenberg2397b9a2017-09-22 06:48:10 -0700712 return muted ? AudioMixer::Source::AudioFrameInfo::kMuted
713 : AudioMixer::Source::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000714}
715
solenberg2397b9a2017-09-22 06:48:10 -0700716int Channel::PreferredSampleRate() const {
kwiberg55b97fe2016-01-28 05:22:45 -0800717 // Return the bigger of playout and receive frequency in the ACM.
solenberg2397b9a2017-09-22 06:48:10 -0700718 return std::max(audio_coding_->ReceiveFrequency(),
719 audio_coding_->PlayoutFrequency());
niklase@google.com470e71d2011-07-07 08:21:25 +0000720}
721
henrikaec6fbd22017-03-31 05:43:36 -0700722int32_t Channel::CreateChannel(Channel*& channel,
723 int32_t channelId,
724 uint32_t instanceId,
725 const VoEBase::ChannelConfig& config) {
solenberg88499ec2016-09-07 07:34:41 -0700726 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800727 if (channel == NULL) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200728 LOG(LS_ERROR) << "unable to allocate memory for new channel";
kwiberg55b97fe2016-01-28 05:22:45 -0800729 return -1;
730 }
731 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000732}
733
pbos@webrtc.org92135212013-05-14 08:31:39 +0000734Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000735 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700736 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800737 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100738 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700739 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800740 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100741 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800742 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100743 rtp_receive_statistics_(
744 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
745 rtp_receiver_(
746 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100747 this,
748 this,
749 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700750 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100751 _outputAudioLevel(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100752 _timeStamp(0), // This is just an offset, RTP module will add it's own
753 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100754 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100755 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100756 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100757 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100758 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
759 capture_start_rtp_time_stamp_(-1),
760 capture_start_ntp_time_ms_(-1),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100761 _moduleProcessThreadPtr(NULL),
762 _audioDeviceModulePtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100763 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700764 input_mute_(false),
765 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100766 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100767 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800768 transport_overhead_per_packet_(0),
769 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100770 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100771 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100772 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700773 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800774 feedback_observer_proxy_(new TransportFeedbackProxy()),
775 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700776 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200777 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
778 kMaxRetransmissionWindowMs)),
elad.alond12a8e12017-03-23 11:04:48 -0700779 decoder_factory_(config.acm_config.decoder_factory),
elad.alon28770482017-03-28 05:03:55 -0700780 use_twcc_plr_for_ana_(
781 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled") {
solenberg88499ec2016-09-07 07:34:41 -0700782 AudioCodingModule::Config acm_config(config.acm_config);
henrik.lundina89ab962016-05-18 08:52:45 -0700783 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800784 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200785
kwiberg55b97fe2016-01-28 05:22:45 -0800786 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000787
kwiberg55b97fe2016-01-28 05:22:45 -0800788 RtpRtcp::Configuration configuration;
789 configuration.audio = true;
790 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800791 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800792 configuration.receive_statistics = rtp_receive_statistics_.get();
793 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800794 if (pacing_enabled_) {
795 configuration.paced_sender = rtp_packet_sender_proxy_.get();
796 configuration.transport_sequence_number_allocator =
797 seq_num_allocator_proxy_.get();
798 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
799 }
ivoc14d5dbe2016-07-04 07:06:55 -0700800 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800801 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200802 configuration.retransmission_rate_limiter =
803 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000804
kwiberg55b97fe2016-01-28 05:22:45 -0800805 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100806 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000807}
808
kwiberg55b97fe2016-01-28 05:22:45 -0800809Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700810 RTC_DCHECK(!channel_state_.Get().sending);
811 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000812}
813
kwiberg55b97fe2016-01-28 05:22:45 -0800814int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700815 RTC_DCHECK(construction_thread_.CalledOnValidThread());
niklase@google.com470e71d2011-07-07 08:21:25 +0000816
kwiberg55b97fe2016-01-28 05:22:45 -0800817 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000818
kwiberg55b97fe2016-01-28 05:22:45 -0800819 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000820
solenberg1c239d42017-09-29 06:00:28 -0700821 if (_moduleProcessThreadPtr == NULL) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200822 LOG(LS_ERROR) << "Channel::Init() must call SetEngineInformation() first";
kwiberg55b97fe2016-01-28 05:22:45 -0800823 return -1;
824 }
825
826 // --- Add modules to process thread (for periodic schedulation)
827
tommidea489f2017-03-03 03:20:24 -0800828 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800829
830 // --- ACM initialization
831
832 if (audio_coding_->InitializeReceiver() == -1) {
solenberg1c239d42017-09-29 06:00:28 -0700833 LOG(LS_ERROR) << "Channel::Init() unable to initialize the ACM - 1";
kwiberg55b97fe2016-01-28 05:22:45 -0800834 return -1;
835 }
836
837 // --- RTP/RTCP module initialization
838
839 // Ensure that RTCP is enabled by default for the created channel.
840 // Note that, the module will keep generating RTCP until it is explicitly
841 // disabled by the user.
842 // After StopListen (when no sockets exists), RTCP packets will no longer
843 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700844 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800845 // RTCP is enabled by default.
846 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
847 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -0800848 if (audio_coding_->RegisterTransportCallback(this) == -1) {
solenberg1c239d42017-09-29 06:00:28 -0700849 LOG(LS_ERROR) << "Channel::Init() callbacks not registered";
kwiberg55b97fe2016-01-28 05:22:45 -0800850 return -1;
851 }
852
kwiberg1c07c702017-03-27 07:15:49 -0700853 return 0;
854}
855
tommi0a2391f2017-03-21 02:31:51 -0700856void Channel::Terminate() {
857 RTC_DCHECK(construction_thread_.CalledOnValidThread());
858 // Must be called on the same thread as Init().
tommi0a2391f2017-03-21 02:31:51 -0700859 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
860
861 StopSend();
862 StopPlayout();
863
tommi0a2391f2017-03-21 02:31:51 -0700864 // The order to safely shutdown modules in a channel is:
865 // 1. De-register callbacks in modules
866 // 2. De-register modules in process thread
867 // 3. Destroy modules
868 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200869 LOG(LS_WARNING) << "Terminate() failed to de-register transport callback"
870 << " (Audio coding module)";
tommi0a2391f2017-03-21 02:31:51 -0700871 }
872
tommi0a2391f2017-03-21 02:31:51 -0700873 // De-register modules in process thread
874 if (_moduleProcessThreadPtr)
875 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
876
877 // End of modules shutdown
878}
879
solenberg1c239d42017-09-29 06:00:28 -0700880int32_t Channel::SetEngineInformation(ProcessThread& moduleProcessThread,
kwiberg55b97fe2016-01-28 05:22:45 -0800881 AudioDeviceModule& audioDeviceModule,
henrikaec6fbd22017-03-31 05:43:36 -0700882 rtc::TaskQueue* encoder_queue) {
883 RTC_DCHECK(encoder_queue);
884 RTC_DCHECK(!encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800885 _moduleProcessThreadPtr = &moduleProcessThread;
886 _audioDeviceModulePtr = &audioDeviceModule;
henrikaec6fbd22017-03-31 05:43:36 -0700887 encoder_queue_ = encoder_queue;
kwiberg55b97fe2016-01-28 05:22:45 -0800888 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000889}
890
kwibergb7f89d62016-02-17 10:04:18 -0800891void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -0800892 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -0800893 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +0100894}
895
ossu29b1a8d2016-06-13 07:34:51 -0700896const rtc::scoped_refptr<AudioDecoderFactory>&
897Channel::GetAudioDecoderFactory() const {
898 return decoder_factory_;
899}
900
kwiberg55b97fe2016-01-28 05:22:45 -0800901int32_t Channel::StartPlayout() {
kwiberg55b97fe2016-01-28 05:22:45 -0800902 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000903 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800904 }
905
kwiberg55b97fe2016-01-28 05:22:45 -0800906 channel_state_.SetPlaying(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800907
908 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000909}
910
kwiberg55b97fe2016-01-28 05:22:45 -0800911int32_t Channel::StopPlayout() {
kwiberg55b97fe2016-01-28 05:22:45 -0800912 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000913 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800914 }
915
kwiberg55b97fe2016-01-28 05:22:45 -0800916 channel_state_.SetPlaying(false);
917 _outputAudioLevel.Clear();
918
919 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000920}
921
kwiberg55b97fe2016-01-28 05:22:45 -0800922int32_t Channel::StartSend() {
kwiberg55b97fe2016-01-28 05:22:45 -0800923 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800925 }
926 channel_state_.SetSending(true);
henrika4515fa02017-05-03 08:30:15 -0700927 {
928 // It is now OK to start posting tasks to the encoder task queue.
929 rtc::CritScope cs(&encoder_queue_lock_);
930 encoder_queue_is_active_ = true;
931 }
solenberg08b19df2017-02-15 00:42:31 -0800932 // Resume the previous sequence number which was reset by StopSend(). This
933 // needs to be done before |sending| is set to true on the RTP/RTCP module.
934 if (send_sequence_number_) {
935 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
936 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100937 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800938 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
solenberg1c239d42017-09-29 06:00:28 -0700939 LOG(LS_ERROR) << "StartSend() RTP/RTCP failed to start sending";
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100940 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -0800941 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000942 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -0800943 return -1;
944 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +0000945
kwiberg55b97fe2016-01-28 05:22:45 -0800946 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000947}
948
henrikaec6fbd22017-03-31 05:43:36 -0700949void Channel::StopSend() {
kwiberg55b97fe2016-01-28 05:22:45 -0800950 if (!channel_state_.Get().sending) {
henrikaec6fbd22017-03-31 05:43:36 -0700951 return;
kwiberg55b97fe2016-01-28 05:22:45 -0800952 }
953 channel_state_.SetSending(false);
954
henrikaec6fbd22017-03-31 05:43:36 -0700955 // Post a task to the encoder thread which sets an event when the task is
956 // executed. We know that no more encoding tasks will be added to the task
957 // queue for this channel since sending is now deactivated. It means that,
958 // if we wait for the event to bet set, we know that no more pending tasks
959 // exists and it is therfore guaranteed that the task queue will never try
960 // to acccess and invalid channel object.
961 RTC_DCHECK(encoder_queue_);
henrika4515fa02017-05-03 08:30:15 -0700962
henrikaec6fbd22017-03-31 05:43:36 -0700963 rtc::Event flush(false, false);
henrika4515fa02017-05-03 08:30:15 -0700964 {
965 // Clear |encoder_queue_is_active_| under lock to prevent any other tasks
966 // than this final "flush task" to be posted on the queue.
967 rtc::CritScope cs(&encoder_queue_lock_);
968 encoder_queue_is_active_ = false;
969 encoder_queue_->PostTask([&flush]() { flush.Set(); });
970 }
henrikaec6fbd22017-03-31 05:43:36 -0700971 flush.Wait(rtc::Event::kForever);
972
kwiberg55b97fe2016-01-28 05:22:45 -0800973 // Store the sequence number to be able to pick up the same sequence for
974 // the next StartSend(). This is needed for restarting device, otherwise
975 // it might cause libSRTP to complain about packets being replayed.
976 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
977 // CL is landed. See issue
978 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
979 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
980
981 // Reset sending SSRC and sequence number and triggers direct transmission
982 // of RTCP BYE
983 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
solenberg1c239d42017-09-29 06:00:28 -0700984 LOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending";
kwiberg55b97fe2016-01-28 05:22:45 -0800985 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100986 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000987}
988
ossu1ffbd6c2017-04-06 12:05:04 -0700989bool Channel::SetEncoder(int payload_type,
990 std::unique_ptr<AudioEncoder> encoder) {
991 RTC_DCHECK_GE(payload_type, 0);
992 RTC_DCHECK_LE(payload_type, 127);
ossu76d29f92017-06-09 07:30:13 -0700993 // TODO(ossu): Make CodecInsts up, for now: one for the RTP/RTCP module and
994 // one for for us to keep track of sample rate and number of channels, etc.
995
996 // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
997 // as well as some other things, so we collect this info and send it along.
998 CodecInst rtp_codec;
999 rtp_codec.pltype = payload_type;
1000 strncpy(rtp_codec.plname, "audio", sizeof(rtp_codec.plname));
1001 rtp_codec.plname[sizeof(rtp_codec.plname) - 1] = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001002 // Seems unclear if it should be clock rate or sample rate. CodecInst
1003 // supposedly carries the sample rate, but only clock rate seems sensible to
1004 // send to the RTP/RTCP module.
ossu76d29f92017-06-09 07:30:13 -07001005 rtp_codec.plfreq = encoder->RtpTimestampRateHz();
1006 rtp_codec.pacsize = rtc::CheckedDivExact(
1007 static_cast<int>(encoder->Max10MsFramesInAPacket() * rtp_codec.plfreq),
1008 100);
1009 rtp_codec.channels = encoder->NumChannels();
1010 rtp_codec.rate = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001011
ossu76d29f92017-06-09 07:30:13 -07001012 // For audio encoding we need, instead, the actual sample rate of the codec.
1013 // The rest of the information should be the same.
1014 CodecInst send_codec = rtp_codec;
1015 send_codec.plfreq = encoder->SampleRateHz();
1016 cached_send_codec_.emplace(send_codec);
1017
1018 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
ossu1ffbd6c2017-04-06 12:05:04 -07001019 _rtpRtcpModule->DeRegisterSendPayload(payload_type);
ossu76d29f92017-06-09 07:30:13 -07001020 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001021 LOG(LS_ERROR)
1022 << "SetEncoder() failed to register codec to RTP/RTCP module";
ossu1ffbd6c2017-04-06 12:05:04 -07001023 return false;
1024 }
1025 }
1026
1027 audio_coding_->SetEncoder(std::move(encoder));
ossu20a4b3f2017-04-27 02:08:52 -07001028 codec_manager_.UnsetCodecInst();
ossu1ffbd6c2017-04-06 12:05:04 -07001029 return true;
1030}
1031
ossu20a4b3f2017-04-27 02:08:52 -07001032void Channel::ModifyEncoder(
1033 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
1034 audio_coding_->ModifyEncoder(modifier);
1035}
1036
kwiberg55b97fe2016-01-28 05:22:45 -08001037int32_t Channel::GetSendCodec(CodecInst& codec) {
ossu76d29f92017-06-09 07:30:13 -07001038 if (cached_send_codec_) {
1039 codec = *cached_send_codec_;
1040 return 0;
1041 } else {
ossu20a4b3f2017-04-27 02:08:52 -07001042 const CodecInst* send_codec = codec_manager_.GetCodecInst();
1043 if (send_codec) {
1044 codec = *send_codec;
1045 return 0;
1046 }
1047 }
kwiberg1fd4a4a2015-11-03 11:20:50 -08001048 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001049}
1050
kwiberg55b97fe2016-01-28 05:22:45 -08001051int32_t Channel::GetRecCodec(CodecInst& codec) {
1052 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001053}
1054
kwiberg55b97fe2016-01-28 05:22:45 -08001055int32_t Channel::SetSendCodec(const CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001056 if (!codec_manager_.RegisterEncoder(codec) ||
1057 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001058 LOG(LS_ERROR) << "SetSendCodec() failed to register codec to ACM";
kwiberg55b97fe2016-01-28 05:22:45 -08001059 return -1;
1060 }
1061
1062 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1063 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1064 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001065 LOG(LS_ERROR)
1066 << "SetSendCodec() failed to register codec to RTP/RTCP module";
kwiberg55b97fe2016-01-28 05:22:45 -08001067 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001068 }
kwiberg55b97fe2016-01-28 05:22:45 -08001069 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001070
ossu76d29f92017-06-09 07:30:13 -07001071 cached_send_codec_.reset();
1072
kwiberg55b97fe2016-01-28 05:22:45 -08001073 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001074}
1075
minyue78b4d562016-11-30 04:47:39 -08001076void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
minyue7e304322016-10-12 05:00:55 -07001077 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001078 if (*encoder) {
1079 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001080 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001081 }
1082 });
michaelt566d8202017-01-12 10:17:38 -08001083 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001084}
1085
elad.alond12a8e12017-03-23 11:04:48 -07001086void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1087 if (!use_twcc_plr_for_ana_)
1088 return;
minyue7e304322016-10-12 05:00:55 -07001089 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001090 if (*encoder) {
1091 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1092 }
1093 });
1094}
1095
elad.alondadb4dc2017-03-23 15:29:50 -07001096void Channel::OnRecoverableUplinkPacketLossRate(
1097 float recoverable_packet_loss_rate) {
1098 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1099 if (*encoder) {
1100 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1101 recoverable_packet_loss_rate);
1102 }
1103 });
1104}
1105
elad.alond12a8e12017-03-23 11:04:48 -07001106void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1107 if (use_twcc_plr_for_ana_)
1108 return;
1109 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1110 if (*encoder) {
1111 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1112 }
minyue7e304322016-10-12 05:00:55 -07001113 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001114}
1115
kwiberg1c07c702017-03-27 07:15:49 -07001116void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1117 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1118 audio_coding_->SetReceiveCodecs(codecs);
1119}
1120
minyue7e304322016-10-12 05:00:55 -07001121bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1122 bool success = false;
1123 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1124 if (*encoder) {
michaelt92aef172017-04-18 00:11:48 -07001125 success = (*encoder)->EnableAudioNetworkAdaptor(config_string,
1126 event_log_proxy_.get());
minyue7e304322016-10-12 05:00:55 -07001127 }
1128 });
1129 return success;
1130}
1131
1132void Channel::DisableAudioNetworkAdaptor() {
1133 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1134 if (*encoder)
1135 (*encoder)->DisableAudioNetworkAdaptor();
1136 });
1137}
1138
1139void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1140 int max_frame_length_ms) {
1141 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1142 if (*encoder) {
1143 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1144 max_frame_length_ms);
1145 }
1146 });
1147}
1148
solenberg1c239d42017-09-29 06:00:28 -07001149void Channel::RegisterTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001150 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001151 _transportPtr = transport;
niklase@google.com470e71d2011-07-07 08:21:25 +00001152}
1153
nisse657bab22017-02-21 06:28:10 -08001154void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
nisse657bab22017-02-21 06:28:10 -08001155 RTPHeader header;
1156 packet.GetHeader(&header);
solenberg946d8862017-09-21 04:02:53 -07001157
1158 // Store playout timestamp for the received RTP packet
1159 UpdatePlayoutTimestamp(false);
1160
1161 header.payload_type_frequency =
1162 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
1163 if (header.payload_type_frequency >= 0) {
1164 bool in_order = IsPacketInOrder(header);
1165 rtp_receive_statistics_->IncomingPacket(
1166 header, packet.size(), IsPacketRetransmitted(header, in_order));
1167 rtp_payload_registry_->SetIncomingPayloadType(header);
1168
1169 ReceivePacket(packet.data(), packet.size(), header, in_order);
1170 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001171}
1172
1173bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001174 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001175 const RTPHeader& header,
1176 bool in_order) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001177 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001178 assert(packet_length >= header.headerLength);
1179 size_t payload_length = packet_length - header.headerLength;
Karl Wiberg73b60b82017-09-21 15:00:58 +02001180 const auto pl =
1181 rtp_payload_registry_->PayloadTypeToPayload(header.payloadType);
1182 if (!pl) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001183 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001184 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001185 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
Karl Wiberg73b60b82017-09-21 15:00:58 +02001186 pl->typeSpecific, in_order);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001187}
1188
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001189bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1190 StreamStatistician* statistician =
1191 rtp_receive_statistics_->GetStatistician(header.ssrc);
1192 if (!statistician)
1193 return false;
1194 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001195}
1196
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001197bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1198 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001199 StreamStatistician* statistician =
1200 rtp_receive_statistics_->GetStatistician(header.ssrc);
1201 if (!statistician)
1202 return false;
1203 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001204 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001205 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001206 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001207}
1208
mflodman3d7db262016-04-29 00:57:13 -07001209int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001210 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001211 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001212
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001213 // Deliver RTCP packet to RTP/RTCP module for parsing
nisse479d3d72017-09-13 07:53:37 -07001214 _rtpRtcpModule->IncomingRtcpPacket(data, length);
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001215
Minyue2013aec2015-05-13 14:14:42 +02001216 int64_t rtt = GetRTT(true);
1217 if (rtt == 0) {
1218 // Waiting for valid RTT.
1219 return 0;
1220 }
Erik Språng737336d2016-07-29 12:59:36 +02001221
1222 int64_t nack_window_ms = rtt;
1223 if (nack_window_ms < kMinRetransmissionWindowMs) {
1224 nack_window_ms = kMinRetransmissionWindowMs;
1225 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1226 nack_window_ms = kMaxRetransmissionWindowMs;
1227 }
1228 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1229
minyue7e304322016-10-12 05:00:55 -07001230 // Invoke audio encoders OnReceivedRtt().
1231 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1232 if (*encoder)
1233 (*encoder)->OnReceivedRtt(rtt);
1234 });
1235
Minyue2013aec2015-05-13 14:14:42 +02001236 uint32_t ntp_secs = 0;
1237 uint32_t ntp_frac = 0;
1238 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001239 if (0 !=
1240 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1241 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001242 // Waiting for RTCP.
1243 return 0;
1244 }
1245
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001246 {
tommi31fc21f2016-01-21 10:37:37 -08001247 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001248 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001249 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001250 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001251}
1252
solenberg8d73f8c2017-03-08 01:52:20 -08001253int Channel::GetSpeechOutputLevel() const {
1254 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00001255}
1256
solenberg8d73f8c2017-03-08 01:52:20 -08001257int Channel::GetSpeechOutputLevelFullRange() const {
1258 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08001259}
1260
zsteine76bd3a2017-07-14 12:17:49 -07001261double Channel::GetTotalOutputEnergy() const {
zstein3c451862017-07-20 09:57:42 -07001262 return _outputAudioLevel.TotalEnergy();
zsteine76bd3a2017-07-14 12:17:49 -07001263}
1264
1265double Channel::GetTotalOutputDuration() const {
zstein3c451862017-07-20 09:57:42 -07001266 return _outputAudioLevel.TotalDuration();
zsteine76bd3a2017-07-14 12:17:49 -07001267}
1268
solenberg8d73f8c2017-03-08 01:52:20 -08001269void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08001270 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07001271 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00001272}
1273
solenberg1c2af8e2016-03-24 10:36:00 -07001274bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08001275 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07001276 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001277}
1278
solenberg8d73f8c2017-03-08 01:52:20 -08001279void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08001280 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08001281 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00001282}
1283
solenberg8842c3e2016-03-11 03:06:41 -08001284int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
solenberg8842c3e2016-03-11 03:06:41 -08001285 RTC_DCHECK_LE(0, event);
1286 RTC_DCHECK_GE(255, event);
1287 RTC_DCHECK_LE(0, duration_ms);
1288 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08001289 if (!Sending()) {
1290 return -1;
1291 }
solenberg8842c3e2016-03-11 03:06:41 -08001292 if (_rtpRtcpModule->SendTelephoneEventOutband(
1293 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
solenberg1c239d42017-09-29 06:00:28 -07001294 LOG(LS_ERROR) << "SendTelephoneEventOutband() failed to send event";
kwiberg55b97fe2016-01-28 05:22:45 -08001295 return -1;
1296 }
1297 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001298}
1299
solenbergffbbcac2016-11-17 05:25:37 -08001300int Channel::SetSendTelephoneEventPayloadType(int payload_type,
1301 int payload_frequency) {
solenberg31642aa2016-03-14 08:00:37 -07001302 RTC_DCHECK_LE(0, payload_type);
1303 RTC_DCHECK_GE(127, payload_type);
1304 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07001305 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08001306 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08001307 memcpy(codec.plname, "telephone-event", 16);
1308 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1309 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1310 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
solenberg1c239d42017-09-29 06:00:28 -07001311 LOG(LS_ERROR) << "SetSendTelephoneEventPayloadType() failed to register "
1312 "send payload type";
kwiberg55b97fe2016-01-28 05:22:45 -08001313 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001314 }
kwiberg55b97fe2016-01-28 05:22:45 -08001315 }
kwiberg55b97fe2016-01-28 05:22:45 -08001316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001317}
1318
kwiberg55b97fe2016-01-28 05:22:45 -08001319int Channel::SetLocalSSRC(unsigned int ssrc) {
kwiberg55b97fe2016-01-28 05:22:45 -08001320 if (channel_state_.Get().sending) {
solenberg1c239d42017-09-29 06:00:28 -07001321 LOG(LS_ERROR) << "SetLocalSSRC() already sending";
kwiberg55b97fe2016-01-28 05:22:45 -08001322 return -1;
1323 }
1324 _rtpRtcpModule->SetSSRC(ssrc);
1325 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001326}
1327
kwiberg55b97fe2016-01-28 05:22:45 -08001328int Channel::GetRemoteSSRC(unsigned int& ssrc) {
1329 ssrc = rtp_receiver_->SSRC();
1330 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001331}
1332
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001333int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00001334 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001335 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001336}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00001337
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00001338int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
1339 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08001340 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
1341 if (enable &&
1342 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
1343 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00001344 return -1;
1345 }
1346 return 0;
1347}
1348
Stefan Holmerb86d4e42015-12-07 10:26:18 +01001349void Channel::EnableSendTransportSequenceNumber(int id) {
1350 int ret =
1351 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
1352 RTC_DCHECK_EQ(0, ret);
1353}
1354
stefan3313ec92016-01-21 06:32:43 -08001355void Channel::EnableReceiveTransportSequenceNumber(int id) {
1356 rtp_header_parser_->DeregisterRtpHeaderExtension(
1357 kRtpExtensionTransportSequenceNumber);
1358 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
1359 kRtpExtensionTransportSequenceNumber, id);
1360 RTC_DCHECK(ret);
1361}
1362
stefanbba9dec2016-02-01 04:39:55 -08001363void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07001364 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08001365 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07001366 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
1367 TransportFeedbackObserver* transport_feedback_observer =
1368 transport->transport_feedback_observer();
1369 PacketRouter* packet_router = transport->packet_router();
1370
stefanbba9dec2016-02-01 04:39:55 -08001371 RTC_DCHECK(rtp_packet_sender);
1372 RTC_DCHECK(transport_feedback_observer);
kwibergee89e782017-08-09 17:22:01 -07001373 RTC_DCHECK(packet_router);
1374 RTC_DCHECK(!packet_router_);
stefan7de8d642017-02-07 07:14:08 -08001375 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08001376 feedback_observer_proxy_->SetTransportFeedbackObserver(
1377 transport_feedback_observer);
1378 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
1379 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
1380 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
eladalon822ff2b2017-08-01 06:30:28 -07001381 constexpr bool remb_candidate = false;
1382 packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
Stefan Holmerb86d4e42015-12-07 10:26:18 +01001383 packet_router_ = packet_router;
1384}
1385
stefanbba9dec2016-02-01 04:39:55 -08001386void Channel::RegisterReceiverCongestionControlObjects(
1387 PacketRouter* packet_router) {
kwibergee89e782017-08-09 17:22:01 -07001388 RTC_DCHECK(packet_router);
1389 RTC_DCHECK(!packet_router_);
eladalon822ff2b2017-08-01 06:30:28 -07001390 constexpr bool remb_candidate = false;
1391 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
stefanbba9dec2016-02-01 04:39:55 -08001392 packet_router_ = packet_router;
1393}
1394
nissefdbfdc92017-03-31 05:44:52 -07001395void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08001396 RTC_DCHECK(packet_router_);
1397 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08001398 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08001399 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
1400 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07001401 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08001402 packet_router_ = nullptr;
1403 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
1404}
1405
nissefdbfdc92017-03-31 05:44:52 -07001406void Channel::ResetReceiverCongestionControlObjects() {
1407 RTC_DCHECK(packet_router_);
1408 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
1409 packet_router_ = nullptr;
1410}
1411
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001412void Channel::SetRTCPStatus(bool enable) {
pbosda903ea2015-10-02 02:36:56 -07001413 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00001414}
1415
kwiberg55b97fe2016-01-28 05:22:45 -08001416int Channel::SetRTCP_CNAME(const char cName[256]) {
kwiberg55b97fe2016-01-28 05:22:45 -08001417 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
solenberg1c239d42017-09-29 06:00:28 -07001418 LOG(LS_ERROR) << "SetRTCP_CNAME() failed to set RTCP CNAME";
kwiberg55b97fe2016-01-28 05:22:45 -08001419 return -1;
1420 }
1421 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001422}
1423
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001424int Channel::GetRemoteRTCPReportBlocks(
1425 std::vector<ReportBlock>* report_blocks) {
1426 if (report_blocks == NULL) {
solenberg1c239d42017-09-29 06:00:28 -07001427 LOG(LS_ERROR) << "GetRemoteRTCPReportBlock()s invalid report_blocks.";
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001428 return -1;
1429 }
1430
1431 // Get the report blocks from the latest received RTCP Sender or Receiver
1432 // Report. Each element in the vector contains the sender's SSRC and a
1433 // report block according to RFC 3550.
1434 std::vector<RTCPReportBlock> rtcp_report_blocks;
1435 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001436 return -1;
1437 }
1438
1439 if (rtcp_report_blocks.empty())
1440 return 0;
1441
1442 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
1443 for (; it != rtcp_report_blocks.end(); ++it) {
1444 ReportBlock report_block;
srte3e69e5c2017-08-09 06:13:45 -07001445 report_block.sender_SSRC = it->sender_ssrc;
1446 report_block.source_SSRC = it->source_ssrc;
1447 report_block.fraction_lost = it->fraction_lost;
1448 report_block.cumulative_num_packets_lost = it->packets_lost;
1449 report_block.extended_highest_sequence_number =
1450 it->extended_highest_sequence_number;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001451 report_block.interarrival_jitter = it->jitter;
srte3e69e5c2017-08-09 06:13:45 -07001452 report_block.last_SR_timestamp = it->last_sender_report_timestamp;
1453 report_block.delay_since_last_SR = it->delay_since_last_sender_report;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001454 report_blocks->push_back(report_block);
1455 }
1456 return 0;
1457}
1458
kwiberg55b97fe2016-01-28 05:22:45 -08001459int Channel::GetRTPStatistics(CallStatistics& stats) {
1460 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00001461
kwiberg55b97fe2016-01-28 05:22:45 -08001462 // The jitter statistics is updated for each received RTP packet and is
1463 // based on received packets.
1464 RtcpStatistics statistics;
1465 StreamStatistician* statistician =
1466 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01001467 if (statistician) {
1468 statistician->GetStatistics(&statistics,
1469 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08001470 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001471
kwiberg55b97fe2016-01-28 05:22:45 -08001472 stats.fractionLost = statistics.fraction_lost;
srte186d9c32017-08-04 05:03:53 -07001473 stats.cumulativeLost = statistics.packets_lost;
1474 stats.extendedMax = statistics.extended_highest_sequence_number;
kwiberg55b97fe2016-01-28 05:22:45 -08001475 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00001476
kwiberg55b97fe2016-01-28 05:22:45 -08001477 // --- RTT
1478 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001479
kwiberg55b97fe2016-01-28 05:22:45 -08001480 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00001481
kwiberg55b97fe2016-01-28 05:22:45 -08001482 size_t bytesSent(0);
1483 uint32_t packetsSent(0);
1484 size_t bytesReceived(0);
1485 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001486
kwiberg55b97fe2016-01-28 05:22:45 -08001487 if (statistician) {
1488 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
1489 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001490
kwiberg55b97fe2016-01-28 05:22:45 -08001491 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001492 LOG(LS_WARNING) << "GetRTPStatistics() failed to retrieve RTP datacounters"
1493 << " => output will not be complete";
kwiberg55b97fe2016-01-28 05:22:45 -08001494 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001495
kwiberg55b97fe2016-01-28 05:22:45 -08001496 stats.bytesSent = bytesSent;
1497 stats.packetsSent = packetsSent;
1498 stats.bytesReceived = bytesReceived;
1499 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00001500
kwiberg55b97fe2016-01-28 05:22:45 -08001501 // --- Timestamps
1502 {
1503 rtc::CritScope lock(&ts_stats_lock_);
1504 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
1505 }
1506 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001507}
1508
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00001509void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
1510 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01001511 // If pacing is enabled we always store packets.
1512 if (!pacing_enabled_)
1513 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001514 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00001515 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001516 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00001517 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001518 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00001519}
1520
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00001521// Called when we are missing one or more packets.
1522int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00001523 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
1524}
1525
henrikaec6fbd22017-03-31 05:43:36 -07001526void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
henrika4515fa02017-05-03 08:30:15 -07001527 // Avoid posting any new tasks if sending was already stopped in StopSend().
1528 rtc::CritScope cs(&encoder_queue_lock_);
1529 if (!encoder_queue_is_active_) {
1530 return;
1531 }
henrikaec6fbd22017-03-31 05:43:36 -07001532 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
1533 // TODO(henrika): try to avoid copying by moving ownership of audio frame
1534 // either into pool of frames or into the task itself.
1535 audio_frame->CopyFrom(audio_input);
henrika45802172017-09-28 09:39:34 +02001536 // Profile time between when the audio frame is added to the task queue and
1537 // when the task is actually executed.
1538 audio_frame->UpdateProfileTimeStamp();
henrikaec6fbd22017-03-31 05:43:36 -07001539 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
1540 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00001541}
1542
henrikaec6fbd22017-03-31 05:43:36 -07001543void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
1544 int sample_rate,
1545 size_t number_of_frames,
1546 size_t number_of_channels) {
henrika4515fa02017-05-03 08:30:15 -07001547 // Avoid posting as new task if sending was already stopped in StopSend().
1548 rtc::CritScope cs(&encoder_queue_lock_);
1549 if (!encoder_queue_is_active_) {
1550 return;
1551 }
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00001552 CodecInst codec;
ossu950c1c92017-07-11 08:19:31 -07001553 const int result = GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07001554 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
ossu950c1c92017-07-11 08:19:31 -07001555 // TODO(ossu): Investigate how this could happen. b/62909493
1556 if (result == 0) {
1557 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
1558 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
1559 } else {
1560 audio_frame->sample_rate_hz_ = sample_rate;
1561 audio_frame->num_channels_ = number_of_channels;
1562 LOG(LS_WARNING) << "Unable to get send codec for channel " << ChannelId();
1563 RTC_NOTREACHED();
1564 }
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07001565 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07001566 sample_rate, &input_resampler_, audio_frame.get());
1567 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
1568 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00001569}
1570
henrikaec6fbd22017-03-31 05:43:36 -07001571void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
1572 RTC_DCHECK_RUN_ON(encoder_queue_);
1573 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
1574 RTC_DCHECK_LE(audio_input->num_channels_, 2);
kwiberg55b97fe2016-01-28 05:22:45 -08001575
henrika45802172017-09-28 09:39:34 +02001576 // Measure time between when the audio frame is added to the task queue and
1577 // when the task is actually executed. Goal is to keep track of unwanted
1578 // extra latency added by the task queue.
1579 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs",
1580 audio_input->ElapsedProfileTimeMs());
1581
henrikaec6fbd22017-03-31 05:43:36 -07001582 bool is_muted = InputMute();
1583 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08001584
kwiberg55b97fe2016-01-28 05:22:45 -08001585 if (_includeAudioLevelIndication) {
1586 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07001587 audio_input->samples_per_channel_ * audio_input->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07001588 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
solenberg1c2af8e2016-03-24 10:36:00 -07001589 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08001590 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08001591 } else {
henrik.lundin50499422016-11-29 04:26:24 -08001592 rms_level_.Analyze(
yujo36b1a5f2017-06-12 12:45:32 -07001593 rtc::ArrayView<const int16_t>(audio_input->data(), length));
niklase@google.com470e71d2011-07-07 08:21:25 +00001594 }
kwiberg55b97fe2016-01-28 05:22:45 -08001595 }
solenberg1c2af8e2016-03-24 10:36:00 -07001596 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00001597
henrikaec6fbd22017-03-31 05:43:36 -07001598 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00001599
kwiberg55b97fe2016-01-28 05:22:45 -08001600 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07001601 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08001602 // This call will trigger AudioPacketizationCallback::SendData if encoding
1603 // is done and payload is ready for packetization and transmission.
1604 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07001605 if (audio_coding_->Add10MsData(*audio_input) < 0) {
1606 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
1607 return;
kwiberg55b97fe2016-01-28 05:22:45 -08001608 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001609
henrikaec6fbd22017-03-31 05:43:36 -07001610 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001611}
1612
solenberg7602aab2016-11-14 11:30:07 -08001613void Channel::set_associate_send_channel(const ChannelOwner& channel) {
1614 RTC_DCHECK(!channel.channel() ||
1615 channel.channel()->ChannelId() != _channelId);
1616 rtc::CritScope lock(&assoc_send_channel_lock_);
1617 associate_send_channel_ = channel;
1618}
1619
Minyue2013aec2015-05-13 14:14:42 +02001620void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08001621 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02001622 Channel* channel = associate_send_channel_.channel();
1623 if (channel && channel->ChannelId() == channel_id) {
1624 // If this channel is associated with a send channel of the specified
1625 // Channel ID, disassociate with it.
1626 ChannelOwner ref(NULL);
1627 associate_send_channel_ = ref;
1628 }
1629}
1630
ivoc14d5dbe2016-07-04 07:06:55 -07001631void Channel::SetRtcEventLog(RtcEventLog* event_log) {
1632 event_log_proxy_->SetEventLog(event_log);
1633}
1634
michaelt9332b7d2016-11-30 07:51:13 -08001635void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
1636 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
1637}
1638
nisse284542b2017-01-10 08:58:32 -08001639void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08001640 size_t overhead_per_packet =
1641 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08001642 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1643 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08001644 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08001645 }
1646 });
1647}
1648
1649void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08001650 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08001651 transport_overhead_per_packet_ = transport_overhead_per_packet;
1652 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08001653}
1654
hbos3fd31fe2017-02-28 05:43:16 -08001655// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08001656void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08001657 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08001658 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
1659 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08001660}
1661
kwiberg55b97fe2016-01-28 05:22:45 -08001662int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
1663 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00001664}
1665
wu@webrtc.org24301a62013-12-13 19:17:43 +00001666void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
1667 audio_coding_->GetDecodingCallStatistics(stats);
1668}
1669
ivoce1198e02017-09-08 08:13:19 -07001670ANAStats Channel::GetANAStatistics() const {
1671 return audio_coding_->GetANAStats();
1672}
1673
solenberg358057b2015-11-27 10:46:42 -08001674uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08001675 rtc::CritScope lock(&video_sync_lock_);
1676 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07001677}
1678
kwiberg55b97fe2016-01-28 05:22:45 -08001679int Channel::SetMinimumPlayoutDelay(int delayMs) {
kwiberg55b97fe2016-01-28 05:22:45 -08001680 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
1681 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
solenberg1c239d42017-09-29 06:00:28 -07001682 LOG(LS_ERROR) << "SetMinimumPlayoutDelay() invalid min delay";
kwiberg55b97fe2016-01-28 05:22:45 -08001683 return -1;
1684 }
1685 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
solenberg1c239d42017-09-29 06:00:28 -07001686 LOG(LS_ERROR) << "SetMinimumPlayoutDelay() failed to set min playout delay";
kwiberg55b97fe2016-01-28 05:22:45 -08001687 return -1;
1688 }
1689 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001690}
1691
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001692int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07001693 uint32_t playout_timestamp_rtp = 0;
1694 {
tommi31fc21f2016-01-21 10:37:37 -08001695 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07001696 playout_timestamp_rtp = playout_timestamp_rtp_;
1697 }
kwiberg55b97fe2016-01-28 05:22:45 -08001698 if (playout_timestamp_rtp == 0) {
solenberg1c239d42017-09-29 06:00:28 -07001699 LOG(LS_ERROR) << "GetPlayoutTimestamp() failed to retrieve timestamp";
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001700 return -1;
1701 }
deadbeef74375882015-08-13 12:09:10 -07001702 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001703 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001704}
1705
kwiberg55b97fe2016-01-28 05:22:45 -08001706int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
1707 RtpReceiver** rtp_receiver) const {
1708 *rtpRtcpModule = _rtpRtcpModule.get();
1709 *rtp_receiver = rtp_receiver_.get();
1710 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001711}
1712
deadbeef74375882015-08-13 12:09:10 -07001713void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07001714 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07001715
henrik.lundin96bd5022016-04-06 04:13:56 -07001716 if (!jitter_buffer_playout_timestamp_) {
1717 // This can happen if this channel has not received any RTP packets. In
1718 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07001719 return;
1720 }
1721
1722 uint16_t delay_ms = 0;
1723 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001724 LOG(LS_WARNING) << "Channel::UpdatePlayoutTimestamp() failed to read"
1725 << " playout delay from the ADM";
deadbeef74375882015-08-13 12:09:10 -07001726 return;
1727 }
1728
henrik.lundin96bd5022016-04-06 04:13:56 -07001729 RTC_DCHECK(jitter_buffer_playout_timestamp_);
1730 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07001731
1732 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07001733 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07001734
deadbeef74375882015-08-13 12:09:10 -07001735 {
tommi31fc21f2016-01-21 10:37:37 -08001736 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08001737 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07001738 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07001739 }
1740 playout_delay_ms_ = delay_ms;
1741 }
1742}
1743
kwiberg55b97fe2016-01-28 05:22:45 -08001744void Channel::RegisterReceiveCodecsToRTPModule() {
kwiberg55b97fe2016-01-28 05:22:45 -08001745 CodecInst codec;
1746 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00001747
kwiberg55b97fe2016-01-28 05:22:45 -08001748 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1749 // Open up the RTP/RTCP receiver for all supported codecs
1750 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001751 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001752 LOG(LS_WARNING) << "Channel::RegisterReceiveCodecsToRTPModule() unable"
1753 << " to register " << codec.plname << " (" << codec.pltype
1754 << "/" << codec.plfreq << "/" << codec.channels << "/"
1755 << codec.rate << ") to RTP/RTCP receiver";
niklase@google.com470e71d2011-07-07 08:21:25 +00001756 }
kwiberg55b97fe2016-01-28 05:22:45 -08001757 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001758}
1759
kwiberg55b97fe2016-01-28 05:22:45 -08001760int Channel::SetSendRtpHeaderExtension(bool enable,
1761 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001762 unsigned char id) {
1763 int error = 0;
1764 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
1765 if (enable) {
1766 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
1767 }
1768 return error;
1769}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001770
ossue280cde2016-10-12 11:04:10 -07001771int Channel::GetRtpTimestampRateHz() const {
1772 const auto format = audio_coding_->ReceiveFormat();
1773 // Default to the playout frequency if we've not gotten any packets yet.
1774 // TODO(ossu): Zero clockrate can only happen if we've added an external
1775 // decoder for a format we don't support internally. Remove once that way of
1776 // adding decoders is gone!
1777 return (format && format->clockrate_hz != 0)
1778 ? format->clockrate_hz
1779 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00001780}
1781
Minyue2013aec2015-05-13 14:14:42 +02001782int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07001783 RtcpMode method = _rtpRtcpModule->RTCP();
1784 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001785 return 0;
1786 }
1787 std::vector<RTCPReportBlock> report_blocks;
1788 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02001789
1790 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001791 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02001792 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08001793 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02001794 Channel* channel = associate_send_channel_.channel();
1795 // Tries to get RTT from an associated channel. This is important for
1796 // receive-only channels.
1797 if (channel) {
1798 // To prevent infinite recursion and deadlock, calling GetRTT of
1799 // associate channel should always use "false" for argument:
1800 // |allow_associate_channel|.
1801 rtt = channel->GetRTT(false);
1802 }
1803 }
1804 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001805 }
1806
1807 uint32_t remoteSSRC = rtp_receiver_->SSRC();
1808 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
1809 for (; it != report_blocks.end(); ++it) {
srte3e69e5c2017-08-09 06:13:45 -07001810 if (it->sender_ssrc == remoteSSRC)
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001811 break;
1812 }
1813 if (it == report_blocks.end()) {
1814 // We have not received packets with SSRC matching the report blocks.
1815 // To calculate RTT we try with the SSRC of the first report block.
1816 // This is very important for send-only channels where we don't know
1817 // the SSRC of the other end.
srte3e69e5c2017-08-09 06:13:45 -07001818 remoteSSRC = report_blocks[0].sender_ssrc;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001819 }
Minyue2013aec2015-05-13 14:14:42 +02001820
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001821 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001822 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001823 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001824 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
1825 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001826 return 0;
1827 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001828 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001829}
1830
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001831} // namespace voe
1832} // namespace webrtc