blob: 4ac040bee0d51de31184b1f3817c6bfd626e59b2 [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
84 bool StartLogging(const std::string& file_name,
85 int64_t max_size_bytes) override {
86 RTC_NOTREACHED();
87 return false;
88 }
89
90 bool StartLogging(rtc::PlatformFile log_file,
91 int64_t max_size_bytes) override {
92 RTC_NOTREACHED();
93 return false;
94 }
95
96 void StopLogging() override { RTC_NOTREACHED(); }
97
Elad Alon4a87e1c2017-10-03 16:11:34 +020098 void Log(std::unique_ptr<RtcEvent> event) override {
99 rtc::CritScope lock(&crit_);
100 if (event_log_) {
101 event_log_->Log(std::move(event));
102 }
103 }
104
ivoc14d5dbe2016-07-04 07:06:55 -0700105 void LogVideoReceiveStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700106 const webrtc::rtclog::StreamConfig&) override {
107 RTC_NOTREACHED();
ivoc14d5dbe2016-07-04 07:06:55 -0700108 }
109
perkjc0876aa2017-05-22 04:08:28 -0700110 void LogVideoSendStreamConfig(const webrtc::rtclog::StreamConfig&) override {
111 RTC_NOTREACHED();
ivoc14d5dbe2016-07-04 07:06:55 -0700112 }
113
ivoce0928d82016-10-10 05:12:51 -0700114 void LogAudioReceiveStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700115 const webrtc::rtclog::StreamConfig& config) override {
ivoce0928d82016-10-10 05:12:51 -0700116 rtc::CritScope lock(&crit_);
117 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200118 event_log_->Log(rtc::MakeUnique<RtcEventAudioReceiveStreamConfig>(
119 rtc::MakeUnique<webrtc::rtclog::StreamConfig>(config)));
ivoce0928d82016-10-10 05:12:51 -0700120 }
121 }
122
123 void LogAudioSendStreamConfig(
perkjf4726992017-05-22 10:12:26 -0700124 const webrtc::rtclog::StreamConfig& config) override {
ivoce0928d82016-10-10 05:12:51 -0700125 rtc::CritScope lock(&crit_);
126 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200127 event_log_->Log(rtc::MakeUnique<RtcEventAudioSendStreamConfig>(
128 rtc::MakeUnique<webrtc::rtclog::StreamConfig>(config)));
ivoce0928d82016-10-10 05:12:51 -0700129 }
130 }
131
Bjorn Terelius440216f2017-09-29 21:01:42 +0200132 void LogIncomingRtpHeader(const RtpPacketReceived& packet) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700133 rtc::CritScope lock(&crit_);
134 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200135 event_log_->Log(rtc::MakeUnique<RtcEventRtpPacketIncoming>(packet));
ivoc14d5dbe2016-07-04 07:06:55 -0700136 }
137 }
138
Bjorn Terelius440216f2017-09-29 21:01:42 +0200139 void LogOutgoingRtpHeader(const RtpPacketToSend& packet,
140 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700141 rtc::CritScope lock(&crit_);
142 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200143 event_log_->Log(
144 rtc::MakeUnique<RtcEventRtpPacketOutgoing>(packet, probe_cluster_id));
Bjorn Terelius440216f2017-09-29 21:01:42 +0200145 }
146 }
147
148 void LogIncomingRtcpPacket(rtc::ArrayView<const uint8_t> packet) override {
149 rtc::CritScope lock(&crit_);
150 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200151 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketIncoming>(packet));
Bjorn Terelius440216f2017-09-29 21:01:42 +0200152 }
153 }
154
155 void LogOutgoingRtcpPacket(rtc::ArrayView<const uint8_t> packet) override {
156 rtc::CritScope lock(&crit_);
157 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200158 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketOutgoing>(packet));
ivoc14d5dbe2016-07-04 07:06:55 -0700159 }
160 }
161
162 void LogAudioPlayout(uint32_t ssrc) override {
163 rtc::CritScope lock(&crit_);
164 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200165 event_log_->Log(rtc::MakeUnique<RtcEventAudioPlayout>(ssrc));
ivoc14d5dbe2016-07-04 07:06:55 -0700166 }
167 }
168
terelius424e6cf2017-02-20 05:14:41 -0800169 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700170 uint8_t fraction_loss,
171 int32_t total_packets) override {
172 rtc::CritScope lock(&crit_);
173 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200174 event_log_->Log(rtc::MakeUnique<RtcEventBweUpdateLossBased>(
175 bitrate_bps, fraction_loss, total_packets));
ivoc14d5dbe2016-07-04 07:06:55 -0700176 }
177 }
178
terelius424e6cf2017-02-20 05:14:41 -0800179 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800180 BandwidthUsage detector_state) override {
181 rtc::CritScope lock(&crit_);
182 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200183 event_log_->Log(rtc::MakeUnique<RtcEventBweUpdateDelayBased>(
184 bitrate_bps, detector_state));
terelius0baf55d2017-02-17 03:38:28 -0800185 }
186 }
187
minyue4b7c9522017-01-24 04:54:59 -0800188 void LogAudioNetworkAdaptation(
michaeltcde46b72017-04-06 05:59:10 -0700189 const AudioEncoderRuntimeConfig& config) override {
minyue4b7c9522017-01-24 04:54:59 -0800190 rtc::CritScope lock(&crit_);
191 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200192 event_log_->Log(rtc::MakeUnique<RtcEventAudioNetworkAdaptation>(
193 rtc::MakeUnique<AudioEncoderRuntimeConfig>(config)));
minyue4b7c9522017-01-24 04:54:59 -0800194 }
195 }
196
philipel32d00102017-02-27 02:18:46 -0800197 void LogProbeClusterCreated(int id,
198 int bitrate_bps,
199 int min_probes,
200 int min_bytes) override {
201 rtc::CritScope lock(&crit_);
202 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200203 event_log_->Log(rtc::MakeUnique<RtcEventProbeClusterCreated>(
204 id, bitrate_bps, min_probes, min_bytes));
philipel32d00102017-02-27 02:18:46 -0800205 }
206 };
207
208 void LogProbeResultSuccess(int id, int bitrate_bps) override {
209 rtc::CritScope lock(&crit_);
210 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200211 event_log_->Log(
212 rtc::MakeUnique<RtcEventProbeResultSuccess>(id, bitrate_bps));
philipel32d00102017-02-27 02:18:46 -0800213 }
214 };
215
216 void LogProbeResultFailure(int id,
217 ProbeFailureReason failure_reason) override {
218 rtc::CritScope lock(&crit_);
219 if (event_log_) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200220 event_log_->Log(
221 rtc::MakeUnique<RtcEventProbeResultFailure>(id, failure_reason));
philipel32d00102017-02-27 02:18:46 -0800222 }
223 };
224
ivoc14d5dbe2016-07-04 07:06:55 -0700225 void SetEventLog(RtcEventLog* event_log) {
226 rtc::CritScope lock(&crit_);
227 event_log_ = event_log;
228 }
229
230 private:
231 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700232 RtcEventLog* event_log_ RTC_GUARDED_BY(crit_);
ivoc14d5dbe2016-07-04 07:06:55 -0700233 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
234};
235
michaelt9332b7d2016-11-30 07:51:13 -0800236class RtcpRttStatsProxy final : public RtcpRttStats {
237 public:
238 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
239
240 void OnRttUpdate(int64_t rtt) override {
241 rtc::CritScope lock(&crit_);
242 if (rtcp_rtt_stats_)
243 rtcp_rtt_stats_->OnRttUpdate(rtt);
244 }
245
246 int64_t LastProcessedRtt() const override {
247 rtc::CritScope lock(&crit_);
248 if (!rtcp_rtt_stats_)
249 return 0;
250 return rtcp_rtt_stats_->LastProcessedRtt();
251 }
252
253 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
254 rtc::CritScope lock(&crit_);
255 rtcp_rtt_stats_ = rtcp_rtt_stats;
256 }
257
258 private:
259 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700260 RtcpRttStats* rtcp_rtt_stats_ RTC_GUARDED_BY(crit_);
michaelt9332b7d2016-11-30 07:51:13 -0800261 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
262};
263
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100264class TransportFeedbackProxy : public TransportFeedbackObserver {
265 public:
266 TransportFeedbackProxy() : feedback_observer_(nullptr) {
267 pacer_thread_.DetachFromThread();
268 network_thread_.DetachFromThread();
269 }
270
271 void SetTransportFeedbackObserver(
272 TransportFeedbackObserver* feedback_observer) {
273 RTC_DCHECK(thread_checker_.CalledOnValidThread());
274 rtc::CritScope lock(&crit_);
275 feedback_observer_ = feedback_observer;
276 }
277
278 // Implements TransportFeedbackObserver.
elad.alond12a8e12017-03-23 11:04:48 -0700279 void AddPacket(uint32_t ssrc,
280 uint16_t sequence_number,
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100281 size_t length,
philipel8aadd502017-02-23 02:56:13 -0800282 const PacedPacketInfo& pacing_info) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100283 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
284 rtc::CritScope lock(&crit_);
285 if (feedback_observer_)
elad.alond12a8e12017-03-23 11:04:48 -0700286 feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100287 }
philipel8aadd502017-02-23 02:56:13 -0800288
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100289 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
290 RTC_DCHECK(network_thread_.CalledOnValidThread());
291 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700292 if (feedback_observer_)
293 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200294 }
elad.alonf9490002017-03-06 05:32:21 -0800295 std::vector<PacketFeedback> GetTransportFeedbackVector() const override {
Stefan Holmer60e43462016-09-07 09:58:20 +0200296 RTC_NOTREACHED();
elad.alonf9490002017-03-06 05:32:21 -0800297 return std::vector<PacketFeedback>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100298 }
299
300 private:
301 rtc::CriticalSection crit_;
302 rtc::ThreadChecker thread_checker_;
303 rtc::ThreadChecker pacer_thread_;
304 rtc::ThreadChecker network_thread_;
danilchapa37de392017-09-09 04:17:22 -0700305 TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100306};
307
308class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
309 public:
310 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
311 pacer_thread_.DetachFromThread();
312 }
313
314 void SetSequenceNumberAllocator(
315 TransportSequenceNumberAllocator* seq_num_allocator) {
316 RTC_DCHECK(thread_checker_.CalledOnValidThread());
317 rtc::CritScope lock(&crit_);
318 seq_num_allocator_ = seq_num_allocator;
319 }
320
321 // Implements TransportSequenceNumberAllocator.
322 uint16_t AllocateSequenceNumber() override {
323 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
324 rtc::CritScope lock(&crit_);
325 if (!seq_num_allocator_)
326 return 0;
327 return seq_num_allocator_->AllocateSequenceNumber();
328 }
329
330 private:
331 rtc::CriticalSection crit_;
332 rtc::ThreadChecker thread_checker_;
333 rtc::ThreadChecker pacer_thread_;
danilchapa37de392017-09-09 04:17:22 -0700334 TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100335};
336
337class RtpPacketSenderProxy : public RtpPacketSender {
338 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800339 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100340
341 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
342 RTC_DCHECK(thread_checker_.CalledOnValidThread());
343 rtc::CritScope lock(&crit_);
344 rtp_packet_sender_ = rtp_packet_sender;
345 }
346
347 // Implements RtpPacketSender.
348 void InsertPacket(Priority priority,
349 uint32_t ssrc,
350 uint16_t sequence_number,
351 int64_t capture_time_ms,
352 size_t bytes,
353 bool retransmission) override {
354 rtc::CritScope lock(&crit_);
355 if (rtp_packet_sender_) {
356 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
357 capture_time_ms, bytes, retransmission);
358 }
359 }
360
361 private:
362 rtc::ThreadChecker thread_checker_;
363 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700364 RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100365};
366
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000367class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000368 public:
stefan7de8d642017-02-07 07:14:08 -0800369 explicit VoERtcpObserver(Channel* owner)
370 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000371 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000372
stefan7de8d642017-02-07 07:14:08 -0800373 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
374 rtc::CritScope lock(&crit_);
375 bandwidth_observer_ = bandwidth_observer;
376 }
377
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000378 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800379 rtc::CritScope lock(&crit_);
380 if (bandwidth_observer_) {
381 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
382 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000383 }
384
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000385 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
386 int64_t rtt,
387 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800388 {
389 rtc::CritScope lock(&crit_);
390 if (bandwidth_observer_) {
391 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
392 now_ms);
393 }
394 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000395 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
396 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
397 // report for VoiceEngine?
398 if (report_blocks.empty())
399 return;
400
401 int fraction_lost_aggregate = 0;
402 int total_number_of_packets = 0;
403
404 // If receiving multiple report blocks, calculate the weighted average based
405 // on the number of packets a report refers to.
406 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
407 block_it != report_blocks.end(); ++block_it) {
408 // Find the previous extended high sequence number for this remote SSRC,
409 // to calculate the number of RTP packets this report refers to. Ignore if
410 // we haven't seen this SSRC before.
411 std::map<uint32_t, uint32_t>::iterator seq_num_it =
srte3e69e5c2017-08-09 06:13:45 -0700412 extended_max_sequence_number_.find(block_it->source_ssrc);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000413 int number_of_packets = 0;
414 if (seq_num_it != extended_max_sequence_number_.end()) {
srte3e69e5c2017-08-09 06:13:45 -0700415 number_of_packets =
416 block_it->extended_highest_sequence_number - seq_num_it->second;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000417 }
srte3e69e5c2017-08-09 06:13:45 -0700418 fraction_lost_aggregate += number_of_packets * block_it->fraction_lost;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000419 total_number_of_packets += number_of_packets;
420
srte3e69e5c2017-08-09 06:13:45 -0700421 extended_max_sequence_number_[block_it->source_ssrc] =
422 block_it->extended_highest_sequence_number;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000423 }
424 int weighted_fraction_lost = 0;
425 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800426 weighted_fraction_lost =
427 (fraction_lost_aggregate + total_number_of_packets / 2) /
428 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000429 }
elad.alond12a8e12017-03-23 11:04:48 -0700430 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000431 }
432
433 private:
434 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000435 // Maps remote side ssrc to extended highest sequence number received.
436 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800437 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700438 RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000439};
440
henrikaec6fbd22017-03-31 05:43:36 -0700441class Channel::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
442 public:
443 ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
444 Channel* channel)
445 : audio_frame_(std::move(audio_frame)), channel_(channel) {
446 RTC_DCHECK(channel_);
447 }
448
449 private:
450 bool Run() override {
451 RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
452 channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
453 return true;
454 }
455
456 std::unique_ptr<AudioFrame> audio_frame_;
457 Channel* const channel_;
458};
459
kwiberg55b97fe2016-01-28 05:22:45 -0800460int32_t Channel::SendData(FrameType frameType,
461 uint8_t payloadType,
462 uint32_t timeStamp,
463 const uint8_t* payloadData,
464 size_t payloadSize,
465 const RTPFragmentationHeader* fragmentation) {
henrikaec6fbd22017-03-31 05:43:36 -0700466 RTC_DCHECK_RUN_ON(encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800467 if (_includeAudioLevelIndication) {
468 // Store current audio level in the RTP/RTCP module.
469 // The level will be used in combination with voice-activity state
470 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800471 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800472 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000473
kwiberg55b97fe2016-01-28 05:22:45 -0800474 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
475 // packetization.
476 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700477 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800478 (FrameType&)frameType, payloadType, timeStamp,
479 // Leaving the time when this frame was
480 // received from the capture device as
481 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700482 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
solenberg1c239d42017-09-29 06:00:28 -0700483 LOG(LS_ERROR) <<
484 "Channel::SendData() failed to send data to RTP/RTCP module";
kwiberg55b97fe2016-01-28 05:22:45 -0800485 return -1;
486 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000487
kwiberg55b97fe2016-01-28 05:22:45 -0800488 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000489}
490
stefan1d8a5062015-10-02 03:39:33 -0700491bool Channel::SendRtp(const uint8_t* data,
492 size_t len,
493 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800494 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000495
kwiberg55b97fe2016-01-28 05:22:45 -0800496 if (_transportPtr == NULL) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200497 LOG(LS_ERROR) << "Channel::SendPacket() failed to send RTP packet due to"
498 << " invalid transport object";
kwiberg55b97fe2016-01-28 05:22:45 -0800499 return false;
500 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000501
kwiberg55b97fe2016-01-28 05:22:45 -0800502 uint8_t* bufferToSendPtr = (uint8_t*)data;
503 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
kwiberg55b97fe2016-01-28 05:22:45 -0800505 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
solenberg1c239d42017-09-29 06:00:28 -0700506 LOG(LS_ERROR) << "Channel::SendPacket() RTP transmission failed";
kwiberg55b97fe2016-01-28 05:22:45 -0800507 return false;
508 }
509 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000510}
511
kwiberg55b97fe2016-01-28 05:22:45 -0800512bool Channel::SendRtcp(const uint8_t* data, size_t len) {
kwiberg55b97fe2016-01-28 05:22:45 -0800513 rtc::CritScope cs(&_callbackCritSect);
514 if (_transportPtr == NULL) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200515 LOG(LS_ERROR) << "Channel::SendRtcp() failed to send RTCP packet due to"
516 << " invalid transport object";
kwiberg55b97fe2016-01-28 05:22:45 -0800517 return false;
518 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000519
kwiberg55b97fe2016-01-28 05:22:45 -0800520 uint8_t* bufferToSendPtr = (uint8_t*)data;
521 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000522
kwiberg55b97fe2016-01-28 05:22:45 -0800523 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
524 if (n < 0) {
solenberg1c239d42017-09-29 06:00:28 -0700525 LOG(LS_ERROR) << "Channel::SendRtcp() transmission failed";
kwiberg55b97fe2016-01-28 05:22:45 -0800526 return false;
527 }
528 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000529}
530
kwiberg55b97fe2016-01-28 05:22:45 -0800531void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
kwiberg55b97fe2016-01-28 05:22:45 -0800532 // Update ssrc so that NTP for AV sync can be updated.
533 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000534}
535
Peter Boströmac547a62015-09-17 23:03:57 +0200536void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200537 // TODO(saza): remove.
niklase@google.com470e71d2011-07-07 08:21:25 +0000538}
539
Peter Boströmac547a62015-09-17 23:03:57 +0200540int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000541 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000542 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000543 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800544 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200545 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800546 CodecInst receiveCodec = {0};
547 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000548
kwiberg55b97fe2016-01-28 05:22:45 -0800549 receiveCodec.pltype = payloadType;
550 receiveCodec.plfreq = frequency;
551 receiveCodec.channels = channels;
552 receiveCodec.rate = rate;
553 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000554
kwiberg55b97fe2016-01-28 05:22:45 -0800555 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
556 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000557
kwiberg55b97fe2016-01-28 05:22:45 -0800558 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700559 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
560 CodecInstToSdp(receiveCodec))) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200561 LOG(LS_WARNING) << "Channel::OnInitializeDecoder() invalid codec (pt="
562 << payloadType << ", name=" << payloadName
563 << ") received - 1";
kwiberg55b97fe2016-01-28 05:22:45 -0800564 return -1;
565 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000566
kwiberg55b97fe2016-01-28 05:22:45 -0800567 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000568}
569
kwiberg55b97fe2016-01-28 05:22:45 -0800570int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
571 size_t payloadSize,
572 const WebRtcRTPHeader* rtpHeader) {
kwiberg55b97fe2016-01-28 05:22:45 -0800573 if (!channel_state_.Get().playing) {
574 // Avoid inserting into NetEQ when we are not playing. Count the
575 // packet as discarded.
niklase@google.com470e71d2011-07-07 08:21:25 +0000576 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800577 }
578
579 // Push the incoming payload (parsed and ready for decoding) into the ACM
580 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
581 0) {
solenberg1c239d42017-09-29 06:00:28 -0700582 LOG(LS_ERROR) <<
583 "Channel::OnReceivedPayloadData() unable to push data to the ACM";
kwiberg55b97fe2016-01-28 05:22:45 -0800584 return -1;
585 }
586
kwiberg55b97fe2016-01-28 05:22:45 -0800587 int64_t round_trip_time = 0;
588 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
589 NULL);
590
591 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
592 if (!nack_list.empty()) {
593 // Can't use nack_list.data() since it's not supported by all
594 // compilers.
595 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
596 }
597 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000598}
599
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000600bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000601 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000602 RTPHeader header;
603 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200604 LOG(LS_WARNING) << "IncomingPacket invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000605 return false;
606 }
607 header.payload_type_frequency =
608 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
609 if (header.payload_type_frequency < 0)
610 return false;
611 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
612}
613
solenberg2397b9a2017-09-22 06:48:10 -0700614AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
615 int sample_rate_hz,
616 AudioFrame* audio_frame) {
617 audio_frame->sample_rate_hz_ = sample_rate_hz;
618
ivoc14d5dbe2016-07-04 07:06:55 -0700619 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800620 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
Elad Alon4a87e1c2017-10-03 16:11:34 +0200621 event_log_proxy_->Log(rtc::MakeUnique<RtcEventAudioPlayout>(ssrc));
kwiberg55b97fe2016-01-28 05:22:45 -0800622 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700623 bool muted;
solenberg2397b9a2017-09-22 06:48:10 -0700624 if (audio_coding_->PlayoutData10Ms(audio_frame->sample_rate_hz_, audio_frame,
henrik.lundind4ccb002016-05-17 12:21:55 -0700625 &muted) == -1) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200626 LOG(LS_ERROR) << "Channel::GetAudioFrame() PlayoutData10Ms() failed!";
kwiberg55b97fe2016-01-28 05:22:45 -0800627 // In all likelihood, the audio in this frame is garbage. We return an
628 // error so that the audio mixer module doesn't add it to the mix. As
629 // a result, it won't be played out and the actions skipped here are
630 // irrelevant.
solenberg2397b9a2017-09-22 06:48:10 -0700631 return AudioMixer::Source::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800632 }
henrik.lundina89ab962016-05-18 08:52:45 -0700633
634 if (muted) {
635 // TODO(henrik.lundin): We should be able to do better than this. But we
636 // will have to go through all the cases below where the audio samples may
637 // be used, and handle the muted case in some way.
solenberg2397b9a2017-09-22 06:48:10 -0700638 AudioFrameOperations::Mute(audio_frame);
henrik.lundina89ab962016-05-18 08:52:45 -0700639 }
kwiberg55b97fe2016-01-28 05:22:45 -0800640
kwiberg55b97fe2016-01-28 05:22:45 -0800641 // Store speech type for dead-or-alive detection
solenberg2397b9a2017-09-22 06:48:10 -0700642 _outputSpeechType = audio_frame->speech_type_;
kwiberg55b97fe2016-01-28 05:22:45 -0800643
kwiberg55b97fe2016-01-28 05:22:45 -0800644 {
645 // Pass the audio buffers to an optional sink callback, before applying
646 // scaling/panning, as that applies to the mix operation.
647 // External recipients of the audio (e.g. via AudioTrack), will do their
648 // own mixing/dynamic processing.
649 rtc::CritScope cs(&_callbackCritSect);
650 if (audio_sink_) {
651 AudioSinkInterface::Data data(
solenberg2397b9a2017-09-22 06:48:10 -0700652 audio_frame->data(), audio_frame->samples_per_channel_,
653 audio_frame->sample_rate_hz_, audio_frame->num_channels_,
654 audio_frame->timestamp_);
kwiberg55b97fe2016-01-28 05:22:45 -0800655 audio_sink_->OnData(data);
656 }
657 }
658
659 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800660 {
661 rtc::CritScope cs(&volume_settings_critsect_);
662 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800663 }
664
665 // Output volume scaling
666 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800667 // TODO(solenberg): Combine with mute state - this can cause clicks!
solenberg2397b9a2017-09-22 06:48:10 -0700668 AudioFrameOperations::ScaleWithSat(output_gain, audio_frame);
kwiberg55b97fe2016-01-28 05:22:45 -0800669 }
670
kwiberg55b97fe2016-01-28 05:22:45 -0800671 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700672 // TODO(henrik.lundin) Use the |muted| information here too.
zstein3c451862017-07-20 09:57:42 -0700673 // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see
zsteine76bd3a2017-07-14 12:17:49 -0700674 // https://crbug.com/webrtc/7517).
solenberg2397b9a2017-09-22 06:48:10 -0700675 _outputAudioLevel.ComputeLevel(*audio_frame, kAudioSampleDurationSeconds);
kwiberg55b97fe2016-01-28 05:22:45 -0800676
solenberg2397b9a2017-09-22 06:48:10 -0700677 if (capture_start_rtp_time_stamp_ < 0 && audio_frame->timestamp_ != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800678 // The first frame with a valid rtp timestamp.
solenberg2397b9a2017-09-22 06:48:10 -0700679 capture_start_rtp_time_stamp_ = audio_frame->timestamp_;
kwiberg55b97fe2016-01-28 05:22:45 -0800680 }
681
682 if (capture_start_rtp_time_stamp_ >= 0) {
solenberg2397b9a2017-09-22 06:48:10 -0700683 // audio_frame.timestamp_ should be valid from now on.
kwiberg55b97fe2016-01-28 05:22:45 -0800684
685 // Compute elapsed time.
686 int64_t unwrap_timestamp =
solenberg2397b9a2017-09-22 06:48:10 -0700687 rtp_ts_wraparound_handler_->Unwrap(audio_frame->timestamp_);
688 audio_frame->elapsed_time_ms_ =
kwiberg55b97fe2016-01-28 05:22:45 -0800689 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700690 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800691
niklase@google.com470e71d2011-07-07 08:21:25 +0000692 {
kwiberg55b97fe2016-01-28 05:22:45 -0800693 rtc::CritScope lock(&ts_stats_lock_);
694 // Compute ntp time.
solenberg2397b9a2017-09-22 06:48:10 -0700695 audio_frame->ntp_time_ms_ =
696 ntp_estimator_.Estimate(audio_frame->timestamp_);
kwiberg55b97fe2016-01-28 05:22:45 -0800697 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
solenberg2397b9a2017-09-22 06:48:10 -0700698 if (audio_frame->ntp_time_ms_ > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800699 // Compute |capture_start_ntp_time_ms_| so that
700 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
701 capture_start_ntp_time_ms_ =
solenberg2397b9a2017-09-22 06:48:10 -0700702 audio_frame->ntp_time_ms_ - audio_frame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000703 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000704 }
kwiberg55b97fe2016-01-28 05:22:45 -0800705 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000706
solenberg2397b9a2017-09-22 06:48:10 -0700707 return muted ? AudioMixer::Source::AudioFrameInfo::kMuted
708 : AudioMixer::Source::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000709}
710
solenberg2397b9a2017-09-22 06:48:10 -0700711int Channel::PreferredSampleRate() const {
kwiberg55b97fe2016-01-28 05:22:45 -0800712 // Return the bigger of playout and receive frequency in the ACM.
solenberg2397b9a2017-09-22 06:48:10 -0700713 return std::max(audio_coding_->ReceiveFrequency(),
714 audio_coding_->PlayoutFrequency());
niklase@google.com470e71d2011-07-07 08:21:25 +0000715}
716
henrikaec6fbd22017-03-31 05:43:36 -0700717int32_t Channel::CreateChannel(Channel*& channel,
718 int32_t channelId,
719 uint32_t instanceId,
720 const VoEBase::ChannelConfig& config) {
solenberg88499ec2016-09-07 07:34:41 -0700721 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800722 if (channel == NULL) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200723 LOG(LS_ERROR) << "unable to allocate memory for new channel";
kwiberg55b97fe2016-01-28 05:22:45 -0800724 return -1;
725 }
726 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000727}
728
pbos@webrtc.org92135212013-05-14 08:31:39 +0000729Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000730 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700731 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800732 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100733 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700734 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800735 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100736 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800737 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100738 rtp_receive_statistics_(
739 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
740 rtp_receiver_(
741 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100742 this,
743 this,
744 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700745 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100746 _outputAudioLevel(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100747 _timeStamp(0), // This is just an offset, RTP module will add it's own
748 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100749 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100750 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100751 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100752 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100753 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
754 capture_start_rtp_time_stamp_(-1),
755 capture_start_ntp_time_ms_(-1),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100756 _moduleProcessThreadPtr(NULL),
757 _audioDeviceModulePtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100758 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700759 input_mute_(false),
760 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100761 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100762 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800763 transport_overhead_per_packet_(0),
764 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100765 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100766 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100767 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700768 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800769 feedback_observer_proxy_(new TransportFeedbackProxy()),
770 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700771 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200772 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
773 kMaxRetransmissionWindowMs)),
elad.alond12a8e12017-03-23 11:04:48 -0700774 decoder_factory_(config.acm_config.decoder_factory),
elad.alon28770482017-03-28 05:03:55 -0700775 use_twcc_plr_for_ana_(
776 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled") {
solenberg88499ec2016-09-07 07:34:41 -0700777 AudioCodingModule::Config acm_config(config.acm_config);
henrik.lundina89ab962016-05-18 08:52:45 -0700778 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800779 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200780
kwiberg55b97fe2016-01-28 05:22:45 -0800781 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
kwiberg55b97fe2016-01-28 05:22:45 -0800783 RtpRtcp::Configuration configuration;
784 configuration.audio = true;
785 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800786 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800787 configuration.receive_statistics = rtp_receive_statistics_.get();
788 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800789 if (pacing_enabled_) {
790 configuration.paced_sender = rtp_packet_sender_proxy_.get();
791 configuration.transport_sequence_number_allocator =
792 seq_num_allocator_proxy_.get();
793 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
794 }
ivoc14d5dbe2016-07-04 07:06:55 -0700795 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800796 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200797 configuration.retransmission_rate_limiter =
798 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000799
kwiberg55b97fe2016-01-28 05:22:45 -0800800 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100801 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802}
803
kwiberg55b97fe2016-01-28 05:22:45 -0800804Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700805 RTC_DCHECK(!channel_state_.Get().sending);
806 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000807}
808
kwiberg55b97fe2016-01-28 05:22:45 -0800809int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700810 RTC_DCHECK(construction_thread_.CalledOnValidThread());
niklase@google.com470e71d2011-07-07 08:21:25 +0000811
kwiberg55b97fe2016-01-28 05:22:45 -0800812 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000813
kwiberg55b97fe2016-01-28 05:22:45 -0800814 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000815
solenberg1c239d42017-09-29 06:00:28 -0700816 if (_moduleProcessThreadPtr == NULL) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200817 LOG(LS_ERROR) << "Channel::Init() must call SetEngineInformation() first";
kwiberg55b97fe2016-01-28 05:22:45 -0800818 return -1;
819 }
820
821 // --- Add modules to process thread (for periodic schedulation)
822
tommidea489f2017-03-03 03:20:24 -0800823 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800824
825 // --- ACM initialization
826
827 if (audio_coding_->InitializeReceiver() == -1) {
solenberg1c239d42017-09-29 06:00:28 -0700828 LOG(LS_ERROR) << "Channel::Init() unable to initialize the ACM - 1";
kwiberg55b97fe2016-01-28 05:22:45 -0800829 return -1;
830 }
831
832 // --- RTP/RTCP module initialization
833
834 // Ensure that RTCP is enabled by default for the created channel.
835 // Note that, the module will keep generating RTCP until it is explicitly
836 // disabled by the user.
837 // After StopListen (when no sockets exists), RTCP packets will no longer
838 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700839 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800840 // RTCP is enabled by default.
841 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
842 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -0800843 if (audio_coding_->RegisterTransportCallback(this) == -1) {
solenberg1c239d42017-09-29 06:00:28 -0700844 LOG(LS_ERROR) << "Channel::Init() callbacks not registered";
kwiberg55b97fe2016-01-28 05:22:45 -0800845 return -1;
846 }
847
kwiberg1c07c702017-03-27 07:15:49 -0700848 return 0;
849}
850
tommi0a2391f2017-03-21 02:31:51 -0700851void Channel::Terminate() {
852 RTC_DCHECK(construction_thread_.CalledOnValidThread());
853 // Must be called on the same thread as Init().
tommi0a2391f2017-03-21 02:31:51 -0700854 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
855
856 StopSend();
857 StopPlayout();
858
tommi0a2391f2017-03-21 02:31:51 -0700859 // The order to safely shutdown modules in a channel is:
860 // 1. De-register callbacks in modules
861 // 2. De-register modules in process thread
862 // 3. Destroy modules
863 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +0200864 LOG(LS_WARNING) << "Terminate() failed to de-register transport callback"
865 << " (Audio coding module)";
tommi0a2391f2017-03-21 02:31:51 -0700866 }
867
tommi0a2391f2017-03-21 02:31:51 -0700868 // De-register modules in process thread
869 if (_moduleProcessThreadPtr)
870 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
871
872 // End of modules shutdown
873}
874
solenberg1c239d42017-09-29 06:00:28 -0700875int32_t Channel::SetEngineInformation(ProcessThread& moduleProcessThread,
kwiberg55b97fe2016-01-28 05:22:45 -0800876 AudioDeviceModule& audioDeviceModule,
henrikaec6fbd22017-03-31 05:43:36 -0700877 rtc::TaskQueue* encoder_queue) {
878 RTC_DCHECK(encoder_queue);
879 RTC_DCHECK(!encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800880 _moduleProcessThreadPtr = &moduleProcessThread;
881 _audioDeviceModulePtr = &audioDeviceModule;
henrikaec6fbd22017-03-31 05:43:36 -0700882 encoder_queue_ = encoder_queue;
kwiberg55b97fe2016-01-28 05:22:45 -0800883 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000884}
885
kwibergb7f89d62016-02-17 10:04:18 -0800886void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -0800887 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -0800888 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +0100889}
890
ossu29b1a8d2016-06-13 07:34:51 -0700891const rtc::scoped_refptr<AudioDecoderFactory>&
892Channel::GetAudioDecoderFactory() const {
893 return decoder_factory_;
894}
895
kwiberg55b97fe2016-01-28 05:22:45 -0800896int32_t Channel::StartPlayout() {
kwiberg55b97fe2016-01-28 05:22:45 -0800897 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800899 }
900
kwiberg55b97fe2016-01-28 05:22:45 -0800901 channel_state_.SetPlaying(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800902
903 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000904}
905
kwiberg55b97fe2016-01-28 05:22:45 -0800906int32_t Channel::StopPlayout() {
kwiberg55b97fe2016-01-28 05:22:45 -0800907 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000908 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800909 }
910
kwiberg55b97fe2016-01-28 05:22:45 -0800911 channel_state_.SetPlaying(false);
912 _outputAudioLevel.Clear();
913
914 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000915}
916
kwiberg55b97fe2016-01-28 05:22:45 -0800917int32_t Channel::StartSend() {
kwiberg55b97fe2016-01-28 05:22:45 -0800918 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000919 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800920 }
921 channel_state_.SetSending(true);
henrika4515fa02017-05-03 08:30:15 -0700922 {
923 // It is now OK to start posting tasks to the encoder task queue.
924 rtc::CritScope cs(&encoder_queue_lock_);
925 encoder_queue_is_active_ = true;
926 }
solenberg08b19df2017-02-15 00:42:31 -0800927 // Resume the previous sequence number which was reset by StopSend(). This
928 // needs to be done before |sending| is set to true on the RTP/RTCP module.
929 if (send_sequence_number_) {
930 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
931 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100932 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800933 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
solenberg1c239d42017-09-29 06:00:28 -0700934 LOG(LS_ERROR) << "StartSend() RTP/RTCP failed to start sending";
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100935 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -0800936 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000937 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -0800938 return -1;
939 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +0000940
kwiberg55b97fe2016-01-28 05:22:45 -0800941 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000942}
943
henrikaec6fbd22017-03-31 05:43:36 -0700944void Channel::StopSend() {
kwiberg55b97fe2016-01-28 05:22:45 -0800945 if (!channel_state_.Get().sending) {
henrikaec6fbd22017-03-31 05:43:36 -0700946 return;
kwiberg55b97fe2016-01-28 05:22:45 -0800947 }
948 channel_state_.SetSending(false);
949
henrikaec6fbd22017-03-31 05:43:36 -0700950 // Post a task to the encoder thread which sets an event when the task is
951 // executed. We know that no more encoding tasks will be added to the task
952 // queue for this channel since sending is now deactivated. It means that,
953 // if we wait for the event to bet set, we know that no more pending tasks
954 // exists and it is therfore guaranteed that the task queue will never try
955 // to acccess and invalid channel object.
956 RTC_DCHECK(encoder_queue_);
henrika4515fa02017-05-03 08:30:15 -0700957
henrikaec6fbd22017-03-31 05:43:36 -0700958 rtc::Event flush(false, false);
henrika4515fa02017-05-03 08:30:15 -0700959 {
960 // Clear |encoder_queue_is_active_| under lock to prevent any other tasks
961 // than this final "flush task" to be posted on the queue.
962 rtc::CritScope cs(&encoder_queue_lock_);
963 encoder_queue_is_active_ = false;
964 encoder_queue_->PostTask([&flush]() { flush.Set(); });
965 }
henrikaec6fbd22017-03-31 05:43:36 -0700966 flush.Wait(rtc::Event::kForever);
967
kwiberg55b97fe2016-01-28 05:22:45 -0800968 // Store the sequence number to be able to pick up the same sequence for
969 // the next StartSend(). This is needed for restarting device, otherwise
970 // it might cause libSRTP to complain about packets being replayed.
971 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
972 // CL is landed. See issue
973 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
974 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
975
976 // Reset sending SSRC and sequence number and triggers direct transmission
977 // of RTCP BYE
978 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
solenberg1c239d42017-09-29 06:00:28 -0700979 LOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending";
kwiberg55b97fe2016-01-28 05:22:45 -0800980 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100981 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000982}
983
ossu1ffbd6c2017-04-06 12:05:04 -0700984bool Channel::SetEncoder(int payload_type,
985 std::unique_ptr<AudioEncoder> encoder) {
986 RTC_DCHECK_GE(payload_type, 0);
987 RTC_DCHECK_LE(payload_type, 127);
ossu76d29f92017-06-09 07:30:13 -0700988 // TODO(ossu): Make CodecInsts up, for now: one for the RTP/RTCP module and
989 // one for for us to keep track of sample rate and number of channels, etc.
990
991 // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
992 // as well as some other things, so we collect this info and send it along.
993 CodecInst rtp_codec;
994 rtp_codec.pltype = payload_type;
995 strncpy(rtp_codec.plname, "audio", sizeof(rtp_codec.plname));
996 rtp_codec.plname[sizeof(rtp_codec.plname) - 1] = 0;
ossu1ffbd6c2017-04-06 12:05:04 -0700997 // Seems unclear if it should be clock rate or sample rate. CodecInst
998 // supposedly carries the sample rate, but only clock rate seems sensible to
999 // send to the RTP/RTCP module.
ossu76d29f92017-06-09 07:30:13 -07001000 rtp_codec.plfreq = encoder->RtpTimestampRateHz();
1001 rtp_codec.pacsize = rtc::CheckedDivExact(
1002 static_cast<int>(encoder->Max10MsFramesInAPacket() * rtp_codec.plfreq),
1003 100);
1004 rtp_codec.channels = encoder->NumChannels();
1005 rtp_codec.rate = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001006
ossu76d29f92017-06-09 07:30:13 -07001007 // For audio encoding we need, instead, the actual sample rate of the codec.
1008 // The rest of the information should be the same.
1009 CodecInst send_codec = rtp_codec;
1010 send_codec.plfreq = encoder->SampleRateHz();
1011 cached_send_codec_.emplace(send_codec);
1012
1013 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
ossu1ffbd6c2017-04-06 12:05:04 -07001014 _rtpRtcpModule->DeRegisterSendPayload(payload_type);
ossu76d29f92017-06-09 07:30:13 -07001015 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001016 LOG(LS_ERROR)
1017 << "SetEncoder() failed to register codec to RTP/RTCP module";
ossu1ffbd6c2017-04-06 12:05:04 -07001018 return false;
1019 }
1020 }
1021
1022 audio_coding_->SetEncoder(std::move(encoder));
ossu20a4b3f2017-04-27 02:08:52 -07001023 codec_manager_.UnsetCodecInst();
ossu1ffbd6c2017-04-06 12:05:04 -07001024 return true;
1025}
1026
ossu20a4b3f2017-04-27 02:08:52 -07001027void Channel::ModifyEncoder(
1028 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
1029 audio_coding_->ModifyEncoder(modifier);
1030}
1031
kwiberg55b97fe2016-01-28 05:22:45 -08001032int32_t Channel::GetSendCodec(CodecInst& codec) {
ossu76d29f92017-06-09 07:30:13 -07001033 if (cached_send_codec_) {
1034 codec = *cached_send_codec_;
1035 return 0;
1036 } else {
ossu20a4b3f2017-04-27 02:08:52 -07001037 const CodecInst* send_codec = codec_manager_.GetCodecInst();
1038 if (send_codec) {
1039 codec = *send_codec;
1040 return 0;
1041 }
1042 }
kwiberg1fd4a4a2015-11-03 11:20:50 -08001043 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001044}
1045
kwiberg55b97fe2016-01-28 05:22:45 -08001046int32_t Channel::GetRecCodec(CodecInst& codec) {
1047 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001048}
1049
kwiberg55b97fe2016-01-28 05:22:45 -08001050int32_t Channel::SetSendCodec(const CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001051 if (!codec_manager_.RegisterEncoder(codec) ||
1052 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001053 LOG(LS_ERROR) << "SetSendCodec() failed to register codec to ACM";
kwiberg55b97fe2016-01-28 05:22:45 -08001054 return -1;
1055 }
1056
1057 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1058 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1059 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001060 LOG(LS_ERROR)
1061 << "SetSendCodec() failed to register codec to RTP/RTCP module";
kwiberg55b97fe2016-01-28 05:22:45 -08001062 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001063 }
kwiberg55b97fe2016-01-28 05:22:45 -08001064 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001065
ossu76d29f92017-06-09 07:30:13 -07001066 cached_send_codec_.reset();
1067
kwiberg55b97fe2016-01-28 05:22:45 -08001068 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001069}
1070
minyue78b4d562016-11-30 04:47:39 -08001071void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
minyue7e304322016-10-12 05:00:55 -07001072 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001073 if (*encoder) {
1074 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001075 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001076 }
1077 });
michaelt566d8202017-01-12 10:17:38 -08001078 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001079}
1080
elad.alond12a8e12017-03-23 11:04:48 -07001081void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1082 if (!use_twcc_plr_for_ana_)
1083 return;
minyue7e304322016-10-12 05:00:55 -07001084 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001085 if (*encoder) {
1086 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1087 }
1088 });
1089}
1090
elad.alondadb4dc2017-03-23 15:29:50 -07001091void Channel::OnRecoverableUplinkPacketLossRate(
1092 float recoverable_packet_loss_rate) {
1093 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1094 if (*encoder) {
1095 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1096 recoverable_packet_loss_rate);
1097 }
1098 });
1099}
1100
elad.alond12a8e12017-03-23 11:04:48 -07001101void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1102 if (use_twcc_plr_for_ana_)
1103 return;
1104 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1105 if (*encoder) {
1106 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1107 }
minyue7e304322016-10-12 05:00:55 -07001108 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001109}
1110
kwiberg1c07c702017-03-27 07:15:49 -07001111void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1112 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1113 audio_coding_->SetReceiveCodecs(codecs);
1114}
1115
minyue7e304322016-10-12 05:00:55 -07001116bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1117 bool success = false;
1118 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1119 if (*encoder) {
michaelt92aef172017-04-18 00:11:48 -07001120 success = (*encoder)->EnableAudioNetworkAdaptor(config_string,
1121 event_log_proxy_.get());
minyue7e304322016-10-12 05:00:55 -07001122 }
1123 });
1124 return success;
1125}
1126
1127void Channel::DisableAudioNetworkAdaptor() {
1128 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1129 if (*encoder)
1130 (*encoder)->DisableAudioNetworkAdaptor();
1131 });
1132}
1133
1134void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1135 int max_frame_length_ms) {
1136 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1137 if (*encoder) {
1138 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1139 max_frame_length_ms);
1140 }
1141 });
1142}
1143
solenberg1c239d42017-09-29 06:00:28 -07001144void Channel::RegisterTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001145 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001146 _transportPtr = transport;
niklase@google.com470e71d2011-07-07 08:21:25 +00001147}
1148
nisse657bab22017-02-21 06:28:10 -08001149void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
nisse657bab22017-02-21 06:28:10 -08001150 RTPHeader header;
1151 packet.GetHeader(&header);
solenberg946d8862017-09-21 04:02:53 -07001152
1153 // Store playout timestamp for the received RTP packet
1154 UpdatePlayoutTimestamp(false);
1155
1156 header.payload_type_frequency =
1157 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
1158 if (header.payload_type_frequency >= 0) {
1159 bool in_order = IsPacketInOrder(header);
1160 rtp_receive_statistics_->IncomingPacket(
1161 header, packet.size(), IsPacketRetransmitted(header, in_order));
1162 rtp_payload_registry_->SetIncomingPayloadType(header);
1163
1164 ReceivePacket(packet.data(), packet.size(), header, in_order);
1165 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001166}
1167
1168bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001169 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001170 const RTPHeader& header,
1171 bool in_order) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001172 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001173 assert(packet_length >= header.headerLength);
1174 size_t payload_length = packet_length - header.headerLength;
Karl Wiberg73b60b82017-09-21 15:00:58 +02001175 const auto pl =
1176 rtp_payload_registry_->PayloadTypeToPayload(header.payloadType);
1177 if (!pl) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001178 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001179 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001180 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
Karl Wiberg73b60b82017-09-21 15:00:58 +02001181 pl->typeSpecific, in_order);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001182}
1183
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001184bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1185 StreamStatistician* statistician =
1186 rtp_receive_statistics_->GetStatistician(header.ssrc);
1187 if (!statistician)
1188 return false;
1189 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001190}
1191
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001192bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1193 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001194 StreamStatistician* statistician =
1195 rtp_receive_statistics_->GetStatistician(header.ssrc);
1196 if (!statistician)
1197 return false;
1198 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001199 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001200 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001201 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001202}
1203
mflodman3d7db262016-04-29 00:57:13 -07001204int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001205 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001206 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001207
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001208 // Deliver RTCP packet to RTP/RTCP module for parsing
nisse479d3d72017-09-13 07:53:37 -07001209 _rtpRtcpModule->IncomingRtcpPacket(data, length);
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001210
Minyue2013aec2015-05-13 14:14:42 +02001211 int64_t rtt = GetRTT(true);
1212 if (rtt == 0) {
1213 // Waiting for valid RTT.
1214 return 0;
1215 }
Erik Språng737336d2016-07-29 12:59:36 +02001216
1217 int64_t nack_window_ms = rtt;
1218 if (nack_window_ms < kMinRetransmissionWindowMs) {
1219 nack_window_ms = kMinRetransmissionWindowMs;
1220 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1221 nack_window_ms = kMaxRetransmissionWindowMs;
1222 }
1223 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1224
minyue7e304322016-10-12 05:00:55 -07001225 // Invoke audio encoders OnReceivedRtt().
1226 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1227 if (*encoder)
1228 (*encoder)->OnReceivedRtt(rtt);
1229 });
1230
Minyue2013aec2015-05-13 14:14:42 +02001231 uint32_t ntp_secs = 0;
1232 uint32_t ntp_frac = 0;
1233 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001234 if (0 !=
1235 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1236 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001237 // Waiting for RTCP.
1238 return 0;
1239 }
1240
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001241 {
tommi31fc21f2016-01-21 10:37:37 -08001242 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001243 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001244 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001245 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001246}
1247
solenberg8d73f8c2017-03-08 01:52:20 -08001248int Channel::GetSpeechOutputLevel() const {
1249 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00001250}
1251
solenberg8d73f8c2017-03-08 01:52:20 -08001252int Channel::GetSpeechOutputLevelFullRange() const {
1253 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08001254}
1255
zsteine76bd3a2017-07-14 12:17:49 -07001256double Channel::GetTotalOutputEnergy() const {
zstein3c451862017-07-20 09:57:42 -07001257 return _outputAudioLevel.TotalEnergy();
zsteine76bd3a2017-07-14 12:17:49 -07001258}
1259
1260double Channel::GetTotalOutputDuration() const {
zstein3c451862017-07-20 09:57:42 -07001261 return _outputAudioLevel.TotalDuration();
zsteine76bd3a2017-07-14 12:17:49 -07001262}
1263
solenberg8d73f8c2017-03-08 01:52:20 -08001264void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08001265 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07001266 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00001267}
1268
solenberg1c2af8e2016-03-24 10:36:00 -07001269bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08001270 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07001271 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001272}
1273
solenberg8d73f8c2017-03-08 01:52:20 -08001274void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08001275 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08001276 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00001277}
1278
solenberg8842c3e2016-03-11 03:06:41 -08001279int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
solenberg8842c3e2016-03-11 03:06:41 -08001280 RTC_DCHECK_LE(0, event);
1281 RTC_DCHECK_GE(255, event);
1282 RTC_DCHECK_LE(0, duration_ms);
1283 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08001284 if (!Sending()) {
1285 return -1;
1286 }
solenberg8842c3e2016-03-11 03:06:41 -08001287 if (_rtpRtcpModule->SendTelephoneEventOutband(
1288 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
solenberg1c239d42017-09-29 06:00:28 -07001289 LOG(LS_ERROR) << "SendTelephoneEventOutband() failed to send event";
kwiberg55b97fe2016-01-28 05:22:45 -08001290 return -1;
1291 }
1292 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001293}
1294
solenbergffbbcac2016-11-17 05:25:37 -08001295int Channel::SetSendTelephoneEventPayloadType(int payload_type,
1296 int payload_frequency) {
solenberg31642aa2016-03-14 08:00:37 -07001297 RTC_DCHECK_LE(0, payload_type);
1298 RTC_DCHECK_GE(127, payload_type);
1299 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07001300 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08001301 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08001302 memcpy(codec.plname, "telephone-event", 16);
1303 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1304 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1305 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
solenberg1c239d42017-09-29 06:00:28 -07001306 LOG(LS_ERROR) << "SetSendTelephoneEventPayloadType() failed to register "
1307 "send payload type";
kwiberg55b97fe2016-01-28 05:22:45 -08001308 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001309 }
kwiberg55b97fe2016-01-28 05:22:45 -08001310 }
kwiberg55b97fe2016-01-28 05:22:45 -08001311 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001312}
1313
kwiberg55b97fe2016-01-28 05:22:45 -08001314int Channel::SetLocalSSRC(unsigned int ssrc) {
kwiberg55b97fe2016-01-28 05:22:45 -08001315 if (channel_state_.Get().sending) {
solenberg1c239d42017-09-29 06:00:28 -07001316 LOG(LS_ERROR) << "SetLocalSSRC() already sending";
kwiberg55b97fe2016-01-28 05:22:45 -08001317 return -1;
1318 }
1319 _rtpRtcpModule->SetSSRC(ssrc);
1320 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001321}
1322
kwiberg55b97fe2016-01-28 05:22:45 -08001323int Channel::GetRemoteSSRC(unsigned int& ssrc) {
1324 ssrc = rtp_receiver_->SSRC();
1325 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001326}
1327
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001328int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00001329 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001330 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001331}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00001332
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00001333int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
1334 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08001335 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
1336 if (enable &&
1337 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
1338 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00001339 return -1;
1340 }
1341 return 0;
1342}
1343
Stefan Holmerb86d4e42015-12-07 10:26:18 +01001344void Channel::EnableSendTransportSequenceNumber(int id) {
1345 int ret =
1346 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
1347 RTC_DCHECK_EQ(0, ret);
1348}
1349
stefan3313ec92016-01-21 06:32:43 -08001350void Channel::EnableReceiveTransportSequenceNumber(int id) {
1351 rtp_header_parser_->DeregisterRtpHeaderExtension(
1352 kRtpExtensionTransportSequenceNumber);
1353 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
1354 kRtpExtensionTransportSequenceNumber, id);
1355 RTC_DCHECK(ret);
1356}
1357
stefanbba9dec2016-02-01 04:39:55 -08001358void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07001359 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08001360 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07001361 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
1362 TransportFeedbackObserver* transport_feedback_observer =
1363 transport->transport_feedback_observer();
1364 PacketRouter* packet_router = transport->packet_router();
1365
stefanbba9dec2016-02-01 04:39:55 -08001366 RTC_DCHECK(rtp_packet_sender);
1367 RTC_DCHECK(transport_feedback_observer);
kwibergee89e782017-08-09 17:22:01 -07001368 RTC_DCHECK(packet_router);
1369 RTC_DCHECK(!packet_router_);
stefan7de8d642017-02-07 07:14:08 -08001370 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08001371 feedback_observer_proxy_->SetTransportFeedbackObserver(
1372 transport_feedback_observer);
1373 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
1374 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
1375 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
eladalon822ff2b2017-08-01 06:30:28 -07001376 constexpr bool remb_candidate = false;
1377 packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
Stefan Holmerb86d4e42015-12-07 10:26:18 +01001378 packet_router_ = packet_router;
1379}
1380
stefanbba9dec2016-02-01 04:39:55 -08001381void Channel::RegisterReceiverCongestionControlObjects(
1382 PacketRouter* packet_router) {
kwibergee89e782017-08-09 17:22:01 -07001383 RTC_DCHECK(packet_router);
1384 RTC_DCHECK(!packet_router_);
eladalon822ff2b2017-08-01 06:30:28 -07001385 constexpr bool remb_candidate = false;
1386 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
stefanbba9dec2016-02-01 04:39:55 -08001387 packet_router_ = packet_router;
1388}
1389
nissefdbfdc92017-03-31 05:44:52 -07001390void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08001391 RTC_DCHECK(packet_router_);
1392 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08001393 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08001394 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
1395 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07001396 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08001397 packet_router_ = nullptr;
1398 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
1399}
1400
nissefdbfdc92017-03-31 05:44:52 -07001401void Channel::ResetReceiverCongestionControlObjects() {
1402 RTC_DCHECK(packet_router_);
1403 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
1404 packet_router_ = nullptr;
1405}
1406
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001407void Channel::SetRTCPStatus(bool enable) {
pbosda903ea2015-10-02 02:36:56 -07001408 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00001409}
1410
kwiberg55b97fe2016-01-28 05:22:45 -08001411int Channel::SetRTCP_CNAME(const char cName[256]) {
kwiberg55b97fe2016-01-28 05:22:45 -08001412 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
solenberg1c239d42017-09-29 06:00:28 -07001413 LOG(LS_ERROR) << "SetRTCP_CNAME() failed to set RTCP CNAME";
kwiberg55b97fe2016-01-28 05:22:45 -08001414 return -1;
1415 }
1416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001417}
1418
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001419int Channel::GetRemoteRTCPReportBlocks(
1420 std::vector<ReportBlock>* report_blocks) {
1421 if (report_blocks == NULL) {
solenberg1c239d42017-09-29 06:00:28 -07001422 LOG(LS_ERROR) << "GetRemoteRTCPReportBlock()s invalid report_blocks.";
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001423 return -1;
1424 }
1425
1426 // Get the report blocks from the latest received RTCP Sender or Receiver
1427 // Report. Each element in the vector contains the sender's SSRC and a
1428 // report block according to RFC 3550.
1429 std::vector<RTCPReportBlock> rtcp_report_blocks;
1430 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001431 return -1;
1432 }
1433
1434 if (rtcp_report_blocks.empty())
1435 return 0;
1436
1437 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
1438 for (; it != rtcp_report_blocks.end(); ++it) {
1439 ReportBlock report_block;
srte3e69e5c2017-08-09 06:13:45 -07001440 report_block.sender_SSRC = it->sender_ssrc;
1441 report_block.source_SSRC = it->source_ssrc;
1442 report_block.fraction_lost = it->fraction_lost;
1443 report_block.cumulative_num_packets_lost = it->packets_lost;
1444 report_block.extended_highest_sequence_number =
1445 it->extended_highest_sequence_number;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001446 report_block.interarrival_jitter = it->jitter;
srte3e69e5c2017-08-09 06:13:45 -07001447 report_block.last_SR_timestamp = it->last_sender_report_timestamp;
1448 report_block.delay_since_last_SR = it->delay_since_last_sender_report;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00001449 report_blocks->push_back(report_block);
1450 }
1451 return 0;
1452}
1453
kwiberg55b97fe2016-01-28 05:22:45 -08001454int Channel::GetRTPStatistics(CallStatistics& stats) {
1455 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00001456
kwiberg55b97fe2016-01-28 05:22:45 -08001457 // The jitter statistics is updated for each received RTP packet and is
1458 // based on received packets.
1459 RtcpStatistics statistics;
1460 StreamStatistician* statistician =
1461 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01001462 if (statistician) {
1463 statistician->GetStatistics(&statistics,
1464 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08001465 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001466
kwiberg55b97fe2016-01-28 05:22:45 -08001467 stats.fractionLost = statistics.fraction_lost;
srte186d9c32017-08-04 05:03:53 -07001468 stats.cumulativeLost = statistics.packets_lost;
1469 stats.extendedMax = statistics.extended_highest_sequence_number;
kwiberg55b97fe2016-01-28 05:22:45 -08001470 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00001471
kwiberg55b97fe2016-01-28 05:22:45 -08001472 // --- RTT
1473 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001474
kwiberg55b97fe2016-01-28 05:22:45 -08001475 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00001476
kwiberg55b97fe2016-01-28 05:22:45 -08001477 size_t bytesSent(0);
1478 uint32_t packetsSent(0);
1479 size_t bytesReceived(0);
1480 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001481
kwiberg55b97fe2016-01-28 05:22:45 -08001482 if (statistician) {
1483 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
1484 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001485
kwiberg55b97fe2016-01-28 05:22:45 -08001486 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001487 LOG(LS_WARNING) << "GetRTPStatistics() failed to retrieve RTP datacounters"
1488 << " => output will not be complete";
kwiberg55b97fe2016-01-28 05:22:45 -08001489 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001490
kwiberg55b97fe2016-01-28 05:22:45 -08001491 stats.bytesSent = bytesSent;
1492 stats.packetsSent = packetsSent;
1493 stats.bytesReceived = bytesReceived;
1494 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00001495
kwiberg55b97fe2016-01-28 05:22:45 -08001496 // --- Timestamps
1497 {
1498 rtc::CritScope lock(&ts_stats_lock_);
1499 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
1500 }
1501 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001502}
1503
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00001504void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
1505 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01001506 // If pacing is enabled we always store packets.
1507 if (!pacing_enabled_)
1508 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001509 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00001510 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001511 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00001512 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001513 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00001514}
1515
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00001516// Called when we are missing one or more packets.
1517int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00001518 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
1519}
1520
henrikaec6fbd22017-03-31 05:43:36 -07001521void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
henrika4515fa02017-05-03 08:30:15 -07001522 // Avoid posting any new tasks if sending was already stopped in StopSend().
1523 rtc::CritScope cs(&encoder_queue_lock_);
1524 if (!encoder_queue_is_active_) {
1525 return;
1526 }
henrikaec6fbd22017-03-31 05:43:36 -07001527 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
1528 // TODO(henrika): try to avoid copying by moving ownership of audio frame
1529 // either into pool of frames or into the task itself.
1530 audio_frame->CopyFrom(audio_input);
henrika45802172017-09-28 09:39:34 +02001531 // Profile time between when the audio frame is added to the task queue and
1532 // when the task is actually executed.
1533 audio_frame->UpdateProfileTimeStamp();
henrikaec6fbd22017-03-31 05:43:36 -07001534 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
1535 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00001536}
1537
henrikaec6fbd22017-03-31 05:43:36 -07001538void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
1539 int sample_rate,
1540 size_t number_of_frames,
1541 size_t number_of_channels) {
henrika4515fa02017-05-03 08:30:15 -07001542 // Avoid posting as new task if sending was already stopped in StopSend().
1543 rtc::CritScope cs(&encoder_queue_lock_);
1544 if (!encoder_queue_is_active_) {
1545 return;
1546 }
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00001547 CodecInst codec;
ossu950c1c92017-07-11 08:19:31 -07001548 const int result = GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07001549 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
ossu950c1c92017-07-11 08:19:31 -07001550 // TODO(ossu): Investigate how this could happen. b/62909493
1551 if (result == 0) {
1552 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
1553 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
1554 } else {
1555 audio_frame->sample_rate_hz_ = sample_rate;
1556 audio_frame->num_channels_ = number_of_channels;
1557 LOG(LS_WARNING) << "Unable to get send codec for channel " << ChannelId();
1558 RTC_NOTREACHED();
1559 }
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07001560 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07001561 sample_rate, &input_resampler_, audio_frame.get());
1562 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
1563 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00001564}
1565
henrikaec6fbd22017-03-31 05:43:36 -07001566void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
1567 RTC_DCHECK_RUN_ON(encoder_queue_);
1568 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
1569 RTC_DCHECK_LE(audio_input->num_channels_, 2);
kwiberg55b97fe2016-01-28 05:22:45 -08001570
henrika45802172017-09-28 09:39:34 +02001571 // Measure time between when the audio frame is added to the task queue and
1572 // when the task is actually executed. Goal is to keep track of unwanted
1573 // extra latency added by the task queue.
1574 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs",
1575 audio_input->ElapsedProfileTimeMs());
1576
henrikaec6fbd22017-03-31 05:43:36 -07001577 bool is_muted = InputMute();
1578 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08001579
kwiberg55b97fe2016-01-28 05:22:45 -08001580 if (_includeAudioLevelIndication) {
1581 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07001582 audio_input->samples_per_channel_ * audio_input->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07001583 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
solenberg1c2af8e2016-03-24 10:36:00 -07001584 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08001585 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08001586 } else {
henrik.lundin50499422016-11-29 04:26:24 -08001587 rms_level_.Analyze(
yujo36b1a5f2017-06-12 12:45:32 -07001588 rtc::ArrayView<const int16_t>(audio_input->data(), length));
niklase@google.com470e71d2011-07-07 08:21:25 +00001589 }
kwiberg55b97fe2016-01-28 05:22:45 -08001590 }
solenberg1c2af8e2016-03-24 10:36:00 -07001591 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00001592
henrikaec6fbd22017-03-31 05:43:36 -07001593 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00001594
kwiberg55b97fe2016-01-28 05:22:45 -08001595 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07001596 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08001597 // This call will trigger AudioPacketizationCallback::SendData if encoding
1598 // is done and payload is ready for packetization and transmission.
1599 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07001600 if (audio_coding_->Add10MsData(*audio_input) < 0) {
1601 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
1602 return;
kwiberg55b97fe2016-01-28 05:22:45 -08001603 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001604
henrikaec6fbd22017-03-31 05:43:36 -07001605 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001606}
1607
solenberg7602aab2016-11-14 11:30:07 -08001608void Channel::set_associate_send_channel(const ChannelOwner& channel) {
1609 RTC_DCHECK(!channel.channel() ||
1610 channel.channel()->ChannelId() != _channelId);
1611 rtc::CritScope lock(&assoc_send_channel_lock_);
1612 associate_send_channel_ = channel;
1613}
1614
Minyue2013aec2015-05-13 14:14:42 +02001615void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08001616 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02001617 Channel* channel = associate_send_channel_.channel();
1618 if (channel && channel->ChannelId() == channel_id) {
1619 // If this channel is associated with a send channel of the specified
1620 // Channel ID, disassociate with it.
1621 ChannelOwner ref(NULL);
1622 associate_send_channel_ = ref;
1623 }
1624}
1625
ivoc14d5dbe2016-07-04 07:06:55 -07001626void Channel::SetRtcEventLog(RtcEventLog* event_log) {
1627 event_log_proxy_->SetEventLog(event_log);
1628}
1629
michaelt9332b7d2016-11-30 07:51:13 -08001630void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
1631 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
1632}
1633
nisse284542b2017-01-10 08:58:32 -08001634void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08001635 size_t overhead_per_packet =
1636 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08001637 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1638 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08001639 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08001640 }
1641 });
1642}
1643
1644void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08001645 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08001646 transport_overhead_per_packet_ = transport_overhead_per_packet;
1647 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08001648}
1649
hbos3fd31fe2017-02-28 05:43:16 -08001650// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08001651void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08001652 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08001653 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
1654 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08001655}
1656
kwiberg55b97fe2016-01-28 05:22:45 -08001657int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
1658 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00001659}
1660
wu@webrtc.org24301a62013-12-13 19:17:43 +00001661void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
1662 audio_coding_->GetDecodingCallStatistics(stats);
1663}
1664
ivoce1198e02017-09-08 08:13:19 -07001665ANAStats Channel::GetANAStatistics() const {
1666 return audio_coding_->GetANAStats();
1667}
1668
solenberg358057b2015-11-27 10:46:42 -08001669uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08001670 rtc::CritScope lock(&video_sync_lock_);
1671 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07001672}
1673
kwiberg55b97fe2016-01-28 05:22:45 -08001674int Channel::SetMinimumPlayoutDelay(int delayMs) {
kwiberg55b97fe2016-01-28 05:22:45 -08001675 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
1676 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
solenberg1c239d42017-09-29 06:00:28 -07001677 LOG(LS_ERROR) << "SetMinimumPlayoutDelay() invalid min delay";
kwiberg55b97fe2016-01-28 05:22:45 -08001678 return -1;
1679 }
1680 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
solenberg1c239d42017-09-29 06:00:28 -07001681 LOG(LS_ERROR) << "SetMinimumPlayoutDelay() failed to set min playout delay";
kwiberg55b97fe2016-01-28 05:22:45 -08001682 return -1;
1683 }
1684 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001685}
1686
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001687int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07001688 uint32_t playout_timestamp_rtp = 0;
1689 {
tommi31fc21f2016-01-21 10:37:37 -08001690 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07001691 playout_timestamp_rtp = playout_timestamp_rtp_;
1692 }
kwiberg55b97fe2016-01-28 05:22:45 -08001693 if (playout_timestamp_rtp == 0) {
solenberg1c239d42017-09-29 06:00:28 -07001694 LOG(LS_ERROR) << "GetPlayoutTimestamp() failed to retrieve timestamp";
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001695 return -1;
1696 }
deadbeef74375882015-08-13 12:09:10 -07001697 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001698 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001699}
1700
kwiberg55b97fe2016-01-28 05:22:45 -08001701int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
1702 RtpReceiver** rtp_receiver) const {
1703 *rtpRtcpModule = _rtpRtcpModule.get();
1704 *rtp_receiver = rtp_receiver_.get();
1705 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001706}
1707
deadbeef74375882015-08-13 12:09:10 -07001708void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07001709 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07001710
henrik.lundin96bd5022016-04-06 04:13:56 -07001711 if (!jitter_buffer_playout_timestamp_) {
1712 // This can happen if this channel has not received any RTP packets. In
1713 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07001714 return;
1715 }
1716
1717 uint16_t delay_ms = 0;
1718 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001719 LOG(LS_WARNING) << "Channel::UpdatePlayoutTimestamp() failed to read"
1720 << " playout delay from the ADM";
deadbeef74375882015-08-13 12:09:10 -07001721 return;
1722 }
1723
henrik.lundin96bd5022016-04-06 04:13:56 -07001724 RTC_DCHECK(jitter_buffer_playout_timestamp_);
1725 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07001726
1727 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07001728 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07001729
deadbeef74375882015-08-13 12:09:10 -07001730 {
tommi31fc21f2016-01-21 10:37:37 -08001731 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08001732 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07001733 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07001734 }
1735 playout_delay_ms_ = delay_ms;
1736 }
1737}
1738
kwiberg55b97fe2016-01-28 05:22:45 -08001739void Channel::RegisterReceiveCodecsToRTPModule() {
kwiberg55b97fe2016-01-28 05:22:45 -08001740 CodecInst codec;
1741 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00001742
kwiberg55b97fe2016-01-28 05:22:45 -08001743 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1744 // Open up the RTP/RTCP receiver for all supported codecs
1745 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001746 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
Sam Zackrissonecc51e92017-10-02 14:32:33 +02001747 LOG(LS_WARNING) << "Channel::RegisterReceiveCodecsToRTPModule() unable"
1748 << " to register " << codec.plname << " (" << codec.pltype
1749 << "/" << codec.plfreq << "/" << codec.channels << "/"
1750 << codec.rate << ") to RTP/RTCP receiver";
niklase@google.com470e71d2011-07-07 08:21:25 +00001751 }
kwiberg55b97fe2016-01-28 05:22:45 -08001752 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001753}
1754
kwiberg55b97fe2016-01-28 05:22:45 -08001755int Channel::SetSendRtpHeaderExtension(bool enable,
1756 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001757 unsigned char id) {
1758 int error = 0;
1759 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
1760 if (enable) {
1761 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
1762 }
1763 return error;
1764}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001765
ossue280cde2016-10-12 11:04:10 -07001766int Channel::GetRtpTimestampRateHz() const {
1767 const auto format = audio_coding_->ReceiveFormat();
1768 // Default to the playout frequency if we've not gotten any packets yet.
1769 // TODO(ossu): Zero clockrate can only happen if we've added an external
1770 // decoder for a format we don't support internally. Remove once that way of
1771 // adding decoders is gone!
1772 return (format && format->clockrate_hz != 0)
1773 ? format->clockrate_hz
1774 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00001775}
1776
Minyue2013aec2015-05-13 14:14:42 +02001777int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07001778 RtcpMode method = _rtpRtcpModule->RTCP();
1779 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001780 return 0;
1781 }
1782 std::vector<RTCPReportBlock> report_blocks;
1783 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02001784
1785 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001786 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02001787 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08001788 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02001789 Channel* channel = associate_send_channel_.channel();
1790 // Tries to get RTT from an associated channel. This is important for
1791 // receive-only channels.
1792 if (channel) {
1793 // To prevent infinite recursion and deadlock, calling GetRTT of
1794 // associate channel should always use "false" for argument:
1795 // |allow_associate_channel|.
1796 rtt = channel->GetRTT(false);
1797 }
1798 }
1799 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001800 }
1801
1802 uint32_t remoteSSRC = rtp_receiver_->SSRC();
1803 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
1804 for (; it != report_blocks.end(); ++it) {
srte3e69e5c2017-08-09 06:13:45 -07001805 if (it->sender_ssrc == remoteSSRC)
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001806 break;
1807 }
1808 if (it == report_blocks.end()) {
1809 // We have not received packets with SSRC matching the report blocks.
1810 // To calculate RTT we try with the SSRC of the first report block.
1811 // This is very important for send-only channels where we don't know
1812 // the SSRC of the other end.
srte3e69e5c2017-08-09 06:13:45 -07001813 remoteSSRC = report_blocks[0].sender_ssrc;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001814 }
Minyue2013aec2015-05-13 14:14:42 +02001815
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001816 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001817 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001818 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001819 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
1820 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001821 return 0;
1822 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001823 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00001824}
1825
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001826} // namespace voe
1827} // namespace webrtc