blob: d69654fb0f2451f1a5e49564f63ef90a32056aff [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
aleloi6321b492016-12-05 01:46:09 -080016#include "webrtc/audio/utility/audio_frame_operations.h"
henrik.lundin50499422016-11-29 04:26:24 -080017#include "webrtc/base/array_view.h"
Ivo Creusenae856f22015-09-17 16:30:16 +020018#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080019#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000020#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080021#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020022#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010023#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000024#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020025#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070026#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070027#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000028#include "webrtc/modules/audio_device/include/audio_device.h"
29#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010031#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
33#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000035#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010036#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010037#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000038#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
39#include "webrtc/voice_engine/output_mixer.h"
40#include "webrtc/voice_engine/statistics.h"
41#include "webrtc/voice_engine/transmit_mixer.h"
42#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andrew@webrtc.org50419b02012-11-14 19:07:54 +000044namespace webrtc {
45namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
kwibergc8d071e2016-04-06 12:22:38 -070047namespace {
48
Erik Språng737336d2016-07-29 12:59:36 +020049constexpr int64_t kMaxRetransmissionWindowMs = 1000;
50constexpr int64_t kMinRetransmissionWindowMs = 30;
51
kwibergc8d071e2016-04-06 12:22:38 -070052} // namespace
53
solenberg8842c3e2016-03-11 03:06:41 -080054const int kTelephoneEventAttenuationdB = 10;
55
ivoc14d5dbe2016-07-04 07:06:55 -070056class RtcEventLogProxy final : public webrtc::RtcEventLog {
57 public:
58 RtcEventLogProxy() : event_log_(nullptr) {}
59
60 bool StartLogging(const std::string& file_name,
61 int64_t max_size_bytes) override {
62 RTC_NOTREACHED();
63 return false;
64 }
65
66 bool StartLogging(rtc::PlatformFile log_file,
67 int64_t max_size_bytes) override {
68 RTC_NOTREACHED();
69 return false;
70 }
71
72 void StopLogging() override { RTC_NOTREACHED(); }
73
74 void LogVideoReceiveStreamConfig(
75 const webrtc::VideoReceiveStream::Config& config) override {
76 rtc::CritScope lock(&crit_);
77 if (event_log_) {
78 event_log_->LogVideoReceiveStreamConfig(config);
79 }
80 }
81
82 void LogVideoSendStreamConfig(
83 const webrtc::VideoSendStream::Config& config) override {
84 rtc::CritScope lock(&crit_);
85 if (event_log_) {
86 event_log_->LogVideoSendStreamConfig(config);
87 }
88 }
89
ivoce0928d82016-10-10 05:12:51 -070090 void LogAudioReceiveStreamConfig(
91 const webrtc::AudioReceiveStream::Config& config) override {
92 rtc::CritScope lock(&crit_);
93 if (event_log_) {
94 event_log_->LogAudioReceiveStreamConfig(config);
95 }
96 }
97
98 void LogAudioSendStreamConfig(
99 const webrtc::AudioSendStream::Config& config) override {
100 rtc::CritScope lock(&crit_);
101 if (event_log_) {
102 event_log_->LogAudioSendStreamConfig(config);
103 }
104 }
105
ivoc14d5dbe2016-07-04 07:06:55 -0700106 void LogRtpHeader(webrtc::PacketDirection direction,
107 webrtc::MediaType media_type,
108 const uint8_t* header,
109 size_t packet_length) override {
110 rtc::CritScope lock(&crit_);
111 if (event_log_) {
112 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
113 }
114 }
115
116 void LogRtcpPacket(webrtc::PacketDirection direction,
117 webrtc::MediaType media_type,
118 const uint8_t* packet,
119 size_t length) override {
120 rtc::CritScope lock(&crit_);
121 if (event_log_) {
122 event_log_->LogRtcpPacket(direction, media_type, packet, length);
123 }
124 }
125
126 void LogAudioPlayout(uint32_t ssrc) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogAudioPlayout(ssrc);
130 }
131 }
132
133 void LogBwePacketLossEvent(int32_t bitrate,
134 uint8_t fraction_loss,
135 int32_t total_packets) override {
136 rtc::CritScope lock(&crit_);
137 if (event_log_) {
138 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
139 }
140 }
141
minyue4b7c9522017-01-24 04:54:59 -0800142 void LogAudioNetworkAdaptation(
143 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) override {
144 rtc::CritScope lock(&crit_);
145 if (event_log_) {
146 event_log_->LogAudioNetworkAdaptation(config);
147 }
148 }
149
ivoc14d5dbe2016-07-04 07:06:55 -0700150 void SetEventLog(RtcEventLog* event_log) {
151 rtc::CritScope lock(&crit_);
152 event_log_ = event_log;
153 }
154
155 private:
156 rtc::CriticalSection crit_;
157 RtcEventLog* event_log_ GUARDED_BY(crit_);
158 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
159};
160
michaelt9332b7d2016-11-30 07:51:13 -0800161class RtcpRttStatsProxy final : public RtcpRttStats {
162 public:
163 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
164
165 void OnRttUpdate(int64_t rtt) override {
166 rtc::CritScope lock(&crit_);
167 if (rtcp_rtt_stats_)
168 rtcp_rtt_stats_->OnRttUpdate(rtt);
169 }
170
171 int64_t LastProcessedRtt() const override {
172 rtc::CritScope lock(&crit_);
173 if (!rtcp_rtt_stats_)
174 return 0;
175 return rtcp_rtt_stats_->LastProcessedRtt();
176 }
177
178 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
179 rtc::CritScope lock(&crit_);
180 rtcp_rtt_stats_ = rtcp_rtt_stats;
181 }
182
183 private:
184 rtc::CriticalSection crit_;
185 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
186 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
187};
188
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100189class TransportFeedbackProxy : public TransportFeedbackObserver {
190 public:
191 TransportFeedbackProxy() : feedback_observer_(nullptr) {
192 pacer_thread_.DetachFromThread();
193 network_thread_.DetachFromThread();
194 }
195
196 void SetTransportFeedbackObserver(
197 TransportFeedbackObserver* feedback_observer) {
198 RTC_DCHECK(thread_checker_.CalledOnValidThread());
199 rtc::CritScope lock(&crit_);
200 feedback_observer_ = feedback_observer;
201 }
202
203 // Implements TransportFeedbackObserver.
204 void AddPacket(uint16_t sequence_number,
205 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700206 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100207 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
208 rtc::CritScope lock(&crit_);
209 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700210 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100211 }
212 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
213 RTC_DCHECK(network_thread_.CalledOnValidThread());
214 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700215 if (feedback_observer_)
216 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200217 }
218 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
219 RTC_NOTREACHED();
220 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100221 }
222
223 private:
224 rtc::CriticalSection crit_;
225 rtc::ThreadChecker thread_checker_;
226 rtc::ThreadChecker pacer_thread_;
227 rtc::ThreadChecker network_thread_;
228 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
229};
230
231class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
232 public:
233 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
234 pacer_thread_.DetachFromThread();
235 }
236
237 void SetSequenceNumberAllocator(
238 TransportSequenceNumberAllocator* seq_num_allocator) {
239 RTC_DCHECK(thread_checker_.CalledOnValidThread());
240 rtc::CritScope lock(&crit_);
241 seq_num_allocator_ = seq_num_allocator;
242 }
243
244 // Implements TransportSequenceNumberAllocator.
245 uint16_t AllocateSequenceNumber() override {
246 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
247 rtc::CritScope lock(&crit_);
248 if (!seq_num_allocator_)
249 return 0;
250 return seq_num_allocator_->AllocateSequenceNumber();
251 }
252
253 private:
254 rtc::CriticalSection crit_;
255 rtc::ThreadChecker thread_checker_;
256 rtc::ThreadChecker pacer_thread_;
257 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
258};
259
260class RtpPacketSenderProxy : public RtpPacketSender {
261 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800262 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100263
264 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
265 RTC_DCHECK(thread_checker_.CalledOnValidThread());
266 rtc::CritScope lock(&crit_);
267 rtp_packet_sender_ = rtp_packet_sender;
268 }
269
270 // Implements RtpPacketSender.
271 void InsertPacket(Priority priority,
272 uint32_t ssrc,
273 uint16_t sequence_number,
274 int64_t capture_time_ms,
275 size_t bytes,
276 bool retransmission) override {
277 rtc::CritScope lock(&crit_);
278 if (rtp_packet_sender_) {
279 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
280 capture_time_ms, bytes, retransmission);
281 }
282 }
283
284 private:
285 rtc::ThreadChecker thread_checker_;
286 rtc::CriticalSection crit_;
287 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
288};
289
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000290class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000291 public:
stefan7de8d642017-02-07 07:14:08 -0800292 explicit VoERtcpObserver(Channel* owner)
293 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000294 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000295
stefan7de8d642017-02-07 07:14:08 -0800296 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
297 rtc::CritScope lock(&crit_);
298 bandwidth_observer_ = bandwidth_observer;
299 }
300
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000301 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800302 rtc::CritScope lock(&crit_);
303 if (bandwidth_observer_) {
304 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
305 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000306 }
307
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000308 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
309 int64_t rtt,
310 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800311 {
312 rtc::CritScope lock(&crit_);
313 if (bandwidth_observer_) {
314 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
315 now_ms);
316 }
317 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000318 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
319 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
320 // report for VoiceEngine?
321 if (report_blocks.empty())
322 return;
323
324 int fraction_lost_aggregate = 0;
325 int total_number_of_packets = 0;
326
327 // If receiving multiple report blocks, calculate the weighted average based
328 // on the number of packets a report refers to.
329 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
330 block_it != report_blocks.end(); ++block_it) {
331 // Find the previous extended high sequence number for this remote SSRC,
332 // to calculate the number of RTP packets this report refers to. Ignore if
333 // we haven't seen this SSRC before.
334 std::map<uint32_t, uint32_t>::iterator seq_num_it =
335 extended_max_sequence_number_.find(block_it->sourceSSRC);
336 int number_of_packets = 0;
337 if (seq_num_it != extended_max_sequence_number_.end()) {
338 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
339 }
340 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
341 total_number_of_packets += number_of_packets;
342
343 extended_max_sequence_number_[block_it->sourceSSRC] =
344 block_it->extendedHighSeqNum;
345 }
346 int weighted_fraction_lost = 0;
347 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800348 weighted_fraction_lost =
349 (fraction_lost_aggregate + total_number_of_packets / 2) /
350 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000351 }
352 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000353 }
354
355 private:
356 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000357 // Maps remote side ssrc to extended highest sequence number received.
358 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800359 rtc::CriticalSection crit_;
360 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000361};
362
kwiberg55b97fe2016-01-28 05:22:45 -0800363int32_t Channel::SendData(FrameType frameType,
364 uint8_t payloadType,
365 uint32_t timeStamp,
366 const uint8_t* payloadData,
367 size_t payloadSize,
368 const RTPFragmentationHeader* fragmentation) {
369 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
370 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
371 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
372 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000373
kwiberg55b97fe2016-01-28 05:22:45 -0800374 if (_includeAudioLevelIndication) {
375 // Store current audio level in the RTP/RTCP module.
376 // The level will be used in combination with voice-activity state
377 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800378 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800379 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000380
kwiberg55b97fe2016-01-28 05:22:45 -0800381 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
382 // packetization.
383 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700384 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800385 (FrameType&)frameType, payloadType, timeStamp,
386 // Leaving the time when this frame was
387 // received from the capture device as
388 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700389 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800390 _engineStatisticsPtr->SetLastError(
391 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
392 "Channel::SendData() failed to send data to RTP/RTCP module");
393 return -1;
394 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
kwiberg55b97fe2016-01-28 05:22:45 -0800396 _lastLocalTimeStamp = timeStamp;
397 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398
kwiberg55b97fe2016-01-28 05:22:45 -0800399 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400}
401
kwiberg55b97fe2016-01-28 05:22:45 -0800402int32_t Channel::InFrameType(FrameType frame_type) {
403 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
404 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
kwiberg55b97fe2016-01-28 05:22:45 -0800406 rtc::CritScope cs(&_callbackCritSect);
407 _sendFrameType = (frame_type == kAudioFrameSpeech);
408 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000409}
410
stefan1d8a5062015-10-02 03:39:33 -0700411bool Channel::SendRtp(const uint8_t* data,
412 size_t len,
413 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800414 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
415 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000416
kwiberg55b97fe2016-01-28 05:22:45 -0800417 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000418
kwiberg55b97fe2016-01-28 05:22:45 -0800419 if (_transportPtr == NULL) {
420 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
421 "Channel::SendPacket() failed to send RTP packet due to"
422 " invalid transport object");
423 return false;
424 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000425
kwiberg55b97fe2016-01-28 05:22:45 -0800426 uint8_t* bufferToSendPtr = (uint8_t*)data;
427 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
kwiberg55b97fe2016-01-28 05:22:45 -0800429 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
430 std::string transport_name =
431 _externalTransport ? "external transport" : "WebRtc sockets";
432 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
433 "Channel::SendPacket() RTP transmission using %s failed",
434 transport_name.c_str());
435 return false;
436 }
437 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000438}
439
kwiberg55b97fe2016-01-28 05:22:45 -0800440bool Channel::SendRtcp(const uint8_t* data, size_t len) {
441 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
442 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
kwiberg55b97fe2016-01-28 05:22:45 -0800444 rtc::CritScope cs(&_callbackCritSect);
445 if (_transportPtr == NULL) {
446 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
447 "Channel::SendRtcp() failed to send RTCP packet"
448 " due to invalid transport object");
449 return false;
450 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000451
kwiberg55b97fe2016-01-28 05:22:45 -0800452 uint8_t* bufferToSendPtr = (uint8_t*)data;
453 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000454
kwiberg55b97fe2016-01-28 05:22:45 -0800455 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
456 if (n < 0) {
457 std::string transport_name =
458 _externalTransport ? "external transport" : "WebRtc sockets";
459 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
460 "Channel::SendRtcp() transmission using %s failed",
461 transport_name.c_str());
462 return false;
463 }
464 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
kwiberg55b97fe2016-01-28 05:22:45 -0800467void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
468 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
469 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
kwiberg55b97fe2016-01-28 05:22:45 -0800471 // Update ssrc so that NTP for AV sync can be updated.
472 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
Peter Boströmac547a62015-09-17 23:03:57 +0200475void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
476 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
477 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
478 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000479}
480
Peter Boströmac547a62015-09-17 23:03:57 +0200481int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000482 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000483 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000484 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800485 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200486 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800487 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
488 "Channel::OnInitializeDecoder(payloadType=%d, "
489 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
490 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
kwiberg55b97fe2016-01-28 05:22:45 -0800492 CodecInst receiveCodec = {0};
493 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000494
kwiberg55b97fe2016-01-28 05:22:45 -0800495 receiveCodec.pltype = payloadType;
496 receiveCodec.plfreq = frequency;
497 receiveCodec.channels = channels;
498 receiveCodec.rate = rate;
499 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000500
kwiberg55b97fe2016-01-28 05:22:45 -0800501 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
502 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
kwiberg55b97fe2016-01-28 05:22:45 -0800504 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700505 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
506 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800507 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
508 "Channel::OnInitializeDecoder() invalid codec ("
509 "pt=%d, name=%s) received - 1",
510 payloadType, payloadName);
511 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
512 return -1;
513 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
kwiberg55b97fe2016-01-28 05:22:45 -0800515 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000516}
517
kwiberg55b97fe2016-01-28 05:22:45 -0800518int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
519 size_t payloadSize,
520 const WebRtcRTPHeader* rtpHeader) {
521 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
522 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
523 ","
524 " payloadType=%u, audioChannel=%" PRIuS ")",
525 payloadSize, rtpHeader->header.payloadType,
526 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000527
kwiberg55b97fe2016-01-28 05:22:45 -0800528 if (!channel_state_.Get().playing) {
529 // Avoid inserting into NetEQ when we are not playing. Count the
530 // packet as discarded.
531 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
532 "received packet is discarded since playing is not"
533 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000534 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800535 }
536
537 // Push the incoming payload (parsed and ready for decoding) into the ACM
538 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
539 0) {
540 _engineStatisticsPtr->SetLastError(
541 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
542 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
543 return -1;
544 }
545
kwiberg55b97fe2016-01-28 05:22:45 -0800546 int64_t round_trip_time = 0;
547 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
548 NULL);
549
550 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
551 if (!nack_list.empty()) {
552 // Can't use nack_list.data() since it's not supported by all
553 // compilers.
554 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
555 }
556 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000557}
558
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000559bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000560 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000561 RTPHeader header;
562 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
563 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
564 "IncomingPacket invalid RTP header");
565 return false;
566 }
567 header.payload_type_frequency =
568 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
569 if (header.payload_type_frequency < 0)
570 return false;
571 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
572}
573
henrik.lundin42dda502016-05-18 05:36:01 -0700574MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
575 int32_t id,
576 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700577 unsigned int ssrc;
578 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
579 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800580 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700581 bool muted;
582 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
583 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800584 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
585 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
586 // In all likelihood, the audio in this frame is garbage. We return an
587 // error so that the audio mixer module doesn't add it to the mix. As
588 // a result, it won't be played out and the actions skipped here are
589 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700590 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800591 }
henrik.lundina89ab962016-05-18 08:52:45 -0700592
593 if (muted) {
594 // TODO(henrik.lundin): We should be able to do better than this. But we
595 // will have to go through all the cases below where the audio samples may
596 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800597 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700598 }
kwiberg55b97fe2016-01-28 05:22:45 -0800599
kwiberg55b97fe2016-01-28 05:22:45 -0800600 // Convert module ID to internal VoE channel ID
601 audioFrame->id_ = VoEChannelId(audioFrame->id_);
602 // Store speech type for dead-or-alive detection
603 _outputSpeechType = audioFrame->speech_type_;
604
605 ChannelState::State state = channel_state_.Get();
606
kwiberg55b97fe2016-01-28 05:22:45 -0800607 {
608 // Pass the audio buffers to an optional sink callback, before applying
609 // scaling/panning, as that applies to the mix operation.
610 // External recipients of the audio (e.g. via AudioTrack), will do their
611 // own mixing/dynamic processing.
612 rtc::CritScope cs(&_callbackCritSect);
613 if (audio_sink_) {
614 AudioSinkInterface::Data data(
615 &audioFrame->data_[0], audioFrame->samples_per_channel_,
616 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
617 audioFrame->timestamp_);
618 audio_sink_->OnData(data);
619 }
620 }
621
622 float output_gain = 1.0f;
623 float left_pan = 1.0f;
624 float right_pan = 1.0f;
625 {
626 rtc::CritScope cs(&volume_settings_critsect_);
627 output_gain = _outputGain;
628 left_pan = _panLeft;
629 right_pan = _panRight;
630 }
631
632 // Output volume scaling
633 if (output_gain < 0.99f || output_gain > 1.01f) {
634 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
635 }
636
637 // Scale left and/or right channel(s) if stereo and master balance is
638 // active
639
640 if (left_pan != 1.0f || right_pan != 1.0f) {
641 if (audioFrame->num_channels_ == 1) {
642 // Emulate stereo mode since panning is active.
643 // The mono signal is copied to both left and right channels here.
644 AudioFrameOperations::MonoToStereo(audioFrame);
645 }
646 // For true stereo mode (when we are receiving a stereo signal), no
647 // action is needed.
648
649 // Do the panning operation (the audio frame contains stereo at this
650 // stage)
651 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
652 }
653
654 // Mix decoded PCM output with file if file mixing is enabled
655 if (state.output_file_playing) {
656 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700657 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800658 }
659
kwiberg55b97fe2016-01-28 05:22:45 -0800660 // Record playout if enabled
661 {
662 rtc::CritScope cs(&_fileCritSect);
663
kwiberg5a25d952016-08-17 07:31:12 -0700664 if (_outputFileRecording && output_file_recorder_) {
665 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800666 }
667 }
668
669 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700670 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800671 _outputAudioLevel.ComputeLevel(*audioFrame);
672
673 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
674 // The first frame with a valid rtp timestamp.
675 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
676 }
677
678 if (capture_start_rtp_time_stamp_ >= 0) {
679 // audioFrame.timestamp_ should be valid from now on.
680
681 // Compute elapsed time.
682 int64_t unwrap_timestamp =
683 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
684 audioFrame->elapsed_time_ms_ =
685 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700686 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800687
niklase@google.com470e71d2011-07-07 08:21:25 +0000688 {
kwiberg55b97fe2016-01-28 05:22:45 -0800689 rtc::CritScope lock(&ts_stats_lock_);
690 // Compute ntp time.
691 audioFrame->ntp_time_ms_ =
692 ntp_estimator_.Estimate(audioFrame->timestamp_);
693 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
694 if (audioFrame->ntp_time_ms_ > 0) {
695 // Compute |capture_start_ntp_time_ms_| so that
696 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
697 capture_start_ntp_time_ms_ =
698 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000699 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000700 }
kwiberg55b97fe2016-01-28 05:22:45 -0800701 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000702
henrik.lundin42dda502016-05-18 05:36:01 -0700703 return muted ? MixerParticipant::AudioFrameInfo::kMuted
704 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000705}
706
aleloi6c278492016-10-20 14:24:39 -0700707AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
708 int sample_rate_hz,
709 AudioFrame* audio_frame) {
710 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700711
aleloi6c278492016-10-20 14:24:39 -0700712 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700713
714 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
715 FrameInfo new_audio_frame_info = FrameInfo::kError;
716 switch (frame_info) {
717 case MixerParticipant::AudioFrameInfo::kNormal:
718 new_audio_frame_info = FrameInfo::kNormal;
719 break;
720 case MixerParticipant::AudioFrameInfo::kMuted:
721 new_audio_frame_info = FrameInfo::kMuted;
722 break;
723 case MixerParticipant::AudioFrameInfo::kError:
724 new_audio_frame_info = FrameInfo::kError;
725 break;
726 }
aleloi6c278492016-10-20 14:24:39 -0700727 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700728}
729
kwiberg55b97fe2016-01-28 05:22:45 -0800730int32_t Channel::NeededFrequency(int32_t id) const {
731 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
732 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000733
kwiberg55b97fe2016-01-28 05:22:45 -0800734 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
kwiberg55b97fe2016-01-28 05:22:45 -0800736 // Determine highest needed receive frequency
737 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000738
kwiberg55b97fe2016-01-28 05:22:45 -0800739 // Return the bigger of playout and receive frequency in the ACM.
740 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
741 highestNeeded = audio_coding_->PlayoutFrequency();
742 } else {
743 highestNeeded = receiveFrequency;
744 }
745
746 // Special case, if we're playing a file on the playout side
747 // we take that frequency into consideration as well
748 // This is not needed on sending side, since the codec will
749 // limit the spectrum anyway.
750 if (channel_state_.Get().output_file_playing) {
751 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700752 if (output_file_player_) {
753 if (output_file_player_->Frequency() > highestNeeded) {
754 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800755 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000756 }
kwiberg55b97fe2016-01-28 05:22:45 -0800757 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000758
kwiberg55b97fe2016-01-28 05:22:45 -0800759 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000760}
761
ossu5f7cfa52016-05-30 08:11:28 -0700762int32_t Channel::CreateChannel(
763 Channel*& channel,
764 int32_t channelId,
765 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700766 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800767 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
768 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
769 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000770
solenberg88499ec2016-09-07 07:34:41 -0700771 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800772 if (channel == NULL) {
773 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
774 "Channel::CreateChannel() unable to allocate memory for"
775 " channel");
776 return -1;
777 }
778 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000779}
780
kwiberg55b97fe2016-01-28 05:22:45 -0800781void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
782 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
783 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
784 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000785
kwiberg55b97fe2016-01-28 05:22:45 -0800786 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000787}
788
kwiberg55b97fe2016-01-28 05:22:45 -0800789void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
790 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
791 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
792 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000793
kwiberg55b97fe2016-01-28 05:22:45 -0800794 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000795}
796
kwiberg55b97fe2016-01-28 05:22:45 -0800797void Channel::PlayFileEnded(int32_t id) {
798 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
799 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000800
kwiberg55b97fe2016-01-28 05:22:45 -0800801 if (id == _inputFilePlayerId) {
802 channel_state_.SetInputFilePlaying(false);
803 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
804 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000805 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800806 } else if (id == _outputFilePlayerId) {
807 channel_state_.SetOutputFilePlaying(false);
808 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
809 "Channel::PlayFileEnded() => output file player module is"
810 " shutdown");
811 }
812}
813
814void Channel::RecordFileEnded(int32_t id) {
815 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
816 "Channel::RecordFileEnded(id=%d)", id);
817
818 assert(id == _outputFileRecorderId);
819
820 rtc::CritScope cs(&_fileCritSect);
821
822 _outputFileRecording = false;
823 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
824 "Channel::RecordFileEnded() => output file recorder module is"
825 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000826}
827
pbos@webrtc.org92135212013-05-14 08:31:39 +0000828Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000829 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700830 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800831 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100832 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700833 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800834 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100835 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800836 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100837 rtp_receive_statistics_(
838 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
839 rtp_receiver_(
840 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100841 this,
842 this,
843 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700844 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100845 _outputAudioLevel(),
846 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100847 // Avoid conflict with other channels by adding 1024 - 1026,
848 // won't use as much as 1024 channels.
849 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
850 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
851 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
852 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100853 _timeStamp(0), // This is just an offset, RTP module will add it's own
854 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100855 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100856 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100857 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100858 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100859 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
860 capture_start_rtp_time_stamp_(-1),
861 capture_start_ntp_time_ms_(-1),
862 _engineStatisticsPtr(NULL),
863 _outputMixerPtr(NULL),
864 _transmitMixerPtr(NULL),
865 _moduleProcessThreadPtr(NULL),
866 _audioDeviceModulePtr(NULL),
867 _voiceEngineObserverPtr(NULL),
868 _callbackCritSectPtr(NULL),
869 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100870 _sendFrameType(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700872 input_mute_(false),
873 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 _panLeft(1.0f),
875 _panRight(1.0f),
876 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100877 _lastLocalTimeStamp(0),
878 _lastPayloadType(0),
879 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800880 transport_overhead_per_packet_(0),
881 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 restored_packet_in_use_(false),
884 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700886 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800887 feedback_observer_proxy_(new TransportFeedbackProxy()),
888 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700889 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200890 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
891 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800892 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800893 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
894 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700895 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800896 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700897 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800898 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200899
kwiberg55b97fe2016-01-28 05:22:45 -0800900 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000901
kwiberg55b97fe2016-01-28 05:22:45 -0800902 RtpRtcp::Configuration configuration;
903 configuration.audio = true;
904 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800905 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800906 configuration.receive_statistics = rtp_receive_statistics_.get();
907 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800908 if (pacing_enabled_) {
909 configuration.paced_sender = rtp_packet_sender_proxy_.get();
910 configuration.transport_sequence_number_allocator =
911 seq_num_allocator_proxy_.get();
912 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
913 }
ivoc14d5dbe2016-07-04 07:06:55 -0700914 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800915 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200916 configuration.retransmission_rate_limiter =
917 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000918
kwiberg55b97fe2016-01-28 05:22:45 -0800919 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100920 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000921}
922
kwiberg55b97fe2016-01-28 05:22:45 -0800923Channel::~Channel() {
924 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
925 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
926 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000927
kwiberg55b97fe2016-01-28 05:22:45 -0800928 StopSend();
929 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000930
kwiberg55b97fe2016-01-28 05:22:45 -0800931 {
932 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700933 if (input_file_player_) {
934 input_file_player_->RegisterModuleFileCallback(NULL);
935 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000936 }
kwiberg5a25d952016-08-17 07:31:12 -0700937 if (output_file_player_) {
938 output_file_player_->RegisterModuleFileCallback(NULL);
939 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800940 }
kwiberg5a25d952016-08-17 07:31:12 -0700941 if (output_file_recorder_) {
942 output_file_recorder_->RegisterModuleFileCallback(NULL);
943 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800944 }
945 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000946
kwiberg55b97fe2016-01-28 05:22:45 -0800947 // The order to safely shutdown modules in a channel is:
948 // 1. De-register callbacks in modules
949 // 2. De-register modules in process thread
950 // 3. Destroy modules
951 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
952 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
953 "~Channel() failed to de-register transport callback"
954 " (Audio coding module)");
955 }
956 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
957 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
958 "~Channel() failed to de-register VAD callback"
959 " (Audio coding module)");
960 }
961 // De-register modules in process thread
962 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000963
kwiberg55b97fe2016-01-28 05:22:45 -0800964 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000965}
966
kwiberg55b97fe2016-01-28 05:22:45 -0800967int32_t Channel::Init() {
968 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
969 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000970
kwiberg55b97fe2016-01-28 05:22:45 -0800971 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000972
kwiberg55b97fe2016-01-28 05:22:45 -0800973 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000974
kwiberg55b97fe2016-01-28 05:22:45 -0800975 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
976 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
977 "Channel::Init() must call SetEngineInformation() first");
978 return -1;
979 }
980
981 // --- Add modules to process thread (for periodic schedulation)
982
983 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
984
985 // --- ACM initialization
986
987 if (audio_coding_->InitializeReceiver() == -1) {
988 _engineStatisticsPtr->SetLastError(
989 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
990 "Channel::Init() unable to initialize the ACM - 1");
991 return -1;
992 }
993
994 // --- RTP/RTCP module initialization
995
996 // Ensure that RTCP is enabled by default for the created channel.
997 // Note that, the module will keep generating RTCP until it is explicitly
998 // disabled by the user.
999 // After StopListen (when no sockets exists), RTCP packets will no longer
1000 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001001 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001002 // RTCP is enabled by default.
1003 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1004 // --- Register all permanent callbacks
1005 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1006 (audio_coding_->RegisterVADCallback(this) == -1);
1007
1008 if (fail) {
1009 _engineStatisticsPtr->SetLastError(
1010 VE_CANNOT_INIT_CHANNEL, kTraceError,
1011 "Channel::Init() callbacks not registered");
1012 return -1;
1013 }
1014
1015 // --- Register all supported codecs to the receiving side of the
1016 // RTP/RTCP module
1017
1018 CodecInst codec;
1019 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1020
1021 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1022 // Open up the RTP/RTCP receiver for all supported codecs
1023 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001024 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001025 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1026 "Channel::Init() unable to register %s "
1027 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1028 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1029 codec.rate);
1030 } else {
1031 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1032 "Channel::Init() %s (%d/%d/%" PRIuS
1033 "/%d) has been "
1034 "added to the RTP/RTCP receiver",
1035 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1036 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001037 }
1038
kwiberg55b97fe2016-01-28 05:22:45 -08001039 // Ensure that PCMU is used as default codec on the sending side
1040 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1041 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001042 }
1043
kwiberg55b97fe2016-01-28 05:22:45 -08001044 // Register default PT for outband 'telephone-event'
1045 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001046 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001047 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1048 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001049 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1050 "Channel::Init() failed to register outband "
1051 "'telephone-event' (%d/%d) correctly",
1052 codec.pltype, codec.plfreq);
1053 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001054 }
1055
kwiberg55b97fe2016-01-28 05:22:45 -08001056 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001057 if (!codec_manager_.RegisterEncoder(codec) ||
1058 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001059 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1060 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001061 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001062 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1063 "Channel::Init() failed to register CN (%d/%d) "
1064 "correctly - 1",
1065 codec.pltype, codec.plfreq);
1066 }
1067 }
kwiberg55b97fe2016-01-28 05:22:45 -08001068 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001069
kwiberg55b97fe2016-01-28 05:22:45 -08001070 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001071}
1072
kwiberg55b97fe2016-01-28 05:22:45 -08001073int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1074 OutputMixer& outputMixer,
1075 voe::TransmitMixer& transmitMixer,
1076 ProcessThread& moduleProcessThread,
1077 AudioDeviceModule& audioDeviceModule,
1078 VoiceEngineObserver* voiceEngineObserver,
1079 rtc::CriticalSection* callbackCritSect) {
1080 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1081 "Channel::SetEngineInformation()");
1082 _engineStatisticsPtr = &engineStatistics;
1083 _outputMixerPtr = &outputMixer;
1084 _transmitMixerPtr = &transmitMixer,
1085 _moduleProcessThreadPtr = &moduleProcessThread;
1086 _audioDeviceModulePtr = &audioDeviceModule;
1087 _voiceEngineObserverPtr = voiceEngineObserver;
1088 _callbackCritSectPtr = callbackCritSect;
1089 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001090}
1091
kwiberg55b97fe2016-01-28 05:22:45 -08001092int32_t Channel::UpdateLocalTimeStamp() {
1093 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1094 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001095}
1096
kwibergb7f89d62016-02-17 10:04:18 -08001097void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001098 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001099 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001100}
1101
ossu29b1a8d2016-06-13 07:34:51 -07001102const rtc::scoped_refptr<AudioDecoderFactory>&
1103Channel::GetAudioDecoderFactory() const {
1104 return decoder_factory_;
1105}
1106
kwiberg55b97fe2016-01-28 05:22:45 -08001107int32_t Channel::StartPlayout() {
1108 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1109 "Channel::StartPlayout()");
1110 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001111 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001112 }
1113
solenberge374e012017-02-14 04:55:00 -08001114 // Add participant as candidates for mixing.
1115 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1116 _engineStatisticsPtr->SetLastError(
1117 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1118 "StartPlayout() failed to add participant to mixer");
1119 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001120 }
1121
1122 channel_state_.SetPlaying(true);
1123 if (RegisterFilePlayingToMixer() != 0)
1124 return -1;
1125
1126 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001127}
1128
kwiberg55b97fe2016-01-28 05:22:45 -08001129int32_t Channel::StopPlayout() {
1130 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1131 "Channel::StopPlayout()");
1132 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001134 }
1135
solenberge374e012017-02-14 04:55:00 -08001136 // Remove participant as candidates for mixing
1137 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1138 _engineStatisticsPtr->SetLastError(
1139 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1140 "StopPlayout() failed to remove participant from mixer");
1141 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001142 }
1143
1144 channel_state_.SetPlaying(false);
1145 _outputAudioLevel.Clear();
1146
1147 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001148}
1149
kwiberg55b97fe2016-01-28 05:22:45 -08001150int32_t Channel::StartSend() {
1151 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1152 "Channel::StartSend()");
1153 // Resume the previous sequence number which was reset by StopSend().
1154 // This needs to be done before |sending| is set to true.
1155 if (send_sequence_number_)
1156 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001157
kwiberg55b97fe2016-01-28 05:22:45 -08001158 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001159 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001160 }
1161 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001162
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001163 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001164 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1165 _engineStatisticsPtr->SetLastError(
1166 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1167 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001168 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001169 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001170 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001171 return -1;
1172 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001173
kwiberg55b97fe2016-01-28 05:22:45 -08001174 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001175}
1176
kwiberg55b97fe2016-01-28 05:22:45 -08001177int32_t Channel::StopSend() {
1178 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1179 "Channel::StopSend()");
1180 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001181 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001182 }
1183 channel_state_.SetSending(false);
1184
1185 // Store the sequence number to be able to pick up the same sequence for
1186 // the next StartSend(). This is needed for restarting device, otherwise
1187 // it might cause libSRTP to complain about packets being replayed.
1188 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1189 // CL is landed. See issue
1190 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1191 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1192
1193 // Reset sending SSRC and sequence number and triggers direct transmission
1194 // of RTCP BYE
1195 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1196 _engineStatisticsPtr->SetLastError(
1197 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1198 "StartSend() RTP/RTCP failed to stop sending");
1199 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001200 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001201
1202 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001203}
1204
kwiberg55b97fe2016-01-28 05:22:45 -08001205int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1206 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1207 "Channel::RegisterVoiceEngineObserver()");
1208 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001209
kwiberg55b97fe2016-01-28 05:22:45 -08001210 if (_voiceEngineObserverPtr) {
1211 _engineStatisticsPtr->SetLastError(
1212 VE_INVALID_OPERATION, kTraceError,
1213 "RegisterVoiceEngineObserver() observer already enabled");
1214 return -1;
1215 }
1216 _voiceEngineObserverPtr = &observer;
1217 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001218}
1219
kwiberg55b97fe2016-01-28 05:22:45 -08001220int32_t Channel::DeRegisterVoiceEngineObserver() {
1221 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1222 "Channel::DeRegisterVoiceEngineObserver()");
1223 rtc::CritScope cs(&_callbackCritSect);
1224
1225 if (!_voiceEngineObserverPtr) {
1226 _engineStatisticsPtr->SetLastError(
1227 VE_INVALID_OPERATION, kTraceWarning,
1228 "DeRegisterVoiceEngineObserver() observer already disabled");
1229 return 0;
1230 }
1231 _voiceEngineObserverPtr = NULL;
1232 return 0;
1233}
1234
1235int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001236 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001237 if (send_codec) {
1238 codec = *send_codec;
1239 return 0;
1240 }
1241 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001242}
1243
kwiberg55b97fe2016-01-28 05:22:45 -08001244int32_t Channel::GetRecCodec(CodecInst& codec) {
1245 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001246}
1247
kwiberg55b97fe2016-01-28 05:22:45 -08001248int32_t Channel::SetSendCodec(const CodecInst& codec) {
1249 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1250 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001251
kwibergc8d071e2016-04-06 12:22:38 -07001252 if (!codec_manager_.RegisterEncoder(codec) ||
1253 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001254 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1255 "SetSendCodec() failed to register codec to ACM");
1256 return -1;
1257 }
1258
1259 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1260 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1261 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1262 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1263 "SetSendCodec() failed to register codec to"
1264 " RTP/RTCP module");
1265 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001266 }
kwiberg55b97fe2016-01-28 05:22:45 -08001267 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001268
kwiberg55b97fe2016-01-28 05:22:45 -08001269 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001270}
1271
minyue78b4d562016-11-30 04:47:39 -08001272void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001273 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1274 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001275 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001276 if (*encoder) {
1277 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001278 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001279 }
1280 });
michaelt566d8202017-01-12 10:17:38 -08001281 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001282}
1283
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001284void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001285 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1286 if (*encoder)
1287 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1288 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001289}
1290
kwiberg55b97fe2016-01-28 05:22:45 -08001291int32_t Channel::SetVADStatus(bool enableVAD,
1292 ACMVADMode mode,
1293 bool disableDTX) {
1294 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1295 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001296 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1297 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1298 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001299 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1300 kTraceError,
1301 "SetVADStatus() failed to set VAD");
1302 return -1;
1303 }
1304 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001305}
1306
kwiberg55b97fe2016-01-28 05:22:45 -08001307int32_t Channel::GetVADStatus(bool& enabledVAD,
1308 ACMVADMode& mode,
1309 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001310 const auto* params = codec_manager_.GetStackParams();
1311 enabledVAD = params->use_cng;
1312 mode = params->vad_mode;
1313 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001314 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001315}
1316
kwiberg55b97fe2016-01-28 05:22:45 -08001317int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001318 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1319}
1320
1321int32_t Channel::SetRecPayloadType(int payload_type,
1322 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001323 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1324 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001325
kwiberg55b97fe2016-01-28 05:22:45 -08001326 if (channel_state_.Get().playing) {
1327 _engineStatisticsPtr->SetLastError(
1328 VE_ALREADY_PLAYING, kTraceError,
1329 "SetRecPayloadType() unable to set PT while playing");
1330 return -1;
1331 }
kwiberg55b97fe2016-01-28 05:22:45 -08001332
kwibergd32bf752017-01-19 07:03:59 -08001333 const CodecInst codec = [&] {
1334 CodecInst c = SdpToCodecInst(payload_type, format);
1335
1336 // Bug 6986: Emulate an old bug that caused us to always choose to decode
1337 // Opus in stereo. To be able to remove this, we first need to fix the
1338 // other half of bug 6986, which is about losing the Opus "stereo"
1339 // parameter.
1340 // TODO(kwiberg): Remove this special case, a.k.a. fix bug 6986.
1341 if (STR_CASE_CMP(codec.plname, "opus") == 0) {
1342 c.channels = 2;
1343 }
1344
1345 return c;
1346 }();
1347
1348 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001349 // De-register the selected codec (RTP/RTCP module and ACM)
1350
1351 int8_t pltype(-1);
1352 CodecInst rxCodec = codec;
1353
1354 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001355 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001356 rxCodec.pltype = pltype;
1357
1358 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1359 _engineStatisticsPtr->SetLastError(
1360 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1361 "SetRecPayloadType() RTP/RTCP-module deregistration "
1362 "failed");
1363 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001364 }
kwiberg55b97fe2016-01-28 05:22:45 -08001365 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1366 _engineStatisticsPtr->SetLastError(
1367 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1368 "SetRecPayloadType() ACM deregistration failed - 1");
1369 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001370 }
kwiberg55b97fe2016-01-28 05:22:45 -08001371 return 0;
1372 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001373
magjed56124bd2016-11-24 09:34:46 -08001374 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001375 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001376 // TODO(kwiberg): Retrying is probably not necessary, since
1377 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001378 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001379 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001380 _engineStatisticsPtr->SetLastError(
1381 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1382 "SetRecPayloadType() RTP/RTCP-module registration failed");
1383 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001384 }
kwiberg55b97fe2016-01-28 05:22:45 -08001385 }
kwibergd32bf752017-01-19 07:03:59 -08001386 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1387 audio_coding_->UnregisterReceiveCodec(payload_type);
1388 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001389 _engineStatisticsPtr->SetLastError(
1390 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1391 "SetRecPayloadType() ACM registration failed - 1");
1392 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001393 }
kwiberg55b97fe2016-01-28 05:22:45 -08001394 }
1395 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001396}
1397
kwiberg55b97fe2016-01-28 05:22:45 -08001398int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1399 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001400 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001401 _engineStatisticsPtr->SetLastError(
1402 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1403 "GetRecPayloadType() failed to retrieve RX payload type");
1404 return -1;
1405 }
1406 codec.pltype = payloadType;
1407 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001408}
1409
kwiberg55b97fe2016-01-28 05:22:45 -08001410int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1411 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1412 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001413
kwiberg55b97fe2016-01-28 05:22:45 -08001414 CodecInst codec;
1415 int32_t samplingFreqHz(-1);
1416 const size_t kMono = 1;
1417 if (frequency == kFreq32000Hz)
1418 samplingFreqHz = 32000;
1419 else if (frequency == kFreq16000Hz)
1420 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001421
kwiberg55b97fe2016-01-28 05:22:45 -08001422 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1423 _engineStatisticsPtr->SetLastError(
1424 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1425 "SetSendCNPayloadType() failed to retrieve default CN codec "
1426 "settings");
1427 return -1;
1428 }
1429
1430 // Modify the payload type (must be set to dynamic range)
1431 codec.pltype = type;
1432
kwibergc8d071e2016-04-06 12:22:38 -07001433 if (!codec_manager_.RegisterEncoder(codec) ||
1434 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001435 _engineStatisticsPtr->SetLastError(
1436 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1437 "SetSendCNPayloadType() failed to register CN to ACM");
1438 return -1;
1439 }
1440
1441 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1442 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1443 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1444 _engineStatisticsPtr->SetLastError(
1445 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1446 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1447 "module");
1448 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001449 }
kwiberg55b97fe2016-01-28 05:22:45 -08001450 }
1451 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001452}
1453
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001454int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001455 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001456 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001457
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001458 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001459 _engineStatisticsPtr->SetLastError(
1460 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001461 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001462 return -1;
1463 }
1464 return 0;
1465}
1466
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001467int Channel::SetOpusDtx(bool enable_dtx) {
1468 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1469 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001470 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001471 : audio_coding_->DisableOpusDtx();
1472 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001473 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1474 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001475 return -1;
1476 }
1477 return 0;
1478}
1479
ivoc85228d62016-07-27 04:53:47 -07001480int Channel::GetOpusDtx(bool* enabled) {
1481 int success = -1;
1482 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1483 if (encoder) {
1484 *enabled = encoder->GetDtx();
1485 success = 0;
1486 }
1487 });
1488 return success;
1489}
1490
minyue7e304322016-10-12 05:00:55 -07001491bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1492 bool success = false;
1493 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1494 if (*encoder) {
1495 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001496 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001497 }
1498 });
1499 return success;
1500}
1501
1502void Channel::DisableAudioNetworkAdaptor() {
1503 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1504 if (*encoder)
1505 (*encoder)->DisableAudioNetworkAdaptor();
1506 });
1507}
1508
1509void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1510 int max_frame_length_ms) {
1511 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1512 if (*encoder) {
1513 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1514 max_frame_length_ms);
1515 }
1516 });
1517}
1518
mflodman3d7db262016-04-29 00:57:13 -07001519int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001520 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001521 "Channel::RegisterExternalTransport()");
1522
kwiberg55b97fe2016-01-28 05:22:45 -08001523 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001524 if (_externalTransport) {
1525 _engineStatisticsPtr->SetLastError(
1526 VE_INVALID_OPERATION, kTraceError,
1527 "RegisterExternalTransport() external transport already enabled");
1528 return -1;
1529 }
1530 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001531 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001532 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001533}
1534
kwiberg55b97fe2016-01-28 05:22:45 -08001535int32_t Channel::DeRegisterExternalTransport() {
1536 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1537 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001538
kwiberg55b97fe2016-01-28 05:22:45 -08001539 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001540 if (_transportPtr) {
1541 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1542 "DeRegisterExternalTransport() all transport is disabled");
1543 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001544 _engineStatisticsPtr->SetLastError(
1545 VE_INVALID_OPERATION, kTraceWarning,
1546 "DeRegisterExternalTransport() external transport already "
1547 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001548 }
1549 _externalTransport = false;
1550 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001551 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001552}
1553
mflodman3d7db262016-04-29 00:57:13 -07001554int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001555 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001556 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001557 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001558 "Channel::ReceivedRTPPacket()");
1559
1560 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001561 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001562
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001563 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001564 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1565 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1566 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001567 return -1;
1568 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001569 header.payload_type_frequency =
1570 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001571 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001572 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001573 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001574 rtp_receive_statistics_->IncomingPacket(
1575 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001576 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001577
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001578 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001579}
1580
1581bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001582 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001583 const RTPHeader& header,
1584 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001585 if (rtp_payload_registry_->IsRtx(header)) {
1586 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001587 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001588 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001589 assert(packet_length >= header.headerLength);
1590 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001591 PayloadUnion payload_specific;
1592 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001593 &payload_specific)) {
1594 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001595 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001596 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1597 payload_specific, in_order);
1598}
1599
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001600bool Channel::HandleRtxPacket(const uint8_t* packet,
1601 size_t packet_length,
1602 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001603 if (!rtp_payload_registry_->IsRtx(header))
1604 return false;
1605
1606 // Remove the RTX header and parse the original RTP header.
1607 if (packet_length < header.headerLength)
1608 return false;
1609 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1610 return false;
1611 if (restored_packet_in_use_) {
1612 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1613 "Multiple RTX headers detected, dropping packet");
1614 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001615 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001616 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001617 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1618 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001619 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1620 "Incoming RTX packet: invalid RTP header");
1621 return false;
1622 }
1623 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001624 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001625 restored_packet_in_use_ = false;
1626 return ret;
1627}
1628
1629bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1630 StreamStatistician* statistician =
1631 rtp_receive_statistics_->GetStatistician(header.ssrc);
1632 if (!statistician)
1633 return false;
1634 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001635}
1636
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001637bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1638 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001639 // Retransmissions are handled separately if RTX is enabled.
1640 if (rtp_payload_registry_->RtxEnabled())
1641 return false;
1642 StreamStatistician* statistician =
1643 rtp_receive_statistics_->GetStatistician(header.ssrc);
1644 if (!statistician)
1645 return false;
1646 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001647 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001648 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001649 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001650}
1651
mflodman3d7db262016-04-29 00:57:13 -07001652int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001653 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001654 "Channel::ReceivedRTCPPacket()");
1655 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001656 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001657
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001658 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001659 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001660 _engineStatisticsPtr->SetLastError(
1661 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1662 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1663 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001664
Minyue2013aec2015-05-13 14:14:42 +02001665 int64_t rtt = GetRTT(true);
1666 if (rtt == 0) {
1667 // Waiting for valid RTT.
1668 return 0;
1669 }
Erik Språng737336d2016-07-29 12:59:36 +02001670
1671 int64_t nack_window_ms = rtt;
1672 if (nack_window_ms < kMinRetransmissionWindowMs) {
1673 nack_window_ms = kMinRetransmissionWindowMs;
1674 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1675 nack_window_ms = kMaxRetransmissionWindowMs;
1676 }
1677 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1678
minyue7e304322016-10-12 05:00:55 -07001679 // Invoke audio encoders OnReceivedRtt().
1680 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1681 if (*encoder)
1682 (*encoder)->OnReceivedRtt(rtt);
1683 });
1684
Minyue2013aec2015-05-13 14:14:42 +02001685 uint32_t ntp_secs = 0;
1686 uint32_t ntp_frac = 0;
1687 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001688 if (0 !=
1689 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1690 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001691 // Waiting for RTCP.
1692 return 0;
1693 }
1694
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001695 {
tommi31fc21f2016-01-21 10:37:37 -08001696 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001697 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001698 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001699 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001700}
1701
niklase@google.com470e71d2011-07-07 08:21:25 +00001702int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001703 bool loop,
1704 FileFormats format,
1705 int startPosition,
1706 float volumeScaling,
1707 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001708 const CodecInst* codecInst) {
1709 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1710 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1711 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1712 "stopPosition=%d)",
1713 fileName, loop, format, volumeScaling, startPosition,
1714 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001715
kwiberg55b97fe2016-01-28 05:22:45 -08001716 if (channel_state_.Get().output_file_playing) {
1717 _engineStatisticsPtr->SetLastError(
1718 VE_ALREADY_PLAYING, kTraceError,
1719 "StartPlayingFileLocally() is already playing");
1720 return -1;
1721 }
1722
1723 {
1724 rtc::CritScope cs(&_fileCritSect);
1725
kwiberg5a25d952016-08-17 07:31:12 -07001726 if (output_file_player_) {
1727 output_file_player_->RegisterModuleFileCallback(NULL);
1728 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001729 }
1730
kwiberg5b356f42016-09-08 04:32:33 -07001731 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001732 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001733
kwiberg5a25d952016-08-17 07:31:12 -07001734 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001735 _engineStatisticsPtr->SetLastError(
1736 VE_INVALID_ARGUMENT, kTraceError,
1737 "StartPlayingFileLocally() filePlayer format is not correct");
1738 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001739 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001740
kwiberg55b97fe2016-01-28 05:22:45 -08001741 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001742
kwiberg5a25d952016-08-17 07:31:12 -07001743 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001744 fileName, loop, startPosition, volumeScaling, notificationTime,
1745 stopPosition, (const CodecInst*)codecInst) != 0) {
1746 _engineStatisticsPtr->SetLastError(
1747 VE_BAD_FILE, kTraceError,
1748 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001749 output_file_player_->StopPlayingFile();
1750 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001751 return -1;
1752 }
kwiberg5a25d952016-08-17 07:31:12 -07001753 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001754 channel_state_.SetOutputFilePlaying(true);
1755 }
1756
1757 if (RegisterFilePlayingToMixer() != 0)
1758 return -1;
1759
1760 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001761}
1762
1763int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001764 FileFormats format,
1765 int startPosition,
1766 float volumeScaling,
1767 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001768 const CodecInst* codecInst) {
1769 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1770 "Channel::StartPlayingFileLocally(format=%d,"
1771 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1772 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001773
kwiberg55b97fe2016-01-28 05:22:45 -08001774 if (stream == NULL) {
1775 _engineStatisticsPtr->SetLastError(
1776 VE_BAD_FILE, kTraceError,
1777 "StartPlayingFileLocally() NULL as input stream");
1778 return -1;
1779 }
1780
1781 if (channel_state_.Get().output_file_playing) {
1782 _engineStatisticsPtr->SetLastError(
1783 VE_ALREADY_PLAYING, kTraceError,
1784 "StartPlayingFileLocally() is already playing");
1785 return -1;
1786 }
1787
1788 {
1789 rtc::CritScope cs(&_fileCritSect);
1790
1791 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001792 if (output_file_player_) {
1793 output_file_player_->RegisterModuleFileCallback(NULL);
1794 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001795 }
1796
kwiberg55b97fe2016-01-28 05:22:45 -08001797 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001798 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001799 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001800
kwiberg5a25d952016-08-17 07:31:12 -07001801 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001802 _engineStatisticsPtr->SetLastError(
1803 VE_INVALID_ARGUMENT, kTraceError,
1804 "StartPlayingFileLocally() filePlayer format isnot correct");
1805 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001806 }
1807
kwiberg55b97fe2016-01-28 05:22:45 -08001808 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001809
kwiberg4ec01d92016-08-22 08:43:54 -07001810 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001811 volumeScaling, notificationTime,
1812 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001813 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1814 "StartPlayingFile() failed to "
1815 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001816 output_file_player_->StopPlayingFile();
1817 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001818 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001819 }
kwiberg5a25d952016-08-17 07:31:12 -07001820 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001821 channel_state_.SetOutputFilePlaying(true);
1822 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001823
kwiberg55b97fe2016-01-28 05:22:45 -08001824 if (RegisterFilePlayingToMixer() != 0)
1825 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001826
kwiberg55b97fe2016-01-28 05:22:45 -08001827 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001828}
1829
kwiberg55b97fe2016-01-28 05:22:45 -08001830int Channel::StopPlayingFileLocally() {
1831 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1832 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001833
kwiberg55b97fe2016-01-28 05:22:45 -08001834 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001835 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001836 }
1837
1838 {
1839 rtc::CritScope cs(&_fileCritSect);
1840
kwiberg5a25d952016-08-17 07:31:12 -07001841 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001842 _engineStatisticsPtr->SetLastError(
1843 VE_STOP_RECORDING_FAILED, kTraceError,
1844 "StopPlayingFile() could not stop playing");
1845 return -1;
1846 }
kwiberg5a25d952016-08-17 07:31:12 -07001847 output_file_player_->RegisterModuleFileCallback(NULL);
1848 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001849 channel_state_.SetOutputFilePlaying(false);
1850 }
1851 // _fileCritSect cannot be taken while calling
1852 // SetAnonymousMixibilityStatus. Refer to comments in
1853 // StartPlayingFileLocally(const char* ...) for more details.
1854 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1855 _engineStatisticsPtr->SetLastError(
1856 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1857 "StopPlayingFile() failed to stop participant from playing as"
1858 "file in the mixer");
1859 return -1;
1860 }
1861
1862 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001863}
1864
kwiberg55b97fe2016-01-28 05:22:45 -08001865int Channel::IsPlayingFileLocally() const {
1866 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001867}
1868
kwiberg55b97fe2016-01-28 05:22:45 -08001869int Channel::RegisterFilePlayingToMixer() {
1870 // Return success for not registering for file playing to mixer if:
1871 // 1. playing file before playout is started on that channel.
1872 // 2. starting playout without file playing on that channel.
1873 if (!channel_state_.Get().playing ||
1874 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001875 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001876 }
1877
1878 // |_fileCritSect| cannot be taken while calling
1879 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1880 // frames can be pulled by the mixer. Since the frames are generated from
1881 // the file, _fileCritSect will be taken. This would result in a deadlock.
1882 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1883 channel_state_.SetOutputFilePlaying(false);
1884 rtc::CritScope cs(&_fileCritSect);
1885 _engineStatisticsPtr->SetLastError(
1886 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1887 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001888 output_file_player_->StopPlayingFile();
1889 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001890 return -1;
1891 }
1892
1893 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001894}
1895
niklase@google.com470e71d2011-07-07 08:21:25 +00001896int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001897 bool loop,
1898 FileFormats format,
1899 int startPosition,
1900 float volumeScaling,
1901 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001902 const CodecInst* codecInst) {
1903 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1904 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1905 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1906 "stopPosition=%d)",
1907 fileName, loop, format, volumeScaling, startPosition,
1908 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001909
kwiberg55b97fe2016-01-28 05:22:45 -08001910 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001911
kwiberg55b97fe2016-01-28 05:22:45 -08001912 if (channel_state_.Get().input_file_playing) {
1913 _engineStatisticsPtr->SetLastError(
1914 VE_ALREADY_PLAYING, kTraceWarning,
1915 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001916 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001917 }
1918
1919 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001920 if (input_file_player_) {
1921 input_file_player_->RegisterModuleFileCallback(NULL);
1922 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001923 }
1924
1925 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001926 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001927 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001928
kwiberg5a25d952016-08-17 07:31:12 -07001929 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001930 _engineStatisticsPtr->SetLastError(
1931 VE_INVALID_ARGUMENT, kTraceError,
1932 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1933 return -1;
1934 }
1935
1936 const uint32_t notificationTime(0);
1937
kwiberg5a25d952016-08-17 07:31:12 -07001938 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001939 fileName, loop, startPosition, volumeScaling, notificationTime,
1940 stopPosition, (const CodecInst*)codecInst) != 0) {
1941 _engineStatisticsPtr->SetLastError(
1942 VE_BAD_FILE, kTraceError,
1943 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001944 input_file_player_->StopPlayingFile();
1945 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001946 return -1;
1947 }
kwiberg5a25d952016-08-17 07:31:12 -07001948 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001949 channel_state_.SetInputFilePlaying(true);
1950
1951 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001952}
1953
1954int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001955 FileFormats format,
1956 int startPosition,
1957 float volumeScaling,
1958 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001959 const CodecInst* codecInst) {
1960 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1961 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1962 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1963 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001964
kwiberg55b97fe2016-01-28 05:22:45 -08001965 if (stream == NULL) {
1966 _engineStatisticsPtr->SetLastError(
1967 VE_BAD_FILE, kTraceError,
1968 "StartPlayingFileAsMicrophone NULL as input stream");
1969 return -1;
1970 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001971
kwiberg55b97fe2016-01-28 05:22:45 -08001972 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001973
kwiberg55b97fe2016-01-28 05:22:45 -08001974 if (channel_state_.Get().input_file_playing) {
1975 _engineStatisticsPtr->SetLastError(
1976 VE_ALREADY_PLAYING, kTraceWarning,
1977 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001978 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001979 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001980
kwiberg55b97fe2016-01-28 05:22:45 -08001981 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001982 if (input_file_player_) {
1983 input_file_player_->RegisterModuleFileCallback(NULL);
1984 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001985 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001986
kwiberg55b97fe2016-01-28 05:22:45 -08001987 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001988 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001989 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001990
kwiberg5a25d952016-08-17 07:31:12 -07001991 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001992 _engineStatisticsPtr->SetLastError(
1993 VE_INVALID_ARGUMENT, kTraceError,
1994 "StartPlayingInputFile() filePlayer format isnot correct");
1995 return -1;
1996 }
1997
1998 const uint32_t notificationTime(0);
1999
kwiberg4ec01d92016-08-22 08:43:54 -07002000 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2001 notificationTime, stopPosition,
2002 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002003 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2004 "StartPlayingFile() failed to start "
2005 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002006 input_file_player_->StopPlayingFile();
2007 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002008 return -1;
2009 }
2010
kwiberg5a25d952016-08-17 07:31:12 -07002011 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002012 channel_state_.SetInputFilePlaying(true);
2013
2014 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002015}
2016
kwiberg55b97fe2016-01-28 05:22:45 -08002017int Channel::StopPlayingFileAsMicrophone() {
2018 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2019 "Channel::StopPlayingFileAsMicrophone()");
2020
2021 rtc::CritScope cs(&_fileCritSect);
2022
2023 if (!channel_state_.Get().input_file_playing) {
2024 return 0;
2025 }
2026
kwiberg5a25d952016-08-17 07:31:12 -07002027 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002028 _engineStatisticsPtr->SetLastError(
2029 VE_STOP_RECORDING_FAILED, kTraceError,
2030 "StopPlayingFile() could not stop playing");
2031 return -1;
2032 }
kwiberg5a25d952016-08-17 07:31:12 -07002033 input_file_player_->RegisterModuleFileCallback(NULL);
2034 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002035 channel_state_.SetInputFilePlaying(false);
2036
2037 return 0;
2038}
2039
2040int Channel::IsPlayingFileAsMicrophone() const {
2041 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002042}
2043
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002044int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002045 const CodecInst* codecInst) {
2046 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2047 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002048
kwiberg55b97fe2016-01-28 05:22:45 -08002049 if (_outputFileRecording) {
2050 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2051 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002052 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002053 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002054
kwiberg55b97fe2016-01-28 05:22:45 -08002055 FileFormats format;
2056 const uint32_t notificationTime(0); // Not supported in VoE
2057 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002058
kwiberg55b97fe2016-01-28 05:22:45 -08002059 if ((codecInst != NULL) &&
2060 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2061 _engineStatisticsPtr->SetLastError(
2062 VE_BAD_ARGUMENT, kTraceError,
2063 "StartRecordingPlayout() invalid compression");
2064 return (-1);
2065 }
2066 if (codecInst == NULL) {
2067 format = kFileFormatPcm16kHzFile;
2068 codecInst = &dummyCodec;
2069 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2070 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2071 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2072 format = kFileFormatWavFile;
2073 } else {
2074 format = kFileFormatCompressedFile;
2075 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002076
kwiberg55b97fe2016-01-28 05:22:45 -08002077 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002078
kwiberg55b97fe2016-01-28 05:22:45 -08002079 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002080 if (output_file_recorder_) {
2081 output_file_recorder_->RegisterModuleFileCallback(NULL);
2082 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002083 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002084
kwiberg5a25d952016-08-17 07:31:12 -07002085 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002086 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002087 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002088 _engineStatisticsPtr->SetLastError(
2089 VE_INVALID_ARGUMENT, kTraceError,
2090 "StartRecordingPlayout() fileRecorder format isnot correct");
2091 return -1;
2092 }
2093
kwiberg5a25d952016-08-17 07:31:12 -07002094 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002095 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2096 _engineStatisticsPtr->SetLastError(
2097 VE_BAD_FILE, kTraceError,
2098 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002099 output_file_recorder_->StopRecording();
2100 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002101 return -1;
2102 }
kwiberg5a25d952016-08-17 07:31:12 -07002103 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002104 _outputFileRecording = true;
2105
2106 return 0;
2107}
2108
2109int Channel::StartRecordingPlayout(OutStream* stream,
2110 const CodecInst* codecInst) {
2111 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2112 "Channel::StartRecordingPlayout()");
2113
2114 if (_outputFileRecording) {
2115 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2116 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002117 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002118 }
2119
2120 FileFormats format;
2121 const uint32_t notificationTime(0); // Not supported in VoE
2122 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2123
2124 if (codecInst != NULL && codecInst->channels != 1) {
2125 _engineStatisticsPtr->SetLastError(
2126 VE_BAD_ARGUMENT, kTraceError,
2127 "StartRecordingPlayout() invalid compression");
2128 return (-1);
2129 }
2130 if (codecInst == NULL) {
2131 format = kFileFormatPcm16kHzFile;
2132 codecInst = &dummyCodec;
2133 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2134 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2135 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2136 format = kFileFormatWavFile;
2137 } else {
2138 format = kFileFormatCompressedFile;
2139 }
2140
2141 rtc::CritScope cs(&_fileCritSect);
2142
2143 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002144 if (output_file_recorder_) {
2145 output_file_recorder_->RegisterModuleFileCallback(NULL);
2146 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002147 }
2148
kwiberg5a25d952016-08-17 07:31:12 -07002149 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002150 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002151 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002152 _engineStatisticsPtr->SetLastError(
2153 VE_INVALID_ARGUMENT, kTraceError,
2154 "StartRecordingPlayout() fileRecorder format isnot correct");
2155 return -1;
2156 }
2157
kwiberg4ec01d92016-08-22 08:43:54 -07002158 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002159 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002160 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2161 "StartRecordingPlayout() failed to "
2162 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002163 output_file_recorder_->StopRecording();
2164 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002165 return -1;
2166 }
2167
kwiberg5a25d952016-08-17 07:31:12 -07002168 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002169 _outputFileRecording = true;
2170
2171 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002172}
2173
kwiberg55b97fe2016-01-28 05:22:45 -08002174int Channel::StopRecordingPlayout() {
2175 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2176 "Channel::StopRecordingPlayout()");
2177
2178 if (!_outputFileRecording) {
2179 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2180 "StopRecordingPlayout() isnot recording");
2181 return -1;
2182 }
2183
2184 rtc::CritScope cs(&_fileCritSect);
2185
kwiberg5a25d952016-08-17 07:31:12 -07002186 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002187 _engineStatisticsPtr->SetLastError(
2188 VE_STOP_RECORDING_FAILED, kTraceError,
2189 "StopRecording() could not stop recording");
2190 return (-1);
2191 }
kwiberg5a25d952016-08-17 07:31:12 -07002192 output_file_recorder_->RegisterModuleFileCallback(NULL);
2193 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002194 _outputFileRecording = false;
2195
2196 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002197}
2198
kwiberg55b97fe2016-01-28 05:22:45 -08002199void Channel::SetMixWithMicStatus(bool mix) {
2200 rtc::CritScope cs(&_fileCritSect);
2201 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002202}
2203
kwiberg55b97fe2016-01-28 05:22:45 -08002204int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2205 int8_t currentLevel = _outputAudioLevel.Level();
2206 level = static_cast<int32_t>(currentLevel);
2207 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002208}
2209
kwiberg55b97fe2016-01-28 05:22:45 -08002210int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2211 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2212 level = static_cast<int32_t>(currentLevel);
2213 return 0;
2214}
2215
solenberg1c2af8e2016-03-24 10:36:00 -07002216int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002217 rtc::CritScope cs(&volume_settings_critsect_);
2218 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002219 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002220 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002221 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002222}
2223
solenberg1c2af8e2016-03-24 10:36:00 -07002224bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002225 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002226 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002227}
2228
kwiberg55b97fe2016-01-28 05:22:45 -08002229int Channel::SetOutputVolumePan(float left, float right) {
2230 rtc::CritScope cs(&volume_settings_critsect_);
2231 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002232 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002233 _panLeft = left;
2234 _panRight = right;
2235 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002236}
2237
kwiberg55b97fe2016-01-28 05:22:45 -08002238int Channel::GetOutputVolumePan(float& left, float& right) const {
2239 rtc::CritScope cs(&volume_settings_critsect_);
2240 left = _panLeft;
2241 right = _panRight;
2242 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002243}
2244
kwiberg55b97fe2016-01-28 05:22:45 -08002245int Channel::SetChannelOutputVolumeScaling(float scaling) {
2246 rtc::CritScope cs(&volume_settings_critsect_);
2247 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002248 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002249 _outputGain = scaling;
2250 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002251}
2252
kwiberg55b97fe2016-01-28 05:22:45 -08002253int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2254 rtc::CritScope cs(&volume_settings_critsect_);
2255 scaling = _outputGain;
2256 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002257}
2258
solenberg8842c3e2016-03-11 03:06:41 -08002259int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002260 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002261 "Channel::SendTelephoneEventOutband(...)");
2262 RTC_DCHECK_LE(0, event);
2263 RTC_DCHECK_GE(255, event);
2264 RTC_DCHECK_LE(0, duration_ms);
2265 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002266 if (!Sending()) {
2267 return -1;
2268 }
solenberg8842c3e2016-03-11 03:06:41 -08002269 if (_rtpRtcpModule->SendTelephoneEventOutband(
2270 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002271 _engineStatisticsPtr->SetLastError(
2272 VE_SEND_DTMF_FAILED, kTraceWarning,
2273 "SendTelephoneEventOutband() failed to send event");
2274 return -1;
2275 }
2276 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002277}
2278
solenbergffbbcac2016-11-17 05:25:37 -08002279int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2280 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002281 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002282 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002283 RTC_DCHECK_LE(0, payload_type);
2284 RTC_DCHECK_GE(127, payload_type);
2285 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002286 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002287 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002288 memcpy(codec.plname, "telephone-event", 16);
2289 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2290 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2291 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2292 _engineStatisticsPtr->SetLastError(
2293 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2294 "SetSendTelephoneEventPayloadType() failed to register send"
2295 "payload type");
2296 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002297 }
kwiberg55b97fe2016-01-28 05:22:45 -08002298 }
kwiberg55b97fe2016-01-28 05:22:45 -08002299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002300}
2301
kwiberg55b97fe2016-01-28 05:22:45 -08002302int Channel::VoiceActivityIndicator(int& activity) {
2303 activity = _sendFrameType;
2304 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002305}
2306
kwiberg55b97fe2016-01-28 05:22:45 -08002307int Channel::SetLocalSSRC(unsigned int ssrc) {
2308 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2309 "Channel::SetLocalSSRC()");
2310 if (channel_state_.Get().sending) {
2311 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2312 "SetLocalSSRC() already sending");
2313 return -1;
2314 }
2315 _rtpRtcpModule->SetSSRC(ssrc);
2316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002317}
2318
kwiberg55b97fe2016-01-28 05:22:45 -08002319int Channel::GetLocalSSRC(unsigned int& ssrc) {
2320 ssrc = _rtpRtcpModule->SSRC();
2321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002322}
2323
kwiberg55b97fe2016-01-28 05:22:45 -08002324int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2325 ssrc = rtp_receiver_->SSRC();
2326 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002327}
2328
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002329int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002330 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002331 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002332}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002333
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002334int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2335 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002336 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2337 if (enable &&
2338 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2339 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002340 return -1;
2341 }
2342 return 0;
2343}
2344
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002345void Channel::EnableSendTransportSequenceNumber(int id) {
2346 int ret =
2347 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2348 RTC_DCHECK_EQ(0, ret);
2349}
2350
stefan3313ec92016-01-21 06:32:43 -08002351void Channel::EnableReceiveTransportSequenceNumber(int id) {
2352 rtp_header_parser_->DeregisterRtpHeaderExtension(
2353 kRtpExtensionTransportSequenceNumber);
2354 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2355 kRtpExtensionTransportSequenceNumber, id);
2356 RTC_DCHECK(ret);
2357}
2358
stefanbba9dec2016-02-01 04:39:55 -08002359void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002360 RtpPacketSender* rtp_packet_sender,
2361 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002362 PacketRouter* packet_router,
2363 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002364 RTC_DCHECK(rtp_packet_sender);
2365 RTC_DCHECK(transport_feedback_observer);
2366 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002367 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002368 feedback_observer_proxy_->SetTransportFeedbackObserver(
2369 transport_feedback_observer);
2370 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2371 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2372 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002373 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002374 packet_router_ = packet_router;
2375}
2376
stefanbba9dec2016-02-01 04:39:55 -08002377void Channel::RegisterReceiverCongestionControlObjects(
2378 PacketRouter* packet_router) {
2379 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002380 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002381 packet_router_ = packet_router;
2382}
2383
2384void Channel::ResetCongestionControlObjects() {
2385 RTC_DCHECK(packet_router_);
2386 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002387 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002388 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2389 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002390 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002391 packet_router_ = nullptr;
2392 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2393}
2394
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002395void Channel::SetRTCPStatus(bool enable) {
2396 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2397 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002398 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002399}
2400
kwiberg55b97fe2016-01-28 05:22:45 -08002401int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002402 RtcpMode method = _rtpRtcpModule->RTCP();
2403 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002404 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002405}
2406
kwiberg55b97fe2016-01-28 05:22:45 -08002407int Channel::SetRTCP_CNAME(const char cName[256]) {
2408 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2409 "Channel::SetRTCP_CNAME()");
2410 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2411 _engineStatisticsPtr->SetLastError(
2412 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2413 "SetRTCP_CNAME() failed to set RTCP CNAME");
2414 return -1;
2415 }
2416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002417}
2418
kwiberg55b97fe2016-01-28 05:22:45 -08002419int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2420 if (cName == NULL) {
2421 _engineStatisticsPtr->SetLastError(
2422 VE_INVALID_ARGUMENT, kTraceError,
2423 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2424 return -1;
2425 }
2426 char cname[RTCP_CNAME_SIZE];
2427 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2428 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2429 _engineStatisticsPtr->SetLastError(
2430 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2431 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2432 return -1;
2433 }
2434 strcpy(cName, cname);
2435 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002436}
2437
kwiberg55b97fe2016-01-28 05:22:45 -08002438int Channel::SendApplicationDefinedRTCPPacket(
2439 unsigned char subType,
2440 unsigned int name,
2441 const char* data,
2442 unsigned short dataLengthInBytes) {
2443 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2444 "Channel::SendApplicationDefinedRTCPPacket()");
2445 if (!channel_state_.Get().sending) {
2446 _engineStatisticsPtr->SetLastError(
2447 VE_NOT_SENDING, kTraceError,
2448 "SendApplicationDefinedRTCPPacket() not sending");
2449 return -1;
2450 }
2451 if (NULL == data) {
2452 _engineStatisticsPtr->SetLastError(
2453 VE_INVALID_ARGUMENT, kTraceError,
2454 "SendApplicationDefinedRTCPPacket() invalid data value");
2455 return -1;
2456 }
2457 if (dataLengthInBytes % 4 != 0) {
2458 _engineStatisticsPtr->SetLastError(
2459 VE_INVALID_ARGUMENT, kTraceError,
2460 "SendApplicationDefinedRTCPPacket() invalid length value");
2461 return -1;
2462 }
2463 RtcpMode status = _rtpRtcpModule->RTCP();
2464 if (status == RtcpMode::kOff) {
2465 _engineStatisticsPtr->SetLastError(
2466 VE_RTCP_ERROR, kTraceError,
2467 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2468 return -1;
2469 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002470
kwiberg55b97fe2016-01-28 05:22:45 -08002471 // Create and schedule the RTCP APP packet for transmission
2472 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2473 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2474 _engineStatisticsPtr->SetLastError(
2475 VE_SEND_ERROR, kTraceError,
2476 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2477 return -1;
2478 }
2479 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002480}
2481
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002482int Channel::GetRemoteRTCPReportBlocks(
2483 std::vector<ReportBlock>* report_blocks) {
2484 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002485 _engineStatisticsPtr->SetLastError(
2486 VE_INVALID_ARGUMENT, kTraceError,
2487 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002488 return -1;
2489 }
2490
2491 // Get the report blocks from the latest received RTCP Sender or Receiver
2492 // Report. Each element in the vector contains the sender's SSRC and a
2493 // report block according to RFC 3550.
2494 std::vector<RTCPReportBlock> rtcp_report_blocks;
2495 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002496 return -1;
2497 }
2498
2499 if (rtcp_report_blocks.empty())
2500 return 0;
2501
2502 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2503 for (; it != rtcp_report_blocks.end(); ++it) {
2504 ReportBlock report_block;
2505 report_block.sender_SSRC = it->remoteSSRC;
2506 report_block.source_SSRC = it->sourceSSRC;
2507 report_block.fraction_lost = it->fractionLost;
2508 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2509 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2510 report_block.interarrival_jitter = it->jitter;
2511 report_block.last_SR_timestamp = it->lastSR;
2512 report_block.delay_since_last_SR = it->delaySinceLastSR;
2513 report_blocks->push_back(report_block);
2514 }
2515 return 0;
2516}
2517
kwiberg55b97fe2016-01-28 05:22:45 -08002518int Channel::GetRTPStatistics(CallStatistics& stats) {
2519 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002520
kwiberg55b97fe2016-01-28 05:22:45 -08002521 // The jitter statistics is updated for each received RTP packet and is
2522 // based on received packets.
2523 RtcpStatistics statistics;
2524 StreamStatistician* statistician =
2525 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002526 if (statistician) {
2527 statistician->GetStatistics(&statistics,
2528 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002529 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002530
kwiberg55b97fe2016-01-28 05:22:45 -08002531 stats.fractionLost = statistics.fraction_lost;
2532 stats.cumulativeLost = statistics.cumulative_lost;
2533 stats.extendedMax = statistics.extended_max_sequence_number;
2534 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002535
kwiberg55b97fe2016-01-28 05:22:45 -08002536 // --- RTT
2537 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002538
kwiberg55b97fe2016-01-28 05:22:45 -08002539 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002540
kwiberg55b97fe2016-01-28 05:22:45 -08002541 size_t bytesSent(0);
2542 uint32_t packetsSent(0);
2543 size_t bytesReceived(0);
2544 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002545
kwiberg55b97fe2016-01-28 05:22:45 -08002546 if (statistician) {
2547 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2548 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002549
kwiberg55b97fe2016-01-28 05:22:45 -08002550 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2551 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2552 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2553 " output will not be complete");
2554 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002555
kwiberg55b97fe2016-01-28 05:22:45 -08002556 stats.bytesSent = bytesSent;
2557 stats.packetsSent = packetsSent;
2558 stats.bytesReceived = bytesReceived;
2559 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002560
kwiberg55b97fe2016-01-28 05:22:45 -08002561 // --- Timestamps
2562 {
2563 rtc::CritScope lock(&ts_stats_lock_);
2564 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2565 }
2566 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002567}
2568
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002569int Channel::SetCodecFECStatus(bool enable) {
2570 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2571 "Channel::SetCodecFECStatus()");
2572
kwibergc8d071e2016-04-06 12:22:38 -07002573 if (!codec_manager_.SetCodecFEC(enable) ||
2574 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002575 _engineStatisticsPtr->SetLastError(
2576 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2577 "SetCodecFECStatus() failed to set FEC state");
2578 return -1;
2579 }
2580 return 0;
2581}
2582
2583bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002584 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002585}
2586
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002587void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2588 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002589 // If pacing is enabled we always store packets.
2590 if (!pacing_enabled_)
2591 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002592 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002593 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002594 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002595 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002596 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002597}
2598
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002599// Called when we are missing one or more packets.
2600int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002601 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2602}
2603
kwiberg55b97fe2016-01-28 05:22:45 -08002604uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2605 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2606 "Channel::Demultiplex()");
2607 _audioFrame.CopyFrom(audioFrame);
2608 _audioFrame.id_ = _channelId;
2609 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002610}
2611
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002612void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002613 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002614 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002615 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002616 CodecInst codec;
2617 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002618
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002619 // Never upsample or upmix the capture signal here. This should be done at the
2620 // end of the send chain.
2621 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2622 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2623 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2624 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002625}
2626
kwiberg55b97fe2016-01-28 05:22:45 -08002627uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2628 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2629 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002630
kwiberg55b97fe2016-01-28 05:22:45 -08002631 if (_audioFrame.samples_per_channel_ == 0) {
2632 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2633 "Channel::PrepareEncodeAndSend() invalid audio frame");
2634 return 0xFFFFFFFF;
2635 }
2636
2637 if (channel_state_.Get().input_file_playing) {
2638 MixOrReplaceAudioWithFile(mixingFrequency);
2639 }
2640
solenberg1c2af8e2016-03-24 10:36:00 -07002641 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2642 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002643
kwiberg55b97fe2016-01-28 05:22:45 -08002644 if (_includeAudioLevelIndication) {
2645 size_t length =
2646 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002647 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002648 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002649 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002650 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002651 rms_level_.Analyze(
2652 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002653 }
kwiberg55b97fe2016-01-28 05:22:45 -08002654 }
solenberg1c2af8e2016-03-24 10:36:00 -07002655 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002656
kwiberg55b97fe2016-01-28 05:22:45 -08002657 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002658}
2659
kwiberg55b97fe2016-01-28 05:22:45 -08002660uint32_t Channel::EncodeAndSend() {
2661 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2662 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002663
kwiberg55b97fe2016-01-28 05:22:45 -08002664 assert(_audioFrame.num_channels_ <= 2);
2665 if (_audioFrame.samples_per_channel_ == 0) {
2666 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2667 "Channel::EncodeAndSend() invalid audio frame");
2668 return 0xFFFFFFFF;
2669 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002670
kwiberg55b97fe2016-01-28 05:22:45 -08002671 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002672
kwiberg55b97fe2016-01-28 05:22:45 -08002673 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002674
kwiberg55b97fe2016-01-28 05:22:45 -08002675 // The ACM resamples internally.
2676 _audioFrame.timestamp_ = _timeStamp;
2677 // This call will trigger AudioPacketizationCallback::SendData if encoding
2678 // is done and payload is ready for packetization and transmission.
2679 // Otherwise, it will return without invoking the callback.
2680 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2681 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2682 "Channel::EncodeAndSend() ACM encoding failed");
2683 return 0xFFFFFFFF;
2684 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002685
kwiberg55b97fe2016-01-28 05:22:45 -08002686 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2687 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002688}
2689
solenberg7602aab2016-11-14 11:30:07 -08002690void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2691 RTC_DCHECK(!channel.channel() ||
2692 channel.channel()->ChannelId() != _channelId);
2693 rtc::CritScope lock(&assoc_send_channel_lock_);
2694 associate_send_channel_ = channel;
2695}
2696
Minyue2013aec2015-05-13 14:14:42 +02002697void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002698 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002699 Channel* channel = associate_send_channel_.channel();
2700 if (channel && channel->ChannelId() == channel_id) {
2701 // If this channel is associated with a send channel of the specified
2702 // Channel ID, disassociate with it.
2703 ChannelOwner ref(NULL);
2704 associate_send_channel_ = ref;
2705 }
2706}
2707
ivoc14d5dbe2016-07-04 07:06:55 -07002708void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2709 event_log_proxy_->SetEventLog(event_log);
2710}
2711
michaelt9332b7d2016-11-30 07:51:13 -08002712void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2713 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2714}
2715
nisse284542b2017-01-10 08:58:32 -08002716void Channel::UpdateOverheadForEncoder() {
2717 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2718 if (*encoder) {
2719 (*encoder)->OnReceivedOverhead(transport_overhead_per_packet_ +
2720 rtp_overhead_per_packet_);
2721 }
2722 });
2723}
2724
2725void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
2726 transport_overhead_per_packet_ = transport_overhead_per_packet;
2727 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002728}
2729
michaeltbf65be52016-12-15 06:24:49 -08002730void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
nisse284542b2017-01-10 08:58:32 -08002731 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2732 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002733}
2734
kwiberg55b97fe2016-01-28 05:22:45 -08002735int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2736 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002737}
2738
wu@webrtc.org24301a62013-12-13 19:17:43 +00002739void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2740 audio_coding_->GetDecodingCallStatistics(stats);
2741}
2742
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002743bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2744 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002745 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002746 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002747 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002748 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002749}
2750
solenberg358057b2015-11-27 10:46:42 -08002751uint32_t Channel::GetDelayEstimate() const {
2752 int jitter_buffer_delay_ms = 0;
2753 int playout_buffer_delay_ms = 0;
2754 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2755 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2756}
2757
deadbeef74375882015-08-13 12:09:10 -07002758int Channel::LeastRequiredDelayMs() const {
2759 return audio_coding_->LeastRequiredDelayMs();
2760}
2761
kwiberg55b97fe2016-01-28 05:22:45 -08002762int Channel::SetMinimumPlayoutDelay(int delayMs) {
2763 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2764 "Channel::SetMinimumPlayoutDelay()");
2765 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2766 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2767 _engineStatisticsPtr->SetLastError(
2768 VE_INVALID_ARGUMENT, kTraceError,
2769 "SetMinimumPlayoutDelay() invalid min delay");
2770 return -1;
2771 }
2772 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2773 _engineStatisticsPtr->SetLastError(
2774 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2775 "SetMinimumPlayoutDelay() failed to set min playout delay");
2776 return -1;
2777 }
2778 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002779}
2780
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002781int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002782 uint32_t playout_timestamp_rtp = 0;
2783 {
tommi31fc21f2016-01-21 10:37:37 -08002784 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002785 playout_timestamp_rtp = playout_timestamp_rtp_;
2786 }
kwiberg55b97fe2016-01-28 05:22:45 -08002787 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002788 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002789 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002790 "GetPlayoutTimestamp() failed to retrieve timestamp");
2791 return -1;
2792 }
deadbeef74375882015-08-13 12:09:10 -07002793 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002794 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002795}
2796
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002797int Channel::SetInitTimestamp(unsigned int timestamp) {
2798 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002799 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002800 if (channel_state_.Get().sending) {
2801 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2802 "SetInitTimestamp() already sending");
2803 return -1;
2804 }
2805 _rtpRtcpModule->SetStartTimestamp(timestamp);
2806 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002807}
2808
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002809int Channel::SetInitSequenceNumber(short sequenceNumber) {
2810 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2811 "Channel::SetInitSequenceNumber()");
2812 if (channel_state_.Get().sending) {
2813 _engineStatisticsPtr->SetLastError(
2814 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
2815 return -1;
2816 }
2817 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
2818 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002819}
2820
kwiberg55b97fe2016-01-28 05:22:45 -08002821int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2822 RtpReceiver** rtp_receiver) const {
2823 *rtpRtcpModule = _rtpRtcpModule.get();
2824 *rtp_receiver = rtp_receiver_.get();
2825 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002826}
2827
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002828// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2829// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002830int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002831 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002832 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002833
kwiberg55b97fe2016-01-28 05:22:45 -08002834 {
2835 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002836
kwiberg5a25d952016-08-17 07:31:12 -07002837 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002838 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2839 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2840 " doesnt exist");
2841 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002842 }
2843
kwiberg4ec01d92016-08-22 08:43:54 -07002844 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002845 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002846 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2847 "Channel::MixOrReplaceAudioWithFile() file mixing "
2848 "failed");
2849 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002850 }
kwiberg55b97fe2016-01-28 05:22:45 -08002851 if (fileSamples == 0) {
2852 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2853 "Channel::MixOrReplaceAudioWithFile() file is ended");
2854 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002855 }
kwiberg55b97fe2016-01-28 05:22:45 -08002856 }
2857
2858 assert(_audioFrame.samples_per_channel_ == fileSamples);
2859
2860 if (_mixFileWithMicrophone) {
2861 // Currently file stream is always mono.
2862 // TODO(xians): Change the code when FilePlayer supports real stereo.
2863 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2864 1, fileSamples);
2865 } else {
2866 // Replace ACM audio with file.
2867 // Currently file stream is always mono.
2868 // TODO(xians): Change the code when FilePlayer supports real stereo.
2869 _audioFrame.UpdateFrame(
2870 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2871 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2872 }
2873 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002874}
2875
kwiberg55b97fe2016-01-28 05:22:45 -08002876int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2877 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002878
kwibergb7f89d62016-02-17 10:04:18 -08002879 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002880 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002881
kwiberg55b97fe2016-01-28 05:22:45 -08002882 {
2883 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002884
kwiberg5a25d952016-08-17 07:31:12 -07002885 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002886 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2887 "Channel::MixAudioWithFile() file mixing failed");
2888 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002889 }
2890
kwiberg55b97fe2016-01-28 05:22:45 -08002891 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002892 if (output_file_player_->Get10msAudioFromFile(
2893 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002894 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2895 "Channel::MixAudioWithFile() file mixing failed");
2896 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002897 }
kwiberg55b97fe2016-01-28 05:22:45 -08002898 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002899
kwiberg55b97fe2016-01-28 05:22:45 -08002900 if (audioFrame.samples_per_channel_ == fileSamples) {
2901 // Currently file stream is always mono.
2902 // TODO(xians): Change the code when FilePlayer supports real stereo.
2903 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2904 fileSamples);
2905 } else {
2906 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2907 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2908 ") != "
2909 "fileSamples(%" PRIuS ")",
2910 audioFrame.samples_per_channel_, fileSamples);
2911 return -1;
2912 }
2913
2914 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002915}
2916
deadbeef74375882015-08-13 12:09:10 -07002917void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002918 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002919
henrik.lundin96bd5022016-04-06 04:13:56 -07002920 if (!jitter_buffer_playout_timestamp_) {
2921 // This can happen if this channel has not received any RTP packets. In
2922 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002923 return;
2924 }
2925
2926 uint16_t delay_ms = 0;
2927 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002928 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002929 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2930 " delay from the ADM");
2931 _engineStatisticsPtr->SetLastError(
2932 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2933 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2934 return;
2935 }
2936
henrik.lundin96bd5022016-04-06 04:13:56 -07002937 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2938 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002939
2940 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002941 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002942
kwiberg55b97fe2016-01-28 05:22:45 -08002943 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002944 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002945 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002946
2947 {
tommi31fc21f2016-01-21 10:37:37 -08002948 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002949 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002950 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002951 }
2952 playout_delay_ms_ = delay_ms;
2953 }
2954}
2955
kwiberg55b97fe2016-01-28 05:22:45 -08002956void Channel::RegisterReceiveCodecsToRTPModule() {
2957 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2958 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002959
kwiberg55b97fe2016-01-28 05:22:45 -08002960 CodecInst codec;
2961 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002962
kwiberg55b97fe2016-01-28 05:22:45 -08002963 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2964 // Open up the RTP/RTCP receiver for all supported codecs
2965 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002966 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002967 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2968 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2969 " to register %s (%d/%d/%" PRIuS
2970 "/%d) to RTP/RTCP "
2971 "receiver",
2972 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2973 codec.rate);
2974 } else {
2975 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2976 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2977 "(%d/%d/%" PRIuS
2978 "/%d) has been added to the RTP/RTCP "
2979 "receiver",
2980 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2981 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002982 }
kwiberg55b97fe2016-01-28 05:22:45 -08002983 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002984}
2985
kwiberg55b97fe2016-01-28 05:22:45 -08002986int Channel::SetSendRtpHeaderExtension(bool enable,
2987 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002988 unsigned char id) {
2989 int error = 0;
2990 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
2991 if (enable) {
2992 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
2993 }
2994 return error;
2995}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002996
ossue280cde2016-10-12 11:04:10 -07002997int Channel::GetRtpTimestampRateHz() const {
2998 const auto format = audio_coding_->ReceiveFormat();
2999 // Default to the playout frequency if we've not gotten any packets yet.
3000 // TODO(ossu): Zero clockrate can only happen if we've added an external
3001 // decoder for a format we don't support internally. Remove once that way of
3002 // adding decoders is gone!
3003 return (format && format->clockrate_hz != 0)
3004 ? format->clockrate_hz
3005 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003006}
3007
Minyue2013aec2015-05-13 14:14:42 +02003008int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003009 RtcpMode method = _rtpRtcpModule->RTCP();
3010 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003011 return 0;
3012 }
3013 std::vector<RTCPReportBlock> report_blocks;
3014 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003015
3016 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003017 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003018 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003019 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003020 Channel* channel = associate_send_channel_.channel();
3021 // Tries to get RTT from an associated channel. This is important for
3022 // receive-only channels.
3023 if (channel) {
3024 // To prevent infinite recursion and deadlock, calling GetRTT of
3025 // associate channel should always use "false" for argument:
3026 // |allow_associate_channel|.
3027 rtt = channel->GetRTT(false);
3028 }
3029 }
3030 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003031 }
3032
3033 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3034 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3035 for (; it != report_blocks.end(); ++it) {
3036 if (it->remoteSSRC == remoteSSRC)
3037 break;
3038 }
3039 if (it == report_blocks.end()) {
3040 // We have not received packets with SSRC matching the report blocks.
3041 // To calculate RTT we try with the SSRC of the first report block.
3042 // This is very important for send-only channels where we don't know
3043 // the SSRC of the other end.
3044 remoteSSRC = report_blocks[0].remoteSSRC;
3045 }
Minyue2013aec2015-05-13 14:14:42 +02003046
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003047 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003048 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003049 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003050 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3051 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003052 return 0;
3053 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003054 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003055}
3056
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003057} // namespace voe
3058} // namespace webrtc