blob: 5bbbce3c406b1c8ebfbbd3ef2e7533e335e836de [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"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010020#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000021#include "webrtc/base/timeutils.h"
minyue@webrtc.orge509f942013-09-12 17:03:00 +000022#include "webrtc/common.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020023#include "webrtc/config.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000024#include "webrtc/modules/audio_device/include/audio_device.h"
25#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010026#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010027#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
29#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
30#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000031#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/utility/include/audio_frame_operations.h"
33#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010034#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000035#include "webrtc/voice_engine/include/voe_base.h"
36#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
solenberg8842c3e2016-03-11 03:06:41 -080046const int kTelephoneEventAttenuationdB = 10;
47
Stefan Holmerb86d4e42015-12-07 10:26:18 +010048class TransportFeedbackProxy : public TransportFeedbackObserver {
49 public:
50 TransportFeedbackProxy() : feedback_observer_(nullptr) {
51 pacer_thread_.DetachFromThread();
52 network_thread_.DetachFromThread();
53 }
54
55 void SetTransportFeedbackObserver(
56 TransportFeedbackObserver* feedback_observer) {
57 RTC_DCHECK(thread_checker_.CalledOnValidThread());
58 rtc::CritScope lock(&crit_);
59 feedback_observer_ = feedback_observer;
60 }
61
62 // Implements TransportFeedbackObserver.
63 void AddPacket(uint16_t sequence_number,
64 size_t length,
65 bool was_paced) override {
66 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
67 rtc::CritScope lock(&crit_);
68 if (feedback_observer_)
69 feedback_observer_->AddPacket(sequence_number, length, was_paced);
70 }
71 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
72 RTC_DCHECK(network_thread_.CalledOnValidThread());
73 rtc::CritScope lock(&crit_);
74 if (feedback_observer_)
75 feedback_observer_->OnTransportFeedback(feedback);
76 }
77
78 private:
79 rtc::CriticalSection crit_;
80 rtc::ThreadChecker thread_checker_;
81 rtc::ThreadChecker pacer_thread_;
82 rtc::ThreadChecker network_thread_;
83 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
84};
85
86class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
87 public:
88 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
89 pacer_thread_.DetachFromThread();
90 }
91
92 void SetSequenceNumberAllocator(
93 TransportSequenceNumberAllocator* seq_num_allocator) {
94 RTC_DCHECK(thread_checker_.CalledOnValidThread());
95 rtc::CritScope lock(&crit_);
96 seq_num_allocator_ = seq_num_allocator;
97 }
98
99 // Implements TransportSequenceNumberAllocator.
100 uint16_t AllocateSequenceNumber() override {
101 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
102 rtc::CritScope lock(&crit_);
103 if (!seq_num_allocator_)
104 return 0;
105 return seq_num_allocator_->AllocateSequenceNumber();
106 }
107
108 private:
109 rtc::CriticalSection crit_;
110 rtc::ThreadChecker thread_checker_;
111 rtc::ThreadChecker pacer_thread_;
112 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
113};
114
115class RtpPacketSenderProxy : public RtpPacketSender {
116 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800117 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100118
119 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
120 RTC_DCHECK(thread_checker_.CalledOnValidThread());
121 rtc::CritScope lock(&crit_);
122 rtp_packet_sender_ = rtp_packet_sender;
123 }
124
125 // Implements RtpPacketSender.
126 void InsertPacket(Priority priority,
127 uint32_t ssrc,
128 uint16_t sequence_number,
129 int64_t capture_time_ms,
130 size_t bytes,
131 bool retransmission) override {
132 rtc::CritScope lock(&crit_);
133 if (rtp_packet_sender_) {
134 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
135 capture_time_ms, bytes, retransmission);
136 }
137 }
138
139 private:
140 rtc::ThreadChecker thread_checker_;
141 rtc::CriticalSection crit_;
142 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
143};
144
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000145// Extend the default RTCP statistics struct with max_jitter, defined as the
146// maximum jitter value seen in an RTCP report block.
147struct ChannelStatistics : public RtcpStatistics {
148 ChannelStatistics() : rtcp(), max_jitter(0) {}
149
150 RtcpStatistics rtcp;
151 uint32_t max_jitter;
152};
153
154// Statistics callback, called at each generation of a new RTCP report block.
155class StatisticsProxy : public RtcpStatisticsCallback {
156 public:
tommi31fc21f2016-01-21 10:37:37 -0800157 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000158 virtual ~StatisticsProxy() {}
159
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000160 void StatisticsUpdated(const RtcpStatistics& statistics,
161 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000162 if (ssrc != ssrc_)
163 return;
164
tommi31fc21f2016-01-21 10:37:37 -0800165 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000166 stats_.rtcp = statistics;
167 if (statistics.jitter > stats_.max_jitter) {
168 stats_.max_jitter = statistics.jitter;
169 }
170 }
171
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000172 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000173
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000174 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800175 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000176 return stats_;
177 }
178
179 private:
180 // StatisticsUpdated calls are triggered from threads in the RTP module,
181 // while GetStats calls can be triggered from the public voice engine API,
182 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800183 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000184 const uint32_t ssrc_;
185 ChannelStatistics stats_;
186};
187
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000188class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000189 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000190 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
191 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000192
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000193 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
194 // Not used for Voice Engine.
195 }
196
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000197 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
198 int64_t rtt,
199 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000200 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
201 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
202 // report for VoiceEngine?
203 if (report_blocks.empty())
204 return;
205
206 int fraction_lost_aggregate = 0;
207 int total_number_of_packets = 0;
208
209 // If receiving multiple report blocks, calculate the weighted average based
210 // on the number of packets a report refers to.
211 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
212 block_it != report_blocks.end(); ++block_it) {
213 // Find the previous extended high sequence number for this remote SSRC,
214 // to calculate the number of RTP packets this report refers to. Ignore if
215 // we haven't seen this SSRC before.
216 std::map<uint32_t, uint32_t>::iterator seq_num_it =
217 extended_max_sequence_number_.find(block_it->sourceSSRC);
218 int number_of_packets = 0;
219 if (seq_num_it != extended_max_sequence_number_.end()) {
220 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
221 }
222 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
223 total_number_of_packets += number_of_packets;
224
225 extended_max_sequence_number_[block_it->sourceSSRC] =
226 block_it->extendedHighSeqNum;
227 }
228 int weighted_fraction_lost = 0;
229 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800230 weighted_fraction_lost =
231 (fraction_lost_aggregate + total_number_of_packets / 2) /
232 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000233 }
234 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000235 }
236
237 private:
238 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000239 // Maps remote side ssrc to extended highest sequence number received.
240 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000241};
242
kwiberg55b97fe2016-01-28 05:22:45 -0800243int32_t Channel::SendData(FrameType frameType,
244 uint8_t payloadType,
245 uint32_t timeStamp,
246 const uint8_t* payloadData,
247 size_t payloadSize,
248 const RTPFragmentationHeader* fragmentation) {
249 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
250 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
251 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
252 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000253
kwiberg55b97fe2016-01-28 05:22:45 -0800254 if (_includeAudioLevelIndication) {
255 // Store current audio level in the RTP/RTCP module.
256 // The level will be used in combination with voice-activity state
257 // (frameType) to add an RTP header extension
258 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
259 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
kwiberg55b97fe2016-01-28 05:22:45 -0800261 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
262 // packetization.
263 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
264 if (_rtpRtcpModule->SendOutgoingData(
265 (FrameType&)frameType, payloadType, timeStamp,
266 // Leaving the time when this frame was
267 // received from the capture device as
268 // undefined for voice for now.
269 -1, payloadData, payloadSize, fragmentation) == -1) {
270 _engineStatisticsPtr->SetLastError(
271 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
272 "Channel::SendData() failed to send data to RTP/RTCP module");
273 return -1;
274 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000275
kwiberg55b97fe2016-01-28 05:22:45 -0800276 _lastLocalTimeStamp = timeStamp;
277 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
kwiberg55b97fe2016-01-28 05:22:45 -0800279 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280}
281
kwiberg55b97fe2016-01-28 05:22:45 -0800282int32_t Channel::InFrameType(FrameType frame_type) {
283 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
284 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000285
kwiberg55b97fe2016-01-28 05:22:45 -0800286 rtc::CritScope cs(&_callbackCritSect);
287 _sendFrameType = (frame_type == kAudioFrameSpeech);
288 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000289}
290
kwiberg55b97fe2016-01-28 05:22:45 -0800291int32_t Channel::OnRxVadDetected(int vadDecision) {
292 rtc::CritScope cs(&_callbackCritSect);
293 if (_rxVadObserverPtr) {
294 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
295 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000296
kwiberg55b97fe2016-01-28 05:22:45 -0800297 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000298}
299
stefan1d8a5062015-10-02 03:39:33 -0700300bool Channel::SendRtp(const uint8_t* data,
301 size_t len,
302 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800303 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
304 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000305
kwiberg55b97fe2016-01-28 05:22:45 -0800306 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000307
kwiberg55b97fe2016-01-28 05:22:45 -0800308 if (_transportPtr == NULL) {
309 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
310 "Channel::SendPacket() failed to send RTP packet due to"
311 " invalid transport object");
312 return false;
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314
kwiberg55b97fe2016-01-28 05:22:45 -0800315 uint8_t* bufferToSendPtr = (uint8_t*)data;
316 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000317
kwiberg55b97fe2016-01-28 05:22:45 -0800318 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
319 std::string transport_name =
320 _externalTransport ? "external transport" : "WebRtc sockets";
321 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
322 "Channel::SendPacket() RTP transmission using %s failed",
323 transport_name.c_str());
324 return false;
325 }
326 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000327}
328
kwiberg55b97fe2016-01-28 05:22:45 -0800329bool Channel::SendRtcp(const uint8_t* data, size_t len) {
330 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
331 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000332
kwiberg55b97fe2016-01-28 05:22:45 -0800333 rtc::CritScope cs(&_callbackCritSect);
334 if (_transportPtr == NULL) {
335 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
336 "Channel::SendRtcp() failed to send RTCP packet"
337 " due to invalid transport object");
338 return false;
339 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000340
kwiberg55b97fe2016-01-28 05:22:45 -0800341 uint8_t* bufferToSendPtr = (uint8_t*)data;
342 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000343
kwiberg55b97fe2016-01-28 05:22:45 -0800344 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
345 if (n < 0) {
346 std::string transport_name =
347 _externalTransport ? "external transport" : "WebRtc sockets";
348 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
349 "Channel::SendRtcp() transmission using %s failed",
350 transport_name.c_str());
351 return false;
352 }
353 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
kwiberg55b97fe2016-01-28 05:22:45 -0800356void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
357 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
358 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
kwiberg55b97fe2016-01-28 05:22:45 -0800360 // Update ssrc so that NTP for AV sync can be updated.
361 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000362}
363
Peter Boströmac547a62015-09-17 23:03:57 +0200364void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
365 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
366 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
367 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000368}
369
Peter Boströmac547a62015-09-17 23:03:57 +0200370int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000371 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000372 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000373 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800374 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200375 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800376 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
377 "Channel::OnInitializeDecoder(payloadType=%d, "
378 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
379 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000380
kwiberg55b97fe2016-01-28 05:22:45 -0800381 CodecInst receiveCodec = {0};
382 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
kwiberg55b97fe2016-01-28 05:22:45 -0800384 receiveCodec.pltype = payloadType;
385 receiveCodec.plfreq = frequency;
386 receiveCodec.channels = channels;
387 receiveCodec.rate = rate;
388 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000389
kwiberg55b97fe2016-01-28 05:22:45 -0800390 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
391 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000392
kwiberg55b97fe2016-01-28 05:22:45 -0800393 // Register the new codec to the ACM
394 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1) {
395 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
396 "Channel::OnInitializeDecoder() invalid codec ("
397 "pt=%d, name=%s) received - 1",
398 payloadType, payloadName);
399 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
400 return -1;
401 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
kwiberg55b97fe2016-01-28 05:22:45 -0800403 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000404}
405
kwiberg55b97fe2016-01-28 05:22:45 -0800406int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
407 size_t payloadSize,
408 const WebRtcRTPHeader* rtpHeader) {
409 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
410 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
411 ","
412 " payloadType=%u, audioChannel=%" PRIuS ")",
413 payloadSize, rtpHeader->header.payloadType,
414 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000415
kwiberg55b97fe2016-01-28 05:22:45 -0800416 if (!channel_state_.Get().playing) {
417 // Avoid inserting into NetEQ when we are not playing. Count the
418 // packet as discarded.
419 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
420 "received packet is discarded since playing is not"
421 " activated");
422 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800424 }
425
426 // Push the incoming payload (parsed and ready for decoding) into the ACM
427 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
428 0) {
429 _engineStatisticsPtr->SetLastError(
430 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
431 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
432 return -1;
433 }
434
435 // Update the packet delay.
436 UpdatePacketDelay(rtpHeader->header.timestamp,
437 rtpHeader->header.sequenceNumber);
438
439 int64_t round_trip_time = 0;
440 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
441 NULL);
442
443 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
444 if (!nack_list.empty()) {
445 // Can't use nack_list.data() since it's not supported by all
446 // compilers.
447 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
448 }
449 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000450}
451
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000452bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000453 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000454 RTPHeader header;
455 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
456 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
457 "IncomingPacket invalid RTP header");
458 return false;
459 }
460 header.payload_type_frequency =
461 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
462 if (header.payload_type_frequency < 0)
463 return false;
464 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
465}
466
kwiberg55b97fe2016-01-28 05:22:45 -0800467int32_t Channel::GetAudioFrame(int32_t id, AudioFrame* audioFrame) {
468 if (event_log_) {
469 unsigned int ssrc;
470 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
471 event_log_->LogAudioPlayout(ssrc);
472 }
473 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
474 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame) ==
475 -1) {
476 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
477 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
478 // In all likelihood, the audio in this frame is garbage. We return an
479 // error so that the audio mixer module doesn't add it to the mix. As
480 // a result, it won't be played out and the actions skipped here are
481 // irrelevant.
482 return -1;
483 }
484
485 if (_RxVadDetection) {
486 UpdateRxVadDetection(*audioFrame);
487 }
488
489 // Convert module ID to internal VoE channel ID
490 audioFrame->id_ = VoEChannelId(audioFrame->id_);
491 // Store speech type for dead-or-alive detection
492 _outputSpeechType = audioFrame->speech_type_;
493
494 ChannelState::State state = channel_state_.Get();
495
496 if (state.rx_apm_is_enabled) {
497 int err = rx_audioproc_->ProcessStream(audioFrame);
498 if (err) {
499 LOG(LS_ERROR) << "ProcessStream() error: " << err;
500 assert(false);
Ivo Creusenae856f22015-09-17 16:30:16 +0200501 }
kwiberg55b97fe2016-01-28 05:22:45 -0800502 }
503
504 {
505 // Pass the audio buffers to an optional sink callback, before applying
506 // scaling/panning, as that applies to the mix operation.
507 // External recipients of the audio (e.g. via AudioTrack), will do their
508 // own mixing/dynamic processing.
509 rtc::CritScope cs(&_callbackCritSect);
510 if (audio_sink_) {
511 AudioSinkInterface::Data data(
512 &audioFrame->data_[0], audioFrame->samples_per_channel_,
513 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
514 audioFrame->timestamp_);
515 audio_sink_->OnData(data);
516 }
517 }
518
519 float output_gain = 1.0f;
520 float left_pan = 1.0f;
521 float right_pan = 1.0f;
522 {
523 rtc::CritScope cs(&volume_settings_critsect_);
524 output_gain = _outputGain;
525 left_pan = _panLeft;
526 right_pan = _panRight;
527 }
528
529 // Output volume scaling
530 if (output_gain < 0.99f || output_gain > 1.01f) {
531 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
532 }
533
534 // Scale left and/or right channel(s) if stereo and master balance is
535 // active
536
537 if (left_pan != 1.0f || right_pan != 1.0f) {
538 if (audioFrame->num_channels_ == 1) {
539 // Emulate stereo mode since panning is active.
540 // The mono signal is copied to both left and right channels here.
541 AudioFrameOperations::MonoToStereo(audioFrame);
542 }
543 // For true stereo mode (when we are receiving a stereo signal), no
544 // action is needed.
545
546 // Do the panning operation (the audio frame contains stereo at this
547 // stage)
548 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
549 }
550
551 // Mix decoded PCM output with file if file mixing is enabled
552 if (state.output_file_playing) {
553 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
554 }
555
556 // External media
557 if (_outputExternalMedia) {
558 rtc::CritScope cs(&_callbackCritSect);
559 const bool isStereo = (audioFrame->num_channels_ == 2);
560 if (_outputExternalMediaCallbackPtr) {
561 _outputExternalMediaCallbackPtr->Process(
562 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
563 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
564 isStereo);
565 }
566 }
567
568 // Record playout if enabled
569 {
570 rtc::CritScope cs(&_fileCritSect);
571
572 if (_outputFileRecording && _outputFileRecorderPtr) {
573 _outputFileRecorderPtr->RecordAudioToFile(*audioFrame);
574 }
575 }
576
577 // Measure audio level (0-9)
578 _outputAudioLevel.ComputeLevel(*audioFrame);
579
580 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
581 // The first frame with a valid rtp timestamp.
582 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
583 }
584
585 if (capture_start_rtp_time_stamp_ >= 0) {
586 // audioFrame.timestamp_ should be valid from now on.
587
588 // Compute elapsed time.
589 int64_t unwrap_timestamp =
590 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
591 audioFrame->elapsed_time_ms_ =
592 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
593 (GetPlayoutFrequency() / 1000);
594
niklase@google.com470e71d2011-07-07 08:21:25 +0000595 {
kwiberg55b97fe2016-01-28 05:22:45 -0800596 rtc::CritScope lock(&ts_stats_lock_);
597 // Compute ntp time.
598 audioFrame->ntp_time_ms_ =
599 ntp_estimator_.Estimate(audioFrame->timestamp_);
600 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
601 if (audioFrame->ntp_time_ms_ > 0) {
602 // Compute |capture_start_ntp_time_ms_| so that
603 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
604 capture_start_ntp_time_ms_ =
605 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000606 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000607 }
kwiberg55b97fe2016-01-28 05:22:45 -0800608 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000609
kwiberg55b97fe2016-01-28 05:22:45 -0800610 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000611}
612
kwiberg55b97fe2016-01-28 05:22:45 -0800613int32_t Channel::NeededFrequency(int32_t id) const {
614 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
615 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000616
kwiberg55b97fe2016-01-28 05:22:45 -0800617 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000618
kwiberg55b97fe2016-01-28 05:22:45 -0800619 // Determine highest needed receive frequency
620 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000621
kwiberg55b97fe2016-01-28 05:22:45 -0800622 // Return the bigger of playout and receive frequency in the ACM.
623 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
624 highestNeeded = audio_coding_->PlayoutFrequency();
625 } else {
626 highestNeeded = receiveFrequency;
627 }
628
629 // Special case, if we're playing a file on the playout side
630 // we take that frequency into consideration as well
631 // This is not needed on sending side, since the codec will
632 // limit the spectrum anyway.
633 if (channel_state_.Get().output_file_playing) {
634 rtc::CritScope cs(&_fileCritSect);
635 if (_outputFilePlayerPtr) {
636 if (_outputFilePlayerPtr->Frequency() > highestNeeded) {
637 highestNeeded = _outputFilePlayerPtr->Frequency();
638 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000639 }
kwiberg55b97fe2016-01-28 05:22:45 -0800640 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000641
kwiberg55b97fe2016-01-28 05:22:45 -0800642 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000643}
644
ivocb04965c2015-09-09 00:09:43 -0700645int32_t Channel::CreateChannel(Channel*& channel,
646 int32_t channelId,
647 uint32_t instanceId,
648 RtcEventLog* const event_log,
649 const Config& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800650 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
651 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
652 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000653
kwiberg55b97fe2016-01-28 05:22:45 -0800654 channel = new Channel(channelId, instanceId, event_log, config);
655 if (channel == NULL) {
656 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
657 "Channel::CreateChannel() unable to allocate memory for"
658 " channel");
659 return -1;
660 }
661 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662}
663
kwiberg55b97fe2016-01-28 05:22:45 -0800664void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
665 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
666 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
667 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000668
kwiberg55b97fe2016-01-28 05:22:45 -0800669 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000670}
671
kwiberg55b97fe2016-01-28 05:22:45 -0800672void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
673 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
674 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
675 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676
kwiberg55b97fe2016-01-28 05:22:45 -0800677 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000678}
679
kwiberg55b97fe2016-01-28 05:22:45 -0800680void Channel::PlayFileEnded(int32_t id) {
681 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
682 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000683
kwiberg55b97fe2016-01-28 05:22:45 -0800684 if (id == _inputFilePlayerId) {
685 channel_state_.SetInputFilePlaying(false);
686 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
687 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000688 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800689 } else if (id == _outputFilePlayerId) {
690 channel_state_.SetOutputFilePlaying(false);
691 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
692 "Channel::PlayFileEnded() => output file player module is"
693 " shutdown");
694 }
695}
696
697void Channel::RecordFileEnded(int32_t id) {
698 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
699 "Channel::RecordFileEnded(id=%d)", id);
700
701 assert(id == _outputFileRecorderId);
702
703 rtc::CritScope cs(&_fileCritSect);
704
705 _outputFileRecording = false;
706 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
707 "Channel::RecordFileEnded() => output file recorder module is"
708 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000709}
710
pbos@webrtc.org92135212013-05-14 08:31:39 +0000711Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000712 uint32_t instanceId,
ivocb04965c2015-09-09 00:09:43 -0700713 RtcEventLog* const event_log,
714 const Config& config)
tommi31fc21f2016-01-21 10:37:37 -0800715 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100716 _channelId(channelId),
717 event_log_(event_log),
718 rtp_header_parser_(RtpHeaderParser::Create()),
719 rtp_payload_registry_(
720 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
721 rtp_receive_statistics_(
722 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
723 rtp_receiver_(
724 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
solenbergb69395b2016-03-16 07:05:17 -0700725 nullptr,
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100726 this,
727 this,
728 rtp_payload_registry_.get())),
729 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
730 _outputAudioLevel(),
731 _externalTransport(false),
732 _inputFilePlayerPtr(NULL),
733 _outputFilePlayerPtr(NULL),
734 _outputFileRecorderPtr(NULL),
735 // Avoid conflict with other channels by adding 1024 - 1026,
736 // won't use as much as 1024 channels.
737 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
738 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
739 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
740 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100741 _outputExternalMedia(false),
742 _inputExternalMediaCallbackPtr(NULL),
743 _outputExternalMediaCallbackPtr(NULL),
744 _timeStamp(0), // This is just an offset, RTP module will add it's own
745 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100746 ntp_estimator_(Clock::GetRealTimeClock()),
747 jitter_buffer_playout_timestamp_(0),
748 playout_timestamp_rtp_(0),
749 playout_timestamp_rtcp_(0),
750 playout_delay_ms_(0),
751 _numberOfDiscardedPackets(0),
752 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100753 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
754 capture_start_rtp_time_stamp_(-1),
755 capture_start_ntp_time_ms_(-1),
756 _engineStatisticsPtr(NULL),
757 _outputMixerPtr(NULL),
758 _transmitMixerPtr(NULL),
759 _moduleProcessThreadPtr(NULL),
760 _audioDeviceModulePtr(NULL),
761 _voiceEngineObserverPtr(NULL),
762 _callbackCritSectPtr(NULL),
763 _transportPtr(NULL),
764 _rxVadObserverPtr(NULL),
765 _oldVadDecision(-1),
766 _sendFrameType(0),
767 _externalMixing(false),
768 _mixFileWithMicrophone(false),
769 _mute(false),
770 _panLeft(1.0f),
771 _panRight(1.0f),
772 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100773 _lastLocalTimeStamp(0),
774 _lastPayloadType(0),
775 _includeAudioLevelIndication(false),
776 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100777 _average_jitter_buffer_delay_us(0),
778 _previousTimestamp(0),
779 _recPacketDelayMs(20),
780 _RxVadDetection(false),
781 _rxAgcIsEnabled(false),
782 _rxNsIsEnabled(false),
783 restored_packet_in_use_(false),
784 rtcp_observer_(new VoERtcpObserver(this)),
785 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100786 associate_send_channel_(ChannelOwner(nullptr)),
787 pacing_enabled_(config.Get<VoicePacing>().enabled),
stefanbba9dec2016-02-01 04:39:55 -0800788 feedback_observer_proxy_(new TransportFeedbackProxy()),
789 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
790 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()) {
kwiberg55b97fe2016-01-28 05:22:45 -0800791 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
792 "Channel::Channel() - ctor");
793 AudioCodingModule::Config acm_config;
794 acm_config.id = VoEModuleId(instanceId, channelId);
795 if (config.Get<NetEqCapacityConfig>().enabled) {
796 // Clamping the buffer capacity at 20 packets. While going lower will
797 // probably work, it makes little sense.
798 acm_config.neteq_config.max_packets_in_buffer =
799 std::max(20, config.Get<NetEqCapacityConfig>().capacity);
800 }
801 acm_config.neteq_config.enable_fast_accelerate =
802 config.Get<NetEqFastAccelerate>().enabled;
803 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200804
kwiberg55b97fe2016-01-28 05:22:45 -0800805 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000806
kwiberg55b97fe2016-01-28 05:22:45 -0800807 RtpRtcp::Configuration configuration;
808 configuration.audio = true;
809 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800810 configuration.receive_statistics = rtp_receive_statistics_.get();
811 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800812 if (pacing_enabled_) {
813 configuration.paced_sender = rtp_packet_sender_proxy_.get();
814 configuration.transport_sequence_number_allocator =
815 seq_num_allocator_proxy_.get();
816 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
817 }
kwiberg55b97fe2016-01-28 05:22:45 -0800818 configuration.event_log = event_log;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000819
kwiberg55b97fe2016-01-28 05:22:45 -0800820 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100821 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000822
kwiberg55b97fe2016-01-28 05:22:45 -0800823 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
824 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
825 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000826
kwiberg55b97fe2016-01-28 05:22:45 -0800827 Config audioproc_config;
828 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
829 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000830}
831
kwiberg55b97fe2016-01-28 05:22:45 -0800832Channel::~Channel() {
833 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
834 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
835 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000836
kwiberg55b97fe2016-01-28 05:22:45 -0800837 if (_outputExternalMedia) {
838 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
839 }
840 if (channel_state_.Get().input_external_media) {
841 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
842 }
843 StopSend();
844 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000845
kwiberg55b97fe2016-01-28 05:22:45 -0800846 {
847 rtc::CritScope cs(&_fileCritSect);
848 if (_inputFilePlayerPtr) {
849 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
850 _inputFilePlayerPtr->StopPlayingFile();
851 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
852 _inputFilePlayerPtr = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000853 }
kwiberg55b97fe2016-01-28 05:22:45 -0800854 if (_outputFilePlayerPtr) {
855 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
856 _outputFilePlayerPtr->StopPlayingFile();
857 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
858 _outputFilePlayerPtr = NULL;
859 }
860 if (_outputFileRecorderPtr) {
861 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
862 _outputFileRecorderPtr->StopRecording();
863 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
864 _outputFileRecorderPtr = NULL;
865 }
866 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000867
kwiberg55b97fe2016-01-28 05:22:45 -0800868 // The order to safely shutdown modules in a channel is:
869 // 1. De-register callbacks in modules
870 // 2. De-register modules in process thread
871 // 3. Destroy modules
872 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
873 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
874 "~Channel() failed to de-register transport callback"
875 " (Audio coding module)");
876 }
877 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
878 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
879 "~Channel() failed to de-register VAD callback"
880 " (Audio coding module)");
881 }
882 // De-register modules in process thread
883 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000884
kwiberg55b97fe2016-01-28 05:22:45 -0800885 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000886}
887
kwiberg55b97fe2016-01-28 05:22:45 -0800888int32_t Channel::Init() {
889 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
890 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000891
kwiberg55b97fe2016-01-28 05:22:45 -0800892 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000893
kwiberg55b97fe2016-01-28 05:22:45 -0800894 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000895
kwiberg55b97fe2016-01-28 05:22:45 -0800896 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
897 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
898 "Channel::Init() must call SetEngineInformation() first");
899 return -1;
900 }
901
902 // --- Add modules to process thread (for periodic schedulation)
903
904 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
905
906 // --- ACM initialization
907
908 if (audio_coding_->InitializeReceiver() == -1) {
909 _engineStatisticsPtr->SetLastError(
910 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
911 "Channel::Init() unable to initialize the ACM - 1");
912 return -1;
913 }
914
915 // --- RTP/RTCP module initialization
916
917 // Ensure that RTCP is enabled by default for the created channel.
918 // Note that, the module will keep generating RTCP until it is explicitly
919 // disabled by the user.
920 // After StopListen (when no sockets exists), RTCP packets will no longer
921 // be transmitted since the Transport object will then be invalid.
922 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
923 // RTCP is enabled by default.
924 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
925 // --- Register all permanent callbacks
926 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
927 (audio_coding_->RegisterVADCallback(this) == -1);
928
929 if (fail) {
930 _engineStatisticsPtr->SetLastError(
931 VE_CANNOT_INIT_CHANNEL, kTraceError,
932 "Channel::Init() callbacks not registered");
933 return -1;
934 }
935
936 // --- Register all supported codecs to the receiving side of the
937 // RTP/RTCP module
938
939 CodecInst codec;
940 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
941
942 for (int idx = 0; idx < nSupportedCodecs; idx++) {
943 // Open up the RTP/RTCP receiver for all supported codecs
944 if ((audio_coding_->Codec(idx, &codec) == -1) ||
945 (rtp_receiver_->RegisterReceivePayload(
946 codec.plname, codec.pltype, codec.plfreq, codec.channels,
947 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
948 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
949 "Channel::Init() unable to register %s "
950 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
951 codec.plname, codec.pltype, codec.plfreq, codec.channels,
952 codec.rate);
953 } else {
954 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
955 "Channel::Init() %s (%d/%d/%" PRIuS
956 "/%d) has been "
957 "added to the RTP/RTCP receiver",
958 codec.plname, codec.pltype, codec.plfreq, codec.channels,
959 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000960 }
961
kwiberg55b97fe2016-01-28 05:22:45 -0800962 // Ensure that PCMU is used as default codec on the sending side
963 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
964 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000965 }
966
kwiberg55b97fe2016-01-28 05:22:45 -0800967 // Register default PT for outband 'telephone-event'
968 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
969 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
970 (audio_coding_->RegisterReceiveCodec(codec) == -1)) {
971 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
972 "Channel::Init() failed to register outband "
973 "'telephone-event' (%d/%d) correctly",
974 codec.pltype, codec.plfreq);
975 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000976 }
977
kwiberg55b97fe2016-01-28 05:22:45 -0800978 if (!STR_CASE_CMP(codec.plname, "CN")) {
979 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
980 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
981 (_rtpRtcpModule->RegisterSendPayload(codec) == -1)) {
982 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
983 "Channel::Init() failed to register CN (%d/%d) "
984 "correctly - 1",
985 codec.pltype, codec.plfreq);
986 }
987 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000988#ifdef WEBRTC_CODEC_RED
kwiberg55b97fe2016-01-28 05:22:45 -0800989 // Register RED to the receiving side of the ACM.
990 // We will not receive an OnInitializeDecoder() callback for RED.
991 if (!STR_CASE_CMP(codec.plname, "RED")) {
992 if (audio_coding_->RegisterReceiveCodec(codec) == -1) {
993 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
994 "Channel::Init() failed to register RED (%d/%d) "
995 "correctly",
996 codec.pltype, codec.plfreq);
997 }
998 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000999#endif
kwiberg55b97fe2016-01-28 05:22:45 -08001000 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001001
kwiberg55b97fe2016-01-28 05:22:45 -08001002 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1003 LOG(LS_ERROR) << "noise_suppression()->set_level(kDefaultNsMode) failed.";
1004 return -1;
1005 }
1006 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1007 LOG(LS_ERROR) << "gain_control()->set_mode(kDefaultRxAgcMode) failed.";
1008 return -1;
1009 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001010
kwiberg55b97fe2016-01-28 05:22:45 -08001011 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001012}
1013
kwiberg55b97fe2016-01-28 05:22:45 -08001014int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1015 OutputMixer& outputMixer,
1016 voe::TransmitMixer& transmitMixer,
1017 ProcessThread& moduleProcessThread,
1018 AudioDeviceModule& audioDeviceModule,
1019 VoiceEngineObserver* voiceEngineObserver,
1020 rtc::CriticalSection* callbackCritSect) {
1021 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1022 "Channel::SetEngineInformation()");
1023 _engineStatisticsPtr = &engineStatistics;
1024 _outputMixerPtr = &outputMixer;
1025 _transmitMixerPtr = &transmitMixer,
1026 _moduleProcessThreadPtr = &moduleProcessThread;
1027 _audioDeviceModulePtr = &audioDeviceModule;
1028 _voiceEngineObserverPtr = voiceEngineObserver;
1029 _callbackCritSectPtr = callbackCritSect;
1030 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001031}
1032
kwiberg55b97fe2016-01-28 05:22:45 -08001033int32_t Channel::UpdateLocalTimeStamp() {
1034 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1035 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001036}
1037
kwibergb7f89d62016-02-17 10:04:18 -08001038void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001039 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001040 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001041}
1042
kwiberg55b97fe2016-01-28 05:22:45 -08001043int32_t Channel::StartPlayout() {
1044 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1045 "Channel::StartPlayout()");
1046 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001047 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001048 }
1049
1050 if (!_externalMixing) {
1051 // Add participant as candidates for mixing.
1052 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1053 _engineStatisticsPtr->SetLastError(
1054 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1055 "StartPlayout() failed to add participant to mixer");
1056 return -1;
1057 }
1058 }
1059
1060 channel_state_.SetPlaying(true);
1061 if (RegisterFilePlayingToMixer() != 0)
1062 return -1;
1063
1064 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001065}
1066
kwiberg55b97fe2016-01-28 05:22:45 -08001067int32_t Channel::StopPlayout() {
1068 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1069 "Channel::StopPlayout()");
1070 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001071 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001072 }
1073
1074 if (!_externalMixing) {
1075 // Remove participant as candidates for mixing
1076 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1077 _engineStatisticsPtr->SetLastError(
1078 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1079 "StopPlayout() failed to remove participant from mixer");
1080 return -1;
1081 }
1082 }
1083
1084 channel_state_.SetPlaying(false);
1085 _outputAudioLevel.Clear();
1086
1087 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001088}
1089
kwiberg55b97fe2016-01-28 05:22:45 -08001090int32_t Channel::StartSend() {
1091 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1092 "Channel::StartSend()");
1093 // Resume the previous sequence number which was reset by StopSend().
1094 // This needs to be done before |sending| is set to true.
1095 if (send_sequence_number_)
1096 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001097
kwiberg55b97fe2016-01-28 05:22:45 -08001098 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001099 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001100 }
1101 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001102
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001103 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001104 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1105 _engineStatisticsPtr->SetLastError(
1106 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1107 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001108 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001109 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001110 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001111 return -1;
1112 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001113
kwiberg55b97fe2016-01-28 05:22:45 -08001114 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001115}
1116
kwiberg55b97fe2016-01-28 05:22:45 -08001117int32_t Channel::StopSend() {
1118 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1119 "Channel::StopSend()");
1120 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001121 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001122 }
1123 channel_state_.SetSending(false);
1124
1125 // Store the sequence number to be able to pick up the same sequence for
1126 // the next StartSend(). This is needed for restarting device, otherwise
1127 // it might cause libSRTP to complain about packets being replayed.
1128 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1129 // CL is landed. See issue
1130 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1131 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1132
1133 // Reset sending SSRC and sequence number and triggers direct transmission
1134 // of RTCP BYE
1135 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1136 _engineStatisticsPtr->SetLastError(
1137 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1138 "StartSend() RTP/RTCP failed to stop sending");
1139 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001140 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001141
1142 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001143}
1144
kwiberg55b97fe2016-01-28 05:22:45 -08001145int32_t Channel::StartReceiving() {
1146 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1147 "Channel::StartReceiving()");
1148 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001149 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001150 }
1151 channel_state_.SetReceiving(true);
1152 _numberOfDiscardedPackets = 0;
1153 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001154}
1155
kwiberg55b97fe2016-01-28 05:22:45 -08001156int32_t Channel::StopReceiving() {
1157 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1158 "Channel::StopReceiving()");
1159 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001160 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001161 }
1162
1163 channel_state_.SetReceiving(false);
1164 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001165}
1166
kwiberg55b97fe2016-01-28 05:22:45 -08001167int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1168 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1169 "Channel::RegisterVoiceEngineObserver()");
1170 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001171
kwiberg55b97fe2016-01-28 05:22:45 -08001172 if (_voiceEngineObserverPtr) {
1173 _engineStatisticsPtr->SetLastError(
1174 VE_INVALID_OPERATION, kTraceError,
1175 "RegisterVoiceEngineObserver() observer already enabled");
1176 return -1;
1177 }
1178 _voiceEngineObserverPtr = &observer;
1179 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001180}
1181
kwiberg55b97fe2016-01-28 05:22:45 -08001182int32_t Channel::DeRegisterVoiceEngineObserver() {
1183 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1184 "Channel::DeRegisterVoiceEngineObserver()");
1185 rtc::CritScope cs(&_callbackCritSect);
1186
1187 if (!_voiceEngineObserverPtr) {
1188 _engineStatisticsPtr->SetLastError(
1189 VE_INVALID_OPERATION, kTraceWarning,
1190 "DeRegisterVoiceEngineObserver() observer already disabled");
1191 return 0;
1192 }
1193 _voiceEngineObserverPtr = NULL;
1194 return 0;
1195}
1196
1197int32_t Channel::GetSendCodec(CodecInst& codec) {
kwiberg1fd4a4a2015-11-03 11:20:50 -08001198 auto send_codec = audio_coding_->SendCodec();
1199 if (send_codec) {
1200 codec = *send_codec;
1201 return 0;
1202 }
1203 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001204}
1205
kwiberg55b97fe2016-01-28 05:22:45 -08001206int32_t Channel::GetRecCodec(CodecInst& codec) {
1207 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001208}
1209
kwiberg55b97fe2016-01-28 05:22:45 -08001210int32_t Channel::SetSendCodec(const CodecInst& codec) {
1211 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1212 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001213
kwiberg55b97fe2016-01-28 05:22:45 -08001214 if (audio_coding_->RegisterSendCodec(codec) != 0) {
1215 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1216 "SetSendCodec() failed to register codec to ACM");
1217 return -1;
1218 }
1219
1220 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1221 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1222 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1223 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1224 "SetSendCodec() failed to register codec to"
1225 " RTP/RTCP module");
1226 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001227 }
kwiberg55b97fe2016-01-28 05:22:45 -08001228 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001229
kwiberg55b97fe2016-01-28 05:22:45 -08001230 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1231 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1232 "SetSendCodec() failed to set audio packet size");
1233 return -1;
1234 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001235
kwiberg55b97fe2016-01-28 05:22:45 -08001236 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001237}
1238
Ivo Creusenadf89b72015-04-29 16:03:33 +02001239void Channel::SetBitRate(int bitrate_bps) {
1240 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1241 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1242 audio_coding_->SetBitRate(bitrate_bps);
1243}
1244
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001245void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001246 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001247 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1248
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001249 // Normalizes rate to 0 - 100.
kwiberg55b97fe2016-01-28 05:22:45 -08001250 if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) !=
1251 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001252 assert(false); // This should not happen.
1253 }
1254}
1255
kwiberg55b97fe2016-01-28 05:22:45 -08001256int32_t Channel::SetVADStatus(bool enableVAD,
1257 ACMVADMode mode,
1258 bool disableDTX) {
1259 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1260 "Channel::SetVADStatus(mode=%d)", mode);
1261 assert(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1262 // To disable VAD, DTX must be disabled too
1263 disableDTX = ((enableVAD == false) ? true : disableDTX);
1264 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0) {
1265 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1266 kTraceError,
1267 "SetVADStatus() failed to set VAD");
1268 return -1;
1269 }
1270 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001271}
1272
kwiberg55b97fe2016-01-28 05:22:45 -08001273int32_t Channel::GetVADStatus(bool& enabledVAD,
1274 ACMVADMode& mode,
1275 bool& disabledDTX) {
1276 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0) {
1277 _engineStatisticsPtr->SetLastError(
1278 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1279 "GetVADStatus() failed to get VAD status");
1280 return -1;
1281 }
1282 disabledDTX = !disabledDTX;
1283 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001284}
1285
kwiberg55b97fe2016-01-28 05:22:45 -08001286int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1287 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1288 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001289
kwiberg55b97fe2016-01-28 05:22:45 -08001290 if (channel_state_.Get().playing) {
1291 _engineStatisticsPtr->SetLastError(
1292 VE_ALREADY_PLAYING, kTraceError,
1293 "SetRecPayloadType() unable to set PT while playing");
1294 return -1;
1295 }
1296 if (channel_state_.Get().receiving) {
1297 _engineStatisticsPtr->SetLastError(
1298 VE_ALREADY_LISTENING, kTraceError,
1299 "SetRecPayloadType() unable to set PT while listening");
1300 return -1;
1301 }
1302
1303 if (codec.pltype == -1) {
1304 // De-register the selected codec (RTP/RTCP module and ACM)
1305
1306 int8_t pltype(-1);
1307 CodecInst rxCodec = codec;
1308
1309 // Get payload type for the given codec
1310 rtp_payload_registry_->ReceivePayloadType(
1311 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1312 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1313 rxCodec.pltype = pltype;
1314
1315 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1316 _engineStatisticsPtr->SetLastError(
1317 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1318 "SetRecPayloadType() RTP/RTCP-module deregistration "
1319 "failed");
1320 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001321 }
kwiberg55b97fe2016-01-28 05:22:45 -08001322 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1323 _engineStatisticsPtr->SetLastError(
1324 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1325 "SetRecPayloadType() ACM deregistration failed - 1");
1326 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001327 }
kwiberg55b97fe2016-01-28 05:22:45 -08001328 return 0;
1329 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001330
kwiberg55b97fe2016-01-28 05:22:45 -08001331 if (rtp_receiver_->RegisterReceivePayload(
1332 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1333 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1334 // First attempt to register failed => de-register and try again
1335 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001336 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001337 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1338 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1339 _engineStatisticsPtr->SetLastError(
1340 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1341 "SetRecPayloadType() RTP/RTCP-module registration failed");
1342 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001343 }
kwiberg55b97fe2016-01-28 05:22:45 -08001344 }
1345 if (audio_coding_->RegisterReceiveCodec(codec) != 0) {
1346 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1347 if (audio_coding_->RegisterReceiveCodec(codec) != 0) {
1348 _engineStatisticsPtr->SetLastError(
1349 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1350 "SetRecPayloadType() ACM registration failed - 1");
1351 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001352 }
kwiberg55b97fe2016-01-28 05:22:45 -08001353 }
1354 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001355}
1356
kwiberg55b97fe2016-01-28 05:22:45 -08001357int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1358 int8_t payloadType(-1);
1359 if (rtp_payload_registry_->ReceivePayloadType(
1360 codec.plname, codec.plfreq, codec.channels,
1361 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1362 _engineStatisticsPtr->SetLastError(
1363 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1364 "GetRecPayloadType() failed to retrieve RX payload type");
1365 return -1;
1366 }
1367 codec.pltype = payloadType;
1368 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001369}
1370
kwiberg55b97fe2016-01-28 05:22:45 -08001371int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1372 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1373 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001374
kwiberg55b97fe2016-01-28 05:22:45 -08001375 CodecInst codec;
1376 int32_t samplingFreqHz(-1);
1377 const size_t kMono = 1;
1378 if (frequency == kFreq32000Hz)
1379 samplingFreqHz = 32000;
1380 else if (frequency == kFreq16000Hz)
1381 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001382
kwiberg55b97fe2016-01-28 05:22:45 -08001383 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1384 _engineStatisticsPtr->SetLastError(
1385 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1386 "SetSendCNPayloadType() failed to retrieve default CN codec "
1387 "settings");
1388 return -1;
1389 }
1390
1391 // Modify the payload type (must be set to dynamic range)
1392 codec.pltype = type;
1393
1394 if (audio_coding_->RegisterSendCodec(codec) != 0) {
1395 _engineStatisticsPtr->SetLastError(
1396 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1397 "SetSendCNPayloadType() failed to register CN to ACM");
1398 return -1;
1399 }
1400
1401 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1402 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1403 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1404 _engineStatisticsPtr->SetLastError(
1405 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1406 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1407 "module");
1408 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001409 }
kwiberg55b97fe2016-01-28 05:22:45 -08001410 }
1411 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001412}
1413
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001414int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001415 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001416 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001417
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001418 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001419 _engineStatisticsPtr->SetLastError(
1420 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001421 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001422 return -1;
1423 }
1424 return 0;
1425}
1426
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001427int Channel::SetOpusDtx(bool enable_dtx) {
1428 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1429 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001430 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001431 : audio_coding_->DisableOpusDtx();
1432 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001433 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1434 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001435 return -1;
1436 }
1437 return 0;
1438}
1439
kwiberg55b97fe2016-01-28 05:22:45 -08001440int32_t Channel::RegisterExternalTransport(Transport& transport) {
1441 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001442 "Channel::RegisterExternalTransport()");
1443
kwiberg55b97fe2016-01-28 05:22:45 -08001444 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001445
kwiberg55b97fe2016-01-28 05:22:45 -08001446 if (_externalTransport) {
1447 _engineStatisticsPtr->SetLastError(
1448 VE_INVALID_OPERATION, kTraceError,
1449 "RegisterExternalTransport() external transport already enabled");
1450 return -1;
1451 }
1452 _externalTransport = true;
1453 _transportPtr = &transport;
1454 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001455}
1456
kwiberg55b97fe2016-01-28 05:22:45 -08001457int32_t Channel::DeRegisterExternalTransport() {
1458 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1459 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001460
kwiberg55b97fe2016-01-28 05:22:45 -08001461 rtc::CritScope cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001462
kwiberg55b97fe2016-01-28 05:22:45 -08001463 if (!_transportPtr) {
1464 _engineStatisticsPtr->SetLastError(
1465 VE_INVALID_OPERATION, kTraceWarning,
1466 "DeRegisterExternalTransport() external transport already "
1467 "disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001468 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001469 }
1470 _externalTransport = false;
1471 _transportPtr = NULL;
1472 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1473 "DeRegisterExternalTransport() all transport is disabled");
1474 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001475}
1476
kwiberg55b97fe2016-01-28 05:22:45 -08001477int32_t Channel::ReceivedRTPPacket(const int8_t* data,
1478 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001479 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001480 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001481 "Channel::ReceivedRTPPacket()");
1482
1483 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001484 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001485
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001486 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001487 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001488 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1489 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1490 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001491 return -1;
1492 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001493 header.payload_type_frequency =
1494 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001495 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001496 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001497 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001498 rtp_receive_statistics_->IncomingPacket(
1499 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001500 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001501
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001502 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001503}
1504
1505bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001506 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001507 const RTPHeader& header,
1508 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001509 if (rtp_payload_registry_->IsRtx(header)) {
1510 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001511 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001512 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001513 assert(packet_length >= header.headerLength);
1514 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001515 PayloadUnion payload_specific;
1516 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001517 &payload_specific)) {
1518 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001519 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001520 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1521 payload_specific, in_order);
1522}
1523
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001524bool Channel::HandleRtxPacket(const uint8_t* packet,
1525 size_t packet_length,
1526 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001527 if (!rtp_payload_registry_->IsRtx(header))
1528 return false;
1529
1530 // Remove the RTX header and parse the original RTP header.
1531 if (packet_length < header.headerLength)
1532 return false;
1533 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1534 return false;
1535 if (restored_packet_in_use_) {
1536 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1537 "Multiple RTX headers detected, dropping packet");
1538 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001539 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001540 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001541 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1542 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001543 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1544 "Incoming RTX packet: invalid RTP header");
1545 return false;
1546 }
1547 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001548 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001549 restored_packet_in_use_ = false;
1550 return ret;
1551}
1552
1553bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1554 StreamStatistician* statistician =
1555 rtp_receive_statistics_->GetStatistician(header.ssrc);
1556 if (!statistician)
1557 return false;
1558 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001559}
1560
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001561bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1562 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001563 // Retransmissions are handled separately if RTX is enabled.
1564 if (rtp_payload_registry_->RtxEnabled())
1565 return false;
1566 StreamStatistician* statistician =
1567 rtp_receive_statistics_->GetStatistician(header.ssrc);
1568 if (!statistician)
1569 return false;
1570 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001571 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001572 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001573 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001574}
1575
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001576int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001577 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001578 "Channel::ReceivedRTCPPacket()");
1579 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001580 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001581
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001582 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001583 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001584 _engineStatisticsPtr->SetLastError(
1585 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1586 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1587 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001588
Minyue2013aec2015-05-13 14:14:42 +02001589 int64_t rtt = GetRTT(true);
1590 if (rtt == 0) {
1591 // Waiting for valid RTT.
1592 return 0;
1593 }
1594 uint32_t ntp_secs = 0;
1595 uint32_t ntp_frac = 0;
1596 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001597 if (0 !=
1598 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1599 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001600 // Waiting for RTCP.
1601 return 0;
1602 }
1603
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001604 {
tommi31fc21f2016-01-21 10:37:37 -08001605 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001606 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001607 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001608 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001609}
1610
niklase@google.com470e71d2011-07-07 08:21:25 +00001611int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001612 bool loop,
1613 FileFormats format,
1614 int startPosition,
1615 float volumeScaling,
1616 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001617 const CodecInst* codecInst) {
1618 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1619 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1620 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1621 "stopPosition=%d)",
1622 fileName, loop, format, volumeScaling, startPosition,
1623 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001624
kwiberg55b97fe2016-01-28 05:22:45 -08001625 if (channel_state_.Get().output_file_playing) {
1626 _engineStatisticsPtr->SetLastError(
1627 VE_ALREADY_PLAYING, kTraceError,
1628 "StartPlayingFileLocally() is already playing");
1629 return -1;
1630 }
1631
1632 {
1633 rtc::CritScope cs(&_fileCritSect);
1634
1635 if (_outputFilePlayerPtr) {
1636 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1637 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1638 _outputFilePlayerPtr = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +00001639 }
1640
kwiberg55b97fe2016-01-28 05:22:45 -08001641 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1642 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001643
kwiberg55b97fe2016-01-28 05:22:45 -08001644 if (_outputFilePlayerPtr == NULL) {
1645 _engineStatisticsPtr->SetLastError(
1646 VE_INVALID_ARGUMENT, kTraceError,
1647 "StartPlayingFileLocally() filePlayer format is not correct");
1648 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001649 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001650
kwiberg55b97fe2016-01-28 05:22:45 -08001651 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001652
kwiberg55b97fe2016-01-28 05:22:45 -08001653 if (_outputFilePlayerPtr->StartPlayingFile(
1654 fileName, loop, startPosition, volumeScaling, notificationTime,
1655 stopPosition, (const CodecInst*)codecInst) != 0) {
1656 _engineStatisticsPtr->SetLastError(
1657 VE_BAD_FILE, kTraceError,
1658 "StartPlayingFile() failed to start file playout");
1659 _outputFilePlayerPtr->StopPlayingFile();
1660 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1661 _outputFilePlayerPtr = NULL;
1662 return -1;
1663 }
1664 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
1665 channel_state_.SetOutputFilePlaying(true);
1666 }
1667
1668 if (RegisterFilePlayingToMixer() != 0)
1669 return -1;
1670
1671 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001672}
1673
1674int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001675 FileFormats format,
1676 int startPosition,
1677 float volumeScaling,
1678 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001679 const CodecInst* codecInst) {
1680 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1681 "Channel::StartPlayingFileLocally(format=%d,"
1682 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1683 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001684
kwiberg55b97fe2016-01-28 05:22:45 -08001685 if (stream == NULL) {
1686 _engineStatisticsPtr->SetLastError(
1687 VE_BAD_FILE, kTraceError,
1688 "StartPlayingFileLocally() NULL as input stream");
1689 return -1;
1690 }
1691
1692 if (channel_state_.Get().output_file_playing) {
1693 _engineStatisticsPtr->SetLastError(
1694 VE_ALREADY_PLAYING, kTraceError,
1695 "StartPlayingFileLocally() is already playing");
1696 return -1;
1697 }
1698
1699 {
1700 rtc::CritScope cs(&_fileCritSect);
1701
1702 // Destroy the old instance
1703 if (_outputFilePlayerPtr) {
1704 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1705 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1706 _outputFilePlayerPtr = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +00001707 }
1708
kwiberg55b97fe2016-01-28 05:22:45 -08001709 // Create the instance
1710 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1711 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001712
kwiberg55b97fe2016-01-28 05:22:45 -08001713 if (_outputFilePlayerPtr == NULL) {
1714 _engineStatisticsPtr->SetLastError(
1715 VE_INVALID_ARGUMENT, kTraceError,
1716 "StartPlayingFileLocally() filePlayer format isnot correct");
1717 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001718 }
1719
kwiberg55b97fe2016-01-28 05:22:45 -08001720 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001721
kwiberg55b97fe2016-01-28 05:22:45 -08001722 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1723 volumeScaling, notificationTime,
1724 stopPosition, codecInst) != 0) {
1725 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1726 "StartPlayingFile() failed to "
1727 "start file playout");
1728 _outputFilePlayerPtr->StopPlayingFile();
1729 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1730 _outputFilePlayerPtr = NULL;
1731 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001732 }
kwiberg55b97fe2016-01-28 05:22:45 -08001733 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
1734 channel_state_.SetOutputFilePlaying(true);
1735 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001736
kwiberg55b97fe2016-01-28 05:22:45 -08001737 if (RegisterFilePlayingToMixer() != 0)
1738 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001739
kwiberg55b97fe2016-01-28 05:22:45 -08001740 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001741}
1742
kwiberg55b97fe2016-01-28 05:22:45 -08001743int Channel::StopPlayingFileLocally() {
1744 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1745 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001746
kwiberg55b97fe2016-01-28 05:22:45 -08001747 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001748 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001749 }
1750
1751 {
1752 rtc::CritScope cs(&_fileCritSect);
1753
1754 if (_outputFilePlayerPtr->StopPlayingFile() != 0) {
1755 _engineStatisticsPtr->SetLastError(
1756 VE_STOP_RECORDING_FAILED, kTraceError,
1757 "StopPlayingFile() could not stop playing");
1758 return -1;
1759 }
1760 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1761 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1762 _outputFilePlayerPtr = NULL;
1763 channel_state_.SetOutputFilePlaying(false);
1764 }
1765 // _fileCritSect cannot be taken while calling
1766 // SetAnonymousMixibilityStatus. Refer to comments in
1767 // StartPlayingFileLocally(const char* ...) for more details.
1768 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1769 _engineStatisticsPtr->SetLastError(
1770 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1771 "StopPlayingFile() failed to stop participant from playing as"
1772 "file in the mixer");
1773 return -1;
1774 }
1775
1776 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001777}
1778
kwiberg55b97fe2016-01-28 05:22:45 -08001779int Channel::IsPlayingFileLocally() const {
1780 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001781}
1782
kwiberg55b97fe2016-01-28 05:22:45 -08001783int Channel::RegisterFilePlayingToMixer() {
1784 // Return success for not registering for file playing to mixer if:
1785 // 1. playing file before playout is started on that channel.
1786 // 2. starting playout without file playing on that channel.
1787 if (!channel_state_.Get().playing ||
1788 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001789 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001790 }
1791
1792 // |_fileCritSect| cannot be taken while calling
1793 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1794 // frames can be pulled by the mixer. Since the frames are generated from
1795 // the file, _fileCritSect will be taken. This would result in a deadlock.
1796 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1797 channel_state_.SetOutputFilePlaying(false);
1798 rtc::CritScope cs(&_fileCritSect);
1799 _engineStatisticsPtr->SetLastError(
1800 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1801 "StartPlayingFile() failed to add participant as file to mixer");
1802 _outputFilePlayerPtr->StopPlayingFile();
1803 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1804 _outputFilePlayerPtr = NULL;
1805 return -1;
1806 }
1807
1808 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001809}
1810
niklase@google.com470e71d2011-07-07 08:21:25 +00001811int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001812 bool loop,
1813 FileFormats format,
1814 int startPosition,
1815 float volumeScaling,
1816 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001817 const CodecInst* codecInst) {
1818 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1819 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1820 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1821 "stopPosition=%d)",
1822 fileName, loop, format, volumeScaling, startPosition,
1823 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001824
kwiberg55b97fe2016-01-28 05:22:45 -08001825 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001826
kwiberg55b97fe2016-01-28 05:22:45 -08001827 if (channel_state_.Get().input_file_playing) {
1828 _engineStatisticsPtr->SetLastError(
1829 VE_ALREADY_PLAYING, kTraceWarning,
1830 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001831 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001832 }
1833
1834 // Destroy the old instance
1835 if (_inputFilePlayerPtr) {
1836 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1837 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1838 _inputFilePlayerPtr = NULL;
1839 }
1840
1841 // Create the instance
1842 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
1843 (const FileFormats)format);
1844
1845 if (_inputFilePlayerPtr == NULL) {
1846 _engineStatisticsPtr->SetLastError(
1847 VE_INVALID_ARGUMENT, kTraceError,
1848 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1849 return -1;
1850 }
1851
1852 const uint32_t notificationTime(0);
1853
1854 if (_inputFilePlayerPtr->StartPlayingFile(
1855 fileName, loop, startPosition, volumeScaling, notificationTime,
1856 stopPosition, (const CodecInst*)codecInst) != 0) {
1857 _engineStatisticsPtr->SetLastError(
1858 VE_BAD_FILE, kTraceError,
1859 "StartPlayingFile() failed to start file playout");
1860 _inputFilePlayerPtr->StopPlayingFile();
1861 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1862 _inputFilePlayerPtr = NULL;
1863 return -1;
1864 }
1865 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
1866 channel_state_.SetInputFilePlaying(true);
1867
1868 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001869}
1870
1871int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001872 FileFormats format,
1873 int startPosition,
1874 float volumeScaling,
1875 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001876 const CodecInst* codecInst) {
1877 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1878 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1879 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1880 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001881
kwiberg55b97fe2016-01-28 05:22:45 -08001882 if (stream == NULL) {
1883 _engineStatisticsPtr->SetLastError(
1884 VE_BAD_FILE, kTraceError,
1885 "StartPlayingFileAsMicrophone NULL as input stream");
1886 return -1;
1887 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001888
kwiberg55b97fe2016-01-28 05:22:45 -08001889 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001890
kwiberg55b97fe2016-01-28 05:22:45 -08001891 if (channel_state_.Get().input_file_playing) {
1892 _engineStatisticsPtr->SetLastError(
1893 VE_ALREADY_PLAYING, kTraceWarning,
1894 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001895 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001896 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001897
kwiberg55b97fe2016-01-28 05:22:45 -08001898 // Destroy the old instance
1899 if (_inputFilePlayerPtr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001900 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1901 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1902 _inputFilePlayerPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001903 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001904
kwiberg55b97fe2016-01-28 05:22:45 -08001905 // Create the instance
1906 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
1907 (const FileFormats)format);
1908
1909 if (_inputFilePlayerPtr == NULL) {
1910 _engineStatisticsPtr->SetLastError(
1911 VE_INVALID_ARGUMENT, kTraceError,
1912 "StartPlayingInputFile() filePlayer format isnot correct");
1913 return -1;
1914 }
1915
1916 const uint32_t notificationTime(0);
1917
1918 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1919 volumeScaling, notificationTime,
1920 stopPosition, codecInst) != 0) {
1921 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1922 "StartPlayingFile() failed to start "
1923 "file playout");
1924 _inputFilePlayerPtr->StopPlayingFile();
1925 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1926 _inputFilePlayerPtr = NULL;
1927 return -1;
1928 }
1929
1930 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
1931 channel_state_.SetInputFilePlaying(true);
1932
1933 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001934}
1935
kwiberg55b97fe2016-01-28 05:22:45 -08001936int Channel::StopPlayingFileAsMicrophone() {
1937 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1938 "Channel::StopPlayingFileAsMicrophone()");
1939
1940 rtc::CritScope cs(&_fileCritSect);
1941
1942 if (!channel_state_.Get().input_file_playing) {
1943 return 0;
1944 }
1945
1946 if (_inputFilePlayerPtr->StopPlayingFile() != 0) {
1947 _engineStatisticsPtr->SetLastError(
1948 VE_STOP_RECORDING_FAILED, kTraceError,
1949 "StopPlayingFile() could not stop playing");
1950 return -1;
1951 }
1952 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1953 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1954 _inputFilePlayerPtr = NULL;
1955 channel_state_.SetInputFilePlaying(false);
1956
1957 return 0;
1958}
1959
1960int Channel::IsPlayingFileAsMicrophone() const {
1961 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001962}
1963
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00001964int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08001965 const CodecInst* codecInst) {
1966 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1967 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00001968
kwiberg55b97fe2016-01-28 05:22:45 -08001969 if (_outputFileRecording) {
1970 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
1971 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001972 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001973 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001974
kwiberg55b97fe2016-01-28 05:22:45 -08001975 FileFormats format;
1976 const uint32_t notificationTime(0); // Not supported in VoE
1977 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00001978
kwiberg55b97fe2016-01-28 05:22:45 -08001979 if ((codecInst != NULL) &&
1980 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
1981 _engineStatisticsPtr->SetLastError(
1982 VE_BAD_ARGUMENT, kTraceError,
1983 "StartRecordingPlayout() invalid compression");
1984 return (-1);
1985 }
1986 if (codecInst == NULL) {
1987 format = kFileFormatPcm16kHzFile;
1988 codecInst = &dummyCodec;
1989 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
1990 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
1991 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
1992 format = kFileFormatWavFile;
1993 } else {
1994 format = kFileFormatCompressedFile;
1995 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001996
kwiberg55b97fe2016-01-28 05:22:45 -08001997 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001998
kwiberg55b97fe2016-01-28 05:22:45 -08001999 // Destroy the old instance
2000 if (_outputFileRecorderPtr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00002001 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2002 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2003 _outputFileRecorderPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08002004 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002005
kwiberg55b97fe2016-01-28 05:22:45 -08002006 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2007 _outputFileRecorderId, (const FileFormats)format);
2008 if (_outputFileRecorderPtr == NULL) {
2009 _engineStatisticsPtr->SetLastError(
2010 VE_INVALID_ARGUMENT, kTraceError,
2011 "StartRecordingPlayout() fileRecorder format isnot correct");
2012 return -1;
2013 }
2014
2015 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2016 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2017 _engineStatisticsPtr->SetLastError(
2018 VE_BAD_FILE, kTraceError,
2019 "StartRecordingAudioFile() failed to start file recording");
2020 _outputFileRecorderPtr->StopRecording();
2021 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2022 _outputFileRecorderPtr = NULL;
2023 return -1;
2024 }
2025 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2026 _outputFileRecording = true;
2027
2028 return 0;
2029}
2030
2031int Channel::StartRecordingPlayout(OutStream* stream,
2032 const CodecInst* codecInst) {
2033 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2034 "Channel::StartRecordingPlayout()");
2035
2036 if (_outputFileRecording) {
2037 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2038 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002039 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002040 }
2041
2042 FileFormats format;
2043 const uint32_t notificationTime(0); // Not supported in VoE
2044 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2045
2046 if (codecInst != NULL && codecInst->channels != 1) {
2047 _engineStatisticsPtr->SetLastError(
2048 VE_BAD_ARGUMENT, kTraceError,
2049 "StartRecordingPlayout() invalid compression");
2050 return (-1);
2051 }
2052 if (codecInst == NULL) {
2053 format = kFileFormatPcm16kHzFile;
2054 codecInst = &dummyCodec;
2055 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2056 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2057 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2058 format = kFileFormatWavFile;
2059 } else {
2060 format = kFileFormatCompressedFile;
2061 }
2062
2063 rtc::CritScope cs(&_fileCritSect);
2064
2065 // Destroy the old instance
2066 if (_outputFileRecorderPtr) {
2067 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2068 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2069 _outputFileRecorderPtr = NULL;
2070 }
2071
2072 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2073 _outputFileRecorderId, (const FileFormats)format);
2074 if (_outputFileRecorderPtr == NULL) {
2075 _engineStatisticsPtr->SetLastError(
2076 VE_INVALID_ARGUMENT, kTraceError,
2077 "StartRecordingPlayout() fileRecorder format isnot correct");
2078 return -1;
2079 }
2080
2081 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2082 notificationTime) != 0) {
2083 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2084 "StartRecordingPlayout() failed to "
2085 "start file recording");
2086 _outputFileRecorderPtr->StopRecording();
2087 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2088 _outputFileRecorderPtr = NULL;
2089 return -1;
2090 }
2091
2092 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2093 _outputFileRecording = true;
2094
2095 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002096}
2097
kwiberg55b97fe2016-01-28 05:22:45 -08002098int Channel::StopRecordingPlayout() {
2099 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2100 "Channel::StopRecordingPlayout()");
2101
2102 if (!_outputFileRecording) {
2103 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2104 "StopRecordingPlayout() isnot recording");
2105 return -1;
2106 }
2107
2108 rtc::CritScope cs(&_fileCritSect);
2109
2110 if (_outputFileRecorderPtr->StopRecording() != 0) {
2111 _engineStatisticsPtr->SetLastError(
2112 VE_STOP_RECORDING_FAILED, kTraceError,
2113 "StopRecording() could not stop recording");
2114 return (-1);
2115 }
2116 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2117 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2118 _outputFileRecorderPtr = NULL;
2119 _outputFileRecording = false;
2120
2121 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002122}
2123
kwiberg55b97fe2016-01-28 05:22:45 -08002124void Channel::SetMixWithMicStatus(bool mix) {
2125 rtc::CritScope cs(&_fileCritSect);
2126 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002127}
2128
kwiberg55b97fe2016-01-28 05:22:45 -08002129int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2130 int8_t currentLevel = _outputAudioLevel.Level();
2131 level = static_cast<int32_t>(currentLevel);
2132 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002133}
2134
kwiberg55b97fe2016-01-28 05:22:45 -08002135int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2136 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2137 level = static_cast<int32_t>(currentLevel);
2138 return 0;
2139}
2140
2141int Channel::SetMute(bool enable) {
2142 rtc::CritScope cs(&volume_settings_critsect_);
2143 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002144 "Channel::SetMute(enable=%d)", enable);
kwiberg55b97fe2016-01-28 05:22:45 -08002145 _mute = enable;
2146 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002147}
2148
kwiberg55b97fe2016-01-28 05:22:45 -08002149bool Channel::Mute() const {
2150 rtc::CritScope cs(&volume_settings_critsect_);
2151 return _mute;
niklase@google.com470e71d2011-07-07 08:21:25 +00002152}
2153
kwiberg55b97fe2016-01-28 05:22:45 -08002154int Channel::SetOutputVolumePan(float left, float right) {
2155 rtc::CritScope cs(&volume_settings_critsect_);
2156 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002157 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002158 _panLeft = left;
2159 _panRight = right;
2160 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002161}
2162
kwiberg55b97fe2016-01-28 05:22:45 -08002163int Channel::GetOutputVolumePan(float& left, float& right) const {
2164 rtc::CritScope cs(&volume_settings_critsect_);
2165 left = _panLeft;
2166 right = _panRight;
2167 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002168}
2169
kwiberg55b97fe2016-01-28 05:22:45 -08002170int Channel::SetChannelOutputVolumeScaling(float scaling) {
2171 rtc::CritScope cs(&volume_settings_critsect_);
2172 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002173 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002174 _outputGain = scaling;
2175 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002176}
2177
kwiberg55b97fe2016-01-28 05:22:45 -08002178int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2179 rtc::CritScope cs(&volume_settings_critsect_);
2180 scaling = _outputGain;
2181 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002182}
2183
solenberg8842c3e2016-03-11 03:06:41 -08002184int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002185 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002186 "Channel::SendTelephoneEventOutband(...)");
2187 RTC_DCHECK_LE(0, event);
2188 RTC_DCHECK_GE(255, event);
2189 RTC_DCHECK_LE(0, duration_ms);
2190 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002191 if (!Sending()) {
2192 return -1;
2193 }
solenberg8842c3e2016-03-11 03:06:41 -08002194 if (_rtpRtcpModule->SendTelephoneEventOutband(
2195 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002196 _engineStatisticsPtr->SetLastError(
2197 VE_SEND_DTMF_FAILED, kTraceWarning,
2198 "SendTelephoneEventOutband() failed to send event");
2199 return -1;
2200 }
2201 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002202}
2203
solenberg31642aa2016-03-14 08:00:37 -07002204int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002205 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002206 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002207 RTC_DCHECK_LE(0, payload_type);
2208 RTC_DCHECK_GE(127, payload_type);
2209 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002210 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002211 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002212 memcpy(codec.plname, "telephone-event", 16);
2213 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2214 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2215 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2216 _engineStatisticsPtr->SetLastError(
2217 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2218 "SetSendTelephoneEventPayloadType() failed to register send"
2219 "payload type");
2220 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002221 }
kwiberg55b97fe2016-01-28 05:22:45 -08002222 }
kwiberg55b97fe2016-01-28 05:22:45 -08002223 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002224}
2225
kwiberg55b97fe2016-01-28 05:22:45 -08002226int Channel::UpdateRxVadDetection(AudioFrame& audioFrame) {
2227 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2228 "Channel::UpdateRxVadDetection()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002229
kwiberg55b97fe2016-01-28 05:22:45 -08002230 int vadDecision = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002231
kwiberg55b97fe2016-01-28 05:22:45 -08002232 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive) ? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002233
kwiberg55b97fe2016-01-28 05:22:45 -08002234 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr) {
2235 OnRxVadDetected(vadDecision);
2236 _oldVadDecision = vadDecision;
2237 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002238
kwiberg55b97fe2016-01-28 05:22:45 -08002239 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2240 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2241 vadDecision);
2242 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002243}
2244
kwiberg55b97fe2016-01-28 05:22:45 -08002245int Channel::RegisterRxVadObserver(VoERxVadCallback& observer) {
2246 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2247 "Channel::RegisterRxVadObserver()");
2248 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002249
kwiberg55b97fe2016-01-28 05:22:45 -08002250 if (_rxVadObserverPtr) {
2251 _engineStatisticsPtr->SetLastError(
2252 VE_INVALID_OPERATION, kTraceError,
2253 "RegisterRxVadObserver() observer already enabled");
2254 return -1;
2255 }
2256 _rxVadObserverPtr = &observer;
2257 _RxVadDetection = true;
2258 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002259}
2260
kwiberg55b97fe2016-01-28 05:22:45 -08002261int Channel::DeRegisterRxVadObserver() {
2262 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2263 "Channel::DeRegisterRxVadObserver()");
2264 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002265
kwiberg55b97fe2016-01-28 05:22:45 -08002266 if (!_rxVadObserverPtr) {
2267 _engineStatisticsPtr->SetLastError(
2268 VE_INVALID_OPERATION, kTraceWarning,
2269 "DeRegisterRxVadObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00002270 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002271 }
2272 _rxVadObserverPtr = NULL;
2273 _RxVadDetection = false;
2274 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002275}
2276
kwiberg55b97fe2016-01-28 05:22:45 -08002277int Channel::VoiceActivityIndicator(int& activity) {
2278 activity = _sendFrameType;
2279 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002280}
2281
2282#ifdef WEBRTC_VOICE_ENGINE_AGC
2283
kwiberg55b97fe2016-01-28 05:22:45 -08002284int Channel::SetRxAgcStatus(bool enable, AgcModes mode) {
2285 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2286 "Channel::SetRxAgcStatus(enable=%d, mode=%d)", (int)enable,
2287 (int)mode);
niklase@google.com470e71d2011-07-07 08:21:25 +00002288
kwiberg55b97fe2016-01-28 05:22:45 -08002289 GainControl::Mode agcMode = kDefaultRxAgcMode;
2290 switch (mode) {
2291 case kAgcDefault:
2292 break;
2293 case kAgcUnchanged:
2294 agcMode = rx_audioproc_->gain_control()->mode();
2295 break;
2296 case kAgcFixedDigital:
2297 agcMode = GainControl::kFixedDigital;
2298 break;
2299 case kAgcAdaptiveDigital:
2300 agcMode = GainControl::kAdaptiveDigital;
2301 break;
2302 default:
2303 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
2304 "SetRxAgcStatus() invalid Agc mode");
2305 return -1;
2306 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002307
kwiberg55b97fe2016-01-28 05:22:45 -08002308 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0) {
2309 _engineStatisticsPtr->SetLastError(
2310 VE_APM_ERROR, kTraceError, "SetRxAgcStatus() failed to set Agc mode");
2311 return -1;
2312 }
2313 if (rx_audioproc_->gain_control()->Enable(enable) != 0) {
2314 _engineStatisticsPtr->SetLastError(
2315 VE_APM_ERROR, kTraceError, "SetRxAgcStatus() failed to set Agc state");
2316 return -1;
2317 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002318
kwiberg55b97fe2016-01-28 05:22:45 -08002319 _rxAgcIsEnabled = enable;
2320 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002321
kwiberg55b97fe2016-01-28 05:22:45 -08002322 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002323}
2324
kwiberg55b97fe2016-01-28 05:22:45 -08002325int Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode) {
2326 bool enable = rx_audioproc_->gain_control()->is_enabled();
2327 GainControl::Mode agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002328
kwiberg55b97fe2016-01-28 05:22:45 -08002329 enabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002330
kwiberg55b97fe2016-01-28 05:22:45 -08002331 switch (agcMode) {
2332 case GainControl::kFixedDigital:
2333 mode = kAgcFixedDigital;
2334 break;
2335 case GainControl::kAdaptiveDigital:
2336 mode = kAgcAdaptiveDigital;
2337 break;
2338 default:
2339 _engineStatisticsPtr->SetLastError(VE_APM_ERROR, kTraceError,
2340 "GetRxAgcStatus() invalid Agc mode");
2341 return -1;
2342 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002343
kwiberg55b97fe2016-01-28 05:22:45 -08002344 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002345}
2346
kwiberg55b97fe2016-01-28 05:22:45 -08002347int Channel::SetRxAgcConfig(AgcConfig config) {
2348 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2349 "Channel::SetRxAgcConfig()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002350
kwiberg55b97fe2016-01-28 05:22:45 -08002351 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
2352 config.targetLeveldBOv) != 0) {
2353 _engineStatisticsPtr->SetLastError(
2354 VE_APM_ERROR, kTraceError,
2355 "SetRxAgcConfig() failed to set target peak |level|"
2356 "(or envelope) of the Agc");
2357 return -1;
2358 }
2359 if (rx_audioproc_->gain_control()->set_compression_gain_db(
2360 config.digitalCompressionGaindB) != 0) {
2361 _engineStatisticsPtr->SetLastError(
2362 VE_APM_ERROR, kTraceError,
2363 "SetRxAgcConfig() failed to set the range in |gain| the"
2364 " digital compression stage may apply");
2365 return -1;
2366 }
2367 if (rx_audioproc_->gain_control()->enable_limiter(config.limiterEnable) !=
2368 0) {
2369 _engineStatisticsPtr->SetLastError(
2370 VE_APM_ERROR, kTraceError,
2371 "SetRxAgcConfig() failed to set hard limiter to the signal");
2372 return -1;
2373 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002374
kwiberg55b97fe2016-01-28 05:22:45 -08002375 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002376}
2377
kwiberg55b97fe2016-01-28 05:22:45 -08002378int Channel::GetRxAgcConfig(AgcConfig& config) {
2379 config.targetLeveldBOv = rx_audioproc_->gain_control()->target_level_dbfs();
2380 config.digitalCompressionGaindB =
2381 rx_audioproc_->gain_control()->compression_gain_db();
2382 config.limiterEnable = rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002383
kwiberg55b97fe2016-01-28 05:22:45 -08002384 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002385}
2386
kwiberg55b97fe2016-01-28 05:22:45 -08002387#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
niklase@google.com470e71d2011-07-07 08:21:25 +00002388
2389#ifdef WEBRTC_VOICE_ENGINE_NR
2390
kwiberg55b97fe2016-01-28 05:22:45 -08002391int Channel::SetRxNsStatus(bool enable, NsModes mode) {
2392 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2393 "Channel::SetRxNsStatus(enable=%d, mode=%d)", (int)enable,
2394 (int)mode);
niklase@google.com470e71d2011-07-07 08:21:25 +00002395
kwiberg55b97fe2016-01-28 05:22:45 -08002396 NoiseSuppression::Level nsLevel = kDefaultNsMode;
2397 switch (mode) {
2398 case kNsDefault:
2399 break;
2400 case kNsUnchanged:
2401 nsLevel = rx_audioproc_->noise_suppression()->level();
2402 break;
2403 case kNsConference:
2404 nsLevel = NoiseSuppression::kHigh;
2405 break;
2406 case kNsLowSuppression:
2407 nsLevel = NoiseSuppression::kLow;
2408 break;
2409 case kNsModerateSuppression:
2410 nsLevel = NoiseSuppression::kModerate;
2411 break;
2412 case kNsHighSuppression:
2413 nsLevel = NoiseSuppression::kHigh;
2414 break;
2415 case kNsVeryHighSuppression:
2416 nsLevel = NoiseSuppression::kVeryHigh;
2417 break;
2418 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002419
kwiberg55b97fe2016-01-28 05:22:45 -08002420 if (rx_audioproc_->noise_suppression()->set_level(nsLevel) != 0) {
2421 _engineStatisticsPtr->SetLastError(
2422 VE_APM_ERROR, kTraceError, "SetRxNsStatus() failed to set NS level");
2423 return -1;
2424 }
2425 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0) {
2426 _engineStatisticsPtr->SetLastError(
2427 VE_APM_ERROR, kTraceError, "SetRxNsStatus() failed to set NS state");
2428 return -1;
2429 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002430
kwiberg55b97fe2016-01-28 05:22:45 -08002431 _rxNsIsEnabled = enable;
2432 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002433
kwiberg55b97fe2016-01-28 05:22:45 -08002434 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002435}
2436
kwiberg55b97fe2016-01-28 05:22:45 -08002437int Channel::GetRxNsStatus(bool& enabled, NsModes& mode) {
2438 bool enable = rx_audioproc_->noise_suppression()->is_enabled();
2439 NoiseSuppression::Level ncLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002440
kwiberg55b97fe2016-01-28 05:22:45 -08002441 enabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002442
kwiberg55b97fe2016-01-28 05:22:45 -08002443 switch (ncLevel) {
2444 case NoiseSuppression::kLow:
2445 mode = kNsLowSuppression;
2446 break;
2447 case NoiseSuppression::kModerate:
2448 mode = kNsModerateSuppression;
2449 break;
2450 case NoiseSuppression::kHigh:
2451 mode = kNsHighSuppression;
2452 break;
2453 case NoiseSuppression::kVeryHigh:
2454 mode = kNsVeryHighSuppression;
2455 break;
2456 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002457
kwiberg55b97fe2016-01-28 05:22:45 -08002458 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002459}
2460
kwiberg55b97fe2016-01-28 05:22:45 -08002461#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
niklase@google.com470e71d2011-07-07 08:21:25 +00002462
kwiberg55b97fe2016-01-28 05:22:45 -08002463int Channel::SetLocalSSRC(unsigned int ssrc) {
2464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2465 "Channel::SetLocalSSRC()");
2466 if (channel_state_.Get().sending) {
2467 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2468 "SetLocalSSRC() already sending");
2469 return -1;
2470 }
2471 _rtpRtcpModule->SetSSRC(ssrc);
2472 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002473}
2474
kwiberg55b97fe2016-01-28 05:22:45 -08002475int Channel::GetLocalSSRC(unsigned int& ssrc) {
2476 ssrc = _rtpRtcpModule->SSRC();
2477 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002478}
2479
kwiberg55b97fe2016-01-28 05:22:45 -08002480int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2481 ssrc = rtp_receiver_->SSRC();
2482 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002483}
2484
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002485int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002486 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002487 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002488}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002489
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002490int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2491 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002492 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2493 if (enable &&
2494 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2495 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002496 return -1;
2497 }
2498 return 0;
2499}
2500
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002501int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2502 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2503}
2504
2505int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2506 rtp_header_parser_->DeregisterRtpHeaderExtension(
2507 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002508 if (enable &&
2509 !rtp_header_parser_->RegisterRtpHeaderExtension(
2510 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002511 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002512 }
2513 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002514}
2515
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002516void Channel::EnableSendTransportSequenceNumber(int id) {
2517 int ret =
2518 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2519 RTC_DCHECK_EQ(0, ret);
2520}
2521
stefan3313ec92016-01-21 06:32:43 -08002522void Channel::EnableReceiveTransportSequenceNumber(int id) {
2523 rtp_header_parser_->DeregisterRtpHeaderExtension(
2524 kRtpExtensionTransportSequenceNumber);
2525 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2526 kRtpExtensionTransportSequenceNumber, id);
2527 RTC_DCHECK(ret);
2528}
2529
stefanbba9dec2016-02-01 04:39:55 -08002530void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002531 RtpPacketSender* rtp_packet_sender,
2532 TransportFeedbackObserver* transport_feedback_observer,
2533 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002534 RTC_DCHECK(rtp_packet_sender);
2535 RTC_DCHECK(transport_feedback_observer);
2536 RTC_DCHECK(packet_router && !packet_router_);
2537 feedback_observer_proxy_->SetTransportFeedbackObserver(
2538 transport_feedback_observer);
2539 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2540 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2541 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002542 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002543 packet_router_ = packet_router;
2544}
2545
stefanbba9dec2016-02-01 04:39:55 -08002546void Channel::RegisterReceiverCongestionControlObjects(
2547 PacketRouter* packet_router) {
2548 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002549 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002550 packet_router_ = packet_router;
2551}
2552
2553void Channel::ResetCongestionControlObjects() {
2554 RTC_DCHECK(packet_router_);
2555 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2556 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2557 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002558 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002559 packet_router_ = nullptr;
2560 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2561}
2562
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002563void Channel::SetRTCPStatus(bool enable) {
2564 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2565 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002566 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002567}
2568
kwiberg55b97fe2016-01-28 05:22:45 -08002569int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002570 RtcpMode method = _rtpRtcpModule->RTCP();
2571 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002572 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002573}
2574
kwiberg55b97fe2016-01-28 05:22:45 -08002575int Channel::SetRTCP_CNAME(const char cName[256]) {
2576 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2577 "Channel::SetRTCP_CNAME()");
2578 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2579 _engineStatisticsPtr->SetLastError(
2580 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2581 "SetRTCP_CNAME() failed to set RTCP CNAME");
2582 return -1;
2583 }
2584 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002585}
2586
kwiberg55b97fe2016-01-28 05:22:45 -08002587int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2588 if (cName == NULL) {
2589 _engineStatisticsPtr->SetLastError(
2590 VE_INVALID_ARGUMENT, kTraceError,
2591 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2592 return -1;
2593 }
2594 char cname[RTCP_CNAME_SIZE];
2595 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2596 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2597 _engineStatisticsPtr->SetLastError(
2598 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2599 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2600 return -1;
2601 }
2602 strcpy(cName, cname);
2603 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002604}
2605
kwiberg55b97fe2016-01-28 05:22:45 -08002606int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2607 unsigned int& NTPLow,
2608 unsigned int& timestamp,
2609 unsigned int& playoutTimestamp,
2610 unsigned int* jitter,
2611 unsigned short* fractionLost) {
2612 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002613
kwiberg55b97fe2016-01-28 05:22:45 -08002614 RTCPSenderInfo senderInfo;
2615 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2616 _engineStatisticsPtr->SetLastError(
2617 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2618 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2619 "side");
2620 return -1;
2621 }
2622
2623 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2624 // and octet count)
2625 NTPHigh = senderInfo.NTPseconds;
2626 NTPLow = senderInfo.NTPfraction;
2627 timestamp = senderInfo.RTPtimeStamp;
2628
2629 // --- Locally derived information
2630
2631 // This value is updated on each incoming RTCP packet (0 when no packet
2632 // has been received)
2633 playoutTimestamp = playout_timestamp_rtcp_;
2634
2635 if (NULL != jitter || NULL != fractionLost) {
2636 // Get all RTCP receiver report blocks that have been received on this
2637 // channel. If we receive RTP packets from a remote source we know the
2638 // remote SSRC and use the report block from him.
2639 // Otherwise use the first report block.
2640 std::vector<RTCPReportBlock> remote_stats;
2641 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2642 remote_stats.empty()) {
2643 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2644 "GetRemoteRTCPData() failed to measure statistics due"
2645 " to lack of received RTP and/or RTCP packets");
2646 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002647 }
2648
kwiberg55b97fe2016-01-28 05:22:45 -08002649 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2650 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2651 for (; it != remote_stats.end(); ++it) {
2652 if (it->remoteSSRC == remoteSSRC)
2653 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002654 }
kwiberg55b97fe2016-01-28 05:22:45 -08002655
2656 if (it == remote_stats.end()) {
2657 // If we have not received any RTCP packets from this SSRC it probably
2658 // means that we have not received any RTP packets.
2659 // Use the first received report block instead.
2660 it = remote_stats.begin();
2661 remoteSSRC = it->remoteSSRC;
2662 }
2663
2664 if (jitter) {
2665 *jitter = it->jitter;
2666 }
2667
2668 if (fractionLost) {
2669 *fractionLost = it->fractionLost;
2670 }
2671 }
2672 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002673}
2674
kwiberg55b97fe2016-01-28 05:22:45 -08002675int Channel::SendApplicationDefinedRTCPPacket(
2676 unsigned char subType,
2677 unsigned int name,
2678 const char* data,
2679 unsigned short dataLengthInBytes) {
2680 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2681 "Channel::SendApplicationDefinedRTCPPacket()");
2682 if (!channel_state_.Get().sending) {
2683 _engineStatisticsPtr->SetLastError(
2684 VE_NOT_SENDING, kTraceError,
2685 "SendApplicationDefinedRTCPPacket() not sending");
2686 return -1;
2687 }
2688 if (NULL == data) {
2689 _engineStatisticsPtr->SetLastError(
2690 VE_INVALID_ARGUMENT, kTraceError,
2691 "SendApplicationDefinedRTCPPacket() invalid data value");
2692 return -1;
2693 }
2694 if (dataLengthInBytes % 4 != 0) {
2695 _engineStatisticsPtr->SetLastError(
2696 VE_INVALID_ARGUMENT, kTraceError,
2697 "SendApplicationDefinedRTCPPacket() invalid length value");
2698 return -1;
2699 }
2700 RtcpMode status = _rtpRtcpModule->RTCP();
2701 if (status == RtcpMode::kOff) {
2702 _engineStatisticsPtr->SetLastError(
2703 VE_RTCP_ERROR, kTraceError,
2704 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2705 return -1;
2706 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002707
kwiberg55b97fe2016-01-28 05:22:45 -08002708 // Create and schedule the RTCP APP packet for transmission
2709 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2710 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2711 _engineStatisticsPtr->SetLastError(
2712 VE_SEND_ERROR, kTraceError,
2713 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2714 return -1;
2715 }
2716 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002717}
2718
kwiberg55b97fe2016-01-28 05:22:45 -08002719int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2720 unsigned int& maxJitterMs,
2721 unsigned int& discardedPackets) {
2722 // The jitter statistics is updated for each received RTP packet and is
2723 // based on received packets.
2724 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2725 // If RTCP is off, there is no timed thread in the RTCP module regularly
2726 // generating new stats, trigger the update manually here instead.
2727 StreamStatistician* statistician =
2728 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2729 if (statistician) {
2730 // Don't use returned statistics, use data from proxy instead so that
2731 // max jitter can be fetched atomically.
2732 RtcpStatistics s;
2733 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002734 }
kwiberg55b97fe2016-01-28 05:22:45 -08002735 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002736
kwiberg55b97fe2016-01-28 05:22:45 -08002737 ChannelStatistics stats = statistics_proxy_->GetStats();
2738 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2739 if (playoutFrequency > 0) {
2740 // Scale RTP statistics given the current playout frequency
2741 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2742 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2743 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002744
kwiberg55b97fe2016-01-28 05:22:45 -08002745 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002746
kwiberg55b97fe2016-01-28 05:22:45 -08002747 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002748}
2749
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002750int Channel::GetRemoteRTCPReportBlocks(
2751 std::vector<ReportBlock>* report_blocks) {
2752 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002753 _engineStatisticsPtr->SetLastError(
2754 VE_INVALID_ARGUMENT, kTraceError,
2755 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002756 return -1;
2757 }
2758
2759 // Get the report blocks from the latest received RTCP Sender or Receiver
2760 // Report. Each element in the vector contains the sender's SSRC and a
2761 // report block according to RFC 3550.
2762 std::vector<RTCPReportBlock> rtcp_report_blocks;
2763 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002764 return -1;
2765 }
2766
2767 if (rtcp_report_blocks.empty())
2768 return 0;
2769
2770 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2771 for (; it != rtcp_report_blocks.end(); ++it) {
2772 ReportBlock report_block;
2773 report_block.sender_SSRC = it->remoteSSRC;
2774 report_block.source_SSRC = it->sourceSSRC;
2775 report_block.fraction_lost = it->fractionLost;
2776 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2777 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2778 report_block.interarrival_jitter = it->jitter;
2779 report_block.last_SR_timestamp = it->lastSR;
2780 report_block.delay_since_last_SR = it->delaySinceLastSR;
2781 report_blocks->push_back(report_block);
2782 }
2783 return 0;
2784}
2785
kwiberg55b97fe2016-01-28 05:22:45 -08002786int Channel::GetRTPStatistics(CallStatistics& stats) {
2787 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002788
kwiberg55b97fe2016-01-28 05:22:45 -08002789 // The jitter statistics is updated for each received RTP packet and is
2790 // based on received packets.
2791 RtcpStatistics statistics;
2792 StreamStatistician* statistician =
2793 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002794 if (statistician) {
2795 statistician->GetStatistics(&statistics,
2796 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002797 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002798
kwiberg55b97fe2016-01-28 05:22:45 -08002799 stats.fractionLost = statistics.fraction_lost;
2800 stats.cumulativeLost = statistics.cumulative_lost;
2801 stats.extendedMax = statistics.extended_max_sequence_number;
2802 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002803
kwiberg55b97fe2016-01-28 05:22:45 -08002804 // --- RTT
2805 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002806
kwiberg55b97fe2016-01-28 05:22:45 -08002807 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002808
kwiberg55b97fe2016-01-28 05:22:45 -08002809 size_t bytesSent(0);
2810 uint32_t packetsSent(0);
2811 size_t bytesReceived(0);
2812 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002813
kwiberg55b97fe2016-01-28 05:22:45 -08002814 if (statistician) {
2815 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2816 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002817
kwiberg55b97fe2016-01-28 05:22:45 -08002818 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2819 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2820 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2821 " output will not be complete");
2822 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002823
kwiberg55b97fe2016-01-28 05:22:45 -08002824 stats.bytesSent = bytesSent;
2825 stats.packetsSent = packetsSent;
2826 stats.bytesReceived = bytesReceived;
2827 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002828
kwiberg55b97fe2016-01-28 05:22:45 -08002829 // --- Timestamps
2830 {
2831 rtc::CritScope lock(&ts_stats_lock_);
2832 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2833 }
2834 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002835}
2836
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002837int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00002838 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002839 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002840
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00002841 if (enable) {
2842 if (redPayloadtype < 0 || redPayloadtype > 127) {
2843 _engineStatisticsPtr->SetLastError(
2844 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002845 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00002846 return -1;
2847 }
2848
2849 if (SetRedPayloadType(redPayloadtype) < 0) {
2850 _engineStatisticsPtr->SetLastError(
2851 VE_CODEC_ERROR, kTraceError,
2852 "SetSecondarySendCodec() Failed to register RED ACM");
2853 return -1;
2854 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00002855 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002856
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00002857 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00002858 _engineStatisticsPtr->SetLastError(
2859 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00002860 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00002861 return -1;
2862 }
2863 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002864}
2865
kwiberg55b97fe2016-01-28 05:22:45 -08002866int Channel::GetREDStatus(bool& enabled, int& redPayloadtype) {
2867 enabled = audio_coding_->REDStatus();
2868 if (enabled) {
2869 int8_t payloadType = 0;
2870 if (_rtpRtcpModule->SendREDPayloadType(&payloadType) != 0) {
2871 _engineStatisticsPtr->SetLastError(
2872 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2873 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
2874 "module");
2875 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002876 }
kwiberg55b97fe2016-01-28 05:22:45 -08002877 redPayloadtype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00002878 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002879 }
2880 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002881}
2882
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002883int Channel::SetCodecFECStatus(bool enable) {
2884 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2885 "Channel::SetCodecFECStatus()");
2886
2887 if (audio_coding_->SetCodecFEC(enable) != 0) {
2888 _engineStatisticsPtr->SetLastError(
2889 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2890 "SetCodecFECStatus() failed to set FEC state");
2891 return -1;
2892 }
2893 return 0;
2894}
2895
2896bool Channel::GetCodecFECStatus() {
2897 bool enabled = audio_coding_->CodecFEC();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002898 return enabled;
2899}
2900
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002901void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2902 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002903 // If pacing is enabled we always store packets.
2904 if (!pacing_enabled_)
2905 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002906 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
2907 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002908 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002909 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002910 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002911 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002912}
2913
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002914// Called when we are missing one or more packets.
2915int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002916 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2917}
2918
kwiberg55b97fe2016-01-28 05:22:45 -08002919uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2920 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2921 "Channel::Demultiplex()");
2922 _audioFrame.CopyFrom(audioFrame);
2923 _audioFrame.id_ = _channelId;
2924 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002925}
2926
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002927void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002928 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002929 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002930 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002931 CodecInst codec;
2932 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002933
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002934 // Never upsample or upmix the capture signal here. This should be done at the
2935 // end of the send chain.
2936 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2937 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2938 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2939 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002940}
2941
kwiberg55b97fe2016-01-28 05:22:45 -08002942uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2943 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2944 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002945
kwiberg55b97fe2016-01-28 05:22:45 -08002946 if (_audioFrame.samples_per_channel_ == 0) {
2947 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2948 "Channel::PrepareEncodeAndSend() invalid audio frame");
2949 return 0xFFFFFFFF;
2950 }
2951
2952 if (channel_state_.Get().input_file_playing) {
2953 MixOrReplaceAudioWithFile(mixingFrequency);
2954 }
2955
2956 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
2957 if (is_muted) {
2958 AudioFrameOperations::Mute(_audioFrame);
2959 }
2960
2961 if (channel_state_.Get().input_external_media) {
2962 rtc::CritScope cs(&_callbackCritSect);
2963 const bool isStereo = (_audioFrame.num_channels_ == 2);
2964 if (_inputExternalMediaCallbackPtr) {
2965 _inputExternalMediaCallbackPtr->Process(
2966 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2967 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2968 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002969 }
kwiberg55b97fe2016-01-28 05:22:45 -08002970 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002971
kwiberg55b97fe2016-01-28 05:22:45 -08002972 if (_includeAudioLevelIndication) {
2973 size_t length =
2974 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00002975 if (is_muted) {
kwiberg55b97fe2016-01-28 05:22:45 -08002976 rms_level_.ProcessMuted(length);
2977 } else {
2978 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002979 }
kwiberg55b97fe2016-01-28 05:22:45 -08002980 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002981
kwiberg55b97fe2016-01-28 05:22:45 -08002982 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002983}
2984
kwiberg55b97fe2016-01-28 05:22:45 -08002985uint32_t Channel::EncodeAndSend() {
2986 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2987 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002988
kwiberg55b97fe2016-01-28 05:22:45 -08002989 assert(_audioFrame.num_channels_ <= 2);
2990 if (_audioFrame.samples_per_channel_ == 0) {
2991 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2992 "Channel::EncodeAndSend() invalid audio frame");
2993 return 0xFFFFFFFF;
2994 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002995
kwiberg55b97fe2016-01-28 05:22:45 -08002996 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002997
kwiberg55b97fe2016-01-28 05:22:45 -08002998 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002999
kwiberg55b97fe2016-01-28 05:22:45 -08003000 // The ACM resamples internally.
3001 _audioFrame.timestamp_ = _timeStamp;
3002 // This call will trigger AudioPacketizationCallback::SendData if encoding
3003 // is done and payload is ready for packetization and transmission.
3004 // Otherwise, it will return without invoking the callback.
3005 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
3006 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
3007 "Channel::EncodeAndSend() ACM encoding failed");
3008 return 0xFFFFFFFF;
3009 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003010
kwiberg55b97fe2016-01-28 05:22:45 -08003011 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
3012 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003013}
3014
Minyue2013aec2015-05-13 14:14:42 +02003015void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08003016 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003017 Channel* channel = associate_send_channel_.channel();
3018 if (channel && channel->ChannelId() == channel_id) {
3019 // If this channel is associated with a send channel of the specified
3020 // Channel ID, disassociate with it.
3021 ChannelOwner ref(NULL);
3022 associate_send_channel_ = ref;
3023 }
3024}
3025
kwiberg55b97fe2016-01-28 05:22:45 -08003026int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
3027 VoEMediaProcess& processObject) {
3028 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3029 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003030
kwiberg55b97fe2016-01-28 05:22:45 -08003031 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003032
kwiberg55b97fe2016-01-28 05:22:45 -08003033 if (kPlaybackPerChannel == type) {
3034 if (_outputExternalMediaCallbackPtr) {
3035 _engineStatisticsPtr->SetLastError(
3036 VE_INVALID_OPERATION, kTraceError,
3037 "Channel::RegisterExternalMediaProcessing() "
3038 "output external media already enabled");
3039 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003040 }
kwiberg55b97fe2016-01-28 05:22:45 -08003041 _outputExternalMediaCallbackPtr = &processObject;
3042 _outputExternalMedia = true;
3043 } else if (kRecordingPerChannel == type) {
3044 if (_inputExternalMediaCallbackPtr) {
3045 _engineStatisticsPtr->SetLastError(
3046 VE_INVALID_OPERATION, kTraceError,
3047 "Channel::RegisterExternalMediaProcessing() "
3048 "output external media already enabled");
3049 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003050 }
kwiberg55b97fe2016-01-28 05:22:45 -08003051 _inputExternalMediaCallbackPtr = &processObject;
3052 channel_state_.SetInputExternalMedia(true);
3053 }
3054 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003055}
3056
kwiberg55b97fe2016-01-28 05:22:45 -08003057int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
3058 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3059 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003060
kwiberg55b97fe2016-01-28 05:22:45 -08003061 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003062
kwiberg55b97fe2016-01-28 05:22:45 -08003063 if (kPlaybackPerChannel == type) {
3064 if (!_outputExternalMediaCallbackPtr) {
3065 _engineStatisticsPtr->SetLastError(
3066 VE_INVALID_OPERATION, kTraceWarning,
3067 "Channel::DeRegisterExternalMediaProcessing() "
3068 "output external media already disabled");
3069 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003070 }
kwiberg55b97fe2016-01-28 05:22:45 -08003071 _outputExternalMedia = false;
3072 _outputExternalMediaCallbackPtr = NULL;
3073 } else if (kRecordingPerChannel == type) {
3074 if (!_inputExternalMediaCallbackPtr) {
3075 _engineStatisticsPtr->SetLastError(
3076 VE_INVALID_OPERATION, kTraceWarning,
3077 "Channel::DeRegisterExternalMediaProcessing() "
3078 "input external media already disabled");
3079 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003080 }
kwiberg55b97fe2016-01-28 05:22:45 -08003081 channel_state_.SetInputExternalMedia(false);
3082 _inputExternalMediaCallbackPtr = NULL;
3083 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003084
kwiberg55b97fe2016-01-28 05:22:45 -08003085 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003086}
3087
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003088int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08003089 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3090 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003091
kwiberg55b97fe2016-01-28 05:22:45 -08003092 if (channel_state_.Get().playing) {
3093 _engineStatisticsPtr->SetLastError(
3094 VE_INVALID_OPERATION, kTraceError,
3095 "Channel::SetExternalMixing() "
3096 "external mixing cannot be changed while playing.");
3097 return -1;
3098 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003099
kwiberg55b97fe2016-01-28 05:22:45 -08003100 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003101
kwiberg55b97fe2016-01-28 05:22:45 -08003102 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003103}
3104
kwiberg55b97fe2016-01-28 05:22:45 -08003105int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
3106 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003107}
3108
wu@webrtc.org24301a62013-12-13 19:17:43 +00003109void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3110 audio_coding_->GetDecodingCallStatistics(stats);
3111}
3112
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003113bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3114 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08003115 rtc::CritScope lock(&video_sync_lock_);
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003116 if (_average_jitter_buffer_delay_us == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003117 return false;
3118 }
kwiberg55b97fe2016-01-28 05:22:45 -08003119 *jitter_buffer_delay_ms =
3120 (_average_jitter_buffer_delay_us + 500) / 1000 + _recPacketDelayMs;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003121 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003122 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003123}
3124
solenberg358057b2015-11-27 10:46:42 -08003125uint32_t Channel::GetDelayEstimate() const {
3126 int jitter_buffer_delay_ms = 0;
3127 int playout_buffer_delay_ms = 0;
3128 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
3129 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
3130}
3131
deadbeef74375882015-08-13 12:09:10 -07003132int Channel::LeastRequiredDelayMs() const {
3133 return audio_coding_->LeastRequiredDelayMs();
3134}
3135
kwiberg55b97fe2016-01-28 05:22:45 -08003136int Channel::SetMinimumPlayoutDelay(int delayMs) {
3137 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3138 "Channel::SetMinimumPlayoutDelay()");
3139 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3140 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
3141 _engineStatisticsPtr->SetLastError(
3142 VE_INVALID_ARGUMENT, kTraceError,
3143 "SetMinimumPlayoutDelay() invalid min delay");
3144 return -1;
3145 }
3146 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
3147 _engineStatisticsPtr->SetLastError(
3148 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3149 "SetMinimumPlayoutDelay() failed to set min playout delay");
3150 return -1;
3151 }
3152 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003153}
3154
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003155int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07003156 uint32_t playout_timestamp_rtp = 0;
3157 {
tommi31fc21f2016-01-21 10:37:37 -08003158 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003159 playout_timestamp_rtp = playout_timestamp_rtp_;
3160 }
kwiberg55b97fe2016-01-28 05:22:45 -08003161 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003162 _engineStatisticsPtr->SetLastError(
3163 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3164 "GetPlayoutTimestamp() failed to retrieve timestamp");
3165 return -1;
3166 }
deadbeef74375882015-08-13 12:09:10 -07003167 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003168 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003169}
3170
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003171int Channel::SetInitTimestamp(unsigned int timestamp) {
3172 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003173 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003174 if (channel_state_.Get().sending) {
3175 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3176 "SetInitTimestamp() already sending");
3177 return -1;
3178 }
3179 _rtpRtcpModule->SetStartTimestamp(timestamp);
3180 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003181}
3182
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003183int Channel::SetInitSequenceNumber(short sequenceNumber) {
3184 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3185 "Channel::SetInitSequenceNumber()");
3186 if (channel_state_.Get().sending) {
3187 _engineStatisticsPtr->SetLastError(
3188 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3189 return -1;
3190 }
3191 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3192 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003193}
3194
kwiberg55b97fe2016-01-28 05:22:45 -08003195int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3196 RtpReceiver** rtp_receiver) const {
3197 *rtpRtcpModule = _rtpRtcpModule.get();
3198 *rtp_receiver = rtp_receiver_.get();
3199 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003200}
3201
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003202// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3203// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003204int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003205 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003206 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003207
kwiberg55b97fe2016-01-28 05:22:45 -08003208 {
3209 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003210
kwiberg55b97fe2016-01-28 05:22:45 -08003211 if (_inputFilePlayerPtr == NULL) {
3212 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3213 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3214 " doesnt exist");
3215 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003216 }
3217
kwiberg55b97fe2016-01-28 05:22:45 -08003218 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(), fileSamples,
3219 mixingFrequency) == -1) {
3220 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3221 "Channel::MixOrReplaceAudioWithFile() file mixing "
3222 "failed");
3223 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003224 }
kwiberg55b97fe2016-01-28 05:22:45 -08003225 if (fileSamples == 0) {
3226 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3227 "Channel::MixOrReplaceAudioWithFile() file is ended");
3228 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003229 }
kwiberg55b97fe2016-01-28 05:22:45 -08003230 }
3231
3232 assert(_audioFrame.samples_per_channel_ == fileSamples);
3233
3234 if (_mixFileWithMicrophone) {
3235 // Currently file stream is always mono.
3236 // TODO(xians): Change the code when FilePlayer supports real stereo.
3237 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3238 1, fileSamples);
3239 } else {
3240 // Replace ACM audio with file.
3241 // Currently file stream is always mono.
3242 // TODO(xians): Change the code when FilePlayer supports real stereo.
3243 _audioFrame.UpdateFrame(
3244 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3245 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3246 }
3247 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003248}
3249
kwiberg55b97fe2016-01-28 05:22:45 -08003250int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3251 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003252
kwibergb7f89d62016-02-17 10:04:18 -08003253 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003254 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003255
kwiberg55b97fe2016-01-28 05:22:45 -08003256 {
3257 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003258
kwiberg55b97fe2016-01-28 05:22:45 -08003259 if (_outputFilePlayerPtr == NULL) {
3260 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3261 "Channel::MixAudioWithFile() file mixing failed");
3262 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003263 }
3264
kwiberg55b97fe2016-01-28 05:22:45 -08003265 // We should get the frequency we ask for.
3266 if (_outputFilePlayerPtr->Get10msAudioFromFile(
3267 fileBuffer.get(), fileSamples, mixingFrequency) == -1) {
3268 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3269 "Channel::MixAudioWithFile() file mixing failed");
3270 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003271 }
kwiberg55b97fe2016-01-28 05:22:45 -08003272 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003273
kwiberg55b97fe2016-01-28 05:22:45 -08003274 if (audioFrame.samples_per_channel_ == fileSamples) {
3275 // Currently file stream is always mono.
3276 // TODO(xians): Change the code when FilePlayer supports real stereo.
3277 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3278 fileSamples);
3279 } else {
3280 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3281 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3282 ") != "
3283 "fileSamples(%" PRIuS ")",
3284 audioFrame.samples_per_channel_, fileSamples);
3285 return -1;
3286 }
3287
3288 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003289}
3290
deadbeef74375882015-08-13 12:09:10 -07003291void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3292 uint32_t playout_timestamp = 0;
3293
kwiberg55b97fe2016-01-28 05:22:45 -08003294 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
deadbeef74375882015-08-13 12:09:10 -07003295 // This can happen if this channel has not been received any RTP packet. In
3296 // this case, NetEq is not capable of computing playout timestamp.
3297 return;
3298 }
3299
3300 uint16_t delay_ms = 0;
3301 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003302 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003303 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3304 " delay from the ADM");
3305 _engineStatisticsPtr->SetLastError(
3306 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3307 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3308 return;
3309 }
3310
3311 jitter_buffer_playout_timestamp_ = playout_timestamp;
3312
3313 // Remove the playout delay.
3314 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
3315
kwiberg55b97fe2016-01-28 05:22:45 -08003316 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003317 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3318 playout_timestamp);
3319
3320 {
tommi31fc21f2016-01-21 10:37:37 -08003321 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003322 if (rtcp) {
3323 playout_timestamp_rtcp_ = playout_timestamp;
3324 } else {
3325 playout_timestamp_rtp_ = playout_timestamp;
3326 }
3327 playout_delay_ms_ = delay_ms;
3328 }
3329}
3330
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003331// Called for incoming RTP packets after successful RTP header parsing.
3332void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
3333 uint16_t sequence_number) {
kwiberg55b97fe2016-01-28 05:22:45 -08003334 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003335 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
3336 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00003337
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003338 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00003339 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00003340
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003341 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
3342 // every incoming packet.
kwiberg55b97fe2016-01-28 05:22:45 -08003343 uint32_t timestamp_diff_ms =
3344 (rtp_timestamp - jitter_buffer_playout_timestamp_) /
3345 (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00003346 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
3347 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
3348 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
3349 // timestamp, the resulting difference is negative, but is set to zero.
3350 // This can happen when a network glitch causes a packet to arrive late,
3351 // and during long comfort noise periods with clock drift.
3352 timestamp_diff_ms = 0;
3353 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003354
kwiberg55b97fe2016-01-28 05:22:45 -08003355 uint16_t packet_delay_ms =
3356 (rtp_timestamp - _previousTimestamp) / (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003357
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003358 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00003359
kwiberg55b97fe2016-01-28 05:22:45 -08003360 if (timestamp_diff_ms == 0)
3361 return;
niklase@google.com470e71d2011-07-07 08:21:25 +00003362
deadbeef74375882015-08-13 12:09:10 -07003363 {
tommi31fc21f2016-01-21 10:37:37 -08003364 rtc::CritScope lock(&video_sync_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003365
deadbeef74375882015-08-13 12:09:10 -07003366 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
3367 _recPacketDelayMs = packet_delay_ms;
3368 }
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003369
deadbeef74375882015-08-13 12:09:10 -07003370 if (_average_jitter_buffer_delay_us == 0) {
3371 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
3372 return;
3373 }
3374
3375 // Filter average delay value using exponential filter (alpha is
3376 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
3377 // risk of rounding error) and compensate for it in GetDelayEstimate()
3378 // later.
kwiberg55b97fe2016-01-28 05:22:45 -08003379 _average_jitter_buffer_delay_us =
3380 (_average_jitter_buffer_delay_us * 7 + 1000 * timestamp_diff_ms + 500) /
3381 8;
deadbeef74375882015-08-13 12:09:10 -07003382 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003383}
3384
kwiberg55b97fe2016-01-28 05:22:45 -08003385void Channel::RegisterReceiveCodecsToRTPModule() {
3386 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3387 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003388
kwiberg55b97fe2016-01-28 05:22:45 -08003389 CodecInst codec;
3390 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003391
kwiberg55b97fe2016-01-28 05:22:45 -08003392 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3393 // Open up the RTP/RTCP receiver for all supported codecs
3394 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3395 (rtp_receiver_->RegisterReceivePayload(
3396 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3397 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3398 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3399 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3400 " to register %s (%d/%d/%" PRIuS
3401 "/%d) to RTP/RTCP "
3402 "receiver",
3403 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3404 codec.rate);
3405 } else {
3406 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3407 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3408 "(%d/%d/%" PRIuS
3409 "/%d) has been added to the RTP/RTCP "
3410 "receiver",
3411 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3412 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003413 }
kwiberg55b97fe2016-01-28 05:22:45 -08003414 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003415}
3416
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003417// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003418int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003419 CodecInst codec;
3420 bool found_red = false;
3421
3422 // Get default RED settings from the ACM database
3423 const int num_codecs = AudioCodingModule::NumberOfCodecs();
3424 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003425 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003426 if (!STR_CASE_CMP(codec.plname, "RED")) {
3427 found_red = true;
3428 break;
3429 }
3430 }
3431
3432 if (!found_red) {
3433 _engineStatisticsPtr->SetLastError(
3434 VE_CODEC_ERROR, kTraceError,
3435 "SetRedPayloadType() RED is not supported");
3436 return -1;
3437 }
3438
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00003439 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003440 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003441 _engineStatisticsPtr->SetLastError(
3442 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3443 "SetRedPayloadType() RED registration in ACM module failed");
3444 return -1;
3445 }
3446
3447 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
3448 _engineStatisticsPtr->SetLastError(
3449 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3450 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
3451 return -1;
3452 }
3453 return 0;
3454}
3455
kwiberg55b97fe2016-01-28 05:22:45 -08003456int Channel::SetSendRtpHeaderExtension(bool enable,
3457 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003458 unsigned char id) {
3459 int error = 0;
3460 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3461 if (enable) {
3462 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3463 }
3464 return error;
3465}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003466
wu@webrtc.org94454b72014-06-05 20:34:08 +00003467int32_t Channel::GetPlayoutFrequency() {
3468 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3469 CodecInst current_recive_codec;
3470 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3471 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3472 // Even though the actual sampling rate for G.722 audio is
3473 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3474 // 8,000 Hz because that value was erroneously assigned in
3475 // RFC 1890 and must remain unchanged for backward compatibility.
3476 playout_frequency = 8000;
3477 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3478 // We are resampling Opus internally to 32,000 Hz until all our
3479 // DSP routines can operate at 48,000 Hz, but the RTP clock
3480 // rate for the Opus payload format is standardized to 48,000 Hz,
3481 // because that is the maximum supported decoding sampling rate.
3482 playout_frequency = 48000;
3483 }
3484 }
3485 return playout_frequency;
3486}
3487
Minyue2013aec2015-05-13 14:14:42 +02003488int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003489 RtcpMode method = _rtpRtcpModule->RTCP();
3490 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003491 return 0;
3492 }
3493 std::vector<RTCPReportBlock> report_blocks;
3494 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003495
3496 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003497 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003498 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003499 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003500 Channel* channel = associate_send_channel_.channel();
3501 // Tries to get RTT from an associated channel. This is important for
3502 // receive-only channels.
3503 if (channel) {
3504 // To prevent infinite recursion and deadlock, calling GetRTT of
3505 // associate channel should always use "false" for argument:
3506 // |allow_associate_channel|.
3507 rtt = channel->GetRTT(false);
3508 }
3509 }
3510 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003511 }
3512
3513 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3514 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3515 for (; it != report_blocks.end(); ++it) {
3516 if (it->remoteSSRC == remoteSSRC)
3517 break;
3518 }
3519 if (it == report_blocks.end()) {
3520 // We have not received packets with SSRC matching the report blocks.
3521 // To calculate RTT we try with the SSRC of the first report block.
3522 // This is very important for send-only channels where we don't know
3523 // the SSRC of the other end.
3524 remoteSSRC = report_blocks[0].remoteSSRC;
3525 }
Minyue2013aec2015-05-13 14:14:42 +02003526
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003527 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003528 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003529 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003530 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3531 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003532 return 0;
3533 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003534 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003535}
3536
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003537} // namespace voe
3538} // namespace webrtc