blob: bbc63895ee26ec57e7068bf33ed1b5076d3facd3 [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
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
Ivo Creusenae856f22015-09-17 16:30:16 +020016#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080017#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000018#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080019#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020020#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000022#include "webrtc/base/timeutils.h"
ivoc14d5dbe2016-07-04 07:06:55 -070023#include "webrtc/call/rtc_event_log.h"
minyue@webrtc.orge509f942013-09-12 17:03:00 +000024#include "webrtc/common.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020025#include "webrtc/config.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000026#include "webrtc/modules/audio_device/include/audio_device.h"
27#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010029#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
32#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000033#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010034#include "webrtc/modules/utility/include/audio_frame_operations.h"
35#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010036#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000037#include "webrtc/voice_engine/include/voe_base.h"
38#include "webrtc/voice_engine/include/voe_external_media.h"
39#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
40#include "webrtc/voice_engine/output_mixer.h"
41#include "webrtc/voice_engine/statistics.h"
42#include "webrtc/voice_engine/transmit_mixer.h"
43#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000044
andrew@webrtc.org50419b02012-11-14 19:07:54 +000045namespace webrtc {
46namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000047
kwibergc8d071e2016-04-06 12:22:38 -070048namespace {
49
Erik Språng737336d2016-07-29 12:59:36 +020050constexpr int64_t kMaxRetransmissionWindowMs = 1000;
51constexpr int64_t kMinRetransmissionWindowMs = 30;
52
kwibergc8d071e2016-04-06 12:22:38 -070053bool RegisterReceiveCodec(std::unique_ptr<AudioCodingModule>* acm,
54 acm2::RentACodec* rac,
55 const CodecInst& ci) {
kwibergabe95ba2016-06-02 02:58:59 -070056 const int result = (*acm)->RegisterReceiveCodec(
57 ci, [&] { return rac->RentIsacDecoder(ci.plfreq); });
kwibergc8d071e2016-04-06 12:22:38 -070058 return result == 0;
59}
60
61} // namespace
62
solenberg8842c3e2016-03-11 03:06:41 -080063const int kTelephoneEventAttenuationdB = 10;
64
ivoc14d5dbe2016-07-04 07:06:55 -070065class RtcEventLogProxy final : public webrtc::RtcEventLog {
66 public:
67 RtcEventLogProxy() : event_log_(nullptr) {}
68
69 bool StartLogging(const std::string& file_name,
70 int64_t max_size_bytes) override {
71 RTC_NOTREACHED();
72 return false;
73 }
74
75 bool StartLogging(rtc::PlatformFile log_file,
76 int64_t max_size_bytes) override {
77 RTC_NOTREACHED();
78 return false;
79 }
80
81 void StopLogging() override { RTC_NOTREACHED(); }
82
83 void LogVideoReceiveStreamConfig(
84 const webrtc::VideoReceiveStream::Config& config) override {
85 rtc::CritScope lock(&crit_);
86 if (event_log_) {
87 event_log_->LogVideoReceiveStreamConfig(config);
88 }
89 }
90
91 void LogVideoSendStreamConfig(
92 const webrtc::VideoSendStream::Config& config) override {
93 rtc::CritScope lock(&crit_);
94 if (event_log_) {
95 event_log_->LogVideoSendStreamConfig(config);
96 }
97 }
98
99 void LogRtpHeader(webrtc::PacketDirection direction,
100 webrtc::MediaType media_type,
101 const uint8_t* header,
102 size_t packet_length) override {
103 rtc::CritScope lock(&crit_);
104 if (event_log_) {
105 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
106 }
107 }
108
109 void LogRtcpPacket(webrtc::PacketDirection direction,
110 webrtc::MediaType media_type,
111 const uint8_t* packet,
112 size_t length) override {
113 rtc::CritScope lock(&crit_);
114 if (event_log_) {
115 event_log_->LogRtcpPacket(direction, media_type, packet, length);
116 }
117 }
118
119 void LogAudioPlayout(uint32_t ssrc) override {
120 rtc::CritScope lock(&crit_);
121 if (event_log_) {
122 event_log_->LogAudioPlayout(ssrc);
123 }
124 }
125
126 void LogBwePacketLossEvent(int32_t bitrate,
127 uint8_t fraction_loss,
128 int32_t total_packets) override {
129 rtc::CritScope lock(&crit_);
130 if (event_log_) {
131 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
132 }
133 }
134
135 void SetEventLog(RtcEventLog* event_log) {
136 rtc::CritScope lock(&crit_);
137 event_log_ = event_log;
138 }
139
140 private:
141 rtc::CriticalSection crit_;
142 RtcEventLog* event_log_ GUARDED_BY(crit_);
143 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
144};
145
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100146class TransportFeedbackProxy : public TransportFeedbackObserver {
147 public:
148 TransportFeedbackProxy() : feedback_observer_(nullptr) {
149 pacer_thread_.DetachFromThread();
150 network_thread_.DetachFromThread();
151 }
152
153 void SetTransportFeedbackObserver(
154 TransportFeedbackObserver* feedback_observer) {
155 RTC_DCHECK(thread_checker_.CalledOnValidThread());
156 rtc::CritScope lock(&crit_);
157 feedback_observer_ = feedback_observer;
158 }
159
160 // Implements TransportFeedbackObserver.
161 void AddPacket(uint16_t sequence_number,
162 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700163 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100164 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
165 rtc::CritScope lock(&crit_);
166 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700167 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100168 }
169 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
170 RTC_DCHECK(network_thread_.CalledOnValidThread());
171 rtc::CritScope lock(&crit_);
172 if (feedback_observer_)
173 feedback_observer_->OnTransportFeedback(feedback);
174 }
175
176 private:
177 rtc::CriticalSection crit_;
178 rtc::ThreadChecker thread_checker_;
179 rtc::ThreadChecker pacer_thread_;
180 rtc::ThreadChecker network_thread_;
181 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
182};
183
184class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
185 public:
186 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
187 pacer_thread_.DetachFromThread();
188 }
189
190 void SetSequenceNumberAllocator(
191 TransportSequenceNumberAllocator* seq_num_allocator) {
192 RTC_DCHECK(thread_checker_.CalledOnValidThread());
193 rtc::CritScope lock(&crit_);
194 seq_num_allocator_ = seq_num_allocator;
195 }
196
197 // Implements TransportSequenceNumberAllocator.
198 uint16_t AllocateSequenceNumber() override {
199 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
200 rtc::CritScope lock(&crit_);
201 if (!seq_num_allocator_)
202 return 0;
203 return seq_num_allocator_->AllocateSequenceNumber();
204 }
205
206 private:
207 rtc::CriticalSection crit_;
208 rtc::ThreadChecker thread_checker_;
209 rtc::ThreadChecker pacer_thread_;
210 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
211};
212
213class RtpPacketSenderProxy : public RtpPacketSender {
214 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800215 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100216
217 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
218 RTC_DCHECK(thread_checker_.CalledOnValidThread());
219 rtc::CritScope lock(&crit_);
220 rtp_packet_sender_ = rtp_packet_sender;
221 }
222
223 // Implements RtpPacketSender.
224 void InsertPacket(Priority priority,
225 uint32_t ssrc,
226 uint16_t sequence_number,
227 int64_t capture_time_ms,
228 size_t bytes,
229 bool retransmission) override {
230 rtc::CritScope lock(&crit_);
231 if (rtp_packet_sender_) {
232 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
233 capture_time_ms, bytes, retransmission);
234 }
235 }
236
237 private:
238 rtc::ThreadChecker thread_checker_;
239 rtc::CriticalSection crit_;
240 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
241};
242
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000243// Extend the default RTCP statistics struct with max_jitter, defined as the
244// maximum jitter value seen in an RTCP report block.
245struct ChannelStatistics : public RtcpStatistics {
246 ChannelStatistics() : rtcp(), max_jitter(0) {}
247
248 RtcpStatistics rtcp;
249 uint32_t max_jitter;
250};
251
252// Statistics callback, called at each generation of a new RTCP report block.
253class StatisticsProxy : public RtcpStatisticsCallback {
254 public:
tommi31fc21f2016-01-21 10:37:37 -0800255 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000256 virtual ~StatisticsProxy() {}
257
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000258 void StatisticsUpdated(const RtcpStatistics& statistics,
259 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000260 if (ssrc != ssrc_)
261 return;
262
tommi31fc21f2016-01-21 10:37:37 -0800263 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000264 stats_.rtcp = statistics;
265 if (statistics.jitter > stats_.max_jitter) {
266 stats_.max_jitter = statistics.jitter;
267 }
268 }
269
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000270 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000271
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000272 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800273 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000274 return stats_;
275 }
276
277 private:
278 // StatisticsUpdated calls are triggered from threads in the RTP module,
279 // while GetStats calls can be triggered from the public voice engine API,
280 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800281 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000282 const uint32_t ssrc_;
283 ChannelStatistics stats_;
284};
285
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000286class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000287 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000288 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
289 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000290
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000291 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
292 // Not used for Voice Engine.
293 }
294
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000295 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
296 int64_t rtt,
297 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000298 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
299 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
300 // report for VoiceEngine?
301 if (report_blocks.empty())
302 return;
303
304 int fraction_lost_aggregate = 0;
305 int total_number_of_packets = 0;
306
307 // If receiving multiple report blocks, calculate the weighted average based
308 // on the number of packets a report refers to.
309 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
310 block_it != report_blocks.end(); ++block_it) {
311 // Find the previous extended high sequence number for this remote SSRC,
312 // to calculate the number of RTP packets this report refers to. Ignore if
313 // we haven't seen this SSRC before.
314 std::map<uint32_t, uint32_t>::iterator seq_num_it =
315 extended_max_sequence_number_.find(block_it->sourceSSRC);
316 int number_of_packets = 0;
317 if (seq_num_it != extended_max_sequence_number_.end()) {
318 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
319 }
320 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
321 total_number_of_packets += number_of_packets;
322
323 extended_max_sequence_number_[block_it->sourceSSRC] =
324 block_it->extendedHighSeqNum;
325 }
326 int weighted_fraction_lost = 0;
327 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800328 weighted_fraction_lost =
329 (fraction_lost_aggregate + total_number_of_packets / 2) /
330 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000331 }
332 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000333 }
334
335 private:
336 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000337 // Maps remote side ssrc to extended highest sequence number received.
338 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000339};
340
kwiberg55b97fe2016-01-28 05:22:45 -0800341int32_t Channel::SendData(FrameType frameType,
342 uint8_t payloadType,
343 uint32_t timeStamp,
344 const uint8_t* payloadData,
345 size_t payloadSize,
346 const RTPFragmentationHeader* fragmentation) {
347 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
348 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
349 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
350 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000351
kwiberg55b97fe2016-01-28 05:22:45 -0800352 if (_includeAudioLevelIndication) {
353 // Store current audio level in the RTP/RTCP module.
354 // The level will be used in combination with voice-activity state
355 // (frameType) to add an RTP header extension
356 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
357 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000358
kwiberg55b97fe2016-01-28 05:22:45 -0800359 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
360 // packetization.
361 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700362 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800363 (FrameType&)frameType, payloadType, timeStamp,
364 // Leaving the time when this frame was
365 // received from the capture device as
366 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700367 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800368 _engineStatisticsPtr->SetLastError(
369 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
370 "Channel::SendData() failed to send data to RTP/RTCP module");
371 return -1;
372 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000373
kwiberg55b97fe2016-01-28 05:22:45 -0800374 _lastLocalTimeStamp = timeStamp;
375 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000376
kwiberg55b97fe2016-01-28 05:22:45 -0800377 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000378}
379
kwiberg55b97fe2016-01-28 05:22:45 -0800380int32_t Channel::InFrameType(FrameType frame_type) {
381 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
382 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
kwiberg55b97fe2016-01-28 05:22:45 -0800384 rtc::CritScope cs(&_callbackCritSect);
385 _sendFrameType = (frame_type == kAudioFrameSpeech);
386 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387}
388
kwiberg55b97fe2016-01-28 05:22:45 -0800389int32_t Channel::OnRxVadDetected(int vadDecision) {
390 rtc::CritScope cs(&_callbackCritSect);
391 if (_rxVadObserverPtr) {
392 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
393 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
kwiberg55b97fe2016-01-28 05:22:45 -0800395 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
stefan1d8a5062015-10-02 03:39:33 -0700398bool Channel::SendRtp(const uint8_t* data,
399 size_t len,
400 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800401 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
402 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
kwiberg55b97fe2016-01-28 05:22:45 -0800404 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000405
kwiberg55b97fe2016-01-28 05:22:45 -0800406 if (_transportPtr == NULL) {
407 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
408 "Channel::SendPacket() failed to send RTP packet due to"
409 " invalid transport object");
410 return false;
411 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000412
kwiberg55b97fe2016-01-28 05:22:45 -0800413 uint8_t* bufferToSendPtr = (uint8_t*)data;
414 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000415
kwiberg55b97fe2016-01-28 05:22:45 -0800416 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
417 std::string transport_name =
418 _externalTransport ? "external transport" : "WebRtc sockets";
419 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
420 "Channel::SendPacket() RTP transmission using %s failed",
421 transport_name.c_str());
422 return false;
423 }
424 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000425}
426
kwiberg55b97fe2016-01-28 05:22:45 -0800427bool Channel::SendRtcp(const uint8_t* data, size_t len) {
428 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
429 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
kwiberg55b97fe2016-01-28 05:22:45 -0800431 rtc::CritScope cs(&_callbackCritSect);
432 if (_transportPtr == NULL) {
433 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
434 "Channel::SendRtcp() failed to send RTCP packet"
435 " due to invalid transport object");
436 return false;
437 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
kwiberg55b97fe2016-01-28 05:22:45 -0800439 uint8_t* bufferToSendPtr = (uint8_t*)data;
440 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
kwiberg55b97fe2016-01-28 05:22:45 -0800442 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
443 if (n < 0) {
444 std::string transport_name =
445 _externalTransport ? "external transport" : "WebRtc sockets";
446 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
447 "Channel::SendRtcp() transmission using %s failed",
448 transport_name.c_str());
449 return false;
450 }
451 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000452}
453
kwiberg55b97fe2016-01-28 05:22:45 -0800454void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
455 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
456 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
kwiberg55b97fe2016-01-28 05:22:45 -0800458 // Update ssrc so that NTP for AV sync can be updated.
459 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
Peter Boströmac547a62015-09-17 23:03:57 +0200462void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
463 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
464 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
465 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000466}
467
Peter Boströmac547a62015-09-17 23:03:57 +0200468int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000469 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000470 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000471 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800472 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200473 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800474 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
475 "Channel::OnInitializeDecoder(payloadType=%d, "
476 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
477 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
kwiberg55b97fe2016-01-28 05:22:45 -0800479 CodecInst receiveCodec = {0};
480 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
kwiberg55b97fe2016-01-28 05:22:45 -0800482 receiveCodec.pltype = payloadType;
483 receiveCodec.plfreq = frequency;
484 receiveCodec.channels = channels;
485 receiveCodec.rate = rate;
486 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000487
kwiberg55b97fe2016-01-28 05:22:45 -0800488 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
489 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
kwiberg55b97fe2016-01-28 05:22:45 -0800491 // Register the new codec to the ACM
kwibergc8d071e2016-04-06 12:22:38 -0700492 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, receiveCodec)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800493 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
494 "Channel::OnInitializeDecoder() invalid codec ("
495 "pt=%d, name=%s) received - 1",
496 payloadType, payloadName);
497 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
498 return -1;
499 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000500
kwiberg55b97fe2016-01-28 05:22:45 -0800501 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000502}
503
kwiberg55b97fe2016-01-28 05:22:45 -0800504int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
505 size_t payloadSize,
506 const WebRtcRTPHeader* rtpHeader) {
507 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
508 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
509 ","
510 " payloadType=%u, audioChannel=%" PRIuS ")",
511 payloadSize, rtpHeader->header.payloadType,
512 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000513
kwiberg55b97fe2016-01-28 05:22:45 -0800514 if (!channel_state_.Get().playing) {
515 // Avoid inserting into NetEQ when we are not playing. Count the
516 // packet as discarded.
517 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
518 "received packet is discarded since playing is not"
519 " activated");
520 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000521 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800522 }
523
524 // Push the incoming payload (parsed and ready for decoding) into the ACM
525 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
526 0) {
527 _engineStatisticsPtr->SetLastError(
528 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
529 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
530 return -1;
531 }
532
533 // Update the packet delay.
534 UpdatePacketDelay(rtpHeader->header.timestamp,
535 rtpHeader->header.sequenceNumber);
536
537 int64_t round_trip_time = 0;
538 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
539 NULL);
540
541 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
542 if (!nack_list.empty()) {
543 // Can't use nack_list.data() since it's not supported by all
544 // compilers.
545 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
546 }
547 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000548}
549
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000550bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000551 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000552 RTPHeader header;
553 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
554 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
555 "IncomingPacket invalid RTP header");
556 return false;
557 }
558 header.payload_type_frequency =
559 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
560 if (header.payload_type_frequency < 0)
561 return false;
562 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
563}
564
henrik.lundin42dda502016-05-18 05:36:01 -0700565MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
566 int32_t id,
567 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700568 unsigned int ssrc;
569 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
570 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800571 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700572 bool muted;
573 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
574 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800575 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
576 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
577 // In all likelihood, the audio in this frame is garbage. We return an
578 // error so that the audio mixer module doesn't add it to the mix. As
579 // a result, it won't be played out and the actions skipped here are
580 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700581 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800582 }
henrik.lundina89ab962016-05-18 08:52:45 -0700583
584 if (muted) {
585 // TODO(henrik.lundin): We should be able to do better than this. But we
586 // will have to go through all the cases below where the audio samples may
587 // be used, and handle the muted case in some way.
588 audioFrame->Mute();
589 }
kwiberg55b97fe2016-01-28 05:22:45 -0800590
591 if (_RxVadDetection) {
592 UpdateRxVadDetection(*audioFrame);
593 }
594
595 // Convert module ID to internal VoE channel ID
596 audioFrame->id_ = VoEChannelId(audioFrame->id_);
597 // Store speech type for dead-or-alive detection
598 _outputSpeechType = audioFrame->speech_type_;
599
600 ChannelState::State state = channel_state_.Get();
601
602 if (state.rx_apm_is_enabled) {
603 int err = rx_audioproc_->ProcessStream(audioFrame);
604 if (err) {
605 LOG(LS_ERROR) << "ProcessStream() error: " << err;
606 assert(false);
Ivo Creusenae856f22015-09-17 16:30:16 +0200607 }
kwiberg55b97fe2016-01-28 05:22:45 -0800608 }
609
610 {
611 // Pass the audio buffers to an optional sink callback, before applying
612 // scaling/panning, as that applies to the mix operation.
613 // External recipients of the audio (e.g. via AudioTrack), will do their
614 // own mixing/dynamic processing.
615 rtc::CritScope cs(&_callbackCritSect);
616 if (audio_sink_) {
617 AudioSinkInterface::Data data(
618 &audioFrame->data_[0], audioFrame->samples_per_channel_,
619 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
620 audioFrame->timestamp_);
621 audio_sink_->OnData(data);
622 }
623 }
624
625 float output_gain = 1.0f;
626 float left_pan = 1.0f;
627 float right_pan = 1.0f;
628 {
629 rtc::CritScope cs(&volume_settings_critsect_);
630 output_gain = _outputGain;
631 left_pan = _panLeft;
632 right_pan = _panRight;
633 }
634
635 // Output volume scaling
636 if (output_gain < 0.99f || output_gain > 1.01f) {
637 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
638 }
639
640 // Scale left and/or right channel(s) if stereo and master balance is
641 // active
642
643 if (left_pan != 1.0f || right_pan != 1.0f) {
644 if (audioFrame->num_channels_ == 1) {
645 // Emulate stereo mode since panning is active.
646 // The mono signal is copied to both left and right channels here.
647 AudioFrameOperations::MonoToStereo(audioFrame);
648 }
649 // For true stereo mode (when we are receiving a stereo signal), no
650 // action is needed.
651
652 // Do the panning operation (the audio frame contains stereo at this
653 // stage)
654 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
655 }
656
657 // Mix decoded PCM output with file if file mixing is enabled
658 if (state.output_file_playing) {
659 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700660 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800661 }
662
663 // External media
664 if (_outputExternalMedia) {
665 rtc::CritScope cs(&_callbackCritSect);
666 const bool isStereo = (audioFrame->num_channels_ == 2);
667 if (_outputExternalMediaCallbackPtr) {
668 _outputExternalMediaCallbackPtr->Process(
669 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
670 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
671 isStereo);
672 }
673 }
674
675 // Record playout if enabled
676 {
677 rtc::CritScope cs(&_fileCritSect);
678
kwiberg5a25d952016-08-17 07:31:12 -0700679 if (_outputFileRecording && output_file_recorder_) {
680 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800681 }
682 }
683
684 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700685 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800686 _outputAudioLevel.ComputeLevel(*audioFrame);
687
688 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
689 // The first frame with a valid rtp timestamp.
690 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
691 }
692
693 if (capture_start_rtp_time_stamp_ >= 0) {
694 // audioFrame.timestamp_ should be valid from now on.
695
696 // Compute elapsed time.
697 int64_t unwrap_timestamp =
698 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
699 audioFrame->elapsed_time_ms_ =
700 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
701 (GetPlayoutFrequency() / 1000);
702
niklase@google.com470e71d2011-07-07 08:21:25 +0000703 {
kwiberg55b97fe2016-01-28 05:22:45 -0800704 rtc::CritScope lock(&ts_stats_lock_);
705 // Compute ntp time.
706 audioFrame->ntp_time_ms_ =
707 ntp_estimator_.Estimate(audioFrame->timestamp_);
708 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
709 if (audioFrame->ntp_time_ms_ > 0) {
710 // Compute |capture_start_ntp_time_ms_| so that
711 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
712 capture_start_ntp_time_ms_ =
713 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000714 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 }
kwiberg55b97fe2016-01-28 05:22:45 -0800716 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000717
henrik.lundin42dda502016-05-18 05:36:01 -0700718 return muted ? MixerParticipant::AudioFrameInfo::kMuted
719 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000720}
721
kwiberg55b97fe2016-01-28 05:22:45 -0800722int32_t Channel::NeededFrequency(int32_t id) const {
723 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
724 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000725
kwiberg55b97fe2016-01-28 05:22:45 -0800726 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
kwiberg55b97fe2016-01-28 05:22:45 -0800728 // Determine highest needed receive frequency
729 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000730
kwiberg55b97fe2016-01-28 05:22:45 -0800731 // Return the bigger of playout and receive frequency in the ACM.
732 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
733 highestNeeded = audio_coding_->PlayoutFrequency();
734 } else {
735 highestNeeded = receiveFrequency;
736 }
737
738 // Special case, if we're playing a file on the playout side
739 // we take that frequency into consideration as well
740 // This is not needed on sending side, since the codec will
741 // limit the spectrum anyway.
742 if (channel_state_.Get().output_file_playing) {
743 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700744 if (output_file_player_) {
745 if (output_file_player_->Frequency() > highestNeeded) {
746 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800747 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 }
kwiberg55b97fe2016-01-28 05:22:45 -0800749 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000750
kwiberg55b97fe2016-01-28 05:22:45 -0800751 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000752}
753
ossu5f7cfa52016-05-30 08:11:28 -0700754int32_t Channel::CreateChannel(
755 Channel*& channel,
756 int32_t channelId,
757 uint32_t instanceId,
ossu5f7cfa52016-05-30 08:11:28 -0700758 const Config& config,
759 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800760 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
761 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
762 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000763
ivoc14d5dbe2016-07-04 07:06:55 -0700764 channel = new Channel(channelId, instanceId, config, decoder_factory);
kwiberg55b97fe2016-01-28 05:22:45 -0800765 if (channel == NULL) {
766 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
767 "Channel::CreateChannel() unable to allocate memory for"
768 " channel");
769 return -1;
770 }
771 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000772}
773
kwiberg55b97fe2016-01-28 05:22:45 -0800774void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
775 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
776 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
777 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000778
kwiberg55b97fe2016-01-28 05:22:45 -0800779 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000780}
781
kwiberg55b97fe2016-01-28 05:22:45 -0800782void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
783 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
784 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
785 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000786
kwiberg55b97fe2016-01-28 05:22:45 -0800787 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000788}
789
kwiberg55b97fe2016-01-28 05:22:45 -0800790void Channel::PlayFileEnded(int32_t id) {
791 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
792 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000793
kwiberg55b97fe2016-01-28 05:22:45 -0800794 if (id == _inputFilePlayerId) {
795 channel_state_.SetInputFilePlaying(false);
796 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
797 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000798 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800799 } else if (id == _outputFilePlayerId) {
800 channel_state_.SetOutputFilePlaying(false);
801 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
802 "Channel::PlayFileEnded() => output file player module is"
803 " shutdown");
804 }
805}
806
807void Channel::RecordFileEnded(int32_t id) {
808 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
809 "Channel::RecordFileEnded(id=%d)", id);
810
811 assert(id == _outputFileRecorderId);
812
813 rtc::CritScope cs(&_fileCritSect);
814
815 _outputFileRecording = false;
816 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
817 "Channel::RecordFileEnded() => output file recorder module is"
818 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000819}
820
pbos@webrtc.org92135212013-05-14 08:31:39 +0000821Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000822 uint32_t instanceId,
ossu5f7cfa52016-05-30 08:11:28 -0700823 const Config& config,
824 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory)
tommi31fc21f2016-01-21 10:37:37 -0800825 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100826 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700827 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100828 rtp_header_parser_(RtpHeaderParser::Create()),
829 rtp_payload_registry_(
830 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
831 rtp_receive_statistics_(
832 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
833 rtp_receiver_(
834 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100835 this,
836 this,
837 rtp_payload_registry_.get())),
838 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
839 _outputAudioLevel(),
840 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100841 // Avoid conflict with other channels by adding 1024 - 1026,
842 // won't use as much as 1024 channels.
843 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
844 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
845 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
846 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100847 _outputExternalMedia(false),
848 _inputExternalMediaCallbackPtr(NULL),
849 _outputExternalMediaCallbackPtr(NULL),
850 _timeStamp(0), // This is just an offset, RTP module will add it's own
851 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100852 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100853 playout_timestamp_rtp_(0),
854 playout_timestamp_rtcp_(0),
855 playout_delay_ms_(0),
856 _numberOfDiscardedPackets(0),
857 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100858 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
859 capture_start_rtp_time_stamp_(-1),
860 capture_start_ntp_time_ms_(-1),
861 _engineStatisticsPtr(NULL),
862 _outputMixerPtr(NULL),
863 _transmitMixerPtr(NULL),
864 _moduleProcessThreadPtr(NULL),
865 _audioDeviceModulePtr(NULL),
866 _voiceEngineObserverPtr(NULL),
867 _callbackCritSectPtr(NULL),
868 _transportPtr(NULL),
869 _rxVadObserverPtr(NULL),
870 _oldVadDecision(-1),
871 _sendFrameType(0),
872 _externalMixing(false),
873 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700874 input_mute_(false),
875 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100876 _panLeft(1.0f),
877 _panRight(1.0f),
878 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100879 _lastLocalTimeStamp(0),
880 _lastPayloadType(0),
881 _includeAudioLevelIndication(false),
882 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 _average_jitter_buffer_delay_us(0),
884 _previousTimestamp(0),
885 _recPacketDelayMs(20),
886 _RxVadDetection(false),
887 _rxAgcIsEnabled(false),
888 _rxNsIsEnabled(false),
889 restored_packet_in_use_(false),
890 rtcp_observer_(new VoERtcpObserver(this)),
891 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100892 associate_send_channel_(ChannelOwner(nullptr)),
893 pacing_enabled_(config.Get<VoicePacing>().enabled),
stefanbba9dec2016-02-01 04:39:55 -0800894 feedback_observer_proxy_(new TransportFeedbackProxy()),
895 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700896 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200897 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
898 kMaxRetransmissionWindowMs)),
ossu29b1a8d2016-06-13 07:34:51 -0700899 decoder_factory_(decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800900 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
901 "Channel::Channel() - ctor");
902 AudioCodingModule::Config acm_config;
903 acm_config.id = VoEModuleId(instanceId, channelId);
904 if (config.Get<NetEqCapacityConfig>().enabled) {
905 // Clamping the buffer capacity at 20 packets. While going lower will
906 // probably work, it makes little sense.
907 acm_config.neteq_config.max_packets_in_buffer =
908 std::max(20, config.Get<NetEqCapacityConfig>().capacity);
909 }
910 acm_config.neteq_config.enable_fast_accelerate =
911 config.Get<NetEqFastAccelerate>().enabled;
henrik.lundina89ab962016-05-18 08:52:45 -0700912 acm_config.neteq_config.enable_muted_state = true;
ossu5f7cfa52016-05-30 08:11:28 -0700913 acm_config.decoder_factory = decoder_factory;
kwiberg55b97fe2016-01-28 05:22:45 -0800914 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200915
kwiberg55b97fe2016-01-28 05:22:45 -0800916 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000917
kwiberg55b97fe2016-01-28 05:22:45 -0800918 RtpRtcp::Configuration configuration;
919 configuration.audio = true;
920 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800921 configuration.receive_statistics = rtp_receive_statistics_.get();
922 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800923 if (pacing_enabled_) {
924 configuration.paced_sender = rtp_packet_sender_proxy_.get();
925 configuration.transport_sequence_number_allocator =
926 seq_num_allocator_proxy_.get();
927 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
928 }
ivoc14d5dbe2016-07-04 07:06:55 -0700929 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200930 configuration.retransmission_rate_limiter =
931 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000932
kwiberg55b97fe2016-01-28 05:22:45 -0800933 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100934 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000935
kwiberg55b97fe2016-01-28 05:22:45 -0800936 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
937 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
938 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000939
kwiberg55b97fe2016-01-28 05:22:45 -0800940 Config audioproc_config;
941 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
942 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000943}
944
kwiberg55b97fe2016-01-28 05:22:45 -0800945Channel::~Channel() {
946 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
947 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
948 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000949
kwiberg55b97fe2016-01-28 05:22:45 -0800950 if (_outputExternalMedia) {
951 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
952 }
953 if (channel_state_.Get().input_external_media) {
954 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
955 }
956 StopSend();
957 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000958
kwiberg55b97fe2016-01-28 05:22:45 -0800959 {
960 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700961 if (input_file_player_) {
962 input_file_player_->RegisterModuleFileCallback(NULL);
963 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000964 }
kwiberg5a25d952016-08-17 07:31:12 -0700965 if (output_file_player_) {
966 output_file_player_->RegisterModuleFileCallback(NULL);
967 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800968 }
kwiberg5a25d952016-08-17 07:31:12 -0700969 if (output_file_recorder_) {
970 output_file_recorder_->RegisterModuleFileCallback(NULL);
971 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800972 }
973 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000974
kwiberg55b97fe2016-01-28 05:22:45 -0800975 // The order to safely shutdown modules in a channel is:
976 // 1. De-register callbacks in modules
977 // 2. De-register modules in process thread
978 // 3. Destroy modules
979 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
980 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
981 "~Channel() failed to de-register transport callback"
982 " (Audio coding module)");
983 }
984 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
985 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
986 "~Channel() failed to de-register VAD callback"
987 " (Audio coding module)");
988 }
989 // De-register modules in process thread
990 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000991
kwiberg55b97fe2016-01-28 05:22:45 -0800992 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000993}
994
kwiberg55b97fe2016-01-28 05:22:45 -0800995int32_t Channel::Init() {
996 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
997 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000998
kwiberg55b97fe2016-01-28 05:22:45 -0800999 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001000
kwiberg55b97fe2016-01-28 05:22:45 -08001001 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +00001002
kwiberg55b97fe2016-01-28 05:22:45 -08001003 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
1004 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1005 "Channel::Init() must call SetEngineInformation() first");
1006 return -1;
1007 }
1008
1009 // --- Add modules to process thread (for periodic schedulation)
1010
1011 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1012
1013 // --- ACM initialization
1014
1015 if (audio_coding_->InitializeReceiver() == -1) {
1016 _engineStatisticsPtr->SetLastError(
1017 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1018 "Channel::Init() unable to initialize the ACM - 1");
1019 return -1;
1020 }
1021
1022 // --- RTP/RTCP module initialization
1023
1024 // Ensure that RTCP is enabled by default for the created channel.
1025 // Note that, the module will keep generating RTCP until it is explicitly
1026 // disabled by the user.
1027 // After StopListen (when no sockets exists), RTCP packets will no longer
1028 // be transmitted since the Transport object will then be invalid.
1029 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
1030 // RTCP is enabled by default.
1031 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1032 // --- Register all permanent callbacks
1033 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1034 (audio_coding_->RegisterVADCallback(this) == -1);
1035
1036 if (fail) {
1037 _engineStatisticsPtr->SetLastError(
1038 VE_CANNOT_INIT_CHANNEL, kTraceError,
1039 "Channel::Init() callbacks not registered");
1040 return -1;
1041 }
1042
1043 // --- Register all supported codecs to the receiving side of the
1044 // RTP/RTCP module
1045
1046 CodecInst codec;
1047 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1048
1049 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1050 // Open up the RTP/RTCP receiver for all supported codecs
1051 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1052 (rtp_receiver_->RegisterReceivePayload(
1053 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1054 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1055 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1056 "Channel::Init() unable to register %s "
1057 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1058 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1059 codec.rate);
1060 } else {
1061 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1062 "Channel::Init() %s (%d/%d/%" PRIuS
1063 "/%d) has been "
1064 "added to the RTP/RTCP receiver",
1065 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1066 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 }
1068
kwiberg55b97fe2016-01-28 05:22:45 -08001069 // Ensure that PCMU is used as default codec on the sending side
1070 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1071 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001072 }
1073
kwiberg55b97fe2016-01-28 05:22:45 -08001074 // Register default PT for outband 'telephone-event'
1075 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001076 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1077 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001078 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1079 "Channel::Init() failed to register outband "
1080 "'telephone-event' (%d/%d) correctly",
1081 codec.pltype, codec.plfreq);
1082 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001083 }
1084
kwiberg55b97fe2016-01-28 05:22:45 -08001085 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001086 if (!codec_manager_.RegisterEncoder(codec) ||
1087 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1088 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1089 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001090 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1091 "Channel::Init() failed to register CN (%d/%d) "
1092 "correctly - 1",
1093 codec.pltype, codec.plfreq);
1094 }
1095 }
kwiberg55b97fe2016-01-28 05:22:45 -08001096 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001097
kwiberg55b97fe2016-01-28 05:22:45 -08001098 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1099 LOG(LS_ERROR) << "noise_suppression()->set_level(kDefaultNsMode) failed.";
1100 return -1;
1101 }
1102 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1103 LOG(LS_ERROR) << "gain_control()->set_mode(kDefaultRxAgcMode) failed.";
1104 return -1;
1105 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001106
kwiberg55b97fe2016-01-28 05:22:45 -08001107 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001108}
1109
kwiberg55b97fe2016-01-28 05:22:45 -08001110int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1111 OutputMixer& outputMixer,
1112 voe::TransmitMixer& transmitMixer,
1113 ProcessThread& moduleProcessThread,
1114 AudioDeviceModule& audioDeviceModule,
1115 VoiceEngineObserver* voiceEngineObserver,
1116 rtc::CriticalSection* callbackCritSect) {
1117 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1118 "Channel::SetEngineInformation()");
1119 _engineStatisticsPtr = &engineStatistics;
1120 _outputMixerPtr = &outputMixer;
1121 _transmitMixerPtr = &transmitMixer,
1122 _moduleProcessThreadPtr = &moduleProcessThread;
1123 _audioDeviceModulePtr = &audioDeviceModule;
1124 _voiceEngineObserverPtr = voiceEngineObserver;
1125 _callbackCritSectPtr = callbackCritSect;
1126 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001127}
1128
kwiberg55b97fe2016-01-28 05:22:45 -08001129int32_t Channel::UpdateLocalTimeStamp() {
1130 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1131 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001132}
1133
kwibergb7f89d62016-02-17 10:04:18 -08001134void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001135 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001136 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001137}
1138
ossu29b1a8d2016-06-13 07:34:51 -07001139const rtc::scoped_refptr<AudioDecoderFactory>&
1140Channel::GetAudioDecoderFactory() const {
1141 return decoder_factory_;
1142}
1143
kwiberg55b97fe2016-01-28 05:22:45 -08001144int32_t Channel::StartPlayout() {
1145 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1146 "Channel::StartPlayout()");
1147 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001148 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001149 }
1150
1151 if (!_externalMixing) {
1152 // Add participant as candidates for mixing.
1153 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1154 _engineStatisticsPtr->SetLastError(
1155 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1156 "StartPlayout() failed to add participant to mixer");
1157 return -1;
1158 }
1159 }
1160
1161 channel_state_.SetPlaying(true);
1162 if (RegisterFilePlayingToMixer() != 0)
1163 return -1;
1164
1165 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001166}
1167
kwiberg55b97fe2016-01-28 05:22:45 -08001168int32_t Channel::StopPlayout() {
1169 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1170 "Channel::StopPlayout()");
1171 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001172 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001173 }
1174
1175 if (!_externalMixing) {
1176 // Remove participant as candidates for mixing
1177 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1178 _engineStatisticsPtr->SetLastError(
1179 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1180 "StopPlayout() failed to remove participant from mixer");
1181 return -1;
1182 }
1183 }
1184
1185 channel_state_.SetPlaying(false);
1186 _outputAudioLevel.Clear();
1187
1188 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001189}
1190
kwiberg55b97fe2016-01-28 05:22:45 -08001191int32_t Channel::StartSend() {
1192 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1193 "Channel::StartSend()");
1194 // Resume the previous sequence number which was reset by StopSend().
1195 // This needs to be done before |sending| is set to true.
1196 if (send_sequence_number_)
1197 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001198
kwiberg55b97fe2016-01-28 05:22:45 -08001199 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001200 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001201 }
1202 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001203
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001204 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001205 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1206 _engineStatisticsPtr->SetLastError(
1207 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1208 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001209 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001210 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001211 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001212 return -1;
1213 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001214
kwiberg55b97fe2016-01-28 05:22:45 -08001215 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001216}
1217
kwiberg55b97fe2016-01-28 05:22:45 -08001218int32_t Channel::StopSend() {
1219 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1220 "Channel::StopSend()");
1221 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001222 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001223 }
1224 channel_state_.SetSending(false);
1225
1226 // Store the sequence number to be able to pick up the same sequence for
1227 // the next StartSend(). This is needed for restarting device, otherwise
1228 // it might cause libSRTP to complain about packets being replayed.
1229 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1230 // CL is landed. See issue
1231 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1232 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1233
1234 // Reset sending SSRC and sequence number and triggers direct transmission
1235 // of RTCP BYE
1236 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1237 _engineStatisticsPtr->SetLastError(
1238 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1239 "StartSend() RTP/RTCP failed to stop sending");
1240 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001241 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001242
1243 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001244}
1245
kwiberg55b97fe2016-01-28 05:22:45 -08001246int32_t Channel::StartReceiving() {
1247 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1248 "Channel::StartReceiving()");
1249 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001250 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001251 }
1252 channel_state_.SetReceiving(true);
1253 _numberOfDiscardedPackets = 0;
1254 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001255}
1256
kwiberg55b97fe2016-01-28 05:22:45 -08001257int32_t Channel::StopReceiving() {
1258 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1259 "Channel::StopReceiving()");
1260 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001261 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001262 }
1263
1264 channel_state_.SetReceiving(false);
1265 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001266}
1267
kwiberg55b97fe2016-01-28 05:22:45 -08001268int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1269 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1270 "Channel::RegisterVoiceEngineObserver()");
1271 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001272
kwiberg55b97fe2016-01-28 05:22:45 -08001273 if (_voiceEngineObserverPtr) {
1274 _engineStatisticsPtr->SetLastError(
1275 VE_INVALID_OPERATION, kTraceError,
1276 "RegisterVoiceEngineObserver() observer already enabled");
1277 return -1;
1278 }
1279 _voiceEngineObserverPtr = &observer;
1280 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001281}
1282
kwiberg55b97fe2016-01-28 05:22:45 -08001283int32_t Channel::DeRegisterVoiceEngineObserver() {
1284 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1285 "Channel::DeRegisterVoiceEngineObserver()");
1286 rtc::CritScope cs(&_callbackCritSect);
1287
1288 if (!_voiceEngineObserverPtr) {
1289 _engineStatisticsPtr->SetLastError(
1290 VE_INVALID_OPERATION, kTraceWarning,
1291 "DeRegisterVoiceEngineObserver() observer already disabled");
1292 return 0;
1293 }
1294 _voiceEngineObserverPtr = NULL;
1295 return 0;
1296}
1297
1298int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001299 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001300 if (send_codec) {
1301 codec = *send_codec;
1302 return 0;
1303 }
1304 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001305}
1306
kwiberg55b97fe2016-01-28 05:22:45 -08001307int32_t Channel::GetRecCodec(CodecInst& codec) {
1308 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001309}
1310
kwiberg55b97fe2016-01-28 05:22:45 -08001311int32_t Channel::SetSendCodec(const CodecInst& codec) {
1312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1313 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001314
kwibergc8d071e2016-04-06 12:22:38 -07001315 if (!codec_manager_.RegisterEncoder(codec) ||
1316 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001317 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1318 "SetSendCodec() failed to register codec to ACM");
1319 return -1;
1320 }
1321
1322 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1323 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1324 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1325 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1326 "SetSendCodec() failed to register codec to"
1327 " RTP/RTCP module");
1328 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001329 }
kwiberg55b97fe2016-01-28 05:22:45 -08001330 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001331
kwiberg55b97fe2016-01-28 05:22:45 -08001332 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1333 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1334 "SetSendCodec() failed to set audio packet size");
1335 return -1;
1336 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001337
kwiberg55b97fe2016-01-28 05:22:45 -08001338 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001339}
1340
Ivo Creusenadf89b72015-04-29 16:03:33 +02001341void Channel::SetBitRate(int bitrate_bps) {
1342 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1343 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1344 audio_coding_->SetBitRate(bitrate_bps);
Erik Språng737336d2016-07-29 12:59:36 +02001345 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001346}
1347
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001348void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001349 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001350 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1351
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001352 // Normalizes rate to 0 - 100.
kwiberg55b97fe2016-01-28 05:22:45 -08001353 if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) !=
1354 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001355 assert(false); // This should not happen.
1356 }
1357}
1358
kwiberg55b97fe2016-01-28 05:22:45 -08001359int32_t Channel::SetVADStatus(bool enableVAD,
1360 ACMVADMode mode,
1361 bool disableDTX) {
1362 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1363 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001364 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1365 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1366 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001367 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1368 kTraceError,
1369 "SetVADStatus() failed to set VAD");
1370 return -1;
1371 }
1372 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001373}
1374
kwiberg55b97fe2016-01-28 05:22:45 -08001375int32_t Channel::GetVADStatus(bool& enabledVAD,
1376 ACMVADMode& mode,
1377 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001378 const auto* params = codec_manager_.GetStackParams();
1379 enabledVAD = params->use_cng;
1380 mode = params->vad_mode;
1381 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001382 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001383}
1384
kwiberg55b97fe2016-01-28 05:22:45 -08001385int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1386 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1387 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001388
kwiberg55b97fe2016-01-28 05:22:45 -08001389 if (channel_state_.Get().playing) {
1390 _engineStatisticsPtr->SetLastError(
1391 VE_ALREADY_PLAYING, kTraceError,
1392 "SetRecPayloadType() unable to set PT while playing");
1393 return -1;
1394 }
1395 if (channel_state_.Get().receiving) {
1396 _engineStatisticsPtr->SetLastError(
1397 VE_ALREADY_LISTENING, kTraceError,
1398 "SetRecPayloadType() unable to set PT while listening");
1399 return -1;
1400 }
1401
1402 if (codec.pltype == -1) {
1403 // De-register the selected codec (RTP/RTCP module and ACM)
1404
1405 int8_t pltype(-1);
1406 CodecInst rxCodec = codec;
1407
1408 // Get payload type for the given codec
1409 rtp_payload_registry_->ReceivePayloadType(
1410 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1411 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1412 rxCodec.pltype = pltype;
1413
1414 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1415 _engineStatisticsPtr->SetLastError(
1416 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1417 "SetRecPayloadType() RTP/RTCP-module deregistration "
1418 "failed");
1419 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001420 }
kwiberg55b97fe2016-01-28 05:22:45 -08001421 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1422 _engineStatisticsPtr->SetLastError(
1423 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1424 "SetRecPayloadType() ACM deregistration failed - 1");
1425 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001426 }
kwiberg55b97fe2016-01-28 05:22:45 -08001427 return 0;
1428 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001429
kwiberg55b97fe2016-01-28 05:22:45 -08001430 if (rtp_receiver_->RegisterReceivePayload(
1431 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1432 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1433 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001434 // TODO(kwiberg): Retrying is probably not necessary, since
1435 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001436 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001437 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001438 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1439 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1440 _engineStatisticsPtr->SetLastError(
1441 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1442 "SetRecPayloadType() RTP/RTCP-module registration failed");
1443 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001444 }
kwiberg55b97fe2016-01-28 05:22:45 -08001445 }
kwibergc8d071e2016-04-06 12:22:38 -07001446 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001447 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001448 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001449 _engineStatisticsPtr->SetLastError(
1450 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1451 "SetRecPayloadType() ACM registration failed - 1");
1452 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001453 }
kwiberg55b97fe2016-01-28 05:22:45 -08001454 }
1455 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001456}
1457
kwiberg55b97fe2016-01-28 05:22:45 -08001458int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1459 int8_t payloadType(-1);
1460 if (rtp_payload_registry_->ReceivePayloadType(
1461 codec.plname, codec.plfreq, codec.channels,
1462 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1463 _engineStatisticsPtr->SetLastError(
1464 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1465 "GetRecPayloadType() failed to retrieve RX payload type");
1466 return -1;
1467 }
1468 codec.pltype = payloadType;
1469 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001470}
1471
kwiberg55b97fe2016-01-28 05:22:45 -08001472int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1473 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1474 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001475
kwiberg55b97fe2016-01-28 05:22:45 -08001476 CodecInst codec;
1477 int32_t samplingFreqHz(-1);
1478 const size_t kMono = 1;
1479 if (frequency == kFreq32000Hz)
1480 samplingFreqHz = 32000;
1481 else if (frequency == kFreq16000Hz)
1482 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001483
kwiberg55b97fe2016-01-28 05:22:45 -08001484 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1485 _engineStatisticsPtr->SetLastError(
1486 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1487 "SetSendCNPayloadType() failed to retrieve default CN codec "
1488 "settings");
1489 return -1;
1490 }
1491
1492 // Modify the payload type (must be set to dynamic range)
1493 codec.pltype = type;
1494
kwibergc8d071e2016-04-06 12:22:38 -07001495 if (!codec_manager_.RegisterEncoder(codec) ||
1496 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001497 _engineStatisticsPtr->SetLastError(
1498 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1499 "SetSendCNPayloadType() failed to register CN to ACM");
1500 return -1;
1501 }
1502
1503 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1504 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1505 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1506 _engineStatisticsPtr->SetLastError(
1507 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1508 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1509 "module");
1510 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001511 }
kwiberg55b97fe2016-01-28 05:22:45 -08001512 }
1513 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001514}
1515
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001516int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001517 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001518 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001519
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001520 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001521 _engineStatisticsPtr->SetLastError(
1522 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001523 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001524 return -1;
1525 }
1526 return 0;
1527}
1528
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001529int Channel::SetOpusDtx(bool enable_dtx) {
1530 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1531 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001532 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001533 : audio_coding_->DisableOpusDtx();
1534 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001535 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1536 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001537 return -1;
1538 }
1539 return 0;
1540}
1541
ivoc85228d62016-07-27 04:53:47 -07001542int Channel::GetOpusDtx(bool* enabled) {
1543 int success = -1;
1544 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1545 if (encoder) {
1546 *enabled = encoder->GetDtx();
1547 success = 0;
1548 }
1549 });
1550 return success;
1551}
1552
mflodman3d7db262016-04-29 00:57:13 -07001553int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001554 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001555 "Channel::RegisterExternalTransport()");
1556
kwiberg55b97fe2016-01-28 05:22:45 -08001557 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001558 if (_externalTransport) {
1559 _engineStatisticsPtr->SetLastError(
1560 VE_INVALID_OPERATION, kTraceError,
1561 "RegisterExternalTransport() external transport already enabled");
1562 return -1;
1563 }
1564 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001565 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001566 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001567}
1568
kwiberg55b97fe2016-01-28 05:22:45 -08001569int32_t Channel::DeRegisterExternalTransport() {
1570 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1571 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001572
kwiberg55b97fe2016-01-28 05:22:45 -08001573 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001574 if (_transportPtr) {
1575 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1576 "DeRegisterExternalTransport() all transport is disabled");
1577 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001578 _engineStatisticsPtr->SetLastError(
1579 VE_INVALID_OPERATION, kTraceWarning,
1580 "DeRegisterExternalTransport() external transport already "
1581 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001582 }
1583 _externalTransport = false;
1584 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001585 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001586}
1587
mflodman3d7db262016-04-29 00:57:13 -07001588int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001589 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001590 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001591 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001592 "Channel::ReceivedRTPPacket()");
1593
1594 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001595 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001596
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001597 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001598 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1599 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1600 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001601 return -1;
1602 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001603 header.payload_type_frequency =
1604 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001605 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001606 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001607 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001608 rtp_receive_statistics_->IncomingPacket(
1609 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001611
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001612 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001613}
1614
1615bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001616 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001617 const RTPHeader& header,
1618 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001619 if (rtp_payload_registry_->IsRtx(header)) {
1620 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001621 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001622 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001623 assert(packet_length >= header.headerLength);
1624 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001625 PayloadUnion payload_specific;
1626 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001627 &payload_specific)) {
1628 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001629 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001630 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1631 payload_specific, in_order);
1632}
1633
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001634bool Channel::HandleRtxPacket(const uint8_t* packet,
1635 size_t packet_length,
1636 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001637 if (!rtp_payload_registry_->IsRtx(header))
1638 return false;
1639
1640 // Remove the RTX header and parse the original RTP header.
1641 if (packet_length < header.headerLength)
1642 return false;
1643 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1644 return false;
1645 if (restored_packet_in_use_) {
1646 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1647 "Multiple RTX headers detected, dropping packet");
1648 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001649 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001650 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001651 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1652 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001653 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1654 "Incoming RTX packet: invalid RTP header");
1655 return false;
1656 }
1657 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001658 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001659 restored_packet_in_use_ = false;
1660 return ret;
1661}
1662
1663bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1664 StreamStatistician* statistician =
1665 rtp_receive_statistics_->GetStatistician(header.ssrc);
1666 if (!statistician)
1667 return false;
1668 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001669}
1670
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001671bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1672 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001673 // Retransmissions are handled separately if RTX is enabled.
1674 if (rtp_payload_registry_->RtxEnabled())
1675 return false;
1676 StreamStatistician* statistician =
1677 rtp_receive_statistics_->GetStatistician(header.ssrc);
1678 if (!statistician)
1679 return false;
1680 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001681 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001682 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001683 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001684}
1685
mflodman3d7db262016-04-29 00:57:13 -07001686int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001687 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001688 "Channel::ReceivedRTCPPacket()");
1689 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001690 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001691
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001692 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001693 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001694 _engineStatisticsPtr->SetLastError(
1695 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1696 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1697 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001698
Minyue2013aec2015-05-13 14:14:42 +02001699 int64_t rtt = GetRTT(true);
1700 if (rtt == 0) {
1701 // Waiting for valid RTT.
1702 return 0;
1703 }
Erik Språng737336d2016-07-29 12:59:36 +02001704
1705 int64_t nack_window_ms = rtt;
1706 if (nack_window_ms < kMinRetransmissionWindowMs) {
1707 nack_window_ms = kMinRetransmissionWindowMs;
1708 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1709 nack_window_ms = kMaxRetransmissionWindowMs;
1710 }
1711 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1712
Minyue2013aec2015-05-13 14:14:42 +02001713 uint32_t ntp_secs = 0;
1714 uint32_t ntp_frac = 0;
1715 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001716 if (0 !=
1717 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1718 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001719 // Waiting for RTCP.
1720 return 0;
1721 }
1722
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001723 {
tommi31fc21f2016-01-21 10:37:37 -08001724 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001725 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001726 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001727 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001728}
1729
niklase@google.com470e71d2011-07-07 08:21:25 +00001730int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001731 bool loop,
1732 FileFormats format,
1733 int startPosition,
1734 float volumeScaling,
1735 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001736 const CodecInst* codecInst) {
1737 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1738 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1739 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1740 "stopPosition=%d)",
1741 fileName, loop, format, volumeScaling, startPosition,
1742 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001743
kwiberg55b97fe2016-01-28 05:22:45 -08001744 if (channel_state_.Get().output_file_playing) {
1745 _engineStatisticsPtr->SetLastError(
1746 VE_ALREADY_PLAYING, kTraceError,
1747 "StartPlayingFileLocally() is already playing");
1748 return -1;
1749 }
1750
1751 {
1752 rtc::CritScope cs(&_fileCritSect);
1753
kwiberg5a25d952016-08-17 07:31:12 -07001754 if (output_file_player_) {
1755 output_file_player_->RegisterModuleFileCallback(NULL);
1756 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001757 }
1758
kwiberg5a25d952016-08-17 07:31:12 -07001759 output_file_player_ = FilePlayer::NewFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001760 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001761
kwiberg5a25d952016-08-17 07:31:12 -07001762 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001763 _engineStatisticsPtr->SetLastError(
1764 VE_INVALID_ARGUMENT, kTraceError,
1765 "StartPlayingFileLocally() filePlayer format is not correct");
1766 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001767 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001768
kwiberg55b97fe2016-01-28 05:22:45 -08001769 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001770
kwiberg5a25d952016-08-17 07:31:12 -07001771 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001772 fileName, loop, startPosition, volumeScaling, notificationTime,
1773 stopPosition, (const CodecInst*)codecInst) != 0) {
1774 _engineStatisticsPtr->SetLastError(
1775 VE_BAD_FILE, kTraceError,
1776 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001777 output_file_player_->StopPlayingFile();
1778 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001779 return -1;
1780 }
kwiberg5a25d952016-08-17 07:31:12 -07001781 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001782 channel_state_.SetOutputFilePlaying(true);
1783 }
1784
1785 if (RegisterFilePlayingToMixer() != 0)
1786 return -1;
1787
1788 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001789}
1790
1791int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001792 FileFormats format,
1793 int startPosition,
1794 float volumeScaling,
1795 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001796 const CodecInst* codecInst) {
1797 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1798 "Channel::StartPlayingFileLocally(format=%d,"
1799 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1800 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001801
kwiberg55b97fe2016-01-28 05:22:45 -08001802 if (stream == NULL) {
1803 _engineStatisticsPtr->SetLastError(
1804 VE_BAD_FILE, kTraceError,
1805 "StartPlayingFileLocally() NULL as input stream");
1806 return -1;
1807 }
1808
1809 if (channel_state_.Get().output_file_playing) {
1810 _engineStatisticsPtr->SetLastError(
1811 VE_ALREADY_PLAYING, kTraceError,
1812 "StartPlayingFileLocally() is already playing");
1813 return -1;
1814 }
1815
1816 {
1817 rtc::CritScope cs(&_fileCritSect);
1818
1819 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001820 if (output_file_player_) {
1821 output_file_player_->RegisterModuleFileCallback(NULL);
1822 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001823 }
1824
kwiberg55b97fe2016-01-28 05:22:45 -08001825 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07001826 output_file_player_ = FilePlayer::NewFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001827 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001828
kwiberg5a25d952016-08-17 07:31:12 -07001829 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001830 _engineStatisticsPtr->SetLastError(
1831 VE_INVALID_ARGUMENT, kTraceError,
1832 "StartPlayingFileLocally() filePlayer format isnot correct");
1833 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001834 }
1835
kwiberg55b97fe2016-01-28 05:22:45 -08001836 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001837
kwiberg4ec01d92016-08-22 08:43:54 -07001838 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001839 volumeScaling, notificationTime,
1840 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001841 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1842 "StartPlayingFile() failed to "
1843 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001844 output_file_player_->StopPlayingFile();
1845 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001846 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001847 }
kwiberg5a25d952016-08-17 07:31:12 -07001848 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001849 channel_state_.SetOutputFilePlaying(true);
1850 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001851
kwiberg55b97fe2016-01-28 05:22:45 -08001852 if (RegisterFilePlayingToMixer() != 0)
1853 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001854
kwiberg55b97fe2016-01-28 05:22:45 -08001855 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001856}
1857
kwiberg55b97fe2016-01-28 05:22:45 -08001858int Channel::StopPlayingFileLocally() {
1859 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1860 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001861
kwiberg55b97fe2016-01-28 05:22:45 -08001862 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001863 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001864 }
1865
1866 {
1867 rtc::CritScope cs(&_fileCritSect);
1868
kwiberg5a25d952016-08-17 07:31:12 -07001869 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001870 _engineStatisticsPtr->SetLastError(
1871 VE_STOP_RECORDING_FAILED, kTraceError,
1872 "StopPlayingFile() could not stop playing");
1873 return -1;
1874 }
kwiberg5a25d952016-08-17 07:31:12 -07001875 output_file_player_->RegisterModuleFileCallback(NULL);
1876 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001877 channel_state_.SetOutputFilePlaying(false);
1878 }
1879 // _fileCritSect cannot be taken while calling
1880 // SetAnonymousMixibilityStatus. Refer to comments in
1881 // StartPlayingFileLocally(const char* ...) for more details.
1882 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1883 _engineStatisticsPtr->SetLastError(
1884 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1885 "StopPlayingFile() failed to stop participant from playing as"
1886 "file in the mixer");
1887 return -1;
1888 }
1889
1890 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001891}
1892
kwiberg55b97fe2016-01-28 05:22:45 -08001893int Channel::IsPlayingFileLocally() const {
1894 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001895}
1896
kwiberg55b97fe2016-01-28 05:22:45 -08001897int Channel::RegisterFilePlayingToMixer() {
1898 // Return success for not registering for file playing to mixer if:
1899 // 1. playing file before playout is started on that channel.
1900 // 2. starting playout without file playing on that channel.
1901 if (!channel_state_.Get().playing ||
1902 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001903 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001904 }
1905
1906 // |_fileCritSect| cannot be taken while calling
1907 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1908 // frames can be pulled by the mixer. Since the frames are generated from
1909 // the file, _fileCritSect will be taken. This would result in a deadlock.
1910 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1911 channel_state_.SetOutputFilePlaying(false);
1912 rtc::CritScope cs(&_fileCritSect);
1913 _engineStatisticsPtr->SetLastError(
1914 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1915 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001916 output_file_player_->StopPlayingFile();
1917 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001918 return -1;
1919 }
1920
1921 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001922}
1923
niklase@google.com470e71d2011-07-07 08:21:25 +00001924int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001925 bool loop,
1926 FileFormats format,
1927 int startPosition,
1928 float volumeScaling,
1929 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001930 const CodecInst* codecInst) {
1931 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1932 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1933 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1934 "stopPosition=%d)",
1935 fileName, loop, format, volumeScaling, startPosition,
1936 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001937
kwiberg55b97fe2016-01-28 05:22:45 -08001938 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001939
kwiberg55b97fe2016-01-28 05:22:45 -08001940 if (channel_state_.Get().input_file_playing) {
1941 _engineStatisticsPtr->SetLastError(
1942 VE_ALREADY_PLAYING, kTraceWarning,
1943 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001944 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001945 }
1946
1947 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001948 if (input_file_player_) {
1949 input_file_player_->RegisterModuleFileCallback(NULL);
1950 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001951 }
1952
1953 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07001954 input_file_player_ = FilePlayer::NewFilePlayer(_inputFilePlayerId,
1955 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001956
kwiberg5a25d952016-08-17 07:31:12 -07001957 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001958 _engineStatisticsPtr->SetLastError(
1959 VE_INVALID_ARGUMENT, kTraceError,
1960 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1961 return -1;
1962 }
1963
1964 const uint32_t notificationTime(0);
1965
kwiberg5a25d952016-08-17 07:31:12 -07001966 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001967 fileName, loop, startPosition, volumeScaling, notificationTime,
1968 stopPosition, (const CodecInst*)codecInst) != 0) {
1969 _engineStatisticsPtr->SetLastError(
1970 VE_BAD_FILE, kTraceError,
1971 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001972 input_file_player_->StopPlayingFile();
1973 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001974 return -1;
1975 }
kwiberg5a25d952016-08-17 07:31:12 -07001976 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001977 channel_state_.SetInputFilePlaying(true);
1978
1979 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001980}
1981
1982int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001983 FileFormats format,
1984 int startPosition,
1985 float volumeScaling,
1986 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001987 const CodecInst* codecInst) {
1988 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1989 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1990 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1991 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001992
kwiberg55b97fe2016-01-28 05:22:45 -08001993 if (stream == NULL) {
1994 _engineStatisticsPtr->SetLastError(
1995 VE_BAD_FILE, kTraceError,
1996 "StartPlayingFileAsMicrophone NULL as input stream");
1997 return -1;
1998 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001999
kwiberg55b97fe2016-01-28 05:22:45 -08002000 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002001
kwiberg55b97fe2016-01-28 05:22:45 -08002002 if (channel_state_.Get().input_file_playing) {
2003 _engineStatisticsPtr->SetLastError(
2004 VE_ALREADY_PLAYING, kTraceWarning,
2005 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002006 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002007 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002008
kwiberg55b97fe2016-01-28 05:22:45 -08002009 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002010 if (input_file_player_) {
2011 input_file_player_->RegisterModuleFileCallback(NULL);
2012 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002013 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002014
kwiberg55b97fe2016-01-28 05:22:45 -08002015 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07002016 input_file_player_ = FilePlayer::NewFilePlayer(_inputFilePlayerId,
2017 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002018
kwiberg5a25d952016-08-17 07:31:12 -07002019 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002020 _engineStatisticsPtr->SetLastError(
2021 VE_INVALID_ARGUMENT, kTraceError,
2022 "StartPlayingInputFile() filePlayer format isnot correct");
2023 return -1;
2024 }
2025
2026 const uint32_t notificationTime(0);
2027
kwiberg4ec01d92016-08-22 08:43:54 -07002028 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2029 notificationTime, stopPosition,
2030 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002031 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2032 "StartPlayingFile() failed to start "
2033 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002034 input_file_player_->StopPlayingFile();
2035 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002036 return -1;
2037 }
2038
kwiberg5a25d952016-08-17 07:31:12 -07002039 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002040 channel_state_.SetInputFilePlaying(true);
2041
2042 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002043}
2044
kwiberg55b97fe2016-01-28 05:22:45 -08002045int Channel::StopPlayingFileAsMicrophone() {
2046 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2047 "Channel::StopPlayingFileAsMicrophone()");
2048
2049 rtc::CritScope cs(&_fileCritSect);
2050
2051 if (!channel_state_.Get().input_file_playing) {
2052 return 0;
2053 }
2054
kwiberg5a25d952016-08-17 07:31:12 -07002055 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002056 _engineStatisticsPtr->SetLastError(
2057 VE_STOP_RECORDING_FAILED, kTraceError,
2058 "StopPlayingFile() could not stop playing");
2059 return -1;
2060 }
kwiberg5a25d952016-08-17 07:31:12 -07002061 input_file_player_->RegisterModuleFileCallback(NULL);
2062 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002063 channel_state_.SetInputFilePlaying(false);
2064
2065 return 0;
2066}
2067
2068int Channel::IsPlayingFileAsMicrophone() const {
2069 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002070}
2071
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002072int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002073 const CodecInst* codecInst) {
2074 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2075 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002076
kwiberg55b97fe2016-01-28 05:22:45 -08002077 if (_outputFileRecording) {
2078 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2079 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002080 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002081 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002082
kwiberg55b97fe2016-01-28 05:22:45 -08002083 FileFormats format;
2084 const uint32_t notificationTime(0); // Not supported in VoE
2085 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002086
kwiberg55b97fe2016-01-28 05:22:45 -08002087 if ((codecInst != NULL) &&
2088 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2089 _engineStatisticsPtr->SetLastError(
2090 VE_BAD_ARGUMENT, kTraceError,
2091 "StartRecordingPlayout() invalid compression");
2092 return (-1);
2093 }
2094 if (codecInst == NULL) {
2095 format = kFileFormatPcm16kHzFile;
2096 codecInst = &dummyCodec;
2097 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2098 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2099 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2100 format = kFileFormatWavFile;
2101 } else {
2102 format = kFileFormatCompressedFile;
2103 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002104
kwiberg55b97fe2016-01-28 05:22:45 -08002105 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002106
kwiberg55b97fe2016-01-28 05:22:45 -08002107 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002108 if (output_file_recorder_) {
2109 output_file_recorder_->RegisterModuleFileCallback(NULL);
2110 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002111 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002112
kwiberg5a25d952016-08-17 07:31:12 -07002113 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002114 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002115 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002116 _engineStatisticsPtr->SetLastError(
2117 VE_INVALID_ARGUMENT, kTraceError,
2118 "StartRecordingPlayout() fileRecorder format isnot correct");
2119 return -1;
2120 }
2121
kwiberg5a25d952016-08-17 07:31:12 -07002122 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002123 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2124 _engineStatisticsPtr->SetLastError(
2125 VE_BAD_FILE, kTraceError,
2126 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002127 output_file_recorder_->StopRecording();
2128 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002129 return -1;
2130 }
kwiberg5a25d952016-08-17 07:31:12 -07002131 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002132 _outputFileRecording = true;
2133
2134 return 0;
2135}
2136
2137int Channel::StartRecordingPlayout(OutStream* stream,
2138 const CodecInst* codecInst) {
2139 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2140 "Channel::StartRecordingPlayout()");
2141
2142 if (_outputFileRecording) {
2143 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2144 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002145 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002146 }
2147
2148 FileFormats format;
2149 const uint32_t notificationTime(0); // Not supported in VoE
2150 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2151
2152 if (codecInst != NULL && codecInst->channels != 1) {
2153 _engineStatisticsPtr->SetLastError(
2154 VE_BAD_ARGUMENT, kTraceError,
2155 "StartRecordingPlayout() invalid compression");
2156 return (-1);
2157 }
2158 if (codecInst == NULL) {
2159 format = kFileFormatPcm16kHzFile;
2160 codecInst = &dummyCodec;
2161 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2162 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2163 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2164 format = kFileFormatWavFile;
2165 } else {
2166 format = kFileFormatCompressedFile;
2167 }
2168
2169 rtc::CritScope cs(&_fileCritSect);
2170
2171 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002172 if (output_file_recorder_) {
2173 output_file_recorder_->RegisterModuleFileCallback(NULL);
2174 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002175 }
2176
kwiberg5a25d952016-08-17 07:31:12 -07002177 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002178 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002179 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002180 _engineStatisticsPtr->SetLastError(
2181 VE_INVALID_ARGUMENT, kTraceError,
2182 "StartRecordingPlayout() fileRecorder format isnot correct");
2183 return -1;
2184 }
2185
kwiberg4ec01d92016-08-22 08:43:54 -07002186 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002187 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002188 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2189 "StartRecordingPlayout() failed to "
2190 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002191 output_file_recorder_->StopRecording();
2192 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002193 return -1;
2194 }
2195
kwiberg5a25d952016-08-17 07:31:12 -07002196 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002197 _outputFileRecording = true;
2198
2199 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002200}
2201
kwiberg55b97fe2016-01-28 05:22:45 -08002202int Channel::StopRecordingPlayout() {
2203 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2204 "Channel::StopRecordingPlayout()");
2205
2206 if (!_outputFileRecording) {
2207 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2208 "StopRecordingPlayout() isnot recording");
2209 return -1;
2210 }
2211
2212 rtc::CritScope cs(&_fileCritSect);
2213
kwiberg5a25d952016-08-17 07:31:12 -07002214 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002215 _engineStatisticsPtr->SetLastError(
2216 VE_STOP_RECORDING_FAILED, kTraceError,
2217 "StopRecording() could not stop recording");
2218 return (-1);
2219 }
kwiberg5a25d952016-08-17 07:31:12 -07002220 output_file_recorder_->RegisterModuleFileCallback(NULL);
2221 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002222 _outputFileRecording = false;
2223
2224 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002225}
2226
kwiberg55b97fe2016-01-28 05:22:45 -08002227void Channel::SetMixWithMicStatus(bool mix) {
2228 rtc::CritScope cs(&_fileCritSect);
2229 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002230}
2231
kwiberg55b97fe2016-01-28 05:22:45 -08002232int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2233 int8_t currentLevel = _outputAudioLevel.Level();
2234 level = static_cast<int32_t>(currentLevel);
2235 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002236}
2237
kwiberg55b97fe2016-01-28 05:22:45 -08002238int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2239 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2240 level = static_cast<int32_t>(currentLevel);
2241 return 0;
2242}
2243
solenberg1c2af8e2016-03-24 10:36:00 -07002244int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002245 rtc::CritScope cs(&volume_settings_critsect_);
2246 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002247 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002248 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002249 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002250}
2251
solenberg1c2af8e2016-03-24 10:36:00 -07002252bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002253 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002254 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002255}
2256
kwiberg55b97fe2016-01-28 05:22:45 -08002257int Channel::SetOutputVolumePan(float left, float right) {
2258 rtc::CritScope cs(&volume_settings_critsect_);
2259 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002260 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002261 _panLeft = left;
2262 _panRight = right;
2263 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002264}
2265
kwiberg55b97fe2016-01-28 05:22:45 -08002266int Channel::GetOutputVolumePan(float& left, float& right) const {
2267 rtc::CritScope cs(&volume_settings_critsect_);
2268 left = _panLeft;
2269 right = _panRight;
2270 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002271}
2272
kwiberg55b97fe2016-01-28 05:22:45 -08002273int Channel::SetChannelOutputVolumeScaling(float scaling) {
2274 rtc::CritScope cs(&volume_settings_critsect_);
2275 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002276 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002277 _outputGain = scaling;
2278 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002279}
2280
kwiberg55b97fe2016-01-28 05:22:45 -08002281int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2282 rtc::CritScope cs(&volume_settings_critsect_);
2283 scaling = _outputGain;
2284 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002285}
2286
solenberg8842c3e2016-03-11 03:06:41 -08002287int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002288 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002289 "Channel::SendTelephoneEventOutband(...)");
2290 RTC_DCHECK_LE(0, event);
2291 RTC_DCHECK_GE(255, event);
2292 RTC_DCHECK_LE(0, duration_ms);
2293 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002294 if (!Sending()) {
2295 return -1;
2296 }
solenberg8842c3e2016-03-11 03:06:41 -08002297 if (_rtpRtcpModule->SendTelephoneEventOutband(
2298 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002299 _engineStatisticsPtr->SetLastError(
2300 VE_SEND_DTMF_FAILED, kTraceWarning,
2301 "SendTelephoneEventOutband() failed to send event");
2302 return -1;
2303 }
2304 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002305}
2306
solenberg31642aa2016-03-14 08:00:37 -07002307int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002308 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002309 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002310 RTC_DCHECK_LE(0, payload_type);
2311 RTC_DCHECK_GE(127, payload_type);
2312 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002313 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002314 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002315 memcpy(codec.plname, "telephone-event", 16);
2316 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2317 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2318 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2319 _engineStatisticsPtr->SetLastError(
2320 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2321 "SetSendTelephoneEventPayloadType() failed to register send"
2322 "payload type");
2323 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002324 }
kwiberg55b97fe2016-01-28 05:22:45 -08002325 }
kwiberg55b97fe2016-01-28 05:22:45 -08002326 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002327}
2328
kwiberg55b97fe2016-01-28 05:22:45 -08002329int Channel::UpdateRxVadDetection(AudioFrame& audioFrame) {
2330 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2331 "Channel::UpdateRxVadDetection()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002332
kwiberg55b97fe2016-01-28 05:22:45 -08002333 int vadDecision = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002334
kwiberg55b97fe2016-01-28 05:22:45 -08002335 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive) ? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002336
kwiberg55b97fe2016-01-28 05:22:45 -08002337 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr) {
2338 OnRxVadDetected(vadDecision);
2339 _oldVadDecision = vadDecision;
2340 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002341
kwiberg55b97fe2016-01-28 05:22:45 -08002342 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2343 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2344 vadDecision);
2345 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002346}
2347
kwiberg55b97fe2016-01-28 05:22:45 -08002348int Channel::RegisterRxVadObserver(VoERxVadCallback& observer) {
2349 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2350 "Channel::RegisterRxVadObserver()");
2351 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002352
kwiberg55b97fe2016-01-28 05:22:45 -08002353 if (_rxVadObserverPtr) {
2354 _engineStatisticsPtr->SetLastError(
2355 VE_INVALID_OPERATION, kTraceError,
2356 "RegisterRxVadObserver() observer already enabled");
2357 return -1;
2358 }
2359 _rxVadObserverPtr = &observer;
2360 _RxVadDetection = true;
2361 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002362}
2363
kwiberg55b97fe2016-01-28 05:22:45 -08002364int Channel::DeRegisterRxVadObserver() {
2365 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2366 "Channel::DeRegisterRxVadObserver()");
2367 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002368
kwiberg55b97fe2016-01-28 05:22:45 -08002369 if (!_rxVadObserverPtr) {
2370 _engineStatisticsPtr->SetLastError(
2371 VE_INVALID_OPERATION, kTraceWarning,
2372 "DeRegisterRxVadObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00002373 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002374 }
2375 _rxVadObserverPtr = NULL;
2376 _RxVadDetection = false;
2377 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002378}
2379
kwiberg55b97fe2016-01-28 05:22:45 -08002380int Channel::VoiceActivityIndicator(int& activity) {
2381 activity = _sendFrameType;
2382 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002383}
2384
2385#ifdef WEBRTC_VOICE_ENGINE_AGC
2386
kwiberg55b97fe2016-01-28 05:22:45 -08002387int Channel::SetRxAgcStatus(bool enable, AgcModes mode) {
2388 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2389 "Channel::SetRxAgcStatus(enable=%d, mode=%d)", (int)enable,
2390 (int)mode);
niklase@google.com470e71d2011-07-07 08:21:25 +00002391
kwiberg55b97fe2016-01-28 05:22:45 -08002392 GainControl::Mode agcMode = kDefaultRxAgcMode;
2393 switch (mode) {
2394 case kAgcDefault:
2395 break;
2396 case kAgcUnchanged:
2397 agcMode = rx_audioproc_->gain_control()->mode();
2398 break;
2399 case kAgcFixedDigital:
2400 agcMode = GainControl::kFixedDigital;
2401 break;
2402 case kAgcAdaptiveDigital:
2403 agcMode = GainControl::kAdaptiveDigital;
2404 break;
2405 default:
2406 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
2407 "SetRxAgcStatus() invalid Agc mode");
2408 return -1;
2409 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002410
kwiberg55b97fe2016-01-28 05:22:45 -08002411 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0) {
2412 _engineStatisticsPtr->SetLastError(
2413 VE_APM_ERROR, kTraceError, "SetRxAgcStatus() failed to set Agc mode");
2414 return -1;
2415 }
2416 if (rx_audioproc_->gain_control()->Enable(enable) != 0) {
2417 _engineStatisticsPtr->SetLastError(
2418 VE_APM_ERROR, kTraceError, "SetRxAgcStatus() failed to set Agc state");
2419 return -1;
2420 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002421
kwiberg55b97fe2016-01-28 05:22:45 -08002422 _rxAgcIsEnabled = enable;
2423 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002424
kwiberg55b97fe2016-01-28 05:22:45 -08002425 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002426}
2427
kwiberg55b97fe2016-01-28 05:22:45 -08002428int Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode) {
2429 bool enable = rx_audioproc_->gain_control()->is_enabled();
2430 GainControl::Mode agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002431
kwiberg55b97fe2016-01-28 05:22:45 -08002432 enabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002433
kwiberg55b97fe2016-01-28 05:22:45 -08002434 switch (agcMode) {
2435 case GainControl::kFixedDigital:
2436 mode = kAgcFixedDigital;
2437 break;
2438 case GainControl::kAdaptiveDigital:
2439 mode = kAgcAdaptiveDigital;
2440 break;
2441 default:
2442 _engineStatisticsPtr->SetLastError(VE_APM_ERROR, kTraceError,
2443 "GetRxAgcStatus() invalid Agc mode");
2444 return -1;
2445 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002446
kwiberg55b97fe2016-01-28 05:22:45 -08002447 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002448}
2449
kwiberg55b97fe2016-01-28 05:22:45 -08002450int Channel::SetRxAgcConfig(AgcConfig config) {
2451 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2452 "Channel::SetRxAgcConfig()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002453
kwiberg55b97fe2016-01-28 05:22:45 -08002454 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
2455 config.targetLeveldBOv) != 0) {
2456 _engineStatisticsPtr->SetLastError(
2457 VE_APM_ERROR, kTraceError,
2458 "SetRxAgcConfig() failed to set target peak |level|"
2459 "(or envelope) of the Agc");
2460 return -1;
2461 }
2462 if (rx_audioproc_->gain_control()->set_compression_gain_db(
2463 config.digitalCompressionGaindB) != 0) {
2464 _engineStatisticsPtr->SetLastError(
2465 VE_APM_ERROR, kTraceError,
2466 "SetRxAgcConfig() failed to set the range in |gain| the"
2467 " digital compression stage may apply");
2468 return -1;
2469 }
2470 if (rx_audioproc_->gain_control()->enable_limiter(config.limiterEnable) !=
2471 0) {
2472 _engineStatisticsPtr->SetLastError(
2473 VE_APM_ERROR, kTraceError,
2474 "SetRxAgcConfig() failed to set hard limiter to the signal");
2475 return -1;
2476 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002477
kwiberg55b97fe2016-01-28 05:22:45 -08002478 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002479}
2480
kwiberg55b97fe2016-01-28 05:22:45 -08002481int Channel::GetRxAgcConfig(AgcConfig& config) {
2482 config.targetLeveldBOv = rx_audioproc_->gain_control()->target_level_dbfs();
2483 config.digitalCompressionGaindB =
2484 rx_audioproc_->gain_control()->compression_gain_db();
2485 config.limiterEnable = rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002486
kwiberg55b97fe2016-01-28 05:22:45 -08002487 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002488}
2489
kwiberg55b97fe2016-01-28 05:22:45 -08002490#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
niklase@google.com470e71d2011-07-07 08:21:25 +00002491
2492#ifdef WEBRTC_VOICE_ENGINE_NR
2493
kwiberg55b97fe2016-01-28 05:22:45 -08002494int Channel::SetRxNsStatus(bool enable, NsModes mode) {
2495 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2496 "Channel::SetRxNsStatus(enable=%d, mode=%d)", (int)enable,
2497 (int)mode);
niklase@google.com470e71d2011-07-07 08:21:25 +00002498
kwiberg55b97fe2016-01-28 05:22:45 -08002499 NoiseSuppression::Level nsLevel = kDefaultNsMode;
2500 switch (mode) {
2501 case kNsDefault:
2502 break;
2503 case kNsUnchanged:
2504 nsLevel = rx_audioproc_->noise_suppression()->level();
2505 break;
2506 case kNsConference:
2507 nsLevel = NoiseSuppression::kHigh;
2508 break;
2509 case kNsLowSuppression:
2510 nsLevel = NoiseSuppression::kLow;
2511 break;
2512 case kNsModerateSuppression:
2513 nsLevel = NoiseSuppression::kModerate;
2514 break;
2515 case kNsHighSuppression:
2516 nsLevel = NoiseSuppression::kHigh;
2517 break;
2518 case kNsVeryHighSuppression:
2519 nsLevel = NoiseSuppression::kVeryHigh;
2520 break;
2521 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002522
kwiberg55b97fe2016-01-28 05:22:45 -08002523 if (rx_audioproc_->noise_suppression()->set_level(nsLevel) != 0) {
2524 _engineStatisticsPtr->SetLastError(
2525 VE_APM_ERROR, kTraceError, "SetRxNsStatus() failed to set NS level");
2526 return -1;
2527 }
2528 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0) {
2529 _engineStatisticsPtr->SetLastError(
2530 VE_APM_ERROR, kTraceError, "SetRxNsStatus() failed to set NS state");
2531 return -1;
2532 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002533
kwiberg55b97fe2016-01-28 05:22:45 -08002534 _rxNsIsEnabled = enable;
2535 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002536
kwiberg55b97fe2016-01-28 05:22:45 -08002537 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002538}
2539
kwiberg55b97fe2016-01-28 05:22:45 -08002540int Channel::GetRxNsStatus(bool& enabled, NsModes& mode) {
2541 bool enable = rx_audioproc_->noise_suppression()->is_enabled();
2542 NoiseSuppression::Level ncLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002543
kwiberg55b97fe2016-01-28 05:22:45 -08002544 enabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002545
kwiberg55b97fe2016-01-28 05:22:45 -08002546 switch (ncLevel) {
2547 case NoiseSuppression::kLow:
2548 mode = kNsLowSuppression;
2549 break;
2550 case NoiseSuppression::kModerate:
2551 mode = kNsModerateSuppression;
2552 break;
2553 case NoiseSuppression::kHigh:
2554 mode = kNsHighSuppression;
2555 break;
2556 case NoiseSuppression::kVeryHigh:
2557 mode = kNsVeryHighSuppression;
2558 break;
2559 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002560
kwiberg55b97fe2016-01-28 05:22:45 -08002561 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002562}
2563
kwiberg55b97fe2016-01-28 05:22:45 -08002564#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
niklase@google.com470e71d2011-07-07 08:21:25 +00002565
kwiberg55b97fe2016-01-28 05:22:45 -08002566int Channel::SetLocalSSRC(unsigned int ssrc) {
2567 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2568 "Channel::SetLocalSSRC()");
2569 if (channel_state_.Get().sending) {
2570 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2571 "SetLocalSSRC() already sending");
2572 return -1;
2573 }
2574 _rtpRtcpModule->SetSSRC(ssrc);
2575 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002576}
2577
kwiberg55b97fe2016-01-28 05:22:45 -08002578int Channel::GetLocalSSRC(unsigned int& ssrc) {
2579 ssrc = _rtpRtcpModule->SSRC();
2580 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002581}
2582
kwiberg55b97fe2016-01-28 05:22:45 -08002583int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2584 ssrc = rtp_receiver_->SSRC();
2585 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002586}
2587
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002588int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002589 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002590 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002591}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002592
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002593int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2594 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002595 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2596 if (enable &&
2597 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2598 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002599 return -1;
2600 }
2601 return 0;
2602}
2603
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002604int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2605 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2606}
2607
2608int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2609 rtp_header_parser_->DeregisterRtpHeaderExtension(
2610 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002611 if (enable &&
2612 !rtp_header_parser_->RegisterRtpHeaderExtension(
2613 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002614 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002615 }
2616 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002617}
2618
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002619void Channel::EnableSendTransportSequenceNumber(int id) {
2620 int ret =
2621 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2622 RTC_DCHECK_EQ(0, ret);
2623}
2624
stefan3313ec92016-01-21 06:32:43 -08002625void Channel::EnableReceiveTransportSequenceNumber(int id) {
2626 rtp_header_parser_->DeregisterRtpHeaderExtension(
2627 kRtpExtensionTransportSequenceNumber);
2628 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2629 kRtpExtensionTransportSequenceNumber, id);
2630 RTC_DCHECK(ret);
2631}
2632
stefanbba9dec2016-02-01 04:39:55 -08002633void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002634 RtpPacketSender* rtp_packet_sender,
2635 TransportFeedbackObserver* transport_feedback_observer,
2636 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002637 RTC_DCHECK(rtp_packet_sender);
2638 RTC_DCHECK(transport_feedback_observer);
2639 RTC_DCHECK(packet_router && !packet_router_);
2640 feedback_observer_proxy_->SetTransportFeedbackObserver(
2641 transport_feedback_observer);
2642 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2643 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2644 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002645 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002646 packet_router_ = packet_router;
2647}
2648
stefanbba9dec2016-02-01 04:39:55 -08002649void Channel::RegisterReceiverCongestionControlObjects(
2650 PacketRouter* packet_router) {
2651 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002652 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002653 packet_router_ = packet_router;
2654}
2655
2656void Channel::ResetCongestionControlObjects() {
2657 RTC_DCHECK(packet_router_);
2658 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2659 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2660 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002661 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002662 packet_router_ = nullptr;
2663 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2664}
2665
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002666void Channel::SetRTCPStatus(bool enable) {
2667 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2668 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002669 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002670}
2671
kwiberg55b97fe2016-01-28 05:22:45 -08002672int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002673 RtcpMode method = _rtpRtcpModule->RTCP();
2674 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002675 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002676}
2677
kwiberg55b97fe2016-01-28 05:22:45 -08002678int Channel::SetRTCP_CNAME(const char cName[256]) {
2679 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2680 "Channel::SetRTCP_CNAME()");
2681 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2682 _engineStatisticsPtr->SetLastError(
2683 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2684 "SetRTCP_CNAME() failed to set RTCP CNAME");
2685 return -1;
2686 }
2687 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002688}
2689
kwiberg55b97fe2016-01-28 05:22:45 -08002690int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2691 if (cName == NULL) {
2692 _engineStatisticsPtr->SetLastError(
2693 VE_INVALID_ARGUMENT, kTraceError,
2694 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2695 return -1;
2696 }
2697 char cname[RTCP_CNAME_SIZE];
2698 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2699 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2700 _engineStatisticsPtr->SetLastError(
2701 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2702 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2703 return -1;
2704 }
2705 strcpy(cName, cname);
2706 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002707}
2708
kwiberg55b97fe2016-01-28 05:22:45 -08002709int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2710 unsigned int& NTPLow,
2711 unsigned int& timestamp,
2712 unsigned int& playoutTimestamp,
2713 unsigned int* jitter,
2714 unsigned short* fractionLost) {
2715 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002716
kwiberg55b97fe2016-01-28 05:22:45 -08002717 RTCPSenderInfo senderInfo;
2718 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2719 _engineStatisticsPtr->SetLastError(
2720 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2721 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2722 "side");
2723 return -1;
2724 }
2725
2726 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2727 // and octet count)
2728 NTPHigh = senderInfo.NTPseconds;
2729 NTPLow = senderInfo.NTPfraction;
2730 timestamp = senderInfo.RTPtimeStamp;
2731
2732 // --- Locally derived information
2733
2734 // This value is updated on each incoming RTCP packet (0 when no packet
2735 // has been received)
2736 playoutTimestamp = playout_timestamp_rtcp_;
2737
2738 if (NULL != jitter || NULL != fractionLost) {
2739 // Get all RTCP receiver report blocks that have been received on this
2740 // channel. If we receive RTP packets from a remote source we know the
2741 // remote SSRC and use the report block from him.
2742 // Otherwise use the first report block.
2743 std::vector<RTCPReportBlock> remote_stats;
2744 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2745 remote_stats.empty()) {
2746 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2747 "GetRemoteRTCPData() failed to measure statistics due"
2748 " to lack of received RTP and/or RTCP packets");
2749 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002750 }
2751
kwiberg55b97fe2016-01-28 05:22:45 -08002752 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2753 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2754 for (; it != remote_stats.end(); ++it) {
2755 if (it->remoteSSRC == remoteSSRC)
2756 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002757 }
kwiberg55b97fe2016-01-28 05:22:45 -08002758
2759 if (it == remote_stats.end()) {
2760 // If we have not received any RTCP packets from this SSRC it probably
2761 // means that we have not received any RTP packets.
2762 // Use the first received report block instead.
2763 it = remote_stats.begin();
2764 remoteSSRC = it->remoteSSRC;
2765 }
2766
2767 if (jitter) {
2768 *jitter = it->jitter;
2769 }
2770
2771 if (fractionLost) {
2772 *fractionLost = it->fractionLost;
2773 }
2774 }
2775 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002776}
2777
kwiberg55b97fe2016-01-28 05:22:45 -08002778int Channel::SendApplicationDefinedRTCPPacket(
2779 unsigned char subType,
2780 unsigned int name,
2781 const char* data,
2782 unsigned short dataLengthInBytes) {
2783 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2784 "Channel::SendApplicationDefinedRTCPPacket()");
2785 if (!channel_state_.Get().sending) {
2786 _engineStatisticsPtr->SetLastError(
2787 VE_NOT_SENDING, kTraceError,
2788 "SendApplicationDefinedRTCPPacket() not sending");
2789 return -1;
2790 }
2791 if (NULL == data) {
2792 _engineStatisticsPtr->SetLastError(
2793 VE_INVALID_ARGUMENT, kTraceError,
2794 "SendApplicationDefinedRTCPPacket() invalid data value");
2795 return -1;
2796 }
2797 if (dataLengthInBytes % 4 != 0) {
2798 _engineStatisticsPtr->SetLastError(
2799 VE_INVALID_ARGUMENT, kTraceError,
2800 "SendApplicationDefinedRTCPPacket() invalid length value");
2801 return -1;
2802 }
2803 RtcpMode status = _rtpRtcpModule->RTCP();
2804 if (status == RtcpMode::kOff) {
2805 _engineStatisticsPtr->SetLastError(
2806 VE_RTCP_ERROR, kTraceError,
2807 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2808 return -1;
2809 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002810
kwiberg55b97fe2016-01-28 05:22:45 -08002811 // Create and schedule the RTCP APP packet for transmission
2812 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2813 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2814 _engineStatisticsPtr->SetLastError(
2815 VE_SEND_ERROR, kTraceError,
2816 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2817 return -1;
2818 }
2819 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002820}
2821
kwiberg55b97fe2016-01-28 05:22:45 -08002822int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2823 unsigned int& maxJitterMs,
2824 unsigned int& discardedPackets) {
2825 // The jitter statistics is updated for each received RTP packet and is
2826 // based on received packets.
2827 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2828 // If RTCP is off, there is no timed thread in the RTCP module regularly
2829 // generating new stats, trigger the update manually here instead.
2830 StreamStatistician* statistician =
2831 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2832 if (statistician) {
2833 // Don't use returned statistics, use data from proxy instead so that
2834 // max jitter can be fetched atomically.
2835 RtcpStatistics s;
2836 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002837 }
kwiberg55b97fe2016-01-28 05:22:45 -08002838 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002839
kwiberg55b97fe2016-01-28 05:22:45 -08002840 ChannelStatistics stats = statistics_proxy_->GetStats();
2841 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2842 if (playoutFrequency > 0) {
2843 // Scale RTP statistics given the current playout frequency
2844 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2845 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2846 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002847
kwiberg55b97fe2016-01-28 05:22:45 -08002848 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002849
kwiberg55b97fe2016-01-28 05:22:45 -08002850 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002851}
2852
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002853int Channel::GetRemoteRTCPReportBlocks(
2854 std::vector<ReportBlock>* report_blocks) {
2855 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002856 _engineStatisticsPtr->SetLastError(
2857 VE_INVALID_ARGUMENT, kTraceError,
2858 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002859 return -1;
2860 }
2861
2862 // Get the report blocks from the latest received RTCP Sender or Receiver
2863 // Report. Each element in the vector contains the sender's SSRC and a
2864 // report block according to RFC 3550.
2865 std::vector<RTCPReportBlock> rtcp_report_blocks;
2866 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002867 return -1;
2868 }
2869
2870 if (rtcp_report_blocks.empty())
2871 return 0;
2872
2873 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2874 for (; it != rtcp_report_blocks.end(); ++it) {
2875 ReportBlock report_block;
2876 report_block.sender_SSRC = it->remoteSSRC;
2877 report_block.source_SSRC = it->sourceSSRC;
2878 report_block.fraction_lost = it->fractionLost;
2879 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2880 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2881 report_block.interarrival_jitter = it->jitter;
2882 report_block.last_SR_timestamp = it->lastSR;
2883 report_block.delay_since_last_SR = it->delaySinceLastSR;
2884 report_blocks->push_back(report_block);
2885 }
2886 return 0;
2887}
2888
kwiberg55b97fe2016-01-28 05:22:45 -08002889int Channel::GetRTPStatistics(CallStatistics& stats) {
2890 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002891
kwiberg55b97fe2016-01-28 05:22:45 -08002892 // The jitter statistics is updated for each received RTP packet and is
2893 // based on received packets.
2894 RtcpStatistics statistics;
2895 StreamStatistician* statistician =
2896 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002897 if (statistician) {
2898 statistician->GetStatistics(&statistics,
2899 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002900 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002901
kwiberg55b97fe2016-01-28 05:22:45 -08002902 stats.fractionLost = statistics.fraction_lost;
2903 stats.cumulativeLost = statistics.cumulative_lost;
2904 stats.extendedMax = statistics.extended_max_sequence_number;
2905 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002906
kwiberg55b97fe2016-01-28 05:22:45 -08002907 // --- RTT
2908 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002909
kwiberg55b97fe2016-01-28 05:22:45 -08002910 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002911
kwiberg55b97fe2016-01-28 05:22:45 -08002912 size_t bytesSent(0);
2913 uint32_t packetsSent(0);
2914 size_t bytesReceived(0);
2915 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002916
kwiberg55b97fe2016-01-28 05:22:45 -08002917 if (statistician) {
2918 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2919 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002920
kwiberg55b97fe2016-01-28 05:22:45 -08002921 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2922 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2923 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2924 " output will not be complete");
2925 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002926
kwiberg55b97fe2016-01-28 05:22:45 -08002927 stats.bytesSent = bytesSent;
2928 stats.packetsSent = packetsSent;
2929 stats.bytesReceived = bytesReceived;
2930 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002931
kwiberg55b97fe2016-01-28 05:22:45 -08002932 // --- Timestamps
2933 {
2934 rtc::CritScope lock(&ts_stats_lock_);
2935 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2936 }
2937 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002938}
2939
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002940int Channel::SetCodecFECStatus(bool enable) {
2941 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2942 "Channel::SetCodecFECStatus()");
2943
kwibergc8d071e2016-04-06 12:22:38 -07002944 if (!codec_manager_.SetCodecFEC(enable) ||
2945 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002946 _engineStatisticsPtr->SetLastError(
2947 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2948 "SetCodecFECStatus() failed to set FEC state");
2949 return -1;
2950 }
2951 return 0;
2952}
2953
2954bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002955 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002956}
2957
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002958void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2959 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002960 // If pacing is enabled we always store packets.
2961 if (!pacing_enabled_)
2962 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002963 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002964 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002965 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002966 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002967 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002968}
2969
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002970// Called when we are missing one or more packets.
2971int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002972 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2973}
2974
kwiberg55b97fe2016-01-28 05:22:45 -08002975uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2976 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2977 "Channel::Demultiplex()");
2978 _audioFrame.CopyFrom(audioFrame);
2979 _audioFrame.id_ = _channelId;
2980 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002981}
2982
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002983void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002984 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002985 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002986 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002987 CodecInst codec;
2988 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002989
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002990 // Never upsample or upmix the capture signal here. This should be done at the
2991 // end of the send chain.
2992 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2993 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2994 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2995 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002996}
2997
kwiberg55b97fe2016-01-28 05:22:45 -08002998uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2999 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
3000 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003001
kwiberg55b97fe2016-01-28 05:22:45 -08003002 if (_audioFrame.samples_per_channel_ == 0) {
3003 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3004 "Channel::PrepareEncodeAndSend() invalid audio frame");
3005 return 0xFFFFFFFF;
3006 }
3007
3008 if (channel_state_.Get().input_file_playing) {
3009 MixOrReplaceAudioWithFile(mixingFrequency);
3010 }
3011
solenberg1c2af8e2016-03-24 10:36:00 -07003012 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
3013 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08003014
3015 if (channel_state_.Get().input_external_media) {
3016 rtc::CritScope cs(&_callbackCritSect);
3017 const bool isStereo = (_audioFrame.num_channels_ == 2);
3018 if (_inputExternalMediaCallbackPtr) {
3019 _inputExternalMediaCallbackPtr->Process(
3020 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
3021 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
3022 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00003023 }
kwiberg55b97fe2016-01-28 05:22:45 -08003024 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003025
kwiberg55b97fe2016-01-28 05:22:45 -08003026 if (_includeAudioLevelIndication) {
3027 size_t length =
3028 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02003029 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07003030 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003031 rms_level_.ProcessMuted(length);
3032 } else {
3033 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00003034 }
kwiberg55b97fe2016-01-28 05:22:45 -08003035 }
solenberg1c2af8e2016-03-24 10:36:00 -07003036 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00003037
kwiberg55b97fe2016-01-28 05:22:45 -08003038 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003039}
3040
kwiberg55b97fe2016-01-28 05:22:45 -08003041uint32_t Channel::EncodeAndSend() {
3042 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
3043 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003044
kwiberg55b97fe2016-01-28 05:22:45 -08003045 assert(_audioFrame.num_channels_ <= 2);
3046 if (_audioFrame.samples_per_channel_ == 0) {
3047 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3048 "Channel::EncodeAndSend() invalid audio frame");
3049 return 0xFFFFFFFF;
3050 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003051
kwiberg55b97fe2016-01-28 05:22:45 -08003052 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003053
kwiberg55b97fe2016-01-28 05:22:45 -08003054 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00003055
kwiberg55b97fe2016-01-28 05:22:45 -08003056 // The ACM resamples internally.
3057 _audioFrame.timestamp_ = _timeStamp;
3058 // This call will trigger AudioPacketizationCallback::SendData if encoding
3059 // is done and payload is ready for packetization and transmission.
3060 // Otherwise, it will return without invoking the callback.
3061 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
3062 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
3063 "Channel::EncodeAndSend() ACM encoding failed");
3064 return 0xFFFFFFFF;
3065 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003066
kwiberg55b97fe2016-01-28 05:22:45 -08003067 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
3068 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003069}
3070
Minyue2013aec2015-05-13 14:14:42 +02003071void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08003072 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003073 Channel* channel = associate_send_channel_.channel();
3074 if (channel && channel->ChannelId() == channel_id) {
3075 // If this channel is associated with a send channel of the specified
3076 // Channel ID, disassociate with it.
3077 ChannelOwner ref(NULL);
3078 associate_send_channel_ = ref;
3079 }
3080}
3081
ivoc14d5dbe2016-07-04 07:06:55 -07003082void Channel::SetRtcEventLog(RtcEventLog* event_log) {
3083 event_log_proxy_->SetEventLog(event_log);
3084}
3085
kwiberg55b97fe2016-01-28 05:22:45 -08003086int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
3087 VoEMediaProcess& processObject) {
3088 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3089 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003090
kwiberg55b97fe2016-01-28 05:22:45 -08003091 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003092
kwiberg55b97fe2016-01-28 05:22:45 -08003093 if (kPlaybackPerChannel == type) {
3094 if (_outputExternalMediaCallbackPtr) {
3095 _engineStatisticsPtr->SetLastError(
3096 VE_INVALID_OPERATION, kTraceError,
3097 "Channel::RegisterExternalMediaProcessing() "
3098 "output external media already enabled");
3099 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003100 }
kwiberg55b97fe2016-01-28 05:22:45 -08003101 _outputExternalMediaCallbackPtr = &processObject;
3102 _outputExternalMedia = true;
3103 } else if (kRecordingPerChannel == type) {
3104 if (_inputExternalMediaCallbackPtr) {
3105 _engineStatisticsPtr->SetLastError(
3106 VE_INVALID_OPERATION, kTraceError,
3107 "Channel::RegisterExternalMediaProcessing() "
3108 "output external media already enabled");
3109 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003110 }
kwiberg55b97fe2016-01-28 05:22:45 -08003111 _inputExternalMediaCallbackPtr = &processObject;
3112 channel_state_.SetInputExternalMedia(true);
3113 }
3114 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003115}
3116
kwiberg55b97fe2016-01-28 05:22:45 -08003117int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
3118 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3119 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003120
kwiberg55b97fe2016-01-28 05:22:45 -08003121 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003122
kwiberg55b97fe2016-01-28 05:22:45 -08003123 if (kPlaybackPerChannel == type) {
3124 if (!_outputExternalMediaCallbackPtr) {
3125 _engineStatisticsPtr->SetLastError(
3126 VE_INVALID_OPERATION, kTraceWarning,
3127 "Channel::DeRegisterExternalMediaProcessing() "
3128 "output external media already disabled");
3129 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003130 }
kwiberg55b97fe2016-01-28 05:22:45 -08003131 _outputExternalMedia = false;
3132 _outputExternalMediaCallbackPtr = NULL;
3133 } else if (kRecordingPerChannel == type) {
3134 if (!_inputExternalMediaCallbackPtr) {
3135 _engineStatisticsPtr->SetLastError(
3136 VE_INVALID_OPERATION, kTraceWarning,
3137 "Channel::DeRegisterExternalMediaProcessing() "
3138 "input external media already disabled");
3139 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003140 }
kwiberg55b97fe2016-01-28 05:22:45 -08003141 channel_state_.SetInputExternalMedia(false);
3142 _inputExternalMediaCallbackPtr = NULL;
3143 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003144
kwiberg55b97fe2016-01-28 05:22:45 -08003145 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003146}
3147
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003148int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08003149 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3150 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003151
kwiberg55b97fe2016-01-28 05:22:45 -08003152 if (channel_state_.Get().playing) {
3153 _engineStatisticsPtr->SetLastError(
3154 VE_INVALID_OPERATION, kTraceError,
3155 "Channel::SetExternalMixing() "
3156 "external mixing cannot be changed while playing.");
3157 return -1;
3158 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003159
kwiberg55b97fe2016-01-28 05:22:45 -08003160 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003161
kwiberg55b97fe2016-01-28 05:22:45 -08003162 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003163}
3164
kwiberg55b97fe2016-01-28 05:22:45 -08003165int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
3166 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003167}
3168
wu@webrtc.org24301a62013-12-13 19:17:43 +00003169void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3170 audio_coding_->GetDecodingCallStatistics(stats);
3171}
3172
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003173bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3174 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08003175 rtc::CritScope lock(&video_sync_lock_);
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003176 if (_average_jitter_buffer_delay_us == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003177 return false;
3178 }
kwiberg55b97fe2016-01-28 05:22:45 -08003179 *jitter_buffer_delay_ms =
3180 (_average_jitter_buffer_delay_us + 500) / 1000 + _recPacketDelayMs;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003181 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003182 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003183}
3184
solenberg358057b2015-11-27 10:46:42 -08003185uint32_t Channel::GetDelayEstimate() const {
3186 int jitter_buffer_delay_ms = 0;
3187 int playout_buffer_delay_ms = 0;
3188 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
3189 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
3190}
3191
deadbeef74375882015-08-13 12:09:10 -07003192int Channel::LeastRequiredDelayMs() const {
3193 return audio_coding_->LeastRequiredDelayMs();
3194}
3195
kwiberg55b97fe2016-01-28 05:22:45 -08003196int Channel::SetMinimumPlayoutDelay(int delayMs) {
3197 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3198 "Channel::SetMinimumPlayoutDelay()");
3199 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3200 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
3201 _engineStatisticsPtr->SetLastError(
3202 VE_INVALID_ARGUMENT, kTraceError,
3203 "SetMinimumPlayoutDelay() invalid min delay");
3204 return -1;
3205 }
3206 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
3207 _engineStatisticsPtr->SetLastError(
3208 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3209 "SetMinimumPlayoutDelay() failed to set min playout delay");
3210 return -1;
3211 }
3212 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003213}
3214
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003215int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07003216 uint32_t playout_timestamp_rtp = 0;
3217 {
tommi31fc21f2016-01-21 10:37:37 -08003218 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003219 playout_timestamp_rtp = playout_timestamp_rtp_;
3220 }
kwiberg55b97fe2016-01-28 05:22:45 -08003221 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003222 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07003223 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003224 "GetPlayoutTimestamp() failed to retrieve timestamp");
3225 return -1;
3226 }
deadbeef74375882015-08-13 12:09:10 -07003227 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003228 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003229}
3230
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003231int Channel::SetInitTimestamp(unsigned int timestamp) {
3232 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003233 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003234 if (channel_state_.Get().sending) {
3235 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3236 "SetInitTimestamp() already sending");
3237 return -1;
3238 }
3239 _rtpRtcpModule->SetStartTimestamp(timestamp);
3240 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003241}
3242
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003243int Channel::SetInitSequenceNumber(short sequenceNumber) {
3244 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3245 "Channel::SetInitSequenceNumber()");
3246 if (channel_state_.Get().sending) {
3247 _engineStatisticsPtr->SetLastError(
3248 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3249 return -1;
3250 }
3251 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3252 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003253}
3254
kwiberg55b97fe2016-01-28 05:22:45 -08003255int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3256 RtpReceiver** rtp_receiver) const {
3257 *rtpRtcpModule = _rtpRtcpModule.get();
3258 *rtp_receiver = rtp_receiver_.get();
3259 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003260}
3261
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003262// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3263// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003264int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003265 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003266 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003267
kwiberg55b97fe2016-01-28 05:22:45 -08003268 {
3269 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003270
kwiberg5a25d952016-08-17 07:31:12 -07003271 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003272 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3273 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3274 " doesnt exist");
3275 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003276 }
3277
kwiberg4ec01d92016-08-22 08:43:54 -07003278 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003279 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003280 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3281 "Channel::MixOrReplaceAudioWithFile() file mixing "
3282 "failed");
3283 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003284 }
kwiberg55b97fe2016-01-28 05:22:45 -08003285 if (fileSamples == 0) {
3286 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3287 "Channel::MixOrReplaceAudioWithFile() file is ended");
3288 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003289 }
kwiberg55b97fe2016-01-28 05:22:45 -08003290 }
3291
3292 assert(_audioFrame.samples_per_channel_ == fileSamples);
3293
3294 if (_mixFileWithMicrophone) {
3295 // Currently file stream is always mono.
3296 // TODO(xians): Change the code when FilePlayer supports real stereo.
3297 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3298 1, fileSamples);
3299 } else {
3300 // Replace ACM audio with file.
3301 // Currently file stream is always mono.
3302 // TODO(xians): Change the code when FilePlayer supports real stereo.
3303 _audioFrame.UpdateFrame(
3304 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3305 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3306 }
3307 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003308}
3309
kwiberg55b97fe2016-01-28 05:22:45 -08003310int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3311 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003312
kwibergb7f89d62016-02-17 10:04:18 -08003313 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003314 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003315
kwiberg55b97fe2016-01-28 05:22:45 -08003316 {
3317 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003318
kwiberg5a25d952016-08-17 07:31:12 -07003319 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003320 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3321 "Channel::MixAudioWithFile() file mixing failed");
3322 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003323 }
3324
kwiberg55b97fe2016-01-28 05:22:45 -08003325 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003326 if (output_file_player_->Get10msAudioFromFile(
3327 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003328 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3329 "Channel::MixAudioWithFile() file mixing failed");
3330 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003331 }
kwiberg55b97fe2016-01-28 05:22:45 -08003332 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003333
kwiberg55b97fe2016-01-28 05:22:45 -08003334 if (audioFrame.samples_per_channel_ == fileSamples) {
3335 // Currently file stream is always mono.
3336 // TODO(xians): Change the code when FilePlayer supports real stereo.
3337 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3338 fileSamples);
3339 } else {
3340 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3341 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3342 ") != "
3343 "fileSamples(%" PRIuS ")",
3344 audioFrame.samples_per_channel_, fileSamples);
3345 return -1;
3346 }
3347
3348 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003349}
3350
deadbeef74375882015-08-13 12:09:10 -07003351void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003352 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003353
henrik.lundin96bd5022016-04-06 04:13:56 -07003354 if (!jitter_buffer_playout_timestamp_) {
3355 // This can happen if this channel has not received any RTP packets. In
3356 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003357 return;
3358 }
3359
3360 uint16_t delay_ms = 0;
3361 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003362 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003363 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3364 " delay from the ADM");
3365 _engineStatisticsPtr->SetLastError(
3366 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3367 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3368 return;
3369 }
3370
henrik.lundin96bd5022016-04-06 04:13:56 -07003371 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3372 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003373
3374 // Remove the playout delay.
henrik.lundin96bd5022016-04-06 04:13:56 -07003375 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003376
kwiberg55b97fe2016-01-28 05:22:45 -08003377 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003378 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003379 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003380
3381 {
tommi31fc21f2016-01-21 10:37:37 -08003382 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003383 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003384 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003385 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003386 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003387 }
3388 playout_delay_ms_ = delay_ms;
3389 }
3390}
3391
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003392// Called for incoming RTP packets after successful RTP header parsing.
3393void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
3394 uint16_t sequence_number) {
kwiberg55b97fe2016-01-28 05:22:45 -08003395 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003396 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
3397 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00003398
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003399 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00003400 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00003401
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003402 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
henrik.lundin96bd5022016-04-06 04:13:56 -07003403 // every incoming packet. May be empty if no valid playout timestamp is
3404 // available.
3405 // If |rtp_timestamp| is newer than |jitter_buffer_playout_timestamp_|, the
3406 // resulting difference is positive and will be used. When the inverse is
3407 // true (can happen when a network glitch causes a packet to arrive late,
3408 // and during long comfort noise periods with clock drift), or when
3409 // |jitter_buffer_playout_timestamp_| has no value, the difference is not
3410 // changed from the initial 0.
3411 uint32_t timestamp_diff_ms = 0;
3412 if (jitter_buffer_playout_timestamp_ &&
3413 IsNewerTimestamp(rtp_timestamp, *jitter_buffer_playout_timestamp_)) {
3414 timestamp_diff_ms = (rtp_timestamp - *jitter_buffer_playout_timestamp_) /
3415 (rtp_receive_frequency / 1000);
3416 if (timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
3417 // Diff is too large; set it to zero instead.
3418 timestamp_diff_ms = 0;
3419 }
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00003420 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003421
kwiberg55b97fe2016-01-28 05:22:45 -08003422 uint16_t packet_delay_ms =
3423 (rtp_timestamp - _previousTimestamp) / (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003424
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003425 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00003426
kwiberg55b97fe2016-01-28 05:22:45 -08003427 if (timestamp_diff_ms == 0)
3428 return;
niklase@google.com470e71d2011-07-07 08:21:25 +00003429
deadbeef74375882015-08-13 12:09:10 -07003430 {
tommi31fc21f2016-01-21 10:37:37 -08003431 rtc::CritScope lock(&video_sync_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003432
deadbeef74375882015-08-13 12:09:10 -07003433 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
3434 _recPacketDelayMs = packet_delay_ms;
3435 }
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003436
deadbeef74375882015-08-13 12:09:10 -07003437 if (_average_jitter_buffer_delay_us == 0) {
3438 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
3439 return;
3440 }
3441
3442 // Filter average delay value using exponential filter (alpha is
3443 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
3444 // risk of rounding error) and compensate for it in GetDelayEstimate()
3445 // later.
kwiberg55b97fe2016-01-28 05:22:45 -08003446 _average_jitter_buffer_delay_us =
3447 (_average_jitter_buffer_delay_us * 7 + 1000 * timestamp_diff_ms + 500) /
3448 8;
deadbeef74375882015-08-13 12:09:10 -07003449 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003450}
3451
kwiberg55b97fe2016-01-28 05:22:45 -08003452void Channel::RegisterReceiveCodecsToRTPModule() {
3453 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3454 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003455
kwiberg55b97fe2016-01-28 05:22:45 -08003456 CodecInst codec;
3457 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003458
kwiberg55b97fe2016-01-28 05:22:45 -08003459 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3460 // Open up the RTP/RTCP receiver for all supported codecs
3461 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3462 (rtp_receiver_->RegisterReceivePayload(
3463 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3464 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3465 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3466 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3467 " to register %s (%d/%d/%" PRIuS
3468 "/%d) to RTP/RTCP "
3469 "receiver",
3470 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3471 codec.rate);
3472 } else {
3473 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3474 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3475 "(%d/%d/%" PRIuS
3476 "/%d) has been added to the RTP/RTCP "
3477 "receiver",
3478 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3479 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003480 }
kwiberg55b97fe2016-01-28 05:22:45 -08003481 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003482}
3483
kwiberg55b97fe2016-01-28 05:22:45 -08003484int Channel::SetSendRtpHeaderExtension(bool enable,
3485 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003486 unsigned char id) {
3487 int error = 0;
3488 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3489 if (enable) {
3490 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3491 }
3492 return error;
3493}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003494
wu@webrtc.org94454b72014-06-05 20:34:08 +00003495int32_t Channel::GetPlayoutFrequency() {
3496 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3497 CodecInst current_recive_codec;
3498 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3499 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3500 // Even though the actual sampling rate for G.722 audio is
3501 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3502 // 8,000 Hz because that value was erroneously assigned in
3503 // RFC 1890 and must remain unchanged for backward compatibility.
3504 playout_frequency = 8000;
3505 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3506 // We are resampling Opus internally to 32,000 Hz until all our
3507 // DSP routines can operate at 48,000 Hz, but the RTP clock
3508 // rate for the Opus payload format is standardized to 48,000 Hz,
3509 // because that is the maximum supported decoding sampling rate.
3510 playout_frequency = 48000;
3511 }
3512 }
3513 return playout_frequency;
3514}
3515
Minyue2013aec2015-05-13 14:14:42 +02003516int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003517 RtcpMode method = _rtpRtcpModule->RTCP();
3518 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003519 return 0;
3520 }
3521 std::vector<RTCPReportBlock> report_blocks;
3522 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003523
3524 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003525 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003526 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003527 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003528 Channel* channel = associate_send_channel_.channel();
3529 // Tries to get RTT from an associated channel. This is important for
3530 // receive-only channels.
3531 if (channel) {
3532 // To prevent infinite recursion and deadlock, calling GetRTT of
3533 // associate channel should always use "false" for argument:
3534 // |allow_associate_channel|.
3535 rtt = channel->GetRTT(false);
3536 }
3537 }
3538 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003539 }
3540
3541 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3542 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3543 for (; it != report_blocks.end(); ++it) {
3544 if (it->remoteSSRC == remoteSSRC)
3545 break;
3546 }
3547 if (it == report_blocks.end()) {
3548 // We have not received packets with SSRC matching the report blocks.
3549 // To calculate RTT we try with the SSRC of the first report block.
3550 // This is very important for send-only channels where we don't know
3551 // the SSRC of the other end.
3552 remoteSSRC = report_blocks[0].remoteSSRC;
3553 }
Minyue2013aec2015-05-13 14:14:42 +02003554
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003555 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003556 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003557 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003558 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3559 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003560 return 0;
3561 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003562 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003563}
3564
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003565} // namespace voe
3566} // namespace webrtc