blob: 2302acc0878c58b343b01628fbd140de7dcc8cf3 [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
aleloi6321b492016-12-05 01:46:09 -080016#include "webrtc/audio/utility/audio_frame_operations.h"
henrik.lundin50499422016-11-29 04:26:24 -080017#include "webrtc/base/array_view.h"
Ivo Creusenae856f22015-09-17 16:30:16 +020018#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080019#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000020#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080021#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020022#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010023#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000024#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020025#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070026#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070027#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000028#include "webrtc/modules/audio_device/include/audio_device.h"
29#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010031#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
33#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000035#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010036#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010037#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000038#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
39#include "webrtc/voice_engine/output_mixer.h"
40#include "webrtc/voice_engine/statistics.h"
41#include "webrtc/voice_engine/transmit_mixer.h"
42#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andrew@webrtc.org50419b02012-11-14 19:07:54 +000044namespace webrtc {
45namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
kwibergc8d071e2016-04-06 12:22:38 -070047namespace {
48
Erik Språng737336d2016-07-29 12:59:36 +020049constexpr int64_t kMaxRetransmissionWindowMs = 1000;
50constexpr int64_t kMinRetransmissionWindowMs = 30;
51
kwibergc8d071e2016-04-06 12:22:38 -070052} // namespace
53
solenberg8842c3e2016-03-11 03:06:41 -080054const int kTelephoneEventAttenuationdB = 10;
55
ivoc14d5dbe2016-07-04 07:06:55 -070056class RtcEventLogProxy final : public webrtc::RtcEventLog {
57 public:
58 RtcEventLogProxy() : event_log_(nullptr) {}
59
60 bool StartLogging(const std::string& file_name,
61 int64_t max_size_bytes) override {
62 RTC_NOTREACHED();
63 return false;
64 }
65
66 bool StartLogging(rtc::PlatformFile log_file,
67 int64_t max_size_bytes) override {
68 RTC_NOTREACHED();
69 return false;
70 }
71
72 void StopLogging() override { RTC_NOTREACHED(); }
73
74 void LogVideoReceiveStreamConfig(
75 const webrtc::VideoReceiveStream::Config& config) override {
76 rtc::CritScope lock(&crit_);
77 if (event_log_) {
78 event_log_->LogVideoReceiveStreamConfig(config);
79 }
80 }
81
82 void LogVideoSendStreamConfig(
83 const webrtc::VideoSendStream::Config& config) override {
84 rtc::CritScope lock(&crit_);
85 if (event_log_) {
86 event_log_->LogVideoSendStreamConfig(config);
87 }
88 }
89
ivoce0928d82016-10-10 05:12:51 -070090 void LogAudioReceiveStreamConfig(
91 const webrtc::AudioReceiveStream::Config& config) override {
92 rtc::CritScope lock(&crit_);
93 if (event_log_) {
94 event_log_->LogAudioReceiveStreamConfig(config);
95 }
96 }
97
98 void LogAudioSendStreamConfig(
99 const webrtc::AudioSendStream::Config& config) override {
100 rtc::CritScope lock(&crit_);
101 if (event_log_) {
102 event_log_->LogAudioSendStreamConfig(config);
103 }
104 }
105
ivoc14d5dbe2016-07-04 07:06:55 -0700106 void LogRtpHeader(webrtc::PacketDirection direction,
107 webrtc::MediaType media_type,
108 const uint8_t* header,
109 size_t packet_length) override {
110 rtc::CritScope lock(&crit_);
111 if (event_log_) {
112 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
113 }
114 }
115
116 void LogRtcpPacket(webrtc::PacketDirection direction,
117 webrtc::MediaType media_type,
118 const uint8_t* packet,
119 size_t length) override {
120 rtc::CritScope lock(&crit_);
121 if (event_log_) {
122 event_log_->LogRtcpPacket(direction, media_type, packet, length);
123 }
124 }
125
126 void LogAudioPlayout(uint32_t ssrc) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogAudioPlayout(ssrc);
130 }
131 }
132
133 void LogBwePacketLossEvent(int32_t bitrate,
134 uint8_t fraction_loss,
135 int32_t total_packets) override {
136 rtc::CritScope lock(&crit_);
137 if (event_log_) {
138 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
139 }
140 }
141
terelius0baf55d2017-02-17 03:38:28 -0800142 void LogBwePacketDelayEvent(int32_t bitrate,
143 BandwidthUsage detector_state) override {
144 rtc::CritScope lock(&crit_);
145 if (event_log_) {
146 event_log_->LogBwePacketDelayEvent(bitrate, detector_state);
147 }
148 }
149
minyue4b7c9522017-01-24 04:54:59 -0800150 void LogAudioNetworkAdaptation(
151 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) override {
152 rtc::CritScope lock(&crit_);
153 if (event_log_) {
154 event_log_->LogAudioNetworkAdaptation(config);
155 }
156 }
157
ivoc14d5dbe2016-07-04 07:06:55 -0700158 void SetEventLog(RtcEventLog* event_log) {
159 rtc::CritScope lock(&crit_);
160 event_log_ = event_log;
161 }
162
163 private:
164 rtc::CriticalSection crit_;
165 RtcEventLog* event_log_ GUARDED_BY(crit_);
166 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
167};
168
michaelt9332b7d2016-11-30 07:51:13 -0800169class RtcpRttStatsProxy final : public RtcpRttStats {
170 public:
171 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
172
173 void OnRttUpdate(int64_t rtt) override {
174 rtc::CritScope lock(&crit_);
175 if (rtcp_rtt_stats_)
176 rtcp_rtt_stats_->OnRttUpdate(rtt);
177 }
178
179 int64_t LastProcessedRtt() const override {
180 rtc::CritScope lock(&crit_);
181 if (!rtcp_rtt_stats_)
182 return 0;
183 return rtcp_rtt_stats_->LastProcessedRtt();
184 }
185
186 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
187 rtc::CritScope lock(&crit_);
188 rtcp_rtt_stats_ = rtcp_rtt_stats;
189 }
190
191 private:
192 rtc::CriticalSection crit_;
193 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
194 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
195};
196
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100197class TransportFeedbackProxy : public TransportFeedbackObserver {
198 public:
199 TransportFeedbackProxy() : feedback_observer_(nullptr) {
200 pacer_thread_.DetachFromThread();
201 network_thread_.DetachFromThread();
202 }
203
204 void SetTransportFeedbackObserver(
205 TransportFeedbackObserver* feedback_observer) {
206 RTC_DCHECK(thread_checker_.CalledOnValidThread());
207 rtc::CritScope lock(&crit_);
208 feedback_observer_ = feedback_observer;
209 }
210
211 // Implements TransportFeedbackObserver.
212 void AddPacket(uint16_t sequence_number,
213 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700214 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100215 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
216 rtc::CritScope lock(&crit_);
217 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700218 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100219 }
220 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
221 RTC_DCHECK(network_thread_.CalledOnValidThread());
222 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700223 if (feedback_observer_)
224 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200225 }
226 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
227 RTC_NOTREACHED();
228 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100229 }
230
231 private:
232 rtc::CriticalSection crit_;
233 rtc::ThreadChecker thread_checker_;
234 rtc::ThreadChecker pacer_thread_;
235 rtc::ThreadChecker network_thread_;
236 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
237};
238
239class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
240 public:
241 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
242 pacer_thread_.DetachFromThread();
243 }
244
245 void SetSequenceNumberAllocator(
246 TransportSequenceNumberAllocator* seq_num_allocator) {
247 RTC_DCHECK(thread_checker_.CalledOnValidThread());
248 rtc::CritScope lock(&crit_);
249 seq_num_allocator_ = seq_num_allocator;
250 }
251
252 // Implements TransportSequenceNumberAllocator.
253 uint16_t AllocateSequenceNumber() override {
254 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
255 rtc::CritScope lock(&crit_);
256 if (!seq_num_allocator_)
257 return 0;
258 return seq_num_allocator_->AllocateSequenceNumber();
259 }
260
261 private:
262 rtc::CriticalSection crit_;
263 rtc::ThreadChecker thread_checker_;
264 rtc::ThreadChecker pacer_thread_;
265 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
266};
267
268class RtpPacketSenderProxy : public RtpPacketSender {
269 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800270 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100271
272 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
273 RTC_DCHECK(thread_checker_.CalledOnValidThread());
274 rtc::CritScope lock(&crit_);
275 rtp_packet_sender_ = rtp_packet_sender;
276 }
277
278 // Implements RtpPacketSender.
279 void InsertPacket(Priority priority,
280 uint32_t ssrc,
281 uint16_t sequence_number,
282 int64_t capture_time_ms,
283 size_t bytes,
284 bool retransmission) override {
285 rtc::CritScope lock(&crit_);
286 if (rtp_packet_sender_) {
287 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
288 capture_time_ms, bytes, retransmission);
289 }
290 }
291
292 private:
293 rtc::ThreadChecker thread_checker_;
294 rtc::CriticalSection crit_;
295 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
296};
297
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000298class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000299 public:
stefan7de8d642017-02-07 07:14:08 -0800300 explicit VoERtcpObserver(Channel* owner)
301 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000302 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000303
stefan7de8d642017-02-07 07:14:08 -0800304 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
305 rtc::CritScope lock(&crit_);
306 bandwidth_observer_ = bandwidth_observer;
307 }
308
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000309 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800310 rtc::CritScope lock(&crit_);
311 if (bandwidth_observer_) {
312 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
313 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000314 }
315
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000316 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
317 int64_t rtt,
318 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800319 {
320 rtc::CritScope lock(&crit_);
321 if (bandwidth_observer_) {
322 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
323 now_ms);
324 }
325 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000326 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
327 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
328 // report for VoiceEngine?
329 if (report_blocks.empty())
330 return;
331
332 int fraction_lost_aggregate = 0;
333 int total_number_of_packets = 0;
334
335 // If receiving multiple report blocks, calculate the weighted average based
336 // on the number of packets a report refers to.
337 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
338 block_it != report_blocks.end(); ++block_it) {
339 // Find the previous extended high sequence number for this remote SSRC,
340 // to calculate the number of RTP packets this report refers to. Ignore if
341 // we haven't seen this SSRC before.
342 std::map<uint32_t, uint32_t>::iterator seq_num_it =
343 extended_max_sequence_number_.find(block_it->sourceSSRC);
344 int number_of_packets = 0;
345 if (seq_num_it != extended_max_sequence_number_.end()) {
346 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
347 }
348 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
349 total_number_of_packets += number_of_packets;
350
351 extended_max_sequence_number_[block_it->sourceSSRC] =
352 block_it->extendedHighSeqNum;
353 }
354 int weighted_fraction_lost = 0;
355 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800356 weighted_fraction_lost =
357 (fraction_lost_aggregate + total_number_of_packets / 2) /
358 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000359 }
360 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000361 }
362
363 private:
364 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000365 // Maps remote side ssrc to extended highest sequence number received.
366 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800367 rtc::CriticalSection crit_;
368 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000369};
370
kwiberg55b97fe2016-01-28 05:22:45 -0800371int32_t Channel::SendData(FrameType frameType,
372 uint8_t payloadType,
373 uint32_t timeStamp,
374 const uint8_t* payloadData,
375 size_t payloadSize,
376 const RTPFragmentationHeader* fragmentation) {
377 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
378 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
379 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
380 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000381
kwiberg55b97fe2016-01-28 05:22:45 -0800382 if (_includeAudioLevelIndication) {
383 // Store current audio level in the RTP/RTCP module.
384 // The level will be used in combination with voice-activity state
385 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800386 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800387 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000388
kwiberg55b97fe2016-01-28 05:22:45 -0800389 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
390 // packetization.
391 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700392 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800393 (FrameType&)frameType, payloadType, timeStamp,
394 // Leaving the time when this frame was
395 // received from the capture device as
396 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700397 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800398 _engineStatisticsPtr->SetLastError(
399 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
400 "Channel::SendData() failed to send data to RTP/RTCP module");
401 return -1;
402 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
kwiberg55b97fe2016-01-28 05:22:45 -0800404 _lastLocalTimeStamp = timeStamp;
405 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
kwiberg55b97fe2016-01-28 05:22:45 -0800407 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000408}
409
kwiberg55b97fe2016-01-28 05:22:45 -0800410int32_t Channel::InFrameType(FrameType frame_type) {
411 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
412 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000413
kwiberg55b97fe2016-01-28 05:22:45 -0800414 rtc::CritScope cs(&_callbackCritSect);
415 _sendFrameType = (frame_type == kAudioFrameSpeech);
416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417}
418
stefan1d8a5062015-10-02 03:39:33 -0700419bool Channel::SendRtp(const uint8_t* data,
420 size_t len,
421 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800422 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
423 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000424
kwiberg55b97fe2016-01-28 05:22:45 -0800425 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000426
kwiberg55b97fe2016-01-28 05:22:45 -0800427 if (_transportPtr == NULL) {
428 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
429 "Channel::SendPacket() failed to send RTP packet due to"
430 " invalid transport object");
431 return false;
432 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000433
kwiberg55b97fe2016-01-28 05:22:45 -0800434 uint8_t* bufferToSendPtr = (uint8_t*)data;
435 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000436
kwiberg55b97fe2016-01-28 05:22:45 -0800437 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
438 std::string transport_name =
439 _externalTransport ? "external transport" : "WebRtc sockets";
440 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
441 "Channel::SendPacket() RTP transmission using %s failed",
442 transport_name.c_str());
443 return false;
444 }
445 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000446}
447
kwiberg55b97fe2016-01-28 05:22:45 -0800448bool Channel::SendRtcp(const uint8_t* data, size_t len) {
449 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
450 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000451
kwiberg55b97fe2016-01-28 05:22:45 -0800452 rtc::CritScope cs(&_callbackCritSect);
453 if (_transportPtr == NULL) {
454 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
455 "Channel::SendRtcp() failed to send RTCP packet"
456 " due to invalid transport object");
457 return false;
458 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
kwiberg55b97fe2016-01-28 05:22:45 -0800460 uint8_t* bufferToSendPtr = (uint8_t*)data;
461 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000462
kwiberg55b97fe2016-01-28 05:22:45 -0800463 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
464 if (n < 0) {
465 std::string transport_name =
466 _externalTransport ? "external transport" : "WebRtc sockets";
467 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
468 "Channel::SendRtcp() transmission using %s failed",
469 transport_name.c_str());
470 return false;
471 }
472 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
kwiberg55b97fe2016-01-28 05:22:45 -0800475void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
476 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
477 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
kwiberg55b97fe2016-01-28 05:22:45 -0800479 // Update ssrc so that NTP for AV sync can be updated.
480 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000481}
482
Peter Boströmac547a62015-09-17 23:03:57 +0200483void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
484 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
485 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
486 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000487}
488
Peter Boströmac547a62015-09-17 23:03:57 +0200489int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000490 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000491 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000492 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800493 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200494 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800495 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
496 "Channel::OnInitializeDecoder(payloadType=%d, "
497 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
498 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000499
kwiberg55b97fe2016-01-28 05:22:45 -0800500 CodecInst receiveCodec = {0};
501 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000502
kwiberg55b97fe2016-01-28 05:22:45 -0800503 receiveCodec.pltype = payloadType;
504 receiveCodec.plfreq = frequency;
505 receiveCodec.channels = channels;
506 receiveCodec.rate = rate;
507 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000508
kwiberg55b97fe2016-01-28 05:22:45 -0800509 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
510 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000511
kwiberg55b97fe2016-01-28 05:22:45 -0800512 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700513 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
514 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800515 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
516 "Channel::OnInitializeDecoder() invalid codec ("
517 "pt=%d, name=%s) received - 1",
518 payloadType, payloadName);
519 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
520 return -1;
521 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000522
kwiberg55b97fe2016-01-28 05:22:45 -0800523 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000524}
525
kwiberg55b97fe2016-01-28 05:22:45 -0800526int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
527 size_t payloadSize,
528 const WebRtcRTPHeader* rtpHeader) {
529 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
530 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
531 ","
532 " payloadType=%u, audioChannel=%" PRIuS ")",
533 payloadSize, rtpHeader->header.payloadType,
534 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000535
kwiberg55b97fe2016-01-28 05:22:45 -0800536 if (!channel_state_.Get().playing) {
537 // Avoid inserting into NetEQ when we are not playing. Count the
538 // packet as discarded.
539 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
540 "received packet is discarded since playing is not"
541 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000542 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800543 }
544
545 // Push the incoming payload (parsed and ready for decoding) into the ACM
546 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
547 0) {
548 _engineStatisticsPtr->SetLastError(
549 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
550 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
551 return -1;
552 }
553
kwiberg55b97fe2016-01-28 05:22:45 -0800554 int64_t round_trip_time = 0;
555 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
556 NULL);
557
558 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
559 if (!nack_list.empty()) {
560 // Can't use nack_list.data() since it's not supported by all
561 // compilers.
562 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
563 }
564 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000565}
566
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000567bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000568 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000569 RTPHeader header;
570 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
571 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
572 "IncomingPacket invalid RTP header");
573 return false;
574 }
575 header.payload_type_frequency =
576 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
577 if (header.payload_type_frequency < 0)
578 return false;
579 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
580}
581
henrik.lundin42dda502016-05-18 05:36:01 -0700582MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
583 int32_t id,
584 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700585 unsigned int ssrc;
nisseb78d4d12017-02-17 08:34:35 -0800586 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700587 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800588 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700589 bool muted;
590 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
591 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800592 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
593 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
594 // In all likelihood, the audio in this frame is garbage. We return an
595 // error so that the audio mixer module doesn't add it to the mix. As
596 // a result, it won't be played out and the actions skipped here are
597 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700598 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800599 }
henrik.lundina89ab962016-05-18 08:52:45 -0700600
601 if (muted) {
602 // TODO(henrik.lundin): We should be able to do better than this. But we
603 // will have to go through all the cases below where the audio samples may
604 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800605 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700606 }
kwiberg55b97fe2016-01-28 05:22:45 -0800607
kwiberg55b97fe2016-01-28 05:22:45 -0800608 // Convert module ID to internal VoE channel ID
609 audioFrame->id_ = VoEChannelId(audioFrame->id_);
610 // Store speech type for dead-or-alive detection
611 _outputSpeechType = audioFrame->speech_type_;
612
613 ChannelState::State state = channel_state_.Get();
614
kwiberg55b97fe2016-01-28 05:22:45 -0800615 {
616 // Pass the audio buffers to an optional sink callback, before applying
617 // scaling/panning, as that applies to the mix operation.
618 // External recipients of the audio (e.g. via AudioTrack), will do their
619 // own mixing/dynamic processing.
620 rtc::CritScope cs(&_callbackCritSect);
621 if (audio_sink_) {
622 AudioSinkInterface::Data data(
623 &audioFrame->data_[0], audioFrame->samples_per_channel_,
624 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
625 audioFrame->timestamp_);
626 audio_sink_->OnData(data);
627 }
628 }
629
630 float output_gain = 1.0f;
631 float left_pan = 1.0f;
632 float right_pan = 1.0f;
633 {
634 rtc::CritScope cs(&volume_settings_critsect_);
635 output_gain = _outputGain;
636 left_pan = _panLeft;
637 right_pan = _panRight;
638 }
639
640 // Output volume scaling
641 if (output_gain < 0.99f || output_gain > 1.01f) {
642 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
643 }
644
645 // Scale left and/or right channel(s) if stereo and master balance is
646 // active
647
648 if (left_pan != 1.0f || right_pan != 1.0f) {
649 if (audioFrame->num_channels_ == 1) {
650 // Emulate stereo mode since panning is active.
651 // The mono signal is copied to both left and right channels here.
652 AudioFrameOperations::MonoToStereo(audioFrame);
653 }
654 // For true stereo mode (when we are receiving a stereo signal), no
655 // action is needed.
656
657 // Do the panning operation (the audio frame contains stereo at this
658 // stage)
659 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
660 }
661
662 // Mix decoded PCM output with file if file mixing is enabled
663 if (state.output_file_playing) {
664 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700665 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800666 }
667
kwiberg55b97fe2016-01-28 05:22:45 -0800668 // Record playout if enabled
669 {
670 rtc::CritScope cs(&_fileCritSect);
671
kwiberg5a25d952016-08-17 07:31:12 -0700672 if (_outputFileRecording && output_file_recorder_) {
673 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800674 }
675 }
676
677 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700678 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800679 _outputAudioLevel.ComputeLevel(*audioFrame);
680
681 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
682 // The first frame with a valid rtp timestamp.
683 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
684 }
685
686 if (capture_start_rtp_time_stamp_ >= 0) {
687 // audioFrame.timestamp_ should be valid from now on.
688
689 // Compute elapsed time.
690 int64_t unwrap_timestamp =
691 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
692 audioFrame->elapsed_time_ms_ =
693 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700694 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800695
niklase@google.com470e71d2011-07-07 08:21:25 +0000696 {
kwiberg55b97fe2016-01-28 05:22:45 -0800697 rtc::CritScope lock(&ts_stats_lock_);
698 // Compute ntp time.
699 audioFrame->ntp_time_ms_ =
700 ntp_estimator_.Estimate(audioFrame->timestamp_);
701 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
702 if (audioFrame->ntp_time_ms_ > 0) {
703 // Compute |capture_start_ntp_time_ms_| so that
704 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
705 capture_start_ntp_time_ms_ =
706 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000707 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000708 }
kwiberg55b97fe2016-01-28 05:22:45 -0800709 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000710
henrik.lundin42dda502016-05-18 05:36:01 -0700711 return muted ? MixerParticipant::AudioFrameInfo::kMuted
712 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000713}
714
aleloi6c278492016-10-20 14:24:39 -0700715AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
716 int sample_rate_hz,
717 AudioFrame* audio_frame) {
718 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700719
aleloi6c278492016-10-20 14:24:39 -0700720 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700721
722 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
723 FrameInfo new_audio_frame_info = FrameInfo::kError;
724 switch (frame_info) {
725 case MixerParticipant::AudioFrameInfo::kNormal:
726 new_audio_frame_info = FrameInfo::kNormal;
727 break;
728 case MixerParticipant::AudioFrameInfo::kMuted:
729 new_audio_frame_info = FrameInfo::kMuted;
730 break;
731 case MixerParticipant::AudioFrameInfo::kError:
732 new_audio_frame_info = FrameInfo::kError;
733 break;
734 }
aleloi6c278492016-10-20 14:24:39 -0700735 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700736}
737
kwiberg55b97fe2016-01-28 05:22:45 -0800738int32_t Channel::NeededFrequency(int32_t id) const {
739 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
740 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000741
kwiberg55b97fe2016-01-28 05:22:45 -0800742 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000743
kwiberg55b97fe2016-01-28 05:22:45 -0800744 // Determine highest needed receive frequency
745 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000746
kwiberg55b97fe2016-01-28 05:22:45 -0800747 // Return the bigger of playout and receive frequency in the ACM.
748 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
749 highestNeeded = audio_coding_->PlayoutFrequency();
750 } else {
751 highestNeeded = receiveFrequency;
752 }
753
754 // Special case, if we're playing a file on the playout side
755 // we take that frequency into consideration as well
756 // This is not needed on sending side, since the codec will
757 // limit the spectrum anyway.
758 if (channel_state_.Get().output_file_playing) {
759 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700760 if (output_file_player_) {
761 if (output_file_player_->Frequency() > highestNeeded) {
762 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800763 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000764 }
kwiberg55b97fe2016-01-28 05:22:45 -0800765 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
kwiberg55b97fe2016-01-28 05:22:45 -0800767 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000768}
769
ossu5f7cfa52016-05-30 08:11:28 -0700770int32_t Channel::CreateChannel(
771 Channel*& channel,
772 int32_t channelId,
773 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700774 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800775 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
776 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
777 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000778
solenberg88499ec2016-09-07 07:34:41 -0700779 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800780 if (channel == NULL) {
781 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
782 "Channel::CreateChannel() unable to allocate memory for"
783 " channel");
784 return -1;
785 }
786 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000787}
788
kwiberg55b97fe2016-01-28 05:22:45 -0800789void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
790 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
791 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
792 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000793
kwiberg55b97fe2016-01-28 05:22:45 -0800794 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000795}
796
kwiberg55b97fe2016-01-28 05:22:45 -0800797void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
798 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
799 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
800 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801
kwiberg55b97fe2016-01-28 05:22:45 -0800802 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000803}
804
kwiberg55b97fe2016-01-28 05:22:45 -0800805void Channel::PlayFileEnded(int32_t id) {
806 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
807 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000808
kwiberg55b97fe2016-01-28 05:22:45 -0800809 if (id == _inputFilePlayerId) {
810 channel_state_.SetInputFilePlaying(false);
811 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
812 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000813 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800814 } else if (id == _outputFilePlayerId) {
815 channel_state_.SetOutputFilePlaying(false);
816 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
817 "Channel::PlayFileEnded() => output file player module is"
818 " shutdown");
819 }
820}
821
822void Channel::RecordFileEnded(int32_t id) {
823 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
824 "Channel::RecordFileEnded(id=%d)", id);
825
826 assert(id == _outputFileRecorderId);
827
828 rtc::CritScope cs(&_fileCritSect);
829
830 _outputFileRecording = false;
831 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
832 "Channel::RecordFileEnded() => output file recorder module is"
833 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000834}
835
pbos@webrtc.org92135212013-05-14 08:31:39 +0000836Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000837 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700838 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800839 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100840 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700841 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800842 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100843 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800844 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100845 rtp_receive_statistics_(
846 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
847 rtp_receiver_(
848 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100849 this,
850 this,
851 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700852 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100853 _outputAudioLevel(),
854 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100855 // Avoid conflict with other channels by adding 1024 - 1026,
856 // won't use as much as 1024 channels.
857 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
858 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
859 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
860 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100861 _timeStamp(0), // This is just an offset, RTP module will add it's own
862 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100863 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100864 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100867 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
868 capture_start_rtp_time_stamp_(-1),
869 capture_start_ntp_time_ms_(-1),
870 _engineStatisticsPtr(NULL),
871 _outputMixerPtr(NULL),
872 _transmitMixerPtr(NULL),
873 _moduleProcessThreadPtr(NULL),
874 _audioDeviceModulePtr(NULL),
875 _voiceEngineObserverPtr(NULL),
876 _callbackCritSectPtr(NULL),
877 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100878 _sendFrameType(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100879 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700880 input_mute_(false),
881 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 _panLeft(1.0f),
883 _panRight(1.0f),
884 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 _lastLocalTimeStamp(0),
886 _lastPayloadType(0),
887 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800888 transport_overhead_per_packet_(0),
889 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100890 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 restored_packet_in_use_(false),
892 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100893 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700894 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800895 feedback_observer_proxy_(new TransportFeedbackProxy()),
896 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700897 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200898 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
899 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800900 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800901 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
902 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700903 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800904 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700905 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800906 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200907
kwiberg55b97fe2016-01-28 05:22:45 -0800908 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000909
kwiberg55b97fe2016-01-28 05:22:45 -0800910 RtpRtcp::Configuration configuration;
911 configuration.audio = true;
912 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800913 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800914 configuration.receive_statistics = rtp_receive_statistics_.get();
915 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800916 if (pacing_enabled_) {
917 configuration.paced_sender = rtp_packet_sender_proxy_.get();
918 configuration.transport_sequence_number_allocator =
919 seq_num_allocator_proxy_.get();
920 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
921 }
ivoc14d5dbe2016-07-04 07:06:55 -0700922 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800923 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200924 configuration.retransmission_rate_limiter =
925 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000926
kwiberg55b97fe2016-01-28 05:22:45 -0800927 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100928 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000929}
930
kwiberg55b97fe2016-01-28 05:22:45 -0800931Channel::~Channel() {
932 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
933 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
934 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000935
kwiberg55b97fe2016-01-28 05:22:45 -0800936 StopSend();
937 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000938
kwiberg55b97fe2016-01-28 05:22:45 -0800939 {
940 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700941 if (input_file_player_) {
942 input_file_player_->RegisterModuleFileCallback(NULL);
943 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000944 }
kwiberg5a25d952016-08-17 07:31:12 -0700945 if (output_file_player_) {
946 output_file_player_->RegisterModuleFileCallback(NULL);
947 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800948 }
kwiberg5a25d952016-08-17 07:31:12 -0700949 if (output_file_recorder_) {
950 output_file_recorder_->RegisterModuleFileCallback(NULL);
951 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800952 }
953 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000954
kwiberg55b97fe2016-01-28 05:22:45 -0800955 // The order to safely shutdown modules in a channel is:
956 // 1. De-register callbacks in modules
957 // 2. De-register modules in process thread
958 // 3. Destroy modules
959 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
960 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
961 "~Channel() failed to de-register transport callback"
962 " (Audio coding module)");
963 }
964 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
965 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
966 "~Channel() failed to de-register VAD callback"
967 " (Audio coding module)");
968 }
969 // De-register modules in process thread
970 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000971
kwiberg55b97fe2016-01-28 05:22:45 -0800972 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000973}
974
kwiberg55b97fe2016-01-28 05:22:45 -0800975int32_t Channel::Init() {
976 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
977 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000978
kwiberg55b97fe2016-01-28 05:22:45 -0800979 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000980
kwiberg55b97fe2016-01-28 05:22:45 -0800981 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000982
kwiberg55b97fe2016-01-28 05:22:45 -0800983 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
984 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
985 "Channel::Init() must call SetEngineInformation() first");
986 return -1;
987 }
988
989 // --- Add modules to process thread (for periodic schedulation)
990
991 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
992
993 // --- ACM initialization
994
995 if (audio_coding_->InitializeReceiver() == -1) {
996 _engineStatisticsPtr->SetLastError(
997 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
998 "Channel::Init() unable to initialize the ACM - 1");
999 return -1;
1000 }
1001
1002 // --- RTP/RTCP module initialization
1003
1004 // Ensure that RTCP is enabled by default for the created channel.
1005 // Note that, the module will keep generating RTCP until it is explicitly
1006 // disabled by the user.
1007 // After StopListen (when no sockets exists), RTCP packets will no longer
1008 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001009 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001010 // RTCP is enabled by default.
1011 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1012 // --- Register all permanent callbacks
1013 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1014 (audio_coding_->RegisterVADCallback(this) == -1);
1015
1016 if (fail) {
1017 _engineStatisticsPtr->SetLastError(
1018 VE_CANNOT_INIT_CHANNEL, kTraceError,
1019 "Channel::Init() callbacks not registered");
1020 return -1;
1021 }
1022
1023 // --- Register all supported codecs to the receiving side of the
1024 // RTP/RTCP module
1025
1026 CodecInst codec;
1027 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1028
1029 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1030 // Open up the RTP/RTCP receiver for all supported codecs
1031 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001032 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001033 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1034 "Channel::Init() unable to register %s "
1035 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1036 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1037 codec.rate);
1038 } else {
1039 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1040 "Channel::Init() %s (%d/%d/%" PRIuS
1041 "/%d) has been "
1042 "added to the RTP/RTCP receiver",
1043 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1044 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001045 }
1046
kwiberg55b97fe2016-01-28 05:22:45 -08001047 // Ensure that PCMU is used as default codec on the sending side
1048 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1049 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001050 }
1051
kwiberg55b97fe2016-01-28 05:22:45 -08001052 // Register default PT for outband 'telephone-event'
1053 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001054 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001055 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1056 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001057 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1058 "Channel::Init() failed to register outband "
1059 "'telephone-event' (%d/%d) correctly",
1060 codec.pltype, codec.plfreq);
1061 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001062 }
1063
kwiberg55b97fe2016-01-28 05:22:45 -08001064 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001065 if (!codec_manager_.RegisterEncoder(codec) ||
1066 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001067 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1068 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001069 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001070 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1071 "Channel::Init() failed to register CN (%d/%d) "
1072 "correctly - 1",
1073 codec.pltype, codec.plfreq);
1074 }
1075 }
kwiberg55b97fe2016-01-28 05:22:45 -08001076 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001077
kwiberg55b97fe2016-01-28 05:22:45 -08001078 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001079}
1080
kwiberg55b97fe2016-01-28 05:22:45 -08001081int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1082 OutputMixer& outputMixer,
1083 voe::TransmitMixer& transmitMixer,
1084 ProcessThread& moduleProcessThread,
1085 AudioDeviceModule& audioDeviceModule,
1086 VoiceEngineObserver* voiceEngineObserver,
1087 rtc::CriticalSection* callbackCritSect) {
1088 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1089 "Channel::SetEngineInformation()");
1090 _engineStatisticsPtr = &engineStatistics;
1091 _outputMixerPtr = &outputMixer;
1092 _transmitMixerPtr = &transmitMixer,
1093 _moduleProcessThreadPtr = &moduleProcessThread;
1094 _audioDeviceModulePtr = &audioDeviceModule;
1095 _voiceEngineObserverPtr = voiceEngineObserver;
1096 _callbackCritSectPtr = callbackCritSect;
1097 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001098}
1099
kwiberg55b97fe2016-01-28 05:22:45 -08001100int32_t Channel::UpdateLocalTimeStamp() {
1101 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1102 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001103}
1104
kwibergb7f89d62016-02-17 10:04:18 -08001105void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001106 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001107 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001108}
1109
ossu29b1a8d2016-06-13 07:34:51 -07001110const rtc::scoped_refptr<AudioDecoderFactory>&
1111Channel::GetAudioDecoderFactory() const {
1112 return decoder_factory_;
1113}
1114
kwiberg55b97fe2016-01-28 05:22:45 -08001115int32_t Channel::StartPlayout() {
1116 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1117 "Channel::StartPlayout()");
1118 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001119 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001120 }
1121
solenberge374e012017-02-14 04:55:00 -08001122 // Add participant as candidates for mixing.
1123 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1124 _engineStatisticsPtr->SetLastError(
1125 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1126 "StartPlayout() failed to add participant to mixer");
1127 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001128 }
1129
1130 channel_state_.SetPlaying(true);
1131 if (RegisterFilePlayingToMixer() != 0)
1132 return -1;
1133
1134 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001135}
1136
kwiberg55b97fe2016-01-28 05:22:45 -08001137int32_t Channel::StopPlayout() {
1138 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1139 "Channel::StopPlayout()");
1140 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001141 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001142 }
1143
solenberge374e012017-02-14 04:55:00 -08001144 // Remove participant as candidates for mixing
1145 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1146 _engineStatisticsPtr->SetLastError(
1147 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1148 "StopPlayout() failed to remove participant from mixer");
1149 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001150 }
1151
1152 channel_state_.SetPlaying(false);
1153 _outputAudioLevel.Clear();
1154
1155 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001156}
1157
kwiberg55b97fe2016-01-28 05:22:45 -08001158int32_t Channel::StartSend() {
1159 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1160 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001161 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001162 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001163 }
1164 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001165
solenberg08b19df2017-02-15 00:42:31 -08001166 // Resume the previous sequence number which was reset by StopSend(). This
1167 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1168 if (send_sequence_number_) {
1169 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1170 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001171 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001172 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1173 _engineStatisticsPtr->SetLastError(
1174 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1175 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001176 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001177 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001178 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001179 return -1;
1180 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001181
kwiberg55b97fe2016-01-28 05:22:45 -08001182 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001183}
1184
kwiberg55b97fe2016-01-28 05:22:45 -08001185int32_t Channel::StopSend() {
1186 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1187 "Channel::StopSend()");
1188 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001190 }
1191 channel_state_.SetSending(false);
1192
1193 // Store the sequence number to be able to pick up the same sequence for
1194 // the next StartSend(). This is needed for restarting device, otherwise
1195 // it might cause libSRTP to complain about packets being replayed.
1196 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1197 // CL is landed. See issue
1198 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1199 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1200
1201 // Reset sending SSRC and sequence number and triggers direct transmission
1202 // of RTCP BYE
1203 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1204 _engineStatisticsPtr->SetLastError(
1205 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1206 "StartSend() RTP/RTCP failed to stop sending");
1207 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001208 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001209
1210 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001211}
1212
kwiberg55b97fe2016-01-28 05:22:45 -08001213int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1214 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1215 "Channel::RegisterVoiceEngineObserver()");
1216 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001217
kwiberg55b97fe2016-01-28 05:22:45 -08001218 if (_voiceEngineObserverPtr) {
1219 _engineStatisticsPtr->SetLastError(
1220 VE_INVALID_OPERATION, kTraceError,
1221 "RegisterVoiceEngineObserver() observer already enabled");
1222 return -1;
1223 }
1224 _voiceEngineObserverPtr = &observer;
1225 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001226}
1227
kwiberg55b97fe2016-01-28 05:22:45 -08001228int32_t Channel::DeRegisterVoiceEngineObserver() {
1229 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1230 "Channel::DeRegisterVoiceEngineObserver()");
1231 rtc::CritScope cs(&_callbackCritSect);
1232
1233 if (!_voiceEngineObserverPtr) {
1234 _engineStatisticsPtr->SetLastError(
1235 VE_INVALID_OPERATION, kTraceWarning,
1236 "DeRegisterVoiceEngineObserver() observer already disabled");
1237 return 0;
1238 }
1239 _voiceEngineObserverPtr = NULL;
1240 return 0;
1241}
1242
1243int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001244 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001245 if (send_codec) {
1246 codec = *send_codec;
1247 return 0;
1248 }
1249 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001250}
1251
kwiberg55b97fe2016-01-28 05:22:45 -08001252int32_t Channel::GetRecCodec(CodecInst& codec) {
1253 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001254}
1255
kwiberg55b97fe2016-01-28 05:22:45 -08001256int32_t Channel::SetSendCodec(const CodecInst& codec) {
1257 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1258 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001259
kwibergc8d071e2016-04-06 12:22:38 -07001260 if (!codec_manager_.RegisterEncoder(codec) ||
1261 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001262 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1263 "SetSendCodec() failed to register codec to ACM");
1264 return -1;
1265 }
1266
1267 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1268 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1269 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1270 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1271 "SetSendCodec() failed to register codec to"
1272 " RTP/RTCP module");
1273 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001274 }
kwiberg55b97fe2016-01-28 05:22:45 -08001275 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001276
kwiberg55b97fe2016-01-28 05:22:45 -08001277 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001278}
1279
minyue78b4d562016-11-30 04:47:39 -08001280void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001281 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1282 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001283 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001284 if (*encoder) {
1285 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001286 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001287 }
1288 });
michaelt566d8202017-01-12 10:17:38 -08001289 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001290}
1291
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001292void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001293 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1294 if (*encoder)
1295 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1296 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001297}
1298
kwiberg55b97fe2016-01-28 05:22:45 -08001299int32_t Channel::SetVADStatus(bool enableVAD,
1300 ACMVADMode mode,
1301 bool disableDTX) {
1302 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1303 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001304 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1305 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1306 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001307 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1308 kTraceError,
1309 "SetVADStatus() failed to set VAD");
1310 return -1;
1311 }
1312 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001313}
1314
kwiberg55b97fe2016-01-28 05:22:45 -08001315int32_t Channel::GetVADStatus(bool& enabledVAD,
1316 ACMVADMode& mode,
1317 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001318 const auto* params = codec_manager_.GetStackParams();
1319 enabledVAD = params->use_cng;
1320 mode = params->vad_mode;
1321 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001322 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001323}
1324
kwiberg55b97fe2016-01-28 05:22:45 -08001325int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001326 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1327}
1328
1329int32_t Channel::SetRecPayloadType(int payload_type,
1330 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001331 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1332 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001333
kwiberg55b97fe2016-01-28 05:22:45 -08001334 if (channel_state_.Get().playing) {
1335 _engineStatisticsPtr->SetLastError(
1336 VE_ALREADY_PLAYING, kTraceError,
1337 "SetRecPayloadType() unable to set PT while playing");
1338 return -1;
1339 }
kwiberg55b97fe2016-01-28 05:22:45 -08001340
kwibergd32bf752017-01-19 07:03:59 -08001341 const CodecInst codec = [&] {
1342 CodecInst c = SdpToCodecInst(payload_type, format);
1343
1344 // Bug 6986: Emulate an old bug that caused us to always choose to decode
1345 // Opus in stereo. To be able to remove this, we first need to fix the
1346 // other half of bug 6986, which is about losing the Opus "stereo"
1347 // parameter.
1348 // TODO(kwiberg): Remove this special case, a.k.a. fix bug 6986.
1349 if (STR_CASE_CMP(codec.plname, "opus") == 0) {
1350 c.channels = 2;
1351 }
1352
1353 return c;
1354 }();
1355
1356 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001357 // De-register the selected codec (RTP/RTCP module and ACM)
1358
1359 int8_t pltype(-1);
1360 CodecInst rxCodec = codec;
1361
1362 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001363 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001364 rxCodec.pltype = pltype;
1365
1366 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1367 _engineStatisticsPtr->SetLastError(
1368 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1369 "SetRecPayloadType() RTP/RTCP-module deregistration "
1370 "failed");
1371 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001372 }
kwiberg55b97fe2016-01-28 05:22:45 -08001373 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1374 _engineStatisticsPtr->SetLastError(
1375 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1376 "SetRecPayloadType() ACM deregistration failed - 1");
1377 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001378 }
kwiberg55b97fe2016-01-28 05:22:45 -08001379 return 0;
1380 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001381
magjed56124bd2016-11-24 09:34:46 -08001382 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001383 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001384 // TODO(kwiberg): Retrying is probably not necessary, since
1385 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001386 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001387 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001388 _engineStatisticsPtr->SetLastError(
1389 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1390 "SetRecPayloadType() RTP/RTCP-module registration failed");
1391 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001392 }
kwiberg55b97fe2016-01-28 05:22:45 -08001393 }
kwibergd32bf752017-01-19 07:03:59 -08001394 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1395 audio_coding_->UnregisterReceiveCodec(payload_type);
1396 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001397 _engineStatisticsPtr->SetLastError(
1398 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1399 "SetRecPayloadType() ACM registration failed - 1");
1400 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001401 }
kwiberg55b97fe2016-01-28 05:22:45 -08001402 }
1403 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001404}
1405
kwiberg55b97fe2016-01-28 05:22:45 -08001406int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1407 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001408 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001409 _engineStatisticsPtr->SetLastError(
1410 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1411 "GetRecPayloadType() failed to retrieve RX payload type");
1412 return -1;
1413 }
1414 codec.pltype = payloadType;
1415 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001416}
1417
kwiberg55b97fe2016-01-28 05:22:45 -08001418int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1419 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1420 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001421
kwiberg55b97fe2016-01-28 05:22:45 -08001422 CodecInst codec;
1423 int32_t samplingFreqHz(-1);
1424 const size_t kMono = 1;
1425 if (frequency == kFreq32000Hz)
1426 samplingFreqHz = 32000;
1427 else if (frequency == kFreq16000Hz)
1428 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001429
kwiberg55b97fe2016-01-28 05:22:45 -08001430 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1431 _engineStatisticsPtr->SetLastError(
1432 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1433 "SetSendCNPayloadType() failed to retrieve default CN codec "
1434 "settings");
1435 return -1;
1436 }
1437
1438 // Modify the payload type (must be set to dynamic range)
1439 codec.pltype = type;
1440
kwibergc8d071e2016-04-06 12:22:38 -07001441 if (!codec_manager_.RegisterEncoder(codec) ||
1442 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001443 _engineStatisticsPtr->SetLastError(
1444 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1445 "SetSendCNPayloadType() failed to register CN to ACM");
1446 return -1;
1447 }
1448
1449 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1450 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1451 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1452 _engineStatisticsPtr->SetLastError(
1453 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1454 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1455 "module");
1456 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001457 }
kwiberg55b97fe2016-01-28 05:22:45 -08001458 }
1459 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001460}
1461
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001462int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001463 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001464 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001465
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001466 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001467 _engineStatisticsPtr->SetLastError(
1468 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001469 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001470 return -1;
1471 }
1472 return 0;
1473}
1474
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001475int Channel::SetOpusDtx(bool enable_dtx) {
1476 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1477 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001478 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001479 : audio_coding_->DisableOpusDtx();
1480 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001481 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1482 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001483 return -1;
1484 }
1485 return 0;
1486}
1487
ivoc85228d62016-07-27 04:53:47 -07001488int Channel::GetOpusDtx(bool* enabled) {
1489 int success = -1;
1490 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1491 if (encoder) {
1492 *enabled = encoder->GetDtx();
1493 success = 0;
1494 }
1495 });
1496 return success;
1497}
1498
minyue7e304322016-10-12 05:00:55 -07001499bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1500 bool success = false;
1501 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1502 if (*encoder) {
1503 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001504 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001505 }
1506 });
1507 return success;
1508}
1509
1510void Channel::DisableAudioNetworkAdaptor() {
1511 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1512 if (*encoder)
1513 (*encoder)->DisableAudioNetworkAdaptor();
1514 });
1515}
1516
1517void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1518 int max_frame_length_ms) {
1519 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1520 if (*encoder) {
1521 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1522 max_frame_length_ms);
1523 }
1524 });
1525}
1526
mflodman3d7db262016-04-29 00:57:13 -07001527int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001528 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001529 "Channel::RegisterExternalTransport()");
1530
kwiberg55b97fe2016-01-28 05:22:45 -08001531 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001532 if (_externalTransport) {
1533 _engineStatisticsPtr->SetLastError(
1534 VE_INVALID_OPERATION, kTraceError,
1535 "RegisterExternalTransport() external transport already enabled");
1536 return -1;
1537 }
1538 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001539 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001540 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001541}
1542
kwiberg55b97fe2016-01-28 05:22:45 -08001543int32_t Channel::DeRegisterExternalTransport() {
1544 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1545 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001546
kwiberg55b97fe2016-01-28 05:22:45 -08001547 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001548 if (_transportPtr) {
1549 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1550 "DeRegisterExternalTransport() all transport is disabled");
1551 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001552 _engineStatisticsPtr->SetLastError(
1553 VE_INVALID_OPERATION, kTraceWarning,
1554 "DeRegisterExternalTransport() external transport already "
1555 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001556 }
1557 _externalTransport = false;
1558 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001559 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001560}
1561
mflodman3d7db262016-04-29 00:57:13 -07001562int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001563 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001564 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001565 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001566 "Channel::ReceivedRTPPacket()");
1567
1568 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001569 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001570
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001571 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001572 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1573 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1574 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001575 return -1;
1576 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001577 header.payload_type_frequency =
1578 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001579 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001580 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001581 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001582 rtp_receive_statistics_->IncomingPacket(
1583 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001584 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001585
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001586 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001587}
1588
1589bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001590 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001591 const RTPHeader& header,
1592 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001593 if (rtp_payload_registry_->IsRtx(header)) {
1594 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001595 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001596 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001597 assert(packet_length >= header.headerLength);
1598 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001599 PayloadUnion payload_specific;
1600 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001601 &payload_specific)) {
1602 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001603 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001604 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1605 payload_specific, in_order);
1606}
1607
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001608bool Channel::HandleRtxPacket(const uint8_t* packet,
1609 size_t packet_length,
1610 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001611 if (!rtp_payload_registry_->IsRtx(header))
1612 return false;
1613
1614 // Remove the RTX header and parse the original RTP header.
1615 if (packet_length < header.headerLength)
1616 return false;
1617 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1618 return false;
1619 if (restored_packet_in_use_) {
1620 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1621 "Multiple RTX headers detected, dropping packet");
1622 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001623 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001624 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001625 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1626 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001627 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1628 "Incoming RTX packet: invalid RTP header");
1629 return false;
1630 }
1631 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001632 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001633 restored_packet_in_use_ = false;
1634 return ret;
1635}
1636
1637bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1638 StreamStatistician* statistician =
1639 rtp_receive_statistics_->GetStatistician(header.ssrc);
1640 if (!statistician)
1641 return false;
1642 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001643}
1644
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001645bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1646 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001647 // Retransmissions are handled separately if RTX is enabled.
1648 if (rtp_payload_registry_->RtxEnabled())
1649 return false;
1650 StreamStatistician* statistician =
1651 rtp_receive_statistics_->GetStatistician(header.ssrc);
1652 if (!statistician)
1653 return false;
1654 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001655 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001656 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001657 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001658}
1659
mflodman3d7db262016-04-29 00:57:13 -07001660int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001661 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001662 "Channel::ReceivedRTCPPacket()");
1663 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001664 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001665
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001666 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001667 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001668 _engineStatisticsPtr->SetLastError(
1669 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1670 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1671 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001672
Minyue2013aec2015-05-13 14:14:42 +02001673 int64_t rtt = GetRTT(true);
1674 if (rtt == 0) {
1675 // Waiting for valid RTT.
1676 return 0;
1677 }
Erik Språng737336d2016-07-29 12:59:36 +02001678
1679 int64_t nack_window_ms = rtt;
1680 if (nack_window_ms < kMinRetransmissionWindowMs) {
1681 nack_window_ms = kMinRetransmissionWindowMs;
1682 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1683 nack_window_ms = kMaxRetransmissionWindowMs;
1684 }
1685 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1686
minyue7e304322016-10-12 05:00:55 -07001687 // Invoke audio encoders OnReceivedRtt().
1688 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1689 if (*encoder)
1690 (*encoder)->OnReceivedRtt(rtt);
1691 });
1692
Minyue2013aec2015-05-13 14:14:42 +02001693 uint32_t ntp_secs = 0;
1694 uint32_t ntp_frac = 0;
1695 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001696 if (0 !=
1697 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1698 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001699 // Waiting for RTCP.
1700 return 0;
1701 }
1702
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001703 {
tommi31fc21f2016-01-21 10:37:37 -08001704 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001705 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001706 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001707 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001708}
1709
niklase@google.com470e71d2011-07-07 08:21:25 +00001710int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001711 bool loop,
1712 FileFormats format,
1713 int startPosition,
1714 float volumeScaling,
1715 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001716 const CodecInst* codecInst) {
1717 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1718 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1719 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1720 "stopPosition=%d)",
1721 fileName, loop, format, volumeScaling, startPosition,
1722 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001723
kwiberg55b97fe2016-01-28 05:22:45 -08001724 if (channel_state_.Get().output_file_playing) {
1725 _engineStatisticsPtr->SetLastError(
1726 VE_ALREADY_PLAYING, kTraceError,
1727 "StartPlayingFileLocally() is already playing");
1728 return -1;
1729 }
1730
1731 {
1732 rtc::CritScope cs(&_fileCritSect);
1733
kwiberg5a25d952016-08-17 07:31:12 -07001734 if (output_file_player_) {
1735 output_file_player_->RegisterModuleFileCallback(NULL);
1736 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001737 }
1738
kwiberg5b356f42016-09-08 04:32:33 -07001739 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001740 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001741
kwiberg5a25d952016-08-17 07:31:12 -07001742 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001743 _engineStatisticsPtr->SetLastError(
1744 VE_INVALID_ARGUMENT, kTraceError,
1745 "StartPlayingFileLocally() filePlayer format is not correct");
1746 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001747 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001748
kwiberg55b97fe2016-01-28 05:22:45 -08001749 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001750
kwiberg5a25d952016-08-17 07:31:12 -07001751 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001752 fileName, loop, startPosition, volumeScaling, notificationTime,
1753 stopPosition, (const CodecInst*)codecInst) != 0) {
1754 _engineStatisticsPtr->SetLastError(
1755 VE_BAD_FILE, kTraceError,
1756 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001757 output_file_player_->StopPlayingFile();
1758 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001759 return -1;
1760 }
kwiberg5a25d952016-08-17 07:31:12 -07001761 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001762 channel_state_.SetOutputFilePlaying(true);
1763 }
1764
1765 if (RegisterFilePlayingToMixer() != 0)
1766 return -1;
1767
1768 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001769}
1770
1771int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001772 FileFormats format,
1773 int startPosition,
1774 float volumeScaling,
1775 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001776 const CodecInst* codecInst) {
1777 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1778 "Channel::StartPlayingFileLocally(format=%d,"
1779 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1780 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001781
kwiberg55b97fe2016-01-28 05:22:45 -08001782 if (stream == NULL) {
1783 _engineStatisticsPtr->SetLastError(
1784 VE_BAD_FILE, kTraceError,
1785 "StartPlayingFileLocally() NULL as input stream");
1786 return -1;
1787 }
1788
1789 if (channel_state_.Get().output_file_playing) {
1790 _engineStatisticsPtr->SetLastError(
1791 VE_ALREADY_PLAYING, kTraceError,
1792 "StartPlayingFileLocally() is already playing");
1793 return -1;
1794 }
1795
1796 {
1797 rtc::CritScope cs(&_fileCritSect);
1798
1799 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001800 if (output_file_player_) {
1801 output_file_player_->RegisterModuleFileCallback(NULL);
1802 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001803 }
1804
kwiberg55b97fe2016-01-28 05:22:45 -08001805 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001806 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001807 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001808
kwiberg5a25d952016-08-17 07:31:12 -07001809 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001810 _engineStatisticsPtr->SetLastError(
1811 VE_INVALID_ARGUMENT, kTraceError,
1812 "StartPlayingFileLocally() filePlayer format isnot correct");
1813 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001814 }
1815
kwiberg55b97fe2016-01-28 05:22:45 -08001816 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001817
kwiberg4ec01d92016-08-22 08:43:54 -07001818 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001819 volumeScaling, notificationTime,
1820 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001821 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1822 "StartPlayingFile() failed to "
1823 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001824 output_file_player_->StopPlayingFile();
1825 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001826 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001827 }
kwiberg5a25d952016-08-17 07:31:12 -07001828 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001829 channel_state_.SetOutputFilePlaying(true);
1830 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001831
kwiberg55b97fe2016-01-28 05:22:45 -08001832 if (RegisterFilePlayingToMixer() != 0)
1833 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001834
kwiberg55b97fe2016-01-28 05:22:45 -08001835 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001836}
1837
kwiberg55b97fe2016-01-28 05:22:45 -08001838int Channel::StopPlayingFileLocally() {
1839 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1840 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001841
kwiberg55b97fe2016-01-28 05:22:45 -08001842 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001843 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001844 }
1845
1846 {
1847 rtc::CritScope cs(&_fileCritSect);
1848
kwiberg5a25d952016-08-17 07:31:12 -07001849 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001850 _engineStatisticsPtr->SetLastError(
1851 VE_STOP_RECORDING_FAILED, kTraceError,
1852 "StopPlayingFile() could not stop playing");
1853 return -1;
1854 }
kwiberg5a25d952016-08-17 07:31:12 -07001855 output_file_player_->RegisterModuleFileCallback(NULL);
1856 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001857 channel_state_.SetOutputFilePlaying(false);
1858 }
1859 // _fileCritSect cannot be taken while calling
1860 // SetAnonymousMixibilityStatus. Refer to comments in
1861 // StartPlayingFileLocally(const char* ...) for more details.
1862 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1863 _engineStatisticsPtr->SetLastError(
1864 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1865 "StopPlayingFile() failed to stop participant from playing as"
1866 "file in the mixer");
1867 return -1;
1868 }
1869
1870 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001871}
1872
kwiberg55b97fe2016-01-28 05:22:45 -08001873int Channel::IsPlayingFileLocally() const {
1874 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001875}
1876
kwiberg55b97fe2016-01-28 05:22:45 -08001877int Channel::RegisterFilePlayingToMixer() {
1878 // Return success for not registering for file playing to mixer if:
1879 // 1. playing file before playout is started on that channel.
1880 // 2. starting playout without file playing on that channel.
1881 if (!channel_state_.Get().playing ||
1882 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001883 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001884 }
1885
1886 // |_fileCritSect| cannot be taken while calling
1887 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1888 // frames can be pulled by the mixer. Since the frames are generated from
1889 // the file, _fileCritSect will be taken. This would result in a deadlock.
1890 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1891 channel_state_.SetOutputFilePlaying(false);
1892 rtc::CritScope cs(&_fileCritSect);
1893 _engineStatisticsPtr->SetLastError(
1894 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1895 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001896 output_file_player_->StopPlayingFile();
1897 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001898 return -1;
1899 }
1900
1901 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001902}
1903
niklase@google.com470e71d2011-07-07 08:21:25 +00001904int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001905 bool loop,
1906 FileFormats format,
1907 int startPosition,
1908 float volumeScaling,
1909 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001910 const CodecInst* codecInst) {
1911 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1912 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1913 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1914 "stopPosition=%d)",
1915 fileName, loop, format, volumeScaling, startPosition,
1916 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001917
kwiberg55b97fe2016-01-28 05:22:45 -08001918 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001919
kwiberg55b97fe2016-01-28 05:22:45 -08001920 if (channel_state_.Get().input_file_playing) {
1921 _engineStatisticsPtr->SetLastError(
1922 VE_ALREADY_PLAYING, kTraceWarning,
1923 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001924 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001925 }
1926
1927 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001928 if (input_file_player_) {
1929 input_file_player_->RegisterModuleFileCallback(NULL);
1930 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001931 }
1932
1933 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001934 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001935 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001936
kwiberg5a25d952016-08-17 07:31:12 -07001937 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001938 _engineStatisticsPtr->SetLastError(
1939 VE_INVALID_ARGUMENT, kTraceError,
1940 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1941 return -1;
1942 }
1943
1944 const uint32_t notificationTime(0);
1945
kwiberg5a25d952016-08-17 07:31:12 -07001946 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001947 fileName, loop, startPosition, volumeScaling, notificationTime,
1948 stopPosition, (const CodecInst*)codecInst) != 0) {
1949 _engineStatisticsPtr->SetLastError(
1950 VE_BAD_FILE, kTraceError,
1951 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001952 input_file_player_->StopPlayingFile();
1953 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001954 return -1;
1955 }
kwiberg5a25d952016-08-17 07:31:12 -07001956 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001957 channel_state_.SetInputFilePlaying(true);
1958
1959 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001960}
1961
1962int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001963 FileFormats format,
1964 int startPosition,
1965 float volumeScaling,
1966 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001967 const CodecInst* codecInst) {
1968 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1969 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1970 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1971 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001972
kwiberg55b97fe2016-01-28 05:22:45 -08001973 if (stream == NULL) {
1974 _engineStatisticsPtr->SetLastError(
1975 VE_BAD_FILE, kTraceError,
1976 "StartPlayingFileAsMicrophone NULL as input stream");
1977 return -1;
1978 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001979
kwiberg55b97fe2016-01-28 05:22:45 -08001980 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001981
kwiberg55b97fe2016-01-28 05:22:45 -08001982 if (channel_state_.Get().input_file_playing) {
1983 _engineStatisticsPtr->SetLastError(
1984 VE_ALREADY_PLAYING, kTraceWarning,
1985 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001986 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001987 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001988
kwiberg55b97fe2016-01-28 05:22:45 -08001989 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001990 if (input_file_player_) {
1991 input_file_player_->RegisterModuleFileCallback(NULL);
1992 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001993 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001994
kwiberg55b97fe2016-01-28 05:22:45 -08001995 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001996 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001997 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001998
kwiberg5a25d952016-08-17 07:31:12 -07001999 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002000 _engineStatisticsPtr->SetLastError(
2001 VE_INVALID_ARGUMENT, kTraceError,
2002 "StartPlayingInputFile() filePlayer format isnot correct");
2003 return -1;
2004 }
2005
2006 const uint32_t notificationTime(0);
2007
kwiberg4ec01d92016-08-22 08:43:54 -07002008 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2009 notificationTime, stopPosition,
2010 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002011 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2012 "StartPlayingFile() failed to start "
2013 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002014 input_file_player_->StopPlayingFile();
2015 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002016 return -1;
2017 }
2018
kwiberg5a25d952016-08-17 07:31:12 -07002019 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002020 channel_state_.SetInputFilePlaying(true);
2021
2022 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002023}
2024
kwiberg55b97fe2016-01-28 05:22:45 -08002025int Channel::StopPlayingFileAsMicrophone() {
2026 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2027 "Channel::StopPlayingFileAsMicrophone()");
2028
2029 rtc::CritScope cs(&_fileCritSect);
2030
2031 if (!channel_state_.Get().input_file_playing) {
2032 return 0;
2033 }
2034
kwiberg5a25d952016-08-17 07:31:12 -07002035 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002036 _engineStatisticsPtr->SetLastError(
2037 VE_STOP_RECORDING_FAILED, kTraceError,
2038 "StopPlayingFile() could not stop playing");
2039 return -1;
2040 }
kwiberg5a25d952016-08-17 07:31:12 -07002041 input_file_player_->RegisterModuleFileCallback(NULL);
2042 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002043 channel_state_.SetInputFilePlaying(false);
2044
2045 return 0;
2046}
2047
2048int Channel::IsPlayingFileAsMicrophone() const {
2049 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002050}
2051
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002052int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002053 const CodecInst* codecInst) {
2054 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2055 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002056
kwiberg55b97fe2016-01-28 05:22:45 -08002057 if (_outputFileRecording) {
2058 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2059 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002060 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002061 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002062
kwiberg55b97fe2016-01-28 05:22:45 -08002063 FileFormats format;
2064 const uint32_t notificationTime(0); // Not supported in VoE
2065 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002066
kwiberg55b97fe2016-01-28 05:22:45 -08002067 if ((codecInst != NULL) &&
2068 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2069 _engineStatisticsPtr->SetLastError(
2070 VE_BAD_ARGUMENT, kTraceError,
2071 "StartRecordingPlayout() invalid compression");
2072 return (-1);
2073 }
2074 if (codecInst == NULL) {
2075 format = kFileFormatPcm16kHzFile;
2076 codecInst = &dummyCodec;
2077 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2078 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2079 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2080 format = kFileFormatWavFile;
2081 } else {
2082 format = kFileFormatCompressedFile;
2083 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002084
kwiberg55b97fe2016-01-28 05:22:45 -08002085 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002086
kwiberg55b97fe2016-01-28 05:22:45 -08002087 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002088 if (output_file_recorder_) {
2089 output_file_recorder_->RegisterModuleFileCallback(NULL);
2090 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002091 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002092
kwiberg5a25d952016-08-17 07:31:12 -07002093 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002094 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002095 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002096 _engineStatisticsPtr->SetLastError(
2097 VE_INVALID_ARGUMENT, kTraceError,
2098 "StartRecordingPlayout() fileRecorder format isnot correct");
2099 return -1;
2100 }
2101
kwiberg5a25d952016-08-17 07:31:12 -07002102 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002103 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2104 _engineStatisticsPtr->SetLastError(
2105 VE_BAD_FILE, kTraceError,
2106 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002107 output_file_recorder_->StopRecording();
2108 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002109 return -1;
2110 }
kwiberg5a25d952016-08-17 07:31:12 -07002111 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002112 _outputFileRecording = true;
2113
2114 return 0;
2115}
2116
2117int Channel::StartRecordingPlayout(OutStream* stream,
2118 const CodecInst* codecInst) {
2119 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2120 "Channel::StartRecordingPlayout()");
2121
2122 if (_outputFileRecording) {
2123 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2124 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002125 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002126 }
2127
2128 FileFormats format;
2129 const uint32_t notificationTime(0); // Not supported in VoE
2130 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2131
2132 if (codecInst != NULL && codecInst->channels != 1) {
2133 _engineStatisticsPtr->SetLastError(
2134 VE_BAD_ARGUMENT, kTraceError,
2135 "StartRecordingPlayout() invalid compression");
2136 return (-1);
2137 }
2138 if (codecInst == NULL) {
2139 format = kFileFormatPcm16kHzFile;
2140 codecInst = &dummyCodec;
2141 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2142 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2143 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2144 format = kFileFormatWavFile;
2145 } else {
2146 format = kFileFormatCompressedFile;
2147 }
2148
2149 rtc::CritScope cs(&_fileCritSect);
2150
2151 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002152 if (output_file_recorder_) {
2153 output_file_recorder_->RegisterModuleFileCallback(NULL);
2154 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002155 }
2156
kwiberg5a25d952016-08-17 07:31:12 -07002157 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002158 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002159 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002160 _engineStatisticsPtr->SetLastError(
2161 VE_INVALID_ARGUMENT, kTraceError,
2162 "StartRecordingPlayout() fileRecorder format isnot correct");
2163 return -1;
2164 }
2165
kwiberg4ec01d92016-08-22 08:43:54 -07002166 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002167 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002168 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2169 "StartRecordingPlayout() failed to "
2170 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002171 output_file_recorder_->StopRecording();
2172 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002173 return -1;
2174 }
2175
kwiberg5a25d952016-08-17 07:31:12 -07002176 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002177 _outputFileRecording = true;
2178
2179 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002180}
2181
kwiberg55b97fe2016-01-28 05:22:45 -08002182int Channel::StopRecordingPlayout() {
2183 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2184 "Channel::StopRecordingPlayout()");
2185
2186 if (!_outputFileRecording) {
2187 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2188 "StopRecordingPlayout() isnot recording");
2189 return -1;
2190 }
2191
2192 rtc::CritScope cs(&_fileCritSect);
2193
kwiberg5a25d952016-08-17 07:31:12 -07002194 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002195 _engineStatisticsPtr->SetLastError(
2196 VE_STOP_RECORDING_FAILED, kTraceError,
2197 "StopRecording() could not stop recording");
2198 return (-1);
2199 }
kwiberg5a25d952016-08-17 07:31:12 -07002200 output_file_recorder_->RegisterModuleFileCallback(NULL);
2201 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002202 _outputFileRecording = false;
2203
2204 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002205}
2206
kwiberg55b97fe2016-01-28 05:22:45 -08002207void Channel::SetMixWithMicStatus(bool mix) {
2208 rtc::CritScope cs(&_fileCritSect);
2209 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002210}
2211
kwiberg55b97fe2016-01-28 05:22:45 -08002212int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2213 int8_t currentLevel = _outputAudioLevel.Level();
2214 level = static_cast<int32_t>(currentLevel);
2215 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002216}
2217
kwiberg55b97fe2016-01-28 05:22:45 -08002218int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2219 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2220 level = static_cast<int32_t>(currentLevel);
2221 return 0;
2222}
2223
solenberg1c2af8e2016-03-24 10:36:00 -07002224int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002225 rtc::CritScope cs(&volume_settings_critsect_);
2226 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002227 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002228 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002229 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002230}
2231
solenberg1c2af8e2016-03-24 10:36:00 -07002232bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002233 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002234 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002235}
2236
kwiberg55b97fe2016-01-28 05:22:45 -08002237int Channel::SetOutputVolumePan(float left, float right) {
2238 rtc::CritScope cs(&volume_settings_critsect_);
2239 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002240 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002241 _panLeft = left;
2242 _panRight = right;
2243 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002244}
2245
kwiberg55b97fe2016-01-28 05:22:45 -08002246int Channel::GetOutputVolumePan(float& left, float& right) const {
2247 rtc::CritScope cs(&volume_settings_critsect_);
2248 left = _panLeft;
2249 right = _panRight;
2250 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002251}
2252
kwiberg55b97fe2016-01-28 05:22:45 -08002253int Channel::SetChannelOutputVolumeScaling(float scaling) {
2254 rtc::CritScope cs(&volume_settings_critsect_);
2255 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002256 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002257 _outputGain = scaling;
2258 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002259}
2260
kwiberg55b97fe2016-01-28 05:22:45 -08002261int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2262 rtc::CritScope cs(&volume_settings_critsect_);
2263 scaling = _outputGain;
2264 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002265}
2266
solenberg8842c3e2016-03-11 03:06:41 -08002267int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002268 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002269 "Channel::SendTelephoneEventOutband(...)");
2270 RTC_DCHECK_LE(0, event);
2271 RTC_DCHECK_GE(255, event);
2272 RTC_DCHECK_LE(0, duration_ms);
2273 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002274 if (!Sending()) {
2275 return -1;
2276 }
solenberg8842c3e2016-03-11 03:06:41 -08002277 if (_rtpRtcpModule->SendTelephoneEventOutband(
2278 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002279 _engineStatisticsPtr->SetLastError(
2280 VE_SEND_DTMF_FAILED, kTraceWarning,
2281 "SendTelephoneEventOutband() failed to send event");
2282 return -1;
2283 }
2284 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002285}
2286
solenbergffbbcac2016-11-17 05:25:37 -08002287int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2288 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002289 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002290 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002291 RTC_DCHECK_LE(0, payload_type);
2292 RTC_DCHECK_GE(127, payload_type);
2293 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002294 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002295 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002296 memcpy(codec.plname, "telephone-event", 16);
2297 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2298 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2299 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2300 _engineStatisticsPtr->SetLastError(
2301 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2302 "SetSendTelephoneEventPayloadType() failed to register send"
2303 "payload type");
2304 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002305 }
kwiberg55b97fe2016-01-28 05:22:45 -08002306 }
kwiberg55b97fe2016-01-28 05:22:45 -08002307 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002308}
2309
kwiberg55b97fe2016-01-28 05:22:45 -08002310int Channel::VoiceActivityIndicator(int& activity) {
2311 activity = _sendFrameType;
2312 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002313}
2314
kwiberg55b97fe2016-01-28 05:22:45 -08002315int Channel::SetLocalSSRC(unsigned int ssrc) {
2316 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2317 "Channel::SetLocalSSRC()");
2318 if (channel_state_.Get().sending) {
2319 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2320 "SetLocalSSRC() already sending");
2321 return -1;
2322 }
2323 _rtpRtcpModule->SetSSRC(ssrc);
2324 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002325}
2326
kwiberg55b97fe2016-01-28 05:22:45 -08002327int Channel::GetLocalSSRC(unsigned int& ssrc) {
2328 ssrc = _rtpRtcpModule->SSRC();
2329 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002330}
2331
kwiberg55b97fe2016-01-28 05:22:45 -08002332int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2333 ssrc = rtp_receiver_->SSRC();
2334 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002335}
2336
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002337int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002338 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002339 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002340}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002341
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002342int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2343 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002344 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2345 if (enable &&
2346 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2347 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002348 return -1;
2349 }
2350 return 0;
2351}
2352
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002353void Channel::EnableSendTransportSequenceNumber(int id) {
2354 int ret =
2355 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2356 RTC_DCHECK_EQ(0, ret);
2357}
2358
stefan3313ec92016-01-21 06:32:43 -08002359void Channel::EnableReceiveTransportSequenceNumber(int id) {
2360 rtp_header_parser_->DeregisterRtpHeaderExtension(
2361 kRtpExtensionTransportSequenceNumber);
2362 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2363 kRtpExtensionTransportSequenceNumber, id);
2364 RTC_DCHECK(ret);
2365}
2366
stefanbba9dec2016-02-01 04:39:55 -08002367void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002368 RtpPacketSender* rtp_packet_sender,
2369 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002370 PacketRouter* packet_router,
2371 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002372 RTC_DCHECK(rtp_packet_sender);
2373 RTC_DCHECK(transport_feedback_observer);
2374 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002375 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002376 feedback_observer_proxy_->SetTransportFeedbackObserver(
2377 transport_feedback_observer);
2378 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2379 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2380 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002381 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002382 packet_router_ = packet_router;
2383}
2384
stefanbba9dec2016-02-01 04:39:55 -08002385void Channel::RegisterReceiverCongestionControlObjects(
2386 PacketRouter* packet_router) {
2387 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002388 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002389 packet_router_ = packet_router;
2390}
2391
2392void Channel::ResetCongestionControlObjects() {
2393 RTC_DCHECK(packet_router_);
2394 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002395 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002396 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2397 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002398 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002399 packet_router_ = nullptr;
2400 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2401}
2402
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002403void Channel::SetRTCPStatus(bool enable) {
2404 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2405 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002406 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002407}
2408
kwiberg55b97fe2016-01-28 05:22:45 -08002409int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002410 RtcpMode method = _rtpRtcpModule->RTCP();
2411 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002412 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002413}
2414
kwiberg55b97fe2016-01-28 05:22:45 -08002415int Channel::SetRTCP_CNAME(const char cName[256]) {
2416 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2417 "Channel::SetRTCP_CNAME()");
2418 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2419 _engineStatisticsPtr->SetLastError(
2420 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2421 "SetRTCP_CNAME() failed to set RTCP CNAME");
2422 return -1;
2423 }
2424 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002425}
2426
kwiberg55b97fe2016-01-28 05:22:45 -08002427int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2428 if (cName == NULL) {
2429 _engineStatisticsPtr->SetLastError(
2430 VE_INVALID_ARGUMENT, kTraceError,
2431 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2432 return -1;
2433 }
2434 char cname[RTCP_CNAME_SIZE];
2435 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2436 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2437 _engineStatisticsPtr->SetLastError(
2438 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2439 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2440 return -1;
2441 }
2442 strcpy(cName, cname);
2443 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002444}
2445
kwiberg55b97fe2016-01-28 05:22:45 -08002446int Channel::SendApplicationDefinedRTCPPacket(
2447 unsigned char subType,
2448 unsigned int name,
2449 const char* data,
2450 unsigned short dataLengthInBytes) {
2451 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2452 "Channel::SendApplicationDefinedRTCPPacket()");
2453 if (!channel_state_.Get().sending) {
2454 _engineStatisticsPtr->SetLastError(
2455 VE_NOT_SENDING, kTraceError,
2456 "SendApplicationDefinedRTCPPacket() not sending");
2457 return -1;
2458 }
2459 if (NULL == data) {
2460 _engineStatisticsPtr->SetLastError(
2461 VE_INVALID_ARGUMENT, kTraceError,
2462 "SendApplicationDefinedRTCPPacket() invalid data value");
2463 return -1;
2464 }
2465 if (dataLengthInBytes % 4 != 0) {
2466 _engineStatisticsPtr->SetLastError(
2467 VE_INVALID_ARGUMENT, kTraceError,
2468 "SendApplicationDefinedRTCPPacket() invalid length value");
2469 return -1;
2470 }
2471 RtcpMode status = _rtpRtcpModule->RTCP();
2472 if (status == RtcpMode::kOff) {
2473 _engineStatisticsPtr->SetLastError(
2474 VE_RTCP_ERROR, kTraceError,
2475 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2476 return -1;
2477 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002478
kwiberg55b97fe2016-01-28 05:22:45 -08002479 // Create and schedule the RTCP APP packet for transmission
2480 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2481 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2482 _engineStatisticsPtr->SetLastError(
2483 VE_SEND_ERROR, kTraceError,
2484 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2485 return -1;
2486 }
2487 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002488}
2489
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002490int Channel::GetRemoteRTCPReportBlocks(
2491 std::vector<ReportBlock>* report_blocks) {
2492 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002493 _engineStatisticsPtr->SetLastError(
2494 VE_INVALID_ARGUMENT, kTraceError,
2495 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002496 return -1;
2497 }
2498
2499 // Get the report blocks from the latest received RTCP Sender or Receiver
2500 // Report. Each element in the vector contains the sender's SSRC and a
2501 // report block according to RFC 3550.
2502 std::vector<RTCPReportBlock> rtcp_report_blocks;
2503 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002504 return -1;
2505 }
2506
2507 if (rtcp_report_blocks.empty())
2508 return 0;
2509
2510 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2511 for (; it != rtcp_report_blocks.end(); ++it) {
2512 ReportBlock report_block;
2513 report_block.sender_SSRC = it->remoteSSRC;
2514 report_block.source_SSRC = it->sourceSSRC;
2515 report_block.fraction_lost = it->fractionLost;
2516 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2517 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2518 report_block.interarrival_jitter = it->jitter;
2519 report_block.last_SR_timestamp = it->lastSR;
2520 report_block.delay_since_last_SR = it->delaySinceLastSR;
2521 report_blocks->push_back(report_block);
2522 }
2523 return 0;
2524}
2525
kwiberg55b97fe2016-01-28 05:22:45 -08002526int Channel::GetRTPStatistics(CallStatistics& stats) {
2527 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002528
kwiberg55b97fe2016-01-28 05:22:45 -08002529 // The jitter statistics is updated for each received RTP packet and is
2530 // based on received packets.
2531 RtcpStatistics statistics;
2532 StreamStatistician* statistician =
2533 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002534 if (statistician) {
2535 statistician->GetStatistics(&statistics,
2536 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002537 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002538
kwiberg55b97fe2016-01-28 05:22:45 -08002539 stats.fractionLost = statistics.fraction_lost;
2540 stats.cumulativeLost = statistics.cumulative_lost;
2541 stats.extendedMax = statistics.extended_max_sequence_number;
2542 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002543
kwiberg55b97fe2016-01-28 05:22:45 -08002544 // --- RTT
2545 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002546
kwiberg55b97fe2016-01-28 05:22:45 -08002547 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002548
kwiberg55b97fe2016-01-28 05:22:45 -08002549 size_t bytesSent(0);
2550 uint32_t packetsSent(0);
2551 size_t bytesReceived(0);
2552 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002553
kwiberg55b97fe2016-01-28 05:22:45 -08002554 if (statistician) {
2555 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2556 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002557
kwiberg55b97fe2016-01-28 05:22:45 -08002558 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2559 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2560 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2561 " output will not be complete");
2562 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002563
kwiberg55b97fe2016-01-28 05:22:45 -08002564 stats.bytesSent = bytesSent;
2565 stats.packetsSent = packetsSent;
2566 stats.bytesReceived = bytesReceived;
2567 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002568
kwiberg55b97fe2016-01-28 05:22:45 -08002569 // --- Timestamps
2570 {
2571 rtc::CritScope lock(&ts_stats_lock_);
2572 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2573 }
2574 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002575}
2576
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002577int Channel::SetCodecFECStatus(bool enable) {
2578 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2579 "Channel::SetCodecFECStatus()");
2580
kwibergc8d071e2016-04-06 12:22:38 -07002581 if (!codec_manager_.SetCodecFEC(enable) ||
2582 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002583 _engineStatisticsPtr->SetLastError(
2584 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2585 "SetCodecFECStatus() failed to set FEC state");
2586 return -1;
2587 }
2588 return 0;
2589}
2590
2591bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002592 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002593}
2594
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002595void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2596 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002597 // If pacing is enabled we always store packets.
2598 if (!pacing_enabled_)
2599 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002600 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002601 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002602 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002603 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002604 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002605}
2606
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002607// Called when we are missing one or more packets.
2608int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002609 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2610}
2611
kwiberg55b97fe2016-01-28 05:22:45 -08002612uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2613 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2614 "Channel::Demultiplex()");
2615 _audioFrame.CopyFrom(audioFrame);
2616 _audioFrame.id_ = _channelId;
2617 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002618}
2619
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002620void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002621 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002622 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002623 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002624 CodecInst codec;
2625 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002626
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002627 // Never upsample or upmix the capture signal here. This should be done at the
2628 // end of the send chain.
2629 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2630 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2631 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2632 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002633}
2634
kwiberg55b97fe2016-01-28 05:22:45 -08002635uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2636 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2637 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002638
kwiberg55b97fe2016-01-28 05:22:45 -08002639 if (_audioFrame.samples_per_channel_ == 0) {
2640 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2641 "Channel::PrepareEncodeAndSend() invalid audio frame");
2642 return 0xFFFFFFFF;
2643 }
2644
2645 if (channel_state_.Get().input_file_playing) {
2646 MixOrReplaceAudioWithFile(mixingFrequency);
2647 }
2648
solenberg1c2af8e2016-03-24 10:36:00 -07002649 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2650 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002651
kwiberg55b97fe2016-01-28 05:22:45 -08002652 if (_includeAudioLevelIndication) {
2653 size_t length =
2654 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002655 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002656 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002657 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002658 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002659 rms_level_.Analyze(
2660 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002661 }
kwiberg55b97fe2016-01-28 05:22:45 -08002662 }
solenberg1c2af8e2016-03-24 10:36:00 -07002663 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002664
kwiberg55b97fe2016-01-28 05:22:45 -08002665 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002666}
2667
kwiberg55b97fe2016-01-28 05:22:45 -08002668uint32_t Channel::EncodeAndSend() {
2669 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2670 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002671
kwiberg55b97fe2016-01-28 05:22:45 -08002672 assert(_audioFrame.num_channels_ <= 2);
2673 if (_audioFrame.samples_per_channel_ == 0) {
2674 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2675 "Channel::EncodeAndSend() invalid audio frame");
2676 return 0xFFFFFFFF;
2677 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002678
kwiberg55b97fe2016-01-28 05:22:45 -08002679 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002680
kwiberg55b97fe2016-01-28 05:22:45 -08002681 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002682
kwiberg55b97fe2016-01-28 05:22:45 -08002683 // The ACM resamples internally.
2684 _audioFrame.timestamp_ = _timeStamp;
2685 // This call will trigger AudioPacketizationCallback::SendData if encoding
2686 // is done and payload is ready for packetization and transmission.
2687 // Otherwise, it will return without invoking the callback.
2688 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2689 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2690 "Channel::EncodeAndSend() ACM encoding failed");
2691 return 0xFFFFFFFF;
2692 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002693
kwiberg55b97fe2016-01-28 05:22:45 -08002694 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2695 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002696}
2697
solenberg7602aab2016-11-14 11:30:07 -08002698void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2699 RTC_DCHECK(!channel.channel() ||
2700 channel.channel()->ChannelId() != _channelId);
2701 rtc::CritScope lock(&assoc_send_channel_lock_);
2702 associate_send_channel_ = channel;
2703}
2704
Minyue2013aec2015-05-13 14:14:42 +02002705void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002706 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002707 Channel* channel = associate_send_channel_.channel();
2708 if (channel && channel->ChannelId() == channel_id) {
2709 // If this channel is associated with a send channel of the specified
2710 // Channel ID, disassociate with it.
2711 ChannelOwner ref(NULL);
2712 associate_send_channel_ = ref;
2713 }
2714}
2715
ivoc14d5dbe2016-07-04 07:06:55 -07002716void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2717 event_log_proxy_->SetEventLog(event_log);
2718}
2719
michaelt9332b7d2016-11-30 07:51:13 -08002720void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2721 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2722}
2723
nisse284542b2017-01-10 08:58:32 -08002724void Channel::UpdateOverheadForEncoder() {
2725 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2726 if (*encoder) {
2727 (*encoder)->OnReceivedOverhead(transport_overhead_per_packet_ +
2728 rtp_overhead_per_packet_);
2729 }
2730 });
2731}
2732
2733void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
2734 transport_overhead_per_packet_ = transport_overhead_per_packet;
2735 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002736}
2737
michaeltbf65be52016-12-15 06:24:49 -08002738void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
nisse284542b2017-01-10 08:58:32 -08002739 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2740 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002741}
2742
kwiberg55b97fe2016-01-28 05:22:45 -08002743int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2744 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002745}
2746
wu@webrtc.org24301a62013-12-13 19:17:43 +00002747void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2748 audio_coding_->GetDecodingCallStatistics(stats);
2749}
2750
solenberg358057b2015-11-27 10:46:42 -08002751uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002752 rtc::CritScope lock(&video_sync_lock_);
2753 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002754}
2755
kwiberg55b97fe2016-01-28 05:22:45 -08002756int Channel::SetMinimumPlayoutDelay(int delayMs) {
2757 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2758 "Channel::SetMinimumPlayoutDelay()");
2759 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2760 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2761 _engineStatisticsPtr->SetLastError(
2762 VE_INVALID_ARGUMENT, kTraceError,
2763 "SetMinimumPlayoutDelay() invalid min delay");
2764 return -1;
2765 }
2766 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2767 _engineStatisticsPtr->SetLastError(
2768 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2769 "SetMinimumPlayoutDelay() failed to set min playout delay");
2770 return -1;
2771 }
2772 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002773}
2774
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002775int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002776 uint32_t playout_timestamp_rtp = 0;
2777 {
tommi31fc21f2016-01-21 10:37:37 -08002778 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002779 playout_timestamp_rtp = playout_timestamp_rtp_;
2780 }
kwiberg55b97fe2016-01-28 05:22:45 -08002781 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002782 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002783 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002784 "GetPlayoutTimestamp() failed to retrieve timestamp");
2785 return -1;
2786 }
deadbeef74375882015-08-13 12:09:10 -07002787 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002788 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002789}
2790
kwiberg55b97fe2016-01-28 05:22:45 -08002791int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2792 RtpReceiver** rtp_receiver) const {
2793 *rtpRtcpModule = _rtpRtcpModule.get();
2794 *rtp_receiver = rtp_receiver_.get();
2795 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002796}
2797
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002798// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2799// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002800int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002801 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002802 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002803
kwiberg55b97fe2016-01-28 05:22:45 -08002804 {
2805 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002806
kwiberg5a25d952016-08-17 07:31:12 -07002807 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002808 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2809 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2810 " doesnt exist");
2811 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002812 }
2813
kwiberg4ec01d92016-08-22 08:43:54 -07002814 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002815 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002816 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2817 "Channel::MixOrReplaceAudioWithFile() file mixing "
2818 "failed");
2819 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002820 }
kwiberg55b97fe2016-01-28 05:22:45 -08002821 if (fileSamples == 0) {
2822 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2823 "Channel::MixOrReplaceAudioWithFile() file is ended");
2824 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002825 }
kwiberg55b97fe2016-01-28 05:22:45 -08002826 }
2827
2828 assert(_audioFrame.samples_per_channel_ == fileSamples);
2829
2830 if (_mixFileWithMicrophone) {
2831 // Currently file stream is always mono.
2832 // TODO(xians): Change the code when FilePlayer supports real stereo.
2833 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2834 1, fileSamples);
2835 } else {
2836 // Replace ACM audio with file.
2837 // Currently file stream is always mono.
2838 // TODO(xians): Change the code when FilePlayer supports real stereo.
2839 _audioFrame.UpdateFrame(
2840 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2841 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2842 }
2843 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002844}
2845
kwiberg55b97fe2016-01-28 05:22:45 -08002846int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2847 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002848
kwibergb7f89d62016-02-17 10:04:18 -08002849 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002850 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002851
kwiberg55b97fe2016-01-28 05:22:45 -08002852 {
2853 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002854
kwiberg5a25d952016-08-17 07:31:12 -07002855 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002856 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2857 "Channel::MixAudioWithFile() file mixing failed");
2858 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002859 }
2860
kwiberg55b97fe2016-01-28 05:22:45 -08002861 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002862 if (output_file_player_->Get10msAudioFromFile(
2863 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002864 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2865 "Channel::MixAudioWithFile() file mixing failed");
2866 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002867 }
kwiberg55b97fe2016-01-28 05:22:45 -08002868 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002869
kwiberg55b97fe2016-01-28 05:22:45 -08002870 if (audioFrame.samples_per_channel_ == fileSamples) {
2871 // Currently file stream is always mono.
2872 // TODO(xians): Change the code when FilePlayer supports real stereo.
2873 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2874 fileSamples);
2875 } else {
2876 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2877 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2878 ") != "
2879 "fileSamples(%" PRIuS ")",
2880 audioFrame.samples_per_channel_, fileSamples);
2881 return -1;
2882 }
2883
2884 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002885}
2886
deadbeef74375882015-08-13 12:09:10 -07002887void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002888 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002889
henrik.lundin96bd5022016-04-06 04:13:56 -07002890 if (!jitter_buffer_playout_timestamp_) {
2891 // This can happen if this channel has not received any RTP packets. In
2892 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002893 return;
2894 }
2895
2896 uint16_t delay_ms = 0;
2897 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002898 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002899 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2900 " delay from the ADM");
2901 _engineStatisticsPtr->SetLastError(
2902 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2903 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2904 return;
2905 }
2906
henrik.lundin96bd5022016-04-06 04:13:56 -07002907 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2908 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002909
2910 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002911 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002912
kwiberg55b97fe2016-01-28 05:22:45 -08002913 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002914 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002915 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002916
2917 {
tommi31fc21f2016-01-21 10:37:37 -08002918 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002919 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002920 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002921 }
2922 playout_delay_ms_ = delay_ms;
2923 }
2924}
2925
kwiberg55b97fe2016-01-28 05:22:45 -08002926void Channel::RegisterReceiveCodecsToRTPModule() {
2927 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2928 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002929
kwiberg55b97fe2016-01-28 05:22:45 -08002930 CodecInst codec;
2931 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002932
kwiberg55b97fe2016-01-28 05:22:45 -08002933 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2934 // Open up the RTP/RTCP receiver for all supported codecs
2935 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002936 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002937 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2938 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2939 " to register %s (%d/%d/%" PRIuS
2940 "/%d) to RTP/RTCP "
2941 "receiver",
2942 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2943 codec.rate);
2944 } else {
2945 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2946 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2947 "(%d/%d/%" PRIuS
2948 "/%d) has been added to the RTP/RTCP "
2949 "receiver",
2950 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2951 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002952 }
kwiberg55b97fe2016-01-28 05:22:45 -08002953 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002954}
2955
kwiberg55b97fe2016-01-28 05:22:45 -08002956int Channel::SetSendRtpHeaderExtension(bool enable,
2957 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002958 unsigned char id) {
2959 int error = 0;
2960 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
2961 if (enable) {
2962 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
2963 }
2964 return error;
2965}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002966
ossue280cde2016-10-12 11:04:10 -07002967int Channel::GetRtpTimestampRateHz() const {
2968 const auto format = audio_coding_->ReceiveFormat();
2969 // Default to the playout frequency if we've not gotten any packets yet.
2970 // TODO(ossu): Zero clockrate can only happen if we've added an external
2971 // decoder for a format we don't support internally. Remove once that way of
2972 // adding decoders is gone!
2973 return (format && format->clockrate_hz != 0)
2974 ? format->clockrate_hz
2975 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00002976}
2977
Minyue2013aec2015-05-13 14:14:42 +02002978int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07002979 RtcpMode method = _rtpRtcpModule->RTCP();
2980 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002981 return 0;
2982 }
2983 std::vector<RTCPReportBlock> report_blocks;
2984 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02002985
2986 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002987 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02002988 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08002989 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002990 Channel* channel = associate_send_channel_.channel();
2991 // Tries to get RTT from an associated channel. This is important for
2992 // receive-only channels.
2993 if (channel) {
2994 // To prevent infinite recursion and deadlock, calling GetRTT of
2995 // associate channel should always use "false" for argument:
2996 // |allow_associate_channel|.
2997 rtt = channel->GetRTT(false);
2998 }
2999 }
3000 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003001 }
3002
3003 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3004 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3005 for (; it != report_blocks.end(); ++it) {
3006 if (it->remoteSSRC == remoteSSRC)
3007 break;
3008 }
3009 if (it == report_blocks.end()) {
3010 // We have not received packets with SSRC matching the report blocks.
3011 // To calculate RTT we try with the SSRC of the first report block.
3012 // This is very important for send-only channels where we don't know
3013 // the SSRC of the other end.
3014 remoteSSRC = report_blocks[0].remoteSSRC;
3015 }
Minyue2013aec2015-05-13 14:14:42 +02003016
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003017 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003018 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003019 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003020 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3021 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003022 return 0;
3023 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003024 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003025}
3026
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003027} // namespace voe
3028} // namespace webrtc