blob: dc5ddcae73ef3745233d4ac04cd54430ba8f4c70 [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
terelius424e6cf2017-02-20 05:14:41 -0800133 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700134 uint8_t fraction_loss,
135 int32_t total_packets) override {
136 rtc::CritScope lock(&crit_);
137 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800138 event_log_->LogLossBasedBweUpdate(bitrate_bps, fraction_loss,
139 total_packets);
ivoc14d5dbe2016-07-04 07:06:55 -0700140 }
141 }
142
terelius424e6cf2017-02-20 05:14:41 -0800143 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800144 BandwidthUsage detector_state) override {
145 rtc::CritScope lock(&crit_);
146 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800147 event_log_->LogDelayBasedBweUpdate(bitrate_bps, detector_state);
terelius0baf55d2017-02-17 03:38:28 -0800148 }
149 }
150
minyue4b7c9522017-01-24 04:54:59 -0800151 void LogAudioNetworkAdaptation(
152 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) override {
153 rtc::CritScope lock(&crit_);
154 if (event_log_) {
155 event_log_->LogAudioNetworkAdaptation(config);
156 }
157 }
158
ivoc14d5dbe2016-07-04 07:06:55 -0700159 void SetEventLog(RtcEventLog* event_log) {
160 rtc::CritScope lock(&crit_);
161 event_log_ = event_log;
162 }
163
164 private:
165 rtc::CriticalSection crit_;
166 RtcEventLog* event_log_ GUARDED_BY(crit_);
167 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
168};
169
michaelt9332b7d2016-11-30 07:51:13 -0800170class RtcpRttStatsProxy final : public RtcpRttStats {
171 public:
172 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
173
174 void OnRttUpdate(int64_t rtt) override {
175 rtc::CritScope lock(&crit_);
176 if (rtcp_rtt_stats_)
177 rtcp_rtt_stats_->OnRttUpdate(rtt);
178 }
179
180 int64_t LastProcessedRtt() const override {
181 rtc::CritScope lock(&crit_);
182 if (!rtcp_rtt_stats_)
183 return 0;
184 return rtcp_rtt_stats_->LastProcessedRtt();
185 }
186
187 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
188 rtc::CritScope lock(&crit_);
189 rtcp_rtt_stats_ = rtcp_rtt_stats;
190 }
191
192 private:
193 rtc::CriticalSection crit_;
194 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
195 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
196};
197
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100198class TransportFeedbackProxy : public TransportFeedbackObserver {
199 public:
200 TransportFeedbackProxy() : feedback_observer_(nullptr) {
201 pacer_thread_.DetachFromThread();
202 network_thread_.DetachFromThread();
203 }
204
205 void SetTransportFeedbackObserver(
206 TransportFeedbackObserver* feedback_observer) {
207 RTC_DCHECK(thread_checker_.CalledOnValidThread());
208 rtc::CritScope lock(&crit_);
209 feedback_observer_ = feedback_observer;
210 }
211
212 // Implements TransportFeedbackObserver.
213 void AddPacket(uint16_t sequence_number,
214 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700215 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100216 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
217 rtc::CritScope lock(&crit_);
218 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700219 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100220 }
221 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
222 RTC_DCHECK(network_thread_.CalledOnValidThread());
223 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700224 if (feedback_observer_)
225 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200226 }
227 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
228 RTC_NOTREACHED();
229 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100230 }
231
232 private:
233 rtc::CriticalSection crit_;
234 rtc::ThreadChecker thread_checker_;
235 rtc::ThreadChecker pacer_thread_;
236 rtc::ThreadChecker network_thread_;
237 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
238};
239
240class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
241 public:
242 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
243 pacer_thread_.DetachFromThread();
244 }
245
246 void SetSequenceNumberAllocator(
247 TransportSequenceNumberAllocator* seq_num_allocator) {
248 RTC_DCHECK(thread_checker_.CalledOnValidThread());
249 rtc::CritScope lock(&crit_);
250 seq_num_allocator_ = seq_num_allocator;
251 }
252
253 // Implements TransportSequenceNumberAllocator.
254 uint16_t AllocateSequenceNumber() override {
255 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
256 rtc::CritScope lock(&crit_);
257 if (!seq_num_allocator_)
258 return 0;
259 return seq_num_allocator_->AllocateSequenceNumber();
260 }
261
262 private:
263 rtc::CriticalSection crit_;
264 rtc::ThreadChecker thread_checker_;
265 rtc::ThreadChecker pacer_thread_;
266 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
267};
268
269class RtpPacketSenderProxy : public RtpPacketSender {
270 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800271 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100272
273 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
274 RTC_DCHECK(thread_checker_.CalledOnValidThread());
275 rtc::CritScope lock(&crit_);
276 rtp_packet_sender_ = rtp_packet_sender;
277 }
278
279 // Implements RtpPacketSender.
280 void InsertPacket(Priority priority,
281 uint32_t ssrc,
282 uint16_t sequence_number,
283 int64_t capture_time_ms,
284 size_t bytes,
285 bool retransmission) override {
286 rtc::CritScope lock(&crit_);
287 if (rtp_packet_sender_) {
288 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
289 capture_time_ms, bytes, retransmission);
290 }
291 }
292
293 private:
294 rtc::ThreadChecker thread_checker_;
295 rtc::CriticalSection crit_;
296 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
297};
298
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000299class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000300 public:
stefan7de8d642017-02-07 07:14:08 -0800301 explicit VoERtcpObserver(Channel* owner)
302 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000303 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000304
stefan7de8d642017-02-07 07:14:08 -0800305 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
306 rtc::CritScope lock(&crit_);
307 bandwidth_observer_ = bandwidth_observer;
308 }
309
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000310 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800311 rtc::CritScope lock(&crit_);
312 if (bandwidth_observer_) {
313 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
314 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000315 }
316
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000317 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
318 int64_t rtt,
319 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800320 {
321 rtc::CritScope lock(&crit_);
322 if (bandwidth_observer_) {
323 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
324 now_ms);
325 }
326 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000327 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
328 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
329 // report for VoiceEngine?
330 if (report_blocks.empty())
331 return;
332
333 int fraction_lost_aggregate = 0;
334 int total_number_of_packets = 0;
335
336 // If receiving multiple report blocks, calculate the weighted average based
337 // on the number of packets a report refers to.
338 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
339 block_it != report_blocks.end(); ++block_it) {
340 // Find the previous extended high sequence number for this remote SSRC,
341 // to calculate the number of RTP packets this report refers to. Ignore if
342 // we haven't seen this SSRC before.
343 std::map<uint32_t, uint32_t>::iterator seq_num_it =
344 extended_max_sequence_number_.find(block_it->sourceSSRC);
345 int number_of_packets = 0;
346 if (seq_num_it != extended_max_sequence_number_.end()) {
347 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
348 }
349 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
350 total_number_of_packets += number_of_packets;
351
352 extended_max_sequence_number_[block_it->sourceSSRC] =
353 block_it->extendedHighSeqNum;
354 }
355 int weighted_fraction_lost = 0;
356 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800357 weighted_fraction_lost =
358 (fraction_lost_aggregate + total_number_of_packets / 2) /
359 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000360 }
361 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000362 }
363
364 private:
365 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000366 // Maps remote side ssrc to extended highest sequence number received.
367 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800368 rtc::CriticalSection crit_;
369 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000370};
371
kwiberg55b97fe2016-01-28 05:22:45 -0800372int32_t Channel::SendData(FrameType frameType,
373 uint8_t payloadType,
374 uint32_t timeStamp,
375 const uint8_t* payloadData,
376 size_t payloadSize,
377 const RTPFragmentationHeader* fragmentation) {
378 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
379 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
380 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
381 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000382
kwiberg55b97fe2016-01-28 05:22:45 -0800383 if (_includeAudioLevelIndication) {
384 // Store current audio level in the RTP/RTCP module.
385 // The level will be used in combination with voice-activity state
386 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800387 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800388 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000389
kwiberg55b97fe2016-01-28 05:22:45 -0800390 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
391 // packetization.
392 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700393 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800394 (FrameType&)frameType, payloadType, timeStamp,
395 // Leaving the time when this frame was
396 // received from the capture device as
397 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700398 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800399 _engineStatisticsPtr->SetLastError(
400 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
401 "Channel::SendData() failed to send data to RTP/RTCP module");
402 return -1;
403 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
kwiberg55b97fe2016-01-28 05:22:45 -0800405 _lastLocalTimeStamp = timeStamp;
406 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000407
kwiberg55b97fe2016-01-28 05:22:45 -0800408 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000409}
410
kwiberg55b97fe2016-01-28 05:22:45 -0800411int32_t Channel::InFrameType(FrameType frame_type) {
412 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
413 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
kwiberg55b97fe2016-01-28 05:22:45 -0800415 rtc::CritScope cs(&_callbackCritSect);
416 _sendFrameType = (frame_type == kAudioFrameSpeech);
417 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000418}
419
stefan1d8a5062015-10-02 03:39:33 -0700420bool Channel::SendRtp(const uint8_t* data,
421 size_t len,
422 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800423 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
424 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000425
kwiberg55b97fe2016-01-28 05:22:45 -0800426 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000427
kwiberg55b97fe2016-01-28 05:22:45 -0800428 if (_transportPtr == NULL) {
429 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
430 "Channel::SendPacket() failed to send RTP packet due to"
431 " invalid transport object");
432 return false;
433 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
kwiberg55b97fe2016-01-28 05:22:45 -0800435 uint8_t* bufferToSendPtr = (uint8_t*)data;
436 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000437
kwiberg55b97fe2016-01-28 05:22:45 -0800438 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
439 std::string transport_name =
440 _externalTransport ? "external transport" : "WebRtc sockets";
441 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
442 "Channel::SendPacket() RTP transmission using %s failed",
443 transport_name.c_str());
444 return false;
445 }
446 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
kwiberg55b97fe2016-01-28 05:22:45 -0800449bool Channel::SendRtcp(const uint8_t* data, size_t len) {
450 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
451 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000452
kwiberg55b97fe2016-01-28 05:22:45 -0800453 rtc::CritScope cs(&_callbackCritSect);
454 if (_transportPtr == NULL) {
455 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
456 "Channel::SendRtcp() failed to send RTCP packet"
457 " due to invalid transport object");
458 return false;
459 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
kwiberg55b97fe2016-01-28 05:22:45 -0800461 uint8_t* bufferToSendPtr = (uint8_t*)data;
462 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000463
kwiberg55b97fe2016-01-28 05:22:45 -0800464 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
465 if (n < 0) {
466 std::string transport_name =
467 _externalTransport ? "external transport" : "WebRtc sockets";
468 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
469 "Channel::SendRtcp() transmission using %s failed",
470 transport_name.c_str());
471 return false;
472 }
473 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000474}
475
kwiberg55b97fe2016-01-28 05:22:45 -0800476void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
477 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
478 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000479
kwiberg55b97fe2016-01-28 05:22:45 -0800480 // Update ssrc so that NTP for AV sync can be updated.
481 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000482}
483
Peter Boströmac547a62015-09-17 23:03:57 +0200484void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
485 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
486 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
487 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000488}
489
Peter Boströmac547a62015-09-17 23:03:57 +0200490int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000491 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000492 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000493 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800494 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200495 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800496 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
497 "Channel::OnInitializeDecoder(payloadType=%d, "
498 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
499 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000500
kwiberg55b97fe2016-01-28 05:22:45 -0800501 CodecInst receiveCodec = {0};
502 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
kwiberg55b97fe2016-01-28 05:22:45 -0800504 receiveCodec.pltype = payloadType;
505 receiveCodec.plfreq = frequency;
506 receiveCodec.channels = channels;
507 receiveCodec.rate = rate;
508 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000509
kwiberg55b97fe2016-01-28 05:22:45 -0800510 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
511 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000512
kwiberg55b97fe2016-01-28 05:22:45 -0800513 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700514 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
515 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800516 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
517 "Channel::OnInitializeDecoder() invalid codec ("
518 "pt=%d, name=%s) received - 1",
519 payloadType, payloadName);
520 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
521 return -1;
522 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000523
kwiberg55b97fe2016-01-28 05:22:45 -0800524 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000525}
526
kwiberg55b97fe2016-01-28 05:22:45 -0800527int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
528 size_t payloadSize,
529 const WebRtcRTPHeader* rtpHeader) {
530 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
531 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
532 ","
533 " payloadType=%u, audioChannel=%" PRIuS ")",
534 payloadSize, rtpHeader->header.payloadType,
535 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000536
kwiberg55b97fe2016-01-28 05:22:45 -0800537 if (!channel_state_.Get().playing) {
538 // Avoid inserting into NetEQ when we are not playing. Count the
539 // packet as discarded.
540 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
541 "received packet is discarded since playing is not"
542 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000543 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800544 }
545
546 // Push the incoming payload (parsed and ready for decoding) into the ACM
547 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
548 0) {
549 _engineStatisticsPtr->SetLastError(
550 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
551 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
552 return -1;
553 }
554
kwiberg55b97fe2016-01-28 05:22:45 -0800555 int64_t round_trip_time = 0;
556 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
557 NULL);
558
559 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
560 if (!nack_list.empty()) {
561 // Can't use nack_list.data() since it's not supported by all
562 // compilers.
563 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
564 }
565 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000566}
567
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000568bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000569 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000570 RTPHeader header;
571 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
572 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
573 "IncomingPacket invalid RTP header");
574 return false;
575 }
576 header.payload_type_frequency =
577 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
578 if (header.payload_type_frequency < 0)
579 return false;
580 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
581}
582
henrik.lundin42dda502016-05-18 05:36:01 -0700583MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
584 int32_t id,
585 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700586 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800587 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700588 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800589 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700590 bool muted;
591 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
592 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800593 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
594 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
595 // In all likelihood, the audio in this frame is garbage. We return an
596 // error so that the audio mixer module doesn't add it to the mix. As
597 // a result, it won't be played out and the actions skipped here are
598 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700599 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800600 }
henrik.lundina89ab962016-05-18 08:52:45 -0700601
602 if (muted) {
603 // TODO(henrik.lundin): We should be able to do better than this. But we
604 // will have to go through all the cases below where the audio samples may
605 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800606 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700607 }
kwiberg55b97fe2016-01-28 05:22:45 -0800608
kwiberg55b97fe2016-01-28 05:22:45 -0800609 // Convert module ID to internal VoE channel ID
610 audioFrame->id_ = VoEChannelId(audioFrame->id_);
611 // Store speech type for dead-or-alive detection
612 _outputSpeechType = audioFrame->speech_type_;
613
614 ChannelState::State state = channel_state_.Get();
615
kwiberg55b97fe2016-01-28 05:22:45 -0800616 {
617 // Pass the audio buffers to an optional sink callback, before applying
618 // scaling/panning, as that applies to the mix operation.
619 // External recipients of the audio (e.g. via AudioTrack), will do their
620 // own mixing/dynamic processing.
621 rtc::CritScope cs(&_callbackCritSect);
622 if (audio_sink_) {
623 AudioSinkInterface::Data data(
624 &audioFrame->data_[0], audioFrame->samples_per_channel_,
625 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
626 audioFrame->timestamp_);
627 audio_sink_->OnData(data);
628 }
629 }
630
631 float output_gain = 1.0f;
632 float left_pan = 1.0f;
633 float right_pan = 1.0f;
634 {
635 rtc::CritScope cs(&volume_settings_critsect_);
636 output_gain = _outputGain;
637 left_pan = _panLeft;
638 right_pan = _panRight;
639 }
640
641 // Output volume scaling
642 if (output_gain < 0.99f || output_gain > 1.01f) {
643 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
644 }
645
646 // Scale left and/or right channel(s) if stereo and master balance is
647 // active
648
649 if (left_pan != 1.0f || right_pan != 1.0f) {
650 if (audioFrame->num_channels_ == 1) {
651 // Emulate stereo mode since panning is active.
652 // The mono signal is copied to both left and right channels here.
653 AudioFrameOperations::MonoToStereo(audioFrame);
654 }
655 // For true stereo mode (when we are receiving a stereo signal), no
656 // action is needed.
657
658 // Do the panning operation (the audio frame contains stereo at this
659 // stage)
660 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
661 }
662
663 // Mix decoded PCM output with file if file mixing is enabled
664 if (state.output_file_playing) {
665 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700666 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800667 }
668
kwiberg55b97fe2016-01-28 05:22:45 -0800669 // Record playout if enabled
670 {
671 rtc::CritScope cs(&_fileCritSect);
672
kwiberg5a25d952016-08-17 07:31:12 -0700673 if (_outputFileRecording && output_file_recorder_) {
674 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800675 }
676 }
677
678 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700679 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800680 _outputAudioLevel.ComputeLevel(*audioFrame);
681
682 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
683 // The first frame with a valid rtp timestamp.
684 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
685 }
686
687 if (capture_start_rtp_time_stamp_ >= 0) {
688 // audioFrame.timestamp_ should be valid from now on.
689
690 // Compute elapsed time.
691 int64_t unwrap_timestamp =
692 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
693 audioFrame->elapsed_time_ms_ =
694 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700695 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800696
niklase@google.com470e71d2011-07-07 08:21:25 +0000697 {
kwiberg55b97fe2016-01-28 05:22:45 -0800698 rtc::CritScope lock(&ts_stats_lock_);
699 // Compute ntp time.
700 audioFrame->ntp_time_ms_ =
701 ntp_estimator_.Estimate(audioFrame->timestamp_);
702 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
703 if (audioFrame->ntp_time_ms_ > 0) {
704 // Compute |capture_start_ntp_time_ms_| so that
705 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
706 capture_start_ntp_time_ms_ =
707 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000708 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000709 }
kwiberg55b97fe2016-01-28 05:22:45 -0800710 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
henrik.lundin42dda502016-05-18 05:36:01 -0700712 return muted ? MixerParticipant::AudioFrameInfo::kMuted
713 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000714}
715
aleloi6c278492016-10-20 14:24:39 -0700716AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
717 int sample_rate_hz,
718 AudioFrame* audio_frame) {
719 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700720
aleloi6c278492016-10-20 14:24:39 -0700721 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700722
723 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
724 FrameInfo new_audio_frame_info = FrameInfo::kError;
725 switch (frame_info) {
726 case MixerParticipant::AudioFrameInfo::kNormal:
727 new_audio_frame_info = FrameInfo::kNormal;
728 break;
729 case MixerParticipant::AudioFrameInfo::kMuted:
730 new_audio_frame_info = FrameInfo::kMuted;
731 break;
732 case MixerParticipant::AudioFrameInfo::kError:
733 new_audio_frame_info = FrameInfo::kError;
734 break;
735 }
aleloi6c278492016-10-20 14:24:39 -0700736 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700737}
738
kwiberg55b97fe2016-01-28 05:22:45 -0800739int32_t Channel::NeededFrequency(int32_t id) const {
740 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
741 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000742
kwiberg55b97fe2016-01-28 05:22:45 -0800743 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000744
kwiberg55b97fe2016-01-28 05:22:45 -0800745 // Determine highest needed receive frequency
746 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000747
kwiberg55b97fe2016-01-28 05:22:45 -0800748 // Return the bigger of playout and receive frequency in the ACM.
749 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
750 highestNeeded = audio_coding_->PlayoutFrequency();
751 } else {
752 highestNeeded = receiveFrequency;
753 }
754
755 // Special case, if we're playing a file on the playout side
756 // we take that frequency into consideration as well
757 // This is not needed on sending side, since the codec will
758 // limit the spectrum anyway.
759 if (channel_state_.Get().output_file_playing) {
760 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700761 if (output_file_player_) {
762 if (output_file_player_->Frequency() > highestNeeded) {
763 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800764 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 }
kwiberg55b97fe2016-01-28 05:22:45 -0800766 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000767
kwiberg55b97fe2016-01-28 05:22:45 -0800768 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000769}
770
ossu5f7cfa52016-05-30 08:11:28 -0700771int32_t Channel::CreateChannel(
772 Channel*& channel,
773 int32_t channelId,
774 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700775 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800776 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
777 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
778 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000779
solenberg88499ec2016-09-07 07:34:41 -0700780 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800781 if (channel == NULL) {
782 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
783 "Channel::CreateChannel() unable to allocate memory for"
784 " channel");
785 return -1;
786 }
787 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000788}
789
kwiberg55b97fe2016-01-28 05:22:45 -0800790void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
791 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
792 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
793 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000794
kwiberg55b97fe2016-01-28 05:22:45 -0800795 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000796}
797
kwiberg55b97fe2016-01-28 05:22:45 -0800798void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
799 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
800 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
801 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802
kwiberg55b97fe2016-01-28 05:22:45 -0800803 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
kwiberg55b97fe2016-01-28 05:22:45 -0800806void Channel::PlayFileEnded(int32_t id) {
807 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
808 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000809
kwiberg55b97fe2016-01-28 05:22:45 -0800810 if (id == _inputFilePlayerId) {
811 channel_state_.SetInputFilePlaying(false);
812 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
813 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000814 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800815 } else if (id == _outputFilePlayerId) {
816 channel_state_.SetOutputFilePlaying(false);
817 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
818 "Channel::PlayFileEnded() => output file player module is"
819 " shutdown");
820 }
821}
822
823void Channel::RecordFileEnded(int32_t id) {
824 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
825 "Channel::RecordFileEnded(id=%d)", id);
826
827 assert(id == _outputFileRecorderId);
828
829 rtc::CritScope cs(&_fileCritSect);
830
831 _outputFileRecording = false;
832 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
833 "Channel::RecordFileEnded() => output file recorder module is"
834 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000835}
836
pbos@webrtc.org92135212013-05-14 08:31:39 +0000837Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000838 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700839 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800840 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100841 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700842 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800843 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100844 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800845 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100846 rtp_receive_statistics_(
847 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
848 rtp_receiver_(
849 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100850 this,
851 this,
852 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700853 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 _outputAudioLevel(),
855 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100856 // Avoid conflict with other channels by adding 1024 - 1026,
857 // won't use as much as 1024 channels.
858 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
859 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
860 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
861 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100862 _timeStamp(0), // This is just an offset, RTP module will add it's own
863 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100864 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100867 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100868 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
869 capture_start_rtp_time_stamp_(-1),
870 capture_start_ntp_time_ms_(-1),
871 _engineStatisticsPtr(NULL),
872 _outputMixerPtr(NULL),
873 _transmitMixerPtr(NULL),
874 _moduleProcessThreadPtr(NULL),
875 _audioDeviceModulePtr(NULL),
876 _voiceEngineObserverPtr(NULL),
877 _callbackCritSectPtr(NULL),
878 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100879 _sendFrameType(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100880 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700881 input_mute_(false),
882 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 _panLeft(1.0f),
884 _panRight(1.0f),
885 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100886 _lastLocalTimeStamp(0),
887 _lastPayloadType(0),
888 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800889 transport_overhead_per_packet_(0),
890 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100892 restored_packet_in_use_(false),
893 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100894 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700895 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800896 feedback_observer_proxy_(new TransportFeedbackProxy()),
897 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700898 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200899 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
900 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800901 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800902 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
903 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700904 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800905 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700906 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800907 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200908
kwiberg55b97fe2016-01-28 05:22:45 -0800909 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000910
kwiberg55b97fe2016-01-28 05:22:45 -0800911 RtpRtcp::Configuration configuration;
912 configuration.audio = true;
913 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800914 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800915 configuration.receive_statistics = rtp_receive_statistics_.get();
916 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800917 if (pacing_enabled_) {
918 configuration.paced_sender = rtp_packet_sender_proxy_.get();
919 configuration.transport_sequence_number_allocator =
920 seq_num_allocator_proxy_.get();
921 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
922 }
ivoc14d5dbe2016-07-04 07:06:55 -0700923 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800924 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200925 configuration.retransmission_rate_limiter =
926 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000927
kwiberg55b97fe2016-01-28 05:22:45 -0800928 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100929 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000930}
931
kwiberg55b97fe2016-01-28 05:22:45 -0800932Channel::~Channel() {
933 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
934 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
935 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000936
kwiberg55b97fe2016-01-28 05:22:45 -0800937 StopSend();
938 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000939
kwiberg55b97fe2016-01-28 05:22:45 -0800940 {
941 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700942 if (input_file_player_) {
943 input_file_player_->RegisterModuleFileCallback(NULL);
944 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000945 }
kwiberg5a25d952016-08-17 07:31:12 -0700946 if (output_file_player_) {
947 output_file_player_->RegisterModuleFileCallback(NULL);
948 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800949 }
kwiberg5a25d952016-08-17 07:31:12 -0700950 if (output_file_recorder_) {
951 output_file_recorder_->RegisterModuleFileCallback(NULL);
952 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800953 }
954 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000955
kwiberg55b97fe2016-01-28 05:22:45 -0800956 // The order to safely shutdown modules in a channel is:
957 // 1. De-register callbacks in modules
958 // 2. De-register modules in process thread
959 // 3. Destroy modules
960 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
961 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
962 "~Channel() failed to de-register transport callback"
963 " (Audio coding module)");
964 }
965 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
966 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
967 "~Channel() failed to de-register VAD callback"
968 " (Audio coding module)");
969 }
970 // De-register modules in process thread
971 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000972
kwiberg55b97fe2016-01-28 05:22:45 -0800973 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000974}
975
kwiberg55b97fe2016-01-28 05:22:45 -0800976int32_t Channel::Init() {
977 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
978 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000979
kwiberg55b97fe2016-01-28 05:22:45 -0800980 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000981
kwiberg55b97fe2016-01-28 05:22:45 -0800982 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000983
kwiberg55b97fe2016-01-28 05:22:45 -0800984 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
985 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
986 "Channel::Init() must call SetEngineInformation() first");
987 return -1;
988 }
989
990 // --- Add modules to process thread (for periodic schedulation)
991
992 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
993
994 // --- ACM initialization
995
996 if (audio_coding_->InitializeReceiver() == -1) {
997 _engineStatisticsPtr->SetLastError(
998 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
999 "Channel::Init() unable to initialize the ACM - 1");
1000 return -1;
1001 }
1002
1003 // --- RTP/RTCP module initialization
1004
1005 // Ensure that RTCP is enabled by default for the created channel.
1006 // Note that, the module will keep generating RTCP until it is explicitly
1007 // disabled by the user.
1008 // After StopListen (when no sockets exists), RTCP packets will no longer
1009 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001010 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001011 // RTCP is enabled by default.
1012 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1013 // --- Register all permanent callbacks
1014 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1015 (audio_coding_->RegisterVADCallback(this) == -1);
1016
1017 if (fail) {
1018 _engineStatisticsPtr->SetLastError(
1019 VE_CANNOT_INIT_CHANNEL, kTraceError,
1020 "Channel::Init() callbacks not registered");
1021 return -1;
1022 }
1023
1024 // --- Register all supported codecs to the receiving side of the
1025 // RTP/RTCP module
1026
1027 CodecInst codec;
1028 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1029
1030 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1031 // Open up the RTP/RTCP receiver for all supported codecs
1032 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001033 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001034 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1035 "Channel::Init() unable to register %s "
1036 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1037 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1038 codec.rate);
1039 } else {
1040 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1041 "Channel::Init() %s (%d/%d/%" PRIuS
1042 "/%d) has been "
1043 "added to the RTP/RTCP receiver",
1044 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1045 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001046 }
1047
kwiberg55b97fe2016-01-28 05:22:45 -08001048 // Ensure that PCMU is used as default codec on the sending side
1049 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1050 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 }
1052
kwiberg55b97fe2016-01-28 05:22:45 -08001053 // Register default PT for outband 'telephone-event'
1054 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001055 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001056 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1057 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001058 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1059 "Channel::Init() failed to register outband "
1060 "'telephone-event' (%d/%d) correctly",
1061 codec.pltype, codec.plfreq);
1062 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001063 }
1064
kwiberg55b97fe2016-01-28 05:22:45 -08001065 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001066 if (!codec_manager_.RegisterEncoder(codec) ||
1067 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001068 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1069 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001070 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001071 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1072 "Channel::Init() failed to register CN (%d/%d) "
1073 "correctly - 1",
1074 codec.pltype, codec.plfreq);
1075 }
1076 }
kwiberg55b97fe2016-01-28 05:22:45 -08001077 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001078
kwiberg55b97fe2016-01-28 05:22:45 -08001079 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001080}
1081
kwiberg55b97fe2016-01-28 05:22:45 -08001082int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1083 OutputMixer& outputMixer,
1084 voe::TransmitMixer& transmitMixer,
1085 ProcessThread& moduleProcessThread,
1086 AudioDeviceModule& audioDeviceModule,
1087 VoiceEngineObserver* voiceEngineObserver,
1088 rtc::CriticalSection* callbackCritSect) {
1089 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1090 "Channel::SetEngineInformation()");
1091 _engineStatisticsPtr = &engineStatistics;
1092 _outputMixerPtr = &outputMixer;
1093 _transmitMixerPtr = &transmitMixer,
1094 _moduleProcessThreadPtr = &moduleProcessThread;
1095 _audioDeviceModulePtr = &audioDeviceModule;
1096 _voiceEngineObserverPtr = voiceEngineObserver;
1097 _callbackCritSectPtr = callbackCritSect;
1098 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001099}
1100
kwiberg55b97fe2016-01-28 05:22:45 -08001101int32_t Channel::UpdateLocalTimeStamp() {
1102 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1103 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001104}
1105
kwibergb7f89d62016-02-17 10:04:18 -08001106void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001107 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001108 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001109}
1110
ossu29b1a8d2016-06-13 07:34:51 -07001111const rtc::scoped_refptr<AudioDecoderFactory>&
1112Channel::GetAudioDecoderFactory() const {
1113 return decoder_factory_;
1114}
1115
kwiberg55b97fe2016-01-28 05:22:45 -08001116int32_t Channel::StartPlayout() {
1117 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1118 "Channel::StartPlayout()");
1119 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001120 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001121 }
1122
solenberge374e012017-02-14 04:55:00 -08001123 // Add participant as candidates for mixing.
1124 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1125 _engineStatisticsPtr->SetLastError(
1126 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1127 "StartPlayout() failed to add participant to mixer");
1128 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001129 }
1130
1131 channel_state_.SetPlaying(true);
1132 if (RegisterFilePlayingToMixer() != 0)
1133 return -1;
1134
1135 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001136}
1137
kwiberg55b97fe2016-01-28 05:22:45 -08001138int32_t Channel::StopPlayout() {
1139 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1140 "Channel::StopPlayout()");
1141 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001143 }
1144
solenberge374e012017-02-14 04:55:00 -08001145 // Remove participant as candidates for mixing
1146 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1147 _engineStatisticsPtr->SetLastError(
1148 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1149 "StopPlayout() failed to remove participant from mixer");
1150 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001151 }
1152
1153 channel_state_.SetPlaying(false);
1154 _outputAudioLevel.Clear();
1155
1156 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001157}
1158
kwiberg55b97fe2016-01-28 05:22:45 -08001159int32_t Channel::StartSend() {
1160 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1161 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001162 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001163 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001164 }
1165 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001166
solenberg08b19df2017-02-15 00:42:31 -08001167 // Resume the previous sequence number which was reset by StopSend(). This
1168 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1169 if (send_sequence_number_) {
1170 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1171 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001172 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001173 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1174 _engineStatisticsPtr->SetLastError(
1175 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1176 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001177 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001178 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001179 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001180 return -1;
1181 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001182
kwiberg55b97fe2016-01-28 05:22:45 -08001183 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001184}
1185
kwiberg55b97fe2016-01-28 05:22:45 -08001186int32_t Channel::StopSend() {
1187 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1188 "Channel::StopSend()");
1189 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001190 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001191 }
1192 channel_state_.SetSending(false);
1193
1194 // Store the sequence number to be able to pick up the same sequence for
1195 // the next StartSend(). This is needed for restarting device, otherwise
1196 // it might cause libSRTP to complain about packets being replayed.
1197 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1198 // CL is landed. See issue
1199 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1200 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1201
1202 // Reset sending SSRC and sequence number and triggers direct transmission
1203 // of RTCP BYE
1204 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1205 _engineStatisticsPtr->SetLastError(
1206 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1207 "StartSend() RTP/RTCP failed to stop sending");
1208 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001209 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001210
1211 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001212}
1213
kwiberg55b97fe2016-01-28 05:22:45 -08001214int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1215 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1216 "Channel::RegisterVoiceEngineObserver()");
1217 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001218
kwiberg55b97fe2016-01-28 05:22:45 -08001219 if (_voiceEngineObserverPtr) {
1220 _engineStatisticsPtr->SetLastError(
1221 VE_INVALID_OPERATION, kTraceError,
1222 "RegisterVoiceEngineObserver() observer already enabled");
1223 return -1;
1224 }
1225 _voiceEngineObserverPtr = &observer;
1226 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001227}
1228
kwiberg55b97fe2016-01-28 05:22:45 -08001229int32_t Channel::DeRegisterVoiceEngineObserver() {
1230 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1231 "Channel::DeRegisterVoiceEngineObserver()");
1232 rtc::CritScope cs(&_callbackCritSect);
1233
1234 if (!_voiceEngineObserverPtr) {
1235 _engineStatisticsPtr->SetLastError(
1236 VE_INVALID_OPERATION, kTraceWarning,
1237 "DeRegisterVoiceEngineObserver() observer already disabled");
1238 return 0;
1239 }
1240 _voiceEngineObserverPtr = NULL;
1241 return 0;
1242}
1243
1244int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001245 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001246 if (send_codec) {
1247 codec = *send_codec;
1248 return 0;
1249 }
1250 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001251}
1252
kwiberg55b97fe2016-01-28 05:22:45 -08001253int32_t Channel::GetRecCodec(CodecInst& codec) {
1254 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001255}
1256
kwiberg55b97fe2016-01-28 05:22:45 -08001257int32_t Channel::SetSendCodec(const CodecInst& codec) {
1258 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1259 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001260
kwibergc8d071e2016-04-06 12:22:38 -07001261 if (!codec_manager_.RegisterEncoder(codec) ||
1262 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001263 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1264 "SetSendCodec() failed to register codec to ACM");
1265 return -1;
1266 }
1267
1268 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1269 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1270 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1271 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1272 "SetSendCodec() failed to register codec to"
1273 " RTP/RTCP module");
1274 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001275 }
kwiberg55b97fe2016-01-28 05:22:45 -08001276 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001277
kwiberg55b97fe2016-01-28 05:22:45 -08001278 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001279}
1280
minyue78b4d562016-11-30 04:47:39 -08001281void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001282 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1283 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001284 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001285 if (*encoder) {
1286 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001287 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001288 }
1289 });
michaelt566d8202017-01-12 10:17:38 -08001290 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001291}
1292
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001293void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001294 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1295 if (*encoder)
1296 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1297 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001298}
1299
kwiberg55b97fe2016-01-28 05:22:45 -08001300int32_t Channel::SetVADStatus(bool enableVAD,
1301 ACMVADMode mode,
1302 bool disableDTX) {
1303 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1304 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001305 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1306 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1307 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001308 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1309 kTraceError,
1310 "SetVADStatus() failed to set VAD");
1311 return -1;
1312 }
1313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001314}
1315
kwiberg55b97fe2016-01-28 05:22:45 -08001316int32_t Channel::GetVADStatus(bool& enabledVAD,
1317 ACMVADMode& mode,
1318 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001319 const auto* params = codec_manager_.GetStackParams();
1320 enabledVAD = params->use_cng;
1321 mode = params->vad_mode;
1322 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001323 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001324}
1325
kwiberg55b97fe2016-01-28 05:22:45 -08001326int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001327 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1328}
1329
1330int32_t Channel::SetRecPayloadType(int payload_type,
1331 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001332 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1333 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001334
kwiberg55b97fe2016-01-28 05:22:45 -08001335 if (channel_state_.Get().playing) {
1336 _engineStatisticsPtr->SetLastError(
1337 VE_ALREADY_PLAYING, kTraceError,
1338 "SetRecPayloadType() unable to set PT while playing");
1339 return -1;
1340 }
kwiberg55b97fe2016-01-28 05:22:45 -08001341
kwibergd32bf752017-01-19 07:03:59 -08001342 const CodecInst codec = [&] {
1343 CodecInst c = SdpToCodecInst(payload_type, format);
1344
1345 // Bug 6986: Emulate an old bug that caused us to always choose to decode
1346 // Opus in stereo. To be able to remove this, we first need to fix the
1347 // other half of bug 6986, which is about losing the Opus "stereo"
1348 // parameter.
1349 // TODO(kwiberg): Remove this special case, a.k.a. fix bug 6986.
1350 if (STR_CASE_CMP(codec.plname, "opus") == 0) {
1351 c.channels = 2;
1352 }
1353
1354 return c;
1355 }();
1356
1357 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001358 // De-register the selected codec (RTP/RTCP module and ACM)
1359
1360 int8_t pltype(-1);
1361 CodecInst rxCodec = codec;
1362
1363 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001364 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001365 rxCodec.pltype = pltype;
1366
1367 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1368 _engineStatisticsPtr->SetLastError(
1369 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1370 "SetRecPayloadType() RTP/RTCP-module deregistration "
1371 "failed");
1372 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001373 }
kwiberg55b97fe2016-01-28 05:22:45 -08001374 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1375 _engineStatisticsPtr->SetLastError(
1376 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1377 "SetRecPayloadType() ACM deregistration failed - 1");
1378 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001379 }
kwiberg55b97fe2016-01-28 05:22:45 -08001380 return 0;
1381 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001382
magjed56124bd2016-11-24 09:34:46 -08001383 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001384 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001385 // TODO(kwiberg): Retrying is probably not necessary, since
1386 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001387 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001388 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001389 _engineStatisticsPtr->SetLastError(
1390 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1391 "SetRecPayloadType() RTP/RTCP-module registration failed");
1392 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001393 }
kwiberg55b97fe2016-01-28 05:22:45 -08001394 }
kwibergd32bf752017-01-19 07:03:59 -08001395 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1396 audio_coding_->UnregisterReceiveCodec(payload_type);
1397 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001398 _engineStatisticsPtr->SetLastError(
1399 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1400 "SetRecPayloadType() ACM registration failed - 1");
1401 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001402 }
kwiberg55b97fe2016-01-28 05:22:45 -08001403 }
1404 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001405}
1406
kwiberg55b97fe2016-01-28 05:22:45 -08001407int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1408 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001409 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001410 _engineStatisticsPtr->SetLastError(
1411 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1412 "GetRecPayloadType() failed to retrieve RX payload type");
1413 return -1;
1414 }
1415 codec.pltype = payloadType;
1416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001417}
1418
kwiberg55b97fe2016-01-28 05:22:45 -08001419int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1420 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1421 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001422
kwiberg55b97fe2016-01-28 05:22:45 -08001423 CodecInst codec;
1424 int32_t samplingFreqHz(-1);
1425 const size_t kMono = 1;
1426 if (frequency == kFreq32000Hz)
1427 samplingFreqHz = 32000;
1428 else if (frequency == kFreq16000Hz)
1429 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001430
kwiberg55b97fe2016-01-28 05:22:45 -08001431 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1432 _engineStatisticsPtr->SetLastError(
1433 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1434 "SetSendCNPayloadType() failed to retrieve default CN codec "
1435 "settings");
1436 return -1;
1437 }
1438
1439 // Modify the payload type (must be set to dynamic range)
1440 codec.pltype = type;
1441
kwibergc8d071e2016-04-06 12:22:38 -07001442 if (!codec_manager_.RegisterEncoder(codec) ||
1443 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001444 _engineStatisticsPtr->SetLastError(
1445 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1446 "SetSendCNPayloadType() failed to register CN to ACM");
1447 return -1;
1448 }
1449
1450 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1451 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1452 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1453 _engineStatisticsPtr->SetLastError(
1454 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1455 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1456 "module");
1457 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001458 }
kwiberg55b97fe2016-01-28 05:22:45 -08001459 }
1460 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001461}
1462
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001463int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001465 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001466
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001467 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001468 _engineStatisticsPtr->SetLastError(
1469 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001470 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001471 return -1;
1472 }
1473 return 0;
1474}
1475
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001476int Channel::SetOpusDtx(bool enable_dtx) {
1477 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1478 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001479 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001480 : audio_coding_->DisableOpusDtx();
1481 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001482 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1483 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001484 return -1;
1485 }
1486 return 0;
1487}
1488
ivoc85228d62016-07-27 04:53:47 -07001489int Channel::GetOpusDtx(bool* enabled) {
1490 int success = -1;
1491 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1492 if (encoder) {
1493 *enabled = encoder->GetDtx();
1494 success = 0;
1495 }
1496 });
1497 return success;
1498}
1499
minyue7e304322016-10-12 05:00:55 -07001500bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1501 bool success = false;
1502 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1503 if (*encoder) {
1504 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001505 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001506 }
1507 });
1508 return success;
1509}
1510
1511void Channel::DisableAudioNetworkAdaptor() {
1512 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1513 if (*encoder)
1514 (*encoder)->DisableAudioNetworkAdaptor();
1515 });
1516}
1517
1518void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1519 int max_frame_length_ms) {
1520 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1521 if (*encoder) {
1522 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1523 max_frame_length_ms);
1524 }
1525 });
1526}
1527
mflodman3d7db262016-04-29 00:57:13 -07001528int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001529 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001530 "Channel::RegisterExternalTransport()");
1531
kwiberg55b97fe2016-01-28 05:22:45 -08001532 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001533 if (_externalTransport) {
1534 _engineStatisticsPtr->SetLastError(
1535 VE_INVALID_OPERATION, kTraceError,
1536 "RegisterExternalTransport() external transport already enabled");
1537 return -1;
1538 }
1539 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001540 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001541 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001542}
1543
kwiberg55b97fe2016-01-28 05:22:45 -08001544int32_t Channel::DeRegisterExternalTransport() {
1545 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1546 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001547
kwiberg55b97fe2016-01-28 05:22:45 -08001548 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001549 if (_transportPtr) {
1550 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1551 "DeRegisterExternalTransport() all transport is disabled");
1552 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001553 _engineStatisticsPtr->SetLastError(
1554 VE_INVALID_OPERATION, kTraceWarning,
1555 "DeRegisterExternalTransport() external transport already "
1556 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001557 }
1558 _externalTransport = false;
1559 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001560 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001561}
1562
mflodman3d7db262016-04-29 00:57:13 -07001563int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001564 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001565 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001566 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001567 "Channel::ReceivedRTPPacket()");
1568
1569 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001570 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001571
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001572 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001573 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1574 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1575 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001576 return -1;
1577 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001578 header.payload_type_frequency =
1579 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001580 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001581 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001582 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001583 rtp_receive_statistics_->IncomingPacket(
1584 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001585 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001586
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001587 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001588}
1589
1590bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001591 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001592 const RTPHeader& header,
1593 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001594 if (rtp_payload_registry_->IsRtx(header)) {
1595 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001596 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001597 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001598 assert(packet_length >= header.headerLength);
1599 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001600 PayloadUnion payload_specific;
1601 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001602 &payload_specific)) {
1603 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001604 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001605 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1606 payload_specific, in_order);
1607}
1608
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001609bool Channel::HandleRtxPacket(const uint8_t* packet,
1610 size_t packet_length,
1611 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001612 if (!rtp_payload_registry_->IsRtx(header))
1613 return false;
1614
1615 // Remove the RTX header and parse the original RTP header.
1616 if (packet_length < header.headerLength)
1617 return false;
1618 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1619 return false;
1620 if (restored_packet_in_use_) {
1621 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1622 "Multiple RTX headers detected, dropping packet");
1623 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001624 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001625 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001626 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1627 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001628 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1629 "Incoming RTX packet: invalid RTP header");
1630 return false;
1631 }
1632 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001633 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001634 restored_packet_in_use_ = false;
1635 return ret;
1636}
1637
1638bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1639 StreamStatistician* statistician =
1640 rtp_receive_statistics_->GetStatistician(header.ssrc);
1641 if (!statistician)
1642 return false;
1643 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001644}
1645
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001646bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1647 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001648 // Retransmissions are handled separately if RTX is enabled.
1649 if (rtp_payload_registry_->RtxEnabled())
1650 return false;
1651 StreamStatistician* statistician =
1652 rtp_receive_statistics_->GetStatistician(header.ssrc);
1653 if (!statistician)
1654 return false;
1655 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001656 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001657 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001658 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001659}
1660
mflodman3d7db262016-04-29 00:57:13 -07001661int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001662 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001663 "Channel::ReceivedRTCPPacket()");
1664 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001665 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001666
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001667 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001668 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001669 _engineStatisticsPtr->SetLastError(
1670 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1671 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1672 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001673
Minyue2013aec2015-05-13 14:14:42 +02001674 int64_t rtt = GetRTT(true);
1675 if (rtt == 0) {
1676 // Waiting for valid RTT.
1677 return 0;
1678 }
Erik Språng737336d2016-07-29 12:59:36 +02001679
1680 int64_t nack_window_ms = rtt;
1681 if (nack_window_ms < kMinRetransmissionWindowMs) {
1682 nack_window_ms = kMinRetransmissionWindowMs;
1683 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1684 nack_window_ms = kMaxRetransmissionWindowMs;
1685 }
1686 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1687
minyue7e304322016-10-12 05:00:55 -07001688 // Invoke audio encoders OnReceivedRtt().
1689 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1690 if (*encoder)
1691 (*encoder)->OnReceivedRtt(rtt);
1692 });
1693
Minyue2013aec2015-05-13 14:14:42 +02001694 uint32_t ntp_secs = 0;
1695 uint32_t ntp_frac = 0;
1696 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001697 if (0 !=
1698 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1699 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001700 // Waiting for RTCP.
1701 return 0;
1702 }
1703
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001704 {
tommi31fc21f2016-01-21 10:37:37 -08001705 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001706 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001707 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001708 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001709}
1710
niklase@google.com470e71d2011-07-07 08:21:25 +00001711int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001712 bool loop,
1713 FileFormats format,
1714 int startPosition,
1715 float volumeScaling,
1716 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001717 const CodecInst* codecInst) {
1718 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1719 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1720 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1721 "stopPosition=%d)",
1722 fileName, loop, format, volumeScaling, startPosition,
1723 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001724
kwiberg55b97fe2016-01-28 05:22:45 -08001725 if (channel_state_.Get().output_file_playing) {
1726 _engineStatisticsPtr->SetLastError(
1727 VE_ALREADY_PLAYING, kTraceError,
1728 "StartPlayingFileLocally() is already playing");
1729 return -1;
1730 }
1731
1732 {
1733 rtc::CritScope cs(&_fileCritSect);
1734
kwiberg5a25d952016-08-17 07:31:12 -07001735 if (output_file_player_) {
1736 output_file_player_->RegisterModuleFileCallback(NULL);
1737 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001738 }
1739
kwiberg5b356f42016-09-08 04:32:33 -07001740 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001741 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001742
kwiberg5a25d952016-08-17 07:31:12 -07001743 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001744 _engineStatisticsPtr->SetLastError(
1745 VE_INVALID_ARGUMENT, kTraceError,
1746 "StartPlayingFileLocally() filePlayer format is not correct");
1747 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001748 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001749
kwiberg55b97fe2016-01-28 05:22:45 -08001750 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001751
kwiberg5a25d952016-08-17 07:31:12 -07001752 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001753 fileName, loop, startPosition, volumeScaling, notificationTime,
1754 stopPosition, (const CodecInst*)codecInst) != 0) {
1755 _engineStatisticsPtr->SetLastError(
1756 VE_BAD_FILE, kTraceError,
1757 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001758 output_file_player_->StopPlayingFile();
1759 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001760 return -1;
1761 }
kwiberg5a25d952016-08-17 07:31:12 -07001762 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001763 channel_state_.SetOutputFilePlaying(true);
1764 }
1765
1766 if (RegisterFilePlayingToMixer() != 0)
1767 return -1;
1768
1769 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001770}
1771
1772int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001773 FileFormats format,
1774 int startPosition,
1775 float volumeScaling,
1776 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001777 const CodecInst* codecInst) {
1778 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1779 "Channel::StartPlayingFileLocally(format=%d,"
1780 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1781 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001782
kwiberg55b97fe2016-01-28 05:22:45 -08001783 if (stream == NULL) {
1784 _engineStatisticsPtr->SetLastError(
1785 VE_BAD_FILE, kTraceError,
1786 "StartPlayingFileLocally() NULL as input stream");
1787 return -1;
1788 }
1789
1790 if (channel_state_.Get().output_file_playing) {
1791 _engineStatisticsPtr->SetLastError(
1792 VE_ALREADY_PLAYING, kTraceError,
1793 "StartPlayingFileLocally() is already playing");
1794 return -1;
1795 }
1796
1797 {
1798 rtc::CritScope cs(&_fileCritSect);
1799
1800 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001801 if (output_file_player_) {
1802 output_file_player_->RegisterModuleFileCallback(NULL);
1803 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001804 }
1805
kwiberg55b97fe2016-01-28 05:22:45 -08001806 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001807 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001808 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001809
kwiberg5a25d952016-08-17 07:31:12 -07001810 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001811 _engineStatisticsPtr->SetLastError(
1812 VE_INVALID_ARGUMENT, kTraceError,
1813 "StartPlayingFileLocally() filePlayer format isnot correct");
1814 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001815 }
1816
kwiberg55b97fe2016-01-28 05:22:45 -08001817 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001818
kwiberg4ec01d92016-08-22 08:43:54 -07001819 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001820 volumeScaling, notificationTime,
1821 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001822 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1823 "StartPlayingFile() failed to "
1824 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001825 output_file_player_->StopPlayingFile();
1826 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001827 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001828 }
kwiberg5a25d952016-08-17 07:31:12 -07001829 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001830 channel_state_.SetOutputFilePlaying(true);
1831 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001832
kwiberg55b97fe2016-01-28 05:22:45 -08001833 if (RegisterFilePlayingToMixer() != 0)
1834 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001835
kwiberg55b97fe2016-01-28 05:22:45 -08001836 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001837}
1838
kwiberg55b97fe2016-01-28 05:22:45 -08001839int Channel::StopPlayingFileLocally() {
1840 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1841 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001842
kwiberg55b97fe2016-01-28 05:22:45 -08001843 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001844 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001845 }
1846
1847 {
1848 rtc::CritScope cs(&_fileCritSect);
1849
kwiberg5a25d952016-08-17 07:31:12 -07001850 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001851 _engineStatisticsPtr->SetLastError(
1852 VE_STOP_RECORDING_FAILED, kTraceError,
1853 "StopPlayingFile() could not stop playing");
1854 return -1;
1855 }
kwiberg5a25d952016-08-17 07:31:12 -07001856 output_file_player_->RegisterModuleFileCallback(NULL);
1857 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001858 channel_state_.SetOutputFilePlaying(false);
1859 }
1860 // _fileCritSect cannot be taken while calling
1861 // SetAnonymousMixibilityStatus. Refer to comments in
1862 // StartPlayingFileLocally(const char* ...) for more details.
1863 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1864 _engineStatisticsPtr->SetLastError(
1865 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1866 "StopPlayingFile() failed to stop participant from playing as"
1867 "file in the mixer");
1868 return -1;
1869 }
1870
1871 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001872}
1873
kwiberg55b97fe2016-01-28 05:22:45 -08001874int Channel::IsPlayingFileLocally() const {
1875 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001876}
1877
kwiberg55b97fe2016-01-28 05:22:45 -08001878int Channel::RegisterFilePlayingToMixer() {
1879 // Return success for not registering for file playing to mixer if:
1880 // 1. playing file before playout is started on that channel.
1881 // 2. starting playout without file playing on that channel.
1882 if (!channel_state_.Get().playing ||
1883 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001884 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001885 }
1886
1887 // |_fileCritSect| cannot be taken while calling
1888 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1889 // frames can be pulled by the mixer. Since the frames are generated from
1890 // the file, _fileCritSect will be taken. This would result in a deadlock.
1891 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1892 channel_state_.SetOutputFilePlaying(false);
1893 rtc::CritScope cs(&_fileCritSect);
1894 _engineStatisticsPtr->SetLastError(
1895 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1896 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001897 output_file_player_->StopPlayingFile();
1898 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001899 return -1;
1900 }
1901
1902 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001903}
1904
niklase@google.com470e71d2011-07-07 08:21:25 +00001905int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001906 bool loop,
1907 FileFormats format,
1908 int startPosition,
1909 float volumeScaling,
1910 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001911 const CodecInst* codecInst) {
1912 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1913 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1914 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1915 "stopPosition=%d)",
1916 fileName, loop, format, volumeScaling, startPosition,
1917 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001918
kwiberg55b97fe2016-01-28 05:22:45 -08001919 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001920
kwiberg55b97fe2016-01-28 05:22:45 -08001921 if (channel_state_.Get().input_file_playing) {
1922 _engineStatisticsPtr->SetLastError(
1923 VE_ALREADY_PLAYING, kTraceWarning,
1924 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001925 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001926 }
1927
1928 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001929 if (input_file_player_) {
1930 input_file_player_->RegisterModuleFileCallback(NULL);
1931 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001932 }
1933
1934 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001935 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001936 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001937
kwiberg5a25d952016-08-17 07:31:12 -07001938 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001939 _engineStatisticsPtr->SetLastError(
1940 VE_INVALID_ARGUMENT, kTraceError,
1941 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1942 return -1;
1943 }
1944
1945 const uint32_t notificationTime(0);
1946
kwiberg5a25d952016-08-17 07:31:12 -07001947 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001948 fileName, loop, startPosition, volumeScaling, notificationTime,
1949 stopPosition, (const CodecInst*)codecInst) != 0) {
1950 _engineStatisticsPtr->SetLastError(
1951 VE_BAD_FILE, kTraceError,
1952 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001953 input_file_player_->StopPlayingFile();
1954 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001955 return -1;
1956 }
kwiberg5a25d952016-08-17 07:31:12 -07001957 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001958 channel_state_.SetInputFilePlaying(true);
1959
1960 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001961}
1962
1963int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001964 FileFormats format,
1965 int startPosition,
1966 float volumeScaling,
1967 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001968 const CodecInst* codecInst) {
1969 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1970 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1971 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1972 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001973
kwiberg55b97fe2016-01-28 05:22:45 -08001974 if (stream == NULL) {
1975 _engineStatisticsPtr->SetLastError(
1976 VE_BAD_FILE, kTraceError,
1977 "StartPlayingFileAsMicrophone NULL as input stream");
1978 return -1;
1979 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001980
kwiberg55b97fe2016-01-28 05:22:45 -08001981 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001982
kwiberg55b97fe2016-01-28 05:22:45 -08001983 if (channel_state_.Get().input_file_playing) {
1984 _engineStatisticsPtr->SetLastError(
1985 VE_ALREADY_PLAYING, kTraceWarning,
1986 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001987 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001988 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001989
kwiberg55b97fe2016-01-28 05:22:45 -08001990 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001991 if (input_file_player_) {
1992 input_file_player_->RegisterModuleFileCallback(NULL);
1993 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001994 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001995
kwiberg55b97fe2016-01-28 05:22:45 -08001996 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001997 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001998 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001999
kwiberg5a25d952016-08-17 07:31:12 -07002000 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002001 _engineStatisticsPtr->SetLastError(
2002 VE_INVALID_ARGUMENT, kTraceError,
2003 "StartPlayingInputFile() filePlayer format isnot correct");
2004 return -1;
2005 }
2006
2007 const uint32_t notificationTime(0);
2008
kwiberg4ec01d92016-08-22 08:43:54 -07002009 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2010 notificationTime, stopPosition,
2011 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002012 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2013 "StartPlayingFile() failed to start "
2014 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002015 input_file_player_->StopPlayingFile();
2016 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002017 return -1;
2018 }
2019
kwiberg5a25d952016-08-17 07:31:12 -07002020 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002021 channel_state_.SetInputFilePlaying(true);
2022
2023 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002024}
2025
kwiberg55b97fe2016-01-28 05:22:45 -08002026int Channel::StopPlayingFileAsMicrophone() {
2027 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2028 "Channel::StopPlayingFileAsMicrophone()");
2029
2030 rtc::CritScope cs(&_fileCritSect);
2031
2032 if (!channel_state_.Get().input_file_playing) {
2033 return 0;
2034 }
2035
kwiberg5a25d952016-08-17 07:31:12 -07002036 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002037 _engineStatisticsPtr->SetLastError(
2038 VE_STOP_RECORDING_FAILED, kTraceError,
2039 "StopPlayingFile() could not stop playing");
2040 return -1;
2041 }
kwiberg5a25d952016-08-17 07:31:12 -07002042 input_file_player_->RegisterModuleFileCallback(NULL);
2043 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002044 channel_state_.SetInputFilePlaying(false);
2045
2046 return 0;
2047}
2048
2049int Channel::IsPlayingFileAsMicrophone() const {
2050 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002051}
2052
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002053int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002054 const CodecInst* codecInst) {
2055 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2056 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002057
kwiberg55b97fe2016-01-28 05:22:45 -08002058 if (_outputFileRecording) {
2059 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2060 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002061 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002062 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002063
kwiberg55b97fe2016-01-28 05:22:45 -08002064 FileFormats format;
2065 const uint32_t notificationTime(0); // Not supported in VoE
2066 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002067
kwiberg55b97fe2016-01-28 05:22:45 -08002068 if ((codecInst != NULL) &&
2069 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2070 _engineStatisticsPtr->SetLastError(
2071 VE_BAD_ARGUMENT, kTraceError,
2072 "StartRecordingPlayout() invalid compression");
2073 return (-1);
2074 }
2075 if (codecInst == NULL) {
2076 format = kFileFormatPcm16kHzFile;
2077 codecInst = &dummyCodec;
2078 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2079 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2080 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2081 format = kFileFormatWavFile;
2082 } else {
2083 format = kFileFormatCompressedFile;
2084 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002085
kwiberg55b97fe2016-01-28 05:22:45 -08002086 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002087
kwiberg55b97fe2016-01-28 05:22:45 -08002088 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002089 if (output_file_recorder_) {
2090 output_file_recorder_->RegisterModuleFileCallback(NULL);
2091 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002092 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002093
kwiberg5a25d952016-08-17 07:31:12 -07002094 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002095 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002096 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002097 _engineStatisticsPtr->SetLastError(
2098 VE_INVALID_ARGUMENT, kTraceError,
2099 "StartRecordingPlayout() fileRecorder format isnot correct");
2100 return -1;
2101 }
2102
kwiberg5a25d952016-08-17 07:31:12 -07002103 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002104 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2105 _engineStatisticsPtr->SetLastError(
2106 VE_BAD_FILE, kTraceError,
2107 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002108 output_file_recorder_->StopRecording();
2109 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002110 return -1;
2111 }
kwiberg5a25d952016-08-17 07:31:12 -07002112 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002113 _outputFileRecording = true;
2114
2115 return 0;
2116}
2117
2118int Channel::StartRecordingPlayout(OutStream* stream,
2119 const CodecInst* codecInst) {
2120 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2121 "Channel::StartRecordingPlayout()");
2122
2123 if (_outputFileRecording) {
2124 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2125 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002126 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002127 }
2128
2129 FileFormats format;
2130 const uint32_t notificationTime(0); // Not supported in VoE
2131 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2132
2133 if (codecInst != NULL && codecInst->channels != 1) {
2134 _engineStatisticsPtr->SetLastError(
2135 VE_BAD_ARGUMENT, kTraceError,
2136 "StartRecordingPlayout() invalid compression");
2137 return (-1);
2138 }
2139 if (codecInst == NULL) {
2140 format = kFileFormatPcm16kHzFile;
2141 codecInst = &dummyCodec;
2142 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2143 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2144 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2145 format = kFileFormatWavFile;
2146 } else {
2147 format = kFileFormatCompressedFile;
2148 }
2149
2150 rtc::CritScope cs(&_fileCritSect);
2151
2152 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002153 if (output_file_recorder_) {
2154 output_file_recorder_->RegisterModuleFileCallback(NULL);
2155 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002156 }
2157
kwiberg5a25d952016-08-17 07:31:12 -07002158 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002159 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002160 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002161 _engineStatisticsPtr->SetLastError(
2162 VE_INVALID_ARGUMENT, kTraceError,
2163 "StartRecordingPlayout() fileRecorder format isnot correct");
2164 return -1;
2165 }
2166
kwiberg4ec01d92016-08-22 08:43:54 -07002167 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002168 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002169 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2170 "StartRecordingPlayout() failed to "
2171 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002172 output_file_recorder_->StopRecording();
2173 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002174 return -1;
2175 }
2176
kwiberg5a25d952016-08-17 07:31:12 -07002177 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002178 _outputFileRecording = true;
2179
2180 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002181}
2182
kwiberg55b97fe2016-01-28 05:22:45 -08002183int Channel::StopRecordingPlayout() {
2184 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2185 "Channel::StopRecordingPlayout()");
2186
2187 if (!_outputFileRecording) {
2188 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2189 "StopRecordingPlayout() isnot recording");
2190 return -1;
2191 }
2192
2193 rtc::CritScope cs(&_fileCritSect);
2194
kwiberg5a25d952016-08-17 07:31:12 -07002195 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002196 _engineStatisticsPtr->SetLastError(
2197 VE_STOP_RECORDING_FAILED, kTraceError,
2198 "StopRecording() could not stop recording");
2199 return (-1);
2200 }
kwiberg5a25d952016-08-17 07:31:12 -07002201 output_file_recorder_->RegisterModuleFileCallback(NULL);
2202 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002203 _outputFileRecording = false;
2204
2205 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002206}
2207
kwiberg55b97fe2016-01-28 05:22:45 -08002208void Channel::SetMixWithMicStatus(bool mix) {
2209 rtc::CritScope cs(&_fileCritSect);
2210 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002211}
2212
kwiberg55b97fe2016-01-28 05:22:45 -08002213int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2214 int8_t currentLevel = _outputAudioLevel.Level();
2215 level = static_cast<int32_t>(currentLevel);
2216 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002217}
2218
kwiberg55b97fe2016-01-28 05:22:45 -08002219int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2220 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2221 level = static_cast<int32_t>(currentLevel);
2222 return 0;
2223}
2224
solenberg1c2af8e2016-03-24 10:36:00 -07002225int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002226 rtc::CritScope cs(&volume_settings_critsect_);
2227 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002228 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002229 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002230 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002231}
2232
solenberg1c2af8e2016-03-24 10:36:00 -07002233bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002234 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002235 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002236}
2237
kwiberg55b97fe2016-01-28 05:22:45 -08002238int Channel::SetOutputVolumePan(float left, float right) {
2239 rtc::CritScope cs(&volume_settings_critsect_);
2240 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002241 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002242 _panLeft = left;
2243 _panRight = right;
2244 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002245}
2246
kwiberg55b97fe2016-01-28 05:22:45 -08002247int Channel::GetOutputVolumePan(float& left, float& right) const {
2248 rtc::CritScope cs(&volume_settings_critsect_);
2249 left = _panLeft;
2250 right = _panRight;
2251 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002252}
2253
kwiberg55b97fe2016-01-28 05:22:45 -08002254int Channel::SetChannelOutputVolumeScaling(float scaling) {
2255 rtc::CritScope cs(&volume_settings_critsect_);
2256 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002257 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002258 _outputGain = scaling;
2259 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002260}
2261
kwiberg55b97fe2016-01-28 05:22:45 -08002262int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2263 rtc::CritScope cs(&volume_settings_critsect_);
2264 scaling = _outputGain;
2265 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002266}
2267
solenberg8842c3e2016-03-11 03:06:41 -08002268int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002269 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002270 "Channel::SendTelephoneEventOutband(...)");
2271 RTC_DCHECK_LE(0, event);
2272 RTC_DCHECK_GE(255, event);
2273 RTC_DCHECK_LE(0, duration_ms);
2274 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002275 if (!Sending()) {
2276 return -1;
2277 }
solenberg8842c3e2016-03-11 03:06:41 -08002278 if (_rtpRtcpModule->SendTelephoneEventOutband(
2279 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002280 _engineStatisticsPtr->SetLastError(
2281 VE_SEND_DTMF_FAILED, kTraceWarning,
2282 "SendTelephoneEventOutband() failed to send event");
2283 return -1;
2284 }
2285 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002286}
2287
solenbergffbbcac2016-11-17 05:25:37 -08002288int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2289 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002290 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002291 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002292 RTC_DCHECK_LE(0, payload_type);
2293 RTC_DCHECK_GE(127, payload_type);
2294 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002295 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002296 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002297 memcpy(codec.plname, "telephone-event", 16);
2298 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2299 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2300 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2301 _engineStatisticsPtr->SetLastError(
2302 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2303 "SetSendTelephoneEventPayloadType() failed to register send"
2304 "payload type");
2305 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002306 }
kwiberg55b97fe2016-01-28 05:22:45 -08002307 }
kwiberg55b97fe2016-01-28 05:22:45 -08002308 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002309}
2310
kwiberg55b97fe2016-01-28 05:22:45 -08002311int Channel::VoiceActivityIndicator(int& activity) {
2312 activity = _sendFrameType;
2313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002314}
2315
kwiberg55b97fe2016-01-28 05:22:45 -08002316int Channel::SetLocalSSRC(unsigned int ssrc) {
2317 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2318 "Channel::SetLocalSSRC()");
2319 if (channel_state_.Get().sending) {
2320 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2321 "SetLocalSSRC() already sending");
2322 return -1;
2323 }
2324 _rtpRtcpModule->SetSSRC(ssrc);
2325 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002326}
2327
kwiberg55b97fe2016-01-28 05:22:45 -08002328int Channel::GetLocalSSRC(unsigned int& ssrc) {
2329 ssrc = _rtpRtcpModule->SSRC();
2330 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002331}
2332
kwiberg55b97fe2016-01-28 05:22:45 -08002333int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2334 ssrc = rtp_receiver_->SSRC();
2335 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002336}
2337
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002338int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002339 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002340 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002341}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002342
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002343int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2344 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002345 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2346 if (enable &&
2347 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2348 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002349 return -1;
2350 }
2351 return 0;
2352}
2353
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002354void Channel::EnableSendTransportSequenceNumber(int id) {
2355 int ret =
2356 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2357 RTC_DCHECK_EQ(0, ret);
2358}
2359
stefan3313ec92016-01-21 06:32:43 -08002360void Channel::EnableReceiveTransportSequenceNumber(int id) {
2361 rtp_header_parser_->DeregisterRtpHeaderExtension(
2362 kRtpExtensionTransportSequenceNumber);
2363 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2364 kRtpExtensionTransportSequenceNumber, id);
2365 RTC_DCHECK(ret);
2366}
2367
stefanbba9dec2016-02-01 04:39:55 -08002368void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002369 RtpPacketSender* rtp_packet_sender,
2370 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002371 PacketRouter* packet_router,
2372 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002373 RTC_DCHECK(rtp_packet_sender);
2374 RTC_DCHECK(transport_feedback_observer);
2375 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002376 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002377 feedback_observer_proxy_->SetTransportFeedbackObserver(
2378 transport_feedback_observer);
2379 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2380 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2381 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002382 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002383 packet_router_ = packet_router;
2384}
2385
stefanbba9dec2016-02-01 04:39:55 -08002386void Channel::RegisterReceiverCongestionControlObjects(
2387 PacketRouter* packet_router) {
2388 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002389 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002390 packet_router_ = packet_router;
2391}
2392
2393void Channel::ResetCongestionControlObjects() {
2394 RTC_DCHECK(packet_router_);
2395 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002396 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002397 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2398 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002399 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002400 packet_router_ = nullptr;
2401 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2402}
2403
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002404void Channel::SetRTCPStatus(bool enable) {
2405 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2406 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002407 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002408}
2409
kwiberg55b97fe2016-01-28 05:22:45 -08002410int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002411 RtcpMode method = _rtpRtcpModule->RTCP();
2412 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002413 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002414}
2415
kwiberg55b97fe2016-01-28 05:22:45 -08002416int Channel::SetRTCP_CNAME(const char cName[256]) {
2417 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2418 "Channel::SetRTCP_CNAME()");
2419 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2420 _engineStatisticsPtr->SetLastError(
2421 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2422 "SetRTCP_CNAME() failed to set RTCP CNAME");
2423 return -1;
2424 }
2425 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002426}
2427
kwiberg55b97fe2016-01-28 05:22:45 -08002428int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2429 if (cName == NULL) {
2430 _engineStatisticsPtr->SetLastError(
2431 VE_INVALID_ARGUMENT, kTraceError,
2432 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2433 return -1;
2434 }
2435 char cname[RTCP_CNAME_SIZE];
2436 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2437 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2438 _engineStatisticsPtr->SetLastError(
2439 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2440 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2441 return -1;
2442 }
2443 strcpy(cName, cname);
2444 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002445}
2446
kwiberg55b97fe2016-01-28 05:22:45 -08002447int Channel::SendApplicationDefinedRTCPPacket(
2448 unsigned char subType,
2449 unsigned int name,
2450 const char* data,
2451 unsigned short dataLengthInBytes) {
2452 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2453 "Channel::SendApplicationDefinedRTCPPacket()");
2454 if (!channel_state_.Get().sending) {
2455 _engineStatisticsPtr->SetLastError(
2456 VE_NOT_SENDING, kTraceError,
2457 "SendApplicationDefinedRTCPPacket() not sending");
2458 return -1;
2459 }
2460 if (NULL == data) {
2461 _engineStatisticsPtr->SetLastError(
2462 VE_INVALID_ARGUMENT, kTraceError,
2463 "SendApplicationDefinedRTCPPacket() invalid data value");
2464 return -1;
2465 }
2466 if (dataLengthInBytes % 4 != 0) {
2467 _engineStatisticsPtr->SetLastError(
2468 VE_INVALID_ARGUMENT, kTraceError,
2469 "SendApplicationDefinedRTCPPacket() invalid length value");
2470 return -1;
2471 }
2472 RtcpMode status = _rtpRtcpModule->RTCP();
2473 if (status == RtcpMode::kOff) {
2474 _engineStatisticsPtr->SetLastError(
2475 VE_RTCP_ERROR, kTraceError,
2476 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2477 return -1;
2478 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002479
kwiberg55b97fe2016-01-28 05:22:45 -08002480 // Create and schedule the RTCP APP packet for transmission
2481 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2482 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2483 _engineStatisticsPtr->SetLastError(
2484 VE_SEND_ERROR, kTraceError,
2485 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2486 return -1;
2487 }
2488 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002489}
2490
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002491int Channel::GetRemoteRTCPReportBlocks(
2492 std::vector<ReportBlock>* report_blocks) {
2493 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002494 _engineStatisticsPtr->SetLastError(
2495 VE_INVALID_ARGUMENT, kTraceError,
2496 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002497 return -1;
2498 }
2499
2500 // Get the report blocks from the latest received RTCP Sender or Receiver
2501 // Report. Each element in the vector contains the sender's SSRC and a
2502 // report block according to RFC 3550.
2503 std::vector<RTCPReportBlock> rtcp_report_blocks;
2504 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002505 return -1;
2506 }
2507
2508 if (rtcp_report_blocks.empty())
2509 return 0;
2510
2511 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2512 for (; it != rtcp_report_blocks.end(); ++it) {
2513 ReportBlock report_block;
2514 report_block.sender_SSRC = it->remoteSSRC;
2515 report_block.source_SSRC = it->sourceSSRC;
2516 report_block.fraction_lost = it->fractionLost;
2517 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2518 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2519 report_block.interarrival_jitter = it->jitter;
2520 report_block.last_SR_timestamp = it->lastSR;
2521 report_block.delay_since_last_SR = it->delaySinceLastSR;
2522 report_blocks->push_back(report_block);
2523 }
2524 return 0;
2525}
2526
kwiberg55b97fe2016-01-28 05:22:45 -08002527int Channel::GetRTPStatistics(CallStatistics& stats) {
2528 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002529
kwiberg55b97fe2016-01-28 05:22:45 -08002530 // The jitter statistics is updated for each received RTP packet and is
2531 // based on received packets.
2532 RtcpStatistics statistics;
2533 StreamStatistician* statistician =
2534 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002535 if (statistician) {
2536 statistician->GetStatistics(&statistics,
2537 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002538 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002539
kwiberg55b97fe2016-01-28 05:22:45 -08002540 stats.fractionLost = statistics.fraction_lost;
2541 stats.cumulativeLost = statistics.cumulative_lost;
2542 stats.extendedMax = statistics.extended_max_sequence_number;
2543 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002544
kwiberg55b97fe2016-01-28 05:22:45 -08002545 // --- RTT
2546 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002547
kwiberg55b97fe2016-01-28 05:22:45 -08002548 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002549
kwiberg55b97fe2016-01-28 05:22:45 -08002550 size_t bytesSent(0);
2551 uint32_t packetsSent(0);
2552 size_t bytesReceived(0);
2553 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002554
kwiberg55b97fe2016-01-28 05:22:45 -08002555 if (statistician) {
2556 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2557 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002558
kwiberg55b97fe2016-01-28 05:22:45 -08002559 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2560 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2561 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2562 " output will not be complete");
2563 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002564
kwiberg55b97fe2016-01-28 05:22:45 -08002565 stats.bytesSent = bytesSent;
2566 stats.packetsSent = packetsSent;
2567 stats.bytesReceived = bytesReceived;
2568 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002569
kwiberg55b97fe2016-01-28 05:22:45 -08002570 // --- Timestamps
2571 {
2572 rtc::CritScope lock(&ts_stats_lock_);
2573 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2574 }
2575 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002576}
2577
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002578int Channel::SetCodecFECStatus(bool enable) {
2579 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2580 "Channel::SetCodecFECStatus()");
2581
kwibergc8d071e2016-04-06 12:22:38 -07002582 if (!codec_manager_.SetCodecFEC(enable) ||
2583 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002584 _engineStatisticsPtr->SetLastError(
2585 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2586 "SetCodecFECStatus() failed to set FEC state");
2587 return -1;
2588 }
2589 return 0;
2590}
2591
2592bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002593 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002594}
2595
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002596void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2597 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002598 // If pacing is enabled we always store packets.
2599 if (!pacing_enabled_)
2600 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002601 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002602 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002603 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002604 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002605 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002606}
2607
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002608// Called when we are missing one or more packets.
2609int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002610 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2611}
2612
kwiberg55b97fe2016-01-28 05:22:45 -08002613uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2614 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2615 "Channel::Demultiplex()");
2616 _audioFrame.CopyFrom(audioFrame);
2617 _audioFrame.id_ = _channelId;
2618 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002619}
2620
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002621void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002622 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002623 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002624 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002625 CodecInst codec;
2626 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002627
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002628 // Never upsample or upmix the capture signal here. This should be done at the
2629 // end of the send chain.
2630 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2631 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2632 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2633 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002634}
2635
kwiberg55b97fe2016-01-28 05:22:45 -08002636uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2637 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2638 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002639
kwiberg55b97fe2016-01-28 05:22:45 -08002640 if (_audioFrame.samples_per_channel_ == 0) {
2641 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2642 "Channel::PrepareEncodeAndSend() invalid audio frame");
2643 return 0xFFFFFFFF;
2644 }
2645
2646 if (channel_state_.Get().input_file_playing) {
2647 MixOrReplaceAudioWithFile(mixingFrequency);
2648 }
2649
solenberg1c2af8e2016-03-24 10:36:00 -07002650 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2651 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002652
kwiberg55b97fe2016-01-28 05:22:45 -08002653 if (_includeAudioLevelIndication) {
2654 size_t length =
2655 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002656 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002657 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002658 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002659 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002660 rms_level_.Analyze(
2661 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002662 }
kwiberg55b97fe2016-01-28 05:22:45 -08002663 }
solenberg1c2af8e2016-03-24 10:36:00 -07002664 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002665
kwiberg55b97fe2016-01-28 05:22:45 -08002666 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002667}
2668
kwiberg55b97fe2016-01-28 05:22:45 -08002669uint32_t Channel::EncodeAndSend() {
2670 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2671 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002672
kwiberg55b97fe2016-01-28 05:22:45 -08002673 assert(_audioFrame.num_channels_ <= 2);
2674 if (_audioFrame.samples_per_channel_ == 0) {
2675 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2676 "Channel::EncodeAndSend() invalid audio frame");
2677 return 0xFFFFFFFF;
2678 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002679
kwiberg55b97fe2016-01-28 05:22:45 -08002680 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002681
kwiberg55b97fe2016-01-28 05:22:45 -08002682 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002683
kwiberg55b97fe2016-01-28 05:22:45 -08002684 // The ACM resamples internally.
2685 _audioFrame.timestamp_ = _timeStamp;
2686 // This call will trigger AudioPacketizationCallback::SendData if encoding
2687 // is done and payload is ready for packetization and transmission.
2688 // Otherwise, it will return without invoking the callback.
2689 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2690 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2691 "Channel::EncodeAndSend() ACM encoding failed");
2692 return 0xFFFFFFFF;
2693 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002694
kwiberg55b97fe2016-01-28 05:22:45 -08002695 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2696 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002697}
2698
solenberg7602aab2016-11-14 11:30:07 -08002699void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2700 RTC_DCHECK(!channel.channel() ||
2701 channel.channel()->ChannelId() != _channelId);
2702 rtc::CritScope lock(&assoc_send_channel_lock_);
2703 associate_send_channel_ = channel;
2704}
2705
Minyue2013aec2015-05-13 14:14:42 +02002706void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002707 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002708 Channel* channel = associate_send_channel_.channel();
2709 if (channel && channel->ChannelId() == channel_id) {
2710 // If this channel is associated with a send channel of the specified
2711 // Channel ID, disassociate with it.
2712 ChannelOwner ref(NULL);
2713 associate_send_channel_ = ref;
2714 }
2715}
2716
ivoc14d5dbe2016-07-04 07:06:55 -07002717void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2718 event_log_proxy_->SetEventLog(event_log);
2719}
2720
michaelt9332b7d2016-11-30 07:51:13 -08002721void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2722 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2723}
2724
nisse284542b2017-01-10 08:58:32 -08002725void Channel::UpdateOverheadForEncoder() {
2726 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2727 if (*encoder) {
2728 (*encoder)->OnReceivedOverhead(transport_overhead_per_packet_ +
2729 rtp_overhead_per_packet_);
2730 }
2731 });
2732}
2733
2734void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
2735 transport_overhead_per_packet_ = transport_overhead_per_packet;
2736 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002737}
2738
michaeltbf65be52016-12-15 06:24:49 -08002739void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
nisse284542b2017-01-10 08:58:32 -08002740 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2741 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002742}
2743
kwiberg55b97fe2016-01-28 05:22:45 -08002744int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2745 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002746}
2747
wu@webrtc.org24301a62013-12-13 19:17:43 +00002748void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2749 audio_coding_->GetDecodingCallStatistics(stats);
2750}
2751
solenberg358057b2015-11-27 10:46:42 -08002752uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002753 rtc::CritScope lock(&video_sync_lock_);
2754 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002755}
2756
kwiberg55b97fe2016-01-28 05:22:45 -08002757int Channel::SetMinimumPlayoutDelay(int delayMs) {
2758 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2759 "Channel::SetMinimumPlayoutDelay()");
2760 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2761 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2762 _engineStatisticsPtr->SetLastError(
2763 VE_INVALID_ARGUMENT, kTraceError,
2764 "SetMinimumPlayoutDelay() invalid min delay");
2765 return -1;
2766 }
2767 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2768 _engineStatisticsPtr->SetLastError(
2769 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2770 "SetMinimumPlayoutDelay() failed to set min playout delay");
2771 return -1;
2772 }
2773 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002774}
2775
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002776int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002777 uint32_t playout_timestamp_rtp = 0;
2778 {
tommi31fc21f2016-01-21 10:37:37 -08002779 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002780 playout_timestamp_rtp = playout_timestamp_rtp_;
2781 }
kwiberg55b97fe2016-01-28 05:22:45 -08002782 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002783 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002784 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002785 "GetPlayoutTimestamp() failed to retrieve timestamp");
2786 return -1;
2787 }
deadbeef74375882015-08-13 12:09:10 -07002788 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002789 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002790}
2791
kwiberg55b97fe2016-01-28 05:22:45 -08002792int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2793 RtpReceiver** rtp_receiver) const {
2794 *rtpRtcpModule = _rtpRtcpModule.get();
2795 *rtp_receiver = rtp_receiver_.get();
2796 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002797}
2798
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002799// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2800// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002801int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002802 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002803 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002804
kwiberg55b97fe2016-01-28 05:22:45 -08002805 {
2806 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002807
kwiberg5a25d952016-08-17 07:31:12 -07002808 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002809 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2810 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2811 " doesnt exist");
2812 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002813 }
2814
kwiberg4ec01d92016-08-22 08:43:54 -07002815 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002816 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002817 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2818 "Channel::MixOrReplaceAudioWithFile() file mixing "
2819 "failed");
2820 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002821 }
kwiberg55b97fe2016-01-28 05:22:45 -08002822 if (fileSamples == 0) {
2823 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2824 "Channel::MixOrReplaceAudioWithFile() file is ended");
2825 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002826 }
kwiberg55b97fe2016-01-28 05:22:45 -08002827 }
2828
2829 assert(_audioFrame.samples_per_channel_ == fileSamples);
2830
2831 if (_mixFileWithMicrophone) {
2832 // Currently file stream is always mono.
2833 // TODO(xians): Change the code when FilePlayer supports real stereo.
2834 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2835 1, fileSamples);
2836 } else {
2837 // Replace ACM audio with file.
2838 // Currently file stream is always mono.
2839 // TODO(xians): Change the code when FilePlayer supports real stereo.
2840 _audioFrame.UpdateFrame(
2841 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2842 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2843 }
2844 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002845}
2846
kwiberg55b97fe2016-01-28 05:22:45 -08002847int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2848 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002849
kwibergb7f89d62016-02-17 10:04:18 -08002850 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002851 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002852
kwiberg55b97fe2016-01-28 05:22:45 -08002853 {
2854 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002855
kwiberg5a25d952016-08-17 07:31:12 -07002856 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002857 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2858 "Channel::MixAudioWithFile() file mixing failed");
2859 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002860 }
2861
kwiberg55b97fe2016-01-28 05:22:45 -08002862 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002863 if (output_file_player_->Get10msAudioFromFile(
2864 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002865 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2866 "Channel::MixAudioWithFile() file mixing failed");
2867 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002868 }
kwiberg55b97fe2016-01-28 05:22:45 -08002869 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002870
kwiberg55b97fe2016-01-28 05:22:45 -08002871 if (audioFrame.samples_per_channel_ == fileSamples) {
2872 // Currently file stream is always mono.
2873 // TODO(xians): Change the code when FilePlayer supports real stereo.
2874 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2875 fileSamples);
2876 } else {
2877 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2878 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2879 ") != "
2880 "fileSamples(%" PRIuS ")",
2881 audioFrame.samples_per_channel_, fileSamples);
2882 return -1;
2883 }
2884
2885 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002886}
2887
deadbeef74375882015-08-13 12:09:10 -07002888void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002889 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002890
henrik.lundin96bd5022016-04-06 04:13:56 -07002891 if (!jitter_buffer_playout_timestamp_) {
2892 // This can happen if this channel has not received any RTP packets. In
2893 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002894 return;
2895 }
2896
2897 uint16_t delay_ms = 0;
2898 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002899 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002900 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2901 " delay from the ADM");
2902 _engineStatisticsPtr->SetLastError(
2903 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2904 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2905 return;
2906 }
2907
henrik.lundin96bd5022016-04-06 04:13:56 -07002908 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2909 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002910
2911 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002912 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002913
kwiberg55b97fe2016-01-28 05:22:45 -08002914 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002915 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002916 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002917
2918 {
tommi31fc21f2016-01-21 10:37:37 -08002919 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002920 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002921 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002922 }
2923 playout_delay_ms_ = delay_ms;
2924 }
2925}
2926
kwiberg55b97fe2016-01-28 05:22:45 -08002927void Channel::RegisterReceiveCodecsToRTPModule() {
2928 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2929 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002930
kwiberg55b97fe2016-01-28 05:22:45 -08002931 CodecInst codec;
2932 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002933
kwiberg55b97fe2016-01-28 05:22:45 -08002934 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2935 // Open up the RTP/RTCP receiver for all supported codecs
2936 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002937 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002938 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2939 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2940 " to register %s (%d/%d/%" PRIuS
2941 "/%d) to RTP/RTCP "
2942 "receiver",
2943 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2944 codec.rate);
2945 } else {
2946 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2947 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2948 "(%d/%d/%" PRIuS
2949 "/%d) has been added to the RTP/RTCP "
2950 "receiver",
2951 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2952 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002953 }
kwiberg55b97fe2016-01-28 05:22:45 -08002954 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002955}
2956
kwiberg55b97fe2016-01-28 05:22:45 -08002957int Channel::SetSendRtpHeaderExtension(bool enable,
2958 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002959 unsigned char id) {
2960 int error = 0;
2961 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
2962 if (enable) {
2963 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
2964 }
2965 return error;
2966}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002967
ossue280cde2016-10-12 11:04:10 -07002968int Channel::GetRtpTimestampRateHz() const {
2969 const auto format = audio_coding_->ReceiveFormat();
2970 // Default to the playout frequency if we've not gotten any packets yet.
2971 // TODO(ossu): Zero clockrate can only happen if we've added an external
2972 // decoder for a format we don't support internally. Remove once that way of
2973 // adding decoders is gone!
2974 return (format && format->clockrate_hz != 0)
2975 ? format->clockrate_hz
2976 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00002977}
2978
Minyue2013aec2015-05-13 14:14:42 +02002979int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07002980 RtcpMode method = _rtpRtcpModule->RTCP();
2981 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002982 return 0;
2983 }
2984 std::vector<RTCPReportBlock> report_blocks;
2985 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02002986
2987 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002988 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02002989 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08002990 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002991 Channel* channel = associate_send_channel_.channel();
2992 // Tries to get RTT from an associated channel. This is important for
2993 // receive-only channels.
2994 if (channel) {
2995 // To prevent infinite recursion and deadlock, calling GetRTT of
2996 // associate channel should always use "false" for argument:
2997 // |allow_associate_channel|.
2998 rtt = channel->GetRTT(false);
2999 }
3000 }
3001 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003002 }
3003
3004 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3005 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3006 for (; it != report_blocks.end(); ++it) {
3007 if (it->remoteSSRC == remoteSSRC)
3008 break;
3009 }
3010 if (it == report_blocks.end()) {
3011 // We have not received packets with SSRC matching the report blocks.
3012 // To calculate RTT we try with the SSRC of the first report block.
3013 // This is very important for send-only channels where we don't know
3014 // the SSRC of the other end.
3015 remoteSSRC = report_blocks[0].remoteSSRC;
3016 }
Minyue2013aec2015-05-13 14:14:42 +02003017
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003018 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003019 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003020 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003021 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3022 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003023 return 0;
3024 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003025 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003026}
3027
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003028} // namespace voe
3029} // namespace webrtc