blob: 301b5a6f8b9a60d43831b37a36fe655b71079216 [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"
Henrik Lundin64dad832015-05-11 12:44:23 +020024#include "webrtc/config.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000025#include "webrtc/modules/audio_device/include/audio_device.h"
26#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010027#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010028#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010029#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
30#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000032#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/utility/include/audio_frame_operations.h"
34#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010035#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000036#include "webrtc/voice_engine/include/voe_external_media.h"
37#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
38#include "webrtc/voice_engine/output_mixer.h"
39#include "webrtc/voice_engine/statistics.h"
40#include "webrtc/voice_engine/transmit_mixer.h"
41#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000042
andrew@webrtc.org50419b02012-11-14 19:07:54 +000043namespace webrtc {
44namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000045
kwibergc8d071e2016-04-06 12:22:38 -070046namespace {
47
Erik Språng737336d2016-07-29 12:59:36 +020048constexpr int64_t kMaxRetransmissionWindowMs = 1000;
49constexpr int64_t kMinRetransmissionWindowMs = 30;
50
kwibergc8d071e2016-04-06 12:22:38 -070051bool RegisterReceiveCodec(std::unique_ptr<AudioCodingModule>* acm,
52 acm2::RentACodec* rac,
53 const CodecInst& ci) {
kwibergabe95ba2016-06-02 02:58:59 -070054 const int result = (*acm)->RegisterReceiveCodec(
55 ci, [&] { return rac->RentIsacDecoder(ci.plfreq); });
kwibergc8d071e2016-04-06 12:22:38 -070056 return result == 0;
57}
58
59} // namespace
60
solenberg8842c3e2016-03-11 03:06:41 -080061const int kTelephoneEventAttenuationdB = 10;
62
ivoc14d5dbe2016-07-04 07:06:55 -070063class RtcEventLogProxy final : public webrtc::RtcEventLog {
64 public:
65 RtcEventLogProxy() : event_log_(nullptr) {}
66
67 bool StartLogging(const std::string& file_name,
68 int64_t max_size_bytes) override {
69 RTC_NOTREACHED();
70 return false;
71 }
72
73 bool StartLogging(rtc::PlatformFile log_file,
74 int64_t max_size_bytes) override {
75 RTC_NOTREACHED();
76 return false;
77 }
78
79 void StopLogging() override { RTC_NOTREACHED(); }
80
81 void LogVideoReceiveStreamConfig(
82 const webrtc::VideoReceiveStream::Config& config) override {
83 rtc::CritScope lock(&crit_);
84 if (event_log_) {
85 event_log_->LogVideoReceiveStreamConfig(config);
86 }
87 }
88
89 void LogVideoSendStreamConfig(
90 const webrtc::VideoSendStream::Config& config) override {
91 rtc::CritScope lock(&crit_);
92 if (event_log_) {
93 event_log_->LogVideoSendStreamConfig(config);
94 }
95 }
96
97 void LogRtpHeader(webrtc::PacketDirection direction,
98 webrtc::MediaType media_type,
99 const uint8_t* header,
100 size_t packet_length) override {
101 rtc::CritScope lock(&crit_);
102 if (event_log_) {
103 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
104 }
105 }
106
107 void LogRtcpPacket(webrtc::PacketDirection direction,
108 webrtc::MediaType media_type,
109 const uint8_t* packet,
110 size_t length) override {
111 rtc::CritScope lock(&crit_);
112 if (event_log_) {
113 event_log_->LogRtcpPacket(direction, media_type, packet, length);
114 }
115 }
116
117 void LogAudioPlayout(uint32_t ssrc) override {
118 rtc::CritScope lock(&crit_);
119 if (event_log_) {
120 event_log_->LogAudioPlayout(ssrc);
121 }
122 }
123
124 void LogBwePacketLossEvent(int32_t bitrate,
125 uint8_t fraction_loss,
126 int32_t total_packets) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
130 }
131 }
132
133 void SetEventLog(RtcEventLog* event_log) {
134 rtc::CritScope lock(&crit_);
135 event_log_ = event_log;
136 }
137
138 private:
139 rtc::CriticalSection crit_;
140 RtcEventLog* event_log_ GUARDED_BY(crit_);
141 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
142};
143
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100144class TransportFeedbackProxy : public TransportFeedbackObserver {
145 public:
146 TransportFeedbackProxy() : feedback_observer_(nullptr) {
147 pacer_thread_.DetachFromThread();
148 network_thread_.DetachFromThread();
149 }
150
151 void SetTransportFeedbackObserver(
152 TransportFeedbackObserver* feedback_observer) {
153 RTC_DCHECK(thread_checker_.CalledOnValidThread());
154 rtc::CritScope lock(&crit_);
155 feedback_observer_ = feedback_observer;
156 }
157
158 // Implements TransportFeedbackObserver.
159 void AddPacket(uint16_t sequence_number,
160 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700161 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100162 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
163 rtc::CritScope lock(&crit_);
164 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700165 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100166 }
167 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
168 RTC_DCHECK(network_thread_.CalledOnValidThread());
169 rtc::CritScope lock(&crit_);
Stefan Holmer60e43462016-09-07 09:58:20 +0200170 feedback_observer_->OnTransportFeedback(feedback);
171 }
172 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
173 RTC_NOTREACHED();
174 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100175 }
176
177 private:
178 rtc::CriticalSection crit_;
179 rtc::ThreadChecker thread_checker_;
180 rtc::ThreadChecker pacer_thread_;
181 rtc::ThreadChecker network_thread_;
182 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
183};
184
185class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
186 public:
187 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
188 pacer_thread_.DetachFromThread();
189 }
190
191 void SetSequenceNumberAllocator(
192 TransportSequenceNumberAllocator* seq_num_allocator) {
193 RTC_DCHECK(thread_checker_.CalledOnValidThread());
194 rtc::CritScope lock(&crit_);
195 seq_num_allocator_ = seq_num_allocator;
196 }
197
198 // Implements TransportSequenceNumberAllocator.
199 uint16_t AllocateSequenceNumber() override {
200 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
201 rtc::CritScope lock(&crit_);
202 if (!seq_num_allocator_)
203 return 0;
204 return seq_num_allocator_->AllocateSequenceNumber();
205 }
206
207 private:
208 rtc::CriticalSection crit_;
209 rtc::ThreadChecker thread_checker_;
210 rtc::ThreadChecker pacer_thread_;
211 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
212};
213
214class RtpPacketSenderProxy : public RtpPacketSender {
215 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800216 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100217
218 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
219 RTC_DCHECK(thread_checker_.CalledOnValidThread());
220 rtc::CritScope lock(&crit_);
221 rtp_packet_sender_ = rtp_packet_sender;
222 }
223
224 // Implements RtpPacketSender.
225 void InsertPacket(Priority priority,
226 uint32_t ssrc,
227 uint16_t sequence_number,
228 int64_t capture_time_ms,
229 size_t bytes,
230 bool retransmission) override {
231 rtc::CritScope lock(&crit_);
232 if (rtp_packet_sender_) {
233 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
234 capture_time_ms, bytes, retransmission);
235 }
236 }
237
238 private:
239 rtc::ThreadChecker thread_checker_;
240 rtc::CriticalSection crit_;
241 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
242};
243
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000244// Extend the default RTCP statistics struct with max_jitter, defined as the
245// maximum jitter value seen in an RTCP report block.
246struct ChannelStatistics : public RtcpStatistics {
247 ChannelStatistics() : rtcp(), max_jitter(0) {}
248
249 RtcpStatistics rtcp;
250 uint32_t max_jitter;
251};
252
253// Statistics callback, called at each generation of a new RTCP report block.
254class StatisticsProxy : public RtcpStatisticsCallback {
255 public:
tommi31fc21f2016-01-21 10:37:37 -0800256 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000257 virtual ~StatisticsProxy() {}
258
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000259 void StatisticsUpdated(const RtcpStatistics& statistics,
260 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000261 if (ssrc != ssrc_)
262 return;
263
tommi31fc21f2016-01-21 10:37:37 -0800264 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000265 stats_.rtcp = statistics;
266 if (statistics.jitter > stats_.max_jitter) {
267 stats_.max_jitter = statistics.jitter;
268 }
269 }
270
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000271 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000272
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000273 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800274 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000275 return stats_;
276 }
277
278 private:
279 // StatisticsUpdated calls are triggered from threads in the RTP module,
280 // while GetStats calls can be triggered from the public voice engine API,
281 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800282 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000283 const uint32_t ssrc_;
284 ChannelStatistics stats_;
285};
286
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000287class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000288 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000289 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
290 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000291
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000292 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
293 // Not used for Voice Engine.
294 }
295
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000296 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
297 int64_t rtt,
298 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000299 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
300 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
301 // report for VoiceEngine?
302 if (report_blocks.empty())
303 return;
304
305 int fraction_lost_aggregate = 0;
306 int total_number_of_packets = 0;
307
308 // If receiving multiple report blocks, calculate the weighted average based
309 // on the number of packets a report refers to.
310 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
311 block_it != report_blocks.end(); ++block_it) {
312 // Find the previous extended high sequence number for this remote SSRC,
313 // to calculate the number of RTP packets this report refers to. Ignore if
314 // we haven't seen this SSRC before.
315 std::map<uint32_t, uint32_t>::iterator seq_num_it =
316 extended_max_sequence_number_.find(block_it->sourceSSRC);
317 int number_of_packets = 0;
318 if (seq_num_it != extended_max_sequence_number_.end()) {
319 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
320 }
321 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
322 total_number_of_packets += number_of_packets;
323
324 extended_max_sequence_number_[block_it->sourceSSRC] =
325 block_it->extendedHighSeqNum;
326 }
327 int weighted_fraction_lost = 0;
328 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800329 weighted_fraction_lost =
330 (fraction_lost_aggregate + total_number_of_packets / 2) /
331 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000332 }
333 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000334 }
335
336 private:
337 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000338 // Maps remote side ssrc to extended highest sequence number received.
339 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000340};
341
kwiberg55b97fe2016-01-28 05:22:45 -0800342int32_t Channel::SendData(FrameType frameType,
343 uint8_t payloadType,
344 uint32_t timeStamp,
345 const uint8_t* payloadData,
346 size_t payloadSize,
347 const RTPFragmentationHeader* fragmentation) {
348 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
349 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
350 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
351 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000352
kwiberg55b97fe2016-01-28 05:22:45 -0800353 if (_includeAudioLevelIndication) {
354 // Store current audio level in the RTP/RTCP module.
355 // The level will be used in combination with voice-activity state
356 // (frameType) to add an RTP header extension
357 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
358 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
kwiberg55b97fe2016-01-28 05:22:45 -0800360 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
361 // packetization.
362 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700363 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800364 (FrameType&)frameType, payloadType, timeStamp,
365 // Leaving the time when this frame was
366 // received from the capture device as
367 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700368 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800369 _engineStatisticsPtr->SetLastError(
370 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
371 "Channel::SendData() failed to send data to RTP/RTCP module");
372 return -1;
373 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
kwiberg55b97fe2016-01-28 05:22:45 -0800375 _lastLocalTimeStamp = timeStamp;
376 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000377
kwiberg55b97fe2016-01-28 05:22:45 -0800378 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000379}
380
kwiberg55b97fe2016-01-28 05:22:45 -0800381int32_t Channel::InFrameType(FrameType frame_type) {
382 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
383 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000384
kwiberg55b97fe2016-01-28 05:22:45 -0800385 rtc::CritScope cs(&_callbackCritSect);
386 _sendFrameType = (frame_type == kAudioFrameSpeech);
387 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000388}
389
kwiberg55b97fe2016-01-28 05:22:45 -0800390int32_t Channel::OnRxVadDetected(int vadDecision) {
391 rtc::CritScope cs(&_callbackCritSect);
392 if (_rxVadObserverPtr) {
393 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
394 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
kwiberg55b97fe2016-01-28 05:22:45 -0800396 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000397}
398
stefan1d8a5062015-10-02 03:39:33 -0700399bool Channel::SendRtp(const uint8_t* data,
400 size_t len,
401 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800402 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
403 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
kwiberg55b97fe2016-01-28 05:22:45 -0800405 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000406
kwiberg55b97fe2016-01-28 05:22:45 -0800407 if (_transportPtr == NULL) {
408 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
409 "Channel::SendPacket() failed to send RTP packet due to"
410 " invalid transport object");
411 return false;
412 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000413
kwiberg55b97fe2016-01-28 05:22:45 -0800414 uint8_t* bufferToSendPtr = (uint8_t*)data;
415 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000416
kwiberg55b97fe2016-01-28 05:22:45 -0800417 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
418 std::string transport_name =
419 _externalTransport ? "external transport" : "WebRtc sockets";
420 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
421 "Channel::SendPacket() RTP transmission using %s failed",
422 transport_name.c_str());
423 return false;
424 }
425 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000426}
427
kwiberg55b97fe2016-01-28 05:22:45 -0800428bool Channel::SendRtcp(const uint8_t* data, size_t len) {
429 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
430 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000431
kwiberg55b97fe2016-01-28 05:22:45 -0800432 rtc::CritScope cs(&_callbackCritSect);
433 if (_transportPtr == NULL) {
434 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
435 "Channel::SendRtcp() failed to send RTCP packet"
436 " due to invalid transport object");
437 return false;
438 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
kwiberg55b97fe2016-01-28 05:22:45 -0800440 uint8_t* bufferToSendPtr = (uint8_t*)data;
441 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
kwiberg55b97fe2016-01-28 05:22:45 -0800443 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
444 if (n < 0) {
445 std::string transport_name =
446 _externalTransport ? "external transport" : "WebRtc sockets";
447 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
448 "Channel::SendRtcp() transmission using %s failed",
449 transport_name.c_str());
450 return false;
451 }
452 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000453}
454
kwiberg55b97fe2016-01-28 05:22:45 -0800455void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
456 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
457 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000458
kwiberg55b97fe2016-01-28 05:22:45 -0800459 // Update ssrc so that NTP for AV sync can be updated.
460 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000461}
462
Peter Boströmac547a62015-09-17 23:03:57 +0200463void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
465 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
466 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000467}
468
Peter Boströmac547a62015-09-17 23:03:57 +0200469int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000470 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000471 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000472 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800473 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200474 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800475 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
476 "Channel::OnInitializeDecoder(payloadType=%d, "
477 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
478 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000479
kwiberg55b97fe2016-01-28 05:22:45 -0800480 CodecInst receiveCodec = {0};
481 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000482
kwiberg55b97fe2016-01-28 05:22:45 -0800483 receiveCodec.pltype = payloadType;
484 receiveCodec.plfreq = frequency;
485 receiveCodec.channels = channels;
486 receiveCodec.rate = rate;
487 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000488
kwiberg55b97fe2016-01-28 05:22:45 -0800489 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
490 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
kwiberg55b97fe2016-01-28 05:22:45 -0800492 // Register the new codec to the ACM
kwibergc8d071e2016-04-06 12:22:38 -0700493 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, receiveCodec)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800494 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
495 "Channel::OnInitializeDecoder() invalid codec ("
496 "pt=%d, name=%s) received - 1",
497 payloadType, payloadName);
498 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
499 return -1;
500 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000501
kwiberg55b97fe2016-01-28 05:22:45 -0800502 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503}
504
kwiberg55b97fe2016-01-28 05:22:45 -0800505int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
506 size_t payloadSize,
507 const WebRtcRTPHeader* rtpHeader) {
508 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
509 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
510 ","
511 " payloadType=%u, audioChannel=%" PRIuS ")",
512 payloadSize, rtpHeader->header.payloadType,
513 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
kwiberg55b97fe2016-01-28 05:22:45 -0800515 if (!channel_state_.Get().playing) {
516 // Avoid inserting into NetEQ when we are not playing. Count the
517 // packet as discarded.
518 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
519 "received packet is discarded since playing is not"
520 " activated");
521 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000522 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800523 }
524
525 // Push the incoming payload (parsed and ready for decoding) into the ACM
526 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
527 0) {
528 _engineStatisticsPtr->SetLastError(
529 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
530 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
531 return -1;
532 }
533
kwiberg55b97fe2016-01-28 05:22:45 -0800534 int64_t round_trip_time = 0;
535 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
536 NULL);
537
538 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
539 if (!nack_list.empty()) {
540 // Can't use nack_list.data() since it's not supported by all
541 // compilers.
542 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
543 }
544 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000545}
546
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000547bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000548 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000549 RTPHeader header;
550 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
551 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
552 "IncomingPacket invalid RTP header");
553 return false;
554 }
555 header.payload_type_frequency =
556 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
557 if (header.payload_type_frequency < 0)
558 return false;
559 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
560}
561
henrik.lundin42dda502016-05-18 05:36:01 -0700562MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
563 int32_t id,
564 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700565 unsigned int ssrc;
566 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
567 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800568 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700569 bool muted;
570 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
571 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800572 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
573 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
574 // In all likelihood, the audio in this frame is garbage. We return an
575 // error so that the audio mixer module doesn't add it to the mix. As
576 // a result, it won't be played out and the actions skipped here are
577 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700578 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800579 }
henrik.lundina89ab962016-05-18 08:52:45 -0700580
581 if (muted) {
582 // TODO(henrik.lundin): We should be able to do better than this. But we
583 // will have to go through all the cases below where the audio samples may
584 // be used, and handle the muted case in some way.
585 audioFrame->Mute();
586 }
kwiberg55b97fe2016-01-28 05:22:45 -0800587
588 if (_RxVadDetection) {
589 UpdateRxVadDetection(*audioFrame);
590 }
591
592 // Convert module ID to internal VoE channel ID
593 audioFrame->id_ = VoEChannelId(audioFrame->id_);
594 // Store speech type for dead-or-alive detection
595 _outputSpeechType = audioFrame->speech_type_;
596
597 ChannelState::State state = channel_state_.Get();
598
599 if (state.rx_apm_is_enabled) {
600 int err = rx_audioproc_->ProcessStream(audioFrame);
601 if (err) {
602 LOG(LS_ERROR) << "ProcessStream() error: " << err;
603 assert(false);
Ivo Creusenae856f22015-09-17 16:30:16 +0200604 }
kwiberg55b97fe2016-01-28 05:22:45 -0800605 }
606
607 {
608 // Pass the audio buffers to an optional sink callback, before applying
609 // scaling/panning, as that applies to the mix operation.
610 // External recipients of the audio (e.g. via AudioTrack), will do their
611 // own mixing/dynamic processing.
612 rtc::CritScope cs(&_callbackCritSect);
613 if (audio_sink_) {
614 AudioSinkInterface::Data data(
615 &audioFrame->data_[0], audioFrame->samples_per_channel_,
616 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
617 audioFrame->timestamp_);
618 audio_sink_->OnData(data);
619 }
620 }
621
622 float output_gain = 1.0f;
623 float left_pan = 1.0f;
624 float right_pan = 1.0f;
625 {
626 rtc::CritScope cs(&volume_settings_critsect_);
627 output_gain = _outputGain;
628 left_pan = _panLeft;
629 right_pan = _panRight;
630 }
631
632 // Output volume scaling
633 if (output_gain < 0.99f || output_gain > 1.01f) {
634 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
635 }
636
637 // Scale left and/or right channel(s) if stereo and master balance is
638 // active
639
640 if (left_pan != 1.0f || right_pan != 1.0f) {
641 if (audioFrame->num_channels_ == 1) {
642 // Emulate stereo mode since panning is active.
643 // The mono signal is copied to both left and right channels here.
644 AudioFrameOperations::MonoToStereo(audioFrame);
645 }
646 // For true stereo mode (when we are receiving a stereo signal), no
647 // action is needed.
648
649 // Do the panning operation (the audio frame contains stereo at this
650 // stage)
651 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
652 }
653
654 // Mix decoded PCM output with file if file mixing is enabled
655 if (state.output_file_playing) {
656 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700657 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800658 }
659
660 // External media
661 if (_outputExternalMedia) {
662 rtc::CritScope cs(&_callbackCritSect);
663 const bool isStereo = (audioFrame->num_channels_ == 2);
664 if (_outputExternalMediaCallbackPtr) {
665 _outputExternalMediaCallbackPtr->Process(
666 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
667 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
668 isStereo);
669 }
670 }
671
672 // Record playout if enabled
673 {
674 rtc::CritScope cs(&_fileCritSect);
675
kwiberg5a25d952016-08-17 07:31:12 -0700676 if (_outputFileRecording && output_file_recorder_) {
677 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800678 }
679 }
680
681 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700682 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800683 _outputAudioLevel.ComputeLevel(*audioFrame);
684
685 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
686 // The first frame with a valid rtp timestamp.
687 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
688 }
689
690 if (capture_start_rtp_time_stamp_ >= 0) {
691 // audioFrame.timestamp_ should be valid from now on.
692
693 // Compute elapsed time.
694 int64_t unwrap_timestamp =
695 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
696 audioFrame->elapsed_time_ms_ =
697 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
698 (GetPlayoutFrequency() / 1000);
699
niklase@google.com470e71d2011-07-07 08:21:25 +0000700 {
kwiberg55b97fe2016-01-28 05:22:45 -0800701 rtc::CritScope lock(&ts_stats_lock_);
702 // Compute ntp time.
703 audioFrame->ntp_time_ms_ =
704 ntp_estimator_.Estimate(audioFrame->timestamp_);
705 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
706 if (audioFrame->ntp_time_ms_ > 0) {
707 // Compute |capture_start_ntp_time_ms_| so that
708 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
709 capture_start_ntp_time_ms_ =
710 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000711 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000712 }
kwiberg55b97fe2016-01-28 05:22:45 -0800713 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000714
henrik.lundin42dda502016-05-18 05:36:01 -0700715 return muted ? MixerParticipant::AudioFrameInfo::kMuted
716 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000717}
718
kwiberg55b97fe2016-01-28 05:22:45 -0800719int32_t Channel::NeededFrequency(int32_t id) const {
720 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
721 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000722
kwiberg55b97fe2016-01-28 05:22:45 -0800723 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000724
kwiberg55b97fe2016-01-28 05:22:45 -0800725 // Determine highest needed receive frequency
726 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
kwiberg55b97fe2016-01-28 05:22:45 -0800728 // Return the bigger of playout and receive frequency in the ACM.
729 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
730 highestNeeded = audio_coding_->PlayoutFrequency();
731 } else {
732 highestNeeded = receiveFrequency;
733 }
734
735 // Special case, if we're playing a file on the playout side
736 // we take that frequency into consideration as well
737 // This is not needed on sending side, since the codec will
738 // limit the spectrum anyway.
739 if (channel_state_.Get().output_file_playing) {
740 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700741 if (output_file_player_) {
742 if (output_file_player_->Frequency() > highestNeeded) {
743 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800744 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000745 }
kwiberg55b97fe2016-01-28 05:22:45 -0800746 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000747
kwiberg55b97fe2016-01-28 05:22:45 -0800748 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000749}
750
ossu5f7cfa52016-05-30 08:11:28 -0700751int32_t Channel::CreateChannel(
752 Channel*& channel,
753 int32_t channelId,
754 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700755 const VoEBase::ChannelConfig& config) {
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
solenberg88499ec2016-09-07 07:34:41 -0700760 channel = new Channel(channelId, instanceId, config);
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,
solenberg88499ec2016-09-07 07:34:41 -0700819 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800820 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100821 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700822 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100823 rtp_header_parser_(RtpHeaderParser::Create()),
824 rtp_payload_registry_(
825 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
826 rtp_receive_statistics_(
827 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
828 rtp_receiver_(
829 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100830 this,
831 this,
832 rtp_payload_registry_.get())),
833 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
834 _outputAudioLevel(),
835 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100836 // Avoid conflict with other channels by adding 1024 - 1026,
837 // won't use as much as 1024 channels.
838 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
839 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
840 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
841 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100842 _outputExternalMedia(false),
843 _inputExternalMediaCallbackPtr(NULL),
844 _outputExternalMediaCallbackPtr(NULL),
845 _timeStamp(0), // This is just an offset, RTP module will add it's own
846 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100847 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100848 playout_timestamp_rtp_(0),
849 playout_timestamp_rtcp_(0),
850 playout_delay_ms_(0),
851 _numberOfDiscardedPackets(0),
852 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100853 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
854 capture_start_rtp_time_stamp_(-1),
855 capture_start_ntp_time_ms_(-1),
856 _engineStatisticsPtr(NULL),
857 _outputMixerPtr(NULL),
858 _transmitMixerPtr(NULL),
859 _moduleProcessThreadPtr(NULL),
860 _audioDeviceModulePtr(NULL),
861 _voiceEngineObserverPtr(NULL),
862 _callbackCritSectPtr(NULL),
863 _transportPtr(NULL),
864 _rxVadObserverPtr(NULL),
865 _oldVadDecision(-1),
866 _sendFrameType(0),
867 _externalMixing(false),
868 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700869 input_mute_(false),
870 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 _panLeft(1.0f),
872 _panRight(1.0f),
873 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 _lastLocalTimeStamp(0),
875 _lastPayloadType(0),
876 _includeAudioLevelIndication(false),
877 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100878 _RxVadDetection(false),
879 _rxAgcIsEnabled(false),
880 _rxNsIsEnabled(false),
881 restored_packet_in_use_(false),
882 rtcp_observer_(new VoERtcpObserver(this)),
883 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700885 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800886 feedback_observer_proxy_(new TransportFeedbackProxy()),
887 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700888 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200889 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
890 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700891 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800892 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
893 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700894 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800895 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700896 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800897 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200898
kwiberg55b97fe2016-01-28 05:22:45 -0800899 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000900
kwiberg55b97fe2016-01-28 05:22:45 -0800901 RtpRtcp::Configuration configuration;
902 configuration.audio = true;
903 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800904 configuration.receive_statistics = rtp_receive_statistics_.get();
905 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800906 if (pacing_enabled_) {
907 configuration.paced_sender = rtp_packet_sender_proxy_.get();
908 configuration.transport_sequence_number_allocator =
909 seq_num_allocator_proxy_.get();
910 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
911 }
ivoc14d5dbe2016-07-04 07:06:55 -0700912 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200913 configuration.retransmission_rate_limiter =
914 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000915
kwiberg55b97fe2016-01-28 05:22:45 -0800916 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100917 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000918
kwiberg55b97fe2016-01-28 05:22:45 -0800919 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
920 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
921 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000922
kwiberg55b97fe2016-01-28 05:22:45 -0800923 Config audioproc_config;
924 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
925 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000926}
927
kwiberg55b97fe2016-01-28 05:22:45 -0800928Channel::~Channel() {
929 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
930 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
931 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000932
kwiberg55b97fe2016-01-28 05:22:45 -0800933 if (_outputExternalMedia) {
934 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
935 }
936 if (channel_state_.Get().input_external_media) {
937 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
938 }
939 StopSend();
940 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000941
kwiberg55b97fe2016-01-28 05:22:45 -0800942 {
943 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700944 if (input_file_player_) {
945 input_file_player_->RegisterModuleFileCallback(NULL);
946 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000947 }
kwiberg5a25d952016-08-17 07:31:12 -0700948 if (output_file_player_) {
949 output_file_player_->RegisterModuleFileCallback(NULL);
950 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800951 }
kwiberg5a25d952016-08-17 07:31:12 -0700952 if (output_file_recorder_) {
953 output_file_recorder_->RegisterModuleFileCallback(NULL);
954 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800955 }
956 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000957
kwiberg55b97fe2016-01-28 05:22:45 -0800958 // The order to safely shutdown modules in a channel is:
959 // 1. De-register callbacks in modules
960 // 2. De-register modules in process thread
961 // 3. Destroy modules
962 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
963 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
964 "~Channel() failed to de-register transport callback"
965 " (Audio coding module)");
966 }
967 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
968 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
969 "~Channel() failed to de-register VAD callback"
970 " (Audio coding module)");
971 }
972 // De-register modules in process thread
973 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000974
kwiberg55b97fe2016-01-28 05:22:45 -0800975 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000976}
977
kwiberg55b97fe2016-01-28 05:22:45 -0800978int32_t Channel::Init() {
979 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
980 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000981
kwiberg55b97fe2016-01-28 05:22:45 -0800982 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000983
kwiberg55b97fe2016-01-28 05:22:45 -0800984 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000985
kwiberg55b97fe2016-01-28 05:22:45 -0800986 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
987 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
988 "Channel::Init() must call SetEngineInformation() first");
989 return -1;
990 }
991
992 // --- Add modules to process thread (for periodic schedulation)
993
994 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
995
996 // --- ACM initialization
997
998 if (audio_coding_->InitializeReceiver() == -1) {
999 _engineStatisticsPtr->SetLastError(
1000 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1001 "Channel::Init() unable to initialize the ACM - 1");
1002 return -1;
1003 }
1004
1005 // --- RTP/RTCP module initialization
1006
1007 // Ensure that RTCP is enabled by default for the created channel.
1008 // Note that, the module will keep generating RTCP until it is explicitly
1009 // disabled by the user.
1010 // After StopListen (when no sockets exists), RTCP packets will no longer
1011 // be transmitted since the Transport object will then be invalid.
1012 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
1013 // RTCP is enabled by default.
1014 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1015 // --- Register all permanent callbacks
1016 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1017 (audio_coding_->RegisterVADCallback(this) == -1);
1018
1019 if (fail) {
1020 _engineStatisticsPtr->SetLastError(
1021 VE_CANNOT_INIT_CHANNEL, kTraceError,
1022 "Channel::Init() callbacks not registered");
1023 return -1;
1024 }
1025
1026 // --- Register all supported codecs to the receiving side of the
1027 // RTP/RTCP module
1028
1029 CodecInst codec;
1030 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1031
1032 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1033 // Open up the RTP/RTCP receiver for all supported codecs
1034 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1035 (rtp_receiver_->RegisterReceivePayload(
1036 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1037 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1038 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1039 "Channel::Init() unable to register %s "
1040 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1041 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1042 codec.rate);
1043 } else {
1044 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1045 "Channel::Init() %s (%d/%d/%" PRIuS
1046 "/%d) has been "
1047 "added to the RTP/RTCP receiver",
1048 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1049 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001050 }
1051
kwiberg55b97fe2016-01-28 05:22:45 -08001052 // Ensure that PCMU is used as default codec on the sending side
1053 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1054 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 }
1056
kwiberg55b97fe2016-01-28 05:22:45 -08001057 // Register default PT for outband 'telephone-event'
1058 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001059 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1060 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001061 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1062 "Channel::Init() failed to register outband "
1063 "'telephone-event' (%d/%d) correctly",
1064 codec.pltype, codec.plfreq);
1065 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 }
1067
kwiberg55b97fe2016-01-28 05:22:45 -08001068 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001069 if (!codec_manager_.RegisterEncoder(codec) ||
1070 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1071 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1072 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001073 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1074 "Channel::Init() failed to register CN (%d/%d) "
1075 "correctly - 1",
1076 codec.pltype, codec.plfreq);
1077 }
1078 }
kwiberg55b97fe2016-01-28 05:22:45 -08001079 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001080
kwiberg55b97fe2016-01-28 05:22:45 -08001081 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1082 LOG(LS_ERROR) << "noise_suppression()->set_level(kDefaultNsMode) failed.";
1083 return -1;
1084 }
1085 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1086 LOG(LS_ERROR) << "gain_control()->set_mode(kDefaultRxAgcMode) failed.";
1087 return -1;
1088 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001089
kwiberg55b97fe2016-01-28 05:22:45 -08001090 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001091}
1092
kwiberg55b97fe2016-01-28 05:22:45 -08001093int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1094 OutputMixer& outputMixer,
1095 voe::TransmitMixer& transmitMixer,
1096 ProcessThread& moduleProcessThread,
1097 AudioDeviceModule& audioDeviceModule,
1098 VoiceEngineObserver* voiceEngineObserver,
1099 rtc::CriticalSection* callbackCritSect) {
1100 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1101 "Channel::SetEngineInformation()");
1102 _engineStatisticsPtr = &engineStatistics;
1103 _outputMixerPtr = &outputMixer;
1104 _transmitMixerPtr = &transmitMixer,
1105 _moduleProcessThreadPtr = &moduleProcessThread;
1106 _audioDeviceModulePtr = &audioDeviceModule;
1107 _voiceEngineObserverPtr = voiceEngineObserver;
1108 _callbackCritSectPtr = callbackCritSect;
1109 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001110}
1111
kwiberg55b97fe2016-01-28 05:22:45 -08001112int32_t Channel::UpdateLocalTimeStamp() {
1113 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1114 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001115}
1116
kwibergb7f89d62016-02-17 10:04:18 -08001117void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001118 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001119 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001120}
1121
ossu29b1a8d2016-06-13 07:34:51 -07001122const rtc::scoped_refptr<AudioDecoderFactory>&
1123Channel::GetAudioDecoderFactory() const {
1124 return decoder_factory_;
1125}
1126
kwiberg55b97fe2016-01-28 05:22:45 -08001127int32_t Channel::StartPlayout() {
1128 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1129 "Channel::StartPlayout()");
1130 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001132 }
1133
1134 if (!_externalMixing) {
1135 // Add participant as candidates for mixing.
1136 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1137 _engineStatisticsPtr->SetLastError(
1138 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1139 "StartPlayout() failed to add participant to mixer");
1140 return -1;
1141 }
1142 }
1143
1144 channel_state_.SetPlaying(true);
1145 if (RegisterFilePlayingToMixer() != 0)
1146 return -1;
1147
1148 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001149}
1150
kwiberg55b97fe2016-01-28 05:22:45 -08001151int32_t Channel::StopPlayout() {
1152 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1153 "Channel::StopPlayout()");
1154 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001155 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001156 }
1157
1158 if (!_externalMixing) {
1159 // Remove participant as candidates for mixing
1160 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1161 _engineStatisticsPtr->SetLastError(
1162 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1163 "StopPlayout() failed to remove participant from mixer");
1164 return -1;
1165 }
1166 }
1167
1168 channel_state_.SetPlaying(false);
1169 _outputAudioLevel.Clear();
1170
1171 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001172}
1173
kwiberg55b97fe2016-01-28 05:22:45 -08001174int32_t Channel::StartSend() {
1175 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1176 "Channel::StartSend()");
1177 // Resume the previous sequence number which was reset by StopSend().
1178 // This needs to be done before |sending| is set to true.
1179 if (send_sequence_number_)
1180 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001181
kwiberg55b97fe2016-01-28 05:22:45 -08001182 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001183 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001184 }
1185 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001186
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001187 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001188 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1189 _engineStatisticsPtr->SetLastError(
1190 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1191 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001192 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001193 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001194 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001195 return -1;
1196 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001197
kwiberg55b97fe2016-01-28 05:22:45 -08001198 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001199}
1200
kwiberg55b97fe2016-01-28 05:22:45 -08001201int32_t Channel::StopSend() {
1202 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1203 "Channel::StopSend()");
1204 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001205 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001206 }
1207 channel_state_.SetSending(false);
1208
1209 // Store the sequence number to be able to pick up the same sequence for
1210 // the next StartSend(). This is needed for restarting device, otherwise
1211 // it might cause libSRTP to complain about packets being replayed.
1212 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1213 // CL is landed. See issue
1214 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1215 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1216
1217 // Reset sending SSRC and sequence number and triggers direct transmission
1218 // of RTCP BYE
1219 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1220 _engineStatisticsPtr->SetLastError(
1221 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1222 "StartSend() RTP/RTCP failed to stop sending");
1223 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001224 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001225
1226 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001227}
1228
kwiberg55b97fe2016-01-28 05:22:45 -08001229int32_t Channel::StartReceiving() {
1230 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1231 "Channel::StartReceiving()");
1232 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001233 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001234 }
1235 channel_state_.SetReceiving(true);
1236 _numberOfDiscardedPackets = 0;
1237 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001238}
1239
kwiberg55b97fe2016-01-28 05:22:45 -08001240int32_t Channel::StopReceiving() {
1241 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1242 "Channel::StopReceiving()");
1243 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001244 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001245 }
1246
1247 channel_state_.SetReceiving(false);
1248 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001249}
1250
kwiberg55b97fe2016-01-28 05:22:45 -08001251int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1252 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1253 "Channel::RegisterVoiceEngineObserver()");
1254 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001255
kwiberg55b97fe2016-01-28 05:22:45 -08001256 if (_voiceEngineObserverPtr) {
1257 _engineStatisticsPtr->SetLastError(
1258 VE_INVALID_OPERATION, kTraceError,
1259 "RegisterVoiceEngineObserver() observer already enabled");
1260 return -1;
1261 }
1262 _voiceEngineObserverPtr = &observer;
1263 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001264}
1265
kwiberg55b97fe2016-01-28 05:22:45 -08001266int32_t Channel::DeRegisterVoiceEngineObserver() {
1267 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1268 "Channel::DeRegisterVoiceEngineObserver()");
1269 rtc::CritScope cs(&_callbackCritSect);
1270
1271 if (!_voiceEngineObserverPtr) {
1272 _engineStatisticsPtr->SetLastError(
1273 VE_INVALID_OPERATION, kTraceWarning,
1274 "DeRegisterVoiceEngineObserver() observer already disabled");
1275 return 0;
1276 }
1277 _voiceEngineObserverPtr = NULL;
1278 return 0;
1279}
1280
1281int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001282 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001283 if (send_codec) {
1284 codec = *send_codec;
1285 return 0;
1286 }
1287 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001288}
1289
kwiberg55b97fe2016-01-28 05:22:45 -08001290int32_t Channel::GetRecCodec(CodecInst& codec) {
1291 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001292}
1293
kwiberg55b97fe2016-01-28 05:22:45 -08001294int32_t Channel::SetSendCodec(const CodecInst& codec) {
1295 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1296 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001297
kwibergc8d071e2016-04-06 12:22:38 -07001298 if (!codec_manager_.RegisterEncoder(codec) ||
1299 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001300 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1301 "SetSendCodec() failed to register codec to ACM");
1302 return -1;
1303 }
1304
1305 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1306 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1307 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1308 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1309 "SetSendCodec() failed to register codec to"
1310 " RTP/RTCP module");
1311 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001312 }
kwiberg55b97fe2016-01-28 05:22:45 -08001313 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001314
kwiberg55b97fe2016-01-28 05:22:45 -08001315 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1316 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1317 "SetSendCodec() failed to set audio packet size");
1318 return -1;
1319 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001320
kwiberg55b97fe2016-01-28 05:22:45 -08001321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001322}
1323
Ivo Creusenadf89b72015-04-29 16:03:33 +02001324void Channel::SetBitRate(int bitrate_bps) {
1325 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1326 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1327 audio_coding_->SetBitRate(bitrate_bps);
Erik Språng737336d2016-07-29 12:59:36 +02001328 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001329}
1330
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001331void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001332 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001333 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1334
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001335 // Normalizes rate to 0 - 100.
kwiberg55b97fe2016-01-28 05:22:45 -08001336 if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) !=
1337 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001338 assert(false); // This should not happen.
1339 }
1340}
1341
kwiberg55b97fe2016-01-28 05:22:45 -08001342int32_t Channel::SetVADStatus(bool enableVAD,
1343 ACMVADMode mode,
1344 bool disableDTX) {
1345 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1346 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001347 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1348 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1349 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001350 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1351 kTraceError,
1352 "SetVADStatus() failed to set VAD");
1353 return -1;
1354 }
1355 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001356}
1357
kwiberg55b97fe2016-01-28 05:22:45 -08001358int32_t Channel::GetVADStatus(bool& enabledVAD,
1359 ACMVADMode& mode,
1360 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001361 const auto* params = codec_manager_.GetStackParams();
1362 enabledVAD = params->use_cng;
1363 mode = params->vad_mode;
1364 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001365 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001366}
1367
kwiberg55b97fe2016-01-28 05:22:45 -08001368int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1369 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1370 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001371
kwiberg55b97fe2016-01-28 05:22:45 -08001372 if (channel_state_.Get().playing) {
1373 _engineStatisticsPtr->SetLastError(
1374 VE_ALREADY_PLAYING, kTraceError,
1375 "SetRecPayloadType() unable to set PT while playing");
1376 return -1;
1377 }
1378 if (channel_state_.Get().receiving) {
1379 _engineStatisticsPtr->SetLastError(
1380 VE_ALREADY_LISTENING, kTraceError,
1381 "SetRecPayloadType() unable to set PT while listening");
1382 return -1;
1383 }
1384
1385 if (codec.pltype == -1) {
1386 // De-register the selected codec (RTP/RTCP module and ACM)
1387
1388 int8_t pltype(-1);
1389 CodecInst rxCodec = codec;
1390
1391 // Get payload type for the given codec
1392 rtp_payload_registry_->ReceivePayloadType(
1393 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1394 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1395 rxCodec.pltype = pltype;
1396
1397 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1398 _engineStatisticsPtr->SetLastError(
1399 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1400 "SetRecPayloadType() RTP/RTCP-module deregistration "
1401 "failed");
1402 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001403 }
kwiberg55b97fe2016-01-28 05:22:45 -08001404 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1405 _engineStatisticsPtr->SetLastError(
1406 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1407 "SetRecPayloadType() ACM deregistration failed - 1");
1408 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001409 }
kwiberg55b97fe2016-01-28 05:22:45 -08001410 return 0;
1411 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001412
kwiberg55b97fe2016-01-28 05:22:45 -08001413 if (rtp_receiver_->RegisterReceivePayload(
1414 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1415 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1416 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001417 // TODO(kwiberg): Retrying is probably not necessary, since
1418 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001419 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001420 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001421 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1422 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1423 _engineStatisticsPtr->SetLastError(
1424 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1425 "SetRecPayloadType() RTP/RTCP-module registration failed");
1426 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001427 }
kwiberg55b97fe2016-01-28 05:22:45 -08001428 }
kwibergc8d071e2016-04-06 12:22:38 -07001429 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001430 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001431 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001432 _engineStatisticsPtr->SetLastError(
1433 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1434 "SetRecPayloadType() ACM registration failed - 1");
1435 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001436 }
kwiberg55b97fe2016-01-28 05:22:45 -08001437 }
1438 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001439}
1440
kwiberg55b97fe2016-01-28 05:22:45 -08001441int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1442 int8_t payloadType(-1);
1443 if (rtp_payload_registry_->ReceivePayloadType(
1444 codec.plname, codec.plfreq, codec.channels,
1445 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1446 _engineStatisticsPtr->SetLastError(
1447 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1448 "GetRecPayloadType() failed to retrieve RX payload type");
1449 return -1;
1450 }
1451 codec.pltype = payloadType;
1452 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001453}
1454
kwiberg55b97fe2016-01-28 05:22:45 -08001455int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1456 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1457 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001458
kwiberg55b97fe2016-01-28 05:22:45 -08001459 CodecInst codec;
1460 int32_t samplingFreqHz(-1);
1461 const size_t kMono = 1;
1462 if (frequency == kFreq32000Hz)
1463 samplingFreqHz = 32000;
1464 else if (frequency == kFreq16000Hz)
1465 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001466
kwiberg55b97fe2016-01-28 05:22:45 -08001467 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1468 _engineStatisticsPtr->SetLastError(
1469 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1470 "SetSendCNPayloadType() failed to retrieve default CN codec "
1471 "settings");
1472 return -1;
1473 }
1474
1475 // Modify the payload type (must be set to dynamic range)
1476 codec.pltype = type;
1477
kwibergc8d071e2016-04-06 12:22:38 -07001478 if (!codec_manager_.RegisterEncoder(codec) ||
1479 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001480 _engineStatisticsPtr->SetLastError(
1481 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1482 "SetSendCNPayloadType() failed to register CN to ACM");
1483 return -1;
1484 }
1485
1486 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1487 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1488 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1489 _engineStatisticsPtr->SetLastError(
1490 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1491 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1492 "module");
1493 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001494 }
kwiberg55b97fe2016-01-28 05:22:45 -08001495 }
1496 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001497}
1498
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001499int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001500 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001501 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001502
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001503 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001504 _engineStatisticsPtr->SetLastError(
1505 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001506 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001507 return -1;
1508 }
1509 return 0;
1510}
1511
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001512int Channel::SetOpusDtx(bool enable_dtx) {
1513 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1514 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001515 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001516 : audio_coding_->DisableOpusDtx();
1517 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001518 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1519 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001520 return -1;
1521 }
1522 return 0;
1523}
1524
ivoc85228d62016-07-27 04:53:47 -07001525int Channel::GetOpusDtx(bool* enabled) {
1526 int success = -1;
1527 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1528 if (encoder) {
1529 *enabled = encoder->GetDtx();
1530 success = 0;
1531 }
1532 });
1533 return success;
1534}
1535
mflodman3d7db262016-04-29 00:57:13 -07001536int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001537 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001538 "Channel::RegisterExternalTransport()");
1539
kwiberg55b97fe2016-01-28 05:22:45 -08001540 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001541 if (_externalTransport) {
1542 _engineStatisticsPtr->SetLastError(
1543 VE_INVALID_OPERATION, kTraceError,
1544 "RegisterExternalTransport() external transport already enabled");
1545 return -1;
1546 }
1547 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001548 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001549 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001550}
1551
kwiberg55b97fe2016-01-28 05:22:45 -08001552int32_t Channel::DeRegisterExternalTransport() {
1553 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1554 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001555
kwiberg55b97fe2016-01-28 05:22:45 -08001556 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001557 if (_transportPtr) {
1558 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1559 "DeRegisterExternalTransport() all transport is disabled");
1560 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001561 _engineStatisticsPtr->SetLastError(
1562 VE_INVALID_OPERATION, kTraceWarning,
1563 "DeRegisterExternalTransport() external transport already "
1564 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001565 }
1566 _externalTransport = false;
1567 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001568 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001569}
1570
mflodman3d7db262016-04-29 00:57:13 -07001571int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001572 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001573 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001574 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001575 "Channel::ReceivedRTPPacket()");
1576
1577 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001578 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001579
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001580 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001581 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1582 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1583 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001584 return -1;
1585 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001586 header.payload_type_frequency =
1587 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001588 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001589 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001590 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001591 rtp_receive_statistics_->IncomingPacket(
1592 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001593 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001594
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001595 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001596}
1597
1598bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001599 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001600 const RTPHeader& header,
1601 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001602 if (rtp_payload_registry_->IsRtx(header)) {
1603 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001604 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001605 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001606 assert(packet_length >= header.headerLength);
1607 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001608 PayloadUnion payload_specific;
1609 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 &payload_specific)) {
1611 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001612 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001613 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1614 payload_specific, in_order);
1615}
1616
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001617bool Channel::HandleRtxPacket(const uint8_t* packet,
1618 size_t packet_length,
1619 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001620 if (!rtp_payload_registry_->IsRtx(header))
1621 return false;
1622
1623 // Remove the RTX header and parse the original RTP header.
1624 if (packet_length < header.headerLength)
1625 return false;
1626 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1627 return false;
1628 if (restored_packet_in_use_) {
1629 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1630 "Multiple RTX headers detected, dropping packet");
1631 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001632 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001633 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001634 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1635 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001636 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1637 "Incoming RTX packet: invalid RTP header");
1638 return false;
1639 }
1640 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001641 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001642 restored_packet_in_use_ = false;
1643 return ret;
1644}
1645
1646bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1647 StreamStatistician* statistician =
1648 rtp_receive_statistics_->GetStatistician(header.ssrc);
1649 if (!statistician)
1650 return false;
1651 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001652}
1653
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001654bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1655 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001656 // Retransmissions are handled separately if RTX is enabled.
1657 if (rtp_payload_registry_->RtxEnabled())
1658 return false;
1659 StreamStatistician* statistician =
1660 rtp_receive_statistics_->GetStatistician(header.ssrc);
1661 if (!statistician)
1662 return false;
1663 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001664 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001665 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001666 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001667}
1668
mflodman3d7db262016-04-29 00:57:13 -07001669int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001670 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001671 "Channel::ReceivedRTCPPacket()");
1672 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001673 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001674
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001675 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001676 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001677 _engineStatisticsPtr->SetLastError(
1678 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1679 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1680 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001681
Minyue2013aec2015-05-13 14:14:42 +02001682 int64_t rtt = GetRTT(true);
1683 if (rtt == 0) {
1684 // Waiting for valid RTT.
1685 return 0;
1686 }
Erik Språng737336d2016-07-29 12:59:36 +02001687
1688 int64_t nack_window_ms = rtt;
1689 if (nack_window_ms < kMinRetransmissionWindowMs) {
1690 nack_window_ms = kMinRetransmissionWindowMs;
1691 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1692 nack_window_ms = kMaxRetransmissionWindowMs;
1693 }
1694 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1695
Minyue2013aec2015-05-13 14:14:42 +02001696 uint32_t ntp_secs = 0;
1697 uint32_t ntp_frac = 0;
1698 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001699 if (0 !=
1700 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1701 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001702 // Waiting for RTCP.
1703 return 0;
1704 }
1705
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001706 {
tommi31fc21f2016-01-21 10:37:37 -08001707 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001708 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001709 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001710 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001711}
1712
niklase@google.com470e71d2011-07-07 08:21:25 +00001713int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001714 bool loop,
1715 FileFormats format,
1716 int startPosition,
1717 float volumeScaling,
1718 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001719 const CodecInst* codecInst) {
1720 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1721 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1722 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1723 "stopPosition=%d)",
1724 fileName, loop, format, volumeScaling, startPosition,
1725 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001726
kwiberg55b97fe2016-01-28 05:22:45 -08001727 if (channel_state_.Get().output_file_playing) {
1728 _engineStatisticsPtr->SetLastError(
1729 VE_ALREADY_PLAYING, kTraceError,
1730 "StartPlayingFileLocally() is already playing");
1731 return -1;
1732 }
1733
1734 {
1735 rtc::CritScope cs(&_fileCritSect);
1736
kwiberg5a25d952016-08-17 07:31:12 -07001737 if (output_file_player_) {
1738 output_file_player_->RegisterModuleFileCallback(NULL);
1739 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001740 }
1741
kwiberg5a25d952016-08-17 07:31:12 -07001742 output_file_player_ = FilePlayer::NewFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001743 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001744
kwiberg5a25d952016-08-17 07:31:12 -07001745 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001746 _engineStatisticsPtr->SetLastError(
1747 VE_INVALID_ARGUMENT, kTraceError,
1748 "StartPlayingFileLocally() filePlayer format is not correct");
1749 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001750 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001751
kwiberg55b97fe2016-01-28 05:22:45 -08001752 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001753
kwiberg5a25d952016-08-17 07:31:12 -07001754 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001755 fileName, loop, startPosition, volumeScaling, notificationTime,
1756 stopPosition, (const CodecInst*)codecInst) != 0) {
1757 _engineStatisticsPtr->SetLastError(
1758 VE_BAD_FILE, kTraceError,
1759 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001760 output_file_player_->StopPlayingFile();
1761 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001762 return -1;
1763 }
kwiberg5a25d952016-08-17 07:31:12 -07001764 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001765 channel_state_.SetOutputFilePlaying(true);
1766 }
1767
1768 if (RegisterFilePlayingToMixer() != 0)
1769 return -1;
1770
1771 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001772}
1773
1774int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001775 FileFormats format,
1776 int startPosition,
1777 float volumeScaling,
1778 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001779 const CodecInst* codecInst) {
1780 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1781 "Channel::StartPlayingFileLocally(format=%d,"
1782 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1783 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001784
kwiberg55b97fe2016-01-28 05:22:45 -08001785 if (stream == NULL) {
1786 _engineStatisticsPtr->SetLastError(
1787 VE_BAD_FILE, kTraceError,
1788 "StartPlayingFileLocally() NULL as input stream");
1789 return -1;
1790 }
1791
1792 if (channel_state_.Get().output_file_playing) {
1793 _engineStatisticsPtr->SetLastError(
1794 VE_ALREADY_PLAYING, kTraceError,
1795 "StartPlayingFileLocally() is already playing");
1796 return -1;
1797 }
1798
1799 {
1800 rtc::CritScope cs(&_fileCritSect);
1801
1802 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001803 if (output_file_player_) {
1804 output_file_player_->RegisterModuleFileCallback(NULL);
1805 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001806 }
1807
kwiberg55b97fe2016-01-28 05:22:45 -08001808 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07001809 output_file_player_ = FilePlayer::NewFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001810 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001811
kwiberg5a25d952016-08-17 07:31:12 -07001812 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001813 _engineStatisticsPtr->SetLastError(
1814 VE_INVALID_ARGUMENT, kTraceError,
1815 "StartPlayingFileLocally() filePlayer format isnot correct");
1816 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001817 }
1818
kwiberg55b97fe2016-01-28 05:22:45 -08001819 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001820
kwiberg4ec01d92016-08-22 08:43:54 -07001821 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001822 volumeScaling, notificationTime,
1823 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001824 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1825 "StartPlayingFile() failed to "
1826 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001827 output_file_player_->StopPlayingFile();
1828 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001829 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001830 }
kwiberg5a25d952016-08-17 07:31:12 -07001831 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001832 channel_state_.SetOutputFilePlaying(true);
1833 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001834
kwiberg55b97fe2016-01-28 05:22:45 -08001835 if (RegisterFilePlayingToMixer() != 0)
1836 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001837
kwiberg55b97fe2016-01-28 05:22:45 -08001838 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001839}
1840
kwiberg55b97fe2016-01-28 05:22:45 -08001841int Channel::StopPlayingFileLocally() {
1842 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1843 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001844
kwiberg55b97fe2016-01-28 05:22:45 -08001845 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001846 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001847 }
1848
1849 {
1850 rtc::CritScope cs(&_fileCritSect);
1851
kwiberg5a25d952016-08-17 07:31:12 -07001852 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001853 _engineStatisticsPtr->SetLastError(
1854 VE_STOP_RECORDING_FAILED, kTraceError,
1855 "StopPlayingFile() could not stop playing");
1856 return -1;
1857 }
kwiberg5a25d952016-08-17 07:31:12 -07001858 output_file_player_->RegisterModuleFileCallback(NULL);
1859 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001860 channel_state_.SetOutputFilePlaying(false);
1861 }
1862 // _fileCritSect cannot be taken while calling
1863 // SetAnonymousMixibilityStatus. Refer to comments in
1864 // StartPlayingFileLocally(const char* ...) for more details.
1865 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1866 _engineStatisticsPtr->SetLastError(
1867 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1868 "StopPlayingFile() failed to stop participant from playing as"
1869 "file in the mixer");
1870 return -1;
1871 }
1872
1873 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001874}
1875
kwiberg55b97fe2016-01-28 05:22:45 -08001876int Channel::IsPlayingFileLocally() const {
1877 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001878}
1879
kwiberg55b97fe2016-01-28 05:22:45 -08001880int Channel::RegisterFilePlayingToMixer() {
1881 // Return success for not registering for file playing to mixer if:
1882 // 1. playing file before playout is started on that channel.
1883 // 2. starting playout without file playing on that channel.
1884 if (!channel_state_.Get().playing ||
1885 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001886 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001887 }
1888
1889 // |_fileCritSect| cannot be taken while calling
1890 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1891 // frames can be pulled by the mixer. Since the frames are generated from
1892 // the file, _fileCritSect will be taken. This would result in a deadlock.
1893 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1894 channel_state_.SetOutputFilePlaying(false);
1895 rtc::CritScope cs(&_fileCritSect);
1896 _engineStatisticsPtr->SetLastError(
1897 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1898 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001899 output_file_player_->StopPlayingFile();
1900 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001901 return -1;
1902 }
1903
1904 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001905}
1906
niklase@google.com470e71d2011-07-07 08:21:25 +00001907int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001908 bool loop,
1909 FileFormats format,
1910 int startPosition,
1911 float volumeScaling,
1912 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001913 const CodecInst* codecInst) {
1914 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1915 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1916 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1917 "stopPosition=%d)",
1918 fileName, loop, format, volumeScaling, startPosition,
1919 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001920
kwiberg55b97fe2016-01-28 05:22:45 -08001921 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001922
kwiberg55b97fe2016-01-28 05:22:45 -08001923 if (channel_state_.Get().input_file_playing) {
1924 _engineStatisticsPtr->SetLastError(
1925 VE_ALREADY_PLAYING, kTraceWarning,
1926 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001927 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001928 }
1929
1930 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001931 if (input_file_player_) {
1932 input_file_player_->RegisterModuleFileCallback(NULL);
1933 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001934 }
1935
1936 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07001937 input_file_player_ = FilePlayer::NewFilePlayer(_inputFilePlayerId,
1938 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001939
kwiberg5a25d952016-08-17 07:31:12 -07001940 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001941 _engineStatisticsPtr->SetLastError(
1942 VE_INVALID_ARGUMENT, kTraceError,
1943 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1944 return -1;
1945 }
1946
1947 const uint32_t notificationTime(0);
1948
kwiberg5a25d952016-08-17 07:31:12 -07001949 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001950 fileName, loop, startPosition, volumeScaling, notificationTime,
1951 stopPosition, (const CodecInst*)codecInst) != 0) {
1952 _engineStatisticsPtr->SetLastError(
1953 VE_BAD_FILE, kTraceError,
1954 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001955 input_file_player_->StopPlayingFile();
1956 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001957 return -1;
1958 }
kwiberg5a25d952016-08-17 07:31:12 -07001959 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001960 channel_state_.SetInputFilePlaying(true);
1961
1962 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001963}
1964
1965int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001966 FileFormats format,
1967 int startPosition,
1968 float volumeScaling,
1969 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001970 const CodecInst* codecInst) {
1971 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1972 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1973 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1974 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001975
kwiberg55b97fe2016-01-28 05:22:45 -08001976 if (stream == NULL) {
1977 _engineStatisticsPtr->SetLastError(
1978 VE_BAD_FILE, kTraceError,
1979 "StartPlayingFileAsMicrophone NULL as input stream");
1980 return -1;
1981 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001982
kwiberg55b97fe2016-01-28 05:22:45 -08001983 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001984
kwiberg55b97fe2016-01-28 05:22:45 -08001985 if (channel_state_.Get().input_file_playing) {
1986 _engineStatisticsPtr->SetLastError(
1987 VE_ALREADY_PLAYING, kTraceWarning,
1988 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001989 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001990 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001991
kwiberg55b97fe2016-01-28 05:22:45 -08001992 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001993 if (input_file_player_) {
1994 input_file_player_->RegisterModuleFileCallback(NULL);
1995 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001996 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001997
kwiberg55b97fe2016-01-28 05:22:45 -08001998 // Create the instance
kwiberg5a25d952016-08-17 07:31:12 -07001999 input_file_player_ = FilePlayer::NewFilePlayer(_inputFilePlayerId,
2000 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002001
kwiberg5a25d952016-08-17 07:31:12 -07002002 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002003 _engineStatisticsPtr->SetLastError(
2004 VE_INVALID_ARGUMENT, kTraceError,
2005 "StartPlayingInputFile() filePlayer format isnot correct");
2006 return -1;
2007 }
2008
2009 const uint32_t notificationTime(0);
2010
kwiberg4ec01d92016-08-22 08:43:54 -07002011 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2012 notificationTime, stopPosition,
2013 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002014 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2015 "StartPlayingFile() failed to start "
2016 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002017 input_file_player_->StopPlayingFile();
2018 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002019 return -1;
2020 }
2021
kwiberg5a25d952016-08-17 07:31:12 -07002022 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002023 channel_state_.SetInputFilePlaying(true);
2024
2025 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002026}
2027
kwiberg55b97fe2016-01-28 05:22:45 -08002028int Channel::StopPlayingFileAsMicrophone() {
2029 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2030 "Channel::StopPlayingFileAsMicrophone()");
2031
2032 rtc::CritScope cs(&_fileCritSect);
2033
2034 if (!channel_state_.Get().input_file_playing) {
2035 return 0;
2036 }
2037
kwiberg5a25d952016-08-17 07:31:12 -07002038 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002039 _engineStatisticsPtr->SetLastError(
2040 VE_STOP_RECORDING_FAILED, kTraceError,
2041 "StopPlayingFile() could not stop playing");
2042 return -1;
2043 }
kwiberg5a25d952016-08-17 07:31:12 -07002044 input_file_player_->RegisterModuleFileCallback(NULL);
2045 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002046 channel_state_.SetInputFilePlaying(false);
2047
2048 return 0;
2049}
2050
2051int Channel::IsPlayingFileAsMicrophone() const {
2052 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002053}
2054
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002055int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002056 const CodecInst* codecInst) {
2057 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2058 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002059
kwiberg55b97fe2016-01-28 05:22:45 -08002060 if (_outputFileRecording) {
2061 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2062 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002063 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002064 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002065
kwiberg55b97fe2016-01-28 05:22:45 -08002066 FileFormats format;
2067 const uint32_t notificationTime(0); // Not supported in VoE
2068 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002069
kwiberg55b97fe2016-01-28 05:22:45 -08002070 if ((codecInst != NULL) &&
2071 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2072 _engineStatisticsPtr->SetLastError(
2073 VE_BAD_ARGUMENT, kTraceError,
2074 "StartRecordingPlayout() invalid compression");
2075 return (-1);
2076 }
2077 if (codecInst == NULL) {
2078 format = kFileFormatPcm16kHzFile;
2079 codecInst = &dummyCodec;
2080 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2081 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2082 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2083 format = kFileFormatWavFile;
2084 } else {
2085 format = kFileFormatCompressedFile;
2086 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002087
kwiberg55b97fe2016-01-28 05:22:45 -08002088 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002089
kwiberg55b97fe2016-01-28 05:22:45 -08002090 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002091 if (output_file_recorder_) {
2092 output_file_recorder_->RegisterModuleFileCallback(NULL);
2093 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002094 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002095
kwiberg5a25d952016-08-17 07:31:12 -07002096 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002097 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002098 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002099 _engineStatisticsPtr->SetLastError(
2100 VE_INVALID_ARGUMENT, kTraceError,
2101 "StartRecordingPlayout() fileRecorder format isnot correct");
2102 return -1;
2103 }
2104
kwiberg5a25d952016-08-17 07:31:12 -07002105 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002106 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2107 _engineStatisticsPtr->SetLastError(
2108 VE_BAD_FILE, kTraceError,
2109 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002110 output_file_recorder_->StopRecording();
2111 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002112 return -1;
2113 }
kwiberg5a25d952016-08-17 07:31:12 -07002114 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002115 _outputFileRecording = true;
2116
2117 return 0;
2118}
2119
2120int Channel::StartRecordingPlayout(OutStream* stream,
2121 const CodecInst* codecInst) {
2122 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2123 "Channel::StartRecordingPlayout()");
2124
2125 if (_outputFileRecording) {
2126 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2127 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002128 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002129 }
2130
2131 FileFormats format;
2132 const uint32_t notificationTime(0); // Not supported in VoE
2133 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2134
2135 if (codecInst != NULL && codecInst->channels != 1) {
2136 _engineStatisticsPtr->SetLastError(
2137 VE_BAD_ARGUMENT, kTraceError,
2138 "StartRecordingPlayout() invalid compression");
2139 return (-1);
2140 }
2141 if (codecInst == NULL) {
2142 format = kFileFormatPcm16kHzFile;
2143 codecInst = &dummyCodec;
2144 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2145 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2146 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2147 format = kFileFormatWavFile;
2148 } else {
2149 format = kFileFormatCompressedFile;
2150 }
2151
2152 rtc::CritScope cs(&_fileCritSect);
2153
2154 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002155 if (output_file_recorder_) {
2156 output_file_recorder_->RegisterModuleFileCallback(NULL);
2157 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002158 }
2159
kwiberg5a25d952016-08-17 07:31:12 -07002160 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002161 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002162 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002163 _engineStatisticsPtr->SetLastError(
2164 VE_INVALID_ARGUMENT, kTraceError,
2165 "StartRecordingPlayout() fileRecorder format isnot correct");
2166 return -1;
2167 }
2168
kwiberg4ec01d92016-08-22 08:43:54 -07002169 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002170 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002171 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2172 "StartRecordingPlayout() failed to "
2173 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002174 output_file_recorder_->StopRecording();
2175 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002176 return -1;
2177 }
2178
kwiberg5a25d952016-08-17 07:31:12 -07002179 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002180 _outputFileRecording = true;
2181
2182 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002183}
2184
kwiberg55b97fe2016-01-28 05:22:45 -08002185int Channel::StopRecordingPlayout() {
2186 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2187 "Channel::StopRecordingPlayout()");
2188
2189 if (!_outputFileRecording) {
2190 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2191 "StopRecordingPlayout() isnot recording");
2192 return -1;
2193 }
2194
2195 rtc::CritScope cs(&_fileCritSect);
2196
kwiberg5a25d952016-08-17 07:31:12 -07002197 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002198 _engineStatisticsPtr->SetLastError(
2199 VE_STOP_RECORDING_FAILED, kTraceError,
2200 "StopRecording() could not stop recording");
2201 return (-1);
2202 }
kwiberg5a25d952016-08-17 07:31:12 -07002203 output_file_recorder_->RegisterModuleFileCallback(NULL);
2204 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002205 _outputFileRecording = false;
2206
2207 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002208}
2209
kwiberg55b97fe2016-01-28 05:22:45 -08002210void Channel::SetMixWithMicStatus(bool mix) {
2211 rtc::CritScope cs(&_fileCritSect);
2212 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002213}
2214
kwiberg55b97fe2016-01-28 05:22:45 -08002215int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2216 int8_t currentLevel = _outputAudioLevel.Level();
2217 level = static_cast<int32_t>(currentLevel);
2218 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002219}
2220
kwiberg55b97fe2016-01-28 05:22:45 -08002221int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2222 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2223 level = static_cast<int32_t>(currentLevel);
2224 return 0;
2225}
2226
solenberg1c2af8e2016-03-24 10:36:00 -07002227int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002228 rtc::CritScope cs(&volume_settings_critsect_);
2229 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002230 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002231 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002232 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002233}
2234
solenberg1c2af8e2016-03-24 10:36:00 -07002235bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002236 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002237 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002238}
2239
kwiberg55b97fe2016-01-28 05:22:45 -08002240int Channel::SetOutputVolumePan(float left, float right) {
2241 rtc::CritScope cs(&volume_settings_critsect_);
2242 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002243 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002244 _panLeft = left;
2245 _panRight = right;
2246 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002247}
2248
kwiberg55b97fe2016-01-28 05:22:45 -08002249int Channel::GetOutputVolumePan(float& left, float& right) const {
2250 rtc::CritScope cs(&volume_settings_critsect_);
2251 left = _panLeft;
2252 right = _panRight;
2253 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002254}
2255
kwiberg55b97fe2016-01-28 05:22:45 -08002256int Channel::SetChannelOutputVolumeScaling(float scaling) {
2257 rtc::CritScope cs(&volume_settings_critsect_);
2258 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002259 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002260 _outputGain = scaling;
2261 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002262}
2263
kwiberg55b97fe2016-01-28 05:22:45 -08002264int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2265 rtc::CritScope cs(&volume_settings_critsect_);
2266 scaling = _outputGain;
2267 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002268}
2269
solenberg8842c3e2016-03-11 03:06:41 -08002270int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002271 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002272 "Channel::SendTelephoneEventOutband(...)");
2273 RTC_DCHECK_LE(0, event);
2274 RTC_DCHECK_GE(255, event);
2275 RTC_DCHECK_LE(0, duration_ms);
2276 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002277 if (!Sending()) {
2278 return -1;
2279 }
solenberg8842c3e2016-03-11 03:06:41 -08002280 if (_rtpRtcpModule->SendTelephoneEventOutband(
2281 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002282 _engineStatisticsPtr->SetLastError(
2283 VE_SEND_DTMF_FAILED, kTraceWarning,
2284 "SendTelephoneEventOutband() failed to send event");
2285 return -1;
2286 }
2287 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002288}
2289
solenberg31642aa2016-03-14 08:00:37 -07002290int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002291 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002292 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002293 RTC_DCHECK_LE(0, payload_type);
2294 RTC_DCHECK_GE(127, payload_type);
2295 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002296 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002297 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002298 memcpy(codec.plname, "telephone-event", 16);
2299 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2300 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2301 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2302 _engineStatisticsPtr->SetLastError(
2303 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2304 "SetSendTelephoneEventPayloadType() failed to register send"
2305 "payload type");
2306 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002307 }
kwiberg55b97fe2016-01-28 05:22:45 -08002308 }
kwiberg55b97fe2016-01-28 05:22:45 -08002309 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002310}
2311
kwiberg55b97fe2016-01-28 05:22:45 -08002312int Channel::UpdateRxVadDetection(AudioFrame& audioFrame) {
2313 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2314 "Channel::UpdateRxVadDetection()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002315
kwiberg55b97fe2016-01-28 05:22:45 -08002316 int vadDecision = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002317
kwiberg55b97fe2016-01-28 05:22:45 -08002318 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive) ? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002319
kwiberg55b97fe2016-01-28 05:22:45 -08002320 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr) {
2321 OnRxVadDetected(vadDecision);
2322 _oldVadDecision = vadDecision;
2323 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002324
kwiberg55b97fe2016-01-28 05:22:45 -08002325 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2326 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2327 vadDecision);
2328 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002329}
2330
kwiberg55b97fe2016-01-28 05:22:45 -08002331int Channel::RegisterRxVadObserver(VoERxVadCallback& observer) {
2332 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2333 "Channel::RegisterRxVadObserver()");
2334 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002335
kwiberg55b97fe2016-01-28 05:22:45 -08002336 if (_rxVadObserverPtr) {
2337 _engineStatisticsPtr->SetLastError(
2338 VE_INVALID_OPERATION, kTraceError,
2339 "RegisterRxVadObserver() observer already enabled");
2340 return -1;
2341 }
2342 _rxVadObserverPtr = &observer;
2343 _RxVadDetection = true;
2344 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002345}
2346
kwiberg55b97fe2016-01-28 05:22:45 -08002347int Channel::DeRegisterRxVadObserver() {
2348 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2349 "Channel::DeRegisterRxVadObserver()");
2350 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002351
kwiberg55b97fe2016-01-28 05:22:45 -08002352 if (!_rxVadObserverPtr) {
2353 _engineStatisticsPtr->SetLastError(
2354 VE_INVALID_OPERATION, kTraceWarning,
2355 "DeRegisterRxVadObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00002356 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002357 }
2358 _rxVadObserverPtr = NULL;
2359 _RxVadDetection = false;
2360 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002361}
2362
kwiberg55b97fe2016-01-28 05:22:45 -08002363int Channel::VoiceActivityIndicator(int& activity) {
2364 activity = _sendFrameType;
2365 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002366}
2367
2368#ifdef WEBRTC_VOICE_ENGINE_AGC
2369
kwiberg55b97fe2016-01-28 05:22:45 -08002370int Channel::SetRxAgcStatus(bool enable, AgcModes mode) {
2371 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2372 "Channel::SetRxAgcStatus(enable=%d, mode=%d)", (int)enable,
2373 (int)mode);
niklase@google.com470e71d2011-07-07 08:21:25 +00002374
kwiberg55b97fe2016-01-28 05:22:45 -08002375 GainControl::Mode agcMode = kDefaultRxAgcMode;
2376 switch (mode) {
2377 case kAgcDefault:
2378 break;
2379 case kAgcUnchanged:
2380 agcMode = rx_audioproc_->gain_control()->mode();
2381 break;
2382 case kAgcFixedDigital:
2383 agcMode = GainControl::kFixedDigital;
2384 break;
2385 case kAgcAdaptiveDigital:
2386 agcMode = GainControl::kAdaptiveDigital;
2387 break;
2388 default:
2389 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
2390 "SetRxAgcStatus() invalid Agc mode");
2391 return -1;
2392 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002393
kwiberg55b97fe2016-01-28 05:22:45 -08002394 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0) {
2395 _engineStatisticsPtr->SetLastError(
2396 VE_APM_ERROR, kTraceError, "SetRxAgcStatus() failed to set Agc mode");
2397 return -1;
2398 }
2399 if (rx_audioproc_->gain_control()->Enable(enable) != 0) {
2400 _engineStatisticsPtr->SetLastError(
2401 VE_APM_ERROR, kTraceError, "SetRxAgcStatus() failed to set Agc state");
2402 return -1;
2403 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002404
kwiberg55b97fe2016-01-28 05:22:45 -08002405 _rxAgcIsEnabled = enable;
2406 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002407
kwiberg55b97fe2016-01-28 05:22:45 -08002408 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002409}
2410
kwiberg55b97fe2016-01-28 05:22:45 -08002411int Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode) {
2412 bool enable = rx_audioproc_->gain_control()->is_enabled();
2413 GainControl::Mode agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002414
kwiberg55b97fe2016-01-28 05:22:45 -08002415 enabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002416
kwiberg55b97fe2016-01-28 05:22:45 -08002417 switch (agcMode) {
2418 case GainControl::kFixedDigital:
2419 mode = kAgcFixedDigital;
2420 break;
2421 case GainControl::kAdaptiveDigital:
2422 mode = kAgcAdaptiveDigital;
2423 break;
2424 default:
2425 _engineStatisticsPtr->SetLastError(VE_APM_ERROR, kTraceError,
2426 "GetRxAgcStatus() invalid Agc mode");
2427 return -1;
2428 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002429
kwiberg55b97fe2016-01-28 05:22:45 -08002430 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002431}
2432
kwiberg55b97fe2016-01-28 05:22:45 -08002433int Channel::SetRxAgcConfig(AgcConfig config) {
2434 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2435 "Channel::SetRxAgcConfig()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002436
kwiberg55b97fe2016-01-28 05:22:45 -08002437 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
2438 config.targetLeveldBOv) != 0) {
2439 _engineStatisticsPtr->SetLastError(
2440 VE_APM_ERROR, kTraceError,
2441 "SetRxAgcConfig() failed to set target peak |level|"
2442 "(or envelope) of the Agc");
2443 return -1;
2444 }
2445 if (rx_audioproc_->gain_control()->set_compression_gain_db(
2446 config.digitalCompressionGaindB) != 0) {
2447 _engineStatisticsPtr->SetLastError(
2448 VE_APM_ERROR, kTraceError,
2449 "SetRxAgcConfig() failed to set the range in |gain| the"
2450 " digital compression stage may apply");
2451 return -1;
2452 }
2453 if (rx_audioproc_->gain_control()->enable_limiter(config.limiterEnable) !=
2454 0) {
2455 _engineStatisticsPtr->SetLastError(
2456 VE_APM_ERROR, kTraceError,
2457 "SetRxAgcConfig() failed to set hard limiter to the signal");
2458 return -1;
2459 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002460
kwiberg55b97fe2016-01-28 05:22:45 -08002461 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002462}
2463
kwiberg55b97fe2016-01-28 05:22:45 -08002464int Channel::GetRxAgcConfig(AgcConfig& config) {
2465 config.targetLeveldBOv = rx_audioproc_->gain_control()->target_level_dbfs();
2466 config.digitalCompressionGaindB =
2467 rx_audioproc_->gain_control()->compression_gain_db();
2468 config.limiterEnable = rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002469
kwiberg55b97fe2016-01-28 05:22:45 -08002470 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002471}
2472
kwiberg55b97fe2016-01-28 05:22:45 -08002473#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
niklase@google.com470e71d2011-07-07 08:21:25 +00002474
2475#ifdef WEBRTC_VOICE_ENGINE_NR
2476
kwiberg55b97fe2016-01-28 05:22:45 -08002477int Channel::SetRxNsStatus(bool enable, NsModes mode) {
2478 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2479 "Channel::SetRxNsStatus(enable=%d, mode=%d)", (int)enable,
2480 (int)mode);
niklase@google.com470e71d2011-07-07 08:21:25 +00002481
kwiberg55b97fe2016-01-28 05:22:45 -08002482 NoiseSuppression::Level nsLevel = kDefaultNsMode;
2483 switch (mode) {
2484 case kNsDefault:
2485 break;
2486 case kNsUnchanged:
2487 nsLevel = rx_audioproc_->noise_suppression()->level();
2488 break;
2489 case kNsConference:
2490 nsLevel = NoiseSuppression::kHigh;
2491 break;
2492 case kNsLowSuppression:
2493 nsLevel = NoiseSuppression::kLow;
2494 break;
2495 case kNsModerateSuppression:
2496 nsLevel = NoiseSuppression::kModerate;
2497 break;
2498 case kNsHighSuppression:
2499 nsLevel = NoiseSuppression::kHigh;
2500 break;
2501 case kNsVeryHighSuppression:
2502 nsLevel = NoiseSuppression::kVeryHigh;
2503 break;
2504 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002505
kwiberg55b97fe2016-01-28 05:22:45 -08002506 if (rx_audioproc_->noise_suppression()->set_level(nsLevel) != 0) {
2507 _engineStatisticsPtr->SetLastError(
2508 VE_APM_ERROR, kTraceError, "SetRxNsStatus() failed to set NS level");
2509 return -1;
2510 }
2511 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0) {
2512 _engineStatisticsPtr->SetLastError(
2513 VE_APM_ERROR, kTraceError, "SetRxNsStatus() failed to set NS state");
2514 return -1;
2515 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002516
kwiberg55b97fe2016-01-28 05:22:45 -08002517 _rxNsIsEnabled = enable;
2518 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002519
kwiberg55b97fe2016-01-28 05:22:45 -08002520 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002521}
2522
kwiberg55b97fe2016-01-28 05:22:45 -08002523int Channel::GetRxNsStatus(bool& enabled, NsModes& mode) {
2524 bool enable = rx_audioproc_->noise_suppression()->is_enabled();
2525 NoiseSuppression::Level ncLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002526
kwiberg55b97fe2016-01-28 05:22:45 -08002527 enabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002528
kwiberg55b97fe2016-01-28 05:22:45 -08002529 switch (ncLevel) {
2530 case NoiseSuppression::kLow:
2531 mode = kNsLowSuppression;
2532 break;
2533 case NoiseSuppression::kModerate:
2534 mode = kNsModerateSuppression;
2535 break;
2536 case NoiseSuppression::kHigh:
2537 mode = kNsHighSuppression;
2538 break;
2539 case NoiseSuppression::kVeryHigh:
2540 mode = kNsVeryHighSuppression;
2541 break;
2542 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002543
kwiberg55b97fe2016-01-28 05:22:45 -08002544 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002545}
2546
kwiberg55b97fe2016-01-28 05:22:45 -08002547#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
niklase@google.com470e71d2011-07-07 08:21:25 +00002548
kwiberg55b97fe2016-01-28 05:22:45 -08002549int Channel::SetLocalSSRC(unsigned int ssrc) {
2550 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2551 "Channel::SetLocalSSRC()");
2552 if (channel_state_.Get().sending) {
2553 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2554 "SetLocalSSRC() already sending");
2555 return -1;
2556 }
2557 _rtpRtcpModule->SetSSRC(ssrc);
2558 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002559}
2560
kwiberg55b97fe2016-01-28 05:22:45 -08002561int Channel::GetLocalSSRC(unsigned int& ssrc) {
2562 ssrc = _rtpRtcpModule->SSRC();
2563 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002564}
2565
kwiberg55b97fe2016-01-28 05:22:45 -08002566int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2567 ssrc = rtp_receiver_->SSRC();
2568 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002569}
2570
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002571int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002572 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002573 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002574}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002575
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002576int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2577 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002578 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2579 if (enable &&
2580 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2581 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002582 return -1;
2583 }
2584 return 0;
2585}
2586
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002587int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2588 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2589}
2590
2591int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2592 rtp_header_parser_->DeregisterRtpHeaderExtension(
2593 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002594 if (enable &&
2595 !rtp_header_parser_->RegisterRtpHeaderExtension(
2596 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002597 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002598 }
2599 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002600}
2601
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002602void Channel::EnableSendTransportSequenceNumber(int id) {
2603 int ret =
2604 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2605 RTC_DCHECK_EQ(0, ret);
2606}
2607
stefan3313ec92016-01-21 06:32:43 -08002608void Channel::EnableReceiveTransportSequenceNumber(int id) {
2609 rtp_header_parser_->DeregisterRtpHeaderExtension(
2610 kRtpExtensionTransportSequenceNumber);
2611 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2612 kRtpExtensionTransportSequenceNumber, id);
2613 RTC_DCHECK(ret);
2614}
2615
stefanbba9dec2016-02-01 04:39:55 -08002616void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002617 RtpPacketSender* rtp_packet_sender,
2618 TransportFeedbackObserver* transport_feedback_observer,
2619 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002620 RTC_DCHECK(rtp_packet_sender);
2621 RTC_DCHECK(transport_feedback_observer);
2622 RTC_DCHECK(packet_router && !packet_router_);
2623 feedback_observer_proxy_->SetTransportFeedbackObserver(
2624 transport_feedback_observer);
2625 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2626 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2627 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002628 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002629 packet_router_ = packet_router;
2630}
2631
stefanbba9dec2016-02-01 04:39:55 -08002632void Channel::RegisterReceiverCongestionControlObjects(
2633 PacketRouter* packet_router) {
2634 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002635 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002636 packet_router_ = packet_router;
2637}
2638
2639void Channel::ResetCongestionControlObjects() {
2640 RTC_DCHECK(packet_router_);
2641 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2642 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2643 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002644 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002645 packet_router_ = nullptr;
2646 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2647}
2648
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002649void Channel::SetRTCPStatus(bool enable) {
2650 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2651 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002652 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002653}
2654
kwiberg55b97fe2016-01-28 05:22:45 -08002655int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002656 RtcpMode method = _rtpRtcpModule->RTCP();
2657 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002658 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002659}
2660
kwiberg55b97fe2016-01-28 05:22:45 -08002661int Channel::SetRTCP_CNAME(const char cName[256]) {
2662 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2663 "Channel::SetRTCP_CNAME()");
2664 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2665 _engineStatisticsPtr->SetLastError(
2666 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2667 "SetRTCP_CNAME() failed to set RTCP CNAME");
2668 return -1;
2669 }
2670 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002671}
2672
kwiberg55b97fe2016-01-28 05:22:45 -08002673int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2674 if (cName == NULL) {
2675 _engineStatisticsPtr->SetLastError(
2676 VE_INVALID_ARGUMENT, kTraceError,
2677 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2678 return -1;
2679 }
2680 char cname[RTCP_CNAME_SIZE];
2681 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2682 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2683 _engineStatisticsPtr->SetLastError(
2684 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2685 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2686 return -1;
2687 }
2688 strcpy(cName, cname);
2689 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002690}
2691
kwiberg55b97fe2016-01-28 05:22:45 -08002692int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2693 unsigned int& NTPLow,
2694 unsigned int& timestamp,
2695 unsigned int& playoutTimestamp,
2696 unsigned int* jitter,
2697 unsigned short* fractionLost) {
2698 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002699
kwiberg55b97fe2016-01-28 05:22:45 -08002700 RTCPSenderInfo senderInfo;
2701 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2702 _engineStatisticsPtr->SetLastError(
2703 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2704 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2705 "side");
2706 return -1;
2707 }
2708
2709 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2710 // and octet count)
2711 NTPHigh = senderInfo.NTPseconds;
2712 NTPLow = senderInfo.NTPfraction;
2713 timestamp = senderInfo.RTPtimeStamp;
2714
2715 // --- Locally derived information
2716
2717 // This value is updated on each incoming RTCP packet (0 when no packet
2718 // has been received)
2719 playoutTimestamp = playout_timestamp_rtcp_;
2720
2721 if (NULL != jitter || NULL != fractionLost) {
2722 // Get all RTCP receiver report blocks that have been received on this
2723 // channel. If we receive RTP packets from a remote source we know the
2724 // remote SSRC and use the report block from him.
2725 // Otherwise use the first report block.
2726 std::vector<RTCPReportBlock> remote_stats;
2727 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2728 remote_stats.empty()) {
2729 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2730 "GetRemoteRTCPData() failed to measure statistics due"
2731 " to lack of received RTP and/or RTCP packets");
2732 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002733 }
2734
kwiberg55b97fe2016-01-28 05:22:45 -08002735 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2736 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2737 for (; it != remote_stats.end(); ++it) {
2738 if (it->remoteSSRC == remoteSSRC)
2739 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002740 }
kwiberg55b97fe2016-01-28 05:22:45 -08002741
2742 if (it == remote_stats.end()) {
2743 // If we have not received any RTCP packets from this SSRC it probably
2744 // means that we have not received any RTP packets.
2745 // Use the first received report block instead.
2746 it = remote_stats.begin();
2747 remoteSSRC = it->remoteSSRC;
2748 }
2749
2750 if (jitter) {
2751 *jitter = it->jitter;
2752 }
2753
2754 if (fractionLost) {
2755 *fractionLost = it->fractionLost;
2756 }
2757 }
2758 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002759}
2760
kwiberg55b97fe2016-01-28 05:22:45 -08002761int Channel::SendApplicationDefinedRTCPPacket(
2762 unsigned char subType,
2763 unsigned int name,
2764 const char* data,
2765 unsigned short dataLengthInBytes) {
2766 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2767 "Channel::SendApplicationDefinedRTCPPacket()");
2768 if (!channel_state_.Get().sending) {
2769 _engineStatisticsPtr->SetLastError(
2770 VE_NOT_SENDING, kTraceError,
2771 "SendApplicationDefinedRTCPPacket() not sending");
2772 return -1;
2773 }
2774 if (NULL == data) {
2775 _engineStatisticsPtr->SetLastError(
2776 VE_INVALID_ARGUMENT, kTraceError,
2777 "SendApplicationDefinedRTCPPacket() invalid data value");
2778 return -1;
2779 }
2780 if (dataLengthInBytes % 4 != 0) {
2781 _engineStatisticsPtr->SetLastError(
2782 VE_INVALID_ARGUMENT, kTraceError,
2783 "SendApplicationDefinedRTCPPacket() invalid length value");
2784 return -1;
2785 }
2786 RtcpMode status = _rtpRtcpModule->RTCP();
2787 if (status == RtcpMode::kOff) {
2788 _engineStatisticsPtr->SetLastError(
2789 VE_RTCP_ERROR, kTraceError,
2790 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2791 return -1;
2792 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002793
kwiberg55b97fe2016-01-28 05:22:45 -08002794 // Create and schedule the RTCP APP packet for transmission
2795 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2796 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2797 _engineStatisticsPtr->SetLastError(
2798 VE_SEND_ERROR, kTraceError,
2799 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2800 return -1;
2801 }
2802 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002803}
2804
kwiberg55b97fe2016-01-28 05:22:45 -08002805int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2806 unsigned int& maxJitterMs,
2807 unsigned int& discardedPackets) {
2808 // The jitter statistics is updated for each received RTP packet and is
2809 // based on received packets.
2810 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2811 // If RTCP is off, there is no timed thread in the RTCP module regularly
2812 // generating new stats, trigger the update manually here instead.
2813 StreamStatistician* statistician =
2814 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2815 if (statistician) {
2816 // Don't use returned statistics, use data from proxy instead so that
2817 // max jitter can be fetched atomically.
2818 RtcpStatistics s;
2819 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002820 }
kwiberg55b97fe2016-01-28 05:22:45 -08002821 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002822
kwiberg55b97fe2016-01-28 05:22:45 -08002823 ChannelStatistics stats = statistics_proxy_->GetStats();
2824 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2825 if (playoutFrequency > 0) {
2826 // Scale RTP statistics given the current playout frequency
2827 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2828 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2829 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002830
kwiberg55b97fe2016-01-28 05:22:45 -08002831 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002832
kwiberg55b97fe2016-01-28 05:22:45 -08002833 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002834}
2835
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002836int Channel::GetRemoteRTCPReportBlocks(
2837 std::vector<ReportBlock>* report_blocks) {
2838 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002839 _engineStatisticsPtr->SetLastError(
2840 VE_INVALID_ARGUMENT, kTraceError,
2841 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002842 return -1;
2843 }
2844
2845 // Get the report blocks from the latest received RTCP Sender or Receiver
2846 // Report. Each element in the vector contains the sender's SSRC and a
2847 // report block according to RFC 3550.
2848 std::vector<RTCPReportBlock> rtcp_report_blocks;
2849 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002850 return -1;
2851 }
2852
2853 if (rtcp_report_blocks.empty())
2854 return 0;
2855
2856 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2857 for (; it != rtcp_report_blocks.end(); ++it) {
2858 ReportBlock report_block;
2859 report_block.sender_SSRC = it->remoteSSRC;
2860 report_block.source_SSRC = it->sourceSSRC;
2861 report_block.fraction_lost = it->fractionLost;
2862 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2863 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2864 report_block.interarrival_jitter = it->jitter;
2865 report_block.last_SR_timestamp = it->lastSR;
2866 report_block.delay_since_last_SR = it->delaySinceLastSR;
2867 report_blocks->push_back(report_block);
2868 }
2869 return 0;
2870}
2871
kwiberg55b97fe2016-01-28 05:22:45 -08002872int Channel::GetRTPStatistics(CallStatistics& stats) {
2873 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002874
kwiberg55b97fe2016-01-28 05:22:45 -08002875 // The jitter statistics is updated for each received RTP packet and is
2876 // based on received packets.
2877 RtcpStatistics statistics;
2878 StreamStatistician* statistician =
2879 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002880 if (statistician) {
2881 statistician->GetStatistics(&statistics,
2882 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002883 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002884
kwiberg55b97fe2016-01-28 05:22:45 -08002885 stats.fractionLost = statistics.fraction_lost;
2886 stats.cumulativeLost = statistics.cumulative_lost;
2887 stats.extendedMax = statistics.extended_max_sequence_number;
2888 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002889
kwiberg55b97fe2016-01-28 05:22:45 -08002890 // --- RTT
2891 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002892
kwiberg55b97fe2016-01-28 05:22:45 -08002893 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002894
kwiberg55b97fe2016-01-28 05:22:45 -08002895 size_t bytesSent(0);
2896 uint32_t packetsSent(0);
2897 size_t bytesReceived(0);
2898 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002899
kwiberg55b97fe2016-01-28 05:22:45 -08002900 if (statistician) {
2901 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2902 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002903
kwiberg55b97fe2016-01-28 05:22:45 -08002904 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2905 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2906 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2907 " output will not be complete");
2908 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002909
kwiberg55b97fe2016-01-28 05:22:45 -08002910 stats.bytesSent = bytesSent;
2911 stats.packetsSent = packetsSent;
2912 stats.bytesReceived = bytesReceived;
2913 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002914
kwiberg55b97fe2016-01-28 05:22:45 -08002915 // --- Timestamps
2916 {
2917 rtc::CritScope lock(&ts_stats_lock_);
2918 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2919 }
2920 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002921}
2922
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002923int Channel::SetCodecFECStatus(bool enable) {
2924 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2925 "Channel::SetCodecFECStatus()");
2926
kwibergc8d071e2016-04-06 12:22:38 -07002927 if (!codec_manager_.SetCodecFEC(enable) ||
2928 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002929 _engineStatisticsPtr->SetLastError(
2930 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2931 "SetCodecFECStatus() failed to set FEC state");
2932 return -1;
2933 }
2934 return 0;
2935}
2936
2937bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002938 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002939}
2940
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002941void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2942 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002943 // If pacing is enabled we always store packets.
2944 if (!pacing_enabled_)
2945 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002946 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002947 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002948 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002949 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002950 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002951}
2952
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002953// Called when we are missing one or more packets.
2954int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002955 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2956}
2957
kwiberg55b97fe2016-01-28 05:22:45 -08002958uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2959 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2960 "Channel::Demultiplex()");
2961 _audioFrame.CopyFrom(audioFrame);
2962 _audioFrame.id_ = _channelId;
2963 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002964}
2965
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002966void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002967 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002968 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002969 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002970 CodecInst codec;
2971 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002972
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002973 // Never upsample or upmix the capture signal here. This should be done at the
2974 // end of the send chain.
2975 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2976 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2977 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2978 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002979}
2980
kwiberg55b97fe2016-01-28 05:22:45 -08002981uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2982 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2983 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002984
kwiberg55b97fe2016-01-28 05:22:45 -08002985 if (_audioFrame.samples_per_channel_ == 0) {
2986 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2987 "Channel::PrepareEncodeAndSend() invalid audio frame");
2988 return 0xFFFFFFFF;
2989 }
2990
2991 if (channel_state_.Get().input_file_playing) {
2992 MixOrReplaceAudioWithFile(mixingFrequency);
2993 }
2994
solenberg1c2af8e2016-03-24 10:36:00 -07002995 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2996 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002997
2998 if (channel_state_.Get().input_external_media) {
2999 rtc::CritScope cs(&_callbackCritSect);
3000 const bool isStereo = (_audioFrame.num_channels_ == 2);
3001 if (_inputExternalMediaCallbackPtr) {
3002 _inputExternalMediaCallbackPtr->Process(
3003 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
3004 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
3005 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00003006 }
kwiberg55b97fe2016-01-28 05:22:45 -08003007 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003008
kwiberg55b97fe2016-01-28 05:22:45 -08003009 if (_includeAudioLevelIndication) {
3010 size_t length =
3011 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02003012 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07003013 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003014 rms_level_.ProcessMuted(length);
3015 } else {
3016 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00003017 }
kwiberg55b97fe2016-01-28 05:22:45 -08003018 }
solenberg1c2af8e2016-03-24 10:36:00 -07003019 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00003020
kwiberg55b97fe2016-01-28 05:22:45 -08003021 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003022}
3023
kwiberg55b97fe2016-01-28 05:22:45 -08003024uint32_t Channel::EncodeAndSend() {
3025 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
3026 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003027
kwiberg55b97fe2016-01-28 05:22:45 -08003028 assert(_audioFrame.num_channels_ <= 2);
3029 if (_audioFrame.samples_per_channel_ == 0) {
3030 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3031 "Channel::EncodeAndSend() invalid audio frame");
3032 return 0xFFFFFFFF;
3033 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003034
kwiberg55b97fe2016-01-28 05:22:45 -08003035 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003036
kwiberg55b97fe2016-01-28 05:22:45 -08003037 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00003038
kwiberg55b97fe2016-01-28 05:22:45 -08003039 // The ACM resamples internally.
3040 _audioFrame.timestamp_ = _timeStamp;
3041 // This call will trigger AudioPacketizationCallback::SendData if encoding
3042 // is done and payload is ready for packetization and transmission.
3043 // Otherwise, it will return without invoking the callback.
3044 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
3045 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
3046 "Channel::EncodeAndSend() ACM encoding failed");
3047 return 0xFFFFFFFF;
3048 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003049
kwiberg55b97fe2016-01-28 05:22:45 -08003050 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
3051 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003052}
3053
Minyue2013aec2015-05-13 14:14:42 +02003054void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08003055 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003056 Channel* channel = associate_send_channel_.channel();
3057 if (channel && channel->ChannelId() == channel_id) {
3058 // If this channel is associated with a send channel of the specified
3059 // Channel ID, disassociate with it.
3060 ChannelOwner ref(NULL);
3061 associate_send_channel_ = ref;
3062 }
3063}
3064
ivoc14d5dbe2016-07-04 07:06:55 -07003065void Channel::SetRtcEventLog(RtcEventLog* event_log) {
3066 event_log_proxy_->SetEventLog(event_log);
3067}
3068
kwiberg55b97fe2016-01-28 05:22:45 -08003069int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
3070 VoEMediaProcess& processObject) {
3071 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3072 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003073
kwiberg55b97fe2016-01-28 05:22:45 -08003074 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003075
kwiberg55b97fe2016-01-28 05:22:45 -08003076 if (kPlaybackPerChannel == type) {
3077 if (_outputExternalMediaCallbackPtr) {
3078 _engineStatisticsPtr->SetLastError(
3079 VE_INVALID_OPERATION, kTraceError,
3080 "Channel::RegisterExternalMediaProcessing() "
3081 "output external media already enabled");
3082 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003083 }
kwiberg55b97fe2016-01-28 05:22:45 -08003084 _outputExternalMediaCallbackPtr = &processObject;
3085 _outputExternalMedia = true;
3086 } else if (kRecordingPerChannel == type) {
3087 if (_inputExternalMediaCallbackPtr) {
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 _inputExternalMediaCallbackPtr = &processObject;
3095 channel_state_.SetInputExternalMedia(true);
3096 }
3097 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003098}
3099
kwiberg55b97fe2016-01-28 05:22:45 -08003100int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
3101 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3102 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003103
kwiberg55b97fe2016-01-28 05:22:45 -08003104 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003105
kwiberg55b97fe2016-01-28 05:22:45 -08003106 if (kPlaybackPerChannel == type) {
3107 if (!_outputExternalMediaCallbackPtr) {
3108 _engineStatisticsPtr->SetLastError(
3109 VE_INVALID_OPERATION, kTraceWarning,
3110 "Channel::DeRegisterExternalMediaProcessing() "
3111 "output external media already disabled");
3112 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003113 }
kwiberg55b97fe2016-01-28 05:22:45 -08003114 _outputExternalMedia = false;
3115 _outputExternalMediaCallbackPtr = NULL;
3116 } else if (kRecordingPerChannel == type) {
3117 if (!_inputExternalMediaCallbackPtr) {
3118 _engineStatisticsPtr->SetLastError(
3119 VE_INVALID_OPERATION, kTraceWarning,
3120 "Channel::DeRegisterExternalMediaProcessing() "
3121 "input external media already disabled");
3122 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003123 }
kwiberg55b97fe2016-01-28 05:22:45 -08003124 channel_state_.SetInputExternalMedia(false);
3125 _inputExternalMediaCallbackPtr = NULL;
3126 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003127
kwiberg55b97fe2016-01-28 05:22:45 -08003128 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003129}
3130
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003131int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08003132 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3133 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003134
kwiberg55b97fe2016-01-28 05:22:45 -08003135 if (channel_state_.Get().playing) {
3136 _engineStatisticsPtr->SetLastError(
3137 VE_INVALID_OPERATION, kTraceError,
3138 "Channel::SetExternalMixing() "
3139 "external mixing cannot be changed while playing.");
3140 return -1;
3141 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003142
kwiberg55b97fe2016-01-28 05:22:45 -08003143 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003144
kwiberg55b97fe2016-01-28 05:22:45 -08003145 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003146}
3147
kwiberg55b97fe2016-01-28 05:22:45 -08003148int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
3149 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003150}
3151
wu@webrtc.org24301a62013-12-13 19:17:43 +00003152void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3153 audio_coding_->GetDecodingCallStatistics(stats);
3154}
3155
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003156bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3157 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08003158 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07003159 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003160 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003161 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003162}
3163
solenberg358057b2015-11-27 10:46:42 -08003164uint32_t Channel::GetDelayEstimate() const {
3165 int jitter_buffer_delay_ms = 0;
3166 int playout_buffer_delay_ms = 0;
3167 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
3168 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
3169}
3170
deadbeef74375882015-08-13 12:09:10 -07003171int Channel::LeastRequiredDelayMs() const {
3172 return audio_coding_->LeastRequiredDelayMs();
3173}
3174
kwiberg55b97fe2016-01-28 05:22:45 -08003175int Channel::SetMinimumPlayoutDelay(int delayMs) {
3176 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3177 "Channel::SetMinimumPlayoutDelay()");
3178 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3179 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
3180 _engineStatisticsPtr->SetLastError(
3181 VE_INVALID_ARGUMENT, kTraceError,
3182 "SetMinimumPlayoutDelay() invalid min delay");
3183 return -1;
3184 }
3185 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
3186 _engineStatisticsPtr->SetLastError(
3187 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3188 "SetMinimumPlayoutDelay() failed to set min playout delay");
3189 return -1;
3190 }
3191 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003192}
3193
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003194int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07003195 uint32_t playout_timestamp_rtp = 0;
3196 {
tommi31fc21f2016-01-21 10:37:37 -08003197 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003198 playout_timestamp_rtp = playout_timestamp_rtp_;
3199 }
kwiberg55b97fe2016-01-28 05:22:45 -08003200 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003201 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07003202 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003203 "GetPlayoutTimestamp() failed to retrieve timestamp");
3204 return -1;
3205 }
deadbeef74375882015-08-13 12:09:10 -07003206 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003207 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003208}
3209
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003210int Channel::SetInitTimestamp(unsigned int timestamp) {
3211 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003212 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003213 if (channel_state_.Get().sending) {
3214 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3215 "SetInitTimestamp() already sending");
3216 return -1;
3217 }
3218 _rtpRtcpModule->SetStartTimestamp(timestamp);
3219 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003220}
3221
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003222int Channel::SetInitSequenceNumber(short sequenceNumber) {
3223 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3224 "Channel::SetInitSequenceNumber()");
3225 if (channel_state_.Get().sending) {
3226 _engineStatisticsPtr->SetLastError(
3227 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3228 return -1;
3229 }
3230 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3231 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003232}
3233
kwiberg55b97fe2016-01-28 05:22:45 -08003234int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3235 RtpReceiver** rtp_receiver) const {
3236 *rtpRtcpModule = _rtpRtcpModule.get();
3237 *rtp_receiver = rtp_receiver_.get();
3238 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003239}
3240
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003241// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3242// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003243int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003244 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003245 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003246
kwiberg55b97fe2016-01-28 05:22:45 -08003247 {
3248 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003249
kwiberg5a25d952016-08-17 07:31:12 -07003250 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003251 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3252 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3253 " doesnt exist");
3254 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003255 }
3256
kwiberg4ec01d92016-08-22 08:43:54 -07003257 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003258 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003259 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3260 "Channel::MixOrReplaceAudioWithFile() file mixing "
3261 "failed");
3262 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003263 }
kwiberg55b97fe2016-01-28 05:22:45 -08003264 if (fileSamples == 0) {
3265 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3266 "Channel::MixOrReplaceAudioWithFile() file is ended");
3267 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003268 }
kwiberg55b97fe2016-01-28 05:22:45 -08003269 }
3270
3271 assert(_audioFrame.samples_per_channel_ == fileSamples);
3272
3273 if (_mixFileWithMicrophone) {
3274 // Currently file stream is always mono.
3275 // TODO(xians): Change the code when FilePlayer supports real stereo.
3276 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3277 1, fileSamples);
3278 } else {
3279 // Replace ACM audio with file.
3280 // Currently file stream is always mono.
3281 // TODO(xians): Change the code when FilePlayer supports real stereo.
3282 _audioFrame.UpdateFrame(
3283 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3284 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3285 }
3286 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003287}
3288
kwiberg55b97fe2016-01-28 05:22:45 -08003289int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3290 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003291
kwibergb7f89d62016-02-17 10:04:18 -08003292 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003293 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003294
kwiberg55b97fe2016-01-28 05:22:45 -08003295 {
3296 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003297
kwiberg5a25d952016-08-17 07:31:12 -07003298 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003299 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3300 "Channel::MixAudioWithFile() file mixing failed");
3301 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003302 }
3303
kwiberg55b97fe2016-01-28 05:22:45 -08003304 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003305 if (output_file_player_->Get10msAudioFromFile(
3306 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003307 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3308 "Channel::MixAudioWithFile() file mixing failed");
3309 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003310 }
kwiberg55b97fe2016-01-28 05:22:45 -08003311 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003312
kwiberg55b97fe2016-01-28 05:22:45 -08003313 if (audioFrame.samples_per_channel_ == fileSamples) {
3314 // Currently file stream is always mono.
3315 // TODO(xians): Change the code when FilePlayer supports real stereo.
3316 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3317 fileSamples);
3318 } else {
3319 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3320 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3321 ") != "
3322 "fileSamples(%" PRIuS ")",
3323 audioFrame.samples_per_channel_, fileSamples);
3324 return -1;
3325 }
3326
3327 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003328}
3329
deadbeef74375882015-08-13 12:09:10 -07003330void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003331 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003332
henrik.lundin96bd5022016-04-06 04:13:56 -07003333 if (!jitter_buffer_playout_timestamp_) {
3334 // This can happen if this channel has not received any RTP packets. In
3335 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003336 return;
3337 }
3338
3339 uint16_t delay_ms = 0;
3340 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003341 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003342 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3343 " delay from the ADM");
3344 _engineStatisticsPtr->SetLastError(
3345 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3346 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3347 return;
3348 }
3349
henrik.lundin96bd5022016-04-06 04:13:56 -07003350 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3351 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003352
3353 // Remove the playout delay.
henrik.lundin96bd5022016-04-06 04:13:56 -07003354 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003355
kwiberg55b97fe2016-01-28 05:22:45 -08003356 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003357 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003358 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003359
3360 {
tommi31fc21f2016-01-21 10:37:37 -08003361 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003362 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003363 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003364 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003365 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003366 }
3367 playout_delay_ms_ = delay_ms;
3368 }
3369}
3370
kwiberg55b97fe2016-01-28 05:22:45 -08003371void Channel::RegisterReceiveCodecsToRTPModule() {
3372 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3373 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003374
kwiberg55b97fe2016-01-28 05:22:45 -08003375 CodecInst codec;
3376 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003377
kwiberg55b97fe2016-01-28 05:22:45 -08003378 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3379 // Open up the RTP/RTCP receiver for all supported codecs
3380 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3381 (rtp_receiver_->RegisterReceivePayload(
3382 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3383 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3384 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3385 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3386 " to register %s (%d/%d/%" PRIuS
3387 "/%d) to RTP/RTCP "
3388 "receiver",
3389 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3390 codec.rate);
3391 } else {
3392 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3393 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3394 "(%d/%d/%" PRIuS
3395 "/%d) has been added to the RTP/RTCP "
3396 "receiver",
3397 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3398 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003399 }
kwiberg55b97fe2016-01-28 05:22:45 -08003400 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003401}
3402
kwiberg55b97fe2016-01-28 05:22:45 -08003403int Channel::SetSendRtpHeaderExtension(bool enable,
3404 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003405 unsigned char id) {
3406 int error = 0;
3407 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3408 if (enable) {
3409 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3410 }
3411 return error;
3412}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003413
henrik.lundinb3e30012016-08-31 14:09:51 -07003414int32_t Channel::GetPlayoutFrequency() const {
wu@webrtc.org94454b72014-06-05 20:34:08 +00003415 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3416 CodecInst current_recive_codec;
3417 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3418 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3419 // Even though the actual sampling rate for G.722 audio is
3420 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3421 // 8,000 Hz because that value was erroneously assigned in
3422 // RFC 1890 and must remain unchanged for backward compatibility.
3423 playout_frequency = 8000;
3424 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3425 // We are resampling Opus internally to 32,000 Hz until all our
3426 // DSP routines can operate at 48,000 Hz, but the RTP clock
3427 // rate for the Opus payload format is standardized to 48,000 Hz,
3428 // because that is the maximum supported decoding sampling rate.
3429 playout_frequency = 48000;
3430 }
3431 }
3432 return playout_frequency;
3433}
3434
Minyue2013aec2015-05-13 14:14:42 +02003435int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003436 RtcpMode method = _rtpRtcpModule->RTCP();
3437 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003438 return 0;
3439 }
3440 std::vector<RTCPReportBlock> report_blocks;
3441 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003442
3443 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003444 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003445 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003446 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003447 Channel* channel = associate_send_channel_.channel();
3448 // Tries to get RTT from an associated channel. This is important for
3449 // receive-only channels.
3450 if (channel) {
3451 // To prevent infinite recursion and deadlock, calling GetRTT of
3452 // associate channel should always use "false" for argument:
3453 // |allow_associate_channel|.
3454 rtt = channel->GetRTT(false);
3455 }
3456 }
3457 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003458 }
3459
3460 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3461 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3462 for (; it != report_blocks.end(); ++it) {
3463 if (it->remoteSSRC == remoteSSRC)
3464 break;
3465 }
3466 if (it == report_blocks.end()) {
3467 // We have not received packets with SSRC matching the report blocks.
3468 // To calculate RTT we try with the SSRC of the first report block.
3469 // This is very important for send-only channels where we don't know
3470 // the SSRC of the other end.
3471 remoteSSRC = report_blocks[0].remoteSSRC;
3472 }
Minyue2013aec2015-05-13 14:14:42 +02003473
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003474 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003475 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003476 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003477 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3478 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003479 return 0;
3480 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003481 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003482}
3483
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003484} // namespace voe
3485} // namespace webrtc