blob: d6daffddf83ee0f751d59665e85b15d89ff2622b [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
kwiberg55b97fe2016-01-28 05:22:45 -0800533 int64_t round_trip_time = 0;
534 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
535 NULL);
536
537 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
538 if (!nack_list.empty()) {
539 // Can't use nack_list.data() since it's not supported by all
540 // compilers.
541 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
542 }
543 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000544}
545
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000546bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000547 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000548 RTPHeader header;
549 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
550 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
551 "IncomingPacket invalid RTP header");
552 return false;
553 }
554 header.payload_type_frequency =
555 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
556 if (header.payload_type_frequency < 0)
557 return false;
558 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
559}
560
henrik.lundin42dda502016-05-18 05:36:01 -0700561MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
562 int32_t id,
563 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700564 unsigned int ssrc;
565 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
566 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800567 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700568 bool muted;
569 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
570 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800571 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
572 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
573 // In all likelihood, the audio in this frame is garbage. We return an
574 // error so that the audio mixer module doesn't add it to the mix. As
575 // a result, it won't be played out and the actions skipped here are
576 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700577 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800578 }
henrik.lundina89ab962016-05-18 08:52:45 -0700579
580 if (muted) {
581 // TODO(henrik.lundin): We should be able to do better than this. But we
582 // will have to go through all the cases below where the audio samples may
583 // be used, and handle the muted case in some way.
584 audioFrame->Mute();
585 }
kwiberg55b97fe2016-01-28 05:22:45 -0800586
587 if (_RxVadDetection) {
588 UpdateRxVadDetection(*audioFrame);
589 }
590
591 // Convert module ID to internal VoE channel ID
592 audioFrame->id_ = VoEChannelId(audioFrame->id_);
593 // Store speech type for dead-or-alive detection
594 _outputSpeechType = audioFrame->speech_type_;
595
596 ChannelState::State state = channel_state_.Get();
597
598 if (state.rx_apm_is_enabled) {
599 int err = rx_audioproc_->ProcessStream(audioFrame);
600 if (err) {
601 LOG(LS_ERROR) << "ProcessStream() error: " << err;
602 assert(false);
Ivo Creusenae856f22015-09-17 16:30:16 +0200603 }
kwiberg55b97fe2016-01-28 05:22:45 -0800604 }
605
606 {
607 // Pass the audio buffers to an optional sink callback, before applying
608 // scaling/panning, as that applies to the mix operation.
609 // External recipients of the audio (e.g. via AudioTrack), will do their
610 // own mixing/dynamic processing.
611 rtc::CritScope cs(&_callbackCritSect);
612 if (audio_sink_) {
613 AudioSinkInterface::Data data(
614 &audioFrame->data_[0], audioFrame->samples_per_channel_,
615 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
616 audioFrame->timestamp_);
617 audio_sink_->OnData(data);
618 }
619 }
620
621 float output_gain = 1.0f;
622 float left_pan = 1.0f;
623 float right_pan = 1.0f;
624 {
625 rtc::CritScope cs(&volume_settings_critsect_);
626 output_gain = _outputGain;
627 left_pan = _panLeft;
628 right_pan = _panRight;
629 }
630
631 // Output volume scaling
632 if (output_gain < 0.99f || output_gain > 1.01f) {
633 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
634 }
635
636 // Scale left and/or right channel(s) if stereo and master balance is
637 // active
638
639 if (left_pan != 1.0f || right_pan != 1.0f) {
640 if (audioFrame->num_channels_ == 1) {
641 // Emulate stereo mode since panning is active.
642 // The mono signal is copied to both left and right channels here.
643 AudioFrameOperations::MonoToStereo(audioFrame);
644 }
645 // For true stereo mode (when we are receiving a stereo signal), no
646 // action is needed.
647
648 // Do the panning operation (the audio frame contains stereo at this
649 // stage)
650 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
651 }
652
653 // Mix decoded PCM output with file if file mixing is enabled
654 if (state.output_file_playing) {
655 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700656 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800657 }
658
659 // External media
660 if (_outputExternalMedia) {
661 rtc::CritScope cs(&_callbackCritSect);
662 const bool isStereo = (audioFrame->num_channels_ == 2);
663 if (_outputExternalMediaCallbackPtr) {
664 _outputExternalMediaCallbackPtr->Process(
665 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
666 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
667 isStereo);
668 }
669 }
670
671 // Record playout if enabled
672 {
673 rtc::CritScope cs(&_fileCritSect);
674
kwiberg5a25d952016-08-17 07:31:12 -0700675 if (_outputFileRecording && output_file_recorder_) {
676 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800677 }
678 }
679
680 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700681 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800682 _outputAudioLevel.ComputeLevel(*audioFrame);
683
684 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
685 // The first frame with a valid rtp timestamp.
686 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
687 }
688
689 if (capture_start_rtp_time_stamp_ >= 0) {
690 // audioFrame.timestamp_ should be valid from now on.
691
692 // Compute elapsed time.
693 int64_t unwrap_timestamp =
694 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
695 audioFrame->elapsed_time_ms_ =
696 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
697 (GetPlayoutFrequency() / 1000);
698
niklase@google.com470e71d2011-07-07 08:21:25 +0000699 {
kwiberg55b97fe2016-01-28 05:22:45 -0800700 rtc::CritScope lock(&ts_stats_lock_);
701 // Compute ntp time.
702 audioFrame->ntp_time_ms_ =
703 ntp_estimator_.Estimate(audioFrame->timestamp_);
704 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
705 if (audioFrame->ntp_time_ms_ > 0) {
706 // Compute |capture_start_ntp_time_ms_| so that
707 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
708 capture_start_ntp_time_ms_ =
709 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000710 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 }
kwiberg55b97fe2016-01-28 05:22:45 -0800712 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000713
henrik.lundin42dda502016-05-18 05:36:01 -0700714 return muted ? MixerParticipant::AudioFrameInfo::kMuted
715 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000716}
717
kwiberg55b97fe2016-01-28 05:22:45 -0800718int32_t Channel::NeededFrequency(int32_t id) const {
719 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
720 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000721
kwiberg55b97fe2016-01-28 05:22:45 -0800722 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000723
kwiberg55b97fe2016-01-28 05:22:45 -0800724 // Determine highest needed receive frequency
725 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000726
kwiberg55b97fe2016-01-28 05:22:45 -0800727 // Return the bigger of playout and receive frequency in the ACM.
728 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
729 highestNeeded = audio_coding_->PlayoutFrequency();
730 } else {
731 highestNeeded = receiveFrequency;
732 }
733
734 // Special case, if we're playing a file on the playout side
735 // we take that frequency into consideration as well
736 // This is not needed on sending side, since the codec will
737 // limit the spectrum anyway.
738 if (channel_state_.Get().output_file_playing) {
739 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700740 if (output_file_player_) {
741 if (output_file_player_->Frequency() > highestNeeded) {
742 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800743 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000744 }
kwiberg55b97fe2016-01-28 05:22:45 -0800745 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000746
kwiberg55b97fe2016-01-28 05:22:45 -0800747 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000748}
749
ossu5f7cfa52016-05-30 08:11:28 -0700750int32_t Channel::CreateChannel(
751 Channel*& channel,
752 int32_t channelId,
753 uint32_t instanceId,
ossu5f7cfa52016-05-30 08:11:28 -0700754 const Config& config,
755 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800756 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
757 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
758 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000759
ivoc14d5dbe2016-07-04 07:06:55 -0700760 channel = new Channel(channelId, instanceId, config, decoder_factory);
kwiberg55b97fe2016-01-28 05:22:45 -0800761 if (channel == NULL) {
762 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
763 "Channel::CreateChannel() unable to allocate memory for"
764 " channel");
765 return -1;
766 }
767 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000768}
769
kwiberg55b97fe2016-01-28 05:22:45 -0800770void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
771 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
772 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
773 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000774
kwiberg55b97fe2016-01-28 05:22:45 -0800775 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000776}
777
kwiberg55b97fe2016-01-28 05:22:45 -0800778void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
779 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
780 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
781 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
kwiberg55b97fe2016-01-28 05:22:45 -0800783 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000784}
785
kwiberg55b97fe2016-01-28 05:22:45 -0800786void Channel::PlayFileEnded(int32_t id) {
787 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
788 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
kwiberg55b97fe2016-01-28 05:22:45 -0800790 if (id == _inputFilePlayerId) {
791 channel_state_.SetInputFilePlaying(false);
792 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
793 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000794 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800795 } else if (id == _outputFilePlayerId) {
796 channel_state_.SetOutputFilePlaying(false);
797 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
798 "Channel::PlayFileEnded() => output file player module is"
799 " shutdown");
800 }
801}
802
803void Channel::RecordFileEnded(int32_t id) {
804 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
805 "Channel::RecordFileEnded(id=%d)", id);
806
807 assert(id == _outputFileRecorderId);
808
809 rtc::CritScope cs(&_fileCritSect);
810
811 _outputFileRecording = false;
812 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
813 "Channel::RecordFileEnded() => output file recorder module is"
814 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000815}
816
pbos@webrtc.org92135212013-05-14 08:31:39 +0000817Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000818 uint32_t instanceId,
ossu5f7cfa52016-05-30 08:11:28 -0700819 const Config& config,
820 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory)
tommi31fc21f2016-01-21 10:37:37 -0800821 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100822 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700823 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100824 rtp_header_parser_(RtpHeaderParser::Create()),
825 rtp_payload_registry_(
826 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
827 rtp_receive_statistics_(
828 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
829 rtp_receiver_(
830 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100831 this,
832 this,
833 rtp_payload_registry_.get())),
834 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
835 _outputAudioLevel(),
836 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100837 // Avoid conflict with other channels by adding 1024 - 1026,
838 // won't use as much as 1024 channels.
839 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
840 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
841 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
842 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100843 _outputExternalMedia(false),
844 _inputExternalMediaCallbackPtr(NULL),
845 _outputExternalMediaCallbackPtr(NULL),
846 _timeStamp(0), // This is just an offset, RTP module will add it's own
847 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100848 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100849 playout_timestamp_rtp_(0),
850 playout_timestamp_rtcp_(0),
851 playout_delay_ms_(0),
852 _numberOfDiscardedPackets(0),
853 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
855 capture_start_rtp_time_stamp_(-1),
856 capture_start_ntp_time_ms_(-1),
857 _engineStatisticsPtr(NULL),
858 _outputMixerPtr(NULL),
859 _transmitMixerPtr(NULL),
860 _moduleProcessThreadPtr(NULL),
861 _audioDeviceModulePtr(NULL),
862 _voiceEngineObserverPtr(NULL),
863 _callbackCritSectPtr(NULL),
864 _transportPtr(NULL),
865 _rxVadObserverPtr(NULL),
866 _oldVadDecision(-1),
867 _sendFrameType(0),
868 _externalMixing(false),
869 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700870 input_mute_(false),
871 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 _panLeft(1.0f),
873 _panRight(1.0f),
874 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100875 _lastLocalTimeStamp(0),
876 _lastPayloadType(0),
877 _includeAudioLevelIndication(false),
878 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100879 _RxVadDetection(false),
880 _rxAgcIsEnabled(false),
881 _rxNsIsEnabled(false),
882 restored_packet_in_use_(false),
883 rtcp_observer_(new VoERtcpObserver(this)),
884 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 associate_send_channel_(ChannelOwner(nullptr)),
886 pacing_enabled_(config.Get<VoicePacing>().enabled),
stefanbba9dec2016-02-01 04:39:55 -0800887 feedback_observer_proxy_(new TransportFeedbackProxy()),
888 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700889 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200890 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
891 kMaxRetransmissionWindowMs)),
ossu29b1a8d2016-06-13 07:34:51 -0700892 decoder_factory_(decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800893 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
894 "Channel::Channel() - ctor");
895 AudioCodingModule::Config acm_config;
896 acm_config.id = VoEModuleId(instanceId, channelId);
897 if (config.Get<NetEqCapacityConfig>().enabled) {
898 // Clamping the buffer capacity at 20 packets. While going lower will
899 // probably work, it makes little sense.
900 acm_config.neteq_config.max_packets_in_buffer =
901 std::max(20, config.Get<NetEqCapacityConfig>().capacity);
902 }
903 acm_config.neteq_config.enable_fast_accelerate =
904 config.Get<NetEqFastAccelerate>().enabled;
henrik.lundina89ab962016-05-18 08:52:45 -0700905 acm_config.neteq_config.enable_muted_state = true;
ossu5f7cfa52016-05-30 08:11:28 -0700906 acm_config.decoder_factory = decoder_factory;
kwiberg55b97fe2016-01-28 05:22:45 -0800907 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200908
kwiberg55b97fe2016-01-28 05:22:45 -0800909 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000910
kwiberg55b97fe2016-01-28 05:22:45 -0800911 RtpRtcp::Configuration configuration;
912 configuration.audio = true;
913 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800914 configuration.receive_statistics = rtp_receive_statistics_.get();
915 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800916 if (pacing_enabled_) {
917 configuration.paced_sender = rtp_packet_sender_proxy_.get();
918 configuration.transport_sequence_number_allocator =
919 seq_num_allocator_proxy_.get();
920 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
921 }
ivoc14d5dbe2016-07-04 07:06:55 -0700922 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200923 configuration.retransmission_rate_limiter =
924 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000925
kwiberg55b97fe2016-01-28 05:22:45 -0800926 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100927 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000928
kwiberg55b97fe2016-01-28 05:22:45 -0800929 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
930 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
931 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000932
kwiberg55b97fe2016-01-28 05:22:45 -0800933 Config audioproc_config;
934 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
935 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000936}
937
kwiberg55b97fe2016-01-28 05:22:45 -0800938Channel::~Channel() {
939 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
940 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
941 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000942
kwiberg55b97fe2016-01-28 05:22:45 -0800943 if (_outputExternalMedia) {
944 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
945 }
946 if (channel_state_.Get().input_external_media) {
947 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
948 }
949 StopSend();
950 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000951
kwiberg55b97fe2016-01-28 05:22:45 -0800952 {
953 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700954 if (input_file_player_) {
955 input_file_player_->RegisterModuleFileCallback(NULL);
956 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000957 }
kwiberg5a25d952016-08-17 07:31:12 -0700958 if (output_file_player_) {
959 output_file_player_->RegisterModuleFileCallback(NULL);
960 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800961 }
kwiberg5a25d952016-08-17 07:31:12 -0700962 if (output_file_recorder_) {
963 output_file_recorder_->RegisterModuleFileCallback(NULL);
964 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800965 }
966 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000967
kwiberg55b97fe2016-01-28 05:22:45 -0800968 // The order to safely shutdown modules in a channel is:
969 // 1. De-register callbacks in modules
970 // 2. De-register modules in process thread
971 // 3. Destroy modules
972 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
973 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
974 "~Channel() failed to de-register transport callback"
975 " (Audio coding module)");
976 }
977 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
978 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
979 "~Channel() failed to de-register VAD callback"
980 " (Audio coding module)");
981 }
982 // De-register modules in process thread
983 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000984
kwiberg55b97fe2016-01-28 05:22:45 -0800985 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000986}
987
kwiberg55b97fe2016-01-28 05:22:45 -0800988int32_t Channel::Init() {
989 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
990 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000991
kwiberg55b97fe2016-01-28 05:22:45 -0800992 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000993
kwiberg55b97fe2016-01-28 05:22:45 -0800994 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000995
kwiberg55b97fe2016-01-28 05:22:45 -0800996 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
997 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
998 "Channel::Init() must call SetEngineInformation() first");
999 return -1;
1000 }
1001
1002 // --- Add modules to process thread (for periodic schedulation)
1003
1004 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1005
1006 // --- ACM initialization
1007
1008 if (audio_coding_->InitializeReceiver() == -1) {
1009 _engineStatisticsPtr->SetLastError(
1010 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1011 "Channel::Init() unable to initialize the ACM - 1");
1012 return -1;
1013 }
1014
1015 // --- RTP/RTCP module initialization
1016
1017 // Ensure that RTCP is enabled by default for the created channel.
1018 // Note that, the module will keep generating RTCP until it is explicitly
1019 // disabled by the user.
1020 // After StopListen (when no sockets exists), RTCP packets will no longer
1021 // be transmitted since the Transport object will then be invalid.
1022 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
1023 // RTCP is enabled by default.
1024 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1025 // --- Register all permanent callbacks
1026 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1027 (audio_coding_->RegisterVADCallback(this) == -1);
1028
1029 if (fail) {
1030 _engineStatisticsPtr->SetLastError(
1031 VE_CANNOT_INIT_CHANNEL, kTraceError,
1032 "Channel::Init() callbacks not registered");
1033 return -1;
1034 }
1035
1036 // --- Register all supported codecs to the receiving side of the
1037 // RTP/RTCP module
1038
1039 CodecInst codec;
1040 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1041
1042 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1043 // Open up the RTP/RTCP receiver for all supported codecs
1044 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1045 (rtp_receiver_->RegisterReceivePayload(
1046 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1047 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1048 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1049 "Channel::Init() unable to register %s "
1050 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1051 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1052 codec.rate);
1053 } else {
1054 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1055 "Channel::Init() %s (%d/%d/%" PRIuS
1056 "/%d) has been "
1057 "added to the RTP/RTCP receiver",
1058 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1059 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001060 }
1061
kwiberg55b97fe2016-01-28 05:22:45 -08001062 // Ensure that PCMU is used as default codec on the sending side
1063 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1064 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 }
1066
kwiberg55b97fe2016-01-28 05:22:45 -08001067 // Register default PT for outband 'telephone-event'
1068 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001069 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1070 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001071 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1072 "Channel::Init() failed to register outband "
1073 "'telephone-event' (%d/%d) correctly",
1074 codec.pltype, codec.plfreq);
1075 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001076 }
1077
kwiberg55b97fe2016-01-28 05:22:45 -08001078 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001079 if (!codec_manager_.RegisterEncoder(codec) ||
1080 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1081 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1082 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001083 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1084 "Channel::Init() failed to register CN (%d/%d) "
1085 "correctly - 1",
1086 codec.pltype, codec.plfreq);
1087 }
1088 }
kwiberg55b97fe2016-01-28 05:22:45 -08001089 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001090
kwiberg55b97fe2016-01-28 05:22:45 -08001091 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1092 LOG(LS_ERROR) << "noise_suppression()->set_level(kDefaultNsMode) failed.";
1093 return -1;
1094 }
1095 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1096 LOG(LS_ERROR) << "gain_control()->set_mode(kDefaultRxAgcMode) failed.";
1097 return -1;
1098 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001099
kwiberg55b97fe2016-01-28 05:22:45 -08001100 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001101}
1102
kwiberg55b97fe2016-01-28 05:22:45 -08001103int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1104 OutputMixer& outputMixer,
1105 voe::TransmitMixer& transmitMixer,
1106 ProcessThread& moduleProcessThread,
1107 AudioDeviceModule& audioDeviceModule,
1108 VoiceEngineObserver* voiceEngineObserver,
1109 rtc::CriticalSection* callbackCritSect) {
1110 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1111 "Channel::SetEngineInformation()");
1112 _engineStatisticsPtr = &engineStatistics;
1113 _outputMixerPtr = &outputMixer;
1114 _transmitMixerPtr = &transmitMixer,
1115 _moduleProcessThreadPtr = &moduleProcessThread;
1116 _audioDeviceModulePtr = &audioDeviceModule;
1117 _voiceEngineObserverPtr = voiceEngineObserver;
1118 _callbackCritSectPtr = callbackCritSect;
1119 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001120}
1121
kwiberg55b97fe2016-01-28 05:22:45 -08001122int32_t Channel::UpdateLocalTimeStamp() {
1123 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1124 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001125}
1126
kwibergb7f89d62016-02-17 10:04:18 -08001127void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001128 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001129 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001130}
1131
ossu29b1a8d2016-06-13 07:34:51 -07001132const rtc::scoped_refptr<AudioDecoderFactory>&
1133Channel::GetAudioDecoderFactory() const {
1134 return decoder_factory_;
1135}
1136
kwiberg55b97fe2016-01-28 05:22:45 -08001137int32_t Channel::StartPlayout() {
1138 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1139 "Channel::StartPlayout()");
1140 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001141 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001142 }
1143
1144 if (!_externalMixing) {
1145 // Add participant as candidates for mixing.
1146 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1147 _engineStatisticsPtr->SetLastError(
1148 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1149 "StartPlayout() failed to add participant to mixer");
1150 return -1;
1151 }
1152 }
1153
1154 channel_state_.SetPlaying(true);
1155 if (RegisterFilePlayingToMixer() != 0)
1156 return -1;
1157
1158 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001159}
1160
kwiberg55b97fe2016-01-28 05:22:45 -08001161int32_t Channel::StopPlayout() {
1162 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1163 "Channel::StopPlayout()");
1164 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001166 }
1167
1168 if (!_externalMixing) {
1169 // Remove participant as candidates for mixing
1170 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1171 _engineStatisticsPtr->SetLastError(
1172 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1173 "StopPlayout() failed to remove participant from mixer");
1174 return -1;
1175 }
1176 }
1177
1178 channel_state_.SetPlaying(false);
1179 _outputAudioLevel.Clear();
1180
1181 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001182}
1183
kwiberg55b97fe2016-01-28 05:22:45 -08001184int32_t Channel::StartSend() {
1185 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1186 "Channel::StartSend()");
1187 // Resume the previous sequence number which was reset by StopSend().
1188 // This needs to be done before |sending| is set to true.
1189 if (send_sequence_number_)
1190 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001191
kwiberg55b97fe2016-01-28 05:22:45 -08001192 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001193 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001194 }
1195 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001196
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001197 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001198 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1199 _engineStatisticsPtr->SetLastError(
1200 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1201 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001202 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001203 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001204 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001205 return -1;
1206 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001207
kwiberg55b97fe2016-01-28 05:22:45 -08001208 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001209}
1210
kwiberg55b97fe2016-01-28 05:22:45 -08001211int32_t Channel::StopSend() {
1212 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1213 "Channel::StopSend()");
1214 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001215 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001216 }
1217 channel_state_.SetSending(false);
1218
1219 // Store the sequence number to be able to pick up the same sequence for
1220 // the next StartSend(). This is needed for restarting device, otherwise
1221 // it might cause libSRTP to complain about packets being replayed.
1222 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1223 // CL is landed. See issue
1224 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1225 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1226
1227 // Reset sending SSRC and sequence number and triggers direct transmission
1228 // of RTCP BYE
1229 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1230 _engineStatisticsPtr->SetLastError(
1231 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1232 "StartSend() RTP/RTCP failed to stop sending");
1233 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001234 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001235
1236 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001237}
1238
kwiberg55b97fe2016-01-28 05:22:45 -08001239int32_t Channel::StartReceiving() {
1240 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1241 "Channel::StartReceiving()");
1242 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001243 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001244 }
1245 channel_state_.SetReceiving(true);
1246 _numberOfDiscardedPackets = 0;
1247 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001248}
1249
kwiberg55b97fe2016-01-28 05:22:45 -08001250int32_t Channel::StopReceiving() {
1251 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1252 "Channel::StopReceiving()");
1253 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001254 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001255 }
1256
1257 channel_state_.SetReceiving(false);
1258 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001259}
1260
kwiberg55b97fe2016-01-28 05:22:45 -08001261int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1262 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1263 "Channel::RegisterVoiceEngineObserver()");
1264 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001265
kwiberg55b97fe2016-01-28 05:22:45 -08001266 if (_voiceEngineObserverPtr) {
1267 _engineStatisticsPtr->SetLastError(
1268 VE_INVALID_OPERATION, kTraceError,
1269 "RegisterVoiceEngineObserver() observer already enabled");
1270 return -1;
1271 }
1272 _voiceEngineObserverPtr = &observer;
1273 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001274}
1275
kwiberg55b97fe2016-01-28 05:22:45 -08001276int32_t Channel::DeRegisterVoiceEngineObserver() {
1277 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1278 "Channel::DeRegisterVoiceEngineObserver()");
1279 rtc::CritScope cs(&_callbackCritSect);
1280
1281 if (!_voiceEngineObserverPtr) {
1282 _engineStatisticsPtr->SetLastError(
1283 VE_INVALID_OPERATION, kTraceWarning,
1284 "DeRegisterVoiceEngineObserver() observer already disabled");
1285 return 0;
1286 }
1287 _voiceEngineObserverPtr = NULL;
1288 return 0;
1289}
1290
1291int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001292 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001293 if (send_codec) {
1294 codec = *send_codec;
1295 return 0;
1296 }
1297 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001298}
1299
kwiberg55b97fe2016-01-28 05:22:45 -08001300int32_t Channel::GetRecCodec(CodecInst& codec) {
1301 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001302}
1303
kwiberg55b97fe2016-01-28 05:22:45 -08001304int32_t Channel::SetSendCodec(const CodecInst& codec) {
1305 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1306 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001307
kwibergc8d071e2016-04-06 12:22:38 -07001308 if (!codec_manager_.RegisterEncoder(codec) ||
1309 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001310 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1311 "SetSendCodec() failed to register codec to ACM");
1312 return -1;
1313 }
1314
1315 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1316 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1317 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1318 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1319 "SetSendCodec() failed to register codec to"
1320 " RTP/RTCP module");
1321 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001322 }
kwiberg55b97fe2016-01-28 05:22:45 -08001323 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001324
kwiberg55b97fe2016-01-28 05:22:45 -08001325 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1326 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1327 "SetSendCodec() failed to set audio packet size");
1328 return -1;
1329 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001330
kwiberg55b97fe2016-01-28 05:22:45 -08001331 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001332}
1333
Ivo Creusenadf89b72015-04-29 16:03:33 +02001334void Channel::SetBitRate(int bitrate_bps) {
1335 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1336 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1337 audio_coding_->SetBitRate(bitrate_bps);
Erik Språng737336d2016-07-29 12:59:36 +02001338 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001339}
1340
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001341void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001342 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001343 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1344
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001345 // Normalizes rate to 0 - 100.
kwiberg55b97fe2016-01-28 05:22:45 -08001346 if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) !=
1347 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001348 assert(false); // This should not happen.
1349 }
1350}
1351
kwiberg55b97fe2016-01-28 05:22:45 -08001352int32_t Channel::SetVADStatus(bool enableVAD,
1353 ACMVADMode mode,
1354 bool disableDTX) {
1355 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1356 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001357 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1358 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1359 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001360 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1361 kTraceError,
1362 "SetVADStatus() failed to set VAD");
1363 return -1;
1364 }
1365 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001366}
1367
kwiberg55b97fe2016-01-28 05:22:45 -08001368int32_t Channel::GetVADStatus(bool& enabledVAD,
1369 ACMVADMode& mode,
1370 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001371 const auto* params = codec_manager_.GetStackParams();
1372 enabledVAD = params->use_cng;
1373 mode = params->vad_mode;
1374 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001375 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001376}
1377
kwiberg55b97fe2016-01-28 05:22:45 -08001378int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1379 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1380 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001381
kwiberg55b97fe2016-01-28 05:22:45 -08001382 if (channel_state_.Get().playing) {
1383 _engineStatisticsPtr->SetLastError(
1384 VE_ALREADY_PLAYING, kTraceError,
1385 "SetRecPayloadType() unable to set PT while playing");
1386 return -1;
1387 }
1388 if (channel_state_.Get().receiving) {
1389 _engineStatisticsPtr->SetLastError(
1390 VE_ALREADY_LISTENING, kTraceError,
1391 "SetRecPayloadType() unable to set PT while listening");
1392 return -1;
1393 }
1394
1395 if (codec.pltype == -1) {
1396 // De-register the selected codec (RTP/RTCP module and ACM)
1397
1398 int8_t pltype(-1);
1399 CodecInst rxCodec = codec;
1400
1401 // Get payload type for the given codec
1402 rtp_payload_registry_->ReceivePayloadType(
1403 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1404 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1405 rxCodec.pltype = pltype;
1406
1407 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1408 _engineStatisticsPtr->SetLastError(
1409 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1410 "SetRecPayloadType() RTP/RTCP-module deregistration "
1411 "failed");
1412 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001413 }
kwiberg55b97fe2016-01-28 05:22:45 -08001414 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1415 _engineStatisticsPtr->SetLastError(
1416 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1417 "SetRecPayloadType() ACM deregistration failed - 1");
1418 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001419 }
kwiberg55b97fe2016-01-28 05:22:45 -08001420 return 0;
1421 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001422
kwiberg55b97fe2016-01-28 05:22:45 -08001423 if (rtp_receiver_->RegisterReceivePayload(
1424 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1425 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1426 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001427 // TODO(kwiberg): Retrying is probably not necessary, since
1428 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001429 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001430 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001431 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1432 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1433 _engineStatisticsPtr->SetLastError(
1434 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1435 "SetRecPayloadType() RTP/RTCP-module registration failed");
1436 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001437 }
kwiberg55b97fe2016-01-28 05:22:45 -08001438 }
kwibergc8d071e2016-04-06 12:22:38 -07001439 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001440 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001441 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001442 _engineStatisticsPtr->SetLastError(
1443 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1444 "SetRecPayloadType() ACM registration failed - 1");
1445 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001446 }
kwiberg55b97fe2016-01-28 05:22:45 -08001447 }
1448 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001449}
1450
kwiberg55b97fe2016-01-28 05:22:45 -08001451int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1452 int8_t payloadType(-1);
1453 if (rtp_payload_registry_->ReceivePayloadType(
1454 codec.plname, codec.plfreq, codec.channels,
1455 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1456 _engineStatisticsPtr->SetLastError(
1457 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1458 "GetRecPayloadType() failed to retrieve RX payload type");
1459 return -1;
1460 }
1461 codec.pltype = payloadType;
1462 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001463}
1464
kwiberg55b97fe2016-01-28 05:22:45 -08001465int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1466 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1467 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001468
kwiberg55b97fe2016-01-28 05:22:45 -08001469 CodecInst codec;
1470 int32_t samplingFreqHz(-1);
1471 const size_t kMono = 1;
1472 if (frequency == kFreq32000Hz)
1473 samplingFreqHz = 32000;
1474 else if (frequency == kFreq16000Hz)
1475 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001476
kwiberg55b97fe2016-01-28 05:22:45 -08001477 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1478 _engineStatisticsPtr->SetLastError(
1479 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1480 "SetSendCNPayloadType() failed to retrieve default CN codec "
1481 "settings");
1482 return -1;
1483 }
1484
1485 // Modify the payload type (must be set to dynamic range)
1486 codec.pltype = type;
1487
kwibergc8d071e2016-04-06 12:22:38 -07001488 if (!codec_manager_.RegisterEncoder(codec) ||
1489 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001490 _engineStatisticsPtr->SetLastError(
1491 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1492 "SetSendCNPayloadType() failed to register CN to ACM");
1493 return -1;
1494 }
1495
1496 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1497 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1498 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1499 _engineStatisticsPtr->SetLastError(
1500 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1501 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1502 "module");
1503 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001504 }
kwiberg55b97fe2016-01-28 05:22:45 -08001505 }
1506 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001507}
1508
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001509int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001510 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001511 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001512
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001513 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001514 _engineStatisticsPtr->SetLastError(
1515 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001516 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001517 return -1;
1518 }
1519 return 0;
1520}
1521
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001522int Channel::SetOpusDtx(bool enable_dtx) {
1523 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1524 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001525 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001526 : audio_coding_->DisableOpusDtx();
1527 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001528 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1529 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001530 return -1;
1531 }
1532 return 0;
1533}
1534
ivoc85228d62016-07-27 04:53:47 -07001535int Channel::GetOpusDtx(bool* enabled) {
1536 int success = -1;
1537 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1538 if (encoder) {
1539 *enabled = encoder->GetDtx();
1540 success = 0;
1541 }
1542 });
1543 return success;
1544}
1545
mflodman3d7db262016-04-29 00:57:13 -07001546int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001547 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001548 "Channel::RegisterExternalTransport()");
1549
kwiberg55b97fe2016-01-28 05:22:45 -08001550 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001551 if (_externalTransport) {
1552 _engineStatisticsPtr->SetLastError(
1553 VE_INVALID_OPERATION, kTraceError,
1554 "RegisterExternalTransport() external transport already enabled");
1555 return -1;
1556 }
1557 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001558 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001559 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001560}
1561
kwiberg55b97fe2016-01-28 05:22:45 -08001562int32_t Channel::DeRegisterExternalTransport() {
1563 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1564 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001565
kwiberg55b97fe2016-01-28 05:22:45 -08001566 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001567 if (_transportPtr) {
1568 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1569 "DeRegisterExternalTransport() all transport is disabled");
1570 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001571 _engineStatisticsPtr->SetLastError(
1572 VE_INVALID_OPERATION, kTraceWarning,
1573 "DeRegisterExternalTransport() external transport already "
1574 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001575 }
1576 _externalTransport = false;
1577 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001578 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001579}
1580
mflodman3d7db262016-04-29 00:57:13 -07001581int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001582 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001583 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001584 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001585 "Channel::ReceivedRTPPacket()");
1586
1587 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001588 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001589
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001590 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001591 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1592 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1593 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001594 return -1;
1595 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001596 header.payload_type_frequency =
1597 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001598 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001599 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001600 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001601 rtp_receive_statistics_->IncomingPacket(
1602 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001603 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001604
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001605 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001606}
1607
1608bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001609 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 const RTPHeader& header,
1611 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001612 if (rtp_payload_registry_->IsRtx(header)) {
1613 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001614 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001615 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001616 assert(packet_length >= header.headerLength);
1617 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001618 PayloadUnion payload_specific;
1619 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001620 &payload_specific)) {
1621 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001622 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001623 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1624 payload_specific, in_order);
1625}
1626
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001627bool Channel::HandleRtxPacket(const uint8_t* packet,
1628 size_t packet_length,
1629 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001630 if (!rtp_payload_registry_->IsRtx(header))
1631 return false;
1632
1633 // Remove the RTX header and parse the original RTP header.
1634 if (packet_length < header.headerLength)
1635 return false;
1636 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1637 return false;
1638 if (restored_packet_in_use_) {
1639 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1640 "Multiple RTX headers detected, dropping packet");
1641 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001642 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001643 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001644 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1645 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001646 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1647 "Incoming RTX packet: invalid RTP header");
1648 return false;
1649 }
1650 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001651 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001652 restored_packet_in_use_ = false;
1653 return ret;
1654}
1655
1656bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1657 StreamStatistician* statistician =
1658 rtp_receive_statistics_->GetStatistician(header.ssrc);
1659 if (!statistician)
1660 return false;
1661 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001662}
1663
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001664bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1665 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001666 // Retransmissions are handled separately if RTX is enabled.
1667 if (rtp_payload_registry_->RtxEnabled())
1668 return false;
1669 StreamStatistician* statistician =
1670 rtp_receive_statistics_->GetStatistician(header.ssrc);
1671 if (!statistician)
1672 return false;
1673 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001674 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001675 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001676 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001677}
1678
mflodman3d7db262016-04-29 00:57:13 -07001679int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001680 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001681 "Channel::ReceivedRTCPPacket()");
1682 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001683 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001684
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001685 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001686 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001687 _engineStatisticsPtr->SetLastError(
1688 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1689 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1690 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001691
Minyue2013aec2015-05-13 14:14:42 +02001692 int64_t rtt = GetRTT(true);
1693 if (rtt == 0) {
1694 // Waiting for valid RTT.
1695 return 0;
1696 }
Erik Språng737336d2016-07-29 12:59:36 +02001697
1698 int64_t nack_window_ms = rtt;
1699 if (nack_window_ms < kMinRetransmissionWindowMs) {
1700 nack_window_ms = kMinRetransmissionWindowMs;
1701 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1702 nack_window_ms = kMaxRetransmissionWindowMs;
1703 }
1704 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1705
Minyue2013aec2015-05-13 14:14:42 +02001706 uint32_t ntp_secs = 0;
1707 uint32_t ntp_frac = 0;
1708 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001709 if (0 !=
1710 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1711 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001712 // Waiting for RTCP.
1713 return 0;
1714 }
1715
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001716 {
tommi31fc21f2016-01-21 10:37:37 -08001717 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001718 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001719 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001720 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001721}
1722
niklase@google.com470e71d2011-07-07 08:21:25 +00001723int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001724 bool loop,
1725 FileFormats format,
1726 int startPosition,
1727 float volumeScaling,
1728 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001729 const CodecInst* codecInst) {
1730 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1731 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1732 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1733 "stopPosition=%d)",
1734 fileName, loop, format, volumeScaling, startPosition,
1735 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001736
kwiberg55b97fe2016-01-28 05:22:45 -08001737 if (channel_state_.Get().output_file_playing) {
1738 _engineStatisticsPtr->SetLastError(
1739 VE_ALREADY_PLAYING, kTraceError,
1740 "StartPlayingFileLocally() is already playing");
1741 return -1;
1742 }
1743
1744 {
1745 rtc::CritScope cs(&_fileCritSect);
1746
kwiberg5a25d952016-08-17 07:31:12 -07001747 if (output_file_player_) {
1748 output_file_player_->RegisterModuleFileCallback(NULL);
1749 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001750 }
1751
kwiberg5a25d952016-08-17 07:31:12 -07001752 output_file_player_ = FilePlayer::NewFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001753 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001754
kwiberg5a25d952016-08-17 07:31:12 -07001755 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001756 _engineStatisticsPtr->SetLastError(
1757 VE_INVALID_ARGUMENT, kTraceError,
1758 "StartPlayingFileLocally() filePlayer format is not correct");
1759 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001760 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001761
kwiberg55b97fe2016-01-28 05:22:45 -08001762 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001763
kwiberg5a25d952016-08-17 07:31:12 -07001764 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001765 fileName, loop, startPosition, volumeScaling, notificationTime,
1766 stopPosition, (const CodecInst*)codecInst) != 0) {
1767 _engineStatisticsPtr->SetLastError(
1768 VE_BAD_FILE, kTraceError,
1769 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001770 output_file_player_->StopPlayingFile();
1771 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001772 return -1;
1773 }
kwiberg5a25d952016-08-17 07:31:12 -07001774 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001775 channel_state_.SetOutputFilePlaying(true);
1776 }
1777
1778 if (RegisterFilePlayingToMixer() != 0)
1779 return -1;
1780
1781 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001782}
1783
1784int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001785 FileFormats format,
1786 int startPosition,
1787 float volumeScaling,
1788 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001789 const CodecInst* codecInst) {
1790 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1791 "Channel::StartPlayingFileLocally(format=%d,"
1792 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1793 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001794
kwiberg55b97fe2016-01-28 05:22:45 -08001795 if (stream == NULL) {
1796 _engineStatisticsPtr->SetLastError(
1797 VE_BAD_FILE, kTraceError,
1798 "StartPlayingFileLocally() NULL as input stream");
1799 return -1;
1800 }
1801
1802 if (channel_state_.Get().output_file_playing) {
1803 _engineStatisticsPtr->SetLastError(
1804 VE_ALREADY_PLAYING, kTraceError,
1805 "StartPlayingFileLocally() is already playing");
1806 return -1;
1807 }
1808
1809 {
1810 rtc::CritScope cs(&_fileCritSect);
1811
1812 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001813 if (output_file_player_) {
1814 output_file_player_->RegisterModuleFileCallback(NULL);
1815 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001816 }
1817
kwiberg55b97fe2016-01-28 05:22:45 -08001818 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07001819 output_file_player_ = FilePlayer::NewFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001820 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001821
kwiberg5a25d952016-08-17 07:31:12 -07001822 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001823 _engineStatisticsPtr->SetLastError(
1824 VE_INVALID_ARGUMENT, kTraceError,
1825 "StartPlayingFileLocally() filePlayer format isnot correct");
1826 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001827 }
1828
kwiberg55b97fe2016-01-28 05:22:45 -08001829 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001830
kwiberg4ec01d92016-08-22 08:43:54 -07001831 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001832 volumeScaling, notificationTime,
1833 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001834 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1835 "StartPlayingFile() failed to "
1836 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001837 output_file_player_->StopPlayingFile();
1838 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001839 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001840 }
kwiberg5a25d952016-08-17 07:31:12 -07001841 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001842 channel_state_.SetOutputFilePlaying(true);
1843 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001844
kwiberg55b97fe2016-01-28 05:22:45 -08001845 if (RegisterFilePlayingToMixer() != 0)
1846 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001847
kwiberg55b97fe2016-01-28 05:22:45 -08001848 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001849}
1850
kwiberg55b97fe2016-01-28 05:22:45 -08001851int Channel::StopPlayingFileLocally() {
1852 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1853 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001854
kwiberg55b97fe2016-01-28 05:22:45 -08001855 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001856 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001857 }
1858
1859 {
1860 rtc::CritScope cs(&_fileCritSect);
1861
kwiberg5a25d952016-08-17 07:31:12 -07001862 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001863 _engineStatisticsPtr->SetLastError(
1864 VE_STOP_RECORDING_FAILED, kTraceError,
1865 "StopPlayingFile() could not stop playing");
1866 return -1;
1867 }
kwiberg5a25d952016-08-17 07:31:12 -07001868 output_file_player_->RegisterModuleFileCallback(NULL);
1869 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001870 channel_state_.SetOutputFilePlaying(false);
1871 }
1872 // _fileCritSect cannot be taken while calling
1873 // SetAnonymousMixibilityStatus. Refer to comments in
1874 // StartPlayingFileLocally(const char* ...) for more details.
1875 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1876 _engineStatisticsPtr->SetLastError(
1877 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1878 "StopPlayingFile() failed to stop participant from playing as"
1879 "file in the mixer");
1880 return -1;
1881 }
1882
1883 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001884}
1885
kwiberg55b97fe2016-01-28 05:22:45 -08001886int Channel::IsPlayingFileLocally() const {
1887 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001888}
1889
kwiberg55b97fe2016-01-28 05:22:45 -08001890int Channel::RegisterFilePlayingToMixer() {
1891 // Return success for not registering for file playing to mixer if:
1892 // 1. playing file before playout is started on that channel.
1893 // 2. starting playout without file playing on that channel.
1894 if (!channel_state_.Get().playing ||
1895 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001896 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001897 }
1898
1899 // |_fileCritSect| cannot be taken while calling
1900 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1901 // frames can be pulled by the mixer. Since the frames are generated from
1902 // the file, _fileCritSect will be taken. This would result in a deadlock.
1903 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1904 channel_state_.SetOutputFilePlaying(false);
1905 rtc::CritScope cs(&_fileCritSect);
1906 _engineStatisticsPtr->SetLastError(
1907 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1908 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001909 output_file_player_->StopPlayingFile();
1910 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001911 return -1;
1912 }
1913
1914 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001915}
1916
niklase@google.com470e71d2011-07-07 08:21:25 +00001917int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001918 bool loop,
1919 FileFormats format,
1920 int startPosition,
1921 float volumeScaling,
1922 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001923 const CodecInst* codecInst) {
1924 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1925 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1926 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1927 "stopPosition=%d)",
1928 fileName, loop, format, volumeScaling, startPosition,
1929 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001930
kwiberg55b97fe2016-01-28 05:22:45 -08001931 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001932
kwiberg55b97fe2016-01-28 05:22:45 -08001933 if (channel_state_.Get().input_file_playing) {
1934 _engineStatisticsPtr->SetLastError(
1935 VE_ALREADY_PLAYING, kTraceWarning,
1936 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001937 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001938 }
1939
1940 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001941 if (input_file_player_) {
1942 input_file_player_->RegisterModuleFileCallback(NULL);
1943 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001944 }
1945
1946 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07001947 input_file_player_ = FilePlayer::NewFilePlayer(_inputFilePlayerId,
1948 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001949
kwiberg5a25d952016-08-17 07:31:12 -07001950 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001951 _engineStatisticsPtr->SetLastError(
1952 VE_INVALID_ARGUMENT, kTraceError,
1953 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1954 return -1;
1955 }
1956
1957 const uint32_t notificationTime(0);
1958
kwiberg5a25d952016-08-17 07:31:12 -07001959 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001960 fileName, loop, startPosition, volumeScaling, notificationTime,
1961 stopPosition, (const CodecInst*)codecInst) != 0) {
1962 _engineStatisticsPtr->SetLastError(
1963 VE_BAD_FILE, kTraceError,
1964 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001965 input_file_player_->StopPlayingFile();
1966 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001967 return -1;
1968 }
kwiberg5a25d952016-08-17 07:31:12 -07001969 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001970 channel_state_.SetInputFilePlaying(true);
1971
1972 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001973}
1974
1975int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001976 FileFormats format,
1977 int startPosition,
1978 float volumeScaling,
1979 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001980 const CodecInst* codecInst) {
1981 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1982 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1983 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1984 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001985
kwiberg55b97fe2016-01-28 05:22:45 -08001986 if (stream == NULL) {
1987 _engineStatisticsPtr->SetLastError(
1988 VE_BAD_FILE, kTraceError,
1989 "StartPlayingFileAsMicrophone NULL as input stream");
1990 return -1;
1991 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001992
kwiberg55b97fe2016-01-28 05:22:45 -08001993 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001994
kwiberg55b97fe2016-01-28 05:22:45 -08001995 if (channel_state_.Get().input_file_playing) {
1996 _engineStatisticsPtr->SetLastError(
1997 VE_ALREADY_PLAYING, kTraceWarning,
1998 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001999 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002000 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002001
kwiberg55b97fe2016-01-28 05:22:45 -08002002 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002003 if (input_file_player_) {
2004 input_file_player_->RegisterModuleFileCallback(NULL);
2005 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002006 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002007
kwiberg55b97fe2016-01-28 05:22:45 -08002008 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07002009 input_file_player_ = FilePlayer::NewFilePlayer(_inputFilePlayerId,
2010 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002011
kwiberg5a25d952016-08-17 07:31:12 -07002012 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002013 _engineStatisticsPtr->SetLastError(
2014 VE_INVALID_ARGUMENT, kTraceError,
2015 "StartPlayingInputFile() filePlayer format isnot correct");
2016 return -1;
2017 }
2018
2019 const uint32_t notificationTime(0);
2020
kwiberg4ec01d92016-08-22 08:43:54 -07002021 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2022 notificationTime, stopPosition,
2023 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002024 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2025 "StartPlayingFile() failed to start "
2026 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002027 input_file_player_->StopPlayingFile();
2028 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002029 return -1;
2030 }
2031
kwiberg5a25d952016-08-17 07:31:12 -07002032 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002033 channel_state_.SetInputFilePlaying(true);
2034
2035 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002036}
2037
kwiberg55b97fe2016-01-28 05:22:45 -08002038int Channel::StopPlayingFileAsMicrophone() {
2039 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2040 "Channel::StopPlayingFileAsMicrophone()");
2041
2042 rtc::CritScope cs(&_fileCritSect);
2043
2044 if (!channel_state_.Get().input_file_playing) {
2045 return 0;
2046 }
2047
kwiberg5a25d952016-08-17 07:31:12 -07002048 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002049 _engineStatisticsPtr->SetLastError(
2050 VE_STOP_RECORDING_FAILED, kTraceError,
2051 "StopPlayingFile() could not stop playing");
2052 return -1;
2053 }
kwiberg5a25d952016-08-17 07:31:12 -07002054 input_file_player_->RegisterModuleFileCallback(NULL);
2055 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002056 channel_state_.SetInputFilePlaying(false);
2057
2058 return 0;
2059}
2060
2061int Channel::IsPlayingFileAsMicrophone() const {
2062 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002063}
2064
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002065int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002066 const CodecInst* codecInst) {
2067 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2068 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002069
kwiberg55b97fe2016-01-28 05:22:45 -08002070 if (_outputFileRecording) {
2071 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2072 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002073 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002074 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002075
kwiberg55b97fe2016-01-28 05:22:45 -08002076 FileFormats format;
2077 const uint32_t notificationTime(0); // Not supported in VoE
2078 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002079
kwiberg55b97fe2016-01-28 05:22:45 -08002080 if ((codecInst != NULL) &&
2081 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2082 _engineStatisticsPtr->SetLastError(
2083 VE_BAD_ARGUMENT, kTraceError,
2084 "StartRecordingPlayout() invalid compression");
2085 return (-1);
2086 }
2087 if (codecInst == NULL) {
2088 format = kFileFormatPcm16kHzFile;
2089 codecInst = &dummyCodec;
2090 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2091 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2092 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2093 format = kFileFormatWavFile;
2094 } else {
2095 format = kFileFormatCompressedFile;
2096 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002097
kwiberg55b97fe2016-01-28 05:22:45 -08002098 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002099
kwiberg55b97fe2016-01-28 05:22:45 -08002100 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002101 if (output_file_recorder_) {
2102 output_file_recorder_->RegisterModuleFileCallback(NULL);
2103 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002104 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002105
kwiberg5a25d952016-08-17 07:31:12 -07002106 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002107 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002108 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002109 _engineStatisticsPtr->SetLastError(
2110 VE_INVALID_ARGUMENT, kTraceError,
2111 "StartRecordingPlayout() fileRecorder format isnot correct");
2112 return -1;
2113 }
2114
kwiberg5a25d952016-08-17 07:31:12 -07002115 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002116 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2117 _engineStatisticsPtr->SetLastError(
2118 VE_BAD_FILE, kTraceError,
2119 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002120 output_file_recorder_->StopRecording();
2121 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002122 return -1;
2123 }
kwiberg5a25d952016-08-17 07:31:12 -07002124 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002125 _outputFileRecording = true;
2126
2127 return 0;
2128}
2129
2130int Channel::StartRecordingPlayout(OutStream* stream,
2131 const CodecInst* codecInst) {
2132 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2133 "Channel::StartRecordingPlayout()");
2134
2135 if (_outputFileRecording) {
2136 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2137 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002138 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002139 }
2140
2141 FileFormats format;
2142 const uint32_t notificationTime(0); // Not supported in VoE
2143 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2144
2145 if (codecInst != NULL && codecInst->channels != 1) {
2146 _engineStatisticsPtr->SetLastError(
2147 VE_BAD_ARGUMENT, kTraceError,
2148 "StartRecordingPlayout() invalid compression");
2149 return (-1);
2150 }
2151 if (codecInst == NULL) {
2152 format = kFileFormatPcm16kHzFile;
2153 codecInst = &dummyCodec;
2154 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2155 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2156 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2157 format = kFileFormatWavFile;
2158 } else {
2159 format = kFileFormatCompressedFile;
2160 }
2161
2162 rtc::CritScope cs(&_fileCritSect);
2163
2164 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002165 if (output_file_recorder_) {
2166 output_file_recorder_->RegisterModuleFileCallback(NULL);
2167 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002168 }
2169
kwiberg5a25d952016-08-17 07:31:12 -07002170 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002171 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002172 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002173 _engineStatisticsPtr->SetLastError(
2174 VE_INVALID_ARGUMENT, kTraceError,
2175 "StartRecordingPlayout() fileRecorder format isnot correct");
2176 return -1;
2177 }
2178
kwiberg4ec01d92016-08-22 08:43:54 -07002179 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002180 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002181 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2182 "StartRecordingPlayout() failed to "
2183 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002184 output_file_recorder_->StopRecording();
2185 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002186 return -1;
2187 }
2188
kwiberg5a25d952016-08-17 07:31:12 -07002189 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002190 _outputFileRecording = true;
2191
2192 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002193}
2194
kwiberg55b97fe2016-01-28 05:22:45 -08002195int Channel::StopRecordingPlayout() {
2196 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2197 "Channel::StopRecordingPlayout()");
2198
2199 if (!_outputFileRecording) {
2200 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2201 "StopRecordingPlayout() isnot recording");
2202 return -1;
2203 }
2204
2205 rtc::CritScope cs(&_fileCritSect);
2206
kwiberg5a25d952016-08-17 07:31:12 -07002207 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002208 _engineStatisticsPtr->SetLastError(
2209 VE_STOP_RECORDING_FAILED, kTraceError,
2210 "StopRecording() could not stop recording");
2211 return (-1);
2212 }
kwiberg5a25d952016-08-17 07:31:12 -07002213 output_file_recorder_->RegisterModuleFileCallback(NULL);
2214 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002215 _outputFileRecording = false;
2216
2217 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002218}
2219
kwiberg55b97fe2016-01-28 05:22:45 -08002220void Channel::SetMixWithMicStatus(bool mix) {
2221 rtc::CritScope cs(&_fileCritSect);
2222 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002223}
2224
kwiberg55b97fe2016-01-28 05:22:45 -08002225int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2226 int8_t currentLevel = _outputAudioLevel.Level();
2227 level = static_cast<int32_t>(currentLevel);
2228 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002229}
2230
kwiberg55b97fe2016-01-28 05:22:45 -08002231int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2232 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2233 level = static_cast<int32_t>(currentLevel);
2234 return 0;
2235}
2236
solenberg1c2af8e2016-03-24 10:36:00 -07002237int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002238 rtc::CritScope cs(&volume_settings_critsect_);
2239 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002240 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002241 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002242 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002243}
2244
solenberg1c2af8e2016-03-24 10:36:00 -07002245bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002246 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002247 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002248}
2249
kwiberg55b97fe2016-01-28 05:22:45 -08002250int Channel::SetOutputVolumePan(float left, float right) {
2251 rtc::CritScope cs(&volume_settings_critsect_);
2252 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002253 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002254 _panLeft = left;
2255 _panRight = right;
2256 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002257}
2258
kwiberg55b97fe2016-01-28 05:22:45 -08002259int Channel::GetOutputVolumePan(float& left, float& right) const {
2260 rtc::CritScope cs(&volume_settings_critsect_);
2261 left = _panLeft;
2262 right = _panRight;
2263 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002264}
2265
kwiberg55b97fe2016-01-28 05:22:45 -08002266int Channel::SetChannelOutputVolumeScaling(float scaling) {
2267 rtc::CritScope cs(&volume_settings_critsect_);
2268 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002269 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002270 _outputGain = scaling;
2271 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002272}
2273
kwiberg55b97fe2016-01-28 05:22:45 -08002274int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2275 rtc::CritScope cs(&volume_settings_critsect_);
2276 scaling = _outputGain;
2277 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002278}
2279
solenberg8842c3e2016-03-11 03:06:41 -08002280int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002281 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002282 "Channel::SendTelephoneEventOutband(...)");
2283 RTC_DCHECK_LE(0, event);
2284 RTC_DCHECK_GE(255, event);
2285 RTC_DCHECK_LE(0, duration_ms);
2286 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002287 if (!Sending()) {
2288 return -1;
2289 }
solenberg8842c3e2016-03-11 03:06:41 -08002290 if (_rtpRtcpModule->SendTelephoneEventOutband(
2291 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002292 _engineStatisticsPtr->SetLastError(
2293 VE_SEND_DTMF_FAILED, kTraceWarning,
2294 "SendTelephoneEventOutband() failed to send event");
2295 return -1;
2296 }
2297 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002298}
2299
solenberg31642aa2016-03-14 08:00:37 -07002300int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002301 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002302 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002303 RTC_DCHECK_LE(0, payload_type);
2304 RTC_DCHECK_GE(127, payload_type);
2305 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002306 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002307 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002308 memcpy(codec.plname, "telephone-event", 16);
2309 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2310 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2311 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2312 _engineStatisticsPtr->SetLastError(
2313 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2314 "SetSendTelephoneEventPayloadType() failed to register send"
2315 "payload type");
2316 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002317 }
kwiberg55b97fe2016-01-28 05:22:45 -08002318 }
kwiberg55b97fe2016-01-28 05:22:45 -08002319 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002320}
2321
kwiberg55b97fe2016-01-28 05:22:45 -08002322int Channel::UpdateRxVadDetection(AudioFrame& audioFrame) {
2323 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2324 "Channel::UpdateRxVadDetection()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002325
kwiberg55b97fe2016-01-28 05:22:45 -08002326 int vadDecision = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002327
kwiberg55b97fe2016-01-28 05:22:45 -08002328 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive) ? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002329
kwiberg55b97fe2016-01-28 05:22:45 -08002330 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr) {
2331 OnRxVadDetected(vadDecision);
2332 _oldVadDecision = vadDecision;
2333 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002334
kwiberg55b97fe2016-01-28 05:22:45 -08002335 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2336 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2337 vadDecision);
2338 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002339}
2340
kwiberg55b97fe2016-01-28 05:22:45 -08002341int Channel::RegisterRxVadObserver(VoERxVadCallback& observer) {
2342 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2343 "Channel::RegisterRxVadObserver()");
2344 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002345
kwiberg55b97fe2016-01-28 05:22:45 -08002346 if (_rxVadObserverPtr) {
2347 _engineStatisticsPtr->SetLastError(
2348 VE_INVALID_OPERATION, kTraceError,
2349 "RegisterRxVadObserver() observer already enabled");
2350 return -1;
2351 }
2352 _rxVadObserverPtr = &observer;
2353 _RxVadDetection = true;
2354 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002355}
2356
kwiberg55b97fe2016-01-28 05:22:45 -08002357int Channel::DeRegisterRxVadObserver() {
2358 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2359 "Channel::DeRegisterRxVadObserver()");
2360 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002361
kwiberg55b97fe2016-01-28 05:22:45 -08002362 if (!_rxVadObserverPtr) {
2363 _engineStatisticsPtr->SetLastError(
2364 VE_INVALID_OPERATION, kTraceWarning,
2365 "DeRegisterRxVadObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00002366 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002367 }
2368 _rxVadObserverPtr = NULL;
2369 _RxVadDetection = false;
2370 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002371}
2372
kwiberg55b97fe2016-01-28 05:22:45 -08002373int Channel::VoiceActivityIndicator(int& activity) {
2374 activity = _sendFrameType;
2375 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002376}
2377
2378#ifdef WEBRTC_VOICE_ENGINE_AGC
2379
kwiberg55b97fe2016-01-28 05:22:45 -08002380int Channel::SetRxAgcStatus(bool enable, AgcModes mode) {
2381 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2382 "Channel::SetRxAgcStatus(enable=%d, mode=%d)", (int)enable,
2383 (int)mode);
niklase@google.com470e71d2011-07-07 08:21:25 +00002384
kwiberg55b97fe2016-01-28 05:22:45 -08002385 GainControl::Mode agcMode = kDefaultRxAgcMode;
2386 switch (mode) {
2387 case kAgcDefault:
2388 break;
2389 case kAgcUnchanged:
2390 agcMode = rx_audioproc_->gain_control()->mode();
2391 break;
2392 case kAgcFixedDigital:
2393 agcMode = GainControl::kFixedDigital;
2394 break;
2395 case kAgcAdaptiveDigital:
2396 agcMode = GainControl::kAdaptiveDigital;
2397 break;
2398 default:
2399 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
2400 "SetRxAgcStatus() invalid Agc mode");
2401 return -1;
2402 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002403
kwiberg55b97fe2016-01-28 05:22:45 -08002404 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0) {
2405 _engineStatisticsPtr->SetLastError(
2406 VE_APM_ERROR, kTraceError, "SetRxAgcStatus() failed to set Agc mode");
2407 return -1;
2408 }
2409 if (rx_audioproc_->gain_control()->Enable(enable) != 0) {
2410 _engineStatisticsPtr->SetLastError(
2411 VE_APM_ERROR, kTraceError, "SetRxAgcStatus() failed to set Agc state");
2412 return -1;
2413 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002414
kwiberg55b97fe2016-01-28 05:22:45 -08002415 _rxAgcIsEnabled = enable;
2416 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002417
kwiberg55b97fe2016-01-28 05:22:45 -08002418 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002419}
2420
kwiberg55b97fe2016-01-28 05:22:45 -08002421int Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode) {
2422 bool enable = rx_audioproc_->gain_control()->is_enabled();
2423 GainControl::Mode agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002424
kwiberg55b97fe2016-01-28 05:22:45 -08002425 enabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002426
kwiberg55b97fe2016-01-28 05:22:45 -08002427 switch (agcMode) {
2428 case GainControl::kFixedDigital:
2429 mode = kAgcFixedDigital;
2430 break;
2431 case GainControl::kAdaptiveDigital:
2432 mode = kAgcAdaptiveDigital;
2433 break;
2434 default:
2435 _engineStatisticsPtr->SetLastError(VE_APM_ERROR, kTraceError,
2436 "GetRxAgcStatus() invalid Agc mode");
2437 return -1;
2438 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002439
kwiberg55b97fe2016-01-28 05:22:45 -08002440 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002441}
2442
kwiberg55b97fe2016-01-28 05:22:45 -08002443int Channel::SetRxAgcConfig(AgcConfig config) {
2444 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2445 "Channel::SetRxAgcConfig()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002446
kwiberg55b97fe2016-01-28 05:22:45 -08002447 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
2448 config.targetLeveldBOv) != 0) {
2449 _engineStatisticsPtr->SetLastError(
2450 VE_APM_ERROR, kTraceError,
2451 "SetRxAgcConfig() failed to set target peak |level|"
2452 "(or envelope) of the Agc");
2453 return -1;
2454 }
2455 if (rx_audioproc_->gain_control()->set_compression_gain_db(
2456 config.digitalCompressionGaindB) != 0) {
2457 _engineStatisticsPtr->SetLastError(
2458 VE_APM_ERROR, kTraceError,
2459 "SetRxAgcConfig() failed to set the range in |gain| the"
2460 " digital compression stage may apply");
2461 return -1;
2462 }
2463 if (rx_audioproc_->gain_control()->enable_limiter(config.limiterEnable) !=
2464 0) {
2465 _engineStatisticsPtr->SetLastError(
2466 VE_APM_ERROR, kTraceError,
2467 "SetRxAgcConfig() failed to set hard limiter to the signal");
2468 return -1;
2469 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002470
kwiberg55b97fe2016-01-28 05:22:45 -08002471 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002472}
2473
kwiberg55b97fe2016-01-28 05:22:45 -08002474int Channel::GetRxAgcConfig(AgcConfig& config) {
2475 config.targetLeveldBOv = rx_audioproc_->gain_control()->target_level_dbfs();
2476 config.digitalCompressionGaindB =
2477 rx_audioproc_->gain_control()->compression_gain_db();
2478 config.limiterEnable = rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002479
kwiberg55b97fe2016-01-28 05:22:45 -08002480 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002481}
2482
kwiberg55b97fe2016-01-28 05:22:45 -08002483#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
niklase@google.com470e71d2011-07-07 08:21:25 +00002484
2485#ifdef WEBRTC_VOICE_ENGINE_NR
2486
kwiberg55b97fe2016-01-28 05:22:45 -08002487int Channel::SetRxNsStatus(bool enable, NsModes mode) {
2488 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2489 "Channel::SetRxNsStatus(enable=%d, mode=%d)", (int)enable,
2490 (int)mode);
niklase@google.com470e71d2011-07-07 08:21:25 +00002491
kwiberg55b97fe2016-01-28 05:22:45 -08002492 NoiseSuppression::Level nsLevel = kDefaultNsMode;
2493 switch (mode) {
2494 case kNsDefault:
2495 break;
2496 case kNsUnchanged:
2497 nsLevel = rx_audioproc_->noise_suppression()->level();
2498 break;
2499 case kNsConference:
2500 nsLevel = NoiseSuppression::kHigh;
2501 break;
2502 case kNsLowSuppression:
2503 nsLevel = NoiseSuppression::kLow;
2504 break;
2505 case kNsModerateSuppression:
2506 nsLevel = NoiseSuppression::kModerate;
2507 break;
2508 case kNsHighSuppression:
2509 nsLevel = NoiseSuppression::kHigh;
2510 break;
2511 case kNsVeryHighSuppression:
2512 nsLevel = NoiseSuppression::kVeryHigh;
2513 break;
2514 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002515
kwiberg55b97fe2016-01-28 05:22:45 -08002516 if (rx_audioproc_->noise_suppression()->set_level(nsLevel) != 0) {
2517 _engineStatisticsPtr->SetLastError(
2518 VE_APM_ERROR, kTraceError, "SetRxNsStatus() failed to set NS level");
2519 return -1;
2520 }
2521 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0) {
2522 _engineStatisticsPtr->SetLastError(
2523 VE_APM_ERROR, kTraceError, "SetRxNsStatus() failed to set NS state");
2524 return -1;
2525 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002526
kwiberg55b97fe2016-01-28 05:22:45 -08002527 _rxNsIsEnabled = enable;
2528 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002529
kwiberg55b97fe2016-01-28 05:22:45 -08002530 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002531}
2532
kwiberg55b97fe2016-01-28 05:22:45 -08002533int Channel::GetRxNsStatus(bool& enabled, NsModes& mode) {
2534 bool enable = rx_audioproc_->noise_suppression()->is_enabled();
2535 NoiseSuppression::Level ncLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002536
kwiberg55b97fe2016-01-28 05:22:45 -08002537 enabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002538
kwiberg55b97fe2016-01-28 05:22:45 -08002539 switch (ncLevel) {
2540 case NoiseSuppression::kLow:
2541 mode = kNsLowSuppression;
2542 break;
2543 case NoiseSuppression::kModerate:
2544 mode = kNsModerateSuppression;
2545 break;
2546 case NoiseSuppression::kHigh:
2547 mode = kNsHighSuppression;
2548 break;
2549 case NoiseSuppression::kVeryHigh:
2550 mode = kNsVeryHighSuppression;
2551 break;
2552 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002553
kwiberg55b97fe2016-01-28 05:22:45 -08002554 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002555}
2556
kwiberg55b97fe2016-01-28 05:22:45 -08002557#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
niklase@google.com470e71d2011-07-07 08:21:25 +00002558
kwiberg55b97fe2016-01-28 05:22:45 -08002559int Channel::SetLocalSSRC(unsigned int ssrc) {
2560 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2561 "Channel::SetLocalSSRC()");
2562 if (channel_state_.Get().sending) {
2563 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2564 "SetLocalSSRC() already sending");
2565 return -1;
2566 }
2567 _rtpRtcpModule->SetSSRC(ssrc);
2568 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002569}
2570
kwiberg55b97fe2016-01-28 05:22:45 -08002571int Channel::GetLocalSSRC(unsigned int& ssrc) {
2572 ssrc = _rtpRtcpModule->SSRC();
2573 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002574}
2575
kwiberg55b97fe2016-01-28 05:22:45 -08002576int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2577 ssrc = rtp_receiver_->SSRC();
2578 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002579}
2580
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002581int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002582 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002583 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002584}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002585
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002586int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2587 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002588 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2589 if (enable &&
2590 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2591 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002592 return -1;
2593 }
2594 return 0;
2595}
2596
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002597int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2598 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2599}
2600
2601int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2602 rtp_header_parser_->DeregisterRtpHeaderExtension(
2603 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002604 if (enable &&
2605 !rtp_header_parser_->RegisterRtpHeaderExtension(
2606 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002607 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002608 }
2609 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002610}
2611
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002612void Channel::EnableSendTransportSequenceNumber(int id) {
2613 int ret =
2614 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2615 RTC_DCHECK_EQ(0, ret);
2616}
2617
stefan3313ec92016-01-21 06:32:43 -08002618void Channel::EnableReceiveTransportSequenceNumber(int id) {
2619 rtp_header_parser_->DeregisterRtpHeaderExtension(
2620 kRtpExtensionTransportSequenceNumber);
2621 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2622 kRtpExtensionTransportSequenceNumber, id);
2623 RTC_DCHECK(ret);
2624}
2625
stefanbba9dec2016-02-01 04:39:55 -08002626void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002627 RtpPacketSender* rtp_packet_sender,
2628 TransportFeedbackObserver* transport_feedback_observer,
2629 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002630 RTC_DCHECK(rtp_packet_sender);
2631 RTC_DCHECK(transport_feedback_observer);
2632 RTC_DCHECK(packet_router && !packet_router_);
2633 feedback_observer_proxy_->SetTransportFeedbackObserver(
2634 transport_feedback_observer);
2635 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2636 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2637 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002638 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002639 packet_router_ = packet_router;
2640}
2641
stefanbba9dec2016-02-01 04:39:55 -08002642void Channel::RegisterReceiverCongestionControlObjects(
2643 PacketRouter* packet_router) {
2644 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002645 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002646 packet_router_ = packet_router;
2647}
2648
2649void Channel::ResetCongestionControlObjects() {
2650 RTC_DCHECK(packet_router_);
2651 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2652 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2653 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002654 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002655 packet_router_ = nullptr;
2656 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2657}
2658
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002659void Channel::SetRTCPStatus(bool enable) {
2660 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2661 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002662 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002663}
2664
kwiberg55b97fe2016-01-28 05:22:45 -08002665int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002666 RtcpMode method = _rtpRtcpModule->RTCP();
2667 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002668 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002669}
2670
kwiberg55b97fe2016-01-28 05:22:45 -08002671int Channel::SetRTCP_CNAME(const char cName[256]) {
2672 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2673 "Channel::SetRTCP_CNAME()");
2674 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2675 _engineStatisticsPtr->SetLastError(
2676 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2677 "SetRTCP_CNAME() failed to set RTCP CNAME");
2678 return -1;
2679 }
2680 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002681}
2682
kwiberg55b97fe2016-01-28 05:22:45 -08002683int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2684 if (cName == NULL) {
2685 _engineStatisticsPtr->SetLastError(
2686 VE_INVALID_ARGUMENT, kTraceError,
2687 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2688 return -1;
2689 }
2690 char cname[RTCP_CNAME_SIZE];
2691 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2692 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2693 _engineStatisticsPtr->SetLastError(
2694 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2695 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2696 return -1;
2697 }
2698 strcpy(cName, cname);
2699 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002700}
2701
kwiberg55b97fe2016-01-28 05:22:45 -08002702int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2703 unsigned int& NTPLow,
2704 unsigned int& timestamp,
2705 unsigned int& playoutTimestamp,
2706 unsigned int* jitter,
2707 unsigned short* fractionLost) {
2708 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002709
kwiberg55b97fe2016-01-28 05:22:45 -08002710 RTCPSenderInfo senderInfo;
2711 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2712 _engineStatisticsPtr->SetLastError(
2713 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2714 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2715 "side");
2716 return -1;
2717 }
2718
2719 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2720 // and octet count)
2721 NTPHigh = senderInfo.NTPseconds;
2722 NTPLow = senderInfo.NTPfraction;
2723 timestamp = senderInfo.RTPtimeStamp;
2724
2725 // --- Locally derived information
2726
2727 // This value is updated on each incoming RTCP packet (0 when no packet
2728 // has been received)
2729 playoutTimestamp = playout_timestamp_rtcp_;
2730
2731 if (NULL != jitter || NULL != fractionLost) {
2732 // Get all RTCP receiver report blocks that have been received on this
2733 // channel. If we receive RTP packets from a remote source we know the
2734 // remote SSRC and use the report block from him.
2735 // Otherwise use the first report block.
2736 std::vector<RTCPReportBlock> remote_stats;
2737 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2738 remote_stats.empty()) {
2739 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2740 "GetRemoteRTCPData() failed to measure statistics due"
2741 " to lack of received RTP and/or RTCP packets");
2742 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002743 }
2744
kwiberg55b97fe2016-01-28 05:22:45 -08002745 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2746 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2747 for (; it != remote_stats.end(); ++it) {
2748 if (it->remoteSSRC == remoteSSRC)
2749 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002750 }
kwiberg55b97fe2016-01-28 05:22:45 -08002751
2752 if (it == remote_stats.end()) {
2753 // If we have not received any RTCP packets from this SSRC it probably
2754 // means that we have not received any RTP packets.
2755 // Use the first received report block instead.
2756 it = remote_stats.begin();
2757 remoteSSRC = it->remoteSSRC;
2758 }
2759
2760 if (jitter) {
2761 *jitter = it->jitter;
2762 }
2763
2764 if (fractionLost) {
2765 *fractionLost = it->fractionLost;
2766 }
2767 }
2768 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002769}
2770
kwiberg55b97fe2016-01-28 05:22:45 -08002771int Channel::SendApplicationDefinedRTCPPacket(
2772 unsigned char subType,
2773 unsigned int name,
2774 const char* data,
2775 unsigned short dataLengthInBytes) {
2776 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2777 "Channel::SendApplicationDefinedRTCPPacket()");
2778 if (!channel_state_.Get().sending) {
2779 _engineStatisticsPtr->SetLastError(
2780 VE_NOT_SENDING, kTraceError,
2781 "SendApplicationDefinedRTCPPacket() not sending");
2782 return -1;
2783 }
2784 if (NULL == data) {
2785 _engineStatisticsPtr->SetLastError(
2786 VE_INVALID_ARGUMENT, kTraceError,
2787 "SendApplicationDefinedRTCPPacket() invalid data value");
2788 return -1;
2789 }
2790 if (dataLengthInBytes % 4 != 0) {
2791 _engineStatisticsPtr->SetLastError(
2792 VE_INVALID_ARGUMENT, kTraceError,
2793 "SendApplicationDefinedRTCPPacket() invalid length value");
2794 return -1;
2795 }
2796 RtcpMode status = _rtpRtcpModule->RTCP();
2797 if (status == RtcpMode::kOff) {
2798 _engineStatisticsPtr->SetLastError(
2799 VE_RTCP_ERROR, kTraceError,
2800 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2801 return -1;
2802 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002803
kwiberg55b97fe2016-01-28 05:22:45 -08002804 // Create and schedule the RTCP APP packet for transmission
2805 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2806 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2807 _engineStatisticsPtr->SetLastError(
2808 VE_SEND_ERROR, kTraceError,
2809 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2810 return -1;
2811 }
2812 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002813}
2814
kwiberg55b97fe2016-01-28 05:22:45 -08002815int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2816 unsigned int& maxJitterMs,
2817 unsigned int& discardedPackets) {
2818 // The jitter statistics is updated for each received RTP packet and is
2819 // based on received packets.
2820 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2821 // If RTCP is off, there is no timed thread in the RTCP module regularly
2822 // generating new stats, trigger the update manually here instead.
2823 StreamStatistician* statistician =
2824 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2825 if (statistician) {
2826 // Don't use returned statistics, use data from proxy instead so that
2827 // max jitter can be fetched atomically.
2828 RtcpStatistics s;
2829 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002830 }
kwiberg55b97fe2016-01-28 05:22:45 -08002831 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002832
kwiberg55b97fe2016-01-28 05:22:45 -08002833 ChannelStatistics stats = statistics_proxy_->GetStats();
2834 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2835 if (playoutFrequency > 0) {
2836 // Scale RTP statistics given the current playout frequency
2837 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2838 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2839 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002840
kwiberg55b97fe2016-01-28 05:22:45 -08002841 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002842
kwiberg55b97fe2016-01-28 05:22:45 -08002843 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002844}
2845
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002846int Channel::GetRemoteRTCPReportBlocks(
2847 std::vector<ReportBlock>* report_blocks) {
2848 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002849 _engineStatisticsPtr->SetLastError(
2850 VE_INVALID_ARGUMENT, kTraceError,
2851 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002852 return -1;
2853 }
2854
2855 // Get the report blocks from the latest received RTCP Sender or Receiver
2856 // Report. Each element in the vector contains the sender's SSRC and a
2857 // report block according to RFC 3550.
2858 std::vector<RTCPReportBlock> rtcp_report_blocks;
2859 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002860 return -1;
2861 }
2862
2863 if (rtcp_report_blocks.empty())
2864 return 0;
2865
2866 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2867 for (; it != rtcp_report_blocks.end(); ++it) {
2868 ReportBlock report_block;
2869 report_block.sender_SSRC = it->remoteSSRC;
2870 report_block.source_SSRC = it->sourceSSRC;
2871 report_block.fraction_lost = it->fractionLost;
2872 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2873 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2874 report_block.interarrival_jitter = it->jitter;
2875 report_block.last_SR_timestamp = it->lastSR;
2876 report_block.delay_since_last_SR = it->delaySinceLastSR;
2877 report_blocks->push_back(report_block);
2878 }
2879 return 0;
2880}
2881
kwiberg55b97fe2016-01-28 05:22:45 -08002882int Channel::GetRTPStatistics(CallStatistics& stats) {
2883 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002884
kwiberg55b97fe2016-01-28 05:22:45 -08002885 // The jitter statistics is updated for each received RTP packet and is
2886 // based on received packets.
2887 RtcpStatistics statistics;
2888 StreamStatistician* statistician =
2889 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002890 if (statistician) {
2891 statistician->GetStatistics(&statistics,
2892 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002893 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002894
kwiberg55b97fe2016-01-28 05:22:45 -08002895 stats.fractionLost = statistics.fraction_lost;
2896 stats.cumulativeLost = statistics.cumulative_lost;
2897 stats.extendedMax = statistics.extended_max_sequence_number;
2898 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002899
kwiberg55b97fe2016-01-28 05:22:45 -08002900 // --- RTT
2901 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002902
kwiberg55b97fe2016-01-28 05:22:45 -08002903 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002904
kwiberg55b97fe2016-01-28 05:22:45 -08002905 size_t bytesSent(0);
2906 uint32_t packetsSent(0);
2907 size_t bytesReceived(0);
2908 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002909
kwiberg55b97fe2016-01-28 05:22:45 -08002910 if (statistician) {
2911 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2912 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002913
kwiberg55b97fe2016-01-28 05:22:45 -08002914 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2915 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2916 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2917 " output will not be complete");
2918 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002919
kwiberg55b97fe2016-01-28 05:22:45 -08002920 stats.bytesSent = bytesSent;
2921 stats.packetsSent = packetsSent;
2922 stats.bytesReceived = bytesReceived;
2923 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002924
kwiberg55b97fe2016-01-28 05:22:45 -08002925 // --- Timestamps
2926 {
2927 rtc::CritScope lock(&ts_stats_lock_);
2928 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2929 }
2930 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002931}
2932
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002933int Channel::SetCodecFECStatus(bool enable) {
2934 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2935 "Channel::SetCodecFECStatus()");
2936
kwibergc8d071e2016-04-06 12:22:38 -07002937 if (!codec_manager_.SetCodecFEC(enable) ||
2938 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002939 _engineStatisticsPtr->SetLastError(
2940 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2941 "SetCodecFECStatus() failed to set FEC state");
2942 return -1;
2943 }
2944 return 0;
2945}
2946
2947bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002948 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002949}
2950
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002951void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2952 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002953 // If pacing is enabled we always store packets.
2954 if (!pacing_enabled_)
2955 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002956 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002957 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002958 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002959 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002960 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002961}
2962
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002963// Called when we are missing one or more packets.
2964int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002965 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2966}
2967
kwiberg55b97fe2016-01-28 05:22:45 -08002968uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2969 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2970 "Channel::Demultiplex()");
2971 _audioFrame.CopyFrom(audioFrame);
2972 _audioFrame.id_ = _channelId;
2973 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002974}
2975
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002976void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002977 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002978 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002979 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002980 CodecInst codec;
2981 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002982
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002983 // Never upsample or upmix the capture signal here. This should be done at the
2984 // end of the send chain.
2985 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2986 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2987 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2988 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002989}
2990
kwiberg55b97fe2016-01-28 05:22:45 -08002991uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2992 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2993 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002994
kwiberg55b97fe2016-01-28 05:22:45 -08002995 if (_audioFrame.samples_per_channel_ == 0) {
2996 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2997 "Channel::PrepareEncodeAndSend() invalid audio frame");
2998 return 0xFFFFFFFF;
2999 }
3000
3001 if (channel_state_.Get().input_file_playing) {
3002 MixOrReplaceAudioWithFile(mixingFrequency);
3003 }
3004
solenberg1c2af8e2016-03-24 10:36:00 -07003005 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
3006 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08003007
3008 if (channel_state_.Get().input_external_media) {
3009 rtc::CritScope cs(&_callbackCritSect);
3010 const bool isStereo = (_audioFrame.num_channels_ == 2);
3011 if (_inputExternalMediaCallbackPtr) {
3012 _inputExternalMediaCallbackPtr->Process(
3013 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
3014 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
3015 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00003016 }
kwiberg55b97fe2016-01-28 05:22:45 -08003017 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003018
kwiberg55b97fe2016-01-28 05:22:45 -08003019 if (_includeAudioLevelIndication) {
3020 size_t length =
3021 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02003022 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07003023 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003024 rms_level_.ProcessMuted(length);
3025 } else {
3026 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00003027 }
kwiberg55b97fe2016-01-28 05:22:45 -08003028 }
solenberg1c2af8e2016-03-24 10:36:00 -07003029 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00003030
kwiberg55b97fe2016-01-28 05:22:45 -08003031 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003032}
3033
kwiberg55b97fe2016-01-28 05:22:45 -08003034uint32_t Channel::EncodeAndSend() {
3035 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
3036 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003037
kwiberg55b97fe2016-01-28 05:22:45 -08003038 assert(_audioFrame.num_channels_ <= 2);
3039 if (_audioFrame.samples_per_channel_ == 0) {
3040 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3041 "Channel::EncodeAndSend() invalid audio frame");
3042 return 0xFFFFFFFF;
3043 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003044
kwiberg55b97fe2016-01-28 05:22:45 -08003045 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003046
kwiberg55b97fe2016-01-28 05:22:45 -08003047 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00003048
kwiberg55b97fe2016-01-28 05:22:45 -08003049 // The ACM resamples internally.
3050 _audioFrame.timestamp_ = _timeStamp;
3051 // This call will trigger AudioPacketizationCallback::SendData if encoding
3052 // is done and payload is ready for packetization and transmission.
3053 // Otherwise, it will return without invoking the callback.
3054 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
3055 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
3056 "Channel::EncodeAndSend() ACM encoding failed");
3057 return 0xFFFFFFFF;
3058 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003059
kwiberg55b97fe2016-01-28 05:22:45 -08003060 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
3061 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003062}
3063
Minyue2013aec2015-05-13 14:14:42 +02003064void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08003065 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003066 Channel* channel = associate_send_channel_.channel();
3067 if (channel && channel->ChannelId() == channel_id) {
3068 // If this channel is associated with a send channel of the specified
3069 // Channel ID, disassociate with it.
3070 ChannelOwner ref(NULL);
3071 associate_send_channel_ = ref;
3072 }
3073}
3074
ivoc14d5dbe2016-07-04 07:06:55 -07003075void Channel::SetRtcEventLog(RtcEventLog* event_log) {
3076 event_log_proxy_->SetEventLog(event_log);
3077}
3078
kwiberg55b97fe2016-01-28 05:22:45 -08003079int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
3080 VoEMediaProcess& processObject) {
3081 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3082 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003083
kwiberg55b97fe2016-01-28 05:22:45 -08003084 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003085
kwiberg55b97fe2016-01-28 05:22:45 -08003086 if (kPlaybackPerChannel == type) {
3087 if (_outputExternalMediaCallbackPtr) {
3088 _engineStatisticsPtr->SetLastError(
3089 VE_INVALID_OPERATION, kTraceError,
3090 "Channel::RegisterExternalMediaProcessing() "
3091 "output external media already enabled");
3092 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003093 }
kwiberg55b97fe2016-01-28 05:22:45 -08003094 _outputExternalMediaCallbackPtr = &processObject;
3095 _outputExternalMedia = true;
3096 } else if (kRecordingPerChannel == type) {
3097 if (_inputExternalMediaCallbackPtr) {
3098 _engineStatisticsPtr->SetLastError(
3099 VE_INVALID_OPERATION, kTraceError,
3100 "Channel::RegisterExternalMediaProcessing() "
3101 "output external media already enabled");
3102 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003103 }
kwiberg55b97fe2016-01-28 05:22:45 -08003104 _inputExternalMediaCallbackPtr = &processObject;
3105 channel_state_.SetInputExternalMedia(true);
3106 }
3107 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003108}
3109
kwiberg55b97fe2016-01-28 05:22:45 -08003110int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
3111 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3112 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003113
kwiberg55b97fe2016-01-28 05:22:45 -08003114 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003115
kwiberg55b97fe2016-01-28 05:22:45 -08003116 if (kPlaybackPerChannel == type) {
3117 if (!_outputExternalMediaCallbackPtr) {
3118 _engineStatisticsPtr->SetLastError(
3119 VE_INVALID_OPERATION, kTraceWarning,
3120 "Channel::DeRegisterExternalMediaProcessing() "
3121 "output external media already disabled");
3122 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003123 }
kwiberg55b97fe2016-01-28 05:22:45 -08003124 _outputExternalMedia = false;
3125 _outputExternalMediaCallbackPtr = NULL;
3126 } else if (kRecordingPerChannel == type) {
3127 if (!_inputExternalMediaCallbackPtr) {
3128 _engineStatisticsPtr->SetLastError(
3129 VE_INVALID_OPERATION, kTraceWarning,
3130 "Channel::DeRegisterExternalMediaProcessing() "
3131 "input external media already disabled");
3132 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003133 }
kwiberg55b97fe2016-01-28 05:22:45 -08003134 channel_state_.SetInputExternalMedia(false);
3135 _inputExternalMediaCallbackPtr = NULL;
3136 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003137
kwiberg55b97fe2016-01-28 05:22:45 -08003138 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003139}
3140
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003141int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08003142 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3143 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003144
kwiberg55b97fe2016-01-28 05:22:45 -08003145 if (channel_state_.Get().playing) {
3146 _engineStatisticsPtr->SetLastError(
3147 VE_INVALID_OPERATION, kTraceError,
3148 "Channel::SetExternalMixing() "
3149 "external mixing cannot be changed while playing.");
3150 return -1;
3151 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003152
kwiberg55b97fe2016-01-28 05:22:45 -08003153 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003154
kwiberg55b97fe2016-01-28 05:22:45 -08003155 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003156}
3157
kwiberg55b97fe2016-01-28 05:22:45 -08003158int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
3159 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003160}
3161
wu@webrtc.org24301a62013-12-13 19:17:43 +00003162void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3163 audio_coding_->GetDecodingCallStatistics(stats);
3164}
3165
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003166bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3167 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08003168 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07003169 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003170 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003171 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003172}
3173
solenberg358057b2015-11-27 10:46:42 -08003174uint32_t Channel::GetDelayEstimate() const {
3175 int jitter_buffer_delay_ms = 0;
3176 int playout_buffer_delay_ms = 0;
3177 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
3178 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
3179}
3180
deadbeef74375882015-08-13 12:09:10 -07003181int Channel::LeastRequiredDelayMs() const {
3182 return audio_coding_->LeastRequiredDelayMs();
3183}
3184
kwiberg55b97fe2016-01-28 05:22:45 -08003185int Channel::SetMinimumPlayoutDelay(int delayMs) {
3186 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3187 "Channel::SetMinimumPlayoutDelay()");
3188 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3189 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
3190 _engineStatisticsPtr->SetLastError(
3191 VE_INVALID_ARGUMENT, kTraceError,
3192 "SetMinimumPlayoutDelay() invalid min delay");
3193 return -1;
3194 }
3195 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
3196 _engineStatisticsPtr->SetLastError(
3197 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3198 "SetMinimumPlayoutDelay() failed to set min playout delay");
3199 return -1;
3200 }
3201 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003202}
3203
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003204int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07003205 uint32_t playout_timestamp_rtp = 0;
3206 {
tommi31fc21f2016-01-21 10:37:37 -08003207 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003208 playout_timestamp_rtp = playout_timestamp_rtp_;
3209 }
kwiberg55b97fe2016-01-28 05:22:45 -08003210 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003211 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07003212 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003213 "GetPlayoutTimestamp() failed to retrieve timestamp");
3214 return -1;
3215 }
deadbeef74375882015-08-13 12:09:10 -07003216 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003217 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003218}
3219
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003220int Channel::SetInitTimestamp(unsigned int timestamp) {
3221 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003222 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003223 if (channel_state_.Get().sending) {
3224 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3225 "SetInitTimestamp() already sending");
3226 return -1;
3227 }
3228 _rtpRtcpModule->SetStartTimestamp(timestamp);
3229 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003230}
3231
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003232int Channel::SetInitSequenceNumber(short sequenceNumber) {
3233 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3234 "Channel::SetInitSequenceNumber()");
3235 if (channel_state_.Get().sending) {
3236 _engineStatisticsPtr->SetLastError(
3237 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3238 return -1;
3239 }
3240 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3241 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003242}
3243
kwiberg55b97fe2016-01-28 05:22:45 -08003244int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3245 RtpReceiver** rtp_receiver) const {
3246 *rtpRtcpModule = _rtpRtcpModule.get();
3247 *rtp_receiver = rtp_receiver_.get();
3248 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003249}
3250
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003251// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3252// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003253int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003254 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003255 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003256
kwiberg55b97fe2016-01-28 05:22:45 -08003257 {
3258 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003259
kwiberg5a25d952016-08-17 07:31:12 -07003260 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003261 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3262 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3263 " doesnt exist");
3264 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003265 }
3266
kwiberg4ec01d92016-08-22 08:43:54 -07003267 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003268 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003269 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3270 "Channel::MixOrReplaceAudioWithFile() file mixing "
3271 "failed");
3272 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003273 }
kwiberg55b97fe2016-01-28 05:22:45 -08003274 if (fileSamples == 0) {
3275 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3276 "Channel::MixOrReplaceAudioWithFile() file is ended");
3277 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003278 }
kwiberg55b97fe2016-01-28 05:22:45 -08003279 }
3280
3281 assert(_audioFrame.samples_per_channel_ == fileSamples);
3282
3283 if (_mixFileWithMicrophone) {
3284 // Currently file stream is always mono.
3285 // TODO(xians): Change the code when FilePlayer supports real stereo.
3286 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3287 1, fileSamples);
3288 } else {
3289 // Replace ACM audio with file.
3290 // Currently file stream is always mono.
3291 // TODO(xians): Change the code when FilePlayer supports real stereo.
3292 _audioFrame.UpdateFrame(
3293 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3294 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3295 }
3296 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003297}
3298
kwiberg55b97fe2016-01-28 05:22:45 -08003299int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3300 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003301
kwibergb7f89d62016-02-17 10:04:18 -08003302 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003303 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003304
kwiberg55b97fe2016-01-28 05:22:45 -08003305 {
3306 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003307
kwiberg5a25d952016-08-17 07:31:12 -07003308 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003309 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3310 "Channel::MixAudioWithFile() file mixing failed");
3311 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003312 }
3313
kwiberg55b97fe2016-01-28 05:22:45 -08003314 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003315 if (output_file_player_->Get10msAudioFromFile(
3316 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003317 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3318 "Channel::MixAudioWithFile() file mixing failed");
3319 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003320 }
kwiberg55b97fe2016-01-28 05:22:45 -08003321 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003322
kwiberg55b97fe2016-01-28 05:22:45 -08003323 if (audioFrame.samples_per_channel_ == fileSamples) {
3324 // Currently file stream is always mono.
3325 // TODO(xians): Change the code when FilePlayer supports real stereo.
3326 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3327 fileSamples);
3328 } else {
3329 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3330 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3331 ") != "
3332 "fileSamples(%" PRIuS ")",
3333 audioFrame.samples_per_channel_, fileSamples);
3334 return -1;
3335 }
3336
3337 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003338}
3339
deadbeef74375882015-08-13 12:09:10 -07003340void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003341 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003342
henrik.lundin96bd5022016-04-06 04:13:56 -07003343 if (!jitter_buffer_playout_timestamp_) {
3344 // This can happen if this channel has not received any RTP packets. In
3345 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003346 return;
3347 }
3348
3349 uint16_t delay_ms = 0;
3350 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003351 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003352 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3353 " delay from the ADM");
3354 _engineStatisticsPtr->SetLastError(
3355 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3356 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3357 return;
3358 }
3359
henrik.lundin96bd5022016-04-06 04:13:56 -07003360 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3361 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003362
3363 // Remove the playout delay.
henrik.lundin96bd5022016-04-06 04:13:56 -07003364 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003365
kwiberg55b97fe2016-01-28 05:22:45 -08003366 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003367 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003368 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003369
3370 {
tommi31fc21f2016-01-21 10:37:37 -08003371 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003372 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003373 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003374 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003375 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003376 }
3377 playout_delay_ms_ = delay_ms;
3378 }
3379}
3380
kwiberg55b97fe2016-01-28 05:22:45 -08003381void Channel::RegisterReceiveCodecsToRTPModule() {
3382 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3383 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003384
kwiberg55b97fe2016-01-28 05:22:45 -08003385 CodecInst codec;
3386 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003387
kwiberg55b97fe2016-01-28 05:22:45 -08003388 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3389 // Open up the RTP/RTCP receiver for all supported codecs
3390 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3391 (rtp_receiver_->RegisterReceivePayload(
3392 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3393 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3394 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3395 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3396 " to register %s (%d/%d/%" PRIuS
3397 "/%d) to RTP/RTCP "
3398 "receiver",
3399 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3400 codec.rate);
3401 } else {
3402 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3403 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3404 "(%d/%d/%" PRIuS
3405 "/%d) has been added to the RTP/RTCP "
3406 "receiver",
3407 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3408 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003409 }
kwiberg55b97fe2016-01-28 05:22:45 -08003410 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003411}
3412
kwiberg55b97fe2016-01-28 05:22:45 -08003413int Channel::SetSendRtpHeaderExtension(bool enable,
3414 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003415 unsigned char id) {
3416 int error = 0;
3417 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3418 if (enable) {
3419 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3420 }
3421 return error;
3422}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003423
henrik.lundinb3e30012016-08-31 14:09:51 -07003424int32_t Channel::GetPlayoutFrequency() const {
wu@webrtc.org94454b72014-06-05 20:34:08 +00003425 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3426 CodecInst current_recive_codec;
3427 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3428 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3429 // Even though the actual sampling rate for G.722 audio is
3430 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3431 // 8,000 Hz because that value was erroneously assigned in
3432 // RFC 1890 and must remain unchanged for backward compatibility.
3433 playout_frequency = 8000;
3434 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3435 // We are resampling Opus internally to 32,000 Hz until all our
3436 // DSP routines can operate at 48,000 Hz, but the RTP clock
3437 // rate for the Opus payload format is standardized to 48,000 Hz,
3438 // because that is the maximum supported decoding sampling rate.
3439 playout_frequency = 48000;
3440 }
3441 }
3442 return playout_frequency;
3443}
3444
Minyue2013aec2015-05-13 14:14:42 +02003445int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003446 RtcpMode method = _rtpRtcpModule->RTCP();
3447 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003448 return 0;
3449 }
3450 std::vector<RTCPReportBlock> report_blocks;
3451 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003452
3453 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003454 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003455 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003456 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003457 Channel* channel = associate_send_channel_.channel();
3458 // Tries to get RTT from an associated channel. This is important for
3459 // receive-only channels.
3460 if (channel) {
3461 // To prevent infinite recursion and deadlock, calling GetRTT of
3462 // associate channel should always use "false" for argument:
3463 // |allow_associate_channel|.
3464 rtt = channel->GetRTT(false);
3465 }
3466 }
3467 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003468 }
3469
3470 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3471 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3472 for (; it != report_blocks.end(); ++it) {
3473 if (it->remoteSSRC == remoteSSRC)
3474 break;
3475 }
3476 if (it == report_blocks.end()) {
3477 // We have not received packets with SSRC matching the report blocks.
3478 // To calculate RTT we try with the SSRC of the first report block.
3479 // This is very important for send-only channels where we don't know
3480 // the SSRC of the other end.
3481 remoteSSRC = report_blocks[0].remoteSSRC;
3482 }
Minyue2013aec2015-05-13 14:14:42 +02003483
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003484 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003485 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003486 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003487 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3488 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003489 return 0;
3490 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003491 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003492}
3493
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003494} // namespace voe
3495} // namespace webrtc