blob: 25f44c1c66fcbaccc7543121971f0c9cda6dc4e1 [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
43#if defined(_WIN32)
44#include <Qos.h>
45#endif
46
andrew@webrtc.org50419b02012-11-14 19:07:54 +000047namespace webrtc {
48namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000049
Stefan Holmerb86d4e42015-12-07 10:26:18 +010050class TransportFeedbackProxy : public TransportFeedbackObserver {
51 public:
52 TransportFeedbackProxy() : feedback_observer_(nullptr) {
53 pacer_thread_.DetachFromThread();
54 network_thread_.DetachFromThread();
55 }
56
57 void SetTransportFeedbackObserver(
58 TransportFeedbackObserver* feedback_observer) {
59 RTC_DCHECK(thread_checker_.CalledOnValidThread());
60 rtc::CritScope lock(&crit_);
61 feedback_observer_ = feedback_observer;
62 }
63
64 // Implements TransportFeedbackObserver.
65 void AddPacket(uint16_t sequence_number,
66 size_t length,
67 bool was_paced) override {
68 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
69 rtc::CritScope lock(&crit_);
70 if (feedback_observer_)
71 feedback_observer_->AddPacket(sequence_number, length, was_paced);
72 }
73 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
74 RTC_DCHECK(network_thread_.CalledOnValidThread());
75 rtc::CritScope lock(&crit_);
76 if (feedback_observer_)
77 feedback_observer_->OnTransportFeedback(feedback);
78 }
79
80 private:
81 rtc::CriticalSection crit_;
82 rtc::ThreadChecker thread_checker_;
83 rtc::ThreadChecker pacer_thread_;
84 rtc::ThreadChecker network_thread_;
85 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
86};
87
88class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
89 public:
90 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
91 pacer_thread_.DetachFromThread();
92 }
93
94 void SetSequenceNumberAllocator(
95 TransportSequenceNumberAllocator* seq_num_allocator) {
96 RTC_DCHECK(thread_checker_.CalledOnValidThread());
97 rtc::CritScope lock(&crit_);
98 seq_num_allocator_ = seq_num_allocator;
99 }
100
101 // Implements TransportSequenceNumberAllocator.
102 uint16_t AllocateSequenceNumber() override {
103 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
104 rtc::CritScope lock(&crit_);
105 if (!seq_num_allocator_)
106 return 0;
107 return seq_num_allocator_->AllocateSequenceNumber();
108 }
109
110 private:
111 rtc::CriticalSection crit_;
112 rtc::ThreadChecker thread_checker_;
113 rtc::ThreadChecker pacer_thread_;
114 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
115};
116
117class RtpPacketSenderProxy : public RtpPacketSender {
118 public:
119 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {
120 }
121
122 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
123 RTC_DCHECK(thread_checker_.CalledOnValidThread());
124 rtc::CritScope lock(&crit_);
125 rtp_packet_sender_ = rtp_packet_sender;
126 }
127
128 // Implements RtpPacketSender.
129 void InsertPacket(Priority priority,
130 uint32_t ssrc,
131 uint16_t sequence_number,
132 int64_t capture_time_ms,
133 size_t bytes,
134 bool retransmission) override {
135 rtc::CritScope lock(&crit_);
136 if (rtp_packet_sender_) {
137 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
138 capture_time_ms, bytes, retransmission);
139 }
140 }
141
142 private:
143 rtc::ThreadChecker thread_checker_;
144 rtc::CriticalSection crit_;
145 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
146};
147
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000148// Extend the default RTCP statistics struct with max_jitter, defined as the
149// maximum jitter value seen in an RTCP report block.
150struct ChannelStatistics : public RtcpStatistics {
151 ChannelStatistics() : rtcp(), max_jitter(0) {}
152
153 RtcpStatistics rtcp;
154 uint32_t max_jitter;
155};
156
157// Statistics callback, called at each generation of a new RTCP report block.
158class StatisticsProxy : public RtcpStatisticsCallback {
159 public:
tommi31fc21f2016-01-21 10:37:37 -0800160 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000161 virtual ~StatisticsProxy() {}
162
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000163 void StatisticsUpdated(const RtcpStatistics& statistics,
164 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000165 if (ssrc != ssrc_)
166 return;
167
tommi31fc21f2016-01-21 10:37:37 -0800168 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000169 stats_.rtcp = statistics;
170 if (statistics.jitter > stats_.max_jitter) {
171 stats_.max_jitter = statistics.jitter;
172 }
173 }
174
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000175 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000176
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000177 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800178 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000179 return stats_;
180 }
181
182 private:
183 // StatisticsUpdated calls are triggered from threads in the RTP module,
184 // while GetStats calls can be triggered from the public voice engine API,
185 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800186 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000187 const uint32_t ssrc_;
188 ChannelStatistics stats_;
189};
190
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000191class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000192 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000193 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
194 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000195
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000196 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
197 // Not used for Voice Engine.
198 }
199
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000200 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
201 int64_t rtt,
202 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000203 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
204 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
205 // report for VoiceEngine?
206 if (report_blocks.empty())
207 return;
208
209 int fraction_lost_aggregate = 0;
210 int total_number_of_packets = 0;
211
212 // If receiving multiple report blocks, calculate the weighted average based
213 // on the number of packets a report refers to.
214 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
215 block_it != report_blocks.end(); ++block_it) {
216 // Find the previous extended high sequence number for this remote SSRC,
217 // to calculate the number of RTP packets this report refers to. Ignore if
218 // we haven't seen this SSRC before.
219 std::map<uint32_t, uint32_t>::iterator seq_num_it =
220 extended_max_sequence_number_.find(block_it->sourceSSRC);
221 int number_of_packets = 0;
222 if (seq_num_it != extended_max_sequence_number_.end()) {
223 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
224 }
225 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
226 total_number_of_packets += number_of_packets;
227
228 extended_max_sequence_number_[block_it->sourceSSRC] =
229 block_it->extendedHighSeqNum;
230 }
231 int weighted_fraction_lost = 0;
232 if (total_number_of_packets > 0) {
233 weighted_fraction_lost = (fraction_lost_aggregate +
234 total_number_of_packets / 2) / total_number_of_packets;
235 }
236 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000237 }
238
239 private:
240 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000241 // Maps remote side ssrc to extended highest sequence number received.
242 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000243};
244
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000245int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000246Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000247 uint8_t payloadType,
248 uint32_t timeStamp,
249 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000250 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 const RTPFragmentationHeader* fragmentation)
252{
253 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
254 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000255 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
256 frameType, payloadType, timeStamp,
257 payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000258
259 if (_includeAudioLevelIndication)
260 {
261 // Store current audio level in the RTP/RTCP module.
262 // The level will be used in combination with voice-activity state
263 // (frameType) to add an RTP header extension
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000264 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +0000265 }
266
267 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
268 // packetization.
269 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000270 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +0000271 payloadType,
272 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000273 // Leaving the time when this frame was
274 // received from the capture device as
275 // undefined for voice for now.
276 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +0000277 payloadData,
278 payloadSize,
279 fragmentation) == -1)
280 {
281 _engineStatisticsPtr->SetLastError(
282 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
283 "Channel::SendData() failed to send data to RTP/RTCP module");
284 return -1;
285 }
286
287 _lastLocalTimeStamp = timeStamp;
288 _lastPayloadType = payloadType;
289
290 return 0;
291}
292
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000293int32_t
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000294Channel::InFrameType(FrameType frame_type)
niklase@google.com470e71d2011-07-07 08:21:25 +0000295{
296 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000297 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000298
tommi31fc21f2016-01-21 10:37:37 -0800299 rtc::CritScope cs(&_callbackCritSect);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000300 _sendFrameType = (frame_type == kAudioFrameSpeech);
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 return 0;
302}
303
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000304int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000305Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000306{
tommi31fc21f2016-01-21 10:37:37 -0800307 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000308 if (_rxVadObserverPtr)
309 {
310 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
311 }
312
313 return 0;
314}
315
stefan1d8a5062015-10-02 03:39:33 -0700316bool Channel::SendRtp(const uint8_t* data,
317 size_t len,
318 const PacketOptions& options) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000319 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Boströmac547a62015-09-17 23:03:57 +0200320 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000321
tommi31fc21f2016-01-21 10:37:37 -0800322 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000323
niklase@google.com470e71d2011-07-07 08:21:25 +0000324 if (_transportPtr == NULL)
325 {
326 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
327 "Channel::SendPacket() failed to send RTP packet due to"
328 " invalid transport object");
pbos2d566682015-09-28 09:59:31 -0700329 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000330 }
331
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000332 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000333 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000334
stefan1d8a5062015-10-02 03:39:33 -0700335 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000336 std::string transport_name =
337 _externalTransport ? "external transport" : "WebRtc sockets";
338 WEBRTC_TRACE(kTraceError, kTraceVoice,
339 VoEId(_instanceId,_channelId),
340 "Channel::SendPacket() RTP transmission using %s failed",
341 transport_name.c_str());
pbos2d566682015-09-28 09:59:31 -0700342 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000343 }
pbos2d566682015-09-28 09:59:31 -0700344 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000345}
346
pbos2d566682015-09-28 09:59:31 -0700347bool
348Channel::SendRtcp(const uint8_t *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000349{
niklase@google.com470e71d2011-07-07 08:21:25 +0000350 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pbos2d566682015-09-28 09:59:31 -0700351 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000352
tommi31fc21f2016-01-21 10:37:37 -0800353 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000354 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000355 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000356 WEBRTC_TRACE(kTraceError, kTraceVoice,
357 VoEId(_instanceId,_channelId),
pbos2d566682015-09-28 09:59:31 -0700358 "Channel::SendRtcp() failed to send RTCP packet"
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000359 " due to invalid transport object");
pbos2d566682015-09-28 09:59:31 -0700360 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000361 }
362
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000363 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000364 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000365
pbos2d566682015-09-28 09:59:31 -0700366 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000367 if (n < 0) {
368 std::string transport_name =
369 _externalTransport ? "external transport" : "WebRtc sockets";
370 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
371 VoEId(_instanceId,_channelId),
pbos2d566682015-09-28 09:59:31 -0700372 "Channel::SendRtcp() transmission using %s failed",
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000373 transport_name.c_str());
pbos2d566682015-09-28 09:59:31 -0700374 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000375 }
pbos2d566682015-09-28 09:59:31 -0700376 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000377}
378
Peter Boströmac547a62015-09-17 23:03:57 +0200379void Channel::OnPlayTelephoneEvent(uint8_t event,
380 uint16_t lengthMs,
381 uint8_t volume) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000382 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Boströmac547a62015-09-17 23:03:57 +0200383 "Channel::OnPlayTelephoneEvent(event=%u, lengthMs=%u,"
384 " volume=%u)", event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000385
386 if (!_playOutbandDtmfEvent || (event > 15))
387 {
388 // Ignore callback since feedback is disabled or event is not a
389 // Dtmf tone event.
390 return;
391 }
392
393 assert(_outputMixerPtr != NULL);
394
395 // Start playing out the Dtmf tone (if playout is enabled).
396 // Reduce length of tone with 80ms to the reduce risk of echo.
397 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
398}
399
400void
Peter Boströmac547a62015-09-17 23:03:57 +0200401Channel::OnIncomingSSRCChanged(uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000402{
403 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Boströmac547a62015-09-17 23:03:57 +0200404 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000406 // Update ssrc so that NTP for AV sync can be updated.
407 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000408}
409
Peter Boströmac547a62015-09-17 23:03:57 +0200410void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
411 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
412 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
413 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000414}
415
Peter Boströmac547a62015-09-17 23:03:57 +0200416int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000417 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000418 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000419 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800420 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200421 uint32_t rate) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000422 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Boströmac547a62015-09-17 23:03:57 +0200423 "Channel::OnInitializeDecoder(payloadType=%d, "
Peter Kasting69558702016-01-12 16:26:35 -0800424 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
Peter Boströmac547a62015-09-17 23:03:57 +0200425 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000427 CodecInst receiveCodec = {0};
428 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000429
430 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000431 receiveCodec.plfreq = frequency;
432 receiveCodec.channels = channels;
433 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000434 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000435
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000436 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000437 receiveCodec.pacsize = dummyCodec.pacsize;
438
439 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000440 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000441 {
442 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000443 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 "Channel::OnInitializeDecoder() invalid codec ("
445 "pt=%d, name=%s) received - 1", payloadType, payloadName);
446 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
447 return -1;
448 }
449
450 return 0;
451}
452
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000453int32_t
454Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000455 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000456 const WebRtcRTPHeader* rtpHeader)
457{
458 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000459 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS ","
Peter Kasting69558702016-01-12 16:26:35 -0800460 " payloadType=%u, audioChannel=%" PRIuS ")",
niklase@google.com470e71d2011-07-07 08:21:25 +0000461 payloadSize,
462 rtpHeader->header.payloadType,
463 rtpHeader->type.Audio.channel);
464
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000465 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000466 {
467 // Avoid inserting into NetEQ when we are not playing. Count the
468 // packet as discarded.
469 WEBRTC_TRACE(kTraceStream, kTraceVoice,
470 VoEId(_instanceId, _channelId),
471 "received packet is discarded since playing is not"
472 " activated");
473 _numberOfDiscardedPackets++;
474 return 0;
475 }
476
477 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000478 if (audio_coding_->IncomingPacket(payloadData,
479 payloadSize,
480 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000481 {
482 _engineStatisticsPtr->SetLastError(
483 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
484 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
485 return -1;
486 }
487
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000488 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000489 UpdatePacketDelay(rtpHeader->header.timestamp,
490 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000491
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000492 int64_t round_trip_time = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000493 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
494 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000495
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000496 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000497 round_trip_time);
498 if (!nack_list.empty()) {
499 // Can't use nack_list.data() since it's not supported by all
500 // compilers.
501 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000502 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000503 return 0;
504}
505
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000506bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000507 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000508 RTPHeader header;
509 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
510 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
511 "IncomingPacket invalid RTP header");
512 return false;
513 }
514 header.payload_type_frequency =
515 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
516 if (header.payload_type_frequency < 0)
517 return false;
518 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
519}
520
minyuel0f4b3732015-08-31 16:04:32 +0200521int32_t Channel::GetAudioFrame(int32_t id, AudioFrame* audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000522{
Ivo Creusenae856f22015-09-17 16:30:16 +0200523 if (event_log_) {
524 unsigned int ssrc;
525 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
526 event_log_->LogAudioPlayout(ssrc);
527 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000528 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
minyuel0f4b3732015-08-31 16:04:32 +0200529 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_,
530 audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000531 {
532 WEBRTC_TRACE(kTraceError, kTraceVoice,
533 VoEId(_instanceId,_channelId),
534 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000535 // In all likelihood, the audio in this frame is garbage. We return an
536 // error so that the audio mixer module doesn't add it to the mix. As
537 // a result, it won't be played out and the actions skipped here are
538 // irrelevant.
539 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000540 }
541
542 if (_RxVadDetection)
543 {
minyuel0f4b3732015-08-31 16:04:32 +0200544 UpdateRxVadDetection(*audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000545 }
546
547 // Convert module ID to internal VoE channel ID
minyuel0f4b3732015-08-31 16:04:32 +0200548 audioFrame->id_ = VoEChannelId(audioFrame->id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000549 // Store speech type for dead-or-alive detection
minyuel0f4b3732015-08-31 16:04:32 +0200550 _outputSpeechType = audioFrame->speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000551
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000552 ChannelState::State state = channel_state_.Get();
553
554 if (state.rx_apm_is_enabled) {
minyuel0f4b3732015-08-31 16:04:32 +0200555 int err = rx_audioproc_->ProcessStream(audioFrame);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000556 if (err) {
557 LOG(LS_ERROR) << "ProcessStream() error: " << err;
558 assert(false);
559 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000560 }
561
Tommif888bb52015-12-12 01:37:01 +0100562 {
563 // Pass the audio buffers to an optional sink callback, before applying
564 // scaling/panning, as that applies to the mix operation.
565 // External recipients of the audio (e.g. via AudioTrack), will do their
566 // own mixing/dynamic processing.
tommi31fc21f2016-01-21 10:37:37 -0800567 rtc::CritScope cs(&_callbackCritSect);
Tommif888bb52015-12-12 01:37:01 +0100568 if (audio_sink_) {
569 AudioSinkInterface::Data data(
570 &audioFrame->data_[0],
571 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
572 audioFrame->num_channels_, audioFrame->timestamp_);
573 audio_sink_->OnData(data);
574 }
575 }
576
wu@webrtc.org63420662013-10-17 18:28:55 +0000577 float output_gain = 1.0f;
578 float left_pan = 1.0f;
579 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000580 {
tommi31fc21f2016-01-21 10:37:37 -0800581 rtc::CritScope cs(&volume_settings_critsect_);
wu@webrtc.org63420662013-10-17 18:28:55 +0000582 output_gain = _outputGain;
583 left_pan = _panLeft;
584 right_pan= _panRight;
585 }
586
587 // Output volume scaling
588 if (output_gain < 0.99f || output_gain > 1.01f)
589 {
minyuel0f4b3732015-08-31 16:04:32 +0200590 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000591 }
592
593 // Scale left and/or right channel(s) if stereo and master balance is
594 // active
595
wu@webrtc.org63420662013-10-17 18:28:55 +0000596 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000597 {
minyuel0f4b3732015-08-31 16:04:32 +0200598 if (audioFrame->num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000599 {
600 // Emulate stereo mode since panning is active.
601 // The mono signal is copied to both left and right channels here.
minyuel0f4b3732015-08-31 16:04:32 +0200602 AudioFrameOperations::MonoToStereo(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000603 }
604 // For true stereo mode (when we are receiving a stereo signal), no
605 // action is needed.
606
607 // Do the panning operation (the audio frame contains stereo at this
608 // stage)
minyuel0f4b3732015-08-31 16:04:32 +0200609 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000610 }
611
612 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000613 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000614 {
minyuel0f4b3732015-08-31 16:04:32 +0200615 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000616 }
617
niklase@google.com470e71d2011-07-07 08:21:25 +0000618 // External media
619 if (_outputExternalMedia)
620 {
tommi31fc21f2016-01-21 10:37:37 -0800621 rtc::CritScope cs(&_callbackCritSect);
minyuel0f4b3732015-08-31 16:04:32 +0200622 const bool isStereo = (audioFrame->num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000623 if (_outputExternalMediaCallbackPtr)
624 {
Tommif888bb52015-12-12 01:37:01 +0100625 _outputExternalMediaCallbackPtr->Process(
626 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
627 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
628 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000629 }
630 }
631
632 // Record playout if enabled
633 {
tommi31fc21f2016-01-21 10:37:37 -0800634 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000635
636 if (_outputFileRecording && _outputFileRecorderPtr)
637 {
minyuel0f4b3732015-08-31 16:04:32 +0200638 _outputFileRecorderPtr->RecordAudioToFile(*audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000639 }
640 }
641
642 // Measure audio level (0-9)
minyuel0f4b3732015-08-31 16:04:32 +0200643 _outputAudioLevel.ComputeLevel(*audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000644
minyuel0f4b3732015-08-31 16:04:32 +0200645 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
wu@webrtc.org94454b72014-06-05 20:34:08 +0000646 // The first frame with a valid rtp timestamp.
minyuel0f4b3732015-08-31 16:04:32 +0200647 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000648 }
649
650 if (capture_start_rtp_time_stamp_ >= 0) {
651 // audioFrame.timestamp_ should be valid from now on.
652
653 // Compute elapsed time.
654 int64_t unwrap_timestamp =
minyuel0f4b3732015-08-31 16:04:32 +0200655 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
656 audioFrame->elapsed_time_ms_ =
wu@webrtc.org94454b72014-06-05 20:34:08 +0000657 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
658 (GetPlayoutFrequency() / 1000);
659
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000660 {
tommi31fc21f2016-01-21 10:37:37 -0800661 rtc::CritScope lock(&ts_stats_lock_);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000662 // Compute ntp time.
minyuel0f4b3732015-08-31 16:04:32 +0200663 audioFrame->ntp_time_ms_ = ntp_estimator_.Estimate(
664 audioFrame->timestamp_);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000665 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
minyuel0f4b3732015-08-31 16:04:32 +0200666 if (audioFrame->ntp_time_ms_ > 0) {
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000667 // Compute |capture_start_ntp_time_ms_| so that
668 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
669 capture_start_ntp_time_ms_ =
minyuel0f4b3732015-08-31 16:04:32 +0200670 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000671 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000672 }
673 }
674
niklase@google.com470e71d2011-07-07 08:21:25 +0000675 return 0;
676}
677
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000678int32_t
minyuel0f4b3732015-08-31 16:04:32 +0200679Channel::NeededFrequency(int32_t id) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000680{
681 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
682 "Channel::NeededFrequency(id=%d)", id);
683
684 int highestNeeded = 0;
685
686 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000687 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000688
689 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000690 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000691 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000692 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000693 }
694 else
695 {
696 highestNeeded = receiveFrequency;
697 }
698
699 // Special case, if we're playing a file on the playout side
700 // we take that frequency into consideration as well
701 // This is not needed on sending side, since the codec will
702 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000703 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000704 {
tommi31fc21f2016-01-21 10:37:37 -0800705 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000706 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000707 {
708 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
709 {
710 highestNeeded=_outputFilePlayerPtr->Frequency();
711 }
712 }
713 }
714
715 return(highestNeeded);
716}
717
ivocb04965c2015-09-09 00:09:43 -0700718int32_t Channel::CreateChannel(Channel*& channel,
719 int32_t channelId,
720 uint32_t instanceId,
721 RtcEventLog* const event_log,
722 const Config& config) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000723 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
724 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
725 channelId, instanceId);
726
ivocb04965c2015-09-09 00:09:43 -0700727 channel = new Channel(channelId, instanceId, event_log, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000728 if (channel == NULL)
729 {
730 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
731 VoEId(instanceId,channelId),
732 "Channel::CreateChannel() unable to allocate memory for"
733 " channel");
734 return -1;
735 }
736 return 0;
737}
738
739void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000740Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000741{
742 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
743 "Channel::PlayNotification(id=%d, durationMs=%d)",
744 id, durationMs);
745
746 // Not implement yet
747}
748
749void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000750Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000751{
752 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
753 "Channel::RecordNotification(id=%d, durationMs=%d)",
754 id, durationMs);
755
756 // Not implement yet
757}
758
759void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000760Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000761{
762 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
763 "Channel::PlayFileEnded(id=%d)", id);
764
765 if (id == _inputFilePlayerId)
766 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000767 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000768 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
769 VoEId(_instanceId,_channelId),
770 "Channel::PlayFileEnded() => input file player module is"
771 " shutdown");
772 }
773 else if (id == _outputFilePlayerId)
774 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000775 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
777 VoEId(_instanceId,_channelId),
778 "Channel::PlayFileEnded() => output file player module is"
779 " shutdown");
780 }
781}
782
783void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000784Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000785{
786 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
787 "Channel::RecordFileEnded(id=%d)", id);
788
789 assert(id == _outputFileRecorderId);
790
tommi31fc21f2016-01-21 10:37:37 -0800791 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000792
793 _outputFileRecording = false;
794 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
795 VoEId(_instanceId,_channelId),
796 "Channel::RecordFileEnded() => output file recorder module is"
797 " shutdown");
798}
799
pbos@webrtc.org92135212013-05-14 08:31:39 +0000800Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000801 uint32_t instanceId,
ivocb04965c2015-09-09 00:09:43 -0700802 RtcEventLog* const event_log,
803 const Config& config)
tommi31fc21f2016-01-21 10:37:37 -0800804 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100805 _channelId(channelId),
806 event_log_(event_log),
807 rtp_header_parser_(RtpHeaderParser::Create()),
808 rtp_payload_registry_(
809 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
810 rtp_receive_statistics_(
811 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
812 rtp_receiver_(
813 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
814 this,
815 this,
816 this,
817 rtp_payload_registry_.get())),
818 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
819 _outputAudioLevel(),
820 _externalTransport(false),
821 _inputFilePlayerPtr(NULL),
822 _outputFilePlayerPtr(NULL),
823 _outputFileRecorderPtr(NULL),
824 // Avoid conflict with other channels by adding 1024 - 1026,
825 // won't use as much as 1024 channels.
826 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
827 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
828 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
829 _outputFileRecording(false),
830 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
831 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
832 _outputExternalMedia(false),
833 _inputExternalMediaCallbackPtr(NULL),
834 _outputExternalMediaCallbackPtr(NULL),
835 _timeStamp(0), // This is just an offset, RTP module will add it's own
836 // random offset
837 _sendTelephoneEventPayloadType(106),
838 ntp_estimator_(Clock::GetRealTimeClock()),
839 jitter_buffer_playout_timestamp_(0),
840 playout_timestamp_rtp_(0),
841 playout_timestamp_rtcp_(0),
842 playout_delay_ms_(0),
843 _numberOfDiscardedPackets(0),
844 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100845 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
846 capture_start_rtp_time_stamp_(-1),
847 capture_start_ntp_time_ms_(-1),
848 _engineStatisticsPtr(NULL),
849 _outputMixerPtr(NULL),
850 _transmitMixerPtr(NULL),
851 _moduleProcessThreadPtr(NULL),
852 _audioDeviceModulePtr(NULL),
853 _voiceEngineObserverPtr(NULL),
854 _callbackCritSectPtr(NULL),
855 _transportPtr(NULL),
856 _rxVadObserverPtr(NULL),
857 _oldVadDecision(-1),
858 _sendFrameType(0),
859 _externalMixing(false),
860 _mixFileWithMicrophone(false),
861 _mute(false),
862 _panLeft(1.0f),
863 _panRight(1.0f),
864 _outputGain(1.0f),
865 _playOutbandDtmfEvent(false),
866 _playInbandDtmfEvent(false),
867 _lastLocalTimeStamp(0),
868 _lastPayloadType(0),
869 _includeAudioLevelIndication(false),
870 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 _average_jitter_buffer_delay_us(0),
872 _previousTimestamp(0),
873 _recPacketDelayMs(20),
874 _RxVadDetection(false),
875 _rxAgcIsEnabled(false),
876 _rxNsIsEnabled(false),
877 restored_packet_in_use_(false),
878 rtcp_observer_(new VoERtcpObserver(this)),
879 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100880 associate_send_channel_(ChannelOwner(nullptr)),
881 pacing_enabled_(config.Get<VoicePacing>().enabled),
882 feedback_observer_proxy_(pacing_enabled_ ? new TransportFeedbackProxy()
883 : nullptr),
884 seq_num_allocator_proxy_(
885 pacing_enabled_ ? new TransportSequenceNumberProxy() : nullptr),
886 rtp_packet_sender_proxy_(pacing_enabled_ ? new RtpPacketSenderProxy()
887 : nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000888 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
889 "Channel::Channel() - ctor");
Henrik Lundin64dad832015-05-11 12:44:23 +0200890 AudioCodingModule::Config acm_config;
891 acm_config.id = VoEModuleId(instanceId, channelId);
892 if (config.Get<NetEqCapacityConfig>().enabled) {
893 // Clamping the buffer capacity at 20 packets. While going lower will
894 // probably work, it makes little sense.
895 acm_config.neteq_config.max_packets_in_buffer =
896 std::max(20, config.Get<NetEqCapacityConfig>().capacity);
897 }
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200898 acm_config.neteq_config.enable_fast_accelerate =
899 config.Get<NetEqFastAccelerate>().enabled;
Henrik Lundin64dad832015-05-11 12:44:23 +0200900 audio_coding_.reset(AudioCodingModule::Create(acm_config));
901
niklase@google.com470e71d2011-07-07 08:21:25 +0000902 _inbandDtmfQueue.ResetDtmf();
903 _inbandDtmfGenerator.Init();
904 _outputAudioLevel.Clear();
905
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000906 RtpRtcp::Configuration configuration;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000907 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000908 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000909 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000910 configuration.receive_statistics = rtp_receive_statistics_.get();
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000911 configuration.bandwidth_callback = rtcp_observer_.get();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100912 configuration.paced_sender = rtp_packet_sender_proxy_.get();
913 configuration.transport_sequence_number_allocator =
914 seq_num_allocator_proxy_.get();
915 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
terelius429c3452016-01-21 05:42:04 -0800916 configuration.event_log = event_log;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000917
918 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000919
920 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
921 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
922 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000923
924 Config audioproc_config;
925 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
926 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000927}
928
929Channel::~Channel()
930{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000931 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000932 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
933 "Channel::~Channel() - dtor");
934
935 if (_outputExternalMedia)
936 {
937 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
938 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000939 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000940 {
941 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
942 }
943 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000944 StopPlayout();
945
946 {
tommi31fc21f2016-01-21 10:37:37 -0800947 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000948 if (_inputFilePlayerPtr)
949 {
950 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
951 _inputFilePlayerPtr->StopPlayingFile();
952 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
953 _inputFilePlayerPtr = NULL;
954 }
955 if (_outputFilePlayerPtr)
956 {
957 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
958 _outputFilePlayerPtr->StopPlayingFile();
959 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
960 _outputFilePlayerPtr = NULL;
961 }
962 if (_outputFileRecorderPtr)
963 {
964 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
965 _outputFileRecorderPtr->StopRecording();
966 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
967 _outputFileRecorderPtr = NULL;
968 }
969 }
970
971 // The order to safely shutdown modules in a channel is:
972 // 1. De-register callbacks in modules
973 // 2. De-register modules in process thread
974 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000975 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000976 {
977 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
978 VoEId(_instanceId,_channelId),
979 "~Channel() failed to de-register transport callback"
980 " (Audio coding module)");
981 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000982 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000983 {
984 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
985 VoEId(_instanceId,_channelId),
986 "~Channel() failed to de-register VAD callback"
987 " (Audio coding module)");
988 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000989 // De-register modules in process thread
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000990 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
991
niklase@google.com470e71d2011-07-07 08:21:25 +0000992 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000993}
994
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000995int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000996Channel::Init()
997{
998 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
999 "Channel::Init()");
1000
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001001 channel_state_.Reset();
1002
niklase@google.com470e71d2011-07-07 08:21:25 +00001003 // --- Initial sanity
1004
1005 if ((_engineStatisticsPtr == NULL) ||
1006 (_moduleProcessThreadPtr == NULL))
1007 {
1008 WEBRTC_TRACE(kTraceError, kTraceVoice,
1009 VoEId(_instanceId,_channelId),
1010 "Channel::Init() must call SetEngineInformation() first");
1011 return -1;
1012 }
1013
1014 // --- Add modules to process thread (for periodic schedulation)
1015
tommi@webrtc.org3985f012015-02-27 13:36:34 +00001016 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1017
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +00001018 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +00001019
henrik.lundin061b79a2015-09-18 01:29:11 -07001020 if (audio_coding_->InitializeReceiver() == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001021 _engineStatisticsPtr->SetLastError(
1022 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1023 "Channel::Init() unable to initialize the ACM - 1");
1024 return -1;
1025 }
1026
1027 // --- RTP/RTCP module initialization
1028
1029 // Ensure that RTCP is enabled by default for the created channel.
1030 // Note that, the module will keep generating RTCP until it is explicitly
1031 // disabled by the user.
1032 // After StopListen (when no sockets exists), RTCP packets will no longer
1033 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001034 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
1035 // RTCP is enabled by default.
pbosda903ea2015-10-02 02:36:56 -07001036 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001037 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +00001038 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001039 (audio_coding_->RegisterTransportCallback(this) == -1) ||
1040 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001041
1042 if (fail)
1043 {
1044 _engineStatisticsPtr->SetLastError(
1045 VE_CANNOT_INIT_CHANNEL, kTraceError,
1046 "Channel::Init() callbacks not registered");
1047 return -1;
1048 }
1049
1050 // --- Register all supported codecs to the receiving side of the
1051 // RTP/RTCP module
1052
1053 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001054 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00001055
1056 for (int idx = 0; idx < nSupportedCodecs; idx++)
1057 {
1058 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001059 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001060 (rtp_receiver_->RegisterReceivePayload(
1061 codec.plname,
1062 codec.pltype,
1063 codec.plfreq,
1064 codec.channels,
1065 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 {
1067 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1068 VoEId(_instanceId,_channelId),
Peter Kasting69558702016-01-12 16:26:35 -08001069 "Channel::Init() unable to register %s "
1070 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
niklase@google.com470e71d2011-07-07 08:21:25 +00001071 codec.plname, codec.pltype, codec.plfreq,
1072 codec.channels, codec.rate);
1073 }
1074 else
1075 {
1076 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1077 VoEId(_instanceId,_channelId),
Peter Kasting69558702016-01-12 16:26:35 -08001078 "Channel::Init() %s (%d/%d/%" PRIuS "/%d) has been "
1079 "added to the RTP/RTCP receiver",
niklase@google.com470e71d2011-07-07 08:21:25 +00001080 codec.plname, codec.pltype, codec.plfreq,
1081 codec.channels, codec.rate);
1082 }
1083
1084 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001085 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001086 {
1087 SetSendCodec(codec);
1088 }
1089
1090 // Register default PT for outband 'telephone-event'
1091 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1092 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001093 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001094 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001095 {
1096 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1097 VoEId(_instanceId,_channelId),
1098 "Channel::Init() failed to register outband "
1099 "'telephone-event' (%d/%d) correctly",
1100 codec.pltype, codec.plfreq);
1101 }
1102 }
1103
1104 if (!STR_CASE_CMP(codec.plname, "CN"))
1105 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001106 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1107 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001108 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001109 {
1110 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1111 VoEId(_instanceId,_channelId),
1112 "Channel::Init() failed to register CN (%d/%d) "
1113 "correctly - 1",
1114 codec.pltype, codec.plfreq);
1115 }
1116 }
1117#ifdef WEBRTC_CODEC_RED
1118 // Register RED to the receiving side of the ACM.
1119 // We will not receive an OnInitializeDecoder() callback for RED.
1120 if (!STR_CASE_CMP(codec.plname, "RED"))
1121 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001122 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001123 {
1124 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1125 VoEId(_instanceId,_channelId),
1126 "Channel::Init() failed to register RED (%d/%d) "
1127 "correctly",
1128 codec.pltype, codec.plfreq);
1129 }
1130 }
1131#endif
1132 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001133
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001134 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
pbosad856222015-11-27 09:48:36 -08001135 LOG(LS_ERROR) << "noise_suppression()->set_level(kDefaultNsMode) failed.";
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001136 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001137 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001138 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
pbosad856222015-11-27 09:48:36 -08001139 LOG(LS_ERROR) << "gain_control()->set_mode(kDefaultRxAgcMode) failed.";
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001140 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001141 }
1142
1143 return 0;
1144}
1145
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001146int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001147Channel::SetEngineInformation(Statistics& engineStatistics,
1148 OutputMixer& outputMixer,
1149 voe::TransmitMixer& transmitMixer,
1150 ProcessThread& moduleProcessThread,
1151 AudioDeviceModule& audioDeviceModule,
1152 VoiceEngineObserver* voiceEngineObserver,
tommi31fc21f2016-01-21 10:37:37 -08001153 rtc::CriticalSection* callbackCritSect)
niklase@google.com470e71d2011-07-07 08:21:25 +00001154{
1155 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1156 "Channel::SetEngineInformation()");
1157 _engineStatisticsPtr = &engineStatistics;
1158 _outputMixerPtr = &outputMixer;
1159 _transmitMixerPtr = &transmitMixer,
1160 _moduleProcessThreadPtr = &moduleProcessThread;
1161 _audioDeviceModulePtr = &audioDeviceModule;
1162 _voiceEngineObserverPtr = voiceEngineObserver;
1163 _callbackCritSectPtr = callbackCritSect;
1164 return 0;
1165}
1166
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001167int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001168Channel::UpdateLocalTimeStamp()
1169{
1170
Peter Kastingb7e50542015-06-11 12:55:50 -07001171 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001172 return 0;
1173}
1174
deadbeef2d110be2016-01-13 12:00:26 -08001175void Channel::SetSink(rtc::scoped_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001176 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001177 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001178}
1179
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001180int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001181Channel::StartPlayout()
1182{
1183 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1184 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001185 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001186 {
1187 return 0;
1188 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001189
1190 if (!_externalMixing) {
1191 // Add participant as candidates for mixing.
1192 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1193 {
1194 _engineStatisticsPtr->SetLastError(
1195 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1196 "StartPlayout() failed to add participant to mixer");
1197 return -1;
1198 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001199 }
1200
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001201 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001202 if (RegisterFilePlayingToMixer() != 0)
1203 return -1;
1204
niklase@google.com470e71d2011-07-07 08:21:25 +00001205 return 0;
1206}
1207
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001208int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001209Channel::StopPlayout()
1210{
1211 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1212 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001213 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001214 {
1215 return 0;
1216 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001217
1218 if (!_externalMixing) {
1219 // Remove participant as candidates for mixing
1220 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1221 {
1222 _engineStatisticsPtr->SetLastError(
1223 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1224 "StopPlayout() failed to remove participant from mixer");
1225 return -1;
1226 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001227 }
1228
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001229 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001230 _outputAudioLevel.Clear();
1231
1232 return 0;
1233}
1234
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001235int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001236Channel::StartSend()
1237{
1238 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1239 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001240 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001241 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001242 if (send_sequence_number_)
1243 SetInitSequenceNumber(send_sequence_number_);
1244
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001245 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001246 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001247 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001248 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001249 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001250
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001251 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001252 {
1253 _engineStatisticsPtr->SetLastError(
1254 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1255 "StartSend() RTP/RTCP failed to start sending");
tommi31fc21f2016-01-21 10:37:37 -08001256 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001257 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001258 return -1;
1259 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001260
niklase@google.com470e71d2011-07-07 08:21:25 +00001261 return 0;
1262}
1263
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001264int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001265Channel::StopSend()
1266{
1267 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1268 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001269 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001270 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001271 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001272 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001273 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001274
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001275 // Store the sequence number to be able to pick up the same sequence for
1276 // the next StartSend(). This is needed for restarting device, otherwise
1277 // it might cause libSRTP to complain about packets being replayed.
1278 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1279 // CL is landed. See issue
1280 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1281 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1282
niklase@google.com470e71d2011-07-07 08:21:25 +00001283 // Reset sending SSRC and sequence number and triggers direct transmission
1284 // of RTCP BYE
pbosd4362982015-07-07 08:32:48 -07001285 if (_rtpRtcpModule->SetSendingStatus(false) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001286 {
1287 _engineStatisticsPtr->SetLastError(
1288 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1289 "StartSend() RTP/RTCP failed to stop sending");
1290 }
1291
niklase@google.com470e71d2011-07-07 08:21:25 +00001292 return 0;
1293}
1294
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001295int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001296Channel::StartReceiving()
1297{
1298 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1299 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001300 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001301 {
1302 return 0;
1303 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001304 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001305 _numberOfDiscardedPackets = 0;
1306 return 0;
1307}
1308
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001309int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001310Channel::StopReceiving()
1311{
1312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1313 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001314 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001315 {
1316 return 0;
1317 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001318
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001319 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001320 return 0;
1321}
1322
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001323int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001324Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1325{
1326 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1327 "Channel::RegisterVoiceEngineObserver()");
tommi31fc21f2016-01-21 10:37:37 -08001328 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001329
1330 if (_voiceEngineObserverPtr)
1331 {
1332 _engineStatisticsPtr->SetLastError(
1333 VE_INVALID_OPERATION, kTraceError,
1334 "RegisterVoiceEngineObserver() observer already enabled");
1335 return -1;
1336 }
1337 _voiceEngineObserverPtr = &observer;
1338 return 0;
1339}
1340
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001341int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001342Channel::DeRegisterVoiceEngineObserver()
1343{
1344 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1345 "Channel::DeRegisterVoiceEngineObserver()");
tommi31fc21f2016-01-21 10:37:37 -08001346 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001347
1348 if (!_voiceEngineObserverPtr)
1349 {
1350 _engineStatisticsPtr->SetLastError(
1351 VE_INVALID_OPERATION, kTraceWarning,
1352 "DeRegisterVoiceEngineObserver() observer already disabled");
1353 return 0;
1354 }
1355 _voiceEngineObserverPtr = NULL;
1356 return 0;
1357}
1358
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001359int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001360Channel::GetSendCodec(CodecInst& codec)
1361{
kwiberg1fd4a4a2015-11-03 11:20:50 -08001362 auto send_codec = audio_coding_->SendCodec();
1363 if (send_codec) {
1364 codec = *send_codec;
1365 return 0;
1366 }
1367 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001368}
1369
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001370int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001371Channel::GetRecCodec(CodecInst& codec)
1372{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001373 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001374}
1375
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001376int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001377Channel::SetSendCodec(const CodecInst& codec)
1378{
1379 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1380 "Channel::SetSendCodec()");
1381
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001382 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001383 {
1384 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1385 "SetSendCodec() failed to register codec to ACM");
1386 return -1;
1387 }
1388
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001389 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001390 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001391 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1392 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001393 {
1394 WEBRTC_TRACE(
1395 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1396 "SetSendCodec() failed to register codec to"
1397 " RTP/RTCP module");
1398 return -1;
1399 }
1400 }
1401
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001402 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001403 {
1404 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1405 "SetSendCodec() failed to set audio packet size");
1406 return -1;
1407 }
1408
1409 return 0;
1410}
1411
Ivo Creusenadf89b72015-04-29 16:03:33 +02001412void Channel::SetBitRate(int bitrate_bps) {
1413 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1414 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1415 audio_coding_->SetBitRate(bitrate_bps);
1416}
1417
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001418void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001419 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001420 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1421
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001422 // Normalizes rate to 0 - 100.
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001423 if (audio_coding_->SetPacketLossRate(
1424 100 * average_fraction_loss / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001425 assert(false); // This should not happen.
1426 }
1427}
1428
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001429int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001430Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1431{
1432 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1433 "Channel::SetVADStatus(mode=%d)", mode);
henrik.lundin@webrtc.org664ccb72015-01-28 14:49:05 +00001434 assert(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
niklase@google.com470e71d2011-07-07 08:21:25 +00001435 // To disable VAD, DTX must be disabled too
1436 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001437 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001438 {
1439 _engineStatisticsPtr->SetLastError(
1440 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1441 "SetVADStatus() failed to set VAD");
1442 return -1;
1443 }
1444 return 0;
1445}
1446
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001447int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001448Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1449{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001450 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001451 {
1452 _engineStatisticsPtr->SetLastError(
1453 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1454 "GetVADStatus() failed to get VAD status");
1455 return -1;
1456 }
1457 disabledDTX = !disabledDTX;
1458 return 0;
1459}
1460
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001461int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001462Channel::SetRecPayloadType(const CodecInst& codec)
1463{
1464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1465 "Channel::SetRecPayloadType()");
1466
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001467 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001468 {
1469 _engineStatisticsPtr->SetLastError(
1470 VE_ALREADY_PLAYING, kTraceError,
1471 "SetRecPayloadType() unable to set PT while playing");
1472 return -1;
1473 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001474 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001475 {
1476 _engineStatisticsPtr->SetLastError(
1477 VE_ALREADY_LISTENING, kTraceError,
1478 "SetRecPayloadType() unable to set PT while listening");
1479 return -1;
1480 }
1481
1482 if (codec.pltype == -1)
1483 {
1484 // De-register the selected codec (RTP/RTCP module and ACM)
1485
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001486 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001487 CodecInst rxCodec = codec;
1488
1489 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001490 rtp_payload_registry_->ReceivePayloadType(
1491 rxCodec.plname,
1492 rxCodec.plfreq,
1493 rxCodec.channels,
1494 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1495 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001496 rxCodec.pltype = pltype;
1497
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001498 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001499 {
1500 _engineStatisticsPtr->SetLastError(
1501 VE_RTP_RTCP_MODULE_ERROR,
1502 kTraceError,
1503 "SetRecPayloadType() RTP/RTCP-module deregistration "
1504 "failed");
1505 return -1;
1506 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001507 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001508 {
1509 _engineStatisticsPtr->SetLastError(
1510 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1511 "SetRecPayloadType() ACM deregistration failed - 1");
1512 return -1;
1513 }
1514 return 0;
1515 }
1516
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001517 if (rtp_receiver_->RegisterReceivePayload(
1518 codec.plname,
1519 codec.pltype,
1520 codec.plfreq,
1521 codec.channels,
1522 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001523 {
1524 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001525 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1526 if (rtp_receiver_->RegisterReceivePayload(
1527 codec.plname,
1528 codec.pltype,
1529 codec.plfreq,
1530 codec.channels,
1531 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001532 {
1533 _engineStatisticsPtr->SetLastError(
1534 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1535 "SetRecPayloadType() RTP/RTCP-module registration failed");
1536 return -1;
1537 }
1538 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001539 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001540 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001541 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1542 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001543 {
1544 _engineStatisticsPtr->SetLastError(
1545 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1546 "SetRecPayloadType() ACM registration failed - 1");
1547 return -1;
1548 }
1549 }
1550 return 0;
1551}
1552
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001553int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001554Channel::GetRecPayloadType(CodecInst& codec)
1555{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001556 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001557 if (rtp_payload_registry_->ReceivePayloadType(
1558 codec.plname,
1559 codec.plfreq,
1560 codec.channels,
1561 (codec.rate < 0) ? 0 : codec.rate,
1562 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001563 {
1564 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001565 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001566 "GetRecPayloadType() failed to retrieve RX payload type");
1567 return -1;
1568 }
1569 codec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00001570 return 0;
1571}
1572
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001573int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001574Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1575{
1576 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1577 "Channel::SetSendCNPayloadType()");
1578
1579 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001580 int32_t samplingFreqHz(-1);
Peter Kasting69558702016-01-12 16:26:35 -08001581 const size_t kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001582 if (frequency == kFreq32000Hz)
1583 samplingFreqHz = 32000;
1584 else if (frequency == kFreq16000Hz)
1585 samplingFreqHz = 16000;
1586
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001587 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001588 {
1589 _engineStatisticsPtr->SetLastError(
1590 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1591 "SetSendCNPayloadType() failed to retrieve default CN codec "
1592 "settings");
1593 return -1;
1594 }
1595
1596 // Modify the payload type (must be set to dynamic range)
1597 codec.pltype = type;
1598
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001599 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001600 {
1601 _engineStatisticsPtr->SetLastError(
1602 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1603 "SetSendCNPayloadType() failed to register CN to ACM");
1604 return -1;
1605 }
1606
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001607 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001608 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001609 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1610 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001611 {
1612 _engineStatisticsPtr->SetLastError(
1613 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1614 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1615 "module");
1616 return -1;
1617 }
1618 }
1619 return 0;
1620}
1621
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001622int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001623 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001624 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001625
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001626 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001627 _engineStatisticsPtr->SetLastError(
1628 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001629 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001630 return -1;
1631 }
1632 return 0;
1633}
1634
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001635int Channel::SetOpusDtx(bool enable_dtx) {
1636 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1637 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001638 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001639 : audio_coding_->DisableOpusDtx();
1640 if (ret != 0) {
1641 _engineStatisticsPtr->SetLastError(
1642 VE_AUDIO_CODING_MODULE_ERROR, kTraceError, "SetOpusDtx() failed");
1643 return -1;
1644 }
1645 return 0;
1646}
1647
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001648int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001649{
1650 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1651 "Channel::RegisterExternalTransport()");
1652
tommi31fc21f2016-01-21 10:37:37 -08001653 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001654
niklase@google.com470e71d2011-07-07 08:21:25 +00001655 if (_externalTransport)
1656 {
1657 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1658 kTraceError,
1659 "RegisterExternalTransport() external transport already enabled");
1660 return -1;
1661 }
1662 _externalTransport = true;
1663 _transportPtr = &transport;
1664 return 0;
1665}
1666
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001667int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001668Channel::DeRegisterExternalTransport()
1669{
1670 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1671 "Channel::DeRegisterExternalTransport()");
1672
tommi31fc21f2016-01-21 10:37:37 -08001673 rtc::CritScope cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001674
niklase@google.com470e71d2011-07-07 08:21:25 +00001675 if (!_transportPtr)
1676 {
1677 _engineStatisticsPtr->SetLastError(
1678 VE_INVALID_OPERATION, kTraceWarning,
1679 "DeRegisterExternalTransport() external transport already "
1680 "disabled");
1681 return 0;
1682 }
1683 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001684 _transportPtr = NULL;
1685 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1686 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001687 return 0;
1688}
1689
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001690int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001691 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001692 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1693 "Channel::ReceivedRTPPacket()");
1694
1695 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001696 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001697
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001698 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001699 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001700 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1701 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1702 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001703 return -1;
1704 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001705 header.payload_type_frequency =
1706 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001707 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001708 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001709 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001710 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001711 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001712 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001713
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001714 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001715}
1716
1717bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001718 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001719 const RTPHeader& header,
1720 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001721 if (rtp_payload_registry_->IsRtx(header)) {
1722 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001723 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001724 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001725 assert(packet_length >= header.headerLength);
1726 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001727 PayloadUnion payload_specific;
1728 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001729 &payload_specific)) {
1730 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001731 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001732 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1733 payload_specific, in_order);
1734}
1735
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001736bool Channel::HandleRtxPacket(const uint8_t* packet,
1737 size_t packet_length,
1738 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001739 if (!rtp_payload_registry_->IsRtx(header))
1740 return false;
1741
1742 // Remove the RTX header and parse the original RTP header.
1743 if (packet_length < header.headerLength)
1744 return false;
1745 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1746 return false;
1747 if (restored_packet_in_use_) {
1748 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1749 "Multiple RTX headers detected, dropping packet");
1750 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001751 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001752 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001753 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1754 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001755 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1756 "Incoming RTX packet: invalid RTP header");
1757 return false;
1758 }
1759 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001760 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001761 restored_packet_in_use_ = false;
1762 return ret;
1763}
1764
1765bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1766 StreamStatistician* statistician =
1767 rtp_receive_statistics_->GetStatistician(header.ssrc);
1768 if (!statistician)
1769 return false;
1770 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001771}
1772
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001773bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1774 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001775 // Retransmissions are handled separately if RTX is enabled.
1776 if (rtp_payload_registry_->RtxEnabled())
1777 return false;
1778 StreamStatistician* statistician =
1779 rtp_receive_statistics_->GetStatistician(header.ssrc);
1780 if (!statistician)
1781 return false;
1782 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001783 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001784 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001785 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001786 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001787}
1788
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001789int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001790 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1791 "Channel::ReceivedRTCPPacket()");
1792 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001793 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001794
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001795 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001796 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001797 _engineStatisticsPtr->SetLastError(
1798 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1799 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1800 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001801
Minyue2013aec2015-05-13 14:14:42 +02001802 int64_t rtt = GetRTT(true);
1803 if (rtt == 0) {
1804 // Waiting for valid RTT.
1805 return 0;
1806 }
1807 uint32_t ntp_secs = 0;
1808 uint32_t ntp_frac = 0;
1809 uint32_t rtp_timestamp = 0;
1810 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1811 &rtp_timestamp)) {
1812 // Waiting for RTCP.
1813 return 0;
1814 }
1815
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001816 {
tommi31fc21f2016-01-21 10:37:37 -08001817 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001818 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001819 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001820 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001821}
1822
niklase@google.com470e71d2011-07-07 08:21:25 +00001823int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001824 bool loop,
1825 FileFormats format,
1826 int startPosition,
1827 float volumeScaling,
1828 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001829 const CodecInst* codecInst)
1830{
1831 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1832 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1833 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1834 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1835 startPosition, stopPosition);
1836
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001837 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001838 {
1839 _engineStatisticsPtr->SetLastError(
1840 VE_ALREADY_PLAYING, kTraceError,
1841 "StartPlayingFileLocally() is already playing");
1842 return -1;
1843 }
1844
niklase@google.com470e71d2011-07-07 08:21:25 +00001845 {
tommi31fc21f2016-01-21 10:37:37 -08001846 rtc::CritScope cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001847
1848 if (_outputFilePlayerPtr)
1849 {
1850 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1851 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1852 _outputFilePlayerPtr = NULL;
1853 }
1854
1855 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1856 _outputFilePlayerId, (const FileFormats)format);
1857
1858 if (_outputFilePlayerPtr == NULL)
1859 {
1860 _engineStatisticsPtr->SetLastError(
1861 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001862 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001863 return -1;
1864 }
1865
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001866 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001867
1868 if (_outputFilePlayerPtr->StartPlayingFile(
1869 fileName,
1870 loop,
1871 startPosition,
1872 volumeScaling,
1873 notificationTime,
1874 stopPosition,
1875 (const CodecInst*)codecInst) != 0)
1876 {
1877 _engineStatisticsPtr->SetLastError(
1878 VE_BAD_FILE, kTraceError,
1879 "StartPlayingFile() failed to start file playout");
1880 _outputFilePlayerPtr->StopPlayingFile();
1881 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1882 _outputFilePlayerPtr = NULL;
1883 return -1;
1884 }
1885 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001886 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001887 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001888
1889 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001890 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001891
1892 return 0;
1893}
1894
1895int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001896 FileFormats format,
1897 int startPosition,
1898 float volumeScaling,
1899 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001900 const CodecInst* codecInst)
1901{
1902 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1903 "Channel::StartPlayingFileLocally(format=%d,"
1904 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1905 format, volumeScaling, startPosition, stopPosition);
1906
1907 if(stream == NULL)
1908 {
1909 _engineStatisticsPtr->SetLastError(
1910 VE_BAD_FILE, kTraceError,
1911 "StartPlayingFileLocally() NULL as input stream");
1912 return -1;
1913 }
1914
1915
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001916 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001917 {
1918 _engineStatisticsPtr->SetLastError(
1919 VE_ALREADY_PLAYING, kTraceError,
1920 "StartPlayingFileLocally() is already playing");
1921 return -1;
1922 }
1923
niklase@google.com470e71d2011-07-07 08:21:25 +00001924 {
tommi31fc21f2016-01-21 10:37:37 -08001925 rtc::CritScope cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001926
1927 // Destroy the old instance
1928 if (_outputFilePlayerPtr)
1929 {
1930 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1931 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1932 _outputFilePlayerPtr = NULL;
1933 }
1934
1935 // Create the instance
1936 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1937 _outputFilePlayerId,
1938 (const FileFormats)format);
1939
1940 if (_outputFilePlayerPtr == NULL)
1941 {
1942 _engineStatisticsPtr->SetLastError(
1943 VE_INVALID_ARGUMENT, kTraceError,
1944 "StartPlayingFileLocally() filePlayer format isnot correct");
1945 return -1;
1946 }
1947
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001948 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001949
1950 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1951 volumeScaling,
1952 notificationTime,
1953 stopPosition, codecInst) != 0)
1954 {
1955 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1956 "StartPlayingFile() failed to "
1957 "start file playout");
1958 _outputFilePlayerPtr->StopPlayingFile();
1959 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1960 _outputFilePlayerPtr = NULL;
1961 return -1;
1962 }
1963 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001964 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001965 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001966
1967 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001968 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001969
niklase@google.com470e71d2011-07-07 08:21:25 +00001970 return 0;
1971}
1972
1973int Channel::StopPlayingFileLocally()
1974{
1975 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1976 "Channel::StopPlayingFileLocally()");
1977
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001978 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001979 {
niklase@google.com470e71d2011-07-07 08:21:25 +00001980 return 0;
1981 }
1982
niklase@google.com470e71d2011-07-07 08:21:25 +00001983 {
tommi31fc21f2016-01-21 10:37:37 -08001984 rtc::CritScope cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001985
1986 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1987 {
1988 _engineStatisticsPtr->SetLastError(
1989 VE_STOP_RECORDING_FAILED, kTraceError,
1990 "StopPlayingFile() could not stop playing");
1991 return -1;
1992 }
1993 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1994 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1995 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001996 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001997 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001998 // _fileCritSect cannot be taken while calling
1999 // SetAnonymousMixibilityStatus. Refer to comments in
2000 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002001 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
2002 {
2003 _engineStatisticsPtr->SetLastError(
2004 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002005 "StopPlayingFile() failed to stop participant from playing as"
2006 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002007 return -1;
2008 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002009
2010 return 0;
2011}
2012
2013int Channel::IsPlayingFileLocally() const
2014{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002015 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002016}
2017
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002018int Channel::RegisterFilePlayingToMixer()
2019{
2020 // Return success for not registering for file playing to mixer if:
2021 // 1. playing file before playout is started on that channel.
2022 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002023 if (!channel_state_.Get().playing ||
2024 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002025 {
2026 return 0;
2027 }
2028
2029 // |_fileCritSect| cannot be taken while calling
2030 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2031 // frames can be pulled by the mixer. Since the frames are generated from
2032 // the file, _fileCritSect will be taken. This would result in a deadlock.
2033 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
2034 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002035 channel_state_.SetOutputFilePlaying(false);
tommi31fc21f2016-01-21 10:37:37 -08002036 rtc::CritScope cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002037 _engineStatisticsPtr->SetLastError(
2038 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2039 "StartPlayingFile() failed to add participant as file to mixer");
2040 _outputFilePlayerPtr->StopPlayingFile();
2041 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2042 _outputFilePlayerPtr = NULL;
2043 return -1;
2044 }
2045
2046 return 0;
2047}
2048
niklase@google.com470e71d2011-07-07 08:21:25 +00002049int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002050 bool loop,
2051 FileFormats format,
2052 int startPosition,
2053 float volumeScaling,
2054 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002055 const CodecInst* codecInst)
2056{
2057 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2058 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2059 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2060 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2061 startPosition, stopPosition);
2062
tommi31fc21f2016-01-21 10:37:37 -08002063 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002064
2065 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002066 {
2067 _engineStatisticsPtr->SetLastError(
2068 VE_ALREADY_PLAYING, kTraceWarning,
2069 "StartPlayingFileAsMicrophone() filePlayer is playing");
2070 return 0;
2071 }
2072
niklase@google.com470e71d2011-07-07 08:21:25 +00002073 // Destroy the old instance
2074 if (_inputFilePlayerPtr)
2075 {
2076 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2077 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2078 _inputFilePlayerPtr = NULL;
2079 }
2080
2081 // Create the instance
2082 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2083 _inputFilePlayerId, (const FileFormats)format);
2084
2085 if (_inputFilePlayerPtr == NULL)
2086 {
2087 _engineStatisticsPtr->SetLastError(
2088 VE_INVALID_ARGUMENT, kTraceError,
2089 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2090 return -1;
2091 }
2092
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002093 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002094
2095 if (_inputFilePlayerPtr->StartPlayingFile(
2096 fileName,
2097 loop,
2098 startPosition,
2099 volumeScaling,
2100 notificationTime,
2101 stopPosition,
2102 (const CodecInst*)codecInst) != 0)
2103 {
2104 _engineStatisticsPtr->SetLastError(
2105 VE_BAD_FILE, kTraceError,
2106 "StartPlayingFile() failed to start file playout");
2107 _inputFilePlayerPtr->StopPlayingFile();
2108 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2109 _inputFilePlayerPtr = NULL;
2110 return -1;
2111 }
2112 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002113 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002114
2115 return 0;
2116}
2117
2118int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002119 FileFormats format,
2120 int startPosition,
2121 float volumeScaling,
2122 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002123 const CodecInst* codecInst)
2124{
2125 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2126 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2127 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2128 format, volumeScaling, startPosition, stopPosition);
2129
2130 if(stream == NULL)
2131 {
2132 _engineStatisticsPtr->SetLastError(
2133 VE_BAD_FILE, kTraceError,
2134 "StartPlayingFileAsMicrophone NULL as input stream");
2135 return -1;
2136 }
2137
tommi31fc21f2016-01-21 10:37:37 -08002138 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002139
2140 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002141 {
2142 _engineStatisticsPtr->SetLastError(
2143 VE_ALREADY_PLAYING, kTraceWarning,
2144 "StartPlayingFileAsMicrophone() is playing");
2145 return 0;
2146 }
2147
niklase@google.com470e71d2011-07-07 08:21:25 +00002148 // Destroy the old instance
2149 if (_inputFilePlayerPtr)
2150 {
2151 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2152 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2153 _inputFilePlayerPtr = NULL;
2154 }
2155
2156 // Create the instance
2157 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2158 _inputFilePlayerId, (const FileFormats)format);
2159
2160 if (_inputFilePlayerPtr == NULL)
2161 {
2162 _engineStatisticsPtr->SetLastError(
2163 VE_INVALID_ARGUMENT, kTraceError,
2164 "StartPlayingInputFile() filePlayer format isnot correct");
2165 return -1;
2166 }
2167
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002168 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002169
2170 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2171 volumeScaling, notificationTime,
2172 stopPosition, codecInst) != 0)
2173 {
2174 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2175 "StartPlayingFile() failed to start "
2176 "file playout");
2177 _inputFilePlayerPtr->StopPlayingFile();
2178 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2179 _inputFilePlayerPtr = NULL;
2180 return -1;
2181 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002182
niklase@google.com470e71d2011-07-07 08:21:25 +00002183 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002184 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002185
2186 return 0;
2187}
2188
2189int Channel::StopPlayingFileAsMicrophone()
2190{
2191 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2192 "Channel::StopPlayingFileAsMicrophone()");
2193
tommi31fc21f2016-01-21 10:37:37 -08002194 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002195
2196 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002197 {
niklase@google.com470e71d2011-07-07 08:21:25 +00002198 return 0;
2199 }
2200
niklase@google.com470e71d2011-07-07 08:21:25 +00002201 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2202 {
2203 _engineStatisticsPtr->SetLastError(
2204 VE_STOP_RECORDING_FAILED, kTraceError,
2205 "StopPlayingFile() could not stop playing");
2206 return -1;
2207 }
2208 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2209 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2210 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002211 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002212
2213 return 0;
2214}
2215
2216int Channel::IsPlayingFileAsMicrophone() const
2217{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002218 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002219}
2220
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002221int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002222 const CodecInst* codecInst)
2223{
2224 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2225 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2226
2227 if (_outputFileRecording)
2228 {
2229 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2230 "StartRecordingPlayout() is already recording");
2231 return 0;
2232 }
2233
2234 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002235 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002236 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2237
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002238 if ((codecInst != NULL) &&
2239 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002240 {
2241 _engineStatisticsPtr->SetLastError(
2242 VE_BAD_ARGUMENT, kTraceError,
2243 "StartRecordingPlayout() invalid compression");
2244 return(-1);
2245 }
2246 if(codecInst == NULL)
2247 {
2248 format = kFileFormatPcm16kHzFile;
2249 codecInst=&dummyCodec;
2250 }
2251 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2252 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2253 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2254 {
2255 format = kFileFormatWavFile;
2256 }
2257 else
2258 {
2259 format = kFileFormatCompressedFile;
2260 }
2261
tommi31fc21f2016-01-21 10:37:37 -08002262 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002263
2264 // Destroy the old instance
2265 if (_outputFileRecorderPtr)
2266 {
2267 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2268 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2269 _outputFileRecorderPtr = NULL;
2270 }
2271
2272 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2273 _outputFileRecorderId, (const FileFormats)format);
2274 if (_outputFileRecorderPtr == NULL)
2275 {
2276 _engineStatisticsPtr->SetLastError(
2277 VE_INVALID_ARGUMENT, kTraceError,
2278 "StartRecordingPlayout() fileRecorder format isnot correct");
2279 return -1;
2280 }
2281
2282 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2283 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2284 {
2285 _engineStatisticsPtr->SetLastError(
2286 VE_BAD_FILE, kTraceError,
2287 "StartRecordingAudioFile() failed to start file recording");
2288 _outputFileRecorderPtr->StopRecording();
2289 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2290 _outputFileRecorderPtr = NULL;
2291 return -1;
2292 }
2293 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2294 _outputFileRecording = true;
2295
2296 return 0;
2297}
2298
2299int Channel::StartRecordingPlayout(OutStream* stream,
2300 const CodecInst* codecInst)
2301{
2302 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2303 "Channel::StartRecordingPlayout()");
2304
2305 if (_outputFileRecording)
2306 {
2307 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2308 "StartRecordingPlayout() is already recording");
2309 return 0;
2310 }
2311
2312 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002313 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002314 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2315
2316 if (codecInst != NULL && codecInst->channels != 1)
2317 {
2318 _engineStatisticsPtr->SetLastError(
2319 VE_BAD_ARGUMENT, kTraceError,
2320 "StartRecordingPlayout() invalid compression");
2321 return(-1);
2322 }
2323 if(codecInst == NULL)
2324 {
2325 format = kFileFormatPcm16kHzFile;
2326 codecInst=&dummyCodec;
2327 }
2328 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2329 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2330 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2331 {
2332 format = kFileFormatWavFile;
2333 }
2334 else
2335 {
2336 format = kFileFormatCompressedFile;
2337 }
2338
tommi31fc21f2016-01-21 10:37:37 -08002339 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002340
2341 // Destroy the old instance
2342 if (_outputFileRecorderPtr)
2343 {
2344 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2345 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2346 _outputFileRecorderPtr = NULL;
2347 }
2348
2349 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2350 _outputFileRecorderId, (const FileFormats)format);
2351 if (_outputFileRecorderPtr == NULL)
2352 {
2353 _engineStatisticsPtr->SetLastError(
2354 VE_INVALID_ARGUMENT, kTraceError,
2355 "StartRecordingPlayout() fileRecorder format isnot correct");
2356 return -1;
2357 }
2358
2359 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2360 notificationTime) != 0)
2361 {
2362 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2363 "StartRecordingPlayout() failed to "
2364 "start file recording");
2365 _outputFileRecorderPtr->StopRecording();
2366 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2367 _outputFileRecorderPtr = NULL;
2368 return -1;
2369 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002370
niklase@google.com470e71d2011-07-07 08:21:25 +00002371 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2372 _outputFileRecording = true;
2373
2374 return 0;
2375}
2376
2377int Channel::StopRecordingPlayout()
2378{
2379 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2380 "Channel::StopRecordingPlayout()");
2381
2382 if (!_outputFileRecording)
2383 {
2384 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2385 "StopRecordingPlayout() isnot recording");
2386 return -1;
2387 }
2388
2389
tommi31fc21f2016-01-21 10:37:37 -08002390 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002391
2392 if (_outputFileRecorderPtr->StopRecording() != 0)
2393 {
2394 _engineStatisticsPtr->SetLastError(
2395 VE_STOP_RECORDING_FAILED, kTraceError,
2396 "StopRecording() could not stop recording");
2397 return(-1);
2398 }
2399 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2400 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2401 _outputFileRecorderPtr = NULL;
2402 _outputFileRecording = false;
2403
2404 return 0;
2405}
2406
2407void
2408Channel::SetMixWithMicStatus(bool mix)
2409{
tommi31fc21f2016-01-21 10:37:37 -08002410 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002411 _mixFileWithMicrophone=mix;
2412}
2413
2414int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002415Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002416{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002417 int8_t currentLevel = _outputAudioLevel.Level();
2418 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002419 return 0;
2420}
2421
2422int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002423Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002424{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002425 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2426 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002427 return 0;
2428}
2429
2430int
2431Channel::SetMute(bool enable)
2432{
tommi31fc21f2016-01-21 10:37:37 -08002433 rtc::CritScope cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002434 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2435 "Channel::SetMute(enable=%d)", enable);
2436 _mute = enable;
2437 return 0;
2438}
2439
2440bool
2441Channel::Mute() const
2442{
tommi31fc21f2016-01-21 10:37:37 -08002443 rtc::CritScope cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002444 return _mute;
2445}
2446
2447int
2448Channel::SetOutputVolumePan(float left, float right)
2449{
tommi31fc21f2016-01-21 10:37:37 -08002450 rtc::CritScope cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002451 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2452 "Channel::SetOutputVolumePan()");
2453 _panLeft = left;
2454 _panRight = right;
2455 return 0;
2456}
2457
2458int
2459Channel::GetOutputVolumePan(float& left, float& right) const
2460{
tommi31fc21f2016-01-21 10:37:37 -08002461 rtc::CritScope cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002462 left = _panLeft;
2463 right = _panRight;
niklase@google.com470e71d2011-07-07 08:21:25 +00002464 return 0;
2465}
2466
2467int
2468Channel::SetChannelOutputVolumeScaling(float scaling)
2469{
tommi31fc21f2016-01-21 10:37:37 -08002470 rtc::CritScope cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002471 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2472 "Channel::SetChannelOutputVolumeScaling()");
2473 _outputGain = scaling;
2474 return 0;
2475}
2476
2477int
2478Channel::GetChannelOutputVolumeScaling(float& scaling) const
2479{
tommi31fc21f2016-01-21 10:37:37 -08002480 rtc::CritScope cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002481 scaling = _outputGain;
niklase@google.com470e71d2011-07-07 08:21:25 +00002482 return 0;
2483}
2484
niklase@google.com470e71d2011-07-07 08:21:25 +00002485int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002486 int lengthMs, int attenuationDb,
2487 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002488{
2489 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2490 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2491 playDtmfEvent);
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002492 if (!Sending()) {
2493 return -1;
2494 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002495
2496 _playOutbandDtmfEvent = playDtmfEvent;
2497
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002498 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002499 attenuationDb) != 0)
2500 {
2501 _engineStatisticsPtr->SetLastError(
2502 VE_SEND_DTMF_FAILED,
2503 kTraceWarning,
2504 "SendTelephoneEventOutband() failed to send event");
2505 return -1;
2506 }
2507 return 0;
2508}
2509
2510int Channel::SendTelephoneEventInband(unsigned char eventCode,
2511 int lengthMs,
2512 int attenuationDb,
2513 bool playDtmfEvent)
2514{
2515 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2516 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2517 playDtmfEvent);
2518
2519 _playInbandDtmfEvent = playDtmfEvent;
2520 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2521
2522 return 0;
2523}
2524
2525int
niklase@google.com470e71d2011-07-07 08:21:25 +00002526Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2527{
2528 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2529 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002530 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002531 {
2532 _engineStatisticsPtr->SetLastError(
2533 VE_INVALID_ARGUMENT, kTraceError,
2534 "SetSendTelephoneEventPayloadType() invalid type");
2535 return -1;
2536 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002537 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002538 codec.plfreq = 8000;
2539 codec.pltype = type;
2540 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002541 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002542 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002543 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2544 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2545 _engineStatisticsPtr->SetLastError(
2546 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2547 "SetSendTelephoneEventPayloadType() failed to register send"
2548 "payload type");
2549 return -1;
2550 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002551 }
2552 _sendTelephoneEventPayloadType = type;
2553 return 0;
2554}
2555
2556int
2557Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2558{
niklase@google.com470e71d2011-07-07 08:21:25 +00002559 type = _sendTelephoneEventPayloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00002560 return 0;
2561}
2562
niklase@google.com470e71d2011-07-07 08:21:25 +00002563int
2564Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2565{
2566 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2567 "Channel::UpdateRxVadDetection()");
2568
2569 int vadDecision = 1;
2570
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002571 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002572
2573 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2574 {
2575 OnRxVadDetected(vadDecision);
2576 _oldVadDecision = vadDecision;
2577 }
2578
2579 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2580 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2581 vadDecision);
2582 return 0;
2583}
2584
2585int
2586Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2587{
2588 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2589 "Channel::RegisterRxVadObserver()");
tommi31fc21f2016-01-21 10:37:37 -08002590 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002591
2592 if (_rxVadObserverPtr)
2593 {
2594 _engineStatisticsPtr->SetLastError(
2595 VE_INVALID_OPERATION, kTraceError,
2596 "RegisterRxVadObserver() observer already enabled");
2597 return -1;
2598 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002599 _rxVadObserverPtr = &observer;
2600 _RxVadDetection = true;
2601 return 0;
2602}
2603
2604int
2605Channel::DeRegisterRxVadObserver()
2606{
2607 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2608 "Channel::DeRegisterRxVadObserver()");
tommi31fc21f2016-01-21 10:37:37 -08002609 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002610
2611 if (!_rxVadObserverPtr)
2612 {
2613 _engineStatisticsPtr->SetLastError(
2614 VE_INVALID_OPERATION, kTraceWarning,
2615 "DeRegisterRxVadObserver() observer already disabled");
2616 return 0;
2617 }
2618 _rxVadObserverPtr = NULL;
2619 _RxVadDetection = false;
2620 return 0;
2621}
2622
2623int
2624Channel::VoiceActivityIndicator(int &activity)
2625{
2626 activity = _sendFrameType;
niklase@google.com470e71d2011-07-07 08:21:25 +00002627 return 0;
2628}
2629
2630#ifdef WEBRTC_VOICE_ENGINE_AGC
2631
2632int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002633Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002634{
2635 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2636 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2637 (int)enable, (int)mode);
2638
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002639 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002640 switch (mode)
2641 {
2642 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002643 break;
2644 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002645 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002646 break;
2647 case kAgcFixedDigital:
2648 agcMode = GainControl::kFixedDigital;
2649 break;
2650 case kAgcAdaptiveDigital:
2651 agcMode =GainControl::kAdaptiveDigital;
2652 break;
2653 default:
2654 _engineStatisticsPtr->SetLastError(
2655 VE_INVALID_ARGUMENT, kTraceError,
2656 "SetRxAgcStatus() invalid Agc mode");
2657 return -1;
2658 }
2659
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002660 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002661 {
2662 _engineStatisticsPtr->SetLastError(
2663 VE_APM_ERROR, kTraceError,
2664 "SetRxAgcStatus() failed to set Agc mode");
2665 return -1;
2666 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002667 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002668 {
2669 _engineStatisticsPtr->SetLastError(
2670 VE_APM_ERROR, kTraceError,
2671 "SetRxAgcStatus() failed to set Agc state");
2672 return -1;
2673 }
2674
2675 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002676 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002677
2678 return 0;
2679}
2680
2681int
2682Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2683{
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002684 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002685 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002686 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002687
2688 enabled = enable;
2689
2690 switch (agcMode)
2691 {
2692 case GainControl::kFixedDigital:
2693 mode = kAgcFixedDigital;
2694 break;
2695 case GainControl::kAdaptiveDigital:
2696 mode = kAgcAdaptiveDigital;
2697 break;
2698 default:
2699 _engineStatisticsPtr->SetLastError(
2700 VE_APM_ERROR, kTraceError,
2701 "GetRxAgcStatus() invalid Agc mode");
2702 return -1;
2703 }
2704
2705 return 0;
2706}
2707
2708int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002709Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002710{
2711 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2712 "Channel::SetRxAgcConfig()");
2713
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002714 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002715 config.targetLeveldBOv) != 0)
2716 {
2717 _engineStatisticsPtr->SetLastError(
2718 VE_APM_ERROR, kTraceError,
2719 "SetRxAgcConfig() failed to set target peak |level|"
2720 "(or envelope) of the Agc");
2721 return -1;
2722 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002723 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002724 config.digitalCompressionGaindB) != 0)
2725 {
2726 _engineStatisticsPtr->SetLastError(
2727 VE_APM_ERROR, kTraceError,
2728 "SetRxAgcConfig() failed to set the range in |gain| the"
2729 " digital compression stage may apply");
2730 return -1;
2731 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002732 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002733 config.limiterEnable) != 0)
2734 {
2735 _engineStatisticsPtr->SetLastError(
2736 VE_APM_ERROR, kTraceError,
2737 "SetRxAgcConfig() failed to set hard limiter to the signal");
2738 return -1;
2739 }
2740
2741 return 0;
2742}
2743
2744int
2745Channel::GetRxAgcConfig(AgcConfig& config)
2746{
niklase@google.com470e71d2011-07-07 08:21:25 +00002747 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002748 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002749 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002750 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002751 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002752 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002753
niklase@google.com470e71d2011-07-07 08:21:25 +00002754 return 0;
2755}
2756
2757#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2758
2759#ifdef WEBRTC_VOICE_ENGINE_NR
2760
2761int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002762Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002763{
2764 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2765 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2766 (int)enable, (int)mode);
2767
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002768 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002769 switch (mode)
2770 {
2771
2772 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002773 break;
2774 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002775 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002776 break;
2777 case kNsConference:
2778 nsLevel = NoiseSuppression::kHigh;
2779 break;
2780 case kNsLowSuppression:
2781 nsLevel = NoiseSuppression::kLow;
2782 break;
2783 case kNsModerateSuppression:
2784 nsLevel = NoiseSuppression::kModerate;
2785 break;
2786 case kNsHighSuppression:
2787 nsLevel = NoiseSuppression::kHigh;
2788 break;
2789 case kNsVeryHighSuppression:
2790 nsLevel = NoiseSuppression::kVeryHigh;
2791 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002792 }
2793
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002794 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002795 != 0)
2796 {
2797 _engineStatisticsPtr->SetLastError(
2798 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002799 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002800 return -1;
2801 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002802 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002803 {
2804 _engineStatisticsPtr->SetLastError(
2805 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002806 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002807 return -1;
2808 }
2809
2810 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002811 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002812
2813 return 0;
2814}
2815
2816int
2817Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2818{
niklase@google.com470e71d2011-07-07 08:21:25 +00002819 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002820 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002821 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002822 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002823
2824 enabled = enable;
2825
2826 switch (ncLevel)
2827 {
2828 case NoiseSuppression::kLow:
2829 mode = kNsLowSuppression;
2830 break;
2831 case NoiseSuppression::kModerate:
2832 mode = kNsModerateSuppression;
2833 break;
2834 case NoiseSuppression::kHigh:
2835 mode = kNsHighSuppression;
2836 break;
2837 case NoiseSuppression::kVeryHigh:
2838 mode = kNsVeryHighSuppression;
2839 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002840 }
2841
niklase@google.com470e71d2011-07-07 08:21:25 +00002842 return 0;
2843}
2844
2845#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2846
2847int
niklase@google.com470e71d2011-07-07 08:21:25 +00002848Channel::SetLocalSSRC(unsigned int ssrc)
2849{
2850 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2851 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002852 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002853 {
2854 _engineStatisticsPtr->SetLastError(
2855 VE_ALREADY_SENDING, kTraceError,
2856 "SetLocalSSRC() already sending");
2857 return -1;
2858 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002859 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002860 return 0;
2861}
2862
2863int
2864Channel::GetLocalSSRC(unsigned int& ssrc)
2865{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002866 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002867 return 0;
2868}
2869
2870int
2871Channel::GetRemoteSSRC(unsigned int& ssrc)
2872{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002873 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002874 return 0;
2875}
2876
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002877int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002878 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002879 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002880}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002881
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002882int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2883 unsigned char id) {
2884 rtp_header_parser_->DeregisterRtpHeaderExtension(
2885 kRtpExtensionAudioLevel);
2886 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2887 kRtpExtensionAudioLevel, id)) {
2888 return -1;
2889 }
2890 return 0;
2891}
2892
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002893int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2894 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2895}
2896
2897int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2898 rtp_header_parser_->DeregisterRtpHeaderExtension(
2899 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002900 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2901 kRtpExtensionAbsoluteSendTime, id)) {
2902 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002903 }
2904 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002905}
2906
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002907void Channel::EnableSendTransportSequenceNumber(int id) {
2908 int ret =
2909 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2910 RTC_DCHECK_EQ(0, ret);
2911}
2912
stefan3313ec92016-01-21 06:32:43 -08002913void Channel::EnableReceiveTransportSequenceNumber(int id) {
2914 rtp_header_parser_->DeregisterRtpHeaderExtension(
2915 kRtpExtensionTransportSequenceNumber);
2916 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2917 kRtpExtensionTransportSequenceNumber, id);
2918 RTC_DCHECK(ret);
2919}
2920
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002921void Channel::SetCongestionControlObjects(
2922 RtpPacketSender* rtp_packet_sender,
2923 TransportFeedbackObserver* transport_feedback_observer,
2924 PacketRouter* packet_router) {
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002925 RTC_DCHECK(packet_router != nullptr || packet_router_ != nullptr);
Stefan Holmer3842c5c2016-01-12 13:55:00 +01002926 if (transport_feedback_observer) {
2927 RTC_DCHECK(feedback_observer_proxy_.get());
2928 feedback_observer_proxy_->SetTransportFeedbackObserver(
2929 transport_feedback_observer);
2930 }
2931 if (rtp_packet_sender) {
2932 RTC_DCHECK(rtp_packet_sender_proxy_.get());
2933 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2934 }
2935 if (seq_num_allocator_proxy_.get()) {
2936 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2937 }
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002938 _rtpRtcpModule->SetStorePacketsStatus(rtp_packet_sender != nullptr, 600);
2939 if (packet_router != nullptr) {
2940 packet_router->AddRtpModule(_rtpRtcpModule.get());
2941 } else {
2942 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
2943 }
2944 packet_router_ = packet_router;
2945}
2946
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002947void Channel::SetRTCPStatus(bool enable) {
2948 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2949 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002950 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002951}
2952
2953int
2954Channel::GetRTCPStatus(bool& enabled)
2955{
pbosda903ea2015-10-02 02:36:56 -07002956 RtcpMode method = _rtpRtcpModule->RTCP();
2957 enabled = (method != RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002958 return 0;
2959}
2960
2961int
2962Channel::SetRTCP_CNAME(const char cName[256])
2963{
2964 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2965 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002966 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002967 {
2968 _engineStatisticsPtr->SetLastError(
2969 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2970 "SetRTCP_CNAME() failed to set RTCP CNAME");
2971 return -1;
2972 }
2973 return 0;
2974}
2975
2976int
niklase@google.com470e71d2011-07-07 08:21:25 +00002977Channel::GetRemoteRTCP_CNAME(char cName[256])
2978{
2979 if (cName == NULL)
2980 {
2981 _engineStatisticsPtr->SetLastError(
2982 VE_INVALID_ARGUMENT, kTraceError,
2983 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2984 return -1;
2985 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002986 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002987 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002988 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002989 {
2990 _engineStatisticsPtr->SetLastError(
2991 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2992 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2993 return -1;
2994 }
2995 strcpy(cName, cname);
niklase@google.com470e71d2011-07-07 08:21:25 +00002996 return 0;
2997}
2998
2999int
3000Channel::GetRemoteRTCPData(
3001 unsigned int& NTPHigh,
3002 unsigned int& NTPLow,
3003 unsigned int& timestamp,
3004 unsigned int& playoutTimestamp,
3005 unsigned int* jitter,
3006 unsigned short* fractionLost)
3007{
3008 // --- Information from sender info in received Sender Reports
3009
3010 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003011 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003012 {
3013 _engineStatisticsPtr->SetLastError(
3014 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003015 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00003016 "side");
3017 return -1;
3018 }
3019
3020 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
3021 // and octet count)
3022 NTPHigh = senderInfo.NTPseconds;
3023 NTPLow = senderInfo.NTPfraction;
3024 timestamp = senderInfo.RTPtimeStamp;
3025
niklase@google.com470e71d2011-07-07 08:21:25 +00003026 // --- Locally derived information
3027
3028 // This value is updated on each incoming RTCP packet (0 when no packet
3029 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003030 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003031
niklase@google.com470e71d2011-07-07 08:21:25 +00003032 if (NULL != jitter || NULL != fractionLost)
3033 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003034 // Get all RTCP receiver report blocks that have been received on this
3035 // channel. If we receive RTP packets from a remote source we know the
3036 // remote SSRC and use the report block from him.
3037 // Otherwise use the first report block.
3038 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003039 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003040 remote_stats.empty()) {
3041 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3042 VoEId(_instanceId, _channelId),
3043 "GetRemoteRTCPData() failed to measure statistics due"
3044 " to lack of received RTP and/or RTCP packets");
3045 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003046 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003047
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003048 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003049 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3050 for (; it != remote_stats.end(); ++it) {
3051 if (it->remoteSSRC == remoteSSRC)
3052 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003053 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003054
3055 if (it == remote_stats.end()) {
3056 // If we have not received any RTCP packets from this SSRC it probably
3057 // means that we have not received any RTP packets.
3058 // Use the first received report block instead.
3059 it = remote_stats.begin();
3060 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003061 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003062
xians@webrtc.org79af7342012-01-31 12:22:14 +00003063 if (jitter) {
3064 *jitter = it->jitter;
xians@webrtc.org79af7342012-01-31 12:22:14 +00003065 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003066
xians@webrtc.org79af7342012-01-31 12:22:14 +00003067 if (fractionLost) {
3068 *fractionLost = it->fractionLost;
xians@webrtc.org79af7342012-01-31 12:22:14 +00003069 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003070 }
3071 return 0;
3072}
3073
3074int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003075Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003076 unsigned int name,
3077 const char* data,
3078 unsigned short dataLengthInBytes)
3079{
3080 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3081 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003082 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003083 {
3084 _engineStatisticsPtr->SetLastError(
3085 VE_NOT_SENDING, kTraceError,
3086 "SendApplicationDefinedRTCPPacket() not sending");
3087 return -1;
3088 }
3089 if (NULL == data)
3090 {
3091 _engineStatisticsPtr->SetLastError(
3092 VE_INVALID_ARGUMENT, kTraceError,
3093 "SendApplicationDefinedRTCPPacket() invalid data value");
3094 return -1;
3095 }
3096 if (dataLengthInBytes % 4 != 0)
3097 {
3098 _engineStatisticsPtr->SetLastError(
3099 VE_INVALID_ARGUMENT, kTraceError,
3100 "SendApplicationDefinedRTCPPacket() invalid length value");
3101 return -1;
3102 }
pbosda903ea2015-10-02 02:36:56 -07003103 RtcpMode status = _rtpRtcpModule->RTCP();
3104 if (status == RtcpMode::kOff) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003105 _engineStatisticsPtr->SetLastError(
3106 VE_RTCP_ERROR, kTraceError,
3107 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3108 return -1;
3109 }
3110
3111 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003112 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003113 subType,
3114 name,
3115 (const unsigned char*) data,
3116 dataLengthInBytes) != 0)
3117 {
3118 _engineStatisticsPtr->SetLastError(
3119 VE_SEND_ERROR, kTraceError,
3120 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3121 return -1;
3122 }
3123 return 0;
3124}
3125
3126int
3127Channel::GetRTPStatistics(
3128 unsigned int& averageJitterMs,
3129 unsigned int& maxJitterMs,
3130 unsigned int& discardedPackets)
3131{
niklase@google.com470e71d2011-07-07 08:21:25 +00003132 // The jitter statistics is updated for each received RTP packet and is
3133 // based on received packets.
pbosda903ea2015-10-02 02:36:56 -07003134 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003135 // If RTCP is off, there is no timed thread in the RTCP module regularly
3136 // generating new stats, trigger the update manually here instead.
3137 StreamStatistician* statistician =
3138 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3139 if (statistician) {
3140 // Don't use returned statistics, use data from proxy instead so that
3141 // max jitter can be fetched atomically.
3142 RtcpStatistics s;
3143 statistician->GetStatistics(&s, true);
3144 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003145 }
3146
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003147 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003148 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003149 if (playoutFrequency > 0) {
3150 // Scale RTP statistics given the current playout frequency
3151 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3152 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003153 }
3154
3155 discardedPackets = _numberOfDiscardedPackets;
3156
niklase@google.com470e71d2011-07-07 08:21:25 +00003157 return 0;
3158}
3159
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003160int Channel::GetRemoteRTCPReportBlocks(
3161 std::vector<ReportBlock>* report_blocks) {
3162 if (report_blocks == NULL) {
3163 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3164 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3165 return -1;
3166 }
3167
3168 // Get the report blocks from the latest received RTCP Sender or Receiver
3169 // Report. Each element in the vector contains the sender's SSRC and a
3170 // report block according to RFC 3550.
3171 std::vector<RTCPReportBlock> rtcp_report_blocks;
3172 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003173 return -1;
3174 }
3175
3176 if (rtcp_report_blocks.empty())
3177 return 0;
3178
3179 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3180 for (; it != rtcp_report_blocks.end(); ++it) {
3181 ReportBlock report_block;
3182 report_block.sender_SSRC = it->remoteSSRC;
3183 report_block.source_SSRC = it->sourceSSRC;
3184 report_block.fraction_lost = it->fractionLost;
3185 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3186 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3187 report_block.interarrival_jitter = it->jitter;
3188 report_block.last_SR_timestamp = it->lastSR;
3189 report_block.delay_since_last_SR = it->delaySinceLastSR;
3190 report_blocks->push_back(report_block);
3191 }
3192 return 0;
3193}
3194
niklase@google.com470e71d2011-07-07 08:21:25 +00003195int
3196Channel::GetRTPStatistics(CallStatistics& stats)
3197{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003198 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003199
3200 // The jitter statistics is updated for each received RTP packet and is
3201 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003202 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003203 StreamStatistician* statistician =
3204 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
pbosda903ea2015-10-02 02:36:56 -07003205 if (!statistician ||
3206 !statistician->GetStatistics(
3207 &statistics, _rtpRtcpModule->RTCP() == RtcpMode::kOff)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003208 _engineStatisticsPtr->SetLastError(
3209 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3210 "GetRTPStatistics() failed to read RTP statistics from the "
3211 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003212 }
3213
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003214 stats.fractionLost = statistics.fraction_lost;
3215 stats.cumulativeLost = statistics.cumulative_lost;
3216 stats.extendedMax = statistics.extended_max_sequence_number;
3217 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003218
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003219 // --- RTT
Minyue2013aec2015-05-13 14:14:42 +02003220 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003221
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003222 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003223
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003224 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003225 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003226 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003227 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003228
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003229 if (statistician) {
3230 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3231 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003232
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003233 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003234 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003235 {
3236 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3237 VoEId(_instanceId, _channelId),
3238 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003239 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003240 }
3241
3242 stats.bytesSent = bytesSent;
3243 stats.packetsSent = packetsSent;
3244 stats.bytesReceived = bytesReceived;
3245 stats.packetsReceived = packetsReceived;
3246
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003247 // --- Timestamps
3248 {
tommi31fc21f2016-01-21 10:37:37 -08003249 rtc::CritScope lock(&ts_stats_lock_);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003250 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3251 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003252 return 0;
3253}
3254
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003255int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003256 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003257 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003258
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003259 if (enable) {
3260 if (redPayloadtype < 0 || redPayloadtype > 127) {
3261 _engineStatisticsPtr->SetLastError(
3262 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003263 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003264 return -1;
3265 }
3266
3267 if (SetRedPayloadType(redPayloadtype) < 0) {
3268 _engineStatisticsPtr->SetLastError(
3269 VE_CODEC_ERROR, kTraceError,
3270 "SetSecondarySendCodec() Failed to register RED ACM");
3271 return -1;
3272 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003273 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003274
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003275 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003276 _engineStatisticsPtr->SetLastError(
3277 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003278 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003279 return -1;
3280 }
3281 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003282}
3283
3284int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003285Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003286{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003287 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003288 if (enabled)
3289 {
danilchap5c1def82015-12-10 09:51:54 -08003290 int8_t payloadType = 0;
3291 if (_rtpRtcpModule->SendREDPayloadType(&payloadType) != 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003292 _engineStatisticsPtr->SetLastError(
3293 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003294 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003295 "module");
3296 return -1;
3297 }
pkasting@chromium.orgdf9a41d2015-01-26 22:35:29 +00003298 redPayloadtype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00003299 return 0;
3300 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003301 return 0;
3302}
3303
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003304int Channel::SetCodecFECStatus(bool enable) {
3305 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3306 "Channel::SetCodecFECStatus()");
3307
3308 if (audio_coding_->SetCodecFEC(enable) != 0) {
3309 _engineStatisticsPtr->SetLastError(
3310 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3311 "SetCodecFECStatus() failed to set FEC state");
3312 return -1;
3313 }
3314 return 0;
3315}
3316
3317bool Channel::GetCodecFECStatus() {
3318 bool enabled = audio_coding_->CodecFEC();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003319 return enabled;
3320}
3321
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003322void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3323 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01003324 // If pacing is enabled we always store packets.
3325 if (!pacing_enabled_)
3326 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003327 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3328 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003329 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003330 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003331 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003332 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003333}
3334
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003335// Called when we are missing one or more packets.
3336int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003337 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3338}
3339
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003340uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003341Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003342{
3343 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003344 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003345 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003346 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003347 return 0;
3348}
3349
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003350void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003351 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07003352 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08003353 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003354 CodecInst codec;
3355 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003356
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07003357 // Never upsample or upmix the capture signal here. This should be done at the
3358 // end of the send chain.
3359 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
3360 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
3361 RemixAndResample(audio_data, number_of_frames, number_of_channels,
3362 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003363}
3364
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003365uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003366Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003367{
3368 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3369 "Channel::PrepareEncodeAndSend()");
3370
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003371 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003372 {
3373 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3374 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003375 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003376 }
3377
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003378 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003379 {
3380 MixOrReplaceAudioWithFile(mixingFrequency);
3381 }
3382
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003383 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3384 if (is_muted) {
3385 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003386 }
3387
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003388 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003389 {
tommi31fc21f2016-01-21 10:37:37 -08003390 rtc::CritScope cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003391 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003392 if (_inputExternalMediaCallbackPtr)
3393 {
3394 _inputExternalMediaCallbackPtr->Process(
3395 _channelId,
3396 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003397 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003398 _audioFrame.samples_per_channel_,
3399 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003400 isStereo);
3401 }
3402 }
3403
3404 InsertInbandDtmfTone();
3405
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003406 if (_includeAudioLevelIndication) {
Peter Kastingdce40cf2015-08-24 14:52:23 -07003407 size_t length =
3408 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003409 if (is_muted) {
3410 rms_level_.ProcessMuted(length);
3411 } else {
3412 rms_level_.Process(_audioFrame.data_, length);
3413 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003414 }
3415
niklase@google.com470e71d2011-07-07 08:21:25 +00003416 return 0;
3417}
3418
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003419uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003420Channel::EncodeAndSend()
3421{
3422 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3423 "Channel::EncodeAndSend()");
3424
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003425 assert(_audioFrame.num_channels_ <= 2);
3426 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003427 {
3428 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3429 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003430 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003431 }
3432
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003433 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003434
3435 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3436
3437 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003438 _audioFrame.timestamp_ = _timeStamp;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003439 // This call will trigger AudioPacketizationCallback::SendData if encoding
3440 // is done and payload is ready for packetization and transmission.
3441 // Otherwise, it will return without invoking the callback.
3442 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003443 {
3444 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3445 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003446 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003447 }
3448
Peter Kastingb7e50542015-06-11 12:55:50 -07003449 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003450 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003451}
3452
Minyue2013aec2015-05-13 14:14:42 +02003453void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08003454 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003455 Channel* channel = associate_send_channel_.channel();
3456 if (channel && channel->ChannelId() == channel_id) {
3457 // If this channel is associated with a send channel of the specified
3458 // Channel ID, disassociate with it.
3459 ChannelOwner ref(NULL);
3460 associate_send_channel_ = ref;
3461 }
3462}
3463
niklase@google.com470e71d2011-07-07 08:21:25 +00003464int Channel::RegisterExternalMediaProcessing(
3465 ProcessingTypes type,
3466 VoEMediaProcess& processObject)
3467{
3468 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3469 "Channel::RegisterExternalMediaProcessing()");
3470
tommi31fc21f2016-01-21 10:37:37 -08003471 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003472
3473 if (kPlaybackPerChannel == type)
3474 {
3475 if (_outputExternalMediaCallbackPtr)
3476 {
3477 _engineStatisticsPtr->SetLastError(
3478 VE_INVALID_OPERATION, kTraceError,
3479 "Channel::RegisterExternalMediaProcessing() "
3480 "output external media already enabled");
3481 return -1;
3482 }
3483 _outputExternalMediaCallbackPtr = &processObject;
3484 _outputExternalMedia = true;
3485 }
3486 else if (kRecordingPerChannel == type)
3487 {
3488 if (_inputExternalMediaCallbackPtr)
3489 {
3490 _engineStatisticsPtr->SetLastError(
3491 VE_INVALID_OPERATION, kTraceError,
3492 "Channel::RegisterExternalMediaProcessing() "
3493 "output external media already enabled");
3494 return -1;
3495 }
3496 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003497 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003498 }
3499 return 0;
3500}
3501
3502int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3503{
3504 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3505 "Channel::DeRegisterExternalMediaProcessing()");
3506
tommi31fc21f2016-01-21 10:37:37 -08003507 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003508
3509 if (kPlaybackPerChannel == type)
3510 {
3511 if (!_outputExternalMediaCallbackPtr)
3512 {
3513 _engineStatisticsPtr->SetLastError(
3514 VE_INVALID_OPERATION, kTraceWarning,
3515 "Channel::DeRegisterExternalMediaProcessing() "
3516 "output external media already disabled");
3517 return 0;
3518 }
3519 _outputExternalMedia = false;
3520 _outputExternalMediaCallbackPtr = NULL;
3521 }
3522 else if (kRecordingPerChannel == type)
3523 {
3524 if (!_inputExternalMediaCallbackPtr)
3525 {
3526 _engineStatisticsPtr->SetLastError(
3527 VE_INVALID_OPERATION, kTraceWarning,
3528 "Channel::DeRegisterExternalMediaProcessing() "
3529 "input external media already disabled");
3530 return 0;
3531 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003532 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003533 _inputExternalMediaCallbackPtr = NULL;
3534 }
3535
3536 return 0;
3537}
3538
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003539int Channel::SetExternalMixing(bool enabled) {
3540 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3541 "Channel::SetExternalMixing(enabled=%d)", enabled);
3542
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003543 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003544 {
3545 _engineStatisticsPtr->SetLastError(
3546 VE_INVALID_OPERATION, kTraceError,
3547 "Channel::SetExternalMixing() "
3548 "external mixing cannot be changed while playing.");
3549 return -1;
3550 }
3551
3552 _externalMixing = enabled;
3553
3554 return 0;
3555}
3556
niklase@google.com470e71d2011-07-07 08:21:25 +00003557int
niklase@google.com470e71d2011-07-07 08:21:25 +00003558Channel::GetNetworkStatistics(NetworkStatistics& stats)
3559{
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +00003560 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003561}
3562
wu@webrtc.org24301a62013-12-13 19:17:43 +00003563void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3564 audio_coding_->GetDecodingCallStatistics(stats);
3565}
3566
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003567bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3568 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08003569 rtc::CritScope lock(&video_sync_lock_);
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003570 if (_average_jitter_buffer_delay_us == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003571 return false;
3572 }
3573 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3574 _recPacketDelayMs;
3575 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003576 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003577}
3578
solenberg358057b2015-11-27 10:46:42 -08003579uint32_t Channel::GetDelayEstimate() const {
3580 int jitter_buffer_delay_ms = 0;
3581 int playout_buffer_delay_ms = 0;
3582 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
3583 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
3584}
3585
deadbeef74375882015-08-13 12:09:10 -07003586int Channel::LeastRequiredDelayMs() const {
3587 return audio_coding_->LeastRequiredDelayMs();
3588}
3589
niklase@google.com470e71d2011-07-07 08:21:25 +00003590int
3591Channel::SetMinimumPlayoutDelay(int delayMs)
3592{
3593 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3594 "Channel::SetMinimumPlayoutDelay()");
3595 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3596 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3597 {
3598 _engineStatisticsPtr->SetLastError(
3599 VE_INVALID_ARGUMENT, kTraceError,
3600 "SetMinimumPlayoutDelay() invalid min delay");
3601 return -1;
3602 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003603 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003604 {
3605 _engineStatisticsPtr->SetLastError(
3606 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3607 "SetMinimumPlayoutDelay() failed to set min playout delay");
3608 return -1;
3609 }
3610 return 0;
3611}
3612
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003613int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07003614 uint32_t playout_timestamp_rtp = 0;
3615 {
tommi31fc21f2016-01-21 10:37:37 -08003616 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003617 playout_timestamp_rtp = playout_timestamp_rtp_;
3618 }
3619 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003620 _engineStatisticsPtr->SetLastError(
3621 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3622 "GetPlayoutTimestamp() failed to retrieve timestamp");
3623 return -1;
3624 }
deadbeef74375882015-08-13 12:09:10 -07003625 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003626 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003627}
3628
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003629int Channel::SetInitTimestamp(unsigned int timestamp) {
3630 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003631 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003632 if (channel_state_.Get().sending) {
3633 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3634 "SetInitTimestamp() already sending");
3635 return -1;
3636 }
3637 _rtpRtcpModule->SetStartTimestamp(timestamp);
3638 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003639}
3640
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003641int Channel::SetInitSequenceNumber(short sequenceNumber) {
3642 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3643 "Channel::SetInitSequenceNumber()");
3644 if (channel_state_.Get().sending) {
3645 _engineStatisticsPtr->SetLastError(
3646 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3647 return -1;
3648 }
3649 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3650 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003651}
3652
3653int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003654Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003655{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003656 *rtpRtcpModule = _rtpRtcpModule.get();
3657 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003658 return 0;
3659}
3660
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003661// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3662// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003663int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003664Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003665{
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003666 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
Peter Kastingdce40cf2015-08-24 14:52:23 -07003667 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003668
3669 {
tommi31fc21f2016-01-21 10:37:37 -08003670 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003671
3672 if (_inputFilePlayerPtr == NULL)
3673 {
3674 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3675 VoEId(_instanceId, _channelId),
3676 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3677 " doesnt exist");
3678 return -1;
3679 }
3680
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003681 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003682 fileSamples,
3683 mixingFrequency) == -1)
3684 {
3685 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3686 VoEId(_instanceId, _channelId),
3687 "Channel::MixOrReplaceAudioWithFile() file mixing "
3688 "failed");
3689 return -1;
3690 }
3691 if (fileSamples == 0)
3692 {
3693 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3694 VoEId(_instanceId, _channelId),
3695 "Channel::MixOrReplaceAudioWithFile() file is ended");
3696 return 0;
3697 }
3698 }
3699
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003700 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003701
3702 if (_mixFileWithMicrophone)
3703 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003704 // Currently file stream is always mono.
3705 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003706 MixWithSat(_audioFrame.data_,
3707 _audioFrame.num_channels_,
3708 fileBuffer.get(),
3709 1,
3710 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003711 }
3712 else
3713 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003714 // Replace ACM audio with file.
3715 // Currently file stream is always mono.
3716 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003717 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003718 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003719 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003720 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003721 mixingFrequency,
3722 AudioFrame::kNormalSpeech,
3723 AudioFrame::kVadUnknown,
3724 1);
3725
3726 }
3727 return 0;
3728}
3729
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003730int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003731Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003732 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003733{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003734 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003735
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003736 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
Peter Kastingdce40cf2015-08-24 14:52:23 -07003737 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003738
3739 {
tommi31fc21f2016-01-21 10:37:37 -08003740 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003741
3742 if (_outputFilePlayerPtr == NULL)
3743 {
3744 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3745 VoEId(_instanceId, _channelId),
3746 "Channel::MixAudioWithFile() file mixing failed");
3747 return -1;
3748 }
3749
3750 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003751 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003752 fileSamples,
3753 mixingFrequency) == -1)
3754 {
3755 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3756 VoEId(_instanceId, _channelId),
3757 "Channel::MixAudioWithFile() file mixing failed");
3758 return -1;
3759 }
3760 }
3761
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003762 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003763 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003764 // Currently file stream is always mono.
3765 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003766 MixWithSat(audioFrame.data_,
3767 audioFrame.num_channels_,
3768 fileBuffer.get(),
3769 1,
3770 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003771 }
3772 else
3773 {
3774 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Kastingdce40cf2015-08-24 14:52:23 -07003775 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS ") != "
3776 "fileSamples(%" PRIuS ")",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003777 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003778 return -1;
3779 }
3780
3781 return 0;
3782}
3783
3784int
3785Channel::InsertInbandDtmfTone()
3786{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003787 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003788 if (_inbandDtmfQueue.PendingDtmf() &&
3789 !_inbandDtmfGenerator.IsAddingTone() &&
3790 _inbandDtmfGenerator.DelaySinceLastTone() >
3791 kMinTelephoneEventSeparationMs)
3792 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003793 int8_t eventCode(0);
3794 uint16_t lengthMs(0);
3795 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003796
3797 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3798 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3799 if (_playInbandDtmfEvent)
3800 {
3801 // Add tone to output mixer using a reduced length to minimize
3802 // risk of echo.
3803 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3804 attenuationDb);
3805 }
3806 }
3807
3808 if (_inbandDtmfGenerator.IsAddingTone())
3809 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003810 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003811 _inbandDtmfGenerator.GetSampleRate(frequency);
3812
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003813 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00003814 {
3815 // Update sample rate of Dtmf tone since the mixing frequency
3816 // has changed.
3817 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003818 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00003819 // Reset the tone to be added taking the new sample rate into
3820 // account.
3821 _inbandDtmfGenerator.ResetTone();
3822 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003823
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003824 int16_t toneBuffer[320];
3825 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003826 // Get 10ms tone segment and set time since last tone to zero
3827 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
3828 {
3829 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3830 VoEId(_instanceId, _channelId),
3831 "Channel::EncodeAndSend() inserting Dtmf failed");
3832 return -1;
3833 }
3834
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003835 // Replace mixed audio with DTMF tone.
Peter Kastingdce40cf2015-08-24 14:52:23 -07003836 for (size_t sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003837 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003838 sample++)
3839 {
Peter Kasting69558702016-01-12 16:26:35 -08003840 for (size_t channel = 0;
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003841 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003842 channel++)
3843 {
Peter Kastingdce40cf2015-08-24 14:52:23 -07003844 const size_t index =
3845 sample * _audioFrame.num_channels_ + channel;
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003846 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003847 }
3848 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003849
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003850 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003851 } else
3852 {
3853 // Add 10ms to "delay-since-last-tone" counter
3854 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
3855 }
3856 return 0;
3857}
3858
deadbeef74375882015-08-13 12:09:10 -07003859void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3860 uint32_t playout_timestamp = 0;
3861
3862 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
3863 // This can happen if this channel has not been received any RTP packet. In
3864 // this case, NetEq is not capable of computing playout timestamp.
3865 return;
3866 }
3867
3868 uint16_t delay_ms = 0;
3869 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3870 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3871 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3872 " delay from the ADM");
3873 _engineStatisticsPtr->SetLastError(
3874 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3875 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3876 return;
3877 }
3878
3879 jitter_buffer_playout_timestamp_ = playout_timestamp;
3880
3881 // Remove the playout delay.
3882 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
3883
3884 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3885 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3886 playout_timestamp);
3887
3888 {
tommi31fc21f2016-01-21 10:37:37 -08003889 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003890 if (rtcp) {
3891 playout_timestamp_rtcp_ = playout_timestamp;
3892 } else {
3893 playout_timestamp_rtp_ = playout_timestamp;
3894 }
3895 playout_delay_ms_ = delay_ms;
3896 }
3897}
3898
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003899// Called for incoming RTP packets after successful RTP header parsing.
3900void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
3901 uint16_t sequence_number) {
3902 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3903 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
3904 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00003905
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003906 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00003907 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00003908
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003909 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
3910 // every incoming packet.
3911 uint32_t timestamp_diff_ms = (rtp_timestamp -
3912 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00003913 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
3914 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
3915 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
3916 // timestamp, the resulting difference is negative, but is set to zero.
3917 // This can happen when a network glitch causes a packet to arrive late,
3918 // and during long comfort noise periods with clock drift.
3919 timestamp_diff_ms = 0;
3920 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003921
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003922 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
3923 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003924
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003925 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00003926
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003927 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00003928
deadbeef74375882015-08-13 12:09:10 -07003929 {
tommi31fc21f2016-01-21 10:37:37 -08003930 rtc::CritScope lock(&video_sync_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003931
deadbeef74375882015-08-13 12:09:10 -07003932 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
3933 _recPacketDelayMs = packet_delay_ms;
3934 }
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003935
deadbeef74375882015-08-13 12:09:10 -07003936 if (_average_jitter_buffer_delay_us == 0) {
3937 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
3938 return;
3939 }
3940
3941 // Filter average delay value using exponential filter (alpha is
3942 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
3943 // risk of rounding error) and compensate for it in GetDelayEstimate()
3944 // later.
3945 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
3946 1000 * timestamp_diff_ms + 500) / 8;
3947 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003948}
3949
3950void
3951Channel::RegisterReceiveCodecsToRTPModule()
3952{
3953 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3954 "Channel::RegisterReceiveCodecsToRTPModule()");
3955
niklase@google.com470e71d2011-07-07 08:21:25 +00003956 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003957 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003958
3959 for (int idx = 0; idx < nSupportedCodecs; idx++)
3960 {
3961 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003962 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003963 (rtp_receiver_->RegisterReceivePayload(
3964 codec.plname,
3965 codec.pltype,
3966 codec.plfreq,
3967 codec.channels,
3968 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00003969 {
Peter Boströmd5c75b12015-09-23 13:24:32 +02003970 WEBRTC_TRACE(kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00003971 kTraceVoice,
3972 VoEId(_instanceId, _channelId),
3973 "Channel::RegisterReceiveCodecsToRTPModule() unable"
Peter Kasting69558702016-01-12 16:26:35 -08003974 " to register %s (%d/%d/%" PRIuS "/%d) to RTP/RTCP "
3975 "receiver",
niklase@google.com470e71d2011-07-07 08:21:25 +00003976 codec.plname, codec.pltype, codec.plfreq,
3977 codec.channels, codec.rate);
3978 }
3979 else
3980 {
Peter Boströmd5c75b12015-09-23 13:24:32 +02003981 WEBRTC_TRACE(kTraceInfo,
niklase@google.com470e71d2011-07-07 08:21:25 +00003982 kTraceVoice,
3983 VoEId(_instanceId, _channelId),
3984 "Channel::RegisterReceiveCodecsToRTPModule() %s "
Peter Kasting69558702016-01-12 16:26:35 -08003985 "(%d/%d/%" PRIuS "/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003986 "receiver",
3987 codec.plname, codec.pltype, codec.plfreq,
3988 codec.channels, codec.rate);
3989 }
3990 }
3991}
3992
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003993// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003994int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003995 CodecInst codec;
3996 bool found_red = false;
3997
3998 // Get default RED settings from the ACM database
3999 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4000 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004001 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004002 if (!STR_CASE_CMP(codec.plname, "RED")) {
4003 found_red = true;
4004 break;
4005 }
4006 }
4007
4008 if (!found_red) {
4009 _engineStatisticsPtr->SetLastError(
4010 VE_CODEC_ERROR, kTraceError,
4011 "SetRedPayloadType() RED is not supported");
4012 return -1;
4013 }
4014
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004015 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004016 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004017 _engineStatisticsPtr->SetLastError(
4018 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4019 "SetRedPayloadType() RED registration in ACM module failed");
4020 return -1;
4021 }
4022
4023 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4024 _engineStatisticsPtr->SetLastError(
4025 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4026 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4027 return -1;
4028 }
4029 return 0;
4030}
4031
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004032int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4033 unsigned char id) {
4034 int error = 0;
4035 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4036 if (enable) {
4037 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4038 }
4039 return error;
4040}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004041
wu@webrtc.org94454b72014-06-05 20:34:08 +00004042int32_t Channel::GetPlayoutFrequency() {
4043 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4044 CodecInst current_recive_codec;
4045 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4046 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4047 // Even though the actual sampling rate for G.722 audio is
4048 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4049 // 8,000 Hz because that value was erroneously assigned in
4050 // RFC 1890 and must remain unchanged for backward compatibility.
4051 playout_frequency = 8000;
4052 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4053 // We are resampling Opus internally to 32,000 Hz until all our
4054 // DSP routines can operate at 48,000 Hz, but the RTP clock
4055 // rate for the Opus payload format is standardized to 48,000 Hz,
4056 // because that is the maximum supported decoding sampling rate.
4057 playout_frequency = 48000;
4058 }
4059 }
4060 return playout_frequency;
4061}
4062
Minyue2013aec2015-05-13 14:14:42 +02004063int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07004064 RtcpMode method = _rtpRtcpModule->RTCP();
4065 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004066 return 0;
4067 }
4068 std::vector<RTCPReportBlock> report_blocks;
4069 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02004070
4071 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004072 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02004073 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08004074 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02004075 Channel* channel = associate_send_channel_.channel();
4076 // Tries to get RTT from an associated channel. This is important for
4077 // receive-only channels.
4078 if (channel) {
4079 // To prevent infinite recursion and deadlock, calling GetRTT of
4080 // associate channel should always use "false" for argument:
4081 // |allow_associate_channel|.
4082 rtt = channel->GetRTT(false);
4083 }
4084 }
4085 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004086 }
4087
4088 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4089 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4090 for (; it != report_blocks.end(); ++it) {
4091 if (it->remoteSSRC == remoteSSRC)
4092 break;
4093 }
4094 if (it == report_blocks.end()) {
4095 // We have not received packets with SSRC matching the report blocks.
4096 // To calculate RTT we try with the SSRC of the first report block.
4097 // This is very important for send-only channels where we don't know
4098 // the SSRC of the other end.
4099 remoteSSRC = report_blocks[0].remoteSSRC;
4100 }
Minyue2013aec2015-05-13 14:14:42 +02004101
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004102 int64_t avg_rtt = 0;
4103 int64_t max_rtt= 0;
4104 int64_t min_rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004105 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4106 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004107 return 0;
4108 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004109 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004110}
4111
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004112} // namespace voe
4113} // namespace webrtc